Customer.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * This object represents a customer of your business. Use it to create recurring charges and track payments that belong to the same customer.
  6. *
  7. * Related guide: <a href="https://stripe.com/docs/payments/save-during-payment">Save a card during payment</a>
  8. *
  9. * @property string $id Unique identifier for the object.
  10. * @property string $object String representing the object's type. Objects of the same type share the same value.
  11. * @property null|\Stripe\StripeObject $address The customer's address.
  12. * @property null|int $balance The current balance, if any, that's stored on the customer. If negative, the customer has credit to apply to their next invoice. If positive, the customer has an amount owed that's added to their next invoice. The balance only considers amounts that Stripe hasn't successfully applied to any invoice. It doesn't reflect unpaid invoices. This balance is only taken into account after invoices finalize.
  13. * @property null|\Stripe\CashBalance $cash_balance The current funds being held by Stripe on behalf of the customer. You can apply these funds towards payment intents when the source is &quot;cash_balance&quot;. The <code>settings[reconciliation_mode]</code> field describes if these funds apply to these payment intents manually or automatically.
  14. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  15. * @property null|string $currency Three-letter <a href="https://stripe.com/docs/currencies">ISO code for the currency</a> the customer can be charged in for recurring billing purposes.
  16. * @property null|string|\Stripe\Account|\Stripe\BankAccount|\Stripe\Card|\Stripe\Source $default_source <p>ID of the default payment source for the customer.</p><p>If you use payment methods created through the PaymentMethods API, see the <a href="https://stripe.com/docs/api/customers/object#customer_object-invoice_settings-default_payment_method">invoice_settings.default_payment_method</a> field instead.</p>
  17. * @property null|bool $delinquent <p>Tracks the most recent state change on any invoice belonging to the customer. Paying an invoice or marking it uncollectible via the API will set this field to false. An automatic payment failure or passing the <code>invoice.due_date</code> will set this field to <code>true</code>.</p><p>If an invoice becomes uncollectible by <a href="https://stripe.com/docs/billing/automatic-collection">dunning</a>, <code>delinquent</code> doesn't reset to <code>false</code>.</p><p>If you care whether the customer has paid their most recent subscription invoice, use <code>subscription.status</code> instead. Paying or marking uncollectible any customer invoice regardless of whether it is the latest invoice for a subscription will always set this field to <code>false</code>.</p>
  18. * @property null|string $description An arbitrary string attached to the object. Often useful for displaying to users.
  19. * @property null|\Stripe\Discount $discount Describes the current discount active on the customer, if there is one.
  20. * @property null|string $email The customer's email address.
  21. * @property null|\Stripe\StripeObject $invoice_credit_balance The current multi-currency balances, if any, that's stored on the customer. If positive in a currency, the customer has a credit to apply to their next invoice denominated in that currency. If negative, the customer has an amount owed that's added to their next invoice denominated in that currency. These balances don't apply to unpaid invoices. They solely track amounts that Stripe hasn't successfully applied to any invoice. Stripe only applies a balance in a specific currency to an invoice after that invoice (which is in the same currency) finalizes.
  22. * @property null|string $invoice_prefix The prefix for the customer used to generate unique invoice numbers.
  23. * @property null|\Stripe\StripeObject $invoice_settings
  24. * @property bool $livemode Has the value <code>true</code> if the object exists in live mode or the value <code>false</code> if the object exists in test mode.
  25. * @property null|\Stripe\StripeObject $metadata Set of <a href="https://stripe.com/docs/api/metadata">key-value pairs</a> that you can attach to an object. This can be useful for storing additional information about the object in a structured format.
  26. * @property null|string $name The customer's full name or business name.
  27. * @property null|int $next_invoice_sequence The suffix of the customer's next invoice number (for example, 0001). When the account uses account level sequencing, this parameter is ignored in API requests and the field omitted in API responses.
  28. * @property null|string $phone The customer's phone number.
  29. * @property null|string[] $preferred_locales The customer's preferred locales (languages), ordered by preference.
  30. * @property null|\Stripe\StripeObject $shipping Mailing and shipping address for the customer. Appears on invoices emailed to this customer.
  31. * @property null|\Stripe\Collection<\Stripe\Account|\Stripe\BankAccount|\Stripe\Card|\Stripe\Source> $sources The customer's payment sources, if any.
  32. * @property null|\Stripe\Collection<\Stripe\Subscription> $subscriptions The customer's current subscriptions, if any.
  33. * @property null|\Stripe\StripeObject $tax
  34. * @property null|string $tax_exempt Describes the customer's tax exemption status, which is <code>none</code>, <code>exempt</code>, or <code>reverse</code>. When set to <code>reverse</code>, invoice and receipt PDFs include the following text: <strong>&quot;Reverse charge&quot;</strong>.
  35. * @property null|\Stripe\Collection<\Stripe\TaxId> $tax_ids The customer's tax IDs.
  36. * @property null|string|\Stripe\TestHelpers\TestClock $test_clock ID of the test clock that this customer belongs to.
  37. */
  38. class Customer extends ApiResource
  39. {
  40. const OBJECT_NAME = 'customer';
  41. use ApiOperations\NestedResource;
  42. use ApiOperations\Update;
  43. const TAX_EXEMPT_EXEMPT = 'exempt';
  44. const TAX_EXEMPT_NONE = 'none';
  45. const TAX_EXEMPT_REVERSE = 'reverse';
  46. /**
  47. * Creates a new customer object.
  48. *
  49. * @param null|array $params
  50. * @param null|array|string $options
  51. *
  52. * @throws \Stripe\Exception\ApiErrorException if the request fails
  53. *
  54. * @return \Stripe\Customer the created resource
  55. */
  56. public static function create($params = null, $options = null)
  57. {
  58. self::_validateParams($params);
  59. $url = static::classUrl();
  60. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  61. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  62. $obj->setLastResponse($response);
  63. return $obj;
  64. }
  65. /**
  66. * Permanently deletes a customer. It cannot be undone. Also immediately cancels
  67. * any active subscriptions on the customer.
  68. *
  69. * @param null|array $params
  70. * @param null|array|string $opts
  71. *
  72. * @throws \Stripe\Exception\ApiErrorException if the request fails
  73. *
  74. * @return \Stripe\Customer the deleted resource
  75. */
  76. public function delete($params = null, $opts = null)
  77. {
  78. self::_validateParams($params);
  79. $url = $this->instanceUrl();
  80. list($response, $opts) = $this->_request('delete', $url, $params, $opts);
  81. $this->refreshFrom($response, $opts);
  82. return $this;
  83. }
  84. /**
  85. * Returns a list of your customers. The customers are returned sorted by creation
  86. * date, with the most recent customers appearing first.
  87. *
  88. * @param null|array $params
  89. * @param null|array|string $opts
  90. *
  91. * @throws \Stripe\Exception\ApiErrorException if the request fails
  92. *
  93. * @return \Stripe\Collection<\Stripe\Customer> of ApiResources
  94. */
  95. public static function all($params = null, $opts = null)
  96. {
  97. $url = static::classUrl();
  98. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  99. }
  100. /**
  101. * Retrieves a Customer object.
  102. *
  103. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  104. * @param null|array|string $opts
  105. *
  106. * @throws \Stripe\Exception\ApiErrorException if the request fails
  107. *
  108. * @return \Stripe\Customer
  109. */
  110. public static function retrieve($id, $opts = null)
  111. {
  112. $opts = \Stripe\Util\RequestOptions::parse($opts);
  113. $instance = new static($id, $opts);
  114. $instance->refresh();
  115. return $instance;
  116. }
  117. /**
  118. * Updates the specified customer by setting the values of the parameters passed.
  119. * Any parameters not provided will be left unchanged. For example, if you pass the
  120. * <strong>source</strong> parameter, that becomes the customer’s active source
  121. * (e.g., a card) to be used for all charges in the future. When you update a
  122. * customer to a new valid card source by passing the <strong>source</strong>
  123. * parameter: for each of the customer’s current subscriptions, if the subscription
  124. * bills automatically and is in the <code>past_due</code> state, then the latest
  125. * open invoice for the subscription with automatic collection enabled will be
  126. * retried. This retry will not count as an automatic retry, and will not affect
  127. * the next regularly scheduled payment for the invoice. Changing the
  128. * <strong>default_source</strong> for a customer will not trigger this behavior.
  129. *
  130. * This request accepts mostly the same arguments as the customer creation call.
  131. *
  132. * @param string $id the ID of the resource to update
  133. * @param null|array $params
  134. * @param null|array|string $opts
  135. *
  136. * @throws \Stripe\Exception\ApiErrorException if the request fails
  137. *
  138. * @return \Stripe\Customer the updated resource
  139. */
  140. public static function update($id, $params = null, $opts = null)
  141. {
  142. self::_validateParams($params);
  143. $url = static::resourceUrl($id);
  144. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  145. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  146. $obj->setLastResponse($response);
  147. return $obj;
  148. }
  149. public static function getSavedNestedResources()
  150. {
  151. static $savedNestedResources = null;
  152. if (null === $savedNestedResources) {
  153. $savedNestedResources = new Util\Set([
  154. 'source',
  155. ]);
  156. }
  157. return $savedNestedResources;
  158. }
  159. /**
  160. * @param null|array $params
  161. * @param null|array|string $opts
  162. *
  163. * @return \Stripe\Customer the updated customer
  164. */
  165. public function deleteDiscount($params = null, $opts = null)
  166. {
  167. $url = $this->instanceUrl() . '/discount';
  168. list($response, $opts) = $this->_request('delete', $url, $params, $opts);
  169. $this->refreshFrom(['discount' => null], $opts, true);
  170. return $this;
  171. }
  172. /**
  173. * @param string $id
  174. * @param null|array $params
  175. * @param null|array|string $opts
  176. *
  177. * @throws \Stripe\Exception\ApiErrorException if the request fails
  178. *
  179. * @return \Stripe\Collection<\Stripe\PaymentMethod> list of payment methods
  180. */
  181. public static function allPaymentMethods($id, $params = null, $opts = null)
  182. {
  183. $url = static::resourceUrl($id) . '/payment_methods';
  184. list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
  185. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  186. $obj->setLastResponse($response);
  187. return $obj;
  188. }
  189. /**
  190. * @param string $payment_method
  191. * @param null|array $params
  192. * @param null|array|string $opts
  193. *
  194. * @throws \Stripe\Exception\ApiErrorException if the request fails
  195. *
  196. * @return \Stripe\PaymentMethod the retrieved payment method
  197. */
  198. public function retrievePaymentMethod($payment_method, $params = null, $opts = null)
  199. {
  200. $url = $this->instanceUrl() . '/payment_methods/' . $payment_method;
  201. list($response, $opts) = $this->_request('get', $url, $params, $opts);
  202. $obj = \Stripe\Util\Util::convertToStripeObject($response, $opts);
  203. $obj->setLastResponse($response);
  204. return $obj;
  205. }
  206. /**
  207. * @param null|array $params
  208. * @param null|array|string $opts
  209. *
  210. * @throws \Stripe\Exception\ApiErrorException if the request fails
  211. *
  212. * @return \Stripe\SearchResult<\Stripe\Customer> the customer search results
  213. */
  214. public static function search($params = null, $opts = null)
  215. {
  216. $url = '/v1/customers/search';
  217. return static::_requestPage($url, \Stripe\SearchResult::class, $params, $opts);
  218. }
  219. const PATH_BALANCE_TRANSACTIONS = '/balance_transactions';
  220. /**
  221. * @param string $id the ID of the customer on which to retrieve the customer balance transactions
  222. * @param null|array $params
  223. * @param null|array|string $opts
  224. *
  225. * @throws \Stripe\Exception\ApiErrorException if the request fails
  226. *
  227. * @return \Stripe\Collection<\Stripe\CustomerBalanceTransaction> the list of customer balance transactions
  228. */
  229. public static function allBalanceTransactions($id, $params = null, $opts = null)
  230. {
  231. return self::_allNestedResources($id, static::PATH_BALANCE_TRANSACTIONS, $params, $opts);
  232. }
  233. /**
  234. * @param string $id the ID of the customer on which to create the customer balance transaction
  235. * @param null|array $params
  236. * @param null|array|string $opts
  237. *
  238. * @throws \Stripe\Exception\ApiErrorException if the request fails
  239. *
  240. * @return \Stripe\CustomerBalanceTransaction
  241. */
  242. public static function createBalanceTransaction($id, $params = null, $opts = null)
  243. {
  244. return self::_createNestedResource($id, static::PATH_BALANCE_TRANSACTIONS, $params, $opts);
  245. }
  246. /**
  247. * @param string $id the ID of the customer to which the customer balance transaction belongs
  248. * @param string $balanceTransactionId the ID of the customer balance transaction to retrieve
  249. * @param null|array $params
  250. * @param null|array|string $opts
  251. *
  252. * @throws \Stripe\Exception\ApiErrorException if the request fails
  253. *
  254. * @return \Stripe\CustomerBalanceTransaction
  255. */
  256. public static function retrieveBalanceTransaction($id, $balanceTransactionId, $params = null, $opts = null)
  257. {
  258. return self::_retrieveNestedResource($id, static::PATH_BALANCE_TRANSACTIONS, $balanceTransactionId, $params, $opts);
  259. }
  260. /**
  261. * @param string $id the ID of the customer to which the customer balance transaction belongs
  262. * @param string $balanceTransactionId the ID of the customer balance transaction to update
  263. * @param null|array $params
  264. * @param null|array|string $opts
  265. *
  266. * @throws \Stripe\Exception\ApiErrorException if the request fails
  267. *
  268. * @return \Stripe\CustomerBalanceTransaction
  269. */
  270. public static function updateBalanceTransaction($id, $balanceTransactionId, $params = null, $opts = null)
  271. {
  272. return self::_updateNestedResource($id, static::PATH_BALANCE_TRANSACTIONS, $balanceTransactionId, $params, $opts);
  273. }
  274. const PATH_CASH_BALANCE_TRANSACTIONS = '/cash_balance_transactions';
  275. /**
  276. * @param string $id the ID of the customer on which to retrieve the customer cash balance transactions
  277. * @param null|array $params
  278. * @param null|array|string $opts
  279. *
  280. * @throws \Stripe\Exception\ApiErrorException if the request fails
  281. *
  282. * @return \Stripe\Collection<\Stripe\CustomerCashBalanceTransaction> the list of customer cash balance transactions
  283. */
  284. public static function allCashBalanceTransactions($id, $params = null, $opts = null)
  285. {
  286. return self::_allNestedResources($id, static::PATH_CASH_BALANCE_TRANSACTIONS, $params, $opts);
  287. }
  288. /**
  289. * @param string $id the ID of the customer to which the customer cash balance transaction belongs
  290. * @param string $cashBalanceTransactionId the ID of the customer cash balance transaction to retrieve
  291. * @param null|array $params
  292. * @param null|array|string $opts
  293. *
  294. * @throws \Stripe\Exception\ApiErrorException if the request fails
  295. *
  296. * @return \Stripe\CustomerCashBalanceTransaction
  297. */
  298. public static function retrieveCashBalanceTransaction($id, $cashBalanceTransactionId, $params = null, $opts = null)
  299. {
  300. return self::_retrieveNestedResource($id, static::PATH_CASH_BALANCE_TRANSACTIONS, $cashBalanceTransactionId, $params, $opts);
  301. }
  302. const PATH_SOURCES = '/sources';
  303. /**
  304. * @param string $id the ID of the customer on which to retrieve the payment sources
  305. * @param null|array $params
  306. * @param null|array|string $opts
  307. *
  308. * @throws \Stripe\Exception\ApiErrorException if the request fails
  309. *
  310. * @return \Stripe\Collection<\Stripe\BankAccount|\Stripe\Card|\Stripe\Source> the list of payment sources (BankAccount, Card or Source)
  311. */
  312. public static function allSources($id, $params = null, $opts = null)
  313. {
  314. return self::_allNestedResources($id, static::PATH_SOURCES, $params, $opts);
  315. }
  316. /**
  317. * @param string $id the ID of the customer on which to create the payment source
  318. * @param null|array $params
  319. * @param null|array|string $opts
  320. *
  321. * @throws \Stripe\Exception\ApiErrorException if the request fails
  322. *
  323. * @return \Stripe\BankAccount|\Stripe\Card|\Stripe\Source
  324. */
  325. public static function createSource($id, $params = null, $opts = null)
  326. {
  327. return self::_createNestedResource($id, static::PATH_SOURCES, $params, $opts);
  328. }
  329. /**
  330. * @param string $id the ID of the customer to which the payment source belongs
  331. * @param string $sourceId the ID of the payment source to delete
  332. * @param null|array $params
  333. * @param null|array|string $opts
  334. *
  335. * @throws \Stripe\Exception\ApiErrorException if the request fails
  336. *
  337. * @return \Stripe\BankAccount|\Stripe\Card|\Stripe\Source
  338. */
  339. public static function deleteSource($id, $sourceId, $params = null, $opts = null)
  340. {
  341. return self::_deleteNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
  342. }
  343. /**
  344. * @param string $id the ID of the customer to which the payment source belongs
  345. * @param string $sourceId the ID of the payment source to retrieve
  346. * @param null|array $params
  347. * @param null|array|string $opts
  348. *
  349. * @throws \Stripe\Exception\ApiErrorException if the request fails
  350. *
  351. * @return \Stripe\BankAccount|\Stripe\Card|\Stripe\Source
  352. */
  353. public static function retrieveSource($id, $sourceId, $params = null, $opts = null)
  354. {
  355. return self::_retrieveNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
  356. }
  357. /**
  358. * @param string $id the ID of the customer to which the payment source belongs
  359. * @param string $sourceId the ID of the payment source to update
  360. * @param null|array $params
  361. * @param null|array|string $opts
  362. *
  363. * @throws \Stripe\Exception\ApiErrorException if the request fails
  364. *
  365. * @return \Stripe\BankAccount|\Stripe\Card|\Stripe\Source
  366. */
  367. public static function updateSource($id, $sourceId, $params = null, $opts = null)
  368. {
  369. return self::_updateNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
  370. }
  371. const PATH_CASH_BALANCE = '/cash_balance';
  372. /**
  373. * @param string $id the ID of the customer to which the cash balance belongs
  374. * @param null|array $params
  375. * @param null|array|string $opts
  376. * @param mixed $cashBalanceId
  377. *
  378. * @throws \Stripe\Exception\ApiErrorException if the request fails
  379. *
  380. * @return \Stripe\CashBalance
  381. */
  382. public static function retrieveCashBalance($id, $cashBalanceId, $params = null, $opts = null)
  383. {
  384. return self::_retrieveNestedResource($id, static::PATH_CASH_BALANCE, $params, $opts);
  385. }
  386. /**
  387. * @param string $id the ID of the customer to which the cash balance belongs
  388. * @param null|array $params
  389. * @param null|array|string $opts
  390. * @param mixed $cashBalanceId
  391. *
  392. * @throws \Stripe\Exception\ApiErrorException if the request fails
  393. *
  394. * @return \Stripe\CashBalance
  395. */
  396. public static function updateCashBalance($id, $cashBalanceId, $params = null, $opts = null)
  397. {
  398. return self::_updateNestedResource($id, static::PATH_CASH_BALANCE, $params, $opts);
  399. }
  400. const PATH_TAX_IDS = '/tax_ids';
  401. /**
  402. * @param string $id the ID of the customer on which to retrieve the tax ids
  403. * @param null|array $params
  404. * @param null|array|string $opts
  405. *
  406. * @throws \Stripe\Exception\ApiErrorException if the request fails
  407. *
  408. * @return \Stripe\Collection<\Stripe\TaxId> the list of tax ids
  409. */
  410. public static function allTaxIds($id, $params = null, $opts = null)
  411. {
  412. return self::_allNestedResources($id, static::PATH_TAX_IDS, $params, $opts);
  413. }
  414. /**
  415. * @param string $id the ID of the customer on which to create the tax id
  416. * @param null|array $params
  417. * @param null|array|string $opts
  418. *
  419. * @throws \Stripe\Exception\ApiErrorException if the request fails
  420. *
  421. * @return \Stripe\TaxId
  422. */
  423. public static function createTaxId($id, $params = null, $opts = null)
  424. {
  425. return self::_createNestedResource($id, static::PATH_TAX_IDS, $params, $opts);
  426. }
  427. /**
  428. * @param string $id the ID of the customer to which the tax id belongs
  429. * @param string $taxIdId the ID of the tax id to delete
  430. * @param null|array $params
  431. * @param null|array|string $opts
  432. *
  433. * @throws \Stripe\Exception\ApiErrorException if the request fails
  434. *
  435. * @return \Stripe\TaxId
  436. */
  437. public static function deleteTaxId($id, $taxIdId, $params = null, $opts = null)
  438. {
  439. return self::_deleteNestedResource($id, static::PATH_TAX_IDS, $taxIdId, $params, $opts);
  440. }
  441. /**
  442. * @param string $id the ID of the customer to which the tax id belongs
  443. * @param string $taxIdId the ID of the tax id to retrieve
  444. * @param null|array $params
  445. * @param null|array|string $opts
  446. *
  447. * @throws \Stripe\Exception\ApiErrorException if the request fails
  448. *
  449. * @return \Stripe\TaxId
  450. */
  451. public static function retrieveTaxId($id, $taxIdId, $params = null, $opts = null)
  452. {
  453. return self::_retrieveNestedResource($id, static::PATH_TAX_IDS, $taxIdId, $params, $opts);
  454. }
  455. }