File Coverage

blib/lib/Business/Monzo/Transaction.pm
Criterion Covered Total %
statement 40 40 100.0
branch 2 2 100.0
condition 1 2 50.0
subroutine 12 12 100.0
pod 2 4 50.0
total 57 60 95.0


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