forked from sekati/php-stripe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStripe.php
653 lines (581 loc) · 23.3 KB
/
Stripe.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
<?php
// Codeigniter access check, remove it for direct use
if( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
// Set the server api endpoint and http methods as constants
define( 'STRIPE_API_ENDPOINT', 'https://api.stripe.com/v1/' );
define( 'STRIPE_METHOD_POST', 'post' );
define( 'STRIPE_METHOD_DELETE', 'delete' );
/**
* A simple to use library to access the stripe.com services.
*
* @copyright Copyright (c) 2011 Pixative Solutions
* @copyright Copyright (c) 2013 Sekati
* @copyright Copyright (c) 2013 Kolby Sisk
* @author Ben Cessa <[email protected]> - http://www.pixative.com
* @author Jason Horwitz <[email protected]>
* @author Kolby Sisk <[email protected]> - http://www.kolbysisk.com
*/
class Stripe {
/**
* Holder for the initial configuration parameters
*
* @var resource
* @access private
*/
private $_conf = NULL;
/**
* Constructor method
*
* @param array Configuration parameters for the library
*/
public function __construct( $params ) {
// Store the config values
$this->_conf = $params;
}
/**
* Create and apply a charge to an existent user based on it's customer_id
*
* @param int The amount to charge in cents ( USD )
* @param string The customer id of the charge subject
* @param string A free form reference for the charge
*/
public function charge_customer( $amount, $customer_id, $desc ) {
$params = array(
'amount' => $amount,
'currency' => $this->_conf['stripe_currency'],
'customer' => $customer_id,
'description' => $desc
);
return $this->_send_request( 'charges', $params, STRIPE_METHOD_POST );
}
/**
* Create and apply a charge based on credit card information
*
* @param int The amount to charge in cents ( USD )
* @param mixed This can be a card token generated with stripe.js ( recommended ) or
* an array with the card information: number, exp_month, exp_year, cvc, name
* @param string A free form reference for the charge
*/
public function charge_card( $amount, $card, $desc ) {
$params = array(
'amount' => $amount,
'currency' => $this->_conf['stripe_currency'],
'card' => $card,
'description' => $desc
);
return $this->_send_request( 'charges', $params, STRIPE_METHOD_POST );
}
/**
* Retrieve information about a specific charge
*
* @param string The charge ID to query
*/
public function charge_info( $charge_id ) {
return $this->_send_request( 'charges/'.$charge_id );
}
/**
* Refund a charge
*
* @param string The charge ID to refund
* @param int The amount to refund, defaults to the total amount charged
*/
public function charge_refund( $charge_id, $amount = FALSE ) {
$amount ? $params = array( 'amount' => $amount ) : $params = array();
return $this->_send_request( 'charges/'.$charge_id.'/refund', $params, STRIPE_METHOD_POST );
}
/**
* Get a list of charges, either general or for a certain customer
*
* @param int The number of charges to return, default 10, max 100
* @param int Offset to apply to the list, default 0
* @param string A customer ID to return only charges for that customer
*/
public function charge_list( $count = 10, $offset = 0, $customer_id = FALSE ) {
$params['count'] = $count;
$params['offset'] = $offset;
if( $customer_id )
$params['customer'] = $customer_id;
$vars = http_build_query( $params, NULL, '&' );
return $this->_send_request( 'charges?'.$vars );
}
/**
* Register a new customer on system
*
* @param mixed This can be a card token generated with stripe.js ( recommended ) or
* an array with the card information: number, exp_month, exp_year, cvc, name
* @param string The customer email address, useful as reference
* @param string A free form reference for the customer record
* @param string A subscription plan identifier to add the customer to it
* @param string If you provide a coupon code, the customer will have a discount applied on all recurring charges.
*/
public function customer_create( $card, $email, $desc = NULL, $plan = NULL, $coupon = NULL ) {
$params = array(
'card' => $card,
'email' => $email
);
if( $desc )
$params['description'] = $desc;
if( $plan )
$params['plan'] = $plan;
if( $coupon )
$params['coupon'] = $coupon;
return $this->_send_request( 'customers', $params, STRIPE_METHOD_POST );
}
/**
* Retrieve information for a given customer
*
* @param string The customer ID to get information about
*/
public function customer_info( $customer_id ) {
return $this->_send_request( 'customers/'.$customer_id );
}
/**
* Update an existing customer record
*
* @param string The customer ID for the record to update
* @param array An array containing the new data for the user, you may use the
* following keys: card, email, description, coupon
*/
public function customer_update( $customer_id, $newdata ) {
return $this->_send_request( 'customers/'.$customer_id, $newdata, STRIPE_METHOD_POST );
}
/**
* Delete an existing customer record
*
* @param string The customer ID of the record to delete
*/
public function customer_delete( $customer_id ) {
return $this->_send_request( 'customers/'.$customer_id, array(), STRIPE_METHOD_DELETE );
}
/**
* Get a list of customers record ordered by creation date
*
* @param int The number of customers to return, default 10, max 100
* @param int Offset to apply to the list, default 0
*/
public function customer_list( $count = 10, $offset = 0 ) {
$params['count'] = $count;
$params['offset'] = $offset;
$vars = http_build_query( $params, NULL, '&' );
return $this->_send_request( 'customers?'.$vars );
}
/**
* Subscribe a customer to a plan
*
* @param string The customer ID
* @param string The plan identifier
* @param array Configuration options for the subscription: prorate, coupon, trial_end(stamp)
*/
public function customer_subscribe( $customer_id, $plan_id, $options = array() ) {
$options['plan'] = $plan_id;
return $this->_send_request( 'customers/'.$customer_id.'/subscription', $options, STRIPE_METHOD_POST );
}
/**
* Cancel a customer's subscription
*
* @param string The customer ID
* @param boolean Cancel the subscription immediately( FALSE ) or at the end of the current period( TRUE )
*/
public function customer_unsubscribe( $customer_id, $at_period_end = TRUE ) {
$at_period_end ? $pend = 'true' : $pend = 'false';
$url = 'customers/'.$customer_id.'/subscription?at_period_end='.$pend;
return $this->_send_request( $url, array(), STRIPE_METHOD_DELETE );
}
/**
* Get the next upcoming invoice for a given customer
*
* @param string Customer ID to get the invoice from
*/
public function customer_upcoming_invoice( $customer_id ) {
return $this->_send_request( 'invoices/upcoming?customer='.$customer_id );
}
/**
* Generate a new single-use stripe card token
*
* @param array An array containing the credit card data, with the following keys:
* number, cvc, exp_month, exp_year, name
* @param int If the token will be used on a charge, this is the amount to charge for
*/
public function card_token_create( $card_data, $amount ) {
$params = array(
'card' => $card_data,
'amount' => $amount,
'currency' => $this->_conf['stripe_currency']
);
return $this->_send_request( 'tokens', $params, STRIPE_METHOD_POST );
}
/**
* Get information about a card token
*
* @param string The card token ID to get the information
*/
public function card_token_info( $token_id ) {
return $this->_send_request( 'tokens/'.$token_id );
}
/**
* Create a new subscription plan on the system
*
* @param string The plan identifier, this will be used when subscribing customers to it
* @param int The amount in cents to charge for each period
* @param string The plan name, will be displayed in invoices and the web interface
* @param string The interval to apply on the plan, could be 'month' or 'year'
* @param int Number of days for the trial period, if any
*/
public function plan_create( $plan_id, $amount, $name, $interval, $trial_days = FALSE ) {
$params = array(
'id' => $plan_id,
'amount' => $amount,
'name' => $name,
'currency' => $this->_conf['stripe_currency'],
'interval' => $interval
);
if( $trial_days )
$params['trial_period_days'] = $trial_days;
return $this->_send_request( 'plans', $params, STRIPE_METHOD_POST );
}
/**
* Retrieve information about a given plan
*
* @param string The plan identifier you wish to get info about
*/
public function plan_info( $plan_id ) {
return $this->_send_request( 'plans/'.$plan_id );
}
/**
* Delete a plan from the system
*
* @param string The identifier of the plan you want to delete
*/
public function plan_delete( $plan_id ) {
return $this->_send_request( 'plans/'.$plan_id, array(), STRIPE_METHOD_DELETE );
}
/**
* Retrieve a list of the plans in the system
*/
public function plan_list( $count = 10, $offset = 0 ) {
$params['count'] = $count;
$params['offset'] = $offset;
$vars = http_build_query( $params, NULL, '&' );
return $this->_send_request( 'plans?'.$vars );
}
/**
* Get infomation about a specific invoice
*
* @param string The invoice ID
*/
public function invoice_info( $invoice_id ) {
return $this->_send_request( 'invoices/'.$invoice_id );
}
/**
* Get a list of invoices on the system
*
* @param string Customer ID to retrieve invoices only for a given customer
* @param int Number of invoices to retrieve, default 10, max 100
* @param int Offset to start the list from, default 0
*/
public function invoice_list( $customer_id = NULL, $count = 10, $offset = 0 ) {
$params['count'] = $count;
$params['offset'] = $offset;
if( $customer_id )
$params['customer'] = $customer_id;
$vars = http_build_query( $params, NULL, '&' );
return $this->_send_request( 'invoices?'.$vars );
}
/**
* Register a new invoice item to the upcoming invoice for a given customer
*
* @param string The customer ID
* @param int The amount to charge in cents
* @param string A free form description explaining the charge
*/
public function invoiceitem_create( $customer_id, $amount, $desc ) {
$params = array(
'customer' => $customer_id,
'amount' => $amount,
'currency' => $this->_conf['stripe_currency'],
'description' => $desc
);
return $this->_send_request( 'invoiceitems', $params, STRIPE_METHOD_POST );
}
/**
* Get information about a specific invoice item
*
* @param string The invoice item ID
*/
public function invoiceitem_info( $invoiceitem_id ) {
return $this->_send_request( 'invoiceitems/'.$invoiceitem_id );
}
/**
* Update an invoice item before is actually charged
*
* @param string The invoice item ID
* @param int The amount for the item in cents
* @param string A free form string describing the charge
*/
public function invoiceitem_update( $invoiceitem_id, $amount, $desc = FALSE ) {
$params['amount'] = $amount;
$params['currency'] = $this->_conf['stripe_currency'];
if( $desc ) $params['description'] = $desc;
return $this->_send_request( 'invoiceitems/'.$invoiceitem_id, $params, STRIPE_METHOD_POST );
}
/**
* Delete a specific invoice item
*
* @param string The invoice item identifier
*/
public function invoiceitem_delete( $invoiceitem_id ) {
return $this->_send_request( 'invoiceitems/'.$invoiceitem_id, array(), STRIPE_METHOD_DELETE );
}
/**
* Get a list of invoice items
*
* @param string Customer ID to retrieve invoices only for a given customer
* @param int Number of invoices to retrieve, default 10, max 100
* @param int Offset to start the list from, default 0
*/
public function invoiceitem_list( $customer_id = FALSE, $count = 10, $offset = 0 ) {
$params['count'] = $count;
$params['offset'] = $offset;
if( $customer_id )
$params['customer'] = $customer_id;
$vars = http_build_query( $params, NULL, '&' );
return $this->_send_request( 'invoiceitems?'.$vars );
}
/**
* Get infomation about a specific event
*
* @param string The event ID
*/
public function event_info( $event_id ) {
return $this->_send_request( 'events/'.$event_id );
}
/**
* Get a list of events on the system
*
* @param string A string containing a specific event name, or group of events using * as a wildcard.
* @param string/dict A filter on the list based on the events created date. The value can be a string with an exact UTC timestamp,
* or it can be a dictionary with the following options:
* gt (optional)
* Return values should have been created after this timestamp.
* gte (optional)
* Return values should have been created after or equal to this timestamp.
* lt (optional)
* Return values should have been created before this timestamp.
* lte (optional)
* Return values should have been created before or equal to this timestamp.
* @param int Number of invoices to retrieve, default 10, max 100
* @param int Offset to start the list from, default 0
*/
public function event_list( $type = NULL, $created = NULL, $count = 10, $offset = 0 ) {
$params['count'] = $count;
$params['offset'] = $offset;
if( $type )
$params['type'] = $type;
if( $created )
$params['created'] = $created;
$vars = http_build_query( $params, NULL, '&' );
return $this->_send_request( 'events?'.$vars );
}
/**
* Create a new coupon on the system
*
* @param string The coupon identifier, that will be used when applying it to a customer
* @param string The duration the coupon will be in effect: "forever", "once", or "repeating"
* @param int A positive integer between 1 and 100 that represents the discount the coupon will apply (if $amount_off is not passed)
* @param int A positive integer representing the amount to subtract from an invoice total (if $percent_off is not passed)
* @param string Currency of the amount_off parameter (if percent_off is not passed)
* @param string (required only if duration is repeating) If duration is repeating, a positive integer that specifies the number of months the discount will be in effect
* @param string A positive integer specifying the number of times the coupon can be redeemed before it's no longer valid. For example, you might have a 50% off coupon that the first 20 readers of your blog can use
* @param string UTC timestamp specifying the last time at which the coupon can be redeemed. After the redeem_by date, the coupon can no longer be applied to new customers.
*/
public function coupon_create( $coupon_id, $duration = 'once', $percent_off=NULL, $amount_off=NULL, $currency=NULL, $duration_in_months=NULL, $max_redemptions=NULL, $redeem_by=NULL ) {
$params = array( 'id' => $coupon_id );
if($duration) $params['duration'] = $duration;
if($percent_off) $params['percent_off'] = $percent_off;
if($amount_off) $params['amount_off'] = $amount_off;
if(!$percent_off) $params['currency'] = ($currency) ? $currency : $this->_conf['stripe_currency'];
if($duration_in_months) $params['duration_in_months'] = $duration_in_months;
if($max_redemptions) $params['max_redemptions'] = $max_redemptions;
if($redeem_by) $params['redeem_by'] = $redeem_by;
return $this->_send_request( 'coupons', $params, STRIPE_METHOD_POST );
}
/**
* Retrieve information about a given coupon
*
* @param string The coupon identifier you wish to get info about
*/
public function coupon_info( $coupon_id ) {
return $this->_send_request( 'coupons/'.$coupon_id );
}
/**
* Delete a coupon from the system
*
* @param string The identifier of the coupon you want to delete
*/
public function coupon_delete( $coupon_id ) {
return $this->_send_request( 'coupons/'.$coupon_id, array(), STRIPE_METHOD_DELETE );
}
/**
* Retrieve a list of the coupons in the system
*/
public function coupon_list( $count = 10, $offset = 0 ) {
$params['count'] = $count;
$params['offset'] = $offset;
$vars = http_build_query( $params, NULL, '&' );
return $this->_send_request( 'coupons?'.$vars );
}
/**
* Register a new recipient on system
*
* @param string The recipient's full, legal name.
* For type individual, should be in the format "First Last", "First Middle Last", or "First M Last" (no prefixes or suffixes).
* For corporation, the full incorporated name
* @param string Type of the recipient: either individual or corporation.
* @param string The recipient's tax ID, as a string. For type individual, the full SSN; for type corporation, the full EIN.
* @param mixed This can be a bank token generated with stripe.js ( recommended ).
* @param string The recipient's email address.
* @param string An arbitrary string which you can attach to a recipient object. It is displayed alongside the recipient in the web interface.
*/
public function recipient_create( $name, $type, $tax_id = NULL, $bank_account = NULL, $email = NULL, $description = NULL ) {
$params = array(
'name' => $name,
'type' => $type
);
if( $tax_id )
$params['tax_id'] = $tax_id;
if( $bank_account )
$params['bank_account'] = $bank_account;
if( $email )
$params['email'] = $email;
if( $description )
$params['description'] = $description;
return $this->_send_request( 'recipients', $params, STRIPE_METHOD_POST );
}
/**
* Retrieve information for a given recipient
*
* @param string The recipient ID to get information about
*/
public function recipient_info( $recipient_id ) {
return $this->_send_request( 'recipients/'.$recipient_id );
}
/**
* Update an existing recipient record
*
* @param string The recipient ID for the record to update
* @param array An array containing the new data for the recipient
*
*/
public function recipient_update( $recipient_id, $newdata ) {
return $this->_send_request( 'recipients/'.$recipient_id, $newdata, STRIPE_METHOD_POST );
}
/**
* Creating a new transfer (sending funds to a third-party bank account)
*
* @param int A positive int, in cents, representing how much money to transfer.
* @param string 3-letter ISO code for currency. For dollars use USD.
* @param string A verified recipient's ID. This should be created using recipient_create above.
* If transferring to the bank account associated with your account use 'self'
* @param string An arbitrary string which you can attach to a transfer object. It is displayed when in the web interface alongside the transfer.
* @param string An arbitrary string which will be displayed on the recipient's bank statement. This should not include your company name, as that will already be part of the descriptor. The maximum length of this string is 15 characters; longer strings will be truncated.
*/
public function transfer_create( $amount, $currency, $recipient, $description = null, $statement_descriptor = null ) {
$params = array(
'amount' => $amount,
'currency' => $currency,
'recipient' => $recipient
);
if( $description )
$params['description'] = $description;
if( $statement_descriptor )
$params['statement_descriptor'] = $statement_descriptor;
return $this->_send_request( 'transfers', $params, STRIPE_METHOD_POST );
}
/**
* Retrieve the detailas of an existing transfer
*
* @param string The transfer ID to get information about
*/
public function transfer_retrieve( $transfer_id ) {
return $this->_send_request( 'transfers/'.$transfer_id );
}
/**
* Canceling a Transfer
*
* @param string The transfer ID you wish to cancel
*/
public function transfer_cancel( $transfer_id ) {
return $this->_send_request( 'transfers/'.$transfer_id, array(), STRIPE_METHOD_DELETE );
}
/**
* List all transfers
*
* @param int A limit on the number of objects to be returned.
* @param string/dict A filter on the list based on the events created date. The value can be a string with an exact UTC timestamp,
* or it can be a dictionary with the following options:
* gt (optional)
* Return values should have been created after this timestamp.
* gte (optional)
* Return values should have been created after or equal to this timestamp.
* lt (optional)
* Return values should have been created before this timestamp.
* lte (optional)
* Return values should have been created before or equal to this timestamp.
* @param int An offset into the list of returned items. The API will return the requested number of items starting at that offset.
* @param string Only return transfers for the recipient specified by this recipient ID
* @param string Only return transfers that have the given status: pending, paid, or failed.
*/
public function transfer_all( $count = 10, $date = null, $offset = 0, $recipient = null, $status = null ) {
if( $count )
$params['count'] = $count;
if( $date )
$params['date'] = $date;
if( $offset )
$params['offset'] = $offset;
if( $recipient )
$params['recipient'] = $recipient;
if( $status )
$params['status'] = $status;
$vars = http_build_query( $params, NULL, '&' );
return $this->_send_request( 'transfers?'.$vars );
}
/**
* Private utility function that prepare and send the request to the API servers
*
* @param string The URL segments to use to complete the http request
* @param array The parameters for the request, if any
* @param srting Either 'post','get' or 'delete' to determine the request method, 'get' is default
*/
private function _send_request( $url_segs, $params = array(), $http_method = 'get' ) {
if( $this->_conf['stripe_test_mode'] )
$key = $this->_conf['stripe_key_test_secret'];
else
$key = $this->_conf['stripe_key_live_secret'];
// Initializ and configure the request
$req = curl_init( 'https://api.stripe.com/v1/'.$url_segs );
curl_setopt( $req, CURLOPT_SSL_VERIFYPEER, $this->_conf['stripe_verify_ssl'] );
curl_setopt( $req, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
curl_setopt( $req, CURLOPT_USERPWD, $key.':' );
curl_setopt( $req, CURLOPT_RETURNTRANSFER, TRUE );
// Are we using POST? Adjust the request properly
if( $http_method == STRIPE_METHOD_POST ) {
curl_setopt( $req, CURLOPT_POST, TRUE );
curl_setopt( $req, CURLOPT_POSTFIELDS, http_build_query( $params, NULL, '&' ) );
}
if( $http_method == STRIPE_METHOD_DELETE ) {
curl_setopt( $req, CURLOPT_CUSTOMREQUEST, "DELETE" );
curl_setopt( $req, CURLOPT_POSTFIELDS, http_build_query( $params, NULL, '&' ) );
}
// Get the response, clean the request and return the data
$response = curl_exec( $req );
curl_close( $req );
// Decode JSON if the user has enabled that option
if( isset($this->_conf['stripe_decode']) && $this->_conf['stripe_decode'] )
return json_decode( $response );
else
return $response;
}
}
// END Stripe Class
/* End of file Stripe.php */
/* Location: ./{APPLICATION}/libraries/Stripe.php */