GoCardless/Pro.pm

filter: Statementstmt 97.5% 201 / 206 Branchbran 65.6% 42 / 64 Conditioncond 62.2% 28 / 45 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 98.1% 52 / 53 Podpod 96.5% 28 / 29 totaltotal 85.2% 357 / 419 CC 54 SCAR 23.4
CC
54
Cov
86%
_redirect_flow_from_legacy_params
47.7
confirm_resource
9.5
__ANON__
6.0
CRAP 10.4
↑ Summary GoCardless/Client.pm »
1 package Business::GoCardless::Pro;
2  
3 =head1 NAME
4  
5 Business::GoCardless::Pro - Perl library for interacting with the GoCardless Pro v2 API
6 (https://gocardless.com)
7  
8 =head1 DESCRIPTION
9  
10 Module for interacting with the GoCardless Pro (v2) API.
11  
12 B<You should refer to the official gocardless API documentation in conjunction>
13 B<with this perldoc>, as the official API documentation explains in more depth
14 some of the functionality including required / optional parameters for certain
15 methods. L<https://developer.gocardless.com>, specifically the docs for the
16 v2 GoCardless API at L<https://developer.gocardless.com/api-reference>.
17  
18 This module supports the resources of the GoCardless API (see
19 L</RESOURCE METHODS>), as well as being a back compatibility wrapper to allow
20 migration from the v1 (Basic) API (see L</BACK COMPATIBILITY METHODS>). Note
21 that new integrations should use Billing Requests rather than Redirect Flows
22 (which the back compatibility methods use), see
23 L</Setting up a mandate with a Billing Request>.
24  
25 Also note this class also currently inherits from L<Business::GoCardless>
26 so has all attributes and methods available on that class (some of which may not
27 make sense from the context of the Pro API).
28  
29 =head1 SYNOPSIS
30  
31 The following examples show instantiating the object and getting a resource
32 (Payment in this case) to manipulate. For more examples see the t/004_end_to_end_pro.t
33 script, which can be run against the gocardless sandbox (or even live) endpoint
34 when given the necessary ENV variables.
35  
36 my $GoCardless = Business::GoCardless::Pro->new(
37 token => $your_gocardless_token
38 client_details => {
39 base_url => $gocardless_url, # defaults to https://api.gocardless.com
40 webhook_secret => $secret,
41 },
42 );
43  
44 # create URL for a one off payment
45 my $new_bill_url = $GoCardless->new_bill_url(
46 session_token => 'foo',
47 description => "Test Payment",
48 success_redirect_url => "http://example.com/rflow/confirm?jwt=$jwt",
49 );
50  
51 # having sent the user to the $new_bill_url and them having complete it,
52 # we need to confirm the resource using the details sent by gocardless to
53 # the redirect_uri (https://developer.gocardless.com/api-reference/#redirect-flows-complete-a-redirect-flow)
54 my $Payment = $GoCardless->confirm_resource(
55 redirect_flow_id => $id,
56 session_token => 'foo', # the one passed to new_bill_url
57 type => 'payment', # bill / payment / pre_auth / subscription
58 amount => 0,
59 currency => 'GBP',
60 );
61  
62 # get a specfic Payment
63 $Payment = $GoCardless->payment( $id );
64  
65 # cancel the Payment
66 $Payment->cancel;
67  
68 # too late? maybe we should refund instead (note: needs to be enabled on GoCardless end)
69 $Payment->refund;
70  
71 # or maybe it failed?
72 $Payment->retry if $Payment->failed;
73  
74 # get a list of Payment objects (filter optional: https://developer.gocardless.com/#filtering)
75 my @payments = $GoCardless->payments( %filter );
76  
77 # on any resource object:
78 my %data = $Payment->to_hash;
79 my $json = $Payment->to_json;
80  
81 =head1 PAGINATION
82  
83 Any methods marked as B<pager> have a dual interface, when called in list context
84 they will return the first page of resource objects (50 by default, you can pass
85 a C<limit> filter of up to 500), when called in scalar context they will return a
86 L<Business::GoCardless::Paginator> object allowing you to iterate through all the
87 objects using the API's cursor based pagination:
88  
89 # get a list of L<Business::GoCardless::Payment> objects
90 # (filter optional: https://developer.gocardless.com/#filtering)
91 my @payments = $GoCardless->payments( %filter );
92  
93 # or using the Business::GoCardless::Paginator object:
94 my $Pager = $GoCardless->payments;
95  
96 while( my @payments = $Pager->next ) {
97 foreach my $Payment ( @payments ) {
98 ...
99 }
100 }
101  
102 =cut
103  
104 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 4 ▶use Carp qw/ confess /;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
109 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 4 extends 'Business::GoCardless';
111  
112 4 ▶use Business::GoCardless::Payment;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 4 ▶use Business::GoCardless::RedirectFlow;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 4 ▶use Business::GoCardless::Subscription;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 4 ▶use Business::GoCardless::Customer;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 4 ▶use Business::GoCardless::Webhook::V2;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 4 ▶use Business::GoCardless::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 =head1 ATTRIBUTES
120  
121 All attributes are inherited from L<Business::GoCardless>.
122  
123 =cut
124  
125 has api_version => (
126 is => 'ro',
127 required => 0,
128 3 ▶ default => sub { 2 },
Subroutine __ANON__: called CC=1 SCAR=0.0
129 4 );
130  
131 =head1 Payment Methods
132  
133 =head2 payment
134  
135 Get an individual payment, returns a L<Business::GoCardless::Payment> object:
136  
137 my $Payment = $GoCardless->payment( $id );
138  
139 =head2 payments (B<pager>)
140  
141 Get a list of Payment objects (%filter is optional)
142  
143 my @payments = $GoCardless->payments( %filter );
144  
145 =head2 create_payment
146  
147 Create a payment with the passed params
148  
149 my $Payment = $GoCardless->create_payment(
150 "amount" => 100,
151 "currency" => "GBP",
152 "charge_date" => "2014-05-19",
153 "reference" => "WINEBOX001",
154 "metadata" => {
155 "order_dispatch_date" => "2014-05-22"
156 },
157 "links" => {
158 "mandate" => "MD123"
159 }
160 );
161  
162 As with all of the create methods you can pass an C<idempotency_key>, see
163 L</IDEMPOTENCY KEYS>.
164  
165 =cut
166  
167 1 ▶sub payment {
Subroutine payment: called CC=1 SCAR=0.0
Pod documented
168 1 my ( $self,$id ) = @_;
169 1 return $self->_generic_find_obj( $id,'Payment','payments' );
170 }
171  
172 4 ▶sub payments {
Subroutine payments: called CC=1 SCAR=0.0
Pod documented
173 4 my ( $self,%filters ) = @_;
174 4 return $self->_list( 'payments',\%filters );
175 }
176  
177 1 ▶sub create_payment {
Subroutine create_payment: called CC=1 SCAR=0.0
Pod documented
178 1 my ( $self,%params ) = @_;
179 1 return $self->_create( 'payments',\%params );
180 }
181  
182 =head1 Subscription Methods
183  
184 =head2 subscription
185  
186 Get an individual subscription, returns a L<Business::GoCardless::Subscrption> object:
187  
188 my $Subscription = $GoCardless->subscription( $id );
189  
190 =head2 subscriptions (B<pager>)
191  
192 Get a list of Subscription objects (%filter is optional)
193  
194 my @subscriptions = $GoCardless->subscriptions( %filter );
195  
196 =cut
197  
198 1 ▶sub subscription {
Subroutine subscription: called CC=1 SCAR=0.0
Pod documented
199 1 my ( $self,$id ) = @_;
200 1 return $self->_generic_find_obj( $id,'Subscription','subscriptions' );
201 }
202  
203 1 ▶sub subscriptions {
Subroutine subscriptions: called CC=1 SCAR=0.0
Pod documented
204 1 my ( $self,%filters ) = @_;
205 1 return $self->_list( 'subscriptions',\%filters );
206 }
207  
208 =head1 RedirectFlow Methods
209  
210 See L<Business::GoCardless::RedirectFlow> for more information on RedirectFlow operations.
211  
212 =head2 pre_authorization
213  
214 Get an individual redirect flow, returns a L<Business::GoCardless::RedirectFlow> object:
215  
216 my $RedirectFlow = $GoCardless->pre_authorization( $id );
217  
218 =head2 pre_authorizations (B<pager>)
219  
220 This is meaningless in the v2 API so will throw an exception if called.
221  
222 =cut
223  
224 1 ▶sub pre_authorization {
Subroutine pre_authorization: called CC=1 SCAR=0.0
Pod documented
225 1 my ( $self,$id ) = @_;
226 1 return $self->_generic_find_obj( $id,'RedirectFlow','redirect_flows' );
227 };
228  
229 1 ▶sub pre_authorizations {
Subroutine pre_authorizations: called CC=1 SCAR=0.0
Pod documented
230 1 Business::GoCardless::Exception->throw({
231 message => "->pre_authorizations is no longer meaningful in the Pro API",
232 });
233 };
234  
235 =head1 Mandate Methods
236  
237 See L<Business::GoCardless::Mandate> for more information on Mandate operations.
238  
239 =head2 mandate
240  
241 Get an individual mandate, returns a L<Business::GoCardless::Mandate> object:
242  
243 my $Mandate = $GoCardless->mandate( $id );
244  
245 =cut
246  
247 0 ▶sub mandate {
Subroutine mandate: not called CC=1 SCAR=6.9
Pod documented
248 0 my ( $self,$id ) = @_;
249 0 return $self->_generic_find_obj( $id,'Mandate','mandates' );
250 }
251  
252 =head1 Customer Methods
253  
254 See L<Business::GoCardless::Customer> for more information on Customer operations.
255  
256 =head2 customer
257  
258 Get an individual customer, returns a L<Business::GoCardless::Customer>.
259  
260 my $Customer = $GoCardless->customer( $id );
261  
262 =head2 customers (B<pager>)
263  
264 Get a list of L<Business::GoCardless::Customer> objects.
265  
266 my @customers = $GoCardless->customers;
267  
268 =cut
269  
270 1 ▶sub customer {
Subroutine customer: called CC=1 SCAR=0.0
Pod documented
271 1 my ( $self,$id ) = @_;
272 1 return $self->_generic_find_obj( $id,'Customer','customers' );
273 }
274  
275 2 ▶sub customers {
Subroutine customers: called CC=1 SCAR=0.0
Pod documented
276 2 my ( $self,%filters ) = @_;
277 2 return $self->_list( 'customers',\%filters );
278 }
279  
280 =head1 Webhook Methods
281  
282 See L<Business::GoCardless::Webhook::V2> for more information on Webhook operations.
283  
284 =head2 webhooks (B<pager>)
285  
286 Get a list of L<Business::GoCardless::Webhook::V2> objects.
287  
288 my @webhooks = $GoCardless->webhooks
289  
290 =cut
291  
292 2 ▶sub webhooks {
Subroutine webhooks: called CC=1 SCAR=0.0
Pod documented
293 2 my ( $self,%filters ) = @_;
294 2 return $self->_list( 'webhooks',\%filters );
295 }
296  
297 =head2 webhook
298  
299 Get a L<Business::GoCardless::Webhook::V2> object from the data sent to you via a
300 GoCardless webhook:
301  
302 my $Webhook = $GoCardless->webhook( $json_data,$signature );
303  
304 Note that GoCardless may continue to send old style webhooks even after you have
305 migrated from the Basic to the Pro API, so to handle this the logic in this method
306 will check the payload and if it is a legacy webhook will return a legacy Webhook
307 object. You can check this like so:
308  
309 if ( $Webhook->is_legacy ) {
310 # process webhook using older legacy code
311 ...
312 } else {
313 # process webhook using new style code
314 ...
315 }
316  
317 =cut
318  
319 2 ▶sub webhook {
Subroutine webhook: called CC=2 SCAR=7.1
Pod documented
320 2 my ( $self,$data,$signature ) = @_;
321  
322 2 my $webhook = Business::GoCardless::Webhook::V2->new(
323 client => $self->client,
324 json => $data,
325 # load ordering handled by setting _signature rather than signature
326 # signature will be set in the json trigger
327 _signature => $signature,
328 );
329  
330 1 ▶ return $webhook->has_legacy_data
Branch: 1 branch
BranchTrueFalseTotal
$webhook->has_legacy_data ? : 0 1 1
331 ? $webhook->legacy_webhook
332 : $webhook;
333 }
334  
335 # payout methods are in the SUPER class
336 =head1 Payout Methods
337  
338 See L<Business::GoCardless::Payout> for more information on Payout operations.
339  
340 =head2 payouts (B<pager>)
341  
342 Get a list of L<Business::GoCardless::Payout> objects.
343  
344 my @payouts = $GoCardless->payouts
345  
346 =head2 payout
347  
348 Get an individual payout, returns a L<Business::GoCardless::Payout> object:
349  
350 my $Payout = $GoCardless->payout( $id );
351  
352 =cut
353  
354 =head1 RESOURCE METHODS
355  
356 Methods for the resources of the Pro API follow the same pattern. For example
357 for billing requests:
358  
359 # get a single resource, returns a Business::GoCardless::BillingRequest
360 my $BillingRequest = $GoCardless->billing_request( $id );
361  
362 # get a list of resources (pager), %filters are optional
363 my @billing_requests = $GoCardless->billing_requests( %filters );
364  
365 # create a resource, returns a Business::GoCardless::BillingRequest
366 my $BillingRequest = $GoCardless->create_billing_request( %params );
367  
368 Operations on a resource (cancel, update, etc) are methods on the resource
369 objects, see the documentation of each resource class for these. The parameters
370 and filters are as documented in the GoCardless API reference. Note that some of
371 the endpoints are restricted, for example to GoCardless Embed customers or to
372 those with custom payment pages enabled, see the GoCardless API reference for
373 details.
374  
375 =head2 Setting up a mandate with a Billing Request
376  
377 Billing Requests have replaced Redirect Flows, for example to set up a mandate
378 using the GoCardless hosted payment pages:
379  
380 my $BillingRequest = $GoCardless->create_billing_request(
381 mandate_request => { scheme => 'bacs' },
382 );
383  
384 my $Flow = $BillingRequest->create_flow(
385 redirect_uri => 'https://example.com/mandate/complete',
386 exit_uri => 'https://example.com/mandate/exit',
387 );
388  
389 # send the customer to $Flow->authorisation_url, once they have completed
390 # the flow GoCardless will send you a billing_requests fulfilled webhook
391 # (or you can check the billing request):
392  
393 $BillingRequest = $GoCardless->billing_request( $BillingRequest->id );
394  
395 if ( $BillingRequest->fulfilled ) {
396 my $Mandate = $GoCardless->mandate( $BillingRequest->mandate_id );
397 ...
398 }
399  
400 =head1 IDEMPOTENCY KEYS
401  
402 All of the create methods take an optional C<idempotency_key> parameter, which
403 is sent as the C<Idempotency-Key> header rather than in the request body. If you
404 don't pass one a random key is generated for the request. See
405 L<Business::GoCardless::Client/IDEMPOTENCY KEYS> for more details.
406  
407 my $Payment = $GoCardless->create_payment(
408 %params,
409 idempotency_key => $your_unique_key,
410 );
411  
412 =head2 Mandates
413  
414 See L<Business::GoCardless::Mandate>
415  
416 my @mandates = $GoCardless->mandates( %filters );
417 my $Mandate = $GoCardless->create_mandate( %params );
418  
419 =head2 Subscriptions
420  
421 See L<Business::GoCardless::Subscription>
422  
423 my $Subscription = $GoCardless->create_subscription( %params );
424  
425 =head2 Customers
426  
427 See L<Business::GoCardless::Customer>
428  
429 my $Customer = $GoCardless->create_customer( %params );
430  
431 =head2 Refunds
432  
433 See L<Business::GoCardless::Refund>
434  
435 my $Refund = $GoCardless->refund( $id );
436 my @refunds = $GoCardless->refunds( %filters );
437 my $Refund = $GoCardless->create_refund( %params );
438  
439 =head2 Events
440  
441 See L<Business::GoCardless::Webhook::Event>
442  
443 my $Event = $GoCardless->event( $id );
444 my @events = $GoCardless->events( %filters );
445  
446 =head2 Billing Requests
447  
448 See L<Business::GoCardless::BillingRequest>
449  
450 my $BillingRequest = $GoCardless->billing_request( $id );
451 my @billing_requests = $GoCardless->billing_requests( %filters );
452 my $BillingRequest = $GoCardless->create_billing_request( %params );
453  
454 =head2 Billing Request Flows
455  
456 See L<Business::GoCardless::BillingRequestFlow>
457  
458 my $BillingRequestFlow = $GoCardless->create_billing_request_flow( %params );
459  
460 =head2 Billing Request Templates
461  
462 See L<Business::GoCardless::BillingRequestTemplate>
463  
464 my $BillingRequestTemplate = $GoCardless->billing_request_template( $id );
465 my @billing_request_templates = $GoCardless->billing_request_templates( %filters );
466 my $BillingRequestTemplate = $GoCardless->create_billing_request_template( %params );
467  
468 =head2 Bank Authorisations
469  
470 See L<Business::GoCardless::BankAuthorisation>
471  
472 my $BankAuthorisation = $GoCardless->bank_authorisation( $id );
473 my $BankAuthorisation = $GoCardless->create_bank_authorisation( %params );
474  
475 =head2 Institutions
476  
477 See L<Business::GoCardless::Institution>
478  
479 my @institutions = $GoCardless->institutions( %filters );
480  
481 =head2 Customer Bank Accounts
482  
483 See L<Business::GoCardless::CustomerBankAccount>
484  
485 my $CustomerBankAccount = $GoCardless->customer_bank_account( $id );
486 my @customer_bank_accounts = $GoCardless->customer_bank_accounts( %filters );
487 my $CustomerBankAccount = $GoCardless->create_customer_bank_account( %params );
488  
489 =head2 Bank Details Lookups
490  
491 See L<Business::GoCardless::BankDetailsLookup>
492  
493 my $BankDetailsLookup = $GoCardless->create_bank_details_lookup( %params );
494  
495 =head2 Creditors
496  
497 See L<Business::GoCardless::Creditor>
498  
499 my $Creditor = $GoCardless->creditor( $id );
500 my @creditors = $GoCardless->creditors( %filters );
501 my $Creditor = $GoCardless->create_creditor( %params );
502  
503 =head2 Creditor Bank Accounts
504  
505 See L<Business::GoCardless::CreditorBankAccount>
506  
507 my $CreditorBankAccount = $GoCardless->creditor_bank_account( $id );
508 my @creditor_bank_accounts = $GoCardless->creditor_bank_accounts( %filters );
509 my $CreditorBankAccount = $GoCardless->create_creditor_bank_account( %params );
510  
511 =head2 Verification Details
512  
513 See L<Business::GoCardless::VerificationDetail>
514  
515 my @verification_details = $GoCardless->verification_details( %filters );
516 my $VerificationDetail = $GoCardless->create_verification_detail( %params );
517  
518 =head2 Instalment Schedules
519  
520 See L<Business::GoCardless::InstalmentSchedule>
521  
522 my $InstalmentSchedule = $GoCardless->instalment_schedule( $id );
523 my @instalment_schedules = $GoCardless->instalment_schedules( %filters );
524  
525 =head2 Tax Rates
526  
527 See L<Business::GoCardless::TaxRate>
528  
529 my $TaxRate = $GoCardless->tax_rate( $id );
530 my @tax_rates = $GoCardless->tax_rates( %filters );
531  
532 =head2 Mandate Imports
533  
534 See L<Business::GoCardless::MandateImport>
535  
536 my $MandateImport = $GoCardless->mandate_import( $id );
537 my $MandateImport = $GoCardless->create_mandate_import( %params );
538  
539 =head2 Mandate Import Entries
540  
541 See L<Business::GoCardless::MandateImportEntry>
542  
543 my @mandate_import_entries = $GoCardless->mandate_import_entries( %filters );
544 my $MandateImportEntry = $GoCardless->create_mandate_import_entry( %params );
545  
546 =head2 Transferred Mandates
547  
548 See L<Business::GoCardless::TransferredMandate>
549  
550 my $TransferredMandate = $GoCardless->transferred_mandate( $id );
551  
552 =head2 Payout Items
553  
554 See L<Business::GoCardless::PayoutItem>
555  
556 my @payout_items = $GoCardless->payout_items( %filters );
557  
558 =head2 Balances
559  
560 See L<Business::GoCardless::Balance>
561  
562 my @balances = $GoCardless->balances( %filters );
563  
564 =head2 Negative Balance Limits
565  
566 See L<Business::GoCardless::NegativeBalanceLimit>
567  
568 my @negative_balance_limits = $GoCardless->negative_balance_limits( %filters );
569  
570 =head2 Currency Exchange Rates
571  
572 See L<Business::GoCardless::CurrencyExchangeRate>
573  
574 my @currency_exchange_rates = $GoCardless->currency_exchange_rates( %filters );
575  
576 =head2 Blocks
577  
578 See L<Business::GoCardless::Block>
579  
580 my $Block = $GoCardless->block( $id );
581 my @blocks = $GoCardless->blocks( %filters );
582 my $Block = $GoCardless->create_block( %params );
583  
584 =head2 Logos
585  
586 See L<Business::GoCardless::Logo>
587  
588 my $Logo = $GoCardless->create_logo( %params );
589  
590 =head2 Payer Themes
591  
592 See L<Business::GoCardless::PayerTheme>
593  
594 my $PayerTheme = $GoCardless->create_payer_theme( %params );
595  
596 =head2 Scheme Identifiers
597  
598 See L<Business::GoCardless::SchemeIdentifier>
599  
600 my $SchemeIdentifier = $GoCardless->scheme_identifier( $id );
601 my @scheme_identifiers = $GoCardless->scheme_identifiers( %filters );
602 my $SchemeIdentifier = $GoCardless->create_scheme_identifier( %params );
603  
604 =head2 Exports
605  
606 See L<Business::GoCardless::Export>
607  
608 my $Export = $GoCardless->export( $id );
609 my @exports = $GoCardless->exports( %filters );
610  
611 =head2 Outbound Payments
612  
613 See L<Business::GoCardless::OutboundPayment>
614  
615 my $OutboundPayment = $GoCardless->outbound_payment( $id );
616 my @outbound_payments = $GoCardless->outbound_payments( %filters );
617 my $OutboundPayment = $GoCardless->create_outbound_payment( %params );
618  
619 =head2 Outbound Payment Imports
620  
621 See L<Business::GoCardless::OutboundPaymentImport>
622  
623 my $OutboundPaymentImport = $GoCardless->outbound_payment_import( $id );
624 my @outbound_payment_imports = $GoCardless->outbound_payment_imports( %filters );
625 my $OutboundPaymentImport = $GoCardless->create_outbound_payment_import( %params );
626  
627 =head2 Outbound Payment Import Entries
628  
629 See L<Business::GoCardless::OutboundPaymentImportEntry>
630  
631 my @outbound_payment_import_entries = $GoCardless->outbound_payment_import_entries( %filters );
632  
633 =head2 Bank Account Holder Verifications
634  
635 See L<Business::GoCardless::BankAccountHolderVerification>
636  
637 my $BankAccountHolderVerification = $GoCardless->bank_account_holder_verification( $id );
638 my $BankAccountHolderVerification = $GoCardless->create_bank_account_holder_verification( %params );
639  
640 =head2 Payment Accounts
641  
642 See L<Business::GoCardless::PaymentAccount>
643  
644 my $PaymentAccount = $GoCardless->payment_account( $id );
645 my @payment_accounts = $GoCardless->payment_accounts( %filters );
646  
647 =head2 Payment Account Transactions
648  
649 See L<Business::GoCardless::PaymentAccountTransaction>
650  
651 my $PaymentAccountTransaction = $GoCardless->payment_account_transaction( $id );
652  
653 =cut
654  
655 # the class for each of the resources (keyed on the API endpoint), and the
656 # path for the resource if it isn't the same as the endpoint
657 4 my %RESOURCE_CLASS = (
658 payments => 'Payment',
659 redirect_flows => 'RedirectFlow',
660 customers => 'Customer',
661 subscriptions => 'Subscription',
662 webhooks => 'Webhook::V2',
663 payouts => 'Payout',
664 );
665  
666 4 my %RESOURCE_PATH;
667  
668 # most of the resources follow the same pattern in the API, so we generate the
669 # methods for them from the following. for each resource the operations are:
670 #
671 # get => $Pro->$singular( $id )
672 # list => $Pro->$endpoint( %filters ) (pager)
673 # create => $Pro->create_$singular( %params )
674 #
675 4 my @RESOURCES = (
676 # endpoint class singular operations
677 [ mandates => 'Mandate' => mandate => qw/ list create / ],
678 [ subscriptions => 'Subscription' => subscription => qw/ create / ],
679 [ customers => 'Customer' => customer => qw/ create / ],
680 [ refunds => 'Refund' => refund => qw/ get list create / ],
681 [ events => 'Webhook::Event' => event => qw/ get list / ],
682 [ billing_requests => 'BillingRequest' => billing_request => qw/ get list create / ],
683 [ billing_request_flows => 'BillingRequestFlow' => billing_request_flow => qw/ create / ],
684 [ billing_request_templates => 'BillingRequestTemplate' => billing_request_template => qw/ get list create / ],
685 [ bank_authorisations => 'BankAuthorisation' => bank_authorisation => qw/ get create / ],
686 [ institutions => 'Institution' => institution => qw/ list / ],
687 [ customer_bank_accounts => 'CustomerBankAccount' => customer_bank_account => qw/ get list create / ],
688 [ bank_details_lookups => 'BankDetailsLookup' => bank_details_lookup => qw/ create / ],
689 [ creditors => 'Creditor' => creditor => qw/ get list create / ],
690 [ creditor_bank_accounts => 'CreditorBankAccount' => creditor_bank_account => qw/ get list create / ],
691 [ verification_details => 'VerificationDetail' => verification_detail => qw/ list create / ],
692 [ instalment_schedules => 'InstalmentSchedule' => instalment_schedule => qw/ get list / ],
693 [ tax_rates => 'TaxRate' => tax_rate => qw/ get list / ],
694 [ mandate_imports => 'MandateImport' => mandate_import => qw/ get create / ],
695 [ mandate_import_entries => 'MandateImportEntry' => mandate_import_entry => qw/ list create / ],
696 [ mandate_pdfs => 'MandatePdf' => mandate_pdf => qw// ],
697 [ transferred_mandates => 'TransferredMandate' => transferred_mandate => qw/ get / ],
698 [ payout_items => 'PayoutItem' => payout_item => qw/ list / ],
699 [ balances => 'Balance' => balance => qw/ list / ],
700 [ negative_balance_limits => 'NegativeBalanceLimit' => negative_balance_limit => qw/ list / ],
701 [ currency_exchange_rates => 'CurrencyExchangeRate' => currency_exchange_rate => qw/ list / ],
702 [ blocks => 'Block' => block => qw/ get list create / ],
703 [ logos => 'Logo' => logo => qw/ create / ],
704 [ payer_themes => 'PayerTheme' => payer_theme => qw/ create / ],
705 [ scheme_identifiers => 'SchemeIdentifier' => scheme_identifier => qw/ get list create / ],
706 [ exports => 'Export' => export => qw/ get list / ],
707 [ outbound_payments => 'OutboundPayment' => outbound_payment => qw/ get list create / ],
708 [ outbound_payment_imports => 'OutboundPaymentImport' => outbound_payment_import => qw/ get list create / ],
709 [ outbound_payment_import_entries => 'OutboundPaymentImportEntry' => outbound_payment_import_entry => qw/ list / ],
710 [ bank_account_holder_verifications => 'BankAccountHolderVerification' => bank_account_holder_verification => qw/ get create / ],
711 [ payment_accounts => 'PaymentAccount' => payment_account => qw/ get list / ],
712 [ payment_account_transactions => 'PaymentAccountTransaction' => payment_account_transaction => qw/ get / ],
713 [ bank_account_details => 'BankAccountDetail' => bank_account_detail => qw// ],
714 [ customer_notifications => 'CustomerNotification' => customer_notification => qw// ],
715 );
716  
717 4 $RESOURCE_PATH{logos} = '/branding/logos';
718 4 $RESOURCE_PATH{payer_themes} = '/branding/payer_themes';
719  
720 4 foreach my $resource ( @RESOURCES ) {
721  
722 152 my ( $endpoint,$class,$singular,@operations ) = @{ $resource };
723  
724 152 $RESOURCE_CLASS{$endpoint} = $class;
725  
726 my %methods = (
727 20 ▶ get => [ $singular => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
728 20 my ( $self,$id ) = @_;
729 20 return $self->_get( $endpoint,$id );
730 } ],
731 48 ▶ list => [ $endpoint => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
732 48 my ( $self,%filters ) = @_;
733 48 return $self->_list( $endpoint,\%filters );
734 } ],
735 22 ▶ create => [ "create_$singular" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
736 22 my ( $self,%params ) = @_;
737 22 return $self->_create( $endpoint,\%params );
738 152 } ],
739 );
740  
741 152 foreach my $operation ( @operations ) {
742  
743 264 my ( $method,$code ) = @{ $methods{$operation} };
744  
745 # make sure we don't clobber any explicitly defined methods
746 264 ▶ confess( "$method is already defined" )
Branch: 1 branch
BranchTrueFalseTotal
if __PACKAGE__->can($method) 0 264 264
747 if __PACKAGE__->can( $method );
748  
749 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
750 264 *{ __PACKAGE__ . "::$method" } = $code;
751 }
752 }
753  
754 # load all of the resource classes
755 eval "require Business::GoCardless::$_; 1" || die $@
756 4 ▶ for sort values( %RESOURCE_CLASS );
Branch: 1 branch
BranchTrueFalseTotal
eval "require Business::GoCardless::$_; 1" or die $@ 168 0 168
757  
758 140 ▶sub _new_resource {
Subroutine _new_resource: called CC=3 SCAR=11.0
759 140 my ( $self,$endpoint,$data ) = @_;
760  
761 140 my $class = "Business::GoCardless::$RESOURCE_CLASS{$endpoint}";
762  
763 return $endpoint eq 'webhooks'
764 ? $class->_new_from_api( $self->client,$data )
765 140 ▶ : $class->new( client => $self->client,%{ $data // {} } );
Branch: 1 branch
BranchTrueFalseTotal
$endpoint eq 'webhooks' ? : 2 138 140
Condition50%1/2
$data // {}
l!l
1380
MC/DC0%0/1
$data // {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
766 }
767  
768 21 ▶sub _get {
Subroutine _get: called CC=2 SCAR=7.0
769 21 my ( $self,$endpoint,$id,$headers ) = @_;
770  
771 21 ▶ my $path = $RESOURCE_PATH{$endpoint} // "/$endpoint";
Condition50%1/2
$RESOURCE_PATH{$endpoint} // "/$endpoint"
l!l
021
MC/DC0%0/1
$RESOURCE_PATH{$endpoint} // "/$endpoint"
$RESOURCE_PATH{$endpoint}
Truth table50%1/2
A
A= $RESOURCE_PATH{$endpoint}
Aresult
11
00
772 21 my $data = $self->client->api_get( "$path/$id",undef,$headers );
773  
774 21 return $self->_new_resource( $endpoint,$data->{$endpoint} );
775 }
776  
777 27 ▶sub _create {
Subroutine _create: called CC=1 SCAR=0.0
778 27 my ( $self,$endpoint,$params,%opts ) = @_;
779  
780 27 my $data = $self->_create_request( $endpoint,$params,%opts );
781 27 return $self->_new_resource( $endpoint,$data->{$endpoint} );
782 }
783  
784 29 ▶sub _create_request {
Subroutine _create_request: called CC=6 SCAR=17.9
785 29 my ( $self,$endpoint,$params,%opts ) = @_;
786  
787 29 ▶ my %params = %{ $params // {} };
Condition50%1/2
$params // {}
l!l
290
MC/DC0%0/1
$params // {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
788 29 ▶ my %headers = %{ $opts{headers} // {} };
Condition100%2/2
$opts{"headers"} // {}
l!l
128
MC/DC100%1/1
$opts{"headers"} // {}
$opts{"headers"}
Truth table100%2/2
A
A= $opts{"headers"}
Aresult
11
00
789  
790 # not part of the request body, see IDEMPOTENCY KEYS
791 29 my $key = delete( $params{idempotency_key} );
792 29 ▶ $headers{'Idempotency-Key'} = $key if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 22 7 29
793  
794 29 ▶ my $path = $opts{path} // $RESOURCE_PATH{$endpoint} // "/$endpoint";
Condition100%5/5
($opts{"path"} // $RESOURCE_PATH{$endpoint}) // "/$endpoint"
l!l
722
$opts{"path"} // $RESOURCE_PATH{$endpoint}
l!l&&r!l&&!r
5222
MC/DC100%2/2
($opts{"path"} // $RESOURCE_PATH{$endpoint}) // "/$endpoint"
$opts{"path"} $RESOURCE_PATH{$endpoint}
Truth table100%3/3
A // B
A= $opts{"path"}B= $RESOURCE_PATH{$endpoint}
ABresult
1X1
011
000
795  
796 29 return $self->client->api_post(
797 $path,{ $endpoint => \%params },\%headers
798 );
799 }
800  
801 58 ▶sub _list {
Subroutine _list: called CC=5 SCAR=16.1
802 58 my ( $self,$endpoint,$filters,$path ) = @_;
803  
804 58 ▶ $filters //= {};
Condition50%1/2
$filters //= {}
l!l
580
MC/DC0%0/1
$filters //= {}
$filters
Truth table50%1/2
A
A= $filters
Aresult
11
00
805 58 ▶ $path //= $RESOURCE_PATH{$endpoint} // "/$endpoint";
Condition75%3/4
$path //= $RESOURCE_PATH{$endpoint} // "/$endpoint"
l!l
157
$RESOURCE_PATH{$endpoint} // "/$endpoint"
l!l
057
MC/DC100%1/1
$path //= $RESOURCE_PATH{$endpoint} // "/$endpoint"
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
806  
807 58 my $class = "Business::GoCardless::$RESOURCE_CLASS{$endpoint}";
808  
809 120 ▶ my $uri_for = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
810 120 my ( %cursor ) = @_;
811  
812 # cursor params replace any passed in the original filters
813 120 my %params = ( %{ $filters },%cursor );
814 120 delete( @params{ grep { ! defined $params{$_} } keys %params } );
815  
816 120 ▶ return keys( %params )
Branch: 1 branch
BranchTrueFalseTotal
keys %params ? : 60 60 120
817 ? "$path?" . $self->client->normalize_params( \%params )
818 : $path;
819 58 };
820  
821 # the v2 API uses cursor based pagination, so we construct the links for
822 # next / previous / first from the meta data in each page of results
823 59 ▶ my $page_loader = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
824 59 my ( $uri ) = @_;
825  
826 59 my ( $data ) = $self->client->api_get( $uri );
827 59 ▶ my $cursors = $data->{meta}{cursors} // {};
Condition100%2/2
$data->{"meta"}{"cursors"} // {}
l!l
2732
MC/DC100%1/1
$data->{"meta"}{"cursors"} // {}
$data->{"meta"}{"cursors"}
Truth table100%2/2
A
A= $data->{"meta"}{"cursors"}
Aresult
11
00
828  
829 87 my @objects = map { $self->_new_resource( $endpoint,$_ ) }
830 59 ▶ @{ $data->{$endpoint} // [] };
Condition50%1/2
$data->{$endpoint} // []
l!l
590
MC/DC0%0/1
$data->{$endpoint} // []
$data->{$endpoint}
Truth table50%1/2
A
A= $data->{$endpoint}
Aresult
11
00
831  
832 my $links = {
833 first => $uri_for->( before => undef,after => undef ),
834 ( $cursors->{after} ? ( next => $uri_for->( after => $cursors->{after},before => undef ) ) : () ),
835 59 ▶ ( $cursors->{before} ? ( previous => $uri_for->( before => $cursors->{before},after => undef ) ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
$cursors->{'after'} ? : 2 57 59
$cursors->{'before'} ? : 1 58 59
836 };
837  
838 59 ▶ return ( \@objects,$links,$data->{meta} // {} );
Condition50%1/2
$data->{"meta"} // {}
l!l
590
MC/DC0%0/1
$data->{"meta"} // {}
$data->{"meta"}
Truth table50%1/2
A
A= $data->{"meta"}
Aresult
11
00
839 58 };
840  
841 58 my ( $objects,$links,$info ) = $page_loader->( $uri_for->() );
842  
843 58 ▶ return wantarray ? ( @{ $objects } ) : Business::GoCardless::Paginator->new(
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 32 26 58
844 class => $class,
845 client => $self->client,
846 links => $links,
847 info => $info,
848 objects => $objects,
849 page_loader => $page_loader,
850 );
851 }
852  
853 =head1 OTHER RESOURCE METHODS
854  
855 The following methods are for API endpoints that don't follow the standard
856 pattern of the L</RESOURCE METHODS>.
857  
858 =head2 create_billing_request_with_actions
859  
860 Create a billing request and perform actions on it in a single request (this
861 is restricted to custom payment pages, and doesn't support ACH/PAD). Returns a
862 L<Business::GoCardless::BillingRequest> object, and in list context also the
863 L<Business::GoCardless::BankAuthorisation> if one was created:
864  
865 my ( $BillingRequest,$BankAuthorisation )
866 = $GoCardless->create_billing_request_with_actions(
867 payment_request => { amount => 1000,currency => 'GBP' },
868 actions => {
869 collect_customer_details => { ... },
870 create_bank_authorisation => JSON::true,
871 },
872 );
873  
874 =head2 create_instalment_schedule_with_dates
875  
876 Create an L<Business::GoCardless::InstalmentSchedule> with the dates of each
877 instalment:
878  
879 my $Schedule = $GoCardless->create_instalment_schedule_with_dates(
880 name => 'Bike',
881 currency => 'GBP',
882 total_amount => 3000,
883 instalments => [
884 { charge_date => '2026-11-01',amount => 1500 },
885 { charge_date => '2026-12-01',amount => 1500 },
886 ],
887 links => { mandate => $mandate_id },
888 );
889  
890 =head2 create_instalment_schedule_with_schedule
891  
892 Create an L<Business::GoCardless::InstalmentSchedule> with a schedule from
893 which the instalment dates are calculated:
894  
895 my $Schedule = $GoCardless->create_instalment_schedule_with_schedule(
896 name => 'Bike',
897 currency => 'GBP',
898 total_amount => 3000,
899 instalments => {
900 start_date => '2026-11-01',
901 interval => 1,
902 interval_unit => 'monthly',
903 amounts => [ 1500,1500 ],
904 },
905 links => { mandate => $mandate_id },
906 );
907  
908 Instalment schedules are created asynchronously, so the status will be
909 C<pending> until it becomes C<active> (or C<creation_failed>).
910  
911 =head2 create_mandate_pdf
912  
913 Create a L<Business::GoCardless::MandatePdf>, the PDF can be in a language
914 other than English by passing C<accept_language>:
915  
916 my $Pdf = $GoCardless->create_mandate_pdf(
917 links => { mandate => $mandate_id },
918 accept_language => 'fr', # optional
919 );
920  
921 my $url = $Pdf->url;
922  
923 =head2 bank_account_detail
924  
925 Get the encrypted bank account details of a customer bank account (restricted
926 to the Encrypted Bank Details Access upgrade), you must pass the ID of your
927 public key. Returns a L<Business::GoCardless::BankAccountDetail> object:
928  
929 my $Detail = $GoCardless->bank_account_detail( $bank_account_id,$key_id );
930  
931 =head2 funds_available
932  
933 Check if funds are available for a VRP mandate, returns true or false:
934  
935 if ( $GoCardless->funds_available( $mandate_id,$amount ) ) {
936 ...
937 }
938  
939 =head2 block_by_ref
940  
941 Create blocks for the details of a customer or mandate, returns a list of the
942 L<Business::GoCardless::Block> objects created:
943  
944 my @blocks = $GoCardless->block_by_ref(
945 reference_type => 'customer',
946 reference_value => $customer_id,
947 reason_type => 'no_intent_to_pay',
948 );
949  
950 =head2 run_scenario_simulator
951  
952 Run a scenario simulator against a resource (sandbox only), for example to
953 simulate a payment being paid out:
954  
955 $GoCardless->run_scenario_simulator( 'payment_paid_out',$payment_id );
956  
957 See L<https://developer.gocardless.com/api-reference/#testing-scenario-simulators>
958 for the available scenarios.
959  
960 =head2 create_outbound_payment_withdrawal
961  
962 Create an L<Business::GoCardless::OutboundPayment> to withdraw funds to your
963 verified business bank account:
964  
965 my $Withdrawal = $GoCardless->create_outbound_payment_withdrawal(
966 amount => 1000,
967 scheme => 'faster_payments',
968 );
969  
970 =head2 outbound_payment_stats
971  
972 Get the statistics of your outbound payments, returns a hashref:
973  
974 my $stats = $GoCardless->outbound_payment_stats;
975  
976 =head2 payment_account_transactions (B<pager>)
977  
978 Get a list of the L<Business::GoCardless::PaymentAccountTransaction> objects
979 for a payment account:
980  
981 my @transactions = $GoCardless->payment_account_transactions(
982 $payment_account_id,
983 value_date_from => '2026-10-01',
984 value_date_to => '2026-10-31',
985 );
986  
987 =head2 handle_customer_notification
988  
989 Handle a customer notification (meaning you have sent it rather than
990 GoCardless), returns a L<Business::GoCardless::CustomerNotification> object:
991  
992 $GoCardless->handle_customer_notification( $notification_id );
993  
994 =cut
995  
996 1 ▶sub create_billing_request_with_actions {
Subroutine create_billing_request_with_actions: called CC=4 SCAR=14.6
Pod documented
997 1 my ( $self,%params ) = @_;
998  
999 my $data = $self->_create_request(
1000 'billing_request_with_actions',\%params,
1001 path => '/billing_requests/create_with_actions',
1002 1 ▶ )->{billing_request_with_actions} // {};
Condition50%1/2
$self->_create_request("billing_request_with_actions", \%params, "path", "/billing_requests/create_with_actions")->{"billing_request_with_actions"} // {}
l!l
10
MC/DC0%0/1
$self->_create_request("billing_request_with_actions", \%params, "path", "/billing_requests/create_with_actions")->{"billing_request_with_actions"} // {}
$self->_create_request("billing_request_with_actions", \%params, "path", "/billing_requests/create_with_actions")->{"billing_request_with_actions"}
Truth table50%1/2
A
A= $self->_create_request("billing_request_with_actions", \%params, "path", "/billing_requests/create_with_actions")->{"billing_request_with_actions"}
Aresult
11
00
1003  
1004 my $BillingRequest = $self->_new_resource(
1005 'billing_requests',$data->{billing_requests}
1006 1 );
1007  
1008 my $BankAuthorisation = $data->{bank_authorisations}
1009 ? $self->_new_resource( 'bank_authorisations',$data->{bank_authorisations} )
1010 1 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
$data->{'bank_authorisations'} ? : 1 0 1
1011  
1012 1 ▶ return wantarray ? ( $BillingRequest,$BankAuthorisation ) : $BillingRequest;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
1013 }
1014  
1015 1 ▶sub create_instalment_schedule_with_dates {
Subroutine create_instalment_schedule_with_dates: called CC=1 SCAR=0.0
Pod documented
1016 1 my ( $self,%params ) = @_;
1017 1 return $self->_create(
1018 'instalment_schedules',\%params,
1019 path => '/instalment_schedules/create_with_dates',
1020 );
1021 }
1022  
1023 1 ▶sub create_instalment_schedule_with_schedule {
Subroutine create_instalment_schedule_with_schedule: called CC=1 SCAR=0.0
Pod documented
1024 1 my ( $self,%params ) = @_;
1025 1 return $self->_create(
1026 'instalment_schedules',\%params,
1027 path => '/instalment_schedules/create_with_schedule',
1028 );
1029 }
1030  
1031 1 ▶sub create_mandate_pdf {
Subroutine create_mandate_pdf: called CC=2 SCAR=7.1
Pod documented
1032 1 my ( $self,%params ) = @_;
1033  
1034 1 my $language = delete( $params{accept_language} );
1035  
1036 1 ▶ return $self->_create(
Branch: 1 branch
BranchTrueFalseTotal
$language ? : 1 0 1
1037 'mandate_pdfs',\%params,
1038 ( $language ? ( headers => { 'Accept-Language' => $language } ) : () ),
1039 );
1040 }
1041  
1042 2 ▶sub bank_account_detail {
Subroutine bank_account_detail: called CC=2 SCAR=6.9
Pod documented
1043 2 my ( $self,$id,$key_id ) = @_;
1044  
1045 2 ▶ $key_id // confess( "key_id is required for bank_account_detail" );
Condition100%2/2
$key_id // confess("key_id is required for bank_account_detail")
l!l
11
MC/DC100%1/1
$key_id // confess("key_id is required for bank_account_detail")
$key_id
Truth table100%2/2
A
A= $key_id
Aresult
11
00
1046  
1047 1 my $Detail = $self->_get( 'bank_account_details',$id,{ 'Gc-Key-Id' => $key_id } );
1048 1 $Detail->id( $id );
1049  
1050 1 return $Detail;
1051 }
1052  
1053 3 ▶sub funds_available {
Subroutine funds_available: called CC=3 SCAR=11.0
Pod documented
1054 3 my ( $self,$mandate_id,$amount ) = @_;
1055  
1056 3 my $data = $self->client->api_get(
1057 "/funds_availability/$mandate_id?"
1058 . $self->client->normalize_params( { amount => $amount } )
1059 );
1060  
1061 # the docs show the response as unwrapped, so handle both
1062 my $availability = ref( $data->{funds_availabilities} ) eq 'HASH'
1063 ? $data->{funds_availabilities}
1064 3 ▶ : $data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'funds_availabilities'} eq 'HASH' ? : 1 2 3
1065  
1066 3 ▶ return $availability->{available} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$availability->{'available'} ? : 2 1 3
1067 }
1068  
1069 1 ▶sub block_by_ref {
Subroutine block_by_ref: called CC=2 SCAR=7.0
Pod documented
1070 1 my ( $self,%params ) = @_;
1071  
1072 1 my $data = $self->_create_request(
1073 'blocks',\%params,path => '/blocks/block_by_ref',
1074 );
1075  
1076 1 ▶ return map { $self->_new_resource( 'blocks',$_ ) } @{ $data->{blocks} // [] };
Condition50%1/2
$data->{"blocks"} // []
l!l
10
MC/DC0%0/1
$data->{"blocks"} // []
$data->{"blocks"}
Truth table50%1/2
A
A= $data->{"blocks"}
Aresult
11
00
1077 }
1078  
1079 1 ▶sub run_scenario_simulator {
Subroutine run_scenario_simulator: called CC=1 SCAR=0.0
Pod documented
1080 1 my ( $self,$scenario,$resource_id ) = @_;
1081  
1082 1 $self->client->api_post(
1083 "/scenario_simulators/$scenario/actions/run",
1084 { data => { links => { resource => $resource_id } } },
1085 );
1086  
1087 1 return 1;
1088 }
1089  
1090 1 ▶sub create_outbound_payment_withdrawal {
Subroutine create_outbound_payment_withdrawal: called CC=1 SCAR=0.0
Pod documented
1091 1 my ( $self,%params ) = @_;
1092 1 return $self->_create(
1093 'outbound_payments',\%params,
1094 path => '/outbound_payments/withdrawal',
1095 );
1096 }
1097  
1098 1 ▶sub outbound_payment_stats {
Subroutine outbound_payment_stats: called CC=3 SCAR=11.3
Pod documented
1099 1 my ( $self,%filters ) = @_;
1100  
1101 1 my $uri = '/outbound_payments/stats';
1102 1 ▶ $uri .= '?' . $self->client->normalize_params( \%filters ) if keys %filters;
Branch: 1 branch
BranchTrueFalseTotal
if keys %filters 0 1 1
1103  
1104 1 my $data = $self->client->api_get( $uri );
1105  
1106 return ref( $data->{outbound_payments} ) eq 'HASH'
1107 ? $data->{outbound_payments}
1108 1 ▶ : $data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'outbound_payments'} eq 'HASH' ? : 1 0 1
1109 }
1110  
1111 1 ▶sub payment_account_transactions {
Subroutine payment_account_transactions: called CC=1 SCAR=0.0
Pod documented
1112 1 my ( $self,$payment_account_id,%filters ) = @_;
1113 1 return $self->_list(
1114 'payment_account_transactions',\%filters,
1115 "/payment_accounts/$payment_account_id/transactions",
1116 );
1117 }
1118  
1119 1 ▶sub handle_customer_notification {
Subroutine handle_customer_notification: called CC=1 SCAR=0.0
Pod documented
1120 1 my ( $self,$id ) = @_;
1121 1 return $self->_new_resource( 'customer_notifications',{ id => $id } )->handle;
1122 }
1123  
1124 ################################################################
1125 #
1126 # BACK COMPATIBILITY WITH V1 (BASIC) API SECTION FOLLOWS
1127 # the Pro version of the API is built on "redirect flows" when
1128 # using their hosted pages, so we can make it back compatible
1129 #
1130 ################################################################
1131  
1132 =head1 BACK COMPATIBILITY METHODS
1133  
1134 These methods are provided for those who want to move from the v1 (Basic)
1135 API with minimal changes in your application code.
1136  
1137 The new_bill_url, new_pre_authorization_url, and new_subscription_url methods
1138 all return a URL for redirecting the user to to complete a direct debit mandate
1139 that will allow you to setup payments, and they all take the same parameters.
1140  
1141 See L<https://developer.gocardless.com/api-reference/#redirect-flows-create-a-redirect-flow>
1142 for more information.
1143  
1144 =head2 new_bill_url
1145  
1146 Return a redirect flow URL for setting up a one off payment
1147  
1148 =head2 new_pre_authorization_url
1149  
1150 Return a redirect flow URL for setting up a mandate
1151  
1152 =head2 new_subscription_url
1153  
1154 Return a redirect flow URL for setting up a subscription
1155  
1156 All three methods take the following parameters:
1157  
1158 my $url = $GoCardless->new_bill_url(
1159  
1160 # required
1161 session_token => $session_token,
1162 success_redirect_url => $success_callback_url,
1163  
1164 # optional
1165 scheme => $direct_debit_scheme
1166 description => $description,
1167 prefilled_customer => { ... }, # see documentation above
1168 links => { ... }, # see documentation above
1169 );
1170  
1171 =cut
1172  
1173 1 ▶sub new_bill_url {
Subroutine new_bill_url: called CC=1 SCAR=0.0
Pod documented
1174 1 my ( $self,%params ) = @_;
1175 1 return $self->_redirect_flow_from_legacy_params( 'bill',%params );
1176 }
1177  
1178 1 ▶sub new_pre_authorization_url {
Subroutine new_pre_authorization_url: called CC=1 SCAR=0.0
Pod documented
1179 1 my ( $self,%params ) = @_;
1180 1 return $self->_redirect_flow_from_legacy_params( 'pre_authorization',%params );
1181 }
1182  
1183 1 ▶sub new_subscription_url {
Subroutine new_subscription_url: called CC=1 SCAR=0.0
Pod documented
1184 1 my ( $self,%params ) = @_;
1185 1 return $self->_redirect_flow_from_legacy_params( 'subscription',%params );
1186 }
1187  
1188 3 ▶sub _redirect_flow_from_legacy_params {
Subroutine _redirect_flow_from_legacy_params: called CC=17 SCAR=38.7
1189 3 my ( $self,$type,%params ) = @_;
1190  
1191 3 for ( qw/ session_token success_redirect_url / ) {
1192 6 ▶ $params{$_} // confess( "$_ is required for new_${type}_url (v2)" );
Condition50%1/2
$params{$_} // confess("$_ is required for new_${type}_url (v2)")
l!l
60
MC/DC0%0/1
$params{$_} // confess("$_ is required for new_${type}_url (v2)")
$params{$_}
Truth table50%1/2
A
A= $params{$_}
Aresult
11
00
1193 }
1194  
1195 # we can't just pass through %params as GoCardless will throw an error
1196 # if it receives any unknown parameters
1197 return $self->client->_new_redirect_flow_url({
1198 ( $params{scheme} ? ( scheme => $params{scheme} ) : () ),
1199 ( $params{description} // $params{name} ? ( description => $params{description} // $params{name} ) : () ),
1200 session_token => $params{session_token},
1201 success_redirect_url => $params{success_redirect_url},
1202  
1203 ( $params{prefilled_customer}
1204 ? (
1205 0 ▶ prefilled_customer => { %{ $params{prefilled_customer} } }
Branch: 7 branches
BranchTrueFalseTotal
$params{'user'}{'billing_address1'} ? : 0 3 3
$params{'user'}{'billing_address2'} ? : 0 3 3
$params{'user'}{'billing_address3'} ? : 0 3 3
$params{'user'}{'billing_town'} ? : 0 3 3
$params{'user'}{'billing_postcode'} ? : 0 3 3
$params{'user'}{'last_name'} ? : 0 3 3
$params{'user'}{'first_name'} ? : 0 3 3
1206 )
1207 : (
1208 prefilled_customer => {
1209  
1210 # only include fields that are defined in the prefilled_customer, otherwise
1211 # gocardless will try to assume the user's location and thus the scheme
1212 ( $params{user}{billing_address1} ? ( address_line1 => $params{user}{billing_address1} ) : () ),
1213 ( $params{user}{billing_address2} ? ( address_line2 => $params{user}{billing_address2} ) : () ),
1214 ( $params{user}{billing_address3} ? ( address_line3 => $params{user}{billing_address3} ) : () ),
1215 ( $params{user}{billing_town} ? ( city => $params{user}{billing_town} ) : () ),
1216 ( $params{user}{billing_postcode} ? ( postal_code => $params{user}{billing_postcode} ) : () ),
1217  
1218 ( $params{user}{last_name} ? ( family_name => $params{user}{last_name} ) : () ),
1219 ( $params{user}{first_name} ? ( given_name => $params{user}{first_name} ) : () ),
1220  
1221 (
1222 27 ▶ map { ( $params{user}{$_} ? ( $_ => $params{user}{$_} ) : () ) }
Branch: 2 branches
BranchTrueFalseTotal
$params{'user'}{$_} ? : 0 27 27
$params{'links'}{'creditor'} ? : 0 3 3
1223 qw/ city company_name country_code email given_name language region swedish_identity_number postal_code /
1224 ),
1225 },
1226 )
1227 ),
1228  
1229 (
1230 $params{links}{creditor}
1231 ? ( links => { creditor => $params{links}{creditor} } )
1232 3 ▶ : ()
Branch: 3 branches
BranchTrueFalseTotal
$params{'scheme'} ? : 0 3 3
$params{'description'} // $params{'name'} ? : 3 0 3
$params{'prefilled_customer'} ? : 0 3 3
Condition33%2/6
$params{"description"} // $params{"name"}
l!l&&r!l&&!r
300
$params{"description"} // $params{"name"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$params{"description"} // $params{"name"}
$params{"description"} $params{"name"}
MC/DC0%0/2
$params{"description"} // $params{"name"}
$params{"description"} $params{"name"}
Truth table33%1/3
A // B
A= $params{"description"}B= $params{"name"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $params{"description"}B= $params{"name"}
ABresult
1X1
011
000
1233 ),
1234 });
1235 }
1236  
1237 =head2 confirm_resource
1238  
1239 After a user completes the form in the redirect flow (using a URL generated from
1240 one of the new_.*?url methods above) GoCardless will redirect them back to the
1241 success_redirect_url with a redirect_flow_id, at which point you can call this
1242 method to confirm the mandate and set up a one off payment, subscription, etc
1243  
1244 The object returned will depend on the C<type> parameter passed to the method
1245  
1246 my $Payment = $GoCardless->confirm_resource(
1247  
1248 # required
1249 redirect_flow_id => $redirect_flow_id,
1250 session_token => $session_token,
1251 type => 'payment', # one of bill, payment, pre_auth, subscription
1252 amount => 0,
1253 currency => 'GBP',
1254  
1255 # required in the case of type being "subscription"
1256 interval_unit =>
1257 interval =>
1258 start_at =>
1259 );
1260  
1261 C<session_token> must be the one passed to new_bill_url,
1262 new_pre_authorization_url or new_subscription_url for this user's session,
1263 otherwise an exception is thrown and nothing is created. A flow that already
1264 has a mandate passes this check on every call, so call confirm_resource once
1265 per flow and discard the session_token afterwards.
1266  
1267 =cut
1268  
1269 # BACK COMPATIBILITY method, in which we (try to) return the correct object for
1270 # the required type as this is how the v1 API works
1271 6 ▶sub confirm_resource {
Subroutine confirm_resource: called CC=9 SCAR=22.6
Pod documented
1272 6 my ( $self,%params ) = @_;
1273  
1274 6 for ( qw/ redirect_flow_id session_token type amount currency / ) {
1275 30 ▶ $params{$_} // confess( "$_ is required for confirm_resource (v2)" );
Condition50%1/2
$params{$_} // confess("$_ is required for confirm_resource (v2)")
l!l
300
MC/DC0%0/1
$params{$_} // confess("$_ is required for confirm_resource (v2)")
$params{$_}
Truth table50%1/2
A
A= $params{$_}
Aresult
11
00
1276 }
1277  
1278 6 my $r_flow_id = $params{redirect_flow_id};
1279 6 my $type = $params{type};
1280 6 my $amount = $params{amount};
1281 6 my $currency = $params{currency};
1282 6 my $int_unit = $params{interval_unit};
1283 6 my $interval = $params{interval};
1284 6 my $start_at = $params{start_at};
1285  
1286 6 ▶ if ( my $RedirectFlow = $self->client->_confirm_redirect_flow( $r_flow_id,$params{session_token} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $RedirectFlow = $self->client->_confirm_redirect_flow($r_flow_id, $params{"session_token"})) 3 0 3
1287  
1288 # now we have a confirmed redirect flow object we can create the
1289 # payment, subscription, whatever
1290 3 ▶ if ( $type =~ /bill|payment/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ /bill|payment/i) { } 1 2 3
elsif ($type =~ /pre_auth/i) { } 1 1 2
elsif ($type =~ /subscription/i) { } 1 0 1
1291  
1292 # Bill -> Payment
1293 my $post_data = {
1294 payments => {
1295 amount => $amount,
1296 currency => $currency,
1297 links => { mandate => ( $RedirectFlow->links // {} )->{mandate} },
1298 },
1299 1 ▶ };
Condition50%1/2
$RedirectFlow->links // {}
l!l
10
MC/DC0%0/1
$RedirectFlow->links // {}
$RedirectFlow->links
Truth table50%1/2
A
A= $RedirectFlow->links
Aresult
11
00
1300  
1301 1 my $data = $self->client->api_post( "/payments",$post_data );
1302  
1303 return Business::GoCardless::Payment->new(
1304 client => $self->client,
1305 1 %{ $data->{payments} },
1306 );
1307  
1308 } elsif ( $type =~ /pre_auth/i ) {
1309  
1310 # a pre authorization is, effectively, a redirect flow
1311 1 return $RedirectFlow;
1312  
1313 } elsif ( $type =~ /subscription/i ) {
1314  
1315 my $post_data = {
1316 subscriptions => {
1317 amount => $amount,
1318 currency => $currency,
1319 interval_unit => $int_unit,
1320 interval => $interval,
1321 start_date => $start_at,
1322 links => { mandate => ( $RedirectFlow->links // {} )->{mandate} },
1323 },
1324 1 ▶ };
Condition50%1/2
$RedirectFlow->links // {}
l!l
10
MC/DC0%0/1
$RedirectFlow->links // {}
$RedirectFlow->links
Truth table50%1/2
A
A= $RedirectFlow->links
Aresult
11
00
1325  
1326 1 my $data = $self->client->api_post( "/subscriptions",$post_data );
1327  
1328 return Business::GoCardless::Subscription->new(
1329 client => $self->client,
1330 1 %{ $data->{subscriptions} },
1331 );
1332 }
1333  
1334 # don't know what to do, complain
1335 Business::GoCardless::Exception->throw({
1336 0 message => "Unknown type ($type) in ->confirm_resource",
1337 });
1338 }
1339  
1340 Business::GoCardless::Exception->throw({
1341 0 message => "Failed to get RedirectFlow for $r_flow_id",
1342 });
1343 }
1344  
1345 1 ▶sub users { shift->customers( @_ ); }
Subroutine users: called CC=1 SCAR=0.0
Pod undocumented
1346  
1347 =head1 SEE ALSO
1348  
1349 L<Business::GoCardless>
1350  
1351 L<Business::GoCardless::Resource>
1352  
1353 L<Business::GoCardless::Payment>
1354  
1355 L<Business::GoCardless::Payout>
1356  
1357 L<Business::GoCardless::Client>
1358  
1359 L<Business::GoCardless::Subscription>
1360  
1361 L<Business::GoCardless::User>
1362  
1363 L<Business::GoCardless::Webhook::V2>
1364  
1365 =head1 AUTHOR
1366  
1367 Lee Johnson - C<lee@payprop.com>
1368  
1369 =head1 LICENSE
1370  
1371 This library is free software; you can redistribute it and/or modify it under
1372 the same terms as Perl itself. If you would like to contribute documentation,
1373 features, bug fixes, or anything else then please raise an issue / pull request:
1374  
1375 https://github.com/payprop/business-gocardless
1376  
1377 =cut
1378  
1379 4 1;
1380  
1381 # vim: ts=4:sw=4:et
↑ Summary GoCardless/Client.pm »