File Coverage

blib/lib/WebService/Stripe.pm
Criterion Covered Total %
statement 62 509 12.1
branch 0 364 0.0
condition 0 188 0.0
subroutine 47 86 54.6
pod n/a
total 109 1147 9.5


line stmt bran cond sub pod time code
1             package WebService::Stripe;
2 12     12   888311 use Moo;
  12         139054  
  12         78  
3             with 'WebService::Client';
4              
5             our $VERSION = '1.0100'; # VERSION
6              
7 12     12   19711 use Carp qw(croak);
  12         26  
  12         567  
8 12     12   9580 use HTTP::Request::Common qw( POST );
  12         312903  
  12         820  
9 12     12   11456 use Method::Signatures;
  12         842055  
  12         92  
10 12     12   14381 use Data::NestedParams;
  12         11944  
  12         829  
11             use constant {
12 12         2581 FILE_UPLOADS_URL => 'https://uploads.stripe.com/v1/files',
13             FILE_PURPOSE_ID_DOCUMENT => 'identity_document',
14             MARKETPLACES_MIN_VERSION => '2014-11-05',
15 12     12   70 };
  12         26  
16              
17             has api_key => (
18             is => 'ro',
19             required => 1,
20             );
21              
22             has '+serializer' => (
23             default => sub {
24             sub {
25             my ($data, %args) = @_;
26             return collapse_nested_params($data);
27             }
28             }
29             );
30              
31             has version => (
32             is => 'ro',
33             default => MARKETPLACES_MIN_VERSION,
34             );
35              
36             has '+base_url' => ( default => 'https://api.stripe.com' );
37              
38             has '+content_type' => ( default => 'application/x-www-form-urlencoded' );
39              
40 12     12   1777711 method BUILD(@args) {
  1     1   13  
  1         6  
41 1         6 $self->ua->default_headers->authorization_basic( $self->api_key, '' );
42 1         5204 $self->ua->default_header( 'Stripe-Version' => $self->version );
43             }
44              
45 12 0 0 12   60873 method next(HashRef $thing, HashRef :$query, HashRef :$headers) {
  0 0 0 0      
  0 0 0        
  0 0 0        
  0 0 0        
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
46 0   0       $query ||= {};
47 0 0         return undef unless $thing->{has_more};
48 0 0         my $starting_after = $thing->{data}[-1]{id} or return undef;
49             return $self->get( $thing->{url},
50 0           { %$query, starting_after => $starting_after }, headers => $headers );
51             }
52              
53 12 0 0 12   56700 method create_customer(Maybe[HashRef] $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
54 0           return $self->post( "/v1/customers", $data, headers => $headers );
55             }
56              
57 12 0 0 12   42954 method create_recipient(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
58 0           return $self->post( "/v1/recipients", $data, headers => $headers );
59             }
60              
61 12 0 0 12   42593 method get_recipient(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
62 0           return $self->get( "/v1/recipients/$id", {}, headers => $headers );
63             }
64              
65 12 0 0 12   42911 method get_application_fee(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
66 0           return $self->get( "/v1/application_fees/$id", {}, headers => $headers );
67             }
68              
69 12 0   12   29460 method get_balance(:$headers) {
  0 0   0      
  0            
  0            
  0            
  0            
  0            
70 0           return $self->get( "/v1/balance", {}, headers => $headers );
71             }
72              
73 12 0 0 12   41388 method get_customer(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
74 0           return $self->get( "/v1/customers/$id", {}, headers => $headers );
75             }
76              
77 12 0 0 12   56821 method update_customer(Str $id, HashRef :$data!, :$headers) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
78 0           return $self->post( "/v1/customers/$id", $data, headers => $headers );
79             }
80              
81 12 0 0 12   47726 method get_customers(HashRef :$query, :$headers) {
  0 0 0 0      
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
82 0           return $self->get( "/v1/customers", $query, headers => $headers );
83             }
84              
85 12 0 0 12   56600 method create_card(HashRef $data, :$customer_id!, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
86 0           return $self->post(
87             "/v1/customers/$customer_id/cards", $data, headers => $headers );
88             }
89              
90 12 0 0 12   60501 method get_charge(HashRef|Str $charge, :$query, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
91 0 0         $charge = $charge->{id} if ref $charge;
92 0           return $self->get( "/v1/charges/$charge", $query, headers => $headers );
93             }
94              
95 12 0 0 12   43787 method create_charge(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
96 0           return $self->post( "/v1/charges", $data, headers => $headers );
97             }
98              
99 12 0 0 12   61658 method update_charge(HashRef|Str $charge, HashRef :$data!, :$headers) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
100 0 0         $charge = $charge->{id} if ref $charge;
101 0           return $self->post( "/v1/charges/$charge", $data, headers => $headers );
102             }
103              
104 12 0 0 12   56816 method capture_charge(Str $id, HashRef :$data, :$headers) {
  0 0 0 0      
  0 0 0        
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
105 0           return $self->post( "/v1/charges/$id/capture", $data, headers => $headers );
106             }
107              
108 12 0 0 12   56420 method refund_charge(Str $id, HashRef :$data, :$headers) {
  0 0 0 0      
  0 0 0        
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
109 0           return $self->post( "/v1/charges/$id/refunds", $data, headers => $headers );
110             }
111              
112 12 0 0 12   56526 method refund_app_fee(Str $fee_id, HashRef :$data, :$headers) {
  0 0 0 0      
  0 0 0        
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
113 0           my $url = "/v1/application_fees/$fee_id/refunds";
114 0           return $self->post( $url, $data, headers => $headers );
115             }
116              
117 12 0 0 12   62303 method add_source(HashRef|Str $cust, HashRef $data, :$headers) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
118 0 0         $cust = $cust->{id} if ref $cust;
119 0           return $self->post( "/v1/customers/$cust/sources", $data, headers => $headers );
120             }
121              
122 12 0 0 12   44058 method create_token(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
123 0           return $self->post( "/v1/tokens", $data, headers => $headers );
124             }
125              
126 12 0 0 12   42818 method get_token(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
127 0           return $self->get( "/v1/tokens/$id", {}, headers => $headers );
128             }
129              
130 12 0 0 12   42912 method create_account(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
131 0           return $self->post( "/v1/accounts", $data, headers => $headers );
132             }
133              
134 12 0 0 12   42493 method get_account(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
135 0           return $self->get( "/v1/accounts/$id", {}, headers => $headers );
136             }
137              
138 12 0   12   29384 method get_platform_account(:$headers) {
  0 0   0      
  0            
  0            
  0            
  0            
  0            
139 0           return $self->get( "/v1/account", {}, headers => $headers );
140             }
141              
142 12 0 0 12   55545 method update_account(Str $id, HashRef :$data!, :$headers) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
143 0           return $self->post( "/v1/accounts/$id", $data, headers => $headers );
144             }
145              
146 12 0 0 12   49927 method upload_identity_document(HashRef|Str $account_id, Str $filepath) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
147             return $self->req(
148             POST FILE_UPLOADS_URL,
149 0 0         Stripe_Account => ( ref $account_id ? $account_id->{id}: $account_id ),
150             Content_Type => 'form-data',
151             Content => [
152             purpose => FILE_PURPOSE_ID_DOCUMENT,
153             file => [ $filepath ],
154             ],
155             );
156             }
157              
158 12 0 0 12   57921 method add_bank(HashRef $data, Str :$account_id!, :$headers) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
159 0           return $self->post(
160             "/v1/accounts/$account_id/bank_accounts", $data, headers => $headers );
161             }
162              
163 12 0 0 12   73372 method update_bank(Str $id, Str :$account_id!, HashRef :$data!, :$headers) {
  0 0 0 0      
  0 0 0        
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
164 0           return $self->post( "/v1/accounts/$account_id/bank_accounts/$id", $data,
165             headers => $headers );
166             }
167              
168 12 0 0 12   60636 method delete_bank(Str $id, Str :$account_id!, :$headers) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
169 0           return $self->delete(
170             "/v1/accounts/$account_id/bank_accounts/$id", headers => $headers );
171             }
172              
173 12 0 0 12   47959 method get_banks(Str :$account_id!, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
174 0           return $self->get(
175             "/v1/accounts/$account_id/bank_accounts", {}, headers => $headers );
176             }
177              
178 12 0 0 12   43299 method create_transfer(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
179 0           return $self->post( "/v1/transfers", $data, headers => $headers );
180             }
181              
182 12 0 0 12   43716 method get_transfer(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
183 0           return $self->get( "/v1/transfers/$id", {}, headers => $headers );
184             }
185              
186 12 0 0 12   44694 method get_transfers(HashRef :$query, :$headers) {
  0 0 0 0      
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
187 0           return $self->get( "/v1/transfers", $query, headers => $headers );
188             }
189              
190 12 0 0 12   57572 method update_transfer(Str $id, HashRef :$data!, :$headers) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
191 0           return $self->post( "/v1/transfers/$id", $data, headers => $headers );
192             }
193              
194 12 0 0 12   44399 method cancel_transfer(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
195 0           return $self->post("/v1/transfers/$id/cancel", undef, headers => $headers);
196             }
197              
198 12 0 0 12   102835 method create_reversal($xfer_id, HashRef :$data = {}, HashRef :$headers = {}) {
  0 0 0 0      
  0 0          
  0 0          
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
199 0           return $self->post("/v1/transfers/$xfer_id/reversals", $data,
200             headers => $headers,
201             );
202             }
203              
204 12 0 0 12   48110 method get_bitcoin_receivers(HashRef :$query, :$headers) {
  0 0 0 0      
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
205 0           return $self->get( "/v1/bitcoin/receivers", $query, headers => $headers );
206             }
207              
208 12 0 0 12   43066 method create_bitcoin_receiver(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
209 0           return $self->post( "/v1/bitcoin/receivers", $data, headers => $headers );
210             }
211              
212 12 0 0 12   43365 method get_bitcoin_receiver(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
213 0           return $self->get( "/v1/bitcoin/receivers/$id", {}, headers => $headers );
214             }
215              
216 12 0 0 12   44450 method get_events(HashRef :$query, :$headers) {
  0 0 0 0      
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
217 0           return $self->get( "/v1/events", $query, headers => $headers );
218             }
219              
220 12 0 0 12   43372 method get_event(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
221 0           return $self->get( "/v1/events/$id", {}, headers => $headers );
222             }
223              
224             # ABSTRACT: Stripe API bindings
225              
226              
227             1;
228              
229             __END__
230              
231             =pod
232              
233             =encoding UTF-8
234              
235             =head1 NAME
236              
237             WebService::Stripe - Stripe API bindings
238              
239             =head1 VERSION
240              
241             version 1.0100
242              
243             =head1 SYNOPSIS
244              
245             my $stripe = WebService::Stripe->new(
246             api_key => 'secret',
247             version => '2014-11-05', # optional
248             );
249             my $customer = $stripe->get_customer('cus_57eDUiS93cycyH');
250              
251             =head1 TESTING
252              
253             Set the PERL_STRIPE_TEST_API_KEY environment variable to your Stripe test
254             secret, then run tests as you normally would using prove.
255              
256             =head1 HEADERS
257              
258             WebService::Stripe supports passing custom headers to any API request by passing a hash of header values as the optional C<headers> named parameter:
259              
260             $stripe->create_charge({ ... }, headers => { stripe_account => "acct_123" })
261              
262             Note that header names are normalized: C<foo_bar>, C<Foo-Bar>, and C<foo-bar> are equivalent.
263              
264             Three headers stand out in particular:
265              
266             =over
267              
268             =item Stripe-Version
269              
270             This indicates the version of the Stripe API to use. If not given, we default to C<2014-11-05>, which is the earliest version of the Stripe API to support marketplaces.
271              
272             =item Stripe-Account
273              
274             This specifies the ID of the account on whom the request is being made. It orients the Stripe API around that account, which may limit what records or actions are able to be taken. For example, a `get_card` request will fail if given the ID of a card that was not associated with the account.
275              
276             =item Idempotency-Key
277              
278             All POST methods support idempotent requests through setting the value of an Idempotency-Key header. This is useful for preventing a request from being executed twice, e.g. preventing double-charges. If two requests are issued with the same key, only the first results in the creation of a resource; the second returns the latest version of the existing object.
279              
280             This feature is in ALPHA and subject to change without notice. Contact Stripe to confirm the latest behavior and header name.
281              
282             =back
283              
284             =head1 METHODS
285              
286             =head2 get_customer
287              
288             get_customer($id)
289              
290             Returns the customer for the given id.
291              
292             =head2 create_customer
293              
294             create_customer($data)
295              
296             Creates a customer.
297             The C<$data> hashref is optional.
298             Returns the customer.
299              
300             Example:
301              
302             $customer = $stripe->create_customer({ email => 'bob@foo.com' });
303              
304             =head2 update_customer
305              
306             update_customer($id, data => $data)
307              
308             Updates a customer.
309             Returns the updated customer.
310              
311             Example:
312              
313             $customer = $stripe->update_customer($id, data => { description => 'foo' });
314              
315             =head2 get_customers
316              
317             get_customers(query => $query)
318              
319             Returns a list of customers.
320             The query param is optional.
321              
322             =head2 next
323              
324             next($collection)
325              
326             Returns the next page of results for the given collection.
327              
328             Example:
329              
330             my $customers = $stripe->get_customers;
331             ...
332             while ($customers = $stripe->next($customers)) {
333             ...
334             }
335              
336             =head2 create_recipient
337              
338             create_recipient($data)
339              
340             Creates a recipient.
341             The C<$data> hashref is required and must contain at least C<name> and
342             C<type> (which can be C<individual> or C<corporate> as per Stripe's
343             documentation), but can contain more (see Stripe Docs).
344             Returns the recipient.
345              
346             Example:
347              
348             $recipient = $stripe->create_recipient({
349             name => 'John Doe',
350             type => 'individual,
351             });
352              
353             =head2 get_recipient
354              
355             get_recipient($id)
356              
357             Retrieves a recipient by id.
358             Returns the recipient.
359              
360             Example:
361              
362             $recipient = $stripe->get_recipient('rcp_123');
363              
364             =head2 create_card
365              
366             create_card($data, customer_id => 'cus_123')
367              
368             =head2 get_charge
369              
370             get_charge($id, query => { expand => ['customer'] })
371              
372             Returns the charge for the given id. The optional :$query parameter allows
373             passing query arguments. Passing an arrayref as a query param value will expand
374             it into Stripe's expected array format.
375              
376             =head2 create_charge
377              
378             create_charge($data)
379              
380             Creates a charge.
381              
382             =head2 capture_charge
383              
384             capture_charge($id, data => $data)
385              
386             Captures the charge with the given id.
387             The data param is optional.
388              
389             =head2 refund_charge
390              
391             refund_charge($id, data => $data)
392              
393             Refunds the charge with the given id.
394             The data param is optional.
395              
396             =head2 refund_app_fee
397              
398             refund_app_fee($fee_id, data => $data)
399              
400             Refunds the application fee with the given id.
401             The data param is optional.
402              
403             =head2 update_charge
404              
405             update_charge($id, data => $data)
406              
407             Updates an existing charge object.
408              
409             =head2 add_source
410              
411             add_source($cust_id, $card_data)
412              
413             Adds a new funding source (credit card) to an existing customer.
414              
415             =head2 get_token
416              
417             get_token($id)
418              
419             =head2 create_token
420              
421             create_token($data)
422              
423             =head2 get_account
424              
425             get_account($id)
426              
427             =head2 create_account
428              
429             create_account($data)
430              
431             =head2 update_account
432              
433             update_account($id, data => $data)
434              
435             =head2 get_platform_account
436              
437             get_platform_account($id)
438              
439             =head2 upload_identity_document
440              
441             Uploads a photo ID to an account.
442              
443             Example:
444              
445             my $account = $stripe->create_account({
446             managed => 'true',
447             country => 'CA',
448             });
449              
450             my $file = $stripe->upload_identity_document( $account, '/tmp/photo.png' );
451             $stripe->update_account( $account->{id}, data => {
452             legal_entity[verification][document] => $file->{id},
453             });
454              
455             =head2 add_bank
456              
457             add_bank($data, account_id => $account_id)
458              
459             Add a bank to an account.
460              
461             Example:
462              
463             my $account = $stripe->create_account({
464             managed => 'true',
465             country => 'CA',
466             });
467              
468             my $bank = $stripe->add_bank(
469             {
470             'bank_account[country]' => 'CA',
471             'bank_account[currency]' => 'cad',
472             'bank_account[routing_number]' => '00022-001',
473             'bank_account[account_number]' => '000123456789',
474             },
475             account_id => $account->{id},
476             );
477              
478             # or add a tokenised bank
479              
480             my $bank_token = $stripe->create_token({
481             'bank_account[country]' => 'CA',
482             'bank_account[currency]' => 'cad',
483             'bank_account[routing_number]' => '00022-001',
484             'bank_account[account_number]' => '000123456789',
485             });
486              
487             $stripe->add_bank(
488             { bank_account => $bank_token->{id} },
489             account_id => $account->{id},
490             );
491              
492             =head2 update_bank
493              
494             update_bank($id, account_id => $account_id, data => $data)
495              
496             =head2 create_transfer
497              
498             create_transfer($data)
499              
500             =head2 get_transfer
501              
502             get_transfer($id)
503              
504             =head2 get_transfers
505              
506             get_transfers(query => $query)
507              
508             =head2 update_transfer
509              
510             update_transfer($id, data => $data)
511              
512             =head2 cancel_transfer
513              
514             cancel_transfer($id)
515              
516             =head2 create_reversal
517              
518             Reverses an existing transfer.
519              
520             L<Stripe Documentation|https://stripe.com/docs/api/python#transfer_reversals>
521              
522             Example:
523              
524             $ws_stripe->create_reversal(
525             # Transfer ID (required)
526             $xfer_id,
527             data => {
528             # POST data (optional)
529             refund_application_fee => 'true',
530             amount => 100,
531             description => 'Invoice Correction',
532             'metadata[local_reversal_id]' => 'rvrsl_123',
533             'metadata[requester]' => 'John Doe'
534             },
535             headers => {
536             # Headers (optional)
537             stripe_account => $account->{'id'}
538             }
539             );
540              
541             =head2 get_balance
542              
543             get_balance()
544              
545             =head2 get_bitcoin_receivers
546              
547             get_bitcoin_receivers()
548              
549             =head2 create_bitcoin_receiver
550              
551             create_bitcoin_receiver($data)
552              
553             Example:
554              
555             my $receiver = $stripe->create_bitcoin_receiver({
556             amount => 100,
557             currency => 'usd',
558             email => 'bob@tilt.com',
559             });
560              
561             =head2 get_bitcoin_receiver
562              
563             get_bitcoin_receiver($id)
564              
565             =head2 get_event
566              
567             get_event($id)
568              
569             Returns an event for the given id.
570              
571             =head2 get_events
572              
573             get_events(query => $query)
574              
575             Returns a list of events.
576             The query param is optional.
577              
578             =head1 AUTHORS
579              
580             =over 4
581              
582             =item *
583              
584             Naveed Massjouni <naveed@vt.edu>
585              
586             =item *
587              
588             Dan Schmidt <danschmidt5189@gmail.com>
589              
590             =item *
591              
592             Chris Behrens <chris@tilt.com>
593              
594             =back
595              
596             =head1 COPYRIGHT AND LICENSE
597              
598             This software is copyright (c) 2014 by Tilt, Inc.
599              
600             This is free software; you can redistribute it and/or modify it under
601             the same terms as the Perl 5 programming language system itself.
602              
603             =cut