Customer.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Stripe;
  3. /**
  4. * Class Customer
  5. *
  6. * @property string $id
  7. * @property string $object
  8. * @property int $account_balance
  9. * @property string $business_vat_id
  10. * @property string $created
  11. * @property string $currency
  12. * @property string $default_source
  13. * @property bool $delinquent
  14. * @property string $description
  15. * @property Discount $discount
  16. * @property string $email
  17. * @property string $invoice_prefix
  18. * @property bool $livemode
  19. * @property StripeObject $metadata
  20. * @property mixed $shipping
  21. * @property Collection $sources
  22. * @property Collection $subscriptions
  23. *
  24. * @package Stripe
  25. */
  26. class Customer extends ApiResource
  27. {
  28. const OBJECT_NAME = "customer";
  29. use ApiOperations\All;
  30. use ApiOperations\Create;
  31. use ApiOperations\Delete;
  32. use ApiOperations\NestedResource;
  33. use ApiOperations\Retrieve;
  34. use ApiOperations\Update;
  35. public static function getSavedNestedResources()
  36. {
  37. static $savedNestedResources = null;
  38. if ($savedNestedResources === null) {
  39. $savedNestedResources = new Util\Set([
  40. 'source',
  41. ]);
  42. }
  43. return $savedNestedResources;
  44. }
  45. const PATH_SOURCES = '/sources';
  46. /**
  47. * @param array|null $params
  48. *
  49. * @return InvoiceItem The resulting invoice item.
  50. */
  51. public function addInvoiceItem($params = null)
  52. {
  53. $params = $params ?: [];
  54. $params['customer'] = $this->id;
  55. $ii = InvoiceItem::create($params, $this->_opts);
  56. return $ii;
  57. }
  58. /**
  59. * @param array|null $params
  60. *
  61. * @return array An array of the customer's Invoices.
  62. */
  63. public function invoices($params = null)
  64. {
  65. $params = $params ?: [];
  66. $params['customer'] = $this->id;
  67. $invoices = Invoice::all($params, $this->_opts);
  68. return $invoices;
  69. }
  70. /**
  71. * @param array|null $params
  72. *
  73. * @return array An array of the customer's InvoiceItems.
  74. */
  75. public function invoiceItems($params = null)
  76. {
  77. $params = $params ?: [];
  78. $params['customer'] = $this->id;
  79. $iis = InvoiceItem::all($params, $this->_opts);
  80. return $iis;
  81. }
  82. /**
  83. * @param array|null $params
  84. *
  85. * @return array An array of the customer's Charges.
  86. */
  87. public function charges($params = null)
  88. {
  89. $params = $params ?: [];
  90. $params['customer'] = $this->id;
  91. $charges = Charge::all($params, $this->_opts);
  92. return $charges;
  93. }
  94. /**
  95. * @param array|null $params
  96. *
  97. * @return Subscription The updated subscription.
  98. */
  99. public function updateSubscription($params = null)
  100. {
  101. $url = $this->instanceUrl() . '/subscription';
  102. list($response, $opts) = $this->_request('post', $url, $params);
  103. $this->refreshFrom(['subscription' => $response], $opts, true);
  104. return $this->subscription;
  105. }
  106. /**
  107. * @param array|null $params
  108. *
  109. * @return Subscription The cancelled subscription.
  110. */
  111. public function cancelSubscription($params = null)
  112. {
  113. $url = $this->instanceUrl() . '/subscription';
  114. list($response, $opts) = $this->_request('delete', $url, $params);
  115. $this->refreshFrom(['subscription' => $response], $opts, true);
  116. return $this->subscription;
  117. }
  118. /**
  119. * @return Customer The updated customer.
  120. */
  121. public function deleteDiscount()
  122. {
  123. $url = $this->instanceUrl() . '/discount';
  124. list($response, $opts) = $this->_request('delete', $url);
  125. $this->refreshFrom(['discount' => null], $opts, true);
  126. }
  127. /**
  128. * @param array|null $id The ID of the customer on which to create the source.
  129. * @param array|null $params
  130. * @param array|string|null $opts
  131. *
  132. * @return ApiResource
  133. */
  134. public static function createSource($id, $params = null, $opts = null)
  135. {
  136. return self::_createNestedResource($id, static::PATH_SOURCES, $params, $opts);
  137. }
  138. /**
  139. * @param array|null $id The ID of the customer to which the source belongs.
  140. * @param array|null $sourceId The ID of the source to retrieve.
  141. * @param array|null $params
  142. * @param array|string|null $opts
  143. *
  144. * @return ApiResource
  145. */
  146. public static function retrieveSource($id, $sourceId, $params = null, $opts = null)
  147. {
  148. return self::_retrieveNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
  149. }
  150. /**
  151. * @param array|null $id The ID of the customer to which the source belongs.
  152. * @param array|null $sourceId The ID of the source to update.
  153. * @param array|null $params
  154. * @param array|string|null $opts
  155. *
  156. * @return ApiResource
  157. */
  158. public static function updateSource($id, $sourceId, $params = null, $opts = null)
  159. {
  160. return self::_updateNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
  161. }
  162. /**
  163. * @param array|null $id The ID of the customer to which the source belongs.
  164. * @param array|null $sourceId The ID of the source to delete.
  165. * @param array|null $params
  166. * @param array|string|null $opts
  167. *
  168. * @return ApiResource
  169. */
  170. public static function deleteSource($id, $sourceId, $params = null, $opts = null)
  171. {
  172. return self::_deleteNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
  173. }
  174. /**
  175. * @param array|null $id The ID of the customer on which to retrieve the sources.
  176. * @param array|null $params
  177. * @param array|string|null $opts
  178. *
  179. * @return ApiResource
  180. */
  181. public static function allSources($id, $params = null, $opts = null)
  182. {
  183. return self::_allNestedResources($id, static::PATH_SOURCES, $params, $opts);
  184. }
  185. }