File Coverage

blib/lib/Business/Mondo/Transaction.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 2 3 66.6
total 41 42 97.6


line stmt bran cond sub pod time code
1             package Business::Mondo::Transaction;
2              
3             =head1 NAME
4              
5             Business::Mondo::Transaction
6              
7             =head1 DESCRIPTION
8              
9             A class for a Mondo transaction, extends L<Business::Mondo::Resource>
10              
11             =cut
12              
13 11     11   49 use strict;
  11         12  
  11         244  
14 11     11   30 use warnings;
  11         12  
  11         192  
15              
16 11     11   28 use Moo;
  11         10  
  11         37  
17             extends 'Business::Mondo::Resource';
18             with 'Business::Mondo::Utils';
19             with 'Business::Mondo::Currency';
20              
21 11     11   7362 use Types::Standard qw/ :all /;
  11         548255  
  11         77  
22 11     11   268717 use Business::Mondo::Merchant;
  11         26  
  11         329  
23 11     11   3793 use Business::Mondo::Attachment;
  11         27  
  11         318  
24 11     11   53 use DateTime::Format::DateParse;
  11         14  
  11         4428  
25              
26             =head1 ATTRIBUTES
27              
28             The Transaction class has the following attributes (with their type).
29              
30             id (Str)
31             account_id (Str)
32             category (Str)
33             dedupe_id (Str)
34             description (Str)
35             notes (Str)
36             scheme (Str)
37             account_balance (Int)
38             amount (Int)
39             local_amount (Int)
40             counterparty (HashRef)
41             metadata (HashRef)
42             is_load (Bool)
43             originator (Bool)
44             merchant (Business::Mondo::Merchant)
45             currency (Data::Currency)
46             local_currency (Data::Currency)
47             created (DateTime)
48             updated (DateTime)
49             settled (DateTime)
50             attachments (ArrayRef[Business::Mondo::Attachment])
51              
52             Note that if a HashRef or Str is passed to ->merchant it will be coerced
53             into a Business::Mondo::Merchant object. When a Str is passed to ->currency
54             / ->local_currency this will be coerced to a Data::Currency object, and
55             when a Str is passed to ->created / ->updated / ->settled this will be
56             coerced to a DateTime object.
57              
58             =cut
59              
60             has [ qw/
61             id account_id category dedupe_id description notes scheme
62             / ] => (
63             is => 'ro',
64             isa => Str,
65             );
66              
67             has [ qw/ account_balance amount local_amount / ] => (
68             is => 'ro',
69             isa => Int,
70             );
71              
72             has [ qw/ counterparty metadata / ] => (
73             is => 'ro',
74             isa => HashRef,
75             );
76              
77             has [ qw/ is_load originator / ] => (
78             is => 'ro',
79             isa => Any,
80             );
81              
82             has merchant => (
83             is => 'ro',
84             isa => Maybe[InstanceOf['Business::Mondo::Merchant']],
85             coerce => sub {
86             my ( $args ) = @_;
87              
88             return undef if ! defined $args;
89              
90             if ( ref( $args ) eq 'HASH' ) {
91             return undef if ! keys %{ $args };
92             $args = Business::Mondo::Merchant->new(
93             client => $Business::Mondo::Resource::client,
94             %{ $args },
95             );
96             } elsif ( ! ref( $args ) ) {
97             $args = Business::Mondo::Merchant->new(
98             client => $Business::Mondo::Resource::client,
99             id => $args,
100             );
101             }
102              
103             return $args;
104             },
105             );
106              
107             has attachments => (
108             is => 'ro',
109             isa => Maybe[ArrayRef[InstanceOf['Business::Mondo::Attachment']]],
110             coerce => sub {
111             my ( $args ) = @_;
112              
113             return undef if ! defined $args;
114              
115             my @attachments;
116              
117             foreach my $attachment ( @{ $args } ) {
118             push( @attachments,Business::Mondo::Attachment->new(
119             client => $Business::Mondo::Resource::client,
120             %{ $attachment },
121             ) );
122             }
123              
124             return [ @attachments ];
125             },
126             );
127              
128             has [ qw/ created updated settled / ] => (
129             is => 'ro',
130             isa => Maybe[InstanceOf['DateTime']],
131             coerce => sub {
132             my ( $args ) = @_;
133              
134             if ( ! ref( $args ) ) {
135             $args = DateTime::Format::DateParse->parse_datetime( $args );
136             }
137              
138             return $args;
139             },
140             );
141              
142             =head1 Operations on an transaction
143              
144             =head2 get
145              
146             Returns a new instance of the object populated with the attributes having called
147             the API
148              
149             my $populated_transaction = $transaction->get;
150              
151             This is for when you have instantiated an object with the id, so calling the API
152             will retrieve the full details for the entity.
153              
154             =cut
155              
156             sub get {
157 1     1 1 7 shift->SUPER::get( 'transaction' );
158             }
159              
160             =head2 annotate
161              
162             Returns a new instance of the object with annotated data having called the API
163              
164             my $annotated_transaction = $transaction->annotate(
165             foo => "bar",
166             baz => "boz,
167             );
168              
169             =cut
170              
171             sub annotate {
172 1     1 1 1059 my ( $self,%annotations ) = @_;
173              
174 1         6 %annotations = $self->_params_as_array_string( 'metadata',\%annotations );
175              
176 1         23 my $data = $self->client->api_patch( $self->url,\%annotations );
177 1         38 $data = $data->{transaction};
178              
179             return $self->new(
180             client => $self->client,
181 1         4 %{ $data },
  1         19  
182             );
183             }
184              
185             sub annotations {
186 1     1 0 243 return shift->metadata;
187             }
188              
189             =head1 SEE ALSO
190              
191             L<Business::Mondo>
192              
193             L<Business::Mondo::Resource>
194              
195             =head1 AUTHOR
196              
197             Lee Johnson - C<leejo@cpan.org>
198              
199             =head1 LICENSE
200              
201             This library is free software; you can redistribute it and/or modify it under
202             the same terms as Perl itself. If you would like to contribute documentation,
203             features, bug fixes, or anything else then please raise an issue / pull request:
204              
205             https://github.com/leejo/business-mondo
206              
207             =cut
208              
209             1;
210              
211             # vim: ts=4:sw=4:et