SubscriptionItem.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * Subscription items allow you to create customer subscriptions with more than
  6. * one plan, making it easy to represent complex billing relationships.
  7. *
  8. * @property string $id Unique identifier for the object.
  9. * @property string $object String representing the object's type. Objects of the same type share the same value.
  10. * @property null|\Stripe\StripeObject $billing_thresholds Define thresholds at which an invoice will be sent, and the related subscription advanced to a new billing period
  11. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  12. * @property (string|\Stripe\Discount)[] $discounts The discounts applied to the subscription item. Subscription item discounts are applied before subscription discounts. Use <code>expand[]=discounts</code> to expand each discount.
  13. * @property \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.
  14. * @property \Stripe\Plan $plan <p>You can now model subscriptions more flexibly using the <a href="https://stripe.com/docs/api#prices">Prices API</a>. It replaces the Plans API and is backwards compatible to simplify your migration.</p><p>Plans define the base price, currency, and billing cycle for recurring purchases of products. <a href="https://stripe.com/docs/api#products">Products</a> help you track inventory or provisioning, and plans help you track pricing. Different physical goods or levels of service should be represented by products, and pricing options should be represented by plans. This approach lets you change prices without having to change your provisioning scheme.</p><p>For example, you might have a single &quot;gold&quot; product that has plans for $10/month, $100/year, €9/month, and €90/year.</p><p>Related guides: <a href="https://stripe.com/docs/billing/subscriptions/set-up-subscription">Set up a subscription</a> and more about <a href="https://stripe.com/docs/products-prices/overview">products and prices</a>.</p>
  15. * @property \Stripe\Price $price <p>Prices define the unit cost, currency, and (optional) billing cycle for both recurring and one-time purchases of products. <a href="https://stripe.com/docs/api#products">Products</a> help you track inventory or provisioning, and prices help you track payment terms. Different physical goods or levels of service should be represented by products, and pricing options should be represented by prices. This approach lets you change prices without having to change your provisioning scheme.</p><p>For example, you might have a single &quot;gold&quot; product that has prices for $10/month, $100/year, and €9 once.</p><p>Related guides: <a href="https://stripe.com/docs/billing/subscriptions/set-up-subscription">Set up a subscription</a>, <a href="https://stripe.com/docs/billing/invoices/create">create an invoice</a>, and more about <a href="https://stripe.com/docs/products-prices/overview">products and prices</a>.</p>
  16. * @property null|int $quantity The <a href="https://stripe.com/docs/subscriptions/quantities">quantity</a> of the plan to which the customer should be subscribed.
  17. * @property string $subscription The <code>subscription</code> this <code>subscription_item</code> belongs to.
  18. * @property null|\Stripe\TaxRate[] $tax_rates The tax rates which apply to this <code>subscription_item</code>. When set, the <code>default_tax_rates</code> on the subscription do not apply to this <code>subscription_item</code>.
  19. */
  20. class SubscriptionItem extends ApiResource
  21. {
  22. const OBJECT_NAME = 'subscription_item';
  23. use ApiOperations\NestedResource;
  24. use ApiOperations\Update;
  25. /**
  26. * Adds a new item to an existing subscription. No existing items will be changed
  27. * or replaced.
  28. *
  29. * @param null|array $params
  30. * @param null|array|string $options
  31. *
  32. * @throws \Stripe\Exception\ApiErrorException if the request fails
  33. *
  34. * @return \Stripe\SubscriptionItem the created resource
  35. */
  36. public static function create($params = null, $options = null)
  37. {
  38. self::_validateParams($params);
  39. $url = static::classUrl();
  40. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  41. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  42. $obj->setLastResponse($response);
  43. return $obj;
  44. }
  45. /**
  46. * Deletes an item from the subscription. Removing a subscription item from a
  47. * subscription will not cancel the subscription.
  48. *
  49. * @param null|array $params
  50. * @param null|array|string $opts
  51. *
  52. * @throws \Stripe\Exception\ApiErrorException if the request fails
  53. *
  54. * @return \Stripe\SubscriptionItem the deleted resource
  55. */
  56. public function delete($params = null, $opts = null)
  57. {
  58. self::_validateParams($params);
  59. $url = $this->instanceUrl();
  60. list($response, $opts) = $this->_request('delete', $url, $params, $opts);
  61. $this->refreshFrom($response, $opts);
  62. return $this;
  63. }
  64. /**
  65. * Returns a list of your subscription items for a given subscription.
  66. *
  67. * @param null|array $params
  68. * @param null|array|string $opts
  69. *
  70. * @throws \Stripe\Exception\ApiErrorException if the request fails
  71. *
  72. * @return \Stripe\Collection<\Stripe\SubscriptionItem> of ApiResources
  73. */
  74. public static function all($params = null, $opts = null)
  75. {
  76. $url = static::classUrl();
  77. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  78. }
  79. /**
  80. * Retrieves the subscription item with the given ID.
  81. *
  82. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  83. * @param null|array|string $opts
  84. *
  85. * @throws \Stripe\Exception\ApiErrorException if the request fails
  86. *
  87. * @return \Stripe\SubscriptionItem
  88. */
  89. public static function retrieve($id, $opts = null)
  90. {
  91. $opts = \Stripe\Util\RequestOptions::parse($opts);
  92. $instance = new static($id, $opts);
  93. $instance->refresh();
  94. return $instance;
  95. }
  96. /**
  97. * Updates the plan or quantity of an item on a current subscription.
  98. *
  99. * @param string $id the ID of the resource to update
  100. * @param null|array $params
  101. * @param null|array|string $opts
  102. *
  103. * @throws \Stripe\Exception\ApiErrorException if the request fails
  104. *
  105. * @return \Stripe\SubscriptionItem the updated resource
  106. */
  107. public static function update($id, $params = null, $opts = null)
  108. {
  109. self::_validateParams($params);
  110. $url = static::resourceUrl($id);
  111. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  112. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  113. $obj->setLastResponse($response);
  114. return $obj;
  115. }
  116. const PATH_USAGE_RECORDS = '/usage_records';
  117. /**
  118. * @param string $id the ID of the subscription item on which to create the usage record
  119. * @param null|array $params
  120. * @param null|array|string $opts
  121. *
  122. * @throws \Stripe\Exception\ApiErrorException if the request fails
  123. *
  124. * @return \Stripe\UsageRecord
  125. */
  126. public static function createUsageRecord($id, $params = null, $opts = null)
  127. {
  128. return self::_createNestedResource($id, static::PATH_USAGE_RECORDS, $params, $opts);
  129. }
  130. const PATH_USAGE_RECORD_SUMMARIES = '/usage_record_summaries';
  131. /**
  132. * @param string $id the ID of the subscription item on which to retrieve the usage record summaries
  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\Collection<\Stripe\UsageRecordSummary> the list of usage record summaries
  139. */
  140. public static function allUsageRecordSummaries($id, $params = null, $opts = null)
  141. {
  142. return self::_allNestedResources($id, static::PATH_USAGE_RECORD_SUMMARIES, $params, $opts);
  143. }
  144. }