11package Net::Stripe ;
22use Moose;
3- use methods ;
3+ use MooseX::Method::Signatures ;
44use LWP::UserAgent;
55use HTTP::Request::Common qw/ GET POST DELETE/ ;
66use MIME::Base64 qw/ encode_base64/ ;
@@ -91,20 +91,17 @@ has 'ua' => (is => 'ro', isa => 'Object', lazy_build => 1);
9191=cut
9292
9393Charges: {
94- method post_charge {
95- my %args = @_ ;
96- my $charge = Net::Stripe::Charge-> new(%args );
94+ sub post_charge {
95+ my $self = shift ;
96+ my $charge = Net::Stripe::Charge-> new(@_ );
9797 return $self -> _post(' charges' , $charge );
9898 }
9999
100- method get_charge {
101- my $id = shift || die " A charge ID is required" ;
100+ method get_charge(Str $id ) {
102101 return $self -> _get(" charges/$id " );
103102 }
104103
105- method refund_charge {
106- my $id = shift || die " A charge ID is required" ;
107- my $amount = shift ;
104+ method refund_charge($id , $amount ?) {
108105 $id = $id -> id if ref ($id );
109106
110107 if ($amount ) {
@@ -116,17 +113,16 @@ Charges: {
116113 return $self -> _post(" charges/$id /refund" . $amount );
117114 }
118115
119- method get_charges {
120- my %args = @_ ;
121- $self -> _get_collections(' charges' , %args );
116+ sub get_charges {
117+ my $self = shift ;
118+ $self -> _get_collections(' charges' , @_ );
122119 }
123120
124121
125122}
126123
127124BalanceTransactions: {
128- method get_balance_transaction {
129- my $id = shift || die " A transaction ID is required" ;
125+ method get_balance_transaction(Str $id ) {
130126 return $self -> _get(" balance/history/$id " );
131127 }
132128}
@@ -145,7 +141,8 @@ BalanceTransactions: {
145141=cut
146142
147143Customers: {
148- method post_customer {
144+ sub post_customer {
145+ my $self = shift ;
149146 # Update from an existing object
150147 if (@_ == 1) {
151148 my $c = shift ;
@@ -157,30 +154,32 @@ Customers: {
157154 }
158155
159156 # adds a subscription, keeping any existing subscriptions unmodified
160- method post_customer_subscription {
161- my $customer_id = shift || die ' post_customer_subscription() requires a customer_id' ;
157+ sub post_customer_subscription {
158+ my $self = shift ;
159+ my $customer_id = shift ;
160+ defined ($customer_id ) || die ' post_customer_subscription() requires a customer_id' ;
162161 die ' post_customer_subscription() requires a param hash' unless @_ ;
163162 $self -> _post(" customers/$customer_id /subscriptions" , @_ );
164163 }
165164
166- method list_subscriptions {
165+ sub list_subscriptions {
166+ my $self = shift ;
167167 my %args = @_ ;
168168 my $cid = delete $args {customer_id };
169169 return $self -> _get(" customers/$cid /subscriptions" , @_ );
170170 }
171171
172- method get_customer {
173- my $id = shift || die ' get_customer() requires a customer id' ;
172+ method get_customer(Str $id ) {
174173 return $self -> _get(" customers/$id " );
175174 }
176175
177- method delete_customer {
178- my $id = shift || die ' delete_customer() requires a customer id' ;
176+ method delete_customer($id ) {
179177 $id = $id -> id if ref ($id );
180178 $self -> _delete(" customers/$id " );
181179 }
182180
183- method get_customers {
181+ sub get_customers {
182+ my $self = shift ;
184183 $self -> _get_collections(' customers' , @_ );
185184 }
186185}
@@ -242,34 +241,38 @@ Cards: {
242241=cut
243242
244243Subscriptions: {
245- method get_subscription {
244+ sub get_subscription {
245+ my $self = shift ;
246246 my %args = @_ ;
247247 my $cid = delete $args {customer_id };
248248 return $self -> _get(" customers/$cid /subscription" );
249249 }
250250
251251 # adds a subscription, keeping any existing subscriptions unmodified
252- method post_subscription {
252+ sub post_subscription {
253+ my $self = shift ;
253254 my %args = @_ ;
254255 my $cid = delete $args {customer_id };
255256 my $subs = Net::Stripe::Subscription-> new(%args );
256257 return $self -> _post(" customers/$cid /subscriptions" , $subs );
257258 }
258259
259- method update_subscription {
260- my %args = @_ ;
261- my $cid = delete $args {customer_id };
262- my $sid = delete $args {subscription_id };
263- return $self -> _post(" customers/$cid /subscriptions/$sid " , \%args );
260+ sub update_subscription {
261+ my $self = shift ;
262+ my %args = @_ ;
263+ my $cid = delete $args {customer_id };
264+ my $sid = delete $args {subscription_id };
265+ return $self -> _post(" customers/$cid /subscriptions/$sid " , \%args );
264266 }
265267
266- method delete_subscription {
267- my %args = @_ ;
268- my $cid = delete $args {customer_id };
269- my $sid = delete $args {subscription_id };
270- my $query = ' ' ;
271- $query .= ' ?at_period_end=true' if $args {at_period_end };
272- return $self -> _delete(" customers/$cid /subscriptions/$sid$query " );
268+ sub delete_subscription {
269+ my $self = shift ;
270+ my %args = @_ ;
271+ my $cid = delete $args {customer_id };
272+ my $sid = delete $args {subscription_id };
273+ my $query = ' ' ;
274+ $query .= ' ?at_period_end=true' if $args {at_period_end };
275+ return $self -> _delete(" customers/$cid /subscriptions/$sid$query " );
273276 }
274277}
275278
@@ -281,13 +284,13 @@ Subscriptions: {
281284=cut
282285
283286Tokens: {
284- method post_token {
287+ sub post_token {
288+ my $self = shift ;
285289 my $token = Net::Stripe::Token-> new(@_ );
286290 return $self -> _post(' tokens' , $token );
287291 }
288292
289- method get_token {
290- my $id = shift || die ' get_token() requires a token id' ;
293+ method get_token(Str $id ) {
291294 return $self -> _get(" tokens/$id " );
292295 }
293296}
@@ -303,23 +306,23 @@ Tokens: {
303306=cut
304307
305308Plans: {
306- method post_plan {
309+ sub post_plan {
310+ my $self = shift ;
307311 my $plan = Net::Stripe::Plan-> new(@_ );
308312 return $self -> _post(' plans' , $plan );
309313 }
310314
311- method get_plan {
312- my $id = shift || die ' get_plan() requires a plan id' ;
315+ method get_plan(Str $id ) {
313316 return $self -> _get(" plans/" . uri_escape($id ));
314317 }
315318
316- method delete_plan {
317- my $id = shift || die ' delete_plan() requires a plan id' ;
319+ method delete_plan($id ) {
318320 $id = $id -> id if ref ($id );
319321 $self -> _delete(" plans/$id " );
320322 }
321323
322- method get_plans {
324+ sub get_plans {
325+ my $self = shift ;
323326 $self -> _get_collections(' plans' , @_ );
324327 }
325328}
@@ -336,23 +339,23 @@ Plans: {
336339=cut
337340
338341Coupons: {
339- method post_coupon {
342+ sub post_coupon {
343+ my $self = shift ;
340344 my $coupon = Net::Stripe::Coupon-> new(@_ );
341345 return $self -> _post(' coupons' , $coupon );
342346 }
343347
344- method get_coupon {
345- my $id = shift || die ' get_coupon() requires a coupon id' ;
348+ method get_coupon(Str $id ) {
346349 return $self -> _get(" coupons/" . uri_escape($id ));
347350 }
348351
349- method delete_coupon {
350- my $id = shift || die ' delete_coupon() requires a coupon id' ;
352+ method delete_coupon($id ) {
351353 $id = $id -> id if ref ($id );
352354 $self -> _delete(" coupons/$id " );
353355 }
354356
355- method get_coupons {
357+ sub get_coupons {
358+ my $self = shift ;
356359 $self -> _get_collections(' coupons' , @_ );
357360 }
358361}
@@ -369,22 +372,20 @@ Coupons: {
369372=cut
370373
371374Invoices: {
372- method post_invoice {
373- my $i = shift ;
375+ method post_invoice($i ) {
374376 return $self -> _post(" invoices/" . $i -> id, $i );
375377 }
376378
377- method get_invoice {
378- my $id = shift || die ' get_invoice() requires an invoice id' ;
379+ method get_invoice(Str $id ) {
379380 return $self -> _get(" invoices/$id " );
380381 }
381382
382- method get_invoices {
383+ sub get_invoices {
384+ my $self = shift ;
383385 $self -> _get_collections(' invoices' , @_ );
384386 }
385387
386- method get_upcominginvoice {
387- my $id = shift || die ' get_upcominginvoice() requires a customer id' ;
388+ method get_upcominginvoice(Str $id ) {
388389 return $self -> _get(" invoices/upcoming?customer=$id " );
389390 }
390391}
@@ -400,7 +401,8 @@ Invoices: {
400401=cut
401402
402403InvoiceItems: {
403- method post_invoiceitem {
404+ sub post_invoiceitem {
405+ my $self = shift ;
404406 # Update from an existing object
405407 if (@_ == 1) {
406408 my $i = shift ;
@@ -412,40 +414,37 @@ InvoiceItems: {
412414 return $self -> _post(' invoiceitems' , $invoiceitem );
413415 }
414416
415- method get_invoiceitem {
416- my $id = shift || die ' get_invoiceitem() requires a invoiceitem id' ;
417+ method get_invoiceitem(Str $id ) {
417418 return $self -> _get(" invoiceitems/$id " );
418419 }
419420
420- method delete_invoiceitem {
421- my $id = shift || die ' delete_invoiceitem() requires a invoiceitem id' ;
421+ method delete_invoiceitem($id ) {
422422 $id = $id -> id if ref ($id );
423423 $self -> _delete(" invoiceitems/$id " );
424424 }
425425
426- method get_invoiceitems {
426+ sub get_invoiceitems {
427+ my $self = shift ;
427428 $self -> _get_collections(' invoiceitems' , @_ );
428429 }
429430}
430431
431432# Helper methods
432433
433- method _get {
434- my $path = shift ;
434+ method _get(Str $path ) {
435435 my $req = GET $self -> api_base . ' /' . $path ;
436436 return $self -> _make_request($req );
437437}
438438
439- method _get_with_args {
440- my $path = shift ;
441- my $args = shift ;
439+ method _get_with_args(Str $path , $args ?) {
442440 if (@$args ) {
443441 $path .= " ?" . join (' &' , @$args );
444442 }
445443 return $self -> _get($path );
446444}
447445
448- method _get_collections {
446+ sub _get_collections {
447+ my $self = shift ;
449448 my $path = shift ;
450449 my %args = @_ ;
451450 my @path_args ;
@@ -471,23 +470,18 @@ method _get_collections {
471470 return $self -> _get_with_args($path , \@path_args );
472471}
473472
474- method _delete {
475- my $path = shift ;
473+ method _delete(Str $path ) {
476474 my $req = DELETE $self -> api_base . ' /' . $path ;
477475 return $self -> _make_request($req );
478476}
479477
480- method _post {
481- my $path = shift ;
482- my $obj = shift ;
483-
478+ method _post(Str $path , $obj ?) {
484479 my $req = POST $self -> api_base . ' /' . $path ,
485480 ($obj ? (Content => [ref ($obj ) eq ' HASH' ? %$obj : $obj -> form_fields]) : ());
486481 return $self -> _make_request($req );
487482}
488483
489- method _make_request {
490- my $req = shift ;
484+ method _make_request($req ) {
491485 $req -> header( Authorization =>
492486 " Basic " . encode_base64($self -> api_key . ' :' ));
493487
0 commit comments