GoCardless/Payment.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 2 / 2 Conditioncond 62.5% 5 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 22 / 22 Podpod 31.2% 5 / 16 totaltotal 82.2% 79 / 96 CC 6 SCAR 6.0
CC
6
Cov
94%
refund
5.0
_link_content
2.0
BEGIN
1.0
CRAP 1.8
« GoCardless/InstalmentSchedule.pm ↑ Summary GoCardless/Webhook.pm »
1 package Business::GoCardless::Payment;
2  
3 =head1 NAME
4  
5 Business::GoCardless::Payment
6  
7 =head1 DESCRIPTION
8  
9 A class for a gocardless payment, extends L<Business::GoCardless::Resource>
10  
11 =cut
12  
13 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 extends 'Business::GoCardless::Resource';
19  
20 7 ▶use Business::GoCardless::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Business::GoCardless::Refund;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 ATTRIBUTES
24  
25 amount
26 amount_refunded
27 charge_date
28 created_at
29 currency
30 description
31 faster_ach
32 fx
33 id
34 links
35 metadata
36 reference
37 retry_if_possible
38 scheme
39 status
40  
41 =cut
42  
43 7 has [ qw/
44 amount
45 amount_refunded
46 charge_date
47 created_at
48 currency
49 description
50 faster_ach
51 fx
52 id
53 links
54 metadata
55 reference
56 retry_if_possible
57 scheme
58 status
59 / ] => (
60 is => 'rw',
61 );
62  
63 =head1 Operations on a payment
64  
65 retry
66 cancel
67 update
68 refund
69  
70 $Payment->retry if $Payment->failed;
71  
72 retry and cancel optionally take metadata:
73  
74 $Payment->cancel( metadata => { ... } );
75  
76 =head2 update
77  
78 Update the payment's metadata and/or retry_if_possible, returns the updated
79 Payment object:
80  
81 $Payment->update( metadata => { ... },retry_if_possible => 1 );
82  
83 =head2 refund
84  
85 Create a refund against the payment, returns a L<Business::GoCardless::Refund>
86 object. Note that refunds are restricted by default on GoCardless accounts and
87 must be enabled by GoCardless support before this will work.
88  
89 my $Refund = $Payment->refund(
90 amount => 100, # required
91 total_amount_confirmation => 100, # defaults to amount_refunded + amount
92 reference => 'REFUND001', # optional
93 metadata => { ... }, # optional
94 );
95  
96 total_amount_confirmation is a safety check used by GoCardless to prevent
97 accidental duplicate refunds - it is the total amount refunded on the payment
98 once this refund is applied. If the payment object is stale (i.e. another refund
99 has since been made) then GoCardless will reject the refund.
100  
101 =cut
102  
103 2 ▶sub retry { shift->_action( 'retry',@_ ); }
Subroutine retry: called CC=1 SCAR=0.0
Pod undocumented
104 2 ▶sub cancel { shift->_action( 'cancel',@_ ); }
Subroutine cancel: called CC=1 SCAR=0.0
Pod undocumented
105 1 ▶sub update { shift->_update( @_ ); }
Subroutine update: called CC=1 SCAR=0.0
Pod documented
106  
107 3 ▶sub refund {
Subroutine refund: called CC=5 SCAR=16.2
Pod documented
108 3 my ( $self,%params ) = @_;
109  
110 # no default for the amount, we don't want to refund anything by accident
111 Business::GoCardless::Exception->throw({
112 message => "amount is required for ->refund",
113 3 ▶ }) if ! defined( $params{amount} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"amount"} 1 2 3
114  
115 2 ▶ my $already_refunded = $self->amount_refunded // 0;
Condition50%1/2
$self->amount_refunded // 0
l!l
20
MC/DC0%0/1
$self->amount_refunded // 0
$self->amount_refunded
Truth table50%1/2
A
A= $self->amount_refunded
Aresult
11
00
116 2 my $amount = $params{amount};
117  
118 my $data = $self->client->api_post(
119 '/refunds',
120 {
121 refunds => {
122 %params,
123 amount => $amount,
124 total_amount_confirmation => $params{total_amount_confirmation}
125 // ( $already_refunded + $amount ),
126 links => {
127 2 ▶ %{ $params{links} // {} },
Condition75%3/4
$params{"total_amount_confirmation"} // $already_refunded + $amount
l!l
11
$params{"links"} // {}
l!l
02
MC/DC100%1/1
$params{"total_amount_confirmation"} // $already_refunded + $amount
$params{"total_amount_confirmation"}
MC/DC0%0/1
$params{"links"} // {}
$params{"links"}
Truth table100%2/2
A
A= $params{"total_amount_confirmation"}
Aresult
11
00
Truth table50%1/2
A
A= $params{"links"}
Aresult
11
00
128 payment => $self->id,
129 },
130 },
131 },
132 );
133  
134 return Business::GoCardless::Refund->new(
135 client => $self->client,
136 2 %{ $data->{refunds} },
137 );
138 }
139  
140 =head1 Status checks on a payment
141  
142 pending
143 paid
144 failed
145 chargedback
146 cancelled
147 withdrawn
148 refunded
149 submitted
150 confirmed
151  
152 if ( $Payment->failed ) {
153 ...
154 }
155  
156 =cut
157  
158 1 ▶sub pending { return shift->status =~ /pending/ }
Subroutine pending: called CC=1 SCAR=0.0
Pod undocumented
159 1 ▶sub paid { return shift->status eq 'paid_out' }
Subroutine paid: called CC=1 SCAR=0.0
Pod undocumented
160 1 ▶sub failed { return shift->status eq 'failed' }
Subroutine failed: called CC=1 SCAR=0.0
Pod undocumented
161 1 ▶sub chargedback { return shift->status eq 'charged_back' }
Subroutine chargedback: called CC=1 SCAR=0.0
Pod undocumented
162 2 ▶sub cancelled { return shift->status eq 'cancelled' }
Subroutine cancelled: called CC=1 SCAR=0.0
Pod undocumented
163 2 ▶sub withdrawn { return shift->status eq 'customer_approval_denied' }
Subroutine withdrawn: called CC=1 SCAR=0.0
Pod undocumented
164 1 ▶sub refunded { return shift->status eq 'refunded' }
Subroutine refunded: called CC=1 SCAR=0.0
Pod undocumented
165 1 ▶sub submitted { return shift->status eq 'submitted' }
Subroutine submitted: called CC=1 SCAR=0.0
Pod undocumented
166 1 ▶sub confirmed { return shift->status eq 'confirmed' }
Subroutine confirmed: called CC=1 SCAR=0.0
Pod undocumented
167  
168 =head1 Links
169  
170 Accessors for details found in the C<links> section
171  
172 =head2 payout_id
173  
174 The ID of the payout the payment was paid out in
175  
176 =head2 mandate_id
177  
178 The ID of the mandate the payment was collected against
179  
180 =head2 creditor_id
181  
182 The ID of the creditor receiving the payment
183  
184 =cut
185  
186 2 ▶sub payout_id { return shift->_link_content( 'payout' ) }
Subroutine payout_id: called CC=1 SCAR=0.0
Pod documented
187 1 ▶sub mandate_id { return shift->_link_content( 'mandate' ) }
Subroutine mandate_id: called CC=1 SCAR=0.0
Pod documented
188 1 ▶sub creditor_id { return shift->_link_content( 'creditor' ) }
Subroutine creditor_id: called CC=1 SCAR=0.0
Pod documented
189  
190 4 ▶sub _link_content {
Subroutine _link_content: called CC=2 SCAR=7.1
191 4 my ( $self,$attr ) = @_;
192 4 ▶ my $links = $self->links || {};
Condition50%1/2
$self->links || {}
l!l
40
MC/DC0%0/1
$self->links || {}
$self->links
Truth table50%1/2
A
A= $self->links
Aresult
11
00
193 4 return $links->{$attr};
194 }
195  
196 =head1 AUTHOR
197  
198 Lee Johnson - C<lee@payprop.com>
199  
200 This library is free software; you can redistribute it and/or modify it under
201 the same terms as Perl itself. If you would like to contribute documentation,
202 features, bug fixes, or anything else then please raise an issue / pull request:
203  
204 https://github.com/payprop/business-gocardless
205  
206 =cut
207  
208 7 1;
209  
210 # vim: ts=4:sw=4:et
« GoCardless/InstalmentSchedule.pm ↑ Summary GoCardless/Webhook.pm »