File Coverage

blib/lib/WebService/Stripe.pm
Criterion Covered Total %
statement 47 396 11.8
branch 0 284 0.0
condition 0 143 0.0
subroutine 36 66 54.5
pod n/a
total 83 889 9.3


line stmt bran cond sub pod time code
1             package WebService::Stripe;
2 7     7   474505 use Moo;
  7         73397  
  7         37  
3             with 'WebService::Client';
4              
5             our $VERSION = '0.0900'; # VERSION
6              
7 7     7   9690 use Carp qw(croak);
  7         11  
  7         368  
8 7     7   4453 use Method::Signatures;
  7         385640  
  7         43  
9 7     7   2874 use constant { MARKETPLACES_MIN_VERSION => '2014-11-05' };
  7         12  
  7         1074  
10              
11             has api_key => (
12             is => 'ro',
13             required => 1,
14             );
15              
16             has version => (
17             is => 'ro',
18             default => MARKETPLACES_MIN_VERSION,
19             );
20              
21             has '+base_url' => ( default => 'https://api.stripe.com' );
22              
23             has '+content_type' => ( default => 'application/x-www-form-urlencoded' );
24              
25 7     7   817659 method BUILD(@args) {
  1     1   3123  
  1         5  
26 1         5 $self->ua->default_headers->authorization_basic( $self->api_key, '' );
27 1         3782 $self->ua->default_header( 'Stripe-Version' => $self->version );
28             }
29              
30 7 0 0 7   29832 method next(HashRef $thing, HashRef :$query) {
  0 0 0 0      
  0 0 0        
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
31 0   0       $query ||= {};
32 0 0         return undef unless $thing->{has_more};
33 0 0         my $starting_after = $thing->{data}[-1]{id} or return undef;
34 0           return $self->get( $thing->{url},
35             { %$query, starting_after => $starting_after } );
36             }
37              
38 7 0 0 7   33442 method create_customer(Maybe[HashRef] $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
39 0           return $self->post( "/v1/customers", $data, headers => $headers );
40             }
41              
42 7 0   7   16849 method get_balance(:$headers) {
  0 0   0      
  0            
  0            
  0            
  0            
  0            
43 0           return $self->get( "/v1/balance", {}, headers => $headers );
44             }
45              
46 7 0 0 7   22538 method get_customer(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
47 0           return $self->get( "/v1/customers/$id", {}, headers => $headers );
48             }
49              
50 7 0 0 7   30252 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            
51 0           return $self->post( "/v1/customers/$id", $data, headers => $headers );
52             }
53              
54 7 0 0 7   26749 method get_customers(HashRef :$query, :$headers) {
  0 0 0 0      
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
55 0           return $self->get( "/v1/customers", $query, headers => $headers );
56             }
57              
58 7 0 0 7   30508 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            
59 0           return $self->post(
60             "/v1/customers/$customer_id/cards", $data, headers => $headers );
61             }
62              
63 7 0 0 7   32404 method get_charge(HashRef|Str $charge, :$query, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
64 0 0         $charge = $charge->{id} if ref $charge;
65 0           return $self->get( "/v1/charges/$charge", $query, headers => $headers );
66             }
67              
68 7 0 0 7   24733 method create_charge(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
69 0           return $self->post( "/v1/charges", $data, headers => $headers );
70             }
71              
72 7 0 0 7   32798 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            
73 0 0         $charge = $charge->{id} if ref $charge;
74 0           return $self->post( "/v1/charges/$charge", $data, headers => $headers );
75             }
76              
77 7 0 0 7   29178 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            
78 0           return $self->post( "/v1/charges/$id/capture", $data, headers => $headers );
79             }
80              
81 7 0 0 7   28904 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            
82 0           return $self->post( "/v1/charges/$id/refunds", $data, headers => $headers );
83             }
84              
85 7 0 0 7   30749 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            
86 0 0         $cust = $cust->{id} if ref $cust;
87 0           return $self->post( "/v1/customers/$cust/sources", $data, headers => $headers );
88             }
89              
90 7 0 0 7   22439 method create_token(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
91 0           return $self->post( "/v1/tokens", $data, headers => $headers );
92             }
93              
94 7 0 0 7   22117 method get_token(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
95 0           return $self->get( "/v1/tokens/$id", {}, headers => $headers );
96             }
97              
98 7 0 0 7   21646 method create_account(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
99 0           return $self->post( "/v1/accounts", $data, headers => $headers );
100             }
101              
102 7 0 0 7   21345 method get_account(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
103 0           return $self->get( "/v1/accounts/$id", {}, headers => $headers );
104             }
105              
106 7 0 0 7   28598 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            
107 0           return $self->post( "/v1/accounts/$id", $data, headers => $headers );
108             }
109              
110 7 0 0 7   28780 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            
111 0           return $self->post(
112             "/v1/accounts/$account_id/bank_accounts", $data, headers => $headers );
113             }
114              
115 7 0 0 7   35408 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            
116 0           return $self->post( "/v1/accounts/$account_id/bank_accounts/$id", $data,
117             headers => $headers );
118             }
119              
120 7 0 0 7   30135 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            
121 0           return $self->delete(
122             "/v1/accounts/$account_id/bank_accounts/$id", headers => $headers );
123             }
124              
125 7 0 0 7   24077 method get_banks(Str :$account_id!, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
126 0           return $self->get(
127             "/v1/accounts/$account_id/bank_accounts", {}, headers => $headers );
128             }
129              
130 7 0 0 7   22431 method create_transfer(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
131 0           return $self->post( "/v1/transfers", $data, headers => $headers );
132             }
133              
134 7 0 0 7   22175 method get_transfer(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
135 0           return $self->get( "/v1/transfers/$id", {}, headers => $headers );
136             }
137              
138 7 0 0 7   23025 method get_transfers(HashRef :$query, :$headers) {
  0 0 0 0      
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
139 0           return $self->get( "/v1/transfers", $query, headers => $headers );
140             }
141              
142 7 0 0 7   28286 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            
143 0           return $self->post( "/v1/transfers/$id", $data, headers => $headers );
144             }
145              
146 7 0 0 7   22868 method cancel_transfer(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
147 0           return $self->post("/v1/transfers/$id/cancel", undef, headers => $headers);
148             }
149              
150 7 0 0 7   23152 method get_bitcoin_receivers(HashRef :$query, :$headers) {
  0 0 0 0      
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
151 0           return $self->get( "/v1/bitcoin/receivers", $query, headers => $headers );
152             }
153              
154 7 0 0 7   21724 method create_bitcoin_receiver(HashRef $data, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
155 0           return $self->post( "/v1/bitcoin/receivers", $data, headers => $headers );
156             }
157              
158 7 0 0 7   22176 method get_bitcoin_receiver(Str $id, :$headers) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
159 0           return $self->get( "/v1/bitcoin/receivers/$id", {}, headers => $headers );
160             }
161              
162             # ABSTRACT: Stripe API bindings
163              
164              
165             1;
166              
167             __END__
168              
169             =pod
170              
171             =encoding UTF-8
172              
173             =head1 NAME
174              
175             WebService::Stripe - Stripe API bindings
176              
177             =head1 VERSION
178              
179             version 0.0900
180              
181             =head1 SYNOPSIS
182              
183             my $stripe = WebService::Stripe->new(
184             api_key => 'secret',
185             version => '2014-11-05', # optional
186             );
187             my $customer = $stripe->get_customer('cus_57eDUiS93cycyH');
188              
189             =head1 TESTING
190              
191             Set the PERL_STRIPE_TEST_API_KEY environment variable to your Stripe test
192             secret, then run tests as you normally would using prove.
193              
194             =head1 HEADERS
195              
196             WebService::Stripe supports passing custom headers to any API request by passing a hash of header values as the optional C<headers> named parameter:
197              
198             $stripe->create_charge({ ... }, headers => { stripe_account => "acct_123" })
199              
200             Note that header names are normalized: C<foo_bar>, C<Foo-Bar>, and C<foo-bar> are equivalent.
201              
202             Three headers stand out in particular:
203              
204             =over
205              
206             =item Stripe-Version
207              
208             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.
209              
210             =item Stripe-Account
211              
212             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.
213              
214             =item Idempotency-Key
215              
216             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.
217              
218             This feature is in ALPHA and subject to change without notice. Contact Stripe to confirm the latest behavior and header name.
219              
220             =back
221              
222             =head1 METHODS
223              
224             =head2 get_customer
225              
226             get_customer($id)
227              
228             Returns the customer for the given id.
229              
230             =head2 create_customer
231              
232             create_customer($data)
233              
234             Creates a customer.
235             The C<$data> hashref is optional.
236             Returns the customer.
237              
238             Example:
239              
240             $customer = $stripe->create_customer({ email => 'bob@foo.com' });
241              
242             =head2 update_customer
243              
244             update_customer($id, $data)
245              
246             Updates a customer.
247             Returns the updated customer.
248              
249             Example:
250              
251             $customer = $stripe->update_customer($id, { description => 'foo' });
252              
253             =head2 get_customers
254              
255             get_customers(query => $query)
256              
257             Returns a list of customers.
258             The query param is optional.
259              
260             =head2 next
261              
262             next($collection)
263              
264             Returns the next page of results for the given collection.
265              
266             Example:
267              
268             my $customers = $stripe->get_customers;
269             ...
270             while ($customers = $stripe->next($customers)) {
271             ...
272             }
273              
274             =head2 create_card
275              
276             create_card($data, customer_id => 'cus_123')
277              
278             =head2 get_charge
279              
280             get_charge($id, query => { expand => ['customer'] })
281              
282             Returns the charge for the given id. The optional :$query parameter allows
283             passing query arguments. Passing an arrayref as a query param value will expand
284             it into Stripe's expected array format.
285              
286             =head2 create_charge
287              
288             create_charge($data)
289              
290             Creates a charge.
291              
292             =head2 capture_charge
293              
294             capture_charge($id, data => $data)
295              
296             Captures the charge with the given id.
297             The data param is optional.
298              
299             =head2 refund_charge
300              
301             refund_charge($id, data => $data)
302              
303             Refunds the charge with the given id.
304             The data param is optional.
305              
306             =head2 update_charge
307              
308             update_charge($id, $data)
309              
310             Updates an existing charge object.
311              
312             =head2 add_source
313              
314             add_source($cust_id, $card_data)
315              
316             Adds a new funding source (credit card) to an existing customer.
317              
318             =head2 get_token
319              
320             get_token($id)
321              
322             =head2 create_token
323              
324             create_token($data)
325              
326             =head2 get_account
327              
328             get_account($id)
329              
330             =head2 create_account
331              
332             create_account($data)
333              
334             =head2 update_account
335              
336             update_account($id, data => $data)
337              
338             =head2 add_bank
339              
340             add_bank($data, account_id => $account_id)
341              
342             Add a bank to an account.
343              
344             Example:
345              
346             my $account = $stripe->create_account({
347             managed => 'true',
348             country => 'CA',
349             });
350              
351             my $bank = $stripe->add_bank(
352             {
353             'bank_account[country]' => 'CA',
354             'bank_account[currency]' => 'cad',
355             'bank_account[routing_number]' => '00022-001',
356             'bank_account[account_number]' => '000123456789',
357             },
358             account_id => $account->{id},
359             );
360              
361             # or add a tokenised bank
362              
363             my $bank_token = $stripe->create_token({
364             'bank_account[country]' => 'CA',
365             'bank_account[currency]' => 'cad',
366             'bank_account[routing_number]' => '00022-001',
367             'bank_account[account_number]' => '000123456789',
368             });
369              
370             $stripe->add_bank(
371             { bank_account => $bank_token->{id} },
372             account_id => $account->{id},
373             );
374              
375             =head2 update_bank
376              
377             update_bank($id, account_id => $account_id, data => $data)
378              
379             =head2 create_transfer
380              
381             create_transfer($data)
382              
383             =head2 get_transfer
384              
385             get_transfer($id)
386              
387             =head2 get_transfers
388              
389             get_transfers(query => $query)
390              
391             =head2 update_transfer
392              
393             update_transfer($id, data => $data)
394              
395             =head2 cancel_transfer
396              
397             cancel_transfer($id)
398              
399             =head2 get_balance
400              
401             get_balance()
402              
403             =head2 get_bitcoin_receivers
404              
405             get_bitcoin_receivers()
406              
407             =head2 create_bitcoin_receiver
408              
409             create_bitcoin_receiver($data)
410              
411             Example:
412              
413             my $receiver = $stripe->create_bitcoin_receiver({
414             amount => 100,
415             currency => 'usd',
416             email => 'bob@tilt.com',
417             });
418              
419             =head2 get_bitcoin_receiver
420              
421             get_bitcoin_receiver($id)
422              
423             =head1 AUTHORS
424              
425             =over 4
426              
427             =item *
428              
429             Naveed Massjouni <naveed@vt.edu>
430              
431             =item *
432              
433             Dan Schmidt <danschmidt5189@gmail.com>
434              
435             =back
436              
437             =head1 COPYRIGHT AND LICENSE
438              
439             This software is copyright (c) 2014 by Tilt, Inc.
440              
441             This is free software; you can redistribute it and/or modify it under
442             the same terms as the Perl 5 programming language system itself.
443              
444             =cut