File Coverage

blib/lib/Business/BalancedPayments/V11.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Business::BalancedPayments::V11;
2 12     12   55 use Moo;
  12         16  
  12         61  
3             extends 'Business::BalancedPayments::Base';
4              
5             our $VERSION = '1.0600'; # VERSION
6              
7 12     12   3610 use Carp qw(croak);
  12         18  
  12         669  
8 12     12   12992 use Method::Signatures;
  0            
  0            
9              
10             has marketplaces_uri => ( is => 'ro', default => '/marketplaces' );
11              
12             has marketplaces => ( is => 'ro', lazy => 1, builder => '_build_marketplaces' );
13              
14             method BUILD(@args) {
15             $self->ua->default_header(
16             accept => 'application/vnd.api+json;revision=1.1');
17             }
18              
19             around get_card => _unpack_response('cards');
20              
21             around create_card => _unpack_response('cards');
22              
23             method add_card(HashRef $card, HashRef :$customer!) {
24             my $card_href = $card->{href} or croak 'The card href is missing';
25             my $cust_href = $customer->{href} or croak 'The customer href is missing';
26             return $self->put($card->{href}, { customer => $cust_href })->{cards}[0];
27             }
28              
29             around get_customer => _unpack_response('customers');
30              
31             around create_customer => _unpack_response('customers');
32              
33             method update_customer(HashRef $customer) {
34             my $cust_href = $customer->{href} or croak 'The customer href is missing';
35             return $self->put($cust_href, $customer)->{customers}[0];
36             }
37              
38             method get_hold(Str $id) {
39             my $res = $self->get($self->_uri('card_holds', $id));
40             return $res ? $res->{card_holds}[0] : undef;
41             }
42              
43             method create_hold(HashRef $hold, HashRef :$card!) {
44             croak 'The hold amount is missing' unless $hold->{amount};
45             my $card_href = $card->{href} or croak 'The card href is missing';
46             return $self->post("$card_href/card_holds", $hold)->{card_holds}[0];
47             }
48              
49             method capture_hold(HashRef $hold, HashRef :$debit={}) {
50             my $hold_href = $hold->{href} or croak 'The hold href is missing';
51             return $self->post("$hold_href/debits", $debit)->{debits}[0];
52             }
53              
54             method void_hold(HashRef $hold) {
55             my $hold_href = $hold->{href} or croak 'The hold href is missing';
56             return $self->put($hold_href, { is_void => 'true' })->{card_holds}[0];
57             }
58              
59             method create_debit(HashRef $debit, HashRef :$card, HashRef :$bank) {
60             my $source = $card || $bank or croak 'A bank or card is required';
61             croak 'The debit amount is missing' unless $debit->{amount};
62             my $source_href = $source->{href}
63             or croak 'The href for the funding source is missing';
64             return $self->post("$source_href/debits", $debit)->{debits}[0];
65             }
66              
67             around get_debit => _unpack_response('debits');
68              
69             method refund_debit(HashRef $debit) {
70             my $debit_href = $debit->{href} or croak 'The debit href is missing';
71             return $self->post("$debit_href/refunds", $debit)->{refunds}[0];
72             }
73              
74             around get_bank_account => _unpack_response('bank_accounts');
75              
76             around create_bank_account => _unpack_response('bank_accounts');
77              
78             method add_bank_account(HashRef $bank, HashRef :$customer!) {
79             my $bank_href = $bank->{href} or croak 'The bank href is missing';
80             my $cust_href = $customer->{href} or croak 'The customer href is missing';
81             my $res = $self->put($bank->{href}, { customer => $cust_href });
82             return $res->{bank_accounts}[0];
83             }
84              
85             method create_credit(HashRef $credit, HashRef :$bank_account, HashRef :$card) {
86             croak 'The credit amount is missing' unless $credit->{amount};
87             if ($bank_account) {
88             my $bank_href = $bank_account->{href}
89             or croak 'The bank_account href is missing';
90             return $self->post("$bank_href/credits", $credit)->{credits}[0];
91             } elsif ($card) {
92             my $card_href = $card->{href} or croak 'The card href is missing';
93             return $self->post("$card_href/credits", $credit)->{credits}[0];
94             } else {
95             croak 'A bank or card param is required';
96             }
97             }
98              
99             around get_credit => _unpack_response('credits');
100              
101             method update_bank_account(HashRef $bank) {
102             my $bank_href = $bank->{href} or croak 'The bank_account href is missing';
103             return $self->put($bank_href, $bank)->{bank_accounts}[0];
104             }
105              
106             method create_bank_verification(HashRef :$bank_account!) {
107             my $bank_href = $bank_account->{href}
108             or croak 'The bank_account href is missing';
109             return $self->post("$bank_href/verifications", {})
110             ->{bank_account_verifications}[0];
111             }
112              
113             method get_bank_verification(Str $id) {
114             my $res = $self->get("/verifications/$id");
115             return $res ? $res->{bank_account_verifications}[0] : undef;
116             }
117              
118             method confirm_bank_verification(HashRef $verification, Int :$amount_1!, Int :$amount_2!) {
119             my $ver_href = $verification->{href}
120             or croak 'The verification href is missing';
121             return $self->put($ver_href, {amount_1 => $amount_1, amount_2 => $amount_2})
122             ->{bank_account_verifications}[0];
123             }
124              
125             method create_check_recipient(HashRef $rec) {
126             croak 'The recipient name is missing' unless defined $rec->{name};
127             croak 'The recipient address line1 is missing'
128             unless $rec->{address}{line1};
129             croak 'The recipient address postal_code is missing'
130             unless $rec->{address}{postal_code};
131             my $res = $self->post('/check_recipients', $rec);
132             return $res->{check_recipients}[0];
133             }
134              
135             method get_dispute(Str $id) {
136             my $res = $self->get($self->_uri('disputes', $id));
137             return $res ? $res->{disputes}[0] : undef;
138             }
139              
140             method get_disputes(HashRef $query = {}) {
141             return $self->get($self->_uri('disputes'), $query);
142             }
143              
144             around get_disputes => _autopaginate();
145              
146             method create_check_recipient_credit(HashRef $credit, HashRef :$check_recipient!) {
147             my $rec_id = $check_recipient->{id}
148             or croak 'The check_recipient hashref needs an id';
149             croak 'The credit must contain an amount' unless $credit->{amount};
150             my $res = $self->post("/check_recipients/$rec_id/credits", $credit);
151             return $res->{credits}[0];
152             }
153              
154             method get_all(HashRef $data, Maybe[CodeRef] :$page_handler) {
155             my ($key) = grep !/^(links|meta)$/, keys %$data;
156             croak "Could not find the top level resource" unless $key;
157             my $result = $data->{$key};
158             while ( my $next = $data->{meta}{next} ) {
159             $data = $self->get($next);
160             $page_handler->( $data ) if $page_handler;
161             push @$result, @{ $data->{$key} };
162             }
163             return { $key => $result };
164             }
165              
166             method _build_marketplaces { $self->get($self->marketplaces_uri) }
167              
168             method _build_marketplace { $self->marketplaces->{marketplaces}[0] }
169              
170             method _build_uris {
171             my $links = $self->marketplaces->{links};
172             return { map { (split /^marketplaces./)[1] => $links->{$_} } keys %$links };
173             }
174              
175             sub _unpack_response {
176             my ($name) = @_;
177             return sub {
178             my ($orig, $self, @args) = @_;
179             my $res = $self->$orig(@args);
180             return $res->{$name}[0] if $res;
181             return $res;
182             }
183             };
184              
185             sub _autopaginate {
186             return sub {
187             my ($orig, $self, $query, %params) = @_;
188             my $res = $self->$orig($query);
189             return $self->get_all($res, page_handler => $params{page_handler})
190             if $params{page_handler} and 'CODE' eq ref $params{page_handler};
191             return $res;
192             }
193             };
194              
195             1;
196              
197             __END__
198              
199             =pod
200              
201             =encoding UTF-8
202              
203             =head1 NAME
204              
205             Business::BalancedPayments::V11
206              
207             =head1 VERSION
208              
209             version 1.0600
210              
211             =head1 AUTHORS
212              
213             =over 4
214              
215             =item *
216              
217             Ali Anari <ali@tilt.com>
218              
219             =item *
220              
221             Khaled Hussein <khaled@tilt.com>
222              
223             =item *
224              
225             Naveed Massjouni <naveed@tilt.com>
226              
227             =item *
228              
229             Al Newkirk <al@tilt.com>
230              
231             =item *
232              
233             Will Wolf <will@tilt.com>
234              
235             =back
236              
237             =head1 COPYRIGHT AND LICENSE
238              
239             This software is copyright (c) 2012 by Crowdtilt, Inc..
240              
241             This is free software; you can redistribute it and/or modify it under
242             the same terms as the Perl 5 programming language system itself.
243              
244             =cut