-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathStripe.pm
3791 lines (2733 loc) · 111 KB
/
Stripe.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Net::Stripe;
use Moose;
use Class::Load;
use Type::Tiny 1.008004;
use Kavorka;
use LWP::UserAgent;
use HTTP::Request::Common qw/GET POST DELETE/;
use MIME::Base64 qw/encode_base64/;
use URI::Escape qw/uri_escape/;
use JSON qw/decode_json/;
use URI qw//;
use DateTime qw//;
use Net::Stripe::TypeConstraints;
use Net::Stripe::Constants;
use Net::Stripe::Token;
use Net::Stripe::Invoiceitem;
use Net::Stripe::Invoice;
use Net::Stripe::Card;
use Net::Stripe::Source;
use Net::Stripe::Plan;
use Net::Stripe::Product;
use Net::Stripe::Coupon;
use Net::Stripe::Charge;
use Net::Stripe::Customer;
use Net::Stripe::Discount;
use Net::Stripe::Subscription;
use Net::Stripe::Error;
use Net::Stripe::BalanceTransaction;
use Net::Stripe::List;
use Net::Stripe::LineItem;
use Net::Stripe::Refund;
use Net::Stripe::PaymentMethod;
use Net::Stripe::PaymentIntent;
# ABSTRACT: API client for Stripe.com
=head1 SYNOPSIS
my $stripe = Net::Stripe->new(api_key => $API_KEY);
my $card_token = 'a token';
my $charge = $stripe->post_charge( # Net::Stripe::Charge
amount => 12500,
currency => 'usd',
source => $card_token,
description => 'YAPC Registration',
);
print "Charge was not paid!\n" unless $charge->paid;
my $card = $charge->card; # Net::Stripe::Card
# look up a charge by id
my $same_charge = $stripe->get_charge(charge_id => $charge->id);
# ... and the API mirrors https://stripe.com/docs/api
# Charges: post_charge() get_charge() refund_charge() get_charges()
# Customer: post_customer()
=head1 DESCRIPTION
This module is a wrapper around the Stripe.com HTTP API. Methods are
generally named after the HTTP method and the object name.
This method returns Moose objects for responses from the API.
=head2 VERSIONING
Because of occasional non-backward-compatible changes in the Stripe API, a
given version of the SDK is only guaranteed to support Stripe API versions
within the range defined by C<Net::Stripe::Constants::MIN_API_VERSION> and
C<Net::Stripe::Constants::MAX_API_VERSION>.
If you need a version of the SDK supporting a specific older Stripe API
version, you can check for available versions at
L<https://github.com/lukec/stripe-perl/branches>, or by cloning this
repository, located at <https://github.com/lukec/stripe-perl> and using
<git branch> to view available version-specific branches.
=head3 DEFAULT VERSIONING
If you do not set the Stripe API version on object instantiation, API
calls will default to the API version setting for your Stripe account.
=head3 SPECIFIC VERSIONING
If you set the Stripe API version on object instantiation you are telling
Stripe to use that version of the API instead of the default for your account,
and therefore the available API request and response parameters, the names of
those parameters and the structure of the format of the returned data will all
be dictated by the version that you specify. You can read more about the
details of specific API versions at
<https://stripe.com/docs/upgrades#api-changelog>.
=head3 OUT OF SCOPE VERSIONING
If you are wearing a cowboy hat and think - although your specified account
version is outside the range defined in C<Net::Stripe::Constants> - that your
use case is simple enough that it should "just work", you can create your
object instance with C<force_api_version =E<gt> 1>, but don't say we didn't
warn you!
=head1 RELEASE NOTES
=head2 Version 0.40
=head3 BREAKING CHANGES
=over
=item deprecate direct handling of PANs
Stripe strongly discourages direct handling of PANs (primary account numbers),
even in test mode, and returns invalid_request_error when passing PANs to the
API from accounts that were created after October 2017. In live mode, all
tokenization should be performed via client-side libraries because direct
handling of PANs violates PCI compliance. So we have removed the methods and
parameter constraints that allow direct handling of PANs and updated the
unit tests appropriately.
=item updating customer card by passing Customer object to post_customer()
If you have code that updates a customer card by updating the internal values
for an existing customer object and then posting that object:
my $customer_obj = $stripe->get_customer(
customer_id => $customer_id,
);
$customer_obj->card( $new_card );
$stripe->post_customer( customer => $customer_obj );
you must unset the default_card attribute in the existing object before
posting the customer object.
$customer_obj->default_card( undef );
Otherwise there is a conflict, since the old default_card attribute in the
object is serialized in the POST stream, and it appears that you are requesting
to set default_card to the id of a card that no longer exists, but rather
is being replaced by the new value of the card attribute in the object.
=item Plan objects now linked to Product objects
For Stripe API versions after 2018-02-15 L<https://stripe.com/docs/upgrades#2018-02-05>
each Plan object is linked to a Product object with type=service. The
Plan object 'name' and 'statement_descriptor' attributes have been moved to
Product objects.
=back
=head3 DEPRECATION WARNING
=over
=item update 'card' to 'source' for Charge and Customer
While the API returns both card-related and source-related values for earlier
versions, making the update mostly backwards-compatible, Stripe API versions
after 2015-02-18 L<https://stripe.com/docs/upgrades#2015-02-18> will no longer
return the card-related values, so you should update your code where necessary
to use the 'source' argument and method for Charge objects, and the 'source',
'sources' and 'default_source' arguments and methods for Customer, in
preparation for the eventual deprecation of the card-related arguments.
=item update 'account_balance' to 'balance' for Customer
While the API returns both 'account_balance' and 'balance' for earlier
versions, making the update backwards-compatible, Stripe API versions
after 2019-10-17 L<https://stripe.com/docs/upgrades#2019-10-17> do not
accept or return 'account_balance', so you should update your code where
necessary to use the 'balance' argument and method for Customer objects in
preparation for the eventual deprecation of the 'account_balance' argument.
=item use 'cancel_at_period_end' instead of 'at_period_end' for canceling Subscriptions
For Stripe API versions after 2018-08-23
L<https://stripe.com/docs/upgrades#2018-08-23>, you can no longer use
'at_period_end' in delete_subscription(). The delete_subscription() method
is reserved for immediate canceling going forward. You should update your
code to use 'cancel_at_period_end in update_subscription() instead.
=item update 'date' to 'created' for Invoice
While the API returns both 'date' and 'created' for earlier versions, making
the update backwards-compatible, Stripe API versions after 2019-03-14
L<https://stripe.com/docs/upgrades#2019-03-14> only return 'created', so you
should update your code where necessary to use the 'created' method for
Invoice objects in preparation for the eventual deprecation of the 'date'
argument.
=item use 'auto_advance' instead of 'closed' for Invoice
The 'closed' attribute for the Invoice object controls automatic collection,
and has been deprecated in favor of the more specific 'auto_advance' attribute.
Where you might have set closed=true on Invoices in the past, set
auto_advance=false. While the API returns both 'closed' and 'auto_advance'
for earlier versions, making the update backwards-compatible, Stripe API
versions after 2018-11-08 L<https://stripe.com/docs/upgrades#2018-11-08>
only return 'auto_advance', so you should update your code where necessary to
use the 'auto_advance' argument and method for Invoice objects in preparation
for the eventual deprecation of the 'closed' argument.
=back
=head3 BUG FIXES
=over
=item fix post_charge() arguments
Some argument types for `customer` and `card` were non-functional in the
current code and have been removed from the Kavorka method signature. We
removed `Net::Stripe::Customer` and `HashRef` for `customer` and we removed
`Net::Stripe::Card` and `Net::Stripe::Token` for `card`, as none of these
forms were being serialized correctly for passing to the API call. We must
retain `Net::Stripe::Card` for the `card` attribute in `Net::Stripe::Charge`
because the `card` value returned from the API call is objectified into
a card object. We have also added TypeConstraints for the string arguments
passed and added in-method validation to ensure that the passed argument
values make sense in the context of one another.
=item cleanup post_card()
Some argument types for `card` are not legitimate, or are being deprecated
and have been removed from the Kavorka method signature. We removed
`Net::Stripe::Card` and updated the string validation to disallow card id
for `card`, as neither of these are legitimate when creating or updating a
card L<https://github.com/lukec/stripe-perl/issues/138>, and the code path
that appeared to be handling `Net::Stripe::Card` was actually unreachable
L<https://github.com/lukec/stripe-perl/issues/100>. We removed the dead code
paths and made the conditional structure more explicit, per discussion in
L<https://github.com/lukec/stripe-perl/pull/133>. We also added unit tests
for all calling forms, per L<https://github.com/lukec/stripe-perl/issues/139>.
=item fix post_customer() arguments
Some argument types for `card` are not legitimate and have been removed from
the Kavorka method signature. We removed `Net::Stripe::Card` and updated the
string validation to disallow card id for `card`, as neither of these are
legitimate when creating or updating a customer L<https://github.com/lukec/stripe-perl/issues/138>.
We have also updated the structure of the method so that we always create a
Net::Stripe::Customer object before posting L<https://github.com/lukec/stripe-perl/issues/148>
and cleaned up and centralized Net::Stripe:Token coercion code.
=item default_card not updating in post_customer()
Prior to ff84dd7, we were passing %args directly to _post(), and therefore
default_card would have been included in the POST stream. Now we are creating
a L<Net::Stripe::Customer> object and posting it, so the posted parameters
rely on the explicit list in $customer_obj->form_fields(), which was lacking
default_card. See also BREAKING CHANGES.
=back
=head3 ENHANCEMENTS
=over
=item coerce old lists
In older Stripe API versions, some list-type data structures were returned
as arrayrefs. We now coerce those old-style lists and collections into the
hashref format that newer versions of the API return, with metadata stored
top-level keys and the list elements in an arrayref with the key 'data',
which is the format that C<Net::Stripe::List> expects. This makes the SDK
compatible with the Stripe API back to the earliest documented API version
L<https://stripe.com/docs/upgrades#2011-06-21>.
=item encode card metdata in convert_to_form_fields()
When passing a hashref with a nested metadata hashref to _post(), that
metadata must be encoded properly before being passed to the Stripe API.
There is now a dedicated block in convert_to_form_fields for this operation.
This update was necessary because of the addition of update_card(), which
accepts a card hashref, which may include metadata.
=item encode objects in convert_to_form_fields()
We removed the nested tertiary operator in _post() and updated
convert_to_form_fields() so that it now handles encoding of objects, both
top-level and nested. This streamlines the hashref vs object serailizing
code, making it easy to adapt to other methods.
=item remove manual serialization in _get_collections() and _get_with_args()
We were using string contatenation to both serilize the individual query args,
in _get_collections(), and to join the individual query args together, in
_get_with_args(). This also involved some unnecessary duplication of the
logic that convert_to_form_fields() was already capable of handling. We now
use convert_to_form_fields() to process the passed data, and L<URI> to
encode and serialize the query string. Along with other updates to
convert_to_form_fields(), _get() can now easily handle the same calling
form as _post(), eliminating the need for _get_collections() and
_get_with_args(). We have also updated _delete() accordingly.
=item add _get_all()
Similar to methods provided by other SDKs, calls using this method will allow
access to all records for a given object type without having to manually
paginate through the results. It is not intended to be used directly, but
will be accessed through new and existing list-retrieval methods. In order to
maintain backwards-compatibility with existing list retrieval behavior, this
method supports passing a value of 0 for 'limit' in order to retrieve all
records. Any other positive integer value for 'limit' will attempt to retrieve
that number of records up to the maximum available. As before, not passing a
value for 'limit', or explicitly passing an undefined value, retrieves whatever
number of records the API returns by default.
=back
=head3 UPDATES
=over
=item update statement_description to statement_descriptor
The statement_description attribute is now statement_descriptor for
L<Net::Stripe::Charge> and L<Net::Stripe::Plan>. The API docs
L<https://stripe.com/docs/upgrades#2014-12-17> indicate that this change
is backwards-compatible. You must update your code to reflect this change
for parameters passed to these objects and methods called on these objects.
=item update unit tests for Charge->status
For Stripe API versions after 2015-02-18 L<https://stripe.com/docs/upgrades#2015-02-18>,
the status property on the Charge object has a value of 'succeeded' for
successful charges. Previously, the status property would be 'paid' for
successful charges. This change does not affect the API calls themselves, but
if your account is using Stripe API version 2015-02-18 or later, you should
update any code that relies on strict checking of the return value of
Charge->status.
=item add update_card()
This method allows updates to card address, expiration, metadata, etc for
existing customer cards.
=item update Token attributes
Added type and client_ip attributes for L<Net::Stripe::Token>.
=item allow capture of partial charge
Passing 'amount' to capture_charge() allows capture of a partial charge.
Per the API, any remaining amount is immediately refunded. The charge object
now also has a 'refunds' attribute, representing a L<Net::Stripe::List>
of L<Net::Stripe::Refund> objects for the charge.
=item add Source
Added a Source object. Also added 'source' attribute and argument for Charge
objects and methods, and added 'source', 'sources' and 'default_source'
attributes and arguments for Customer objects and methods.
=item add balance for Customer
Added 'balance' attribute and arguments for Customer objects and methods.
=item add cancel_at_period_end for update_subscription()
Added 'cancel_at_period_end' argument update_subscription() and added
'cancel_at_period_end' to the POST stream for Subscription objects.
=item update Invoice
Added 'created' attribute for Invoice objects, and removed the required
constraint for the deprecated 'date' attribute. Also added the 'auto_advance'
attribute and removed the required constraint for the deprecated 'closed'
attribute.
=item add Product
Added a Product object. Also added 'product' attribute and argument for Plan
objects and methods.
=item add PaymentMethod and PaymentIntent
Added PaymentMethod and PaymentIntent objects and methods.
=back
=method new PARAMHASH
This creates a new stripe API object. The following parameters are accepted:
=over
=item api_key
This is required. You get this from your Stripe Account settings.
=item api_version
This is the value of the Stripe-Version header <https://stripe.com/docs/api/versioning>
you wish to transmit for API calls.
=item force_api_version
Set this to true to bypass the safety checks for API version compatibility with
a given version of the SDK. Please see the warnings above, and remember that
you are handling the financial data of your users. Use with extreme caution!
=item debug
You can set this to true to see extra debug info.
=item debug_network
You can set this to true to see the actual network requests.
=back
=cut
has 'debug' => (is => 'rw', isa => 'Bool', default => 0, documentation => "The debug flag");
has 'debug_network' => (is => 'rw', isa => 'Bool', default => 0, documentation => "The debug network request flag");
has 'api_key' => (is => 'ro', isa => 'Str', required => 1, documentation => "You get this from your Stripe Account settings");
has 'api_base' => (is => 'ro', isa => 'Str', lazy_build => 1, documentation => "This is the base part of the URL for every request made");
has 'ua' => (is => 'ro', isa => 'Object', lazy_build => 1, documentation => "The LWP::UserAgent that is used for requests");
has 'api_version' => (is => 'ro', isa => 'StripeAPIVersion', documentation => "This is the value of the Stripe-Version header you wish to transmit for API calls");
has 'force_api_version' => (is => 'ro', isa => 'Bool', default => 0, documentation => "Set this to true to bypass the safety checks for API version compatibility.");
sub BUILD {
my ( $self, $args ) = @_;
$self->_validate_api_version_range();
$self->_validate_api_version_value();
}
=charge_method post_charge
Create a new charge.
L<https://stripe.com/docs/api/charges/create#create_charge>
=over
=item * amount - Int - amount to charge
=item * currency - Str - currency for charge
=item * customer - StripeCustomerId - customer to charge - optional
=item * card - StripeTokenId or StripeCardId - card to use - optional
=item * source - StripeTokenId or StripeCardId - source to use - optional
=item * description - Str - description for the charge - optional
=item * metadata - HashRef - metadata for the charge - optional
=item * capture - Bool - optional
=item * statement_descriptor - Str - descriptor for statement - optional
=item * application_fee - Int - optional
=item * receipt_email - Str - The email address to send this charge's receipt to - optional
=back
Returns L<Net::Stripe::Charge>.
$stripe->post_charge(currency => 'USD', amount => 500, customer => 'testcustomer');
=charge_method get_charge
Retrieve a charge.
L<https://stripe.com/docs/api#retrieve_charge>
=over
=item * charge_id - Str - charge id to retrieve
=back
Returns L<Net::Stripe::Charge>.
$stripe->get_charge(charge_id => 'chargeid');
=charge_method refund_charge
Refunds a charge.
L<https://stripe.com/docs/api#create_refund>
=over
=item * charge - L<Net::Stripe::Charge> or Str - charge or charge_id to refund
=item * amount - Int - amount to refund in cents, optional
=back
Returns a new L<Net::Stripe::Refund>.
$stripe->refund_charge(charge => $charge, amount => 500);
=charge_method get_charges
Returns a L<Net::Stripe::List> object containing L<Net::Stripe::Charge> objects.
L<https://stripe.com/docs/api#list_charges>
=over
=item * created - HashRef - created conditions to match, optional
=item * customer - L<Net::Stripe::Customer> or Str - customer to match
=item * ending_before - Str - ending before condition, optional
=item * limit - Int - maximum number of charges to return, optional
=item * starting_after - Str - starting after condition, optional
=back
Returns a list of L<Net::Stripe::Charge> objects.
$stripe->get_charges(customer => $customer, limit => 5);
=charge_method capture_charge
L<https://stripe.com/docs/api/charges/capture#capture_charge>
=over
=item * charge - L<Net::Stripe::Charge> or Str - charge to capture
=item * amount - Int - amount to capture
=back
Returns a L<Net::Stripe::Charge>.
$stripe->capture_charge(charge => $charge_id);
=cut
Charges: {
method post_charge(Int :$amount,
Str :$currency,
StripeCustomerId :$customer?,
StripeTokenId|StripeCardId :$card?,
StripeTokenId|StripeCardId|StripeSourceId :$source?,
Str :$description?,
HashRef :$metadata?,
Bool :$capture?,
Str :$statement_descriptor?,
Int :$application_fee?,
Str :$receipt_email?
) {
if ( defined( $card ) ) {
my $card_id_type = Moose::Util::TypeConstraints::find_type_constraint( 'StripeCardId' );
if ( defined( $customer ) && ! $card_id_type->check( $card ) ) {
die Net::Stripe::Error->new(
type => "post_charge error",
message => sprintf(
"Invalid value '%s' passed for parameter 'card'. Charges for an existing customer can only accept a card id.",
$card,
),
);
}
my $token_id_type = Moose::Util::TypeConstraints::find_type_constraint( 'StripeTokenId' );
if ( ! defined( $customer ) && ! $token_id_type->check( $card ) ) {
die Net::Stripe::Error->new(
type => "post_charge error",
message => sprintf(
"Invalid value '%s' passed for parameter 'card'. Charges without an existing customer can only accept a token id.",
$card,
),
);
}
}
if ( defined( $source ) ) {
my $card_id_type = Moose::Util::TypeConstraints::find_type_constraint( 'StripeCardId' );
if ( defined( $customer ) && ! $card_id_type->check( $source ) ) {
die Net::Stripe::Error->new(
type => "post_charge error",
message => sprintf(
"Invalid value '%s' passed for parameter 'source'. Charges for an existing customer can only accept a card id.",
$source,
),
);
}
my $token_id_type = Moose::Util::TypeConstraints::find_type_constraint( 'StripeTokenId' );
my $source_id_type = Moose::Util::TypeConstraints::find_type_constraint( 'StripeSourceId' );
if ( ! defined( $customer ) && ! $token_id_type->check( $source ) && ! $source_id_type->check( $source ) ) {
die Net::Stripe::Error->new(
type => "post_charge error",
message => sprintf(
"Invalid value '%s' passed for parameter 'source'. Charges without an existing customer can only accept a token id or source id.",
$source,
),
);
}
}
my $charge = Net::Stripe::Charge->new(amount => $amount,
currency => $currency,
customer => $customer,
card => $card,
source => $source,
description => $description,
metadata => $metadata,
capture => $capture,
statement_descriptor => $statement_descriptor,
application_fee => $application_fee,
receipt_email => $receipt_email
);
return $self->_post('charges', $charge);
}
method get_charge(Str :$charge_id) {
return $self->_get("charges/" . $charge_id);
}
method refund_charge(Net::Stripe::Charge|Str :$charge, Int :$amount?) {
if (ref($charge)) {
$charge = $charge->id;
}
my $refund = Net::Stripe::Refund->new(id => $charge,
amount => $amount
);
return $self->_post("charges/$charge/refunds", $refund);
}
method get_charges(HashRef :$created?,
Net::Stripe::Customer|Str :$customer?,
Str :$ending_before?,
Int :$limit?,
Str :$starting_after?) {
if (ref($customer)) {
$customer = $customer->id;
}
my %args = (
path => 'charges',
created => $created,
customer => $customer,
ending_before => $ending_before,
limit => $limit,
starting_after => $starting_after,
);
$self->_get_all(%args);
}
method capture_charge(
Net::Stripe::Charge|Str :$charge!,
Int :$amount?,
) {
if (ref($charge)) {
$charge = $charge->id;
}
my %args = (
amount => $amount,
);
return $self->_post("charges/$charge/capture", \%args);
}
}
=payment_intent_method create_payment_intent
Create a PaymentIntent object
L<https://stripe.com/docs/api/payment_intents/create#create_payment_intent>
=over
=item * amount - Int - amount intended to be collected by this PaymentIntent - required
=item * currency - Str - currency - required
=item * application_fee_amount - Int - the amount of the application fee
=item * capture_method - StripeCaptureMethod - capture method
=item * confirm - Bool - attempt to confirm this PaymentIntent immediately
=item * confirmation_method - StripeConfirmationMethod - confirmation method
=item * customer - StripeCustomerId - id of Customer this PaymentIntent belongs to
=item * description - Str - description
=item * error_on_requires_action - Bool - fail the payment attempt if the PaymentIntent transitions into `requires_action`
=item * mandate - Str - id of the mandate to be used for this payment
=item * mandate_data - HashRef - details about the Mandate to create
=item * metadata - HashRef[Str] - metadata
=item * off_session - Bool - indicate that the customer is not in your checkout flow
=item * on_behalf_of - Str - Stripe account ID for which these funds are intended
=item * payment_method - StripePaymentMethodId - id of PaymentMethod to attach to this PaymentIntent
=item * payment_method_options - HashRef - PaymentMethod-specific configuration for this PaymentIntent
=item * payment_method_types - ArrayRef[StripePaymentMethodType] - list of PaymentMethod types that this PaymentIntent is allowed to use
=item * receipt_email - Str - email address to send the receipt to
=item * return_url - Str - URL to redirect your customer back to
=item * save_payment_method - Bool - save the payment method to the customer
=item * setup_future_usage - Str - allow future payments with this PaymentIntent's PaymentMethod
=item * shipping - HashRef - shipping information for this PaymentIntent
=item * statement_descriptor - Str - descriptor for statement
=item * statement_descriptor_suffix - Str - suffix to be concatenated with the statement descriptor
=item * transfer_data - HashRef - parameters used to automatically create a Transfer when the payment succeeds
=item * transfer_group - Str - identifies the resulting payment as part of a group
=item * use_stripe_sdk - Bool - use manual confirmation and the iOS or Android SDKs to handle additional authentication steps
=back
Returns a L<Net::Stripe::PaymentIntent>
$stripe->create_payment_intent(
amount => 3300,
currency => 'usd',
);
=payment_intent_method get_payment_intent
Retrieve an existing PaymentIntent
L<https://stripe.com/docs/api/payment_intents/retrieve#retrieve_payment_intent>
=over
=item * payment_intent_id - StripePaymentIntentId - id of PaymentIntent to retrieve - required
=item * client_secret - Str - client secret of the PaymentIntent to retrieve
=back
Returns a L<Net::Stripe::PaymentIntent>
$stripe->get_payment_intent(
payment_intent_id => $payment_intent_id,
);
=payment_intent_method update_payment_intent
Update an existing PaymentIntent
L<https://stripe.com/docs/api/payment_intents/update#update_payment_intent>
=over
=item * payment_intent_id - StripePaymentIntentId - id of PaymentIntent to update - required
=item * amount - Int - amount intended to be collected by this PaymentIntent - required
=item * application_fee_amount - Int - the amount of the application fee
=item * currency - Str - currency - required
=item * customer - StripeCustomerId - id of Customer this PaymentIntent belongs to
=item * description - Str - description
=item * metadata - HashRef[Str] - metadata
=item * payment_method - StripePaymentMethodId - id of PaymentMethod to attach to this PaymentIntent
=item * payment_method_options - HashRef - PaymentMethod-specific configuration for this PaymentIntent
=item * payment_method_types - ArrayRef[StripePaymentMethodType] - list of PaymentMethod types that this PaymentIntent is allowed to use
=item * receipt_email - Str - email address to send the receipt to
=item * save_payment_method - Bool - save the payment method to the customer
=item * setup_future_usage - Str - allow future payments with this PaymentIntent's PaymentMethod
=item * shipping - HashRef - shipping information for this PaymentIntent
=item * statement_descriptor - Str - descriptor for statement
=item * statement_descriptor_suffix - Str - suffix to be concatenated with the statement descriptor
=item * transfer_data - HashRef - parameters used to automatically create a Transfer when the payment succeeds
=item * transfer_group - Str - identifies the resulting payment as part of a group
=back
Returns a L<Net::Stripe::PaymentIntent>
$stripe->update_payment_intent(
payment_intent_id => $payment_intent_id,
description => 'Updated Description',
);
=payment_intent_method confirm_payment_intent
Confirm that customer intends to pay with provided PaymentMethod
L<https://stripe.com/docs/api/payment_intents/confirm#confirm_payment_intent>
=over
=item * payment_intent_id - StripePaymentIntentId - id of PaymentIntent to confirm - required
=item * client_secret - Str - client secret of the PaymentIntent
=item * error_on_requires_action - Bool - fail the payment attempt if the PaymentIntent transitions into `requires_action`
=item * mandate - Str - id of the mandate to be used for this payment
=item * mandate_data - HashRef - details about the Mandate to create
=item * off_session - Bool - indicate that the customer is not in your checkout flow
=item * payment_method - StripePaymentMethodId - id of PaymentMethod to attach to this PaymentIntent
=item * payment_method_options - HashRef - PaymentMethod-specific configuration for this PaymentIntent
=item * payment_method_types - ArrayRef[StripePaymentMethodType] - list of PaymentMethod types that this PaymentIntent is allowed to use
=item * receipt_email - Str - email address to send the receipt to
=item * return_url - Str - URL to redirect your customer back to
=item * save_payment_method - Bool - save the payment method to the customer
=item * setup_future_usage - Str - allow future payments with this PaymentIntent's PaymentMethod
=item * shipping - HashRef - shipping information for this PaymentIntent
=item * use_stripe_sdk - Bool - use manual confirmation and the iOS or Android SDKs to handle additional authentication steps
=back
Returns a L<Net::Stripe::PaymentIntent>
$stripe->confirm_payment_intent(
payment_intent_id => $payment_intent_id,
);
=payment_intent_method capture_payment_intent
Capture the funds for the PaymentIntent
L<https://stripe.com/docs/api/payment_intents/capture#capture_payment_intent>
=over
=item * payment_intent_id - StripePaymentIntentId - id of PaymentIntent to capture - required
=item * amount_to_capture - Int - amount to capture from the PaymentIntent
=item * application_fee_amount - Int - application fee amount
=item * statement_descriptor - Str - descriptor for statement
=item * statement_descriptor_suffix - Str - suffix to be concatenated with the statement descriptor
=item * transfer_data - HashRef - parameters used to automatically create a Transfer when the payment succeeds
=back
Returns a L<Net::Stripe::PaymentIntent>
$stripe->capture_payment_intent(
payment_intent_id => $payment_intent_id,
);
=payment_intent_method cancel_payment_intent
Cancel the PaymentIntent
L<https://stripe.com/docs/api/payment_intents/cancel#cancel_payment_intent>
=over
=item * payment_intent_id - StripePaymentIntentId - id of PaymentIntent to cancel - required
=item * cancellation_reason - StripeCancellationReason - reason for cancellation
=back
Returns a L<Net::Stripe::PaymentIntent>
$stripe->cancel_payment_intent(
payment_intent_id => $payment_intent_id,
cancellation_reason => 'requested_by_customer',
);
=payment_intent_method list_payment_intents
Retrieve a list of PaymentIntents
L<https://stripe.com/docs/api/payment_intents/list#list_payment_intents>
=over
=item * customer - StripeCustomerId - return only PaymentIntents for the specified Customer id
=item * created - HashRef[Int] - created conditions to match
=item * ending_before - Str - ending before condition
=item * limit - Int - maximum number of objects to return
=item * starting_after - Str - starting after condition
=back
Returns a L<Net::Stripe::List> object containing L<Net::Stripe::PaymentIntent> objects.
$stripe->list_payment_intents(
customer => $customer_id,
type => 'card',
limit => 10,
);
=cut
PaymentIntents: {
method create_payment_intent(
Int :$amount!,
Str :$currency!,
Int :$application_fee_amount?,
StripeCaptureMethod :$capture_method?,
Bool :$confirm?,
StripeConfirmationMethod :$confirmation_method?,
StripeCustomerId :$customer?,
Str :$description?,
Bool :$error_on_requires_action?,
Str :$mandate?,
HashRef :$mandate_data?,
HashRef[Str] :$metadata?,
Bool :$off_session?,
Str :$on_behalf_of?,
StripePaymentMethodId :$payment_method?,
HashRef :$payment_method_options?,
ArrayRef[StripePaymentMethodType] :$payment_method_types?,
Str :$receipt_email?,
Str :$return_url?,
Bool :$save_payment_method?,
Str :$setup_future_usage?,
HashRef :$shipping?,
Str :$statement_descriptor?,
Str :$statement_descriptor_suffix?,
HashRef :$transfer_data?,
Str :$transfer_group?,
Bool :$use_stripe_sdk?,
) {
my %args = (
amount => $amount,
currency => $currency,
application_fee_amount => $application_fee_amount,
capture_method => $capture_method,
confirm => $confirm,
confirmation_method => $confirmation_method,
customer => $customer,
description => $description,
error_on_requires_action => $error_on_requires_action,
mandate => $mandate,
mandate_data => $mandate_data,
metadata => $metadata,
off_session => $off_session,
on_behalf_of => $on_behalf_of,
payment_method => $payment_method,
payment_method_options => $payment_method_options,
payment_method_types => $payment_method_types,
receipt_email => $receipt_email,
return_url => $return_url,
save_payment_method => $save_payment_method,
setup_future_usage => $setup_future_usage,
shipping => $shipping,
statement_descriptor => $statement_descriptor,
statement_descriptor_suffix => $statement_descriptor_suffix,
transfer_data => $transfer_data,
transfer_group => $transfer_group,
use_stripe_sdk => $use_stripe_sdk,
);
my $payment_intent_obj = Net::Stripe::PaymentIntent->new( %args );
return $self->_post("payment_intents", $payment_intent_obj);
}