SubscriptionService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe\Service;
  4. /**
  5. * @phpstan-import-type RequestOptionsArray from \Stripe\Util\RequestOptions
  6. * @psalm-import-type RequestOptionsArray from \Stripe\Util\RequestOptions
  7. */
  8. class SubscriptionService extends \Stripe\Service\AbstractService
  9. {
  10. /**
  11. * By default, returns a list of subscriptions that have not been canceled. In
  12. * order to list canceled subscriptions, specify <code>status=canceled</code>.
  13. *
  14. * @param null|array $params
  15. * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts
  16. *
  17. * @throws \Stripe\Exception\ApiErrorException if the request fails
  18. *
  19. * @return \Stripe\Collection<\Stripe\Subscription>
  20. */
  21. public function all($params = null, $opts = null)
  22. {
  23. return $this->requestCollection('get', '/v1/subscriptions', $params, $opts);
  24. }
  25. /**
  26. * Cancels a customer’s subscription immediately. The customer will not be charged
  27. * again for the subscription.
  28. *
  29. * Note, however, that any pending invoice items that you’ve created will still be
  30. * charged for at the end of the period, unless manually <a
  31. * href="#delete_invoiceitem">deleted</a>. If you’ve set the subscription to cancel
  32. * at the end of the period, any pending prorations will also be left in place and
  33. * collected at the end of the period. But if the subscription is set to cancel
  34. * immediately, pending prorations will be removed.
  35. *
  36. * By default, upon subscription cancellation, Stripe will stop automatic
  37. * collection of all finalized invoices for the customer. This is intended to
  38. * prevent unexpected payment attempts after the customer has canceled a
  39. * subscription. However, you can resume automatic collection of the invoices
  40. * manually after subscription cancellation to have us proceed. Or, you could check
  41. * for unpaid invoices before allowing the customer to cancel the subscription at
  42. * all.
  43. *
  44. * @param string $id
  45. * @param null|array $params
  46. * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts
  47. *
  48. * @throws \Stripe\Exception\ApiErrorException if the request fails
  49. *
  50. * @return \Stripe\Subscription
  51. */
  52. public function cancel($id, $params = null, $opts = null)
  53. {
  54. return $this->request('delete', $this->buildPath('/v1/subscriptions/%s', $id), $params, $opts);
  55. }
  56. /**
  57. * Creates a new subscription on an existing customer. Each customer can have up to
  58. * 500 active or scheduled subscriptions.
  59. *
  60. * When you create a subscription with
  61. * <code>collection_method=charge_automatically</code>, the first invoice is
  62. * finalized as part of the request. The <code>payment_behavior</code> parameter
  63. * determines the exact behavior of the initial payment.
  64. *
  65. * To start subscriptions where the first invoice always begins in a
  66. * <code>draft</code> status, use <a
  67. * href="/docs/billing/subscriptions/subscription-schedules#managing">subscription
  68. * schedules</a> instead. Schedules provide the flexibility to model more complex
  69. * billing configurations that change over time.
  70. *
  71. * @param null|array $params
  72. * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts
  73. *
  74. * @throws \Stripe\Exception\ApiErrorException if the request fails
  75. *
  76. * @return \Stripe\Subscription
  77. */
  78. public function create($params = null, $opts = null)
  79. {
  80. return $this->request('post', '/v1/subscriptions', $params, $opts);
  81. }
  82. /**
  83. * Removes the currently applied discount on a subscription.
  84. *
  85. * @param string $id
  86. * @param null|array $params
  87. * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts
  88. *
  89. * @throws \Stripe\Exception\ApiErrorException if the request fails
  90. *
  91. * @return \Stripe\Discount
  92. */
  93. public function deleteDiscount($id, $params = null, $opts = null)
  94. {
  95. return $this->request('delete', $this->buildPath('/v1/subscriptions/%s/discount', $id), $params, $opts);
  96. }
  97. /**
  98. * Initiates resumption of a paused subscription, optionally resetting the billing
  99. * cycle anchor and creating prorations. If a resumption invoice is generated, it
  100. * must be paid or marked uncollectible before the subscription will be unpaused.
  101. * If payment succeeds the subscription will become <code>active</code>, and if
  102. * payment fails the subscription will be <code>past_due</code>. The resumption
  103. * invoice will void automatically if not paid by the expiration date.
  104. *
  105. * @param string $id
  106. * @param null|array $params
  107. * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts
  108. *
  109. * @throws \Stripe\Exception\ApiErrorException if the request fails
  110. *
  111. * @return \Stripe\Subscription
  112. */
  113. public function resume($id, $params = null, $opts = null)
  114. {
  115. return $this->request('post', $this->buildPath('/v1/subscriptions/%s/resume', $id), $params, $opts);
  116. }
  117. /**
  118. * Retrieves the subscription with the given ID.
  119. *
  120. * @param string $id
  121. * @param null|array $params
  122. * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts
  123. *
  124. * @throws \Stripe\Exception\ApiErrorException if the request fails
  125. *
  126. * @return \Stripe\Subscription
  127. */
  128. public function retrieve($id, $params = null, $opts = null)
  129. {
  130. return $this->request('get', $this->buildPath('/v1/subscriptions/%s', $id), $params, $opts);
  131. }
  132. /**
  133. * Search for subscriptions you’ve previously created using Stripe’s <a
  134. * href="/docs/search#search-query-language">Search Query Language</a>. Don’t use
  135. * search in read-after-write flows where strict consistency is necessary. Under
  136. * normal operating conditions, data is searchable in less than a minute.
  137. * Occasionally, propagation of new or updated data can be up to an hour behind
  138. * during outages. Search functionality is not available to merchants in India.
  139. *
  140. * @param null|array $params
  141. * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts
  142. *
  143. * @throws \Stripe\Exception\ApiErrorException if the request fails
  144. *
  145. * @return \Stripe\SearchResult<\Stripe\Subscription>
  146. */
  147. public function search($params = null, $opts = null)
  148. {
  149. return $this->requestSearchResult('get', '/v1/subscriptions/search', $params, $opts);
  150. }
  151. /**
  152. * Updates an existing subscription to match the specified parameters. When
  153. * changing prices or quantities, we optionally prorate the price we charge next
  154. * month to make up for any price changes. To preview how the proration is
  155. * calculated, use the <a href="/docs/api/invoices/upcoming">upcoming invoice</a>
  156. * endpoint.
  157. *
  158. * By default, we prorate subscription changes. For example, if a customer signs up
  159. * on May 1 for a <currency>100</currency> price, they’ll be billed
  160. * <currency>100</currency> immediately. If on May 15 they switch to a
  161. * <currency>200</currency> price, then on June 1 they’ll be billed
  162. * <currency>250</currency> (<currency>200</currency> for a renewal of her
  163. * subscription, plus a <currency>50</currency> prorating adjustment for half of
  164. * the previous month’s <currency>100</currency> difference). Similarly, a
  165. * downgrade generates a credit that is applied to the next invoice. We also
  166. * prorate when you make quantity changes.
  167. *
  168. * Switching prices does not normally change the billing date or generate an
  169. * immediate charge unless:
  170. *
  171. * <ul> <li>The billing interval is changed (for example, from monthly to
  172. * yearly).</li> <li>The subscription moves from free to paid, or paid to
  173. * free.</li> <li>A trial starts or ends.</li> </ul>
  174. *
  175. * In these cases, we apply a credit for the unused time on the previous price,
  176. * immediately charge the customer using the new price, and reset the billing date.
  177. *
  178. * If you want to charge for an upgrade immediately, pass
  179. * <code>proration_behavior</code> as <code>always_invoice</code> to create
  180. * prorations, automatically invoice the customer for those proration adjustments,
  181. * and attempt to collect payment. If you pass <code>create_prorations</code>, the
  182. * prorations are created but not automatically invoiced. If you want to bill the
  183. * customer for the prorations before the subscription’s renewal date, you need to
  184. * manually <a href="/docs/api/invoices/create">invoice the customer</a>.
  185. *
  186. * If you don’t want to prorate, set the <code>proration_behavior</code> option to
  187. * <code>none</code>. With this option, the customer is billed
  188. * <currency>100</currency> on May 1 and <currency>200</currency> on June 1.
  189. * Similarly, if you set <code>proration_behavior</code> to <code>none</code> when
  190. * switching between different billing intervals (for example, from monthly to
  191. * yearly), we don’t generate any credits for the old subscription’s unused time.
  192. * We still reset the billing date and bill immediately for the new subscription.
  193. *
  194. * Updating the quantity on a subscription many times in an hour may result in <a
  195. * href="/docs/rate-limits">rate limiting</a>. If you need to bill for a frequently
  196. * changing quantity, consider integrating <a
  197. * href="/docs/billing/subscriptions/usage-based">usage-based billing</a> instead.
  198. *
  199. * @param string $id
  200. * @param null|array $params
  201. * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts
  202. *
  203. * @throws \Stripe\Exception\ApiErrorException if the request fails
  204. *
  205. * @return \Stripe\Subscription
  206. */
  207. public function update($id, $params = null, $opts = null)
  208. {
  209. return $this->request('post', $this->buildPath('/v1/subscriptions/%s', $id), $params, $opts);
  210. }
  211. }