| 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 | 11 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 11 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 11 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 11 | extends 'Business::Monzo::Resource'; | |||||||||||||
| 18 | 11 | with 'Business::Monzo::Utils'; | |||||||||||||
| 19 | 11 | with 'Business::Monzo::Currency'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 11 | ▶ | use Types::Standard qw/ :all /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 11 | ▶ | use Business::Monzo::Merchant; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 11 | ▶ | use Business::Monzo::Attachment; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 11 | ▶ | use DateTime::Format::DateParse; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 11 | ▶ | use Locale::Currency::Format; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 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 | 11 | has [ qw/ | |||||||||||||
| 62 | id account_id category dedupe_id description notes scheme | ||||||||||||||
| 63 | / ] => ( | ||||||||||||||
| 64 | is => 'ro', | ||||||||||||||
| 65 | isa => Str, | ||||||||||||||
| 66 | ); | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 11 | has [ qw/ account_balance amount local_amount / ] => ( | |||||||||||||
| 69 | is => 'ro', | ||||||||||||||
| 70 | isa => Int, | ||||||||||||||
| 71 | ); | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 11 | has [ qw/ counterparty metadata / ] => ( | |||||||||||||
| 74 | is => 'ro', | ||||||||||||||
| 75 | isa => HashRef, | ||||||||||||||
| 76 | ); | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 11 | 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 | 5 | ▶ | coerce => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||
| 87 | 5 | my ( $args ) = @_; | |||||||||||||
| 88 | |||||||||||||||
| 89 | 5 | ▶ | return undef if ! defined $args; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | |||||||||||||||
| 91 | 5 | ▶ | if ( ref( $args ) eq 'HASH' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 92 | 3 | ▶ | return undef if ! keys %{ $args }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | $args = Business::Monzo::Merchant->new( | ||||||||||||||
| 94 | client => $Business::Monzo::Resource::client, | ||||||||||||||
| 95 | 3 | %{ $args }, | |||||||||||||
| 96 | ); | ||||||||||||||
| 97 | } elsif ( ! ref( $args ) ) { | ||||||||||||||
| 98 | 2 | $args = Business::Monzo::Merchant->new( | |||||||||||||
| 99 | client => $Business::Monzo::Resource::client, | ||||||||||||||
| 100 | id => $args, | ||||||||||||||
| 101 | ); | ||||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 5 | return $args; | |||||||||||||
| 105 | }, | ||||||||||||||
| 106 | 11 | ); | |||||||||||||
| 107 | |||||||||||||||
| 108 | has attachments => ( | ||||||||||||||
| 109 | is => 'ro', | ||||||||||||||
| 110 | isa => Maybe[ArrayRef[InstanceOf['Business::Monzo::Attachment']]], | ||||||||||||||
| 111 | 2 | ▶ | coerce => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 112 | 2 | my ( $args ) = @_; | |||||||||||||
| 113 | |||||||||||||||
| 114 | 2 | ▶ | return undef if ! defined $args; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | |||||||||||||||
| 116 | 2 | my @attachments; | |||||||||||||
| 117 | |||||||||||||||
| 118 | 2 | foreach my $attachment ( @{ $args } ) { | |||||||||||||
| 119 | push( @attachments,Business::Monzo::Attachment->new( | ||||||||||||||
| 120 | client => $Business::Monzo::Resource::client, | ||||||||||||||
| 121 | 4 | %{ $attachment }, | |||||||||||||
| 122 | ) ); | ||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 2 | return [ @attachments ]; | |||||||||||||
| 126 | }, | ||||||||||||||
| 127 | 11 | ); | |||||||||||||
| 128 | |||||||||||||||
| 129 | has [ qw/ created updated settled / ] => ( | ||||||||||||||
| 130 | is => 'ro', | ||||||||||||||
| 131 | isa => Maybe[InstanceOf['DateTime']], | ||||||||||||||
| 132 | 10 | ▶ | coerce => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||
| 133 | 10 | my ( $args ) = @_; | |||||||||||||
| 134 | |||||||||||||||
| 135 | 10 | ▶ | if ( ! ref( $args ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 10 | $args = DateTime::Format::DateParse->parse_datetime( $args ); | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 10 | return $args; | |||||||||||||
| 140 | }, | ||||||||||||||
| 141 | 11 | ); | |||||||||||||
| 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 | 1 | ▶ | sub get { | ||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 158 | 1 | 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 | 1 | ▶ | sub annotate { | ||||||||||||
|
Subroutine
annotate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 173 | 1 | my ( $self,%annotations ) = @_; | |||||||||||||
| 174 | |||||||||||||||
| 175 | 1 | %annotations = $self->_params_as_array_string( 'metadata',\%annotations ); | |||||||||||||
| 176 | |||||||||||||||
| 177 | 1 | my $data = $self->client->api_patch( $self->url,\%annotations ); | |||||||||||||
| 178 | 1 | $data = $data->{transaction}; | |||||||||||||
| 179 | |||||||||||||||
| 180 | return $self->new( | ||||||||||||||
| 181 | client => $self->client, | ||||||||||||||
| 182 | 1 | %{ $data }, | |||||||||||||
| 183 | ); | ||||||||||||||
| 184 | } | ||||||||||||||
| 185 | |||||||||||||||
| 186 | 1 | ▶ | sub annotations { | ||||||||||||
|
Subroutine
annotations: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 187 | 1 | return shift->metadata; | |||||||||||||
| 188 | } | ||||||||||||||
| 189 | |||||||||||||||
| 190 | 6 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 191 | 6 | my ( $self,$args ) = @_; | |||||||||||||
| 192 | |||||||||||||||
| 193 | 6 | foreach my $c ( 'local_','' ) { | |||||||||||||
| 194 | |||||||||||||||
| 195 | 12 | my $amount_accessor = "${c}amount"; | |||||||||||||
| 196 | 12 | my $currency_accessor = "${c}currency"; | |||||||||||||
| 197 | |||||||||||||||
| 198 | 12 | ▶ | if ( my $amount = $self->$amount_accessor ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 199 | # not all currencies have sub units, so default to 0 for exponent | ||||||||||||||
| 200 | 5 | ▶ | my $decimal_precision = decimal_precision( $self->$currency_accessor->code ) // 0; | ||||||||||||
|
Condition50%1/2
decimal_precision($self->$currency_accessor->code) // 0
MC/DC0%0/1
decimal_precision($self->$currency_accessor->code) // 0
decimal_precision($self->$currency_accessor->code)
Truth table50%1/2
A
A= decimal_precision($self->$currency_accessor->code)
| |||||||||||||||
| 201 | 5 | my $value = $amount / ( 10 ** $decimal_precision ); | |||||||||||||
| 202 | 5 | $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 | 11 | 1; | |||||||||||||
| 229 | |||||||||||||||
| 230 | # vim: ts=4:sw=4:et | ||||||||||||||