This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-stripe-api.php
3362 lines (3106 loc) · 155 KB
/
wp-stripe-api.php
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
<?php
/**
* WP Stripe API
*
* @link https://stripe.com/docs/api
* @package WP-API-Libraries\WP-Stripe-API
*/
/*
* Plugin Name: WP Stripe API
* Plugin URI: https://github.com/wp-api-libraries/wp-stripe-api
* Description: Perform API requests to Stripe in WordPress.
* Author: WP API Libraries
* Version: 1.0.0
* Author URI: https://wp-api-libraries.com
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-stripe-api
* GitHub Branch: master
*/
/* Exit if accessed directly. */
if ( ! defined( 'ABSPATH' ) ) {
exit; }
/* Check if class exists. */
if ( ! class_exists( 'StripeAPI' ) ) {
/**
* StripeAPI Class.
*/
class StripeAPI {
/* ------------------- API Methods ------------------------ */
protected $base_uri = 'https://api.stripe.com/v1/';
private $api_key;
protected $args;
protected $idempotent_key;
public function __construct( $api_key, bool $is_debug = false ) {
$this->set_api_key( $api_key );
$this->is_debug = $is_debug;
}
public function set_api_key( $api_key ) {
$this->api_key = $api_key;
}
/**
* Build request function: prepares the class for a fetch request.
*
* @param string $route URL to be accessed.
* @param array $args Arguments to pass in. If the method is GET, will be passed as query arguments attached to the route. If the method is not get, but the content type as defined in headers is 'application/json', then the body of the request will be set to a json_encode of $args. Otherwise, they will be passed as the body.
* @param string $method (Default: 'GET') The method.
* @return [type] The return of the function.
*/
protected function build_request( $route, $body = array(), $method = 'GET' ) {
// Sets request args and headers.
$this->args['method'] = $method;
$this->set_headers();
// Sets route.
$this->route = $route;
// If method is get, then there is no body.
if ( 'GET' === $method ) {
$this->route = add_query_arg( array_filter( $body ), $route );
} // Otherwise, if the content type is application/json, then the body needs to be json_encoded
elseif ( 'application/json' === $this->args['headers']['Content-Type'] ) {
$this->args['body'] = wp_json_encode( $body );
} // Anything else, let the user take care of it.
else {
$this->args['body'] = $body;
}
$this->args['timeout'] = 20;
return $this;
}
protected function fetch() {
// pp( $this->base_uri . $this->route, $this->args );
$response = wp_remote_request( $this->base_uri . $this->route, $this->args );
// pp( $this->base_uri . $this->route, $response );
// Retrieve status code and body.
$code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ) );
// Clear last request.
$this->clear();
if ( ! $this->is_status_ok( $code ) && ! $this->is_debug ) {
return new WP_Error( 'response-error', sprintf( __( 'Status: %d', 'wp-postmark-api' ), $code ), $body );
}
return $body;
}
/**
* Returns whether status is in [ 200, 300 ).
*/
protected function is_status_ok( $code ) {
return ( 200 <= $code && 300 > $code );
}
protected function set_headers() {
$this->args['headers'] = array(
'Authorization' => ' Bearer ' . $this->api_key,
'Content-Type' => 'application/x-www-form-urlencoded',
);
if ( 'GET' !== $this->args['method'] && 'DELETE' !== $this->args['method'] ) {
$this->args['headers']['Idempotency-Key'] = $this->new_uuid();
}
}
protected function clear() {
$this->args = array();
}
protected function run( $route, $body = array(), $method = 'GET' ) {
// Strip slashes from route.
$route = rtrim( $route, '/' );
return $this->build_request( $route, $body, $method )->fetch();
}
public function new_uuid() {
// Can we just bin2hex(random_bytes(16)) instead? For cleanliness.
return bin2hex( random_bytes( 16 ) );
}
/* ------------------- CORE RESOURCES --------------------- */
/* BALANCES. */
/**
* Retrieves the current account balance, based on the authentication that was
* used to make the request.
*
* @return object Returns a balance object for the account authenticated as.
*/
public function get_balance() {
// https://api.stripe.com/v1/balance
return $this->run( 'balance' );
}
/**
* Retrieves the balance transaction with the given ID.
*
* @param string $transaction_id The ID of the desired balance transaction (as
* found on any API object that affects the balance,
* e.g., a charge or transfer).
* @return object A balance transaction if a valid balance transaction
* ID was provided. Returns an error otherwise.
*/
public function get_balance_transaction( $transaction_id ) {
return $this->run( 'balance/history/' . $transaction_id );
}
/**
* Returns a list of transactions that have contributed to the Stripe account
* balance (e.g., charges, transfers, and so forth). The transactions are returned
* in sorted order, with the most recent transactions appearing first.
*
* $args supports optional arguments:
* available_on:
* A filter on the list based on the object available_on field. The value can
* be a string with an integer Unix timestamp, or it can be a dictionary with
* the options lt, lte, gt, and/or gte.
* created:
* A filter on the list based on the object created field. The value can be
* a string with an integer Unix timestamp, or it can be a dictionary with
* the options lt, lte, gt, and/or gte.
* currency:
* A specified currency to filter by.
* ending_before:
* A cursor for use in pagination. ending_before is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, starting with obj_bar, your subsequent call can include
* ending_before=obj_bar in order to fetch the previous page of the list.
* limit:
* A limit on the number of objects to be returned. Limit can range between
* 1 and 100 items, and the default is 10 items.
* payout:
* For automatic Stripe payouts only, only returns transactions that were
* payed out on teh specified payout ID.
* source:
* Only returns the original transaction.
* starting_after:
* A cursor for use in pagination. starting_after is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, ending with obj_foo, your subsequent call can include
* starting_after=obj_foo in order to fetch the next page of the list.
* type:
* Only returns transactions that are a charge, refund, adjustment, application_fee,
* application_fee_refund, transfer, payment, payout, payout_failure, or stripe_fee.
*
* @param array $args Additional arguments to supply to your query.
* @return object A dictionary with a data property that contains an array
* of up to limit transactions, starting after transaction
* starting_after. Each entry in the array is a separate
* transaction history object. If no more transactions are
* available, the resulting array will be empty.
*/
public function list_balance_history( $args = array() ) {
return $this->run( 'balance/history', $args );
}
/* CHARGES. */
/**
* Create a charge
*
* To charge a credit card, you create a Charge object. If your API key is in
* test mode, the supplied payment source (e.g., card) won't actually be charged,
* though everything else will occur as if in live mode. (Stripe assumes that the
* charge would have completed successfully).
*
* This function takes care of ammount, currency, and source/customer. All else
* are optional.
*
* @param [type] $amount A positive integer in the smallest currency unit (e.g.,
* 100 cents to charge $1.00 or 100 to charge ¥100, a zero-decimal
* currency) representing how much to charge the card. The
* minimum amount is $0.50 US or equivalent in charge currency.
* @param string $source A payment source to be charged, such as a credit card.
* If you also pass a customer ID, the source must be the
* ID of a source belonging to the customer (e.g., a saved
* card). Otherwise, if you do not pass a customer ID,
* the source you provide must either be a token, like
* the ones returned by Stripe.js, or a dictionary containing
* a user's credit card details, with the options described
* below. Although not all information is required, the extra
* info helps prevent fraud.
* @param string $customer The ID of an existing customer that will be charged
* in this request.
* @param string $currency (Default: 'usd') 3-letter ISO code for currency.
* @param array $args (Default: array()) An array of additional arguments.
* Supports 'application_fee', 'capture', 'description',
* 'destination', 'transfer_group', 'on_behalf_of', 'metadata',
* 'receipt_email', 'shipping', and 'statement_descriptor'.
* @return object A charge object if the charge succeeded. Returns
* an error if something goes wrong. A common source of error
* is an invalid or expired card, or a valid card with insufficient
* available balance.
*
* If the cvc parameter is provided, Stripe will attempt to
* check the correctness of the CVC, and the check's result
* will be returned. Similarly, if address_line1 or address_zip
* are provided, Stripe will try to check the validity of
* those parameters. Some card issuers do not support checking
* one or more of these parameters, in which case Stripe will
* return an 'unavailable' result. Also note that, depending
* on the card issuer, charges can succeed even when passed
* incorrect CVC and address information.
*/
public function create_charge( $amount, $source = null, $customer = null, $currency = 'usd', $args = array() ) {
if ( null === $source && null === $token ) {
return new WP_Error( 'invalid-data', __( 'Must provide either source or token parameters for creating a charge.', 'wp-stripe-api' ), array( 'status' => 400 ) );
}
$args['amount'] = $amount;
$args['currency'] = $currency;
if ( null !== $source ) {
$args['source'] = $source;
}
if ( null !== $token ) {
$args['token'] = $token;
}
return $this->run( 'charges', $args, 'POST' );
}
/**
* Retrieve a charge
*
* Retrieves the details of a charge that has previously been created. Supply
* the unique charge ID that was returned from your previous request, and Stripe
* will return the corresponding charge information. The same information is
* returned when creating or refunding the charge.
*
* @param string $charge_id The identifier of the charge to be retrieved.
* @return object A charge if a valid identifier was provided,
* and returns an error otherwise.
*/
public function retrieve_charge( $charge_id ) {
return $this->run( 'charges/' . $charge_id );
}
/**
* Update a charge
*
* Updates the specified charge by setting the values of the parameters passed.
* Any parameters not provided will be left unchanged.
*
* This request accepts only the customer, description, fraud_details, metadata,
* receipt_email, and shipping arguments.
*
* @param string $charge_id The identifier of the charge to be retrieved.
* @param array $args (Default: array()) Properties that you would like to update,
* including and limited to customer, description, fraud_details,
* metadata, receipt_email, and shipping arguments.
* @return object The charge object if the update succeeded. This
* call will return an error if update parameters are invalid.
*/
public function update_charge( $charge_id, $args = array() ) {
return $this->run( 'charges/' . $charge_id, $args, 'POST' );
}
/**
* Capture a charge
*
* Capture the payment of an existing, uncaptured, charge. This is the second half
* of the two-step payment flow, where first you created a charge with the capture
* option set to false.
*
* Uncaptured payments expire exactly seven days after they are created. If they
* are not captured by that point in time, they will be marked as refunded and
* will no longer be capturable.
*
* @param string $charge_id The identifier of the charge to be retrieved.
* @param array $args (Default: array()) Arguments you may pass into the charge
* being captured. Including and limited to amount, application_fee,
* destination, receipt_email, statement_descriptor
* @return object The charge object, with an updated captured property
* (set to true). Capturing a charge will always succeed,
* unless the charge is already refunded, expired, captured,
* or an invalid capture amount is specified, in which case
* this method will return an error.
*/
public function capture_charge( $charge_id, $args = array() ) {
return $this->run( 'charges/' . $charge_id . '/capture', $args, 'POST' );
}
/**
* List all charges
*
* Returns a list of charges you’ve previously created. The charges are returned
* in sorted order, with the most recent charges appearing first.
*
* @param array $args (Default: array()) Optional arguments to clarify the query.
* created:
* A filter on the list based on the object created
* field. The value can be a string with an integer Unix timestamp,
* or it can be a dictionary with the following options:
* customer:
* Only return charges for the customer specified by this customer ID.
* ending_before:
* A cursor for use in pagination. ending_before is an
* object ID that defines your place in the list. For instance,
* if you make a list request and receive 100 objects, starting
* with obj_bar, your subsequent call can include ending_before=obj_bar
* in order to fetch the previous page of the list.
* limit:
* A limit on the number of objects to be returned. Limit
* can range between 1 and 100 items, and the default is 10 items.
* source:
* A filter on the list based on the source of the charge.
* The value can be a dictionary with the following options:
* starting_after:
* A cursor for use in pagination. starting_after is an object
* ID that defines your place in the list. For instance, if
* you make a list request and receive 100 objects, ending with
* obj_foo, your subsequent call can include starting_after=obj_foo
* in order to fetch the next page of the list.
* transfer_group:
* Only return charges for this transfer group.
* @return object A dictionary with a data property that contains an array of
* up to limit charges, starting after charge starting_after. Each
* entry in the array is a separate charge object. If no more charges
* are available, the resulting array will be empty. If you provide a
* non-existent customer ID, this call returns an error.
*/
public function list_charges( $args = array() ) {
return $this->run( 'charges', $args );
}
/* CUSTOMERS. */
/**
* Create a customer
*
* Creates a new customer object.
*
* $args argument supports the following properties.
*
* account_balance
* An integer amount in cents that is the starting account balance for your customer. A negative amount represents a credit that will be used before attempting any charges to the customer’s card; a positive amount will be added to the next invoice.
*
* business_vat_id:
* The customer’s VAT identification number. If you are using Relay, this
* field gets passed to tax provider you are using for your orders.
* coupon:
* If you provide a coupon code, the customer will have a discount applied
* on all recurring charges. Charges you create through the API will not
* have the discount.
* default_source:
* The default source to use.
* description:
* An arbitrary string that you can attach to a customer object. It is
* displayed alongside the customer in the dashboard.
* email:
* Customer’s email address. It’s displayed alongside the customer in your
* dashboard and can be useful for searching and tracking. This may be up to
* 512 characters. This will be unset if you POST an empty value.
* metadata:
* A set of key/value pairs that you can attach to a customer object. It can be
* useful for storing additional information about the customer in a structured format.
* shipping:
* Shipping meta-information.
* source:
* The source can either be a Token’s or a Source’s ID, as returned by Elements,
* or a dictionary containing a user’s credit card details (with the options shown below).
*
* @param string $email The customer's email.
* @param array $args (Default: array()) Additional properties to pass in.
* @return object Returns a customer object if the call succeeded. The
* returned object will have information about subscriptions,
* discount, and payment sources, if that information has been
* provided. If an invoice payment is due and a source is not
* provided, the call will return an error. If a non-existent
* plan or a non-existent or expired coupon is provided, the
* call will return an error.
*
* If a source has been attached to the customer, the returned
* customer object will have a default_source attribute, which
* is an ID that can be expanded into the full source details
* when retrieving the customer.
*/
public function create_customer( $email = '', $args = array() ) {
if ( '' !== $email ) {
$args['email'] = $email;
}
return $this->run( 'customers', $args, 'POST' );
}
/**
* Retrieve a customer
*
* Retrieves the details of an existing customer. You need only supply the
* unique customer identifier that was returned upon customer creation.
*
* @param string $customer_id The identifier of the customer to be retrieved.
* @return object Returns a customer object if a valid identifier
* was provided. When requesting the ID of a customer
* that has been deleted, a subset of the customer’s
* information will be returned, including a deleted
* property, which will be true.
*/
public function retrieve_customer( $customer_id ) {
return $this->run( 'customers/' . $customer_id );
}
/**
* Update a customer
*
* Updates the specified customer by setting the values of the parameters passed.
* Any parameters not provided will be left unchanged. For example, if you pass
* the source parameter, that becomes the customer’s active source (e.g., a card)
* to be used for all charges in the future. When you update a customer to a new
* valid source: for each of the customer’s current subscriptions, if the subscription
* bills automatically and is in the past_due state, then the latest unpaid, unclosed
* invoice for the subscription will be retried (note that this retry will not count
* as an automatic retry, and will not affect the next regularly scheduled payment
* for the invoice). (Note also that no invoices pertaining to subscriptions in the
* unpaid state, or invoices pertaining to canceled subscriptions, will be retried
* as a result of updating the customer’s source.)
*
* This request accepts mostly the same arguments as the customer creation call.
*
* @param string $customer_id The identifier of the customer to be updated.
* @param array $args (Default: array()) Properties to updated for
* a customer. Supports account_balance, business_vat_id,
* coupon, default_source, description, email, metadata
* shipping, source
* @return object The customer object if the update succeeded.
* Returns an error if update parameters are invalid (e.g.
* specifying an invalid coupon or an invalid source).
*/
public function update_customer( $customer_id, $args = array() ) {
return $this->run( 'customers/' . $customer_id, $args, 'POST' );
}
/**
* Delete a customer
*
* Permanently deletes a customer. It cannot be undone. Also immediately cancels
* any active subscriptions on the customer.
*
* @param string $customer_id The identifier of the customer to be deleted.
* @return object Returns an object with a deleted parameter on success.
* If the customer ID does not exist, this call returns
* an error.
*
* Unlike other objects, deleted customers can still
* be retrieved through the API, in order to be able
* to track the history of customers while still removing
* their credit card details and preventing any further
* operations to be performed (such as adding a
* new subscription).
*/
public function delete_customer( $customer_id ) {
return $this->run( 'customers/' . $customer_id, array(), 'DELETE' );
}
/**
* List all customers
*
* Returns a list of your customers. The customers are returned sorted by creation
* date, with the most recent customers appearing first.
*
* $args supports optional arguments:
* created:
* A filter on the list based on the object created field. The value can be
* a string with an integer Unix timestamp, or it can be a dictionary with
* the following options:
* email:
* A filter on the list based on the customer’s email field. The value must
* be a string. This will be unset if you POST an empty value.
* ending_before:
* A cursor for use in pagination. ending_before is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, starting with obj_bar, your subsequent call can include
* ending_before=obj_bar in order to fetch the previous page of the list.
* limit:
* A limit on the number of objects to be returned. Limit can range between
* 1 and 100 items, and the default is 10 items.
* starting_after:
* A cursor for use in pagination. starting_after is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, ending with obj_foo, your subsequent call can include
* starting_after=obj_foo in order to fetch the next page of the list.
*
* @param array $args (Default: array()) An array of arguments that can
* modify the query.
* @return [type] A dictionary with a data property that contains an array
* of up to limit customers, starting after customer starting_after.
* Passing an optional email will result in filtering to customers
* with only that exact email address. Each entry in the array is
* a separate customer object. If no more customers are available,
* the resulting array will be empty. This request should never
* return an error.
*/
public function list_customers( $args = array() ) {
return $this->run( 'customers', $args );
}
/* DISPUTES. */
/**
* Retrieve a dispute
*
* Retrieves the dispute with the given ID.
*
* @param string $dispute_id ID of dspute to retrieve.
* @return object Returns a dispute if a valid dispute ID was provided.
* Returns an error otherwise.
*/
public function retrieve_dispute( $dispute_id ) {
return $this->run( 'disputes/' . $dispute_id );
}
/**
* Update a dispute
*
* When you get a dispute, contacting your customer is always the best first step.
* If that doesn’t work, you can submit evidence in order to help us resolve the
* dispute in your favor. You can do this in your dashboard, but if you prefer,
* you can use the API to submit evidence programmatically.
*
* Depending on your dispute type, different evidence fields will give you a
* better chance of winning your dispute. You may want to consult our guide to
* dispute types to help you figure out which evidence fields to provide.
*
* $args supports optional arguments to update:
* evidence:
* Evidence to upload to respond to a dispute. Updating any field in the
* hash will submit all fields in the hash for review.
*
* Includes subfields:
* access_activity_log, billing_address, cancellation_policy,
* cancellation_policy_disclosure, cancellation_rebuttal, customer_communication,
* customer_email_address, customer_name, customer_purchase_ip, customer_signature,
* duplicate_charge_documentation, duplicate_charge_explanation, duplicate_charge_id,
* product_description, receipt, refund_policy, refund_policy_disclosure,
* refund_refusal_explanation, service_date, service_documentation, shipping_address,
* shipping_carrier, shipping_date, shipping_documentation, shipping_tracking_number,
* uncategorized_file, uncategorized_text
* metadata:
* A set of key/value pairs that you can attach to a dispute object. It
* can be useful for storing additional information about the dispute in
* a structured format.
* submit:
* Whether to immediately submit evidence to the bank. If false, evidence
* is staged on the dispute. Staged evidence is visible in the API and Dashboard,
* and can be submitted to the bank by making another request with this attribute
* set to true (the default).
*
* @param string $dispute_id ID of dispute to update.
* @param array $args (Default: array()) An array of arguments to update
* for the dispute.
* @return object Returns the dispute object.
*/
public function update_dispute( $dispute_id, $args = array() ) {
return $this->run( 'disputes/' . $dispute_id, $args, 'POST' );
}
/**
* Close a dispute
*
* Closing the dispute for a charge indicates that you do not have any evidence
* to submit and are essentially ‘dismissing’ the dispute, acknowledging it as lost.
*
* The status of the dispute will change from needs_response to lost. Closing a
* dispute is irreversible.
*
* @param string $dispute_id ID of dispute to close.
* @return object Returns the dispute object.
*/
public function close_dispute( $dispute_id ) {
return $this->run( 'disputes/' . $dispute_id . '/close', array(), 'POST' );
}
/**
* List all disputes
*
* Get a list of all disputes.
*
* $args supports optional arguments:
* created:
* A filter on the list based on the object created field. The value can be
* a string with an integer Unix timestamp, or it can be a dictionary with
* the following options, gt, gte, lt, lte
* ending_before:
* A cursor for use in pagination. ending_before is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, starting with obj_bar, your subsequent call can include
* ending_before=obj_bar in order to fetch the previous page of the list.
* limit:
* A limit on the number of objects to be returned. Limit can range between
* 1 and 100 items, and the default is 10 items.
* starting_after:
* A cursor for use in pagination. starting_after is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, ending with obj_foo, your subsequent call can include
* starting_after=obj_foo in order to fetch the next page of the list.
*
* @param array $args [description]
* @return object A dictionary with a data property that contains an array
* of up to limit disputes, starting after dispute starting_after.
* Each entry in the array is a separate dispute object. If no
* more disputes are available, the resulting array will be
* empty. This request should never return an error.
*/
public function list_disputes( $args = array() ) {
return $this->run( 'disputes', $args );
}
/* EVENTS. */
/**
* Retrieve an event
*
* Retrieves the details of an event. Supply the unique identifier of the event,
* which you might have received in a webhook.
*
* @param string $id The ID for the event.
* @return object Returns an event object if a valid identifier was provided.
* All events share a common structure, detailed to the right.
* The only property that will differ is the data property.
*
* In each case, the data dictionary will have an attribute called
* object and its value will be the same as retrieving the same
* object directly from the API. For example, a customer.created
* event will have the same information as retrieving the
* relevant customer would.
*
* In cases where the attributes of an object have changed, data
* will also contain a dictionary containing the changes.
*/
public function retrieve_event( $id ) {
return $this->run( 'events/' . $id );
}
/**
* List all events
*
* List events, going back up to 30 days.
*
* $args supports optional query arguments:
* created:
* Arguments based on date, supports gt, gte, lt, and lte.
* ending_before:
* A cursor for use in pagination. ending_before is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, starting with obj_bar, your subsequent call can include
* ending_before=obj_bar in order to fetch the previous page of the list.
* limit:
* A limit on the number of objects to be returned. Limit can range between
* 1 and 100 items, and the default is 10 items.
* starting_after:
* A cursor for use in pagination. starting_after is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, ending with obj_foo, your subsequent call can include
* starting_after=obj_foo in order to fetch the next page of the list.
* type:
* A string containing a specific event name, or group of events using * as
* a wildcard. The list will be filtered to include only events with a
* matching event property. You may pass either type or types, but not both.
* types:
* An array of up to 20 strings containing specific event names. The list
* will be filtered to include only events with a matching event property.
* You may pass either type or types, but not both.
*
* @param array $args (Default: array() Additional optional arguments to pass.
* @return object A dictionary with a data property that contains an array
* of up to limit events, starting after event starting_after.
* Each entry in the array is a separate event object. If no
* more events are available, the resulting array will be empty.
* This request should never return an error.
*/
public function list_events( $args = array() ) {
return $this->run( 'events', $args );
}
/* FILE UPLOADS. */
/**
* Create a file upload
*
* To upload a file to Stripe, you’ll need to send a request of type multipart/form-data.
* The request should contain the file you would like to upload, as well as the
* parameters for creating a file.
*
* All of Stripe’s officially supported API libraries should have support for
* sending multipart/form-data.
*
* @param string $file_path A file to upload. The file should follow the specifications
* of RFC 2388 (which defines file transfers for the
* multipart/form-data protocol).
* @param string $purpose The purpose of the uploaded file. Possible values are
* customer_signature, dispute_evidence, identity_document,
* tax_document_user_upload.
* @return object The file object.
*/
public function create_file_upload( $file_path, $purpose ) {
$args = array(
'purpose' => $purpose,
'file' => $file_path,
);
return $this->run( 'files', $args, 'POST' );
}
/**
* Retrieve a file upload
*
* Retrieves the details of an existing file object. Supply the unique file upload
* ID from a file creation request, and Stripe will return the corresponding
* transfer information.
*
* @param string $file_id The identifier of the file upload to be retrieved.
* @return object A file upload object if a valid identifier
* was provided, and returns an error otherwise.
*/
public function retreive_file_upload( $file_id ) {
return $this->run( 'files/' . $file_id );
}
/**
* List all file uploads
*
* Returns a list of the files that you have uploaded to Stripe. The file uploads
* are returned sorted by creation date, with the most recently created file
* uploads appearing first.
*
* $args supports optional query arguments:
* created:
* Arguments based on date, supports gt, gte, lt, and lte.
* ending_before:
* A cursor for use in pagination. ending_before is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, starting with obj_bar, your subsequent call can include
* ending_before=obj_bar in order to fetch the previous page of the list.
* limit:
* A limit on the number of objects to be returned. Limit can range between
* 1 and 100 items, and the default is 10 items.
* starting_after:
* A cursor for use in pagination. starting_after is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, ending with obj_foo, your subsequent call can include
* starting_after=obj_foo in order to fetch the next page of the list.
* purpose:
* The file purpose to filter queries by. If none is provided, files will
* not be filtered by purpose.
*
* @param array $args (Default: array()) Optional arguments for the query.
* @return object A dictionary with a data property that contains an array
* of up to limit file uploads, starting after file upload
* starting_after. Each entry in the array is a separate file
* upload object. If no more file uploads are available, the
* resulting array will be empty. This request should never
* return an error.
*/
public function list_uploads( $args = array() ) {
return $this->run( 'files', $args );
}
/* PAYOUTS. */
/**
* Create a payout
*
* To send funds to your own bank account, you create a new payout object.
* Your Stripe balance must be able to cover the payout amount, or you’ll
* receive an “Insufficient Funds” error.
*
* If your API key is in test mode, money won’t actually be sent, though everything
* else will occur as if in live mode.
*
* If you are creating a manual payout on a Stripe account that uses multiple
* payment source types, you’ll need to specify the source type balance that
* the payout should draw from. The balance object details available and pending
* amounts by source type.
*
* $args supports additional arguments:
* currency:
* Three-letter ISO currency code, in lowercase. Must be a supported currency.
* description:
* An arbitrary string attached to the object. Often useful for displaying
* to users. This will be unset if you POST an empty value.
* destination:
* The ID of a bank account or a card to send the payout to. If no destination
* is supplied, the default external account for the specified currency will be used.
* metadata:
* A set of key/value pairs that you can attach to a payout object. It can be
* useful for storing additional information about the payout in a structured format.
* method:
* The method used to send this payout, which can be standard or instant.
* Instant is only supported for payouts to debit cards. (See Instant payouts
* for marketplaces for more information.)
* source_type:
* The source balance to draw this payout from. Balances for different payment
* sources are kept separately. You can find the amounts with the balances API.
* Valid options are: alipay_account, bank_account, and card.
* statement_descriptor:
* A string to be displayed on the recipient’s bank or card statement. This
* may be at most 22 characters. Attempting to use a statement_descriptor
* longer than 22 characters will return an error. Note: Most banks will
* truncate this information and/or display it inconsistently. Some may
* not display it at all.
*
* @param int $amount A positive integer in cents representing how much to payout.
* @param array $args (Default: array()) Additional arguments, describing the payout.
* If currency is not specificied, 'usd' is automatically set.
* @return object Returns a payout object if there were no initial errors with
* the payout creation (invalid routing number, insufficient funds,
* etc). The status of the payout object will be initially
* marked as pending.
*/
public function create_payout( $amount, $args = array() ) {
if ( ! isset( $args['currency'] ) ) {
$args['currency'] = 'usd';
}
$args['amount'] = intval( $amount );
return $this->run( 'payouts', $args, 'POST' );
}
/**
* Retrieve a payout
*
* Retrieves the details of an existing payout. Supply the unique payout ID
* from either a payout creation request or the payout list, and Stripe will
* return the corresponding payout information.
*
* @param string $payment_id The identifier of the payout to be retrieved.
* @return object Returns a payout object if a valid identifier
* was provided, and returns an error otherwise.
*/
public function retreive_payout( $payment_id ) {
return $this->run( 'payouts/' . $payment_id );
}
/**
* Update a payout
*
* Updates the specified payout by setting the values of the parameters passed.
* Any parameters not provided will be left unchanged. This request accepts only
* the metadata as arguments.
*
* @param string $payment_id The identifier of the payout to be updated.
* @param array $metadata A set of key/value pairs that you can attach to
* a payout object. It can be useful for storing additional
* information about the payout in a structured format.
* @return object Returns the payout object if the update succeeded. This
* call returns an error if update parameters are invalid.
*/
public function update_payout( $payment_id, $metadata = null ) {
$args = ( null !== $metadata ) ? array( 'metadata' => $metadata ) : array();
return $this->run( 'payouts/' . $payment_id, $args, 'POST' );
}
/**
* List all payouts
*
* Returns a list of existing payouts sent to third-party bank accounts or that
* Stripe has sent you. The payouts are returned in sorted order, with the most
* recently created payouts appearing first.
*
* $args supports optional query arguments:
* arrival_date:
* Arguments based on date, supports gt, gte, lt, and lte.
* created:
* Arguments based on date, supports gt, gte, lt, and lte.
* ending_before:
* A cursor for use in pagination. ending_before is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, starting with obj_bar, your subsequent call can include
* ending_before=obj_bar in order to fetch the previous page of the list.
* limit:
* A limit on the number of objects to be returned. Limit can range between
* 1 and 100 items, and the default is 10 items.
* starting_after:
* A cursor for use in pagination. starting_after is an object ID that defines
* your place in the list. For instance, if you make a list request and receive
* 100 objects, ending with obj_foo, your subsequent call can include
* starting_after=obj_foo in order to fetch the next page of the list.
* destination:
* The ID of an external account - only return payouts sent to this external account.
* status:
* Only return payouts that have the given status: pending, paid, failed, or canceled
*
* @param array $args (Default: array()) Additional query arguments.
* @return object A dictionary with a data property that contains an array
* of up to limit payouts, starting after payout starting_after.
* Each entry in the array is a separate payout object. If no more
* payouts are available, the resulting array will be empty.
*/
public function list_payouts( $args = array() ) {
return $this->run( 'payouts', $args );
}
/**
* Cancel a payout
*
* A dictionary with a data property that contains an array of up to limit payouts,
* starting after payout starting_after. Each entry in the array is a separate payout
* object. If no more payouts are available, the resulting array will be empty.
*
* @param string $payout_id The identifier of the payout to be canceled.
* @return object A the payout object if the cancellation succeeded.
* Returns an error if the payout has already been canceled
* or cannot be canceled.
*/
public function cancel_payout( $payout_id ) {
return $this->run( 'payouts/' . $payout_id . '/cancel', array(), 'POST' );
}
/* PRODUCTS. */
/**
* Creates a new product object. To create a product for use with subscriptions, see Subscriptions Products.
*
* @see https://stripe.com/docs/api/curl#create_product Documentation
*
* @param string $name The product’s name, meant to be displayable to the customer. Applicable to both service
* and good types.
* @param string $type The type of the product. The product is either of type service, which is eligible for
* use with Subscriptions and Plans or good, which is eligible for use with Orders and SKUs.
* @param array $optional Optional args to send into the request. See documentation.
* @return array Product Object
*/
public function create_product( $name, $type, $optional = array() ) {
$args = array_merge( compact( 'name', 'type' ), $optional );
return $this->run( 'products', $args, 'POST' );
}
/**
* Retrieves the details of an existing product. Supply the unique product ID from either a product creation request
* or the product list, and Stripe will return the corresponding product information.
*
* @see https://stripe.com/docs/api/curl#retrieve_product Documentation
*
* @param string $product_id ID of product to retrieve.
* @return array Product Object
*/
public function retrieve_product( $product_id ) {
return $this->run( "products/$product_id" );
}
/**
* Updates the specific product by setting the values of the parameters passed. Any parameters not provided will be
* left unchanged.
*