PageRenderTime 34ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/StripeComponent.php

https://github.com/hashmode/CakePHP-Stripe-Component-Full
PHP | 2674 lines | 1293 code | 556 blank | 825 comment | 144 complexity | c2260cb654658762fa785abf089f21e2 MD5 | raw file
  1. <?php
  2. /**
  3. * Stripe Component
  4. *
  5. * PHP 5
  6. *
  7. * Licensed under The MIT License
  8. * associative
  9. * @version 1.0
  10. * @author http://hashmode.com
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. * @link https://github.com/hashmode/CakePHP-Stripe-Component-Full
  13. *
  14. * Compatible with Stripe API version 1.15.0
  15. *
  16. * ***** IMPORTANT ******
  17. * Stripe PHP library is not included, it should be downloaded from Stripe's website
  18. * @link https://stripe.com/docs/libraries
  19. *
  20. * Stripe php documentation
  21. * @link https://stripe.com/docs/api/php
  22. */
  23. App::uses('Component', 'Controller');
  24. /**
  25. * Stripe Components allows to use Stripe Payments
  26. *
  27. */
  28. class StripeComponent extends Component {
  29. /**
  30. * Stripe mode, can be 'Live' or 'Test'
  31. *
  32. * @var string
  33. */
  34. public $mode = 'Test';
  35. /**
  36. * Stripe API Secret key
  37. *
  38. * @var string
  39. */
  40. public $key = null;
  41. /**
  42. * Stripe currency, default is 'usd'
  43. *
  44. * @var string
  45. */
  46. public $currency = 'usd';
  47. /**
  48. *
  49. * If provided, statuses will be saved in that file, default is false
  50. * if enabled, log should be added in bootstrap, e.g. if log file should be in tmp/logs/stripe.log
  51. *
  52. CakeLog::config('stripe', array(
  53. 'engine' => 'FileLog',
  54. 'file' => 'stripe',
  55. ));
  56. *
  57. * @var string
  58. */
  59. public $logFile = false;
  60. /**
  61. * Can be 'both', 'success' or 'error', to what results to save, default is 'error'
  62. *
  63. * @var string
  64. */
  65. public $logType = 'error';
  66. /**
  67. * For saving the reflection class, to use in the loop
  68. *
  69. * @var array
  70. */
  71. protected $reflectionClass = array();
  72. /**
  73. * Initialize component
  74. *
  75. * @param Controller $controller Instantiating controller
  76. * @return void
  77. */
  78. public function initialize(Controller $controller) {
  79. $this->Controller = $controller;
  80. App::import ( 'Vendor', 'stripe',
  81. array(
  82. 'file' => 'stripe'.DS.'lib'.DS.'Stripe.php'
  83. )
  84. );
  85. if (!class_exists('Stripe')) {
  86. throw new CakeException('Stripe PHP Library not found. Be sure it is unpacked in app/Vendor/stripe directory.
  87. It can be downloaded from https://stripe.com/docs/libraries');
  88. }
  89. // if mode is not set in bootstrap, defaults to 'Test'
  90. $mode = Configure::read('Stripe.mode');
  91. if ($mode) {
  92. $this->mode = $mode;
  93. }
  94. // set the Stripe API key
  95. $this->key = Configure::read('Stripe.' . $this->mode . 'Secret');
  96. if (!$this->key) {
  97. throw new CakeException('Stripe API Secret key is not set');
  98. }
  99. // if currency is not set, defaults to 'usd'
  100. $currency = Configure::read('Stripe.currency');
  101. if ($currency) {
  102. $this->currency = strtolower($currency);
  103. }
  104. // set logging
  105. if (isset($this->settings['logFile'])) {
  106. $this->logFile = $this->settings['logFile'];
  107. // validate logFile
  108. $logFiles = CakeLog::configured();
  109. if (array_search($this->logFile, $logFiles) === false) {
  110. throw new CakeException(__('Logging file is not added. Add this in your bootstrap').': '
  111. .'CakeLog::config("'.$this->logFile.'", array("engine" => "FileLog", "file" => "'.$this->logFile.'" ));' );
  112. }
  113. if (isset($this->settings['logType'])) {
  114. $this->logType = $this->settings['logType'];
  115. // validate logType
  116. if (!in_array($this->logType, array('both', 'success', 'error'))) {
  117. throw new CakeException(__('Log Type can be only set as "success", "error" or "both" '));
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * charge method
  124. * Charges the given credit card(card_id, array or token) or customer
  125. *
  126. * @param array $data
  127. * @param string $customerId[optional]
  128. * @return array
  129. *
  130. * @link https://stripe.com/docs/api#create_charge
  131. */
  132. public function charge($data = null, $customerId = null) {
  133. if (!$customerId && (!isset($data['card']) || empty($data['card'])) ) {
  134. throw new CakeException(__('Customer Id or Card is required'));
  135. }
  136. if ($customerId) {
  137. $data['customer'] = $customerId;
  138. }
  139. // set default currency
  140. if (!isset($data['currency'])) {
  141. $data['currency'] = $this->currency;
  142. }
  143. return $this->request(__FUNCTION__, $data);
  144. }
  145. /**
  146. * retrieveCharge method
  147. * Retrieves the details of a charge that has previously been created
  148. *
  149. * @param string $chargeId
  150. * @return array
  151. *
  152. * @link https://stripe.com/docs/api#retrieve_charge
  153. */
  154. public function retrieveCharge($chargeId = null) {
  155. if (!$chargeId) {
  156. throw new CakeException(__('Charge Id is required'));
  157. }
  158. $data = array(
  159. 'charge_id' => $chargeId
  160. );
  161. return $this->request(__FUNCTION__, $data);
  162. }
  163. /**
  164. * updateCharge method
  165. * Updates the specified charge
  166. *
  167. * @param string $chargeId
  168. * @param array $data
  169. * @return array
  170. *
  171. * @link https://stripe.com/docs/api#update_charge
  172. */
  173. public function updateCharge($chargeId = null, $data = array()) {
  174. if (!$chargeId) {
  175. throw new CakeException(__('Charge Id is not provided'));
  176. }
  177. if (empty($data)) {
  178. throw new CakeException(__('No data is provided to updates the card'));
  179. }
  180. $data = array(
  181. 'charge_id' => $chargeId,
  182. 'fields' => $data,
  183. );
  184. return $this->request(__FUNCTION__, $data);
  185. }
  186. /**
  187. * refundCharge method
  188. * Refunds a charge that has previously been created but not yet refunded
  189. *
  190. * @param string $chargeId
  191. * @param array $data
  192. * @return array
  193. *
  194. * @link https://stripe.com/docs/api#refund_charge
  195. */
  196. public function refundCharge($chargeId = null, $data = array()) {
  197. if (!$chargeId) {
  198. throw new CakeException(__('Charge Id is not provided'));
  199. }
  200. $data['charge_id'] = $chargeId;
  201. return $this->request(__FUNCTION__, $data);
  202. }
  203. /**
  204. * captureCharge method
  205. * Capture the payment of an existing, uncaptured, charge.
  206. *
  207. * @param string $chargeId
  208. * @param array $data
  209. * @return array
  210. *
  211. * @link https://stripe.com/docs/api#charge_capture
  212. */
  213. public function captureCharge($chargeId = null, $data = array()) {
  214. if (!$chargeId) {
  215. throw new CakeException(__('Charge Id is not provided'));
  216. }
  217. $data['charge_id'] = $chargeId;
  218. return $this->request(__FUNCTION__, $data);
  219. }
  220. /**
  221. * listCharges method
  222. * Returns a list of charges you've previously created
  223. *
  224. * @param array $data
  225. * @return array
  226. *
  227. * @link https://stripe.com/docs/api#list_charges
  228. */
  229. public function listCharges($data = array()) {
  230. $data = array(
  231. 'options' => $data
  232. );
  233. return $this->request(__FUNCTION__, $data);
  234. }
  235. /**
  236. * createCustomer method
  237. * Creates a new customer
  238. *
  239. * @param array $data - according to customer object
  240. * @return array
  241. *
  242. * @link https://stripe.com/docs/api/php#create_customers
  243. */
  244. public function createCustomer($data) {
  245. if (empty($data) || !is_array($data)) {
  246. throw new CakeException(__('Data is empty or is not an array'));
  247. }
  248. return $this->request(__FUNCTION__, $data);
  249. }
  250. /**
  251. * retrieveCustomer method
  252. * Retrives the customer information
  253. *
  254. * @param string $customerId
  255. * @return array
  256. *
  257. * @link https://stripe.com/docs/api/php#retrieve_customer
  258. */
  259. public function retrieveCustomer($customerId = null) {
  260. if (!$customerId) {
  261. throw new CakeException(__('Customer Id is not provided'));
  262. }
  263. $data = array(
  264. 'customer_id' => $customerId
  265. );
  266. return $this->request(__FUNCTION__, $data);
  267. }
  268. /**
  269. * updateCustomer method
  270. * Updates the customer info
  271. *
  272. * @param string $customerId
  273. * @param array $fields - fields to be updated
  274. * @return array
  275. *
  276. * @link https://stripe.com/docs/api/php#update_customer
  277. */
  278. public function updateCustomer($customerId = null, $fields = array()) {
  279. if (!$customerId) {
  280. throw new CakeException(__('Customer Id is not provided'));
  281. }
  282. if (empty($fields)) {
  283. throw new CakeException(__('Update fields are empty'));
  284. }
  285. $data = array(
  286. 'customer_id' => $customerId,
  287. 'fields' => $fields
  288. );
  289. return $this->request(__FUNCTION__, $data);
  290. }
  291. /**
  292. * deleteCustomer method
  293. * Deletes the customer
  294. *
  295. * @param string $customerId
  296. * @return array
  297. *
  298. * @link https://stripe.com/docs/api/php#delete_customer
  299. */
  300. public function deleteCustomer($customerId = null) {
  301. if (!$customerId) {
  302. throw new CakeException(__('Customer Id is not provided'));
  303. }
  304. $data = array(
  305. 'customer_id' => $customerId
  306. );
  307. return $this->request(__FUNCTION__, $data);
  308. }
  309. /**
  310. * listCustomers method
  311. * Returns array with customers
  312. * *
  313. * @param array $data
  314. * @param array $cards - default is false, if true each customers cards will be returned as array
  315. * @param array $subscriptions - default is false, if true each customers subscriptions will be returned as array
  316. * @return array
  317. *
  318. * @link https://stripe.com/docs/api/php#list_customers
  319. */
  320. public function listCustomers($data = array()) {
  321. $data = array(
  322. 'options' => $data
  323. );
  324. return $this->request(__FUNCTION__, $data);
  325. }
  326. /**
  327. * createCard method
  328. * Creates a new card for the given customer
  329. *
  330. * @param string $customerId
  331. * @param mixed $data - card data, token or array
  332. * @return array
  333. *
  334. * @link https://stripe.com/docs/api/php#create_card
  335. */
  336. public function createCard($customerId = null, $card = null) {
  337. if (!$customerId) {
  338. throw new CakeException(__('Customer Id is not provided'));
  339. }
  340. if (!$card) {
  341. throw new CakeException(__('Card data is not provided'));
  342. }
  343. $data = array(
  344. 'customer_id' => $customerId,
  345. 'card' => $card
  346. );
  347. return $this->request(__FUNCTION__, $data);
  348. }
  349. /**
  350. * retrieveCard method
  351. * Retrives an existing card for the given customer
  352. *
  353. * @param string $customerId
  354. * @param string $cardId
  355. * @return array
  356. *
  357. * @link https://stripe.com/docs/api/php#retrieve_card
  358. */
  359. public function retrieveCard($customerId = null, $cardId = null) {
  360. if (!$customerId) {
  361. throw new CakeException(__('Customer Id is not provided'));
  362. }
  363. if (!$cardId) {
  364. throw new CakeException(__('Card Id is not provided'));
  365. }
  366. $data = array(
  367. 'customer_id' => $customerId,
  368. 'card_id' => $cardId
  369. );
  370. return $this->request(__FUNCTION__, $data);
  371. }
  372. /**
  373. * updateCard method
  374. * Updates an existing card for the given customer
  375. *
  376. * @param string $customerId
  377. * @param string $cardId
  378. * @param array $data
  379. * @return array
  380. *
  381. * @link https://stripe.com/docs/api/php#update_card
  382. */
  383. public function updateCard($customerId = null, $cardId = null, $data = array()) {
  384. if (!$customerId) {
  385. throw new CakeException(__('Customer Id is not provided'));
  386. }
  387. if (!$cardId) {
  388. throw new CakeException(__('Card Id is not provided'));
  389. }
  390. if (empty($data)) {
  391. throw new CakeException(__('No data is provided to updates the card'));
  392. }
  393. $data = array(
  394. 'customer_id' => $customerId,
  395. 'card_id' => $cardId,
  396. 'fields' => $data,
  397. );
  398. return $this->request(__FUNCTION__, $data);
  399. }
  400. /**
  401. * deleteCard method
  402. * Deletes an existing card for the given customer
  403. *
  404. * @param string $customerId
  405. * @param string $cardId
  406. * @return array
  407. *
  408. * @link https://stripe.com/docs/api/php#delete_card
  409. */
  410. public function deleteCard($customerId = null, $cardId = null) {
  411. if (!$customerId) {
  412. throw new CakeException(__('Customer Id is not provided'));
  413. }
  414. if (!$cardId) {
  415. throw new CakeException(__('Card Id is not provided'));
  416. }
  417. $data = array(
  418. 'customer_id' => $customerId,
  419. 'card_id' => $cardId
  420. );
  421. return $this->request(__FUNCTION__, $data);
  422. }
  423. /**
  424. * listCards method
  425. * Returs cards for the given customer
  426. *
  427. * @param string $customerId
  428. * @param string $cardId
  429. * @return array
  430. *
  431. * @link https://stripe.com/docs/api/php#list_cards
  432. */
  433. public function listCards($customerId = null, $data = array()) {
  434. if (!$customerId) {
  435. throw new CakeException(__('Customer Id is not provided'));
  436. }
  437. $data = array(
  438. 'customer_id' => $customerId,
  439. 'options' => $data
  440. );
  441. return $this->request(__FUNCTION__, $data);
  442. }
  443. /**
  444. * createSubscription method
  445. * Creates a new subscription for the given customer
  446. *
  447. * @param string $customerId
  448. * @param array $data - subscription data, token or array
  449. * @return array
  450. *
  451. * @link https://stripe.com/docs/api/php#create_subscription
  452. */
  453. public function createSubscription($customerId = null, $subscription = null) {
  454. if (!$customerId) {
  455. throw new CakeException(__('Customer Id is not provided'));
  456. }
  457. if (!$subscription) {
  458. throw new CakeException(__('Subscription data is not provided'));
  459. }
  460. $data = array(
  461. 'customer_id' => $customerId,
  462. 'subscription' => $subscription
  463. );
  464. return $this->request(__FUNCTION__, $data);
  465. }
  466. /**
  467. * retrieveSubscription method
  468. * Retrives an existing subscription for the given customer
  469. *
  470. * @param string $customerId
  471. * @param string $subscriptionId
  472. * @return array
  473. *
  474. * @link https://stripe.com/docs/api/php#retrieve_subscription
  475. */
  476. public function retrieveSubscription($customerId = null, $subscriptionId = null) {
  477. if (!$customerId) {
  478. throw new CakeException(__('Customer Id is not provided'));
  479. }
  480. if (!$subscriptionId) {
  481. throw new CakeException(__('Subscription Id is not provided'));
  482. }
  483. $data = array(
  484. 'customer_id' => $customerId,
  485. 'subscription_id' => $subscriptionId
  486. );
  487. return $this->request(__FUNCTION__, $data);
  488. }
  489. /**
  490. * updateSubscription method
  491. * Updates an existing subscription for the given customer
  492. *
  493. * @param string $customerId
  494. * @param string $subscriptionId
  495. * @param array $data
  496. * @return array
  497. *
  498. * @link https://stripe.com/docs/api/php#update_subscription
  499. */
  500. public function updateSubscription($customerId = null, $subscriptionId = null, $data = array()) {
  501. if (!$customerId) {
  502. throw new CakeException(__('Customer Id is not provided'));
  503. }
  504. if (!$subscriptionId) {
  505. throw new CakeException(__('Subscription Id is not provided'));
  506. }
  507. if (empty($data)) {
  508. throw new CakeException(__('No data is provided to update the subscription'));
  509. }
  510. $data = array(
  511. 'customer_id' => $customerId,
  512. 'subscription_id' => $subscriptionId,
  513. 'fields' => $data,
  514. );
  515. return $this->request(__FUNCTION__, $data);
  516. }
  517. /**
  518. * cancelSubscription method
  519. * Cancels an existing subscription for the given customer
  520. *
  521. * @param string $customerId
  522. * @param string $subscriptionId
  523. * @return array
  524. *
  525. * @link https://stripe.com/docs/api/php#cancel_subscription
  526. */
  527. public function cancelSubscription($customerId = null, $subscriptionId = null) {
  528. if (!$customerId) {
  529. throw new CakeException(__('Customer Id is not provided'));
  530. }
  531. if (!$subscriptionId) {
  532. throw new CakeException(__('Subscription Id is not provided'));
  533. }
  534. $data = array(
  535. 'customer_id' => $customerId,
  536. 'subscription_id' => $subscriptionId
  537. );
  538. return $this->request(__FUNCTION__, $data);
  539. }
  540. /**
  541. * listSubscriptions method
  542. * Returs subscriptions for the given customer
  543. *
  544. * @param string $customerId
  545. * @param string $subscriptionId
  546. * @return array
  547. *
  548. * @link https://stripe.com/docs/api/php#list_subscriptions
  549. */
  550. public function listSubscriptions($customerId = null, $data = array()) {
  551. if (!$customerId) {
  552. throw new CakeException(__('Customer Id is not provided'));
  553. }
  554. $data = array(
  555. 'customer_id' => $customerId,
  556. 'options' => $data
  557. );
  558. return $this->request(__FUNCTION__, $data);
  559. }
  560. /**
  561. * createPlan method
  562. * Creates a new subscription plan
  563. *
  564. * @param array $data
  565. * @return array
  566. *
  567. * @link https://stripe.com/docs/api/php#create_plan
  568. */
  569. public function createPlan($data = array()) {
  570. if (empty($data) || !is_array($data)) {
  571. throw new CakeException(__('Data is empty or is not an array'));
  572. }
  573. // set default currency
  574. if (!isset($data['currency'])) {
  575. $data['currency'] = $this->currency;
  576. }
  577. return $this->request(__FUNCTION__, $data);
  578. }
  579. /**
  580. * retrievePlan method
  581. * Retrieves the existing subscription plan
  582. *
  583. * @param string $planId
  584. * @return array
  585. *
  586. * @link https://stripe.com/docs/api/php#retrieve_plan
  587. */
  588. public function retrievePlan($planId = null) {
  589. if (!$planId) {
  590. throw new CakeException(__('Plan Id is required'));
  591. }
  592. $data = array(
  593. 'plan_id' => $planId
  594. );
  595. return $this->request(__FUNCTION__, $data);
  596. }
  597. /**
  598. * updatePlan method
  599. * Updates the existing subscription plan
  600. *
  601. * @param string $planId
  602. * @return array
  603. *
  604. * @link https://stripe.com/docs/api/php#update_plan
  605. */
  606. public function updatePlan($planId = null, $data = array()) {
  607. if (!$planId) {
  608. throw new CakeException(__('Plan Id is required'));
  609. }
  610. if (empty($data)) {
  611. throw new CakeException(__('No data is provided to updates the plan'));
  612. }
  613. $data = array(
  614. 'plan_id' => $planId,
  615. 'fields' => $data
  616. );
  617. return $this->request(__FUNCTION__, $data);
  618. }
  619. /**
  620. * deletePlan method
  621. * Deletes the existing subscription plan
  622. *
  623. * @param string $planId
  624. * @return array
  625. *
  626. * @link https://stripe.com/docs/api/php#delete_plan
  627. */
  628. public function deletePlan($planId = null) {
  629. if (!$planId) {
  630. throw new CakeException(__('Plan Id is required'));
  631. }
  632. $data = array(
  633. 'plan_id' => $planId,
  634. );
  635. return $this->request(__FUNCTION__, $data);
  636. }
  637. /**
  638. * listPlans method
  639. * Returns all the plans
  640. *
  641. * @param array $data
  642. * @return array
  643. *
  644. * @link https://stripe.com/docs/api/php#list_plans
  645. */
  646. public function listPlans($data = array()) {
  647. $data = array(
  648. 'options' => $data,
  649. );
  650. return $this->request(__FUNCTION__, $data);
  651. }
  652. /**
  653. * createCoupon method
  654. * Creates a new coupon
  655. *
  656. * @param array $data
  657. * @return array
  658. *
  659. * @link https://stripe.com/docs/api/php#create_coupon
  660. */
  661. public function createCoupon($data = array()) {
  662. if (empty($data) || !is_array($data)) {
  663. throw new CakeException(__('Data is empty or is not an array'));
  664. }
  665. // set default currency
  666. if (!isset($data['currency'])) {
  667. $data['currency'] = $this->currency;
  668. }
  669. return $this->request(__FUNCTION__, $data);
  670. }
  671. /**
  672. * retrieveCoupon method
  673. * Retrieves the existing coupon
  674. *
  675. * @param string $couponId
  676. * @return array
  677. *
  678. * @link https://stripe.com/docs/api/php#retrieve_coupon
  679. */
  680. public function retrieveCoupon($couponId = null) {
  681. if (!$couponId) {
  682. throw new CakeException(__('Coupon Id is required'));
  683. }
  684. $data = array(
  685. 'coupon_id' => $couponId
  686. );
  687. return $this->request(__FUNCTION__, $data);
  688. }
  689. /**
  690. * deleteCoupon method
  691. * Deletes the existing coupon
  692. *
  693. * @param string $couponId
  694. * @return array
  695. *
  696. * @link https://stripe.com/docs/api/php#delete_coupon
  697. */
  698. public function deleteCoupon($couponId = null) {
  699. if (!$couponId) {
  700. throw new CakeException(__('Coupon Id is required'));
  701. }
  702. $data = array(
  703. 'coupon_id' => $couponId,
  704. );
  705. return $this->request(__FUNCTION__, $data);
  706. }
  707. /**
  708. * listCoupons method
  709. * Returns all the plans
  710. *
  711. * @param array $data
  712. * @return array
  713. *
  714. * @link https://stripe.com/docs/api/php#list_coupons
  715. */
  716. public function listCoupons($data = array()) {
  717. $data = array(
  718. 'options' => $data,
  719. );
  720. return $this->request(__FUNCTION__, $data);
  721. }
  722. /**
  723. * deleteCustomerDiscount method
  724. * Removes the currently applied discount on a customer
  725. *
  726. * @param string $customerId
  727. * @return array
  728. *
  729. * @link https://stripe.com/docs/api/php#delete_discount
  730. */
  731. public function deleteCustomerDiscount($customerId = null) {
  732. if (!$customerId) {
  733. throw new CakeException(__('Customer Id is not provided'));
  734. }
  735. $data = array(
  736. 'customer_id' => $customerId
  737. );
  738. return $this->request(__FUNCTION__, $data);
  739. }
  740. /**
  741. * deleteSubscriptionDiscount method
  742. * Removes the currently applied discount on a subscription.
  743. *
  744. * @param string $customerId
  745. * @param string $subscriptionId
  746. * @return array
  747. *
  748. * @link https://stripe.com/docs/api#delete_subscription_discount
  749. */
  750. public function deleteSubscriptionDiscount($customerId = null, $subscriptionId = null) {
  751. if (!$customerId) {
  752. throw new CakeException(__('Customer Id is not provided'));
  753. }
  754. if (!$subscriptionId) {
  755. throw new CakeException(__('Subscription Id is not provided'));
  756. }
  757. $data = array(
  758. 'customer_id' => $customerId,
  759. 'subscription_id' => $subscriptionId
  760. );
  761. return $this->request(__FUNCTION__, $data);
  762. }
  763. /**
  764. * retrieveInvoice method
  765. * Retrives an existing invoice
  766. *
  767. * @param string $invoiceId
  768. * @return array
  769. *
  770. * @link https://stripe.com/docs/api/php#retrieve_invoice
  771. */
  772. public function retrieveInvoice($invoiceId = null) {
  773. if (!$invoiceId) {
  774. throw new CakeException(__('Invoice Id is not provided'));
  775. }
  776. $data = array(
  777. 'invoice_id' => $invoiceId
  778. );
  779. return $this->request(__FUNCTION__, $data);
  780. }
  781. /**
  782. * retrieveInvoiceLine method
  783. * Retrives an existing invoice's line
  784. *
  785. * @param string $invoiceId
  786. * @param array $data
  787. * @return array
  788. *
  789. * @link https://stripe.com/docs/api#invoice_lines
  790. */
  791. public function retrieveInvoiceLine($invoiceId = null, $data = array()) {
  792. if (!$invoiceId) {
  793. throw new CakeException(__('Invoice Id is not provided'));
  794. }
  795. $data = array(
  796. 'invoice_id' => $invoiceId,
  797. 'options' => $data
  798. );
  799. return $this->request(__FUNCTION__, $data);
  800. }
  801. /**
  802. * createInvoice method
  803. * Creates a new invoice
  804. *
  805. * @param string $customerId
  806. * @param array $data
  807. * @return array
  808. *
  809. * @link https://stripe.com/docs/api#create_invoice
  810. */
  811. public function createInvoice($customerId = null, $data = array()) {
  812. if (!$customerId) {
  813. throw new CakeException(__('Customer Id is not provided'));
  814. }
  815. $data['customer'] = $customerId;
  816. return $this->request(__FUNCTION__, $data);
  817. }
  818. /**
  819. * payInvoice method
  820. * Pay invoice
  821. *
  822. * @param string $invoiceId
  823. * @return array
  824. *
  825. * @link https://stripe.com/docs/api#pay_invoice
  826. */
  827. public function payInvoice($invoiceId = null) {
  828. if (!$invoiceId) {
  829. throw new CakeException(__('Invoice Id is not provided'));
  830. }
  831. $data['invoice_id'] = $invoiceId;
  832. return $this->request(__FUNCTION__, $data);
  833. }
  834. /**
  835. * updateInvoice method
  836. * Update Invoice
  837. *
  838. * @param string $invoiceId
  839. * @return array
  840. *
  841. * @link https://stripe.com/docs/api#update_invoice
  842. */
  843. public function updateInvoice($invoiceId = null, $data = array()) {
  844. if (!$invoiceId) {
  845. throw new CakeException(__('Invoice Id is not provided'));
  846. }
  847. if (empty($data)) {
  848. throw new CakeException(__('Data is empty for updating the invoice'));
  849. }
  850. $data = array(
  851. 'invoice_id' => $invoiceId,
  852. 'fields' => $data
  853. );
  854. return $this->request(__FUNCTION__, $data);
  855. }
  856. /**
  857. * listInvoices method
  858. * List all invoices, if customer id is provided, only corresponding invoices will be returned
  859. *
  860. * @param string $customerId - not required
  861. * @param array $data
  862. * @return array
  863. *
  864. * @link https://stripe.com/docs/api#list_customer_invoices
  865. */
  866. public function listInvoices($customerId = null, $data = array()) {
  867. $data['customer'] = $customerId;
  868. return $this->request(__FUNCTION__, $data);
  869. }
  870. /**
  871. * retrieveUpcomingInvoice method
  872. *
  873. * @param string $customerId - not required
  874. * @param string $subscriptionId
  875. * @return array
  876. *
  877. * @link https://stripe.com/docs/api#retrieve_customer_invoice
  878. */
  879. public function retrieveUpcomingInvoice($customerId = null, $subscriptionId = null) {
  880. if (!$customerId) {
  881. throw new CakeException(__('Customer Id is not provided'));
  882. }
  883. $data = array(
  884. 'customer' => $customerId,
  885. 'subscription' => $subscriptionId
  886. );
  887. return $this->request(__FUNCTION__, $data);
  888. }
  889. /**
  890. * createInvoiceItem method
  891. * Adds an arbitrary charge or credit to the customer's upcoming invoice.
  892. *
  893. * @param string $customerId
  894. * @param array $data
  895. * @return array
  896. *
  897. * @link https://stripe.com/docs/api#create_invoiceitem
  898. */
  899. public function createInvoiceItem($customerId = null, $data = array()) {
  900. if (!$customerId) {
  901. throw new CakeException(__('Customer Id is not provided'));
  902. }
  903. $data['customer'] = $customerId;
  904. if (!isset($data['currency'])) {
  905. $data['currency'] = $this->currency;
  906. }
  907. return $this->request(__FUNCTION__, $data);
  908. }
  909. /**
  910. * retrieveInvoiceItem method
  911. * Retrieves the invoice item with the given ID.
  912. *
  913. * @param string $invoiceItemId
  914. * @return array
  915. *
  916. * @link https://stripe.com/docs/api#retrieve_invoiceitem
  917. */
  918. public function retrieveInvoiceItem($invoiceItemId = null) {
  919. if (!$invoiceItemId) {
  920. throw new CakeException(__('Invoice Item Id is not provided'));
  921. }
  922. $data = array(
  923. 'invoice_item_id' => $invoiceItemId
  924. );
  925. return $this->request(__FUNCTION__, $data);
  926. }
  927. /**
  928. * updateInvoiceItem method
  929. * Updates the amount or description of an invoice item on an upcoming invoice.
  930. * Updating an invoice item is only possible before the invoice it's attached to is closed.
  931. *
  932. * @param string $invoiceId
  933. * @return array
  934. *
  935. * @link https://stripe.com/docs/api#update_invoiceitem
  936. */
  937. public function updateInvoiceItem($invoiceItemId = null, $data = array()) {
  938. if (!$invoiceItemId) {
  939. throw new CakeException(__('Invoice Item Id is not provided'));
  940. }
  941. $data = array(
  942. 'invoice_item_id' => $invoiceItemId,
  943. 'fields' => $data
  944. );
  945. return $this->request(__FUNCTION__, $data);
  946. }
  947. /**
  948. * deleteInvoiceItem method
  949. * Removes an invoice item from the upcoming invoice. Removing an invoice item is only possible before the invoice it's attached to is closed.
  950. *
  951. * @param string $invoiceId
  952. * @return array
  953. *
  954. * @link https://stripe.com/docs/api#delete_invoiceitem
  955. */
  956. public function deleteInvoiceItem($invoiceItemId = null) {
  957. if (!$invoiceItemId) {
  958. throw new CakeException(__('Invoice Item Id is not provided'));
  959. }
  960. $data = array(
  961. 'invoice_item_id' => $invoiceItemId
  962. );
  963. return $this->request(__FUNCTION__, $data);
  964. }
  965. /**
  966. * listInvoiceItems method
  967. * Returns a list of your invoice items. Invoice Items are returned sorted by creation date, with the most recently created invoice items appearing first.
  968. *
  969. * @param array $data
  970. * @return array
  971. *
  972. * @link https://stripe.com/docs/api#list_invoiceitems
  973. */
  974. public function listInvoiceItems($data = array()) {
  975. $data = array(
  976. 'options' => $data
  977. );
  978. return $this->request(__FUNCTION__, $data);
  979. }
  980. /**
  981. * updateDispute method
  982. *
  983. * @param string $chargeId
  984. * @param array $data
  985. * @return array
  986. *
  987. * @link https://stripe.com/docs/api#update_dispute
  988. */
  989. public function updateDispute($chargeId = null, $data = array()) {
  990. if (!$chargeId) {
  991. throw new CakeException(__('Charge Id is not provided'));
  992. }
  993. if (empty($data)) {
  994. throw new CakeException(__('Data is empty for updating the dispute'));
  995. }
  996. $data = array(
  997. 'charge_id' => $chargeId,
  998. 'dispute' => $data
  999. );
  1000. return $this->request(__FUNCTION__, $data);
  1001. }
  1002. /**
  1003. * closeDispute method
  1004. * Closes the dispute, which changes the status from "under_review" to "lost"
  1005. *
  1006. * @param string $chargeId
  1007. * @return array
  1008. *
  1009. * @link https://stripe.com/docs/api#close_dispute
  1010. */
  1011. public function closeDispute($chargeId = null) {
  1012. if (!$chargeId) {
  1013. throw new CakeException(__('Charge Id is not provided'));
  1014. }
  1015. $data = array(
  1016. 'charge_id' => $chargeId,
  1017. );
  1018. return $this->request(__FUNCTION__, $data);
  1019. }
  1020. /**
  1021. * createTransfer method
  1022. * Used to send funds from your Stripe account to a third-party recipient or to your own bank account
  1023. *
  1024. * @param string $recipientId
  1025. * @param array $data
  1026. * @return array
  1027. *
  1028. * @link https://stripe.com/docs/api/php#create_transfer
  1029. */
  1030. public function createTransfer($recipientId = null, $data = array()) {
  1031. if (!$recipientId) {
  1032. throw new CakeException(__('Recipient Id is not provided'));
  1033. }
  1034. if (empty($data)) {
  1035. throw new CakeException(__('Transfer data is not provided'));
  1036. }
  1037. $data['recipient'] = $recipientId;
  1038. if (!isset($data['currency'])) {
  1039. $data['currency'] = $this->currency;
  1040. }
  1041. return $this->request(__FUNCTION__, $data);
  1042. }
  1043. /**
  1044. * retrieveTransfer method
  1045. * Retrieves the details of an existing transfer
  1046. *
  1047. * @param string $recipientId
  1048. * @param string $transferId
  1049. * @return array
  1050. *
  1051. * @link https://stripe.com/docs/api/php#retrieve_transfer
  1052. */
  1053. public function retrieveTransfer($transferId = null) {
  1054. if (!$transferId) {
  1055. throw new CakeException(__('Transfer Id is not provided'));
  1056. }
  1057. $data = array(
  1058. 'transfer_id' => $transferId
  1059. );
  1060. return $this->request(__FUNCTION__, $data);
  1061. }
  1062. /**
  1063. * updateTransfer method
  1064. * Updates an existing transfer
  1065. *
  1066. * @param string $recipientId
  1067. * @param string $transferId
  1068. * @param array $data
  1069. * @return array
  1070. *
  1071. * @link https://stripe.com/docs/api/php#update_transfer
  1072. */
  1073. public function updateTransfer($transferId = null, $data = array()) {
  1074. if (!$transferId) {
  1075. throw new CakeException(__('Transfer Id is not provided'));
  1076. }
  1077. if (empty($data)) {
  1078. throw new CakeException(__('No data is provided to update the transfer'));
  1079. }
  1080. $data = array(
  1081. 'transfer_id' => $transferId,
  1082. 'fields' => $data
  1083. );
  1084. return $this->request(__FUNCTION__, $data);
  1085. }
  1086. /**
  1087. * cancelTransfer method
  1088. * Cancels an existing transfer
  1089. *
  1090. * @param string $recipientId
  1091. * @param string $transferId
  1092. * @return array
  1093. *
  1094. * @link https://stripe.com/docs/api/php#cancel_transfer
  1095. */
  1096. public function cancelTransfer($transferId = null) {
  1097. if (!$transferId) {
  1098. throw new CakeException(__('Transfer Id is not provided'));
  1099. }
  1100. $data = array(
  1101. 'transfer_id' => $transferId
  1102. );
  1103. return $this->request(__FUNCTION__, $data);
  1104. }
  1105. /**
  1106. * listTransfers method
  1107. * Returs transfers
  1108. *
  1109. * @param array $data
  1110. * @return array
  1111. *
  1112. * @link https://stripe.com/docs/api/php#list_transfers
  1113. */
  1114. public function listTransfers($data = array()) {
  1115. $data = array(
  1116. 'options' => $data
  1117. );
  1118. return $this->request(__FUNCTION__, $data);
  1119. }
  1120. /**
  1121. * createCardToken method
  1122. * Creates a single use token that wraps the details of a credit card.
  1123. *
  1124. * @param array $data - card data
  1125. * @param array $customerId [optional]
  1126. * @return array
  1127. *
  1128. * @link https://stripe.com/docs/api#create_card_token
  1129. */
  1130. public function createCardToken($card = null, $customerId = null) {
  1131. if (!$card) {
  1132. throw new CakeException(__('Card data is not provided'));
  1133. }
  1134. $data = array(
  1135. 'customer' => $customerId,
  1136. 'card' => $card
  1137. );
  1138. return $this->request(__FUNCTION__, $data);
  1139. }
  1140. /**
  1141. * createBankAccountToken method
  1142. * Creates a single use token that wraps the details of a bank account.
  1143. *
  1144. * @param array $data - card data
  1145. * @return array
  1146. *
  1147. * @link https://stripe.com/docs/api#create_bank_account_token
  1148. */
  1149. public function createBankAccountToken($bankAccount = null) {
  1150. if (!$bankAccount) {
  1151. throw new CakeException(__('Bank Account is not provided'));
  1152. }
  1153. $data = array(
  1154. 'bank_account' => $bankAccount
  1155. );
  1156. return $this->request(__FUNCTION__, $data);
  1157. }
  1158. /**
  1159. * retrieveToken method
  1160. * Retrieves the token with the given ID.
  1161. *
  1162. * @param string $tokenId
  1163. * @return array
  1164. *
  1165. * @link https://stripe.com/docs/api#retrieve_token
  1166. */
  1167. public function retrieveToken($tokenId = null) {
  1168. if (!$tokenId) {
  1169. throw new CakeException(__('Token Id is not provided'));
  1170. }
  1171. $data = array(
  1172. 'token_id' => $tokenId
  1173. );
  1174. return $this->request(__FUNCTION__, $data);
  1175. }
  1176. /**
  1177. * createRecipient method
  1178. * Creates a new recipient
  1179. *
  1180. * @param array $data - according to recipient object
  1181. * @return array
  1182. *
  1183. * @link https://stripe.com/docs/api#create_recipient
  1184. */
  1185. public function createRecipient($data) {
  1186. if (empty($data) || !is_array($data)) {
  1187. throw new CakeException(__('Data is empty or is not an array'));
  1188. }
  1189. return $this->request(__FUNCTION__, $data);
  1190. }
  1191. /**
  1192. * retrieveRecipient method
  1193. * Retrives the recipient information
  1194. *
  1195. * @param string $recipientId
  1196. * @return array
  1197. *
  1198. * @link https://stripe.com/docs/api/php#retrieve_recipient
  1199. */
  1200. public function retrieveRecipient($recipientId = null) {
  1201. if (!$recipientId) {
  1202. throw new CakeException(__('Recipient Id is not provided'));
  1203. }
  1204. $data = array(
  1205. 'recipient_id' => $recipientId
  1206. );
  1207. return $this->request(__FUNCTION__, $data);
  1208. }
  1209. /**
  1210. * updateRecipient method
  1211. * Updates the recipient info
  1212. *
  1213. * @param string $recipientId
  1214. * @param array $fields - fields to be updated
  1215. * @return array
  1216. *
  1217. * @link https://stripe.com/docs/api/php#update_recipient
  1218. */
  1219. public function updateRecipient($recipientId = null, $fields = array()) {
  1220. if (!$recipientId) {
  1221. throw new CakeException(__('Recipient Id is not provided'));
  1222. }
  1223. if (empty($fields)) {
  1224. throw new CakeException(__('Update fields are empty'));
  1225. }
  1226. $data = array(
  1227. 'recipient_id' => $recipientId,
  1228. 'fields' => $fields
  1229. );
  1230. return $this->request(__FUNCTION__, $data);
  1231. }
  1232. /**
  1233. * deleteRecipient method
  1234. * Deletes the recipient
  1235. *
  1236. * @param string $recipientId
  1237. * @return array
  1238. *
  1239. * @link https://stripe.com/docs/api/php#delete_recipient
  1240. */
  1241. public function deleteRecipient($recipientId = null) {
  1242. if (!$recipientId) {
  1243. throw new CakeException(__('Recipient Id is not provided'));
  1244. }
  1245. $data = array(
  1246. 'recipient_id' => $recipientId
  1247. );
  1248. return $this->request(__FUNCTION__, $data);
  1249. }
  1250. /**
  1251. * listRecipients method
  1252. * Returns array with recipients
  1253. *
  1254. * @param array $data
  1255. * @return array
  1256. *
  1257. * @link https://stripe.com/docs/api/php#list_recipients
  1258. */
  1259. public function listRecipients($data = array()) {
  1260. $data = array(
  1261. 'options' => $data
  1262. );
  1263. return $this->request(__FUNCTION__, $data);
  1264. }
  1265. /**
  1266. * retrieveApplicationFee method
  1267. * Retrieves the details of an application fee that your account has collected
  1268. *
  1269. * @param string $applicationFeeId
  1270. * @return array
  1271. *
  1272. * @link https://stripe.com/docs/api#retrieve_application_fee
  1273. */
  1274. public function retrieveApplicationFee($applicationFeeId = null) {
  1275. if (!$applicationFeeId) {
  1276. throw new CakeException(__('Application Fee Id is not provided'));
  1277. }
  1278. $data = array(
  1279. 'application_fee_id' => $applicationFeeId
  1280. );
  1281. return $this->request(__FUNCTION__, $data);
  1282. }
  1283. /**
  1284. * refundApplicationFee method
  1285. * Returns a list of application fees you've previously collected
  1286. *
  1287. * @param string $applicationFeeId
  1288. * @param int $amount
  1289. * @return array
  1290. *
  1291. * @link https://stripe.com/docs/api#refund_application_fee
  1292. */
  1293. public function refundApplicationFee($applicationFeeId = null) {
  1294. if (!$applicationFeeId) {
  1295. throw new CakeException(__('Application Fee Id is not provided'));
  1296. }
  1297. $data['application_fee_id'] = $applicationFeeId;
  1298. return $this->request(__FUNCTION__, $data);
  1299. }
  1300. /**
  1301. * listApplicationFees method
  1302. * Returns a list of application fees you've previously collected
  1303. *
  1304. * @param array $data
  1305. * @return array
  1306. *
  1307. * @link https://stripe.com/docs/api#list_application_fees
  1308. */
  1309. public function listApplicationFees($data = array()) {
  1310. $data = array(
  1311. 'options' => $data
  1312. );
  1313. return $this->request(__FUNCTION__, $data);
  1314. }
  1315. /**
  1316. * retrieveAccount method
  1317. * Retrieves the details of the account, based on the API key that was used to make the request.
  1318. *
  1319. * @return array
  1320. *
  1321. * @link https://stripe.com/docs/api#retrieve_account
  1322. */
  1323. public function retrieveAccount() {
  1324. $data = array();
  1325. return $this->request(__FUNCTION__, $data);
  1326. }
  1327. /**
  1328. * retrieveBalance method
  1329. * Retrieves the current account balance, based on the API key that was used to make the request.
  1330. *
  1331. * @return array
  1332. *
  1333. * @link https://stripe.com/docs/api#retrieve_balance
  1334. */
  1335. public function retrieveBalance() {
  1336. $data = array();
  1337. return $this->request(__FUNCTION__, $data);
  1338. }
  1339. /**
  1340. * retrieveBalanceTransaction method
  1341. * Retrieves the balance transaction with the given ID.
  1342. *
  1343. * @param string $transactionId
  1344. * @return array
  1345. *
  1346. * @link https://stripe.com/docs/api#retrieve_balance_transaction
  1347. */
  1348. public function retrieveBalanceTransaction($transactionId = null) {
  1349. if (!$transactionId) {
  1350. throw new CakeException(__('Transaction Id is not provided'));
  1351. }
  1352. $data = array(
  1353. 'transaction_id' => $transactionId
  1354. );
  1355. return $this->request(__FUNCTION__, $data);
  1356. }
  1357. /**
  1358. * listBalanceHistory method
  1359. * Returns a list of transactions that have contributed to the Stripe account balance (includes charges, refunds, transfers, and so on).
  1360. *
  1361. * @param array $data
  1362. * @return array
  1363. *
  1364. * @link https://stripe.com/docs/api#balance_history
  1365. */
  1366. public function listBalanceHistory($data = array()) {
  1367. $data = array(
  1368. 'options' => $data
  1369. );
  1370. return $this->request(__FUNCTION__, $data);
  1371. }
  1372. /**
  1373. * retrievePlan method
  1374. * Retrieves the details of an event
  1375. *
  1376. * @param string $eventId
  1377. * @return array
  1378. *
  1379. * @link https://stripe.com/docs/api#retrieve_event
  1380. */
  1381. public function retrieveEvent($eventId = null) {
  1382. if (!$eventId) {
  1383. throw new CakeException(__('Event Id is required'));
  1384. }
  1385. $data = array(
  1386. 'event_id' => $eventId
  1387. );
  1388. return $this->request(__FUNCTION__, $data);
  1389. }
  1390. /**
  1391. * listEvents method
  1392. * List events, going back up to 30 days.
  1393. *
  1394. * @param array $data
  1395. * @return array
  1396. *
  1397. * @link https://stripe.com/docs/api#list_events
  1398. */
  1399. public function listEvents($data = array()) {
  1400. $data = array(
  1401. 'options' => $data,
  1402. );
  1403. return $this->request(__FUNCTION__, $data);
  1404. }
  1405. /**
  1406. * request method
  1407. *
  1408. * @param string $method
  1409. * @param array $data
  1410. *
  1411. * @return array - containing 'status', 'message' and 'response' keys
  1412. * if response was successful, keys will be 'success', 'Success' and the stripe response as associative array respectively,
  1413. * if request failed, keys will be 'error', the card error message if it was card_error, boolen false otherwise, and
  1414. * error data as an array respectively
  1415. */
  1416. private function request($method = null, $data = null) {
  1417. if (!$method) {
  1418. throw new CakeException(__('Request method is missing'));
  1419. }
  1420. if (is_null($data)) {
  1421. throw new CakeException(__('Request Data is not provided'));
  1422. }
  1423. Stripe::setApiKey($this->key);
  1424. $success = null;
  1425. $error = null;
  1426. $message = false;
  1427. $log = null;
  1428. try {
  1429. switch ($method) {
  1430. /**
  1431. *
  1432. * CHARGES
  1433. *
  1434. */
  1435. case 'charge':
  1436. $success = $this->fetch(Stripe_Charge::create($data));
  1437. break;
  1438. case 'retrieveCharge':
  1439. $success = $this->fetch(Stripe_Charge::retrieve($data['charge_id']));
  1440. if (!empty($success['refunds'])) {
  1441. foreach ($success['refunds'] as &$refund) {
  1442. $refund = $this->fetch($refund);
  1443. }
  1444. }
  1445. break;
  1446. case 'updateCharge':
  1447. $charge = Stripe_Charge::retrieve($data['charge_id']);
  1448. foreach ($data['fields'] as $field => $value) {
  1449. $charge->$field = $value;
  1450. }
  1451. $success = $this->fetch($charge->save());
  1452. break;
  1453. case 'refundCharge':
  1454. $charge = Stripe_Charge::retrieve($data['charge_id']);
  1455. unset($data['charge_id']);
  1456. $success = $this->fetch($charge->refund($data));
  1457. foreach ($success['refunds'] as &$refund) {
  1458. $refund = $this->fetch($refund);
  1459. }
  1460. break;
  1461. case 'captureCharge':
  1462. $charge = Stripe_Charge::retrieve($data['charge_id']);
  1463. unset($data['charge_id']);
  1464. $success = $this->fetch($charge->capture($data));
  1465. foreach ($success['refunds'] as &$refund) {
  1466. $refund = $this->fetch($refund);
  1467. }
  1468. break;
  1469. case 'listCharges':
  1470. $charges = Stripe_Charge::all();
  1471. $success = $this->fetch($charges);
  1472. foreach ($success['data'] as &$charge) {
  1473. $charge = $this->fetch($charge);
  1474. if (isset($charge['refunds']) && !empty($charge['refunds'])) {
  1475. foreach ($charge['refunds'] as &$refund) {
  1476. $refund = $this->fetch($refund);
  1477. }
  1478. unset($refund);
  1479. }
  1480. }
  1481. break;
  1482. /**
  1483. * CUSTOMERS
  1484. */
  1485. case 'createCustomer':
  1486. $customer = Stripe_Customer::create($data);
  1487. $success = $this->fetch($customer);
  1488. if (!empty($success['cards']['data'])) {
  1489. foreach ($success['cards']['data'] as &$card) {
  1490. $card = $this->fetch($card);
  1491. }
  1492. unset($card);
  1493. }
  1494. if (!empty($success['subscriptions']['data'])) {
  1495. foreach ($success['subscriptions']['data'] as &$subscription) {
  1496. $subscription = $this->fetch($subscription);
  1497. }
  1498. unset($subscription);
  1499. }
  1500. break;
  1501. case 'retrieveCustomer':
  1502. $customer = Stripe_Customer::retrieve($data['customer_id']);
  1503. $success = $this->fetch($customer);
  1504. if (!empty($success['cards']['data'])) {
  1505. foreach ($success['cards']['data'] as &$card) {
  1506. $card = $this->fetch($card);
  1507. }
  1508. unset($card);
  1509. }
  1510. if (!empty($success['subscriptions']['data'])) {
  1511. foreach ($success['subscriptions']['data'] as &$subscription) {
  1512. $subscription = $this->fetch($subscription);
  1513. }
  1514. unset($subscription);
  1515. }
  1516. break;
  1517. case 'updateCustomer':
  1518. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1519. foreach ($data['fields'] as $field => $value) {
  1520. $cu->$field = $value;
  1521. }
  1522. $success = $this->fetch($cu->save());
  1523. if (!empty($success['cards']['data'])) {
  1524. foreach ($success['cards']['data'] as &$card) {
  1525. $card = $this->fetch($card);
  1526. }
  1527. unset($card);
  1528. }
  1529. if (!empty($success['subscriptions']['data'])) {
  1530. foreach ($success['subscriptions']['data'] as &$subscription) {
  1531. $subscription = $this->fetch($subscription);
  1532. }
  1533. unset($subscription);
  1534. }
  1535. break;
  1536. case 'deleteCustomer':
  1537. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1538. $success = $this->fetch($cu->delete());
  1539. break;
  1540. case 'listCustomers':
  1541. $customers = Stripe_Customer::all($data['options']);
  1542. $success = $this->fetch($customers);
  1543. foreach ($success['data'] as &$customer) {
  1544. $customer = $this->fetch($customer);
  1545. if (!empty($customer['cards']['data'])) {
  1546. foreach ($customer['cards']['data'] as &$card) {
  1547. $card = $this->fetch($card);
  1548. }
  1549. unset($card);
  1550. }
  1551. if (!empty($customer['subscriptions']['data'])) {
  1552. foreach ($customer['subscriptions']['data'] as &$subscription) {
  1553. $subscription = $this->fetch($subscription);
  1554. }
  1555. unset($subscription);
  1556. }
  1557. }
  1558. break;
  1559. /**
  1560. * CARDS
  1561. *
  1562. */
  1563. case 'createCard':
  1564. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1565. // unset customer_id to prevent unknown parameter stripe error
  1566. unset($data['customer_id']);
  1567. $card = $cu->cards->create($data);
  1568. $success = $this->fetch($card);
  1569. break;
  1570. case 'retrieveCard':
  1571. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1572. $card = $cu->cards->retrieve($data['card_id']);
  1573. $success = $this->fetch($card);
  1574. break;
  1575. case 'updateCard':
  1576. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1577. $cuCard = $cu->cards->retrieve($data['card_id']);
  1578. foreach ($data['fields'] as $field => $value) {
  1579. $cuCard->$field = $value;
  1580. }
  1581. $card = $cuCard->save();
  1582. $success = $this->fetch($card);
  1583. break;
  1584. case 'deleteCard':
  1585. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1586. $card = $cu->cards->retrieve($data['card_id'])->delete();
  1587. $success = $this->fetch($card);
  1588. break;
  1589. case 'listCards':
  1590. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1591. $cards = $cu->cards->all($data['options']);
  1592. $success = $this->fetch($cards);
  1593. foreach ($success['data'] as &$card) {
  1594. $card = $this->fetch($card);
  1595. }
  1596. break;
  1597. /**
  1598. * SUBSCRIPTIONS
  1599. *
  1600. */
  1601. case 'createSubscription':
  1602. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1603. // unset customer_id to prevent unknown parameter stripe error
  1604. unset($data['customer_id']);
  1605. $subscription = $cu->subscriptions->create($data['subscription']);
  1606. $success = $this->fetch($subscription);
  1607. break;
  1608. case 'retrieveSubscription':
  1609. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1610. $subscription = $cu->subscriptions->retrieve($data['subscription_id']);
  1611. $success = $this->fetch($subscription);
  1612. break;
  1613. case 'updateSubscription':
  1614. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1615. $cuSubscription = $cu->subscriptions->retrieve($data['subscription_id']);
  1616. foreach ($data['fields'] as $field => $value) {
  1617. $cuSubscription->$field = $value;
  1618. }
  1619. $subscription = $cuSubscription->save();
  1620. $success = $this->fetch($subscription);
  1621. break;
  1622. case 'cancelSubscription':
  1623. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1624. $subscription = $cu->subscriptions->retrieve($data['subscription_id'])->cancel();
  1625. $success = $this->fetch($subscription);
  1626. break;
  1627. case 'listSubscriptions':
  1628. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1629. $subscriptions = $cu->subscriptions->all($data['options']);
  1630. $success = $this->fetch($subscriptions);
  1631. foreach ($success['data'] as &$subscription) {
  1632. $subscription = $this->fetch($subscription);
  1633. }
  1634. break;
  1635. /**
  1636. * PLANS
  1637. *
  1638. */
  1639. case 'createPlan':
  1640. $plan = Stripe_Plan::create($data);
  1641. $success = $this->fetch($plan);
  1642. break;
  1643. case 'retrievePlan':
  1644. $plan = Stripe_Plan::retrieve($data['plan_id']);
  1645. $success = $this->fetch($plan);
  1646. break;
  1647. case 'updatePlan':
  1648. $p = Stripe_Plan::retrieve($data['plan_id']);
  1649. foreach ($data['fields'] as $field => $value) {
  1650. $p->$field = $value;
  1651. }
  1652. $plan = $p->save();
  1653. $success = $this->fetch($plan);
  1654. break;
  1655. case 'deletePlan':
  1656. $p = Stripe_Plan::retrieve($data['plan_id']);
  1657. $plan = $p->delete();
  1658. $success = $this->fetch($plan);
  1659. break;
  1660. case 'listPlans':
  1661. $plans = Stripe_Plan::all($data['options']);
  1662. $success = $this->fetch($plans);
  1663. foreach ($success['data'] as &$plan) {
  1664. $plan = $this->fetch($plan);
  1665. }
  1666. break;
  1667. /**
  1668. * COUPONS
  1669. *
  1670. */
  1671. case 'createCoupon':
  1672. $coupon = Stripe_Coupon::create($data);
  1673. $success = $this->fetch($coupon);
  1674. break;
  1675. case 'retrieveCoupon':
  1676. $coupon = Stripe_Coupon::retrieve($data['coupon_id']);
  1677. $success = $this->fetch($coupon);
  1678. break;
  1679. case 'deleteCoupon':
  1680. $c = Stripe_Coupon::retrieve($data['coupon_id']);
  1681. $coupon = $c->delete();
  1682. $success = $this->fetch($coupon);
  1683. break;
  1684. case 'listCoupons':
  1685. $coupons = Stripe_Coupon::all($data['options']);
  1686. $success = $this->fetch($coupons);
  1687. foreach ($success['data'] as &$coupon) {
  1688. $coupon = $this->fetch($coupon);
  1689. }
  1690. break;
  1691. /**
  1692. * DISCOUNTS
  1693. *
  1694. */
  1695. case 'deleteCustomerDiscount':
  1696. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1697. $discount = $cu->deleteDiscount();
  1698. $success = $this->fetch($discount);
  1699. break;
  1700. case 'deleteSubscriptionDiscount':
  1701. $cu = Stripe_Customer::retrieve($data['customer_id']);
  1702. $discount = $cu->subscriptions->retrieve($data['subscription_id'])->deleteDiscount();
  1703. $success = $this->fetch($discount);
  1704. break;
  1705. /**
  1706. *
  1707. * INVOICES
  1708. *
  1709. */
  1710. case 'retrieveInvoice':
  1711. $coupon = Stripe_Invoice::retrieve($data['invoice_id']);
  1712. $success = $this->fetch($coupon);
  1713. if (!empty($success['lines']['data'])) {
  1714. foreach ($success['lines']['data'] as &$invoice) {
  1715. $invoice = $this->fetch($invoice);
  1716. }
  1717. }
  1718. break;
  1719. case 'retrieveInvoiceLine':
  1720. $in = Stripe_Invoice::retrieve($data['invoice_id'])->lines->all($data['options']);
  1721. $success = $this->fetch($in);
  1722. if (!empty($success['data'])) {
  1723. foreach ($success['data'] as &$invoice) {
  1724. $invoice = $this->fetch($invoice);
  1725. }
  1726. }
  1727. break;
  1728. case 'createInvoice':
  1729. $invoice = Stripe_Invoice::create($data);
  1730. $success = $this->fetch($invoice);
  1731. break;
  1732. case 'payInvoice':
  1733. $invoice = Stripe_Invoice::retrieve($data['invoice_id']);
  1734. $in = $invoice->pay();
  1735. $success = $this->fetch($in);
  1736. break;
  1737. case 'updateInvoice':
  1738. $in = Stripe_Invoice::retrieve($data['invoice_id']);
  1739. foreach ($data['fields'] as $field => $value) {
  1740. $in->$field = $value;
  1741. }
  1742. $invoice = $in->save();
  1743. $success = $this->fetch($invoice);
  1744. if (!empty($success['lines']['data'])) {
  1745. foreach ($success['lines']['data'] as &$invoice) {
  1746. $invoice = $this->fetch($invoice);
  1747. }
  1748. }
  1749. break;
  1750. case 'listInvoices':
  1751. $invocies = Stripe_Invoice::all($data);
  1752. $success = $this->fetch($invocies);
  1753. if (!empty($success['data'])) {
  1754. foreach ($success['data'] as &$invoice) {
  1755. $invoice = $this->fetch($invoice);
  1756. if (!empty($invoice['lines']['data'])) {
  1757. foreach ($invoice['lines']['data'] as &$invoiceLine) {
  1758. $invoiceLine = $this->fetch($invoiceLine);
  1759. }
  1760. unset($invoiceLine);
  1761. }
  1762. }
  1763. }
  1764. break;
  1765. case 'retrieveUpcomingInvoice':
  1766. $invoice = Stripe_Invoice::upcoming($data);
  1767. $success = $this->fetch($invoice);
  1768. if (!empty($success['lines']['data'])) {
  1769. foreach ($success['lines']['data'] as &$invoice) {
  1770. $invoice = $this->fetch($invoice);
  1771. }
  1772. }
  1773. break;
  1774. /**
  1775. *
  1776. * INVOICE ITEMS
  1777. *
  1778. */
  1779. case 'createInvoiceItem':
  1780. $success = $this->fetch(Stripe_InvoiceItem::create($data));
  1781. break;
  1782. case 'retrieveInvoiceItem':
  1783. $success = $this->fetch(Stripe_InvoiceItem::retrieve($data['invoice_item_id']));
  1784. break;
  1785. case 'updateInvoiceItem':
  1786. $ii = Stripe_InvoiceItem::retrieve($data['invoice_item_id']);
  1787. foreach ($data['fields'] as $field => $value) {
  1788. $ii->$field = $value;
  1789. }
  1790. $success = $this->fetch($ii->save());
  1791. break;
  1792. case 'deleteInvoiceItem':
  1793. $ii = Stripe_InvoiceItem::retrieve($data['invoice_item_id']);
  1794. $success = $this->fetch($ii->delete());
  1795. break;
  1796. case 'listInvoiceItems':
  1797. $ii = Stripe_InvoiceItem::all($data['options']);
  1798. $success = $this->fetch($ii);
  1799. if (!empty($success['data'])) {
  1800. foreach ($success['data'] as &$card) {
  1801. $card = $this->fetch($card);
  1802. }
  1803. }
  1804. break;
  1805. /**
  1806. * DISPUTES
  1807. *
  1808. */
  1809. case 'updateDispute':
  1810. $ch = Stripe_Charge::retrieve($data['charge_id']);
  1811. $success = $this->fetch($ch->updateDispute($data['dispute']));
  1812. break;
  1813. case 'closeDispute':
  1814. $ch = Stripe_Charge::retrieve($data['charge_id']);
  1815. $success = $this->fetch($ch->closeDispute());
  1816. break;
  1817. /**
  1818. *
  1819. * TRANSFERS
  1820. *
  1821. */
  1822. case 'createTransfer':
  1823. $success = $this->fetch(Stripe_Transfer::create($data));
  1824. break;
  1825. case 'retrieveTransfer':
  1826. $success = $this->fetch(Stripe_Transfer::retrieve($data['transfer_id']));
  1827. break;
  1828. case 'updateTransfer':
  1829. $tr = Stripe_Transfer::retrieve($data['transfer_id']);
  1830. foreach ($data['fields'] as $field => $value) {
  1831. $tr->$field = $value;
  1832. }
  1833. $success = $this->fetch($tr->save());
  1834. break;
  1835. case 'cancelTransfer':
  1836. $tr = Stripe_Transfer::retrieve($data['transfer_id']);
  1837. $success = $this->fetch($tr->cancel());
  1838. break;
  1839. case 'listTransfers':
  1840. $tr = Stripe_Transfer::all($data['options']);
  1841. $success = $this->fetch($tr);
  1842. foreach ($success['data'] as &$transfer) {
  1843. $transfer = $this->fetch($transfer);
  1844. // charge_fee_details
  1845. if (isset($transfer['summary']['charge_fee_details']) && !empty($transfer['summary']['charge_fee_details'])) {
  1846. foreach ($transfer['summary']['charge_fee_details'] as &$chargeFee) {
  1847. $chargeFee = $this->fetch($chargeFee);
  1848. }
  1849. unset($chargeFee);
  1850. }
  1851. // refund_fee_details
  1852. if (isset($transfer['summary']['refund_fee_details']) && !empty($transfer['summary']['refund_fee_details'])) {
  1853. foreach ($transfer['summary']['refund_fee_details'] as &$refundFee) {
  1854. $refundFee = $this->fetch($refundFee);
  1855. }
  1856. unset($refundFee);
  1857. }
  1858. // adjustment_fee_details
  1859. if (isset($transfer['summary']['adjustment_fee_details']) && !empty($transfer['summary']['adjustment_fee_details'])) {
  1860. foreach ($transfer['summary']['adjustment_fee_details'] as &$adjustmentFee) {
  1861. $adjustmentFee = $this->fetch($adjustmentFee);
  1862. }
  1863. unset($adjustmentFee);
  1864. }
  1865. // transactions
  1866. if (isset($transfer['transactions']['data']) && !empty($transfer['transactions']['data'])) {
  1867. foreach ($transfer['transactions']['data'] as &$transaction) {
  1868. $transaction = $this->fetch($transaction);
  1869. // fee details
  1870. if (isset($transaction['fee_details']) && !empty($transaction['fee_details'])) {
  1871. foreach ($transaction['fee_details'] as &$feeDetails) {
  1872. $feeDetails = $this->fetch($feeDetails);
  1873. }
  1874. unset($feeDetails);
  1875. }
  1876. }
  1877. unset($transaction);
  1878. }
  1879. }
  1880. break;
  1881. /**
  1882. *
  1883. * RECIPIENTS
  1884. *
  1885. */
  1886. case 'createRecipient':
  1887. $recipient = Stripe_Recipient::create($data);
  1888. $success = $this->fetch($recipient);
  1889. if (!empty($success['cards']['data'])) {
  1890. foreach ($success['cards']['data'] as &$card) {
  1891. $card = $this->fetch($card);
  1892. }
  1893. unset($card);
  1894. }
  1895. break;
  1896. case 'retrieveRecipient':
  1897. $recipient = Stripe_Recipient::retrieve($data['recipient_id']);
  1898. $success = $this->fetch($recipient);
  1899. if (!empty($success['cards']['data'])) {
  1900. foreach ($success['cards']['data'] as &$card) {
  1901. $card = $this->fetch($card);
  1902. }
  1903. unset($card);
  1904. }
  1905. break;
  1906. case 'updateRecipient':
  1907. $rp = Stripe_Recipient::retrieve($data['recipient_id']);
  1908. foreach ($data['fields'] as $field => $value) {
  1909. $rp->$field = $value;
  1910. }
  1911. $success = $this->fetch($rp->save());
  1912. if (!empty($success['cards']['data'])) {
  1913. foreach ($success['cards']['data'] as &$card) {
  1914. $card = $this->fetch($card);
  1915. }
  1916. unset($card);
  1917. }
  1918. break;
  1919. case 'deleteRecipient':
  1920. $rp = Stripe_Recipient::retrieve($data['recipient_id']);
  1921. $success = $this->fetch($rp->delete());
  1922. break;
  1923. case 'listRecipients':
  1924. $recipients = Stripe_Recipient::all($data['options']);
  1925. $success = $this->fetch($recipients);
  1926. foreach ($success['data'] as &$recipient) {
  1927. $recipient = $this->fetch($recipient);
  1928. if (!empty($recipient['cards']['data'])) {
  1929. foreach ($recipient['cards']['data'] as &$card) {
  1930. $card = $this->fetch($card);
  1931. }
  1932. unset($card);
  1933. }
  1934. }
  1935. break;
  1936. /**
  1937. *
  1938. * APPLICATION FEES
  1939. *
  1940. */
  1941. case 'retrieveApplicationFee':
  1942. $success = $this->fetch(Stripe_ApplicationFee::retrieve($data['application_fee_id']));
  1943. if (!empty($success['refunds'])) {
  1944. foreach ($success['refunds'] as &$refund) {
  1945. $refund = $this->fetch($refund);
  1946. }
  1947. }
  1948. break;
  1949. case 'refundApplicationFee':
  1950. $fee = Stripe_ApplicationFee::retrieve($data['application_fee_id']);
  1951. unset($data['application_fee_id']);
  1952. $success = $this->fetch($fee->refund($data));
  1953. if (!empty($success['refunds'])) {
  1954. foreach ($success['refunds'] as &$refund) {
  1955. $refund = $this->fetch($refund);
  1956. }
  1957. }
  1958. break;
  1959. case 'listApplicationFees':
  1960. $fees = Stripe_ApplicationFee::all($data['options']);
  1961. $success = $this->fetch($fees);
  1962. foreach ($success['data'] as &$fee) {
  1963. $fee = $this->fetch($fee);
  1964. }
  1965. break;
  1966. /**
  1967. *
  1968. * ACCOUNT
  1969. *
  1970. */
  1971. case 'retrieveAccount':
  1972. $success = $this->fetch(Stripe_Account::retrieve());
  1973. break;
  1974. /**
  1975. *
  1976. * BALANCE
  1977. *
  1978. */
  1979. case 'retrieveBalance':
  1980. $success = $this->fetch(Stripe_Balance::retrieve());
  1981. break;
  1982. case 'retrieveBalanceTransaction':
  1983. $success = $this->fetch(Stripe_BalanceTransaction::retrieve($data['transaction_id']));
  1984. break;
  1985. case 'listBalanceHistory':
  1986. $history = Stripe_BalanceTransaction::all($data['options']);
  1987. $success = $this->fetch($history);
  1988. foreach ($success['data'] as &$transaction) {
  1989. $transaction = $this->fetch($transaction);
  1990. }
  1991. break;
  1992. /**
  1993. *
  1994. * EVENTS
  1995. *
  1996. */
  1997. case 'retrieveEvent':
  1998. $event = Stripe_Event::retrieve($data['event_id']);
  1999. $success = $this->fetch($event);
  2000. // cards
  2001. if (isset($success['data']['object']['cards']['data']) && !empty($success['data']['object']['cards']['data'])) {
  2002. foreach ($success['data']['object']['cards']['data'] as &$card) {
  2003. $card = $this->fetch($card);
  2004. }
  2005. unset($refund);
  2006. }
  2007. break;
  2008. case 'listEvents':
  2009. $events = Stripe_Event::all($data['options']);
  2010. $success = $this->fetch($events);
  2011. foreach ($success['data'] as &$event) {
  2012. $event = $this->fetch($event);
  2013. // refunds
  2014. if (isset($event['data']['object']['refunds']) && !empty($event['data']['object']['refunds'])) {
  2015. foreach ($event['data']['object']['refunds'] as &$refund) {
  2016. $refund = $this->fetch($refund);
  2017. }
  2018. unset($refund);
  2019. }
  2020. // cards
  2021. if (isset($event['data']['object']['cards']['data']) && !empty($event['data']['object']['cards']['data'])) {
  2022. foreach ($event['data']['object']['cards']['data'] as &$card) {
  2023. $card = $this->fetch($card);
  2024. }
  2025. unset($refund);
  2026. }
  2027. }
  2028. break;
  2029. /**
  2030. *
  2031. * TOKENS
  2032. *
  2033. */
  2034. case 'createCardToken':
  2035. $success = $this->fetch(Stripe_Token::create($data));
  2036. break;
  2037. case 'createBankAccountToken':
  2038. $success = $this->fetch(Stripe_Token::create($data));
  2039. break;
  2040. case 'retrieveToken':
  2041. $success = $this->fetch(Stripe_Token::retrieve($data['token_id']));
  2042. break;
  2043. default:
  2044. throw new CakeException($method.' '.__('method not found in StripeComponent'));
  2045. break;
  2046. }
  2047. } catch(Stripe_CardError $e) {
  2048. $body = $e->getJsonBody();
  2049. $error = $body['error'];
  2050. $error['http_status'] = $e->getHttpStatus();
  2051. $message = $error['message'];
  2052. } catch (Stripe_InvalidRequestError $e) {
  2053. $body = $e->getJsonBody();
  2054. $error = $body['error'];
  2055. $error['http_status'] = $e->getHttpStatus();
  2056. } catch (Stripe_AuthenticationError $e) {
  2057. $body = $e->getJsonBody();
  2058. $error = $body['error'];
  2059. $error['http_status'] = $e->getHttpStatus();
  2060. } catch (Stripe_ApiConnectionError $e) {
  2061. $body = $e->getJsonBody();
  2062. $error = $body['error'];
  2063. $error['http_status'] = $e->getHttpStatus();
  2064. } catch (Stripe_Error $e) {
  2065. $body = $e->getJsonBody();
  2066. $error = $body['error'];
  2067. $error['http_status'] = $e->getHttpStatus();
  2068. } catch (Exception $e) {
  2069. $body = $e->getJsonBody();
  2070. $error = $body['error'];
  2071. $error['http_status'] = $e->getHttpStatus();
  2072. }
  2073. if ($success) {
  2074. if ($this->logFile && in_array($this->logType, array('both', 'success'))) {
  2075. CakeLog::write('success', $method, $this->logFile);
  2076. }
  2077. return array(
  2078. 'status' => 'success',
  2079. 'message' => 'Success',
  2080. 'response' => $success
  2081. );
  2082. }
  2083. $str = $method.", type:".@$error['type'].", http_status:".@$error['http_status'].", param:".@$error['param'].", message:".@$error['message'];
  2084. if ($this->logFile && in_array($this->logType, array('both', 'error'))) {
  2085. CakeLog::error( $str, $this->logFile );
  2086. }
  2087. return array(
  2088. 'status' => 'error',
  2089. 'message' => $message,
  2090. 'response' => $error
  2091. );
  2092. }
  2093. /**
  2094. * fetch method
  2095. * Converts object to array - checking also one level nested objects
  2096. *
  2097. * @param object $object
  2098. * @return array
  2099. */
  2100. private function fetch($object) {
  2101. $objectClass = get_class($object);
  2102. if (!isset($this->reflectionClass[$objectClass])) {
  2103. $this->reflectionClass[$objectClass] = new ReflectionClass($objectClass);
  2104. }
  2105. $array = array();
  2106. foreach ($this->reflectionClass[$objectClass]->getProperties() as $property) {
  2107. $property->setAccessible(true);
  2108. $array[$property->getName()] = $property->getValue($object);
  2109. $property->setAccessible(false);
  2110. }
  2111. foreach ($array['_values'] as $k => $value) {
  2112. if (is_object($value)) {
  2113. $array['_values'][$k] = $this->fetch($value);
  2114. }
  2115. }
  2116. return $array['_values'];
  2117. }
  2118. }