Plan.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * 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.
  6. *
  7. * Plans define the base price, currency, and billing cycle for recurring purchases of products.
  8. * <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.
  9. *
  10. * For example, you might have a single &quot;gold&quot; product that has plans for $10/month, $100/year, €9/month, and €90/year.
  11. *
  12. * 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>.
  13. *
  14. * @property string $id Unique identifier for the object.
  15. * @property string $object String representing the object's type. Objects of the same type share the same value.
  16. * @property bool $active Whether the plan can be used for new purchases.
  17. * @property null|string $aggregate_usage Specifies a usage aggregation strategy for plans of <code>usage_type=metered</code>. Allowed values are <code>sum</code> for summing up all usage during a period, <code>last_during_period</code> for using the last usage record reported within a period, <code>last_ever</code> for using the last usage record ever (across period bounds) or <code>max</code> which uses the usage record with the maximum reported usage during a period. Defaults to <code>sum</code>.
  18. * @property null|int $amount The unit amount in cents (or local equivalent) to be charged, represented as a whole integer if possible. Only set if <code>billing_scheme=per_unit</code>.
  19. * @property null|string $amount_decimal The unit amount in cents (or local equivalent) to be charged, represented as a decimal string with at most 12 decimal places. Only set if <code>billing_scheme=per_unit</code>.
  20. * @property string $billing_scheme Describes how to compute the price per period. Either <code>per_unit</code> or <code>tiered</code>. <code>per_unit</code> indicates that the fixed amount (specified in <code>amount</code>) will be charged per unit in <code>quantity</code> (for plans with <code>usage_type=licensed</code>), or per unit of total usage (for plans with <code>usage_type=metered</code>). <code>tiered</code> indicates that the unit pricing will be computed using a tiering strategy as defined using the <code>tiers</code> and <code>tiers_mode</code> attributes.
  21. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  22. * @property string $currency Three-letter <a href="https://www.iso.org/iso-4217-currency-codes.html">ISO currency code</a>, in lowercase. Must be a <a href="https://stripe.com/docs/currencies">supported currency</a>.
  23. * @property string $interval The frequency at which a subscription is billed. One of <code>day</code>, <code>week</code>, <code>month</code> or <code>year</code>.
  24. * @property int $interval_count The number of intervals (specified in the <code>interval</code> attribute) between subscription billings. For example, <code>interval=month</code> and <code>interval_count=3</code> bills every 3 months.
  25. * @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.
  26. * @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.
  27. * @property null|string $meter The meter tracking the usage of a metered price
  28. * @property null|string $nickname A brief description of the plan, hidden from customers.
  29. * @property null|string|\Stripe\Product $product The product whose pricing this plan determines.
  30. * @property null|\Stripe\StripeObject[] $tiers Each element represents a pricing tier. This parameter requires <code>billing_scheme</code> to be set to <code>tiered</code>. See also the documentation for <code>billing_scheme</code>.
  31. * @property null|string $tiers_mode Defines if the tiering price should be <code>graduated</code> or <code>volume</code> based. In <code>volume</code>-based tiering, the maximum quantity within a period determines the per unit price. In <code>graduated</code> tiering, pricing can change as the quantity grows.
  32. * @property null|\Stripe\StripeObject $transform_usage Apply a transformation to the reported usage or set quantity before computing the amount billed. Cannot be combined with <code>tiers</code>.
  33. * @property null|int $trial_period_days Default number of trial days when subscribing a customer to this plan using <a href="https://stripe.com/docs/api#create_subscription-trial_from_plan"><code>trial_from_plan=true</code></a>.
  34. * @property string $usage_type Configures how the quantity per period should be determined. Can be either <code>metered</code> or <code>licensed</code>. <code>licensed</code> automatically bills the <code>quantity</code> set when adding it to a subscription. <code>metered</code> aggregates the total usage based on usage records. Defaults to <code>licensed</code>.
  35. */
  36. class Plan extends ApiResource
  37. {
  38. const OBJECT_NAME = 'plan';
  39. use ApiOperations\Update;
  40. const AGGREGATE_USAGE_LAST_DURING_PERIOD = 'last_during_period';
  41. const AGGREGATE_USAGE_LAST_EVER = 'last_ever';
  42. const AGGREGATE_USAGE_MAX = 'max';
  43. const AGGREGATE_USAGE_SUM = 'sum';
  44. const BILLING_SCHEME_PER_UNIT = 'per_unit';
  45. const BILLING_SCHEME_TIERED = 'tiered';
  46. const INTERVAL_DAY = 'day';
  47. const INTERVAL_MONTH = 'month';
  48. const INTERVAL_WEEK = 'week';
  49. const INTERVAL_YEAR = 'year';
  50. const TIERS_MODE_GRADUATED = 'graduated';
  51. const TIERS_MODE_VOLUME = 'volume';
  52. const USAGE_TYPE_LICENSED = 'licensed';
  53. const USAGE_TYPE_METERED = 'metered';
  54. /**
  55. * You can now model subscriptions more flexibly using the <a href="#prices">Prices
  56. * API</a>. It replaces the Plans API and is backwards compatible to simplify your
  57. * migration.
  58. *
  59. * @param null|array $params
  60. * @param null|array|string $options
  61. *
  62. * @throws \Stripe\Exception\ApiErrorException if the request fails
  63. *
  64. * @return \Stripe\Plan the created resource
  65. */
  66. public static function create($params = null, $options = null)
  67. {
  68. self::_validateParams($params);
  69. $url = static::classUrl();
  70. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  71. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  72. $obj->setLastResponse($response);
  73. return $obj;
  74. }
  75. /**
  76. * Deleting plans means new subscribers can’t be added. Existing subscribers aren’t
  77. * affected.
  78. *
  79. * @param null|array $params
  80. * @param null|array|string $opts
  81. *
  82. * @throws \Stripe\Exception\ApiErrorException if the request fails
  83. *
  84. * @return \Stripe\Plan the deleted resource
  85. */
  86. public function delete($params = null, $opts = null)
  87. {
  88. self::_validateParams($params);
  89. $url = $this->instanceUrl();
  90. list($response, $opts) = $this->_request('delete', $url, $params, $opts);
  91. $this->refreshFrom($response, $opts);
  92. return $this;
  93. }
  94. /**
  95. * Returns a list of your plans.
  96. *
  97. * @param null|array $params
  98. * @param null|array|string $opts
  99. *
  100. * @throws \Stripe\Exception\ApiErrorException if the request fails
  101. *
  102. * @return \Stripe\Collection<\Stripe\Plan> of ApiResources
  103. */
  104. public static function all($params = null, $opts = null)
  105. {
  106. $url = static::classUrl();
  107. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  108. }
  109. /**
  110. * Retrieves the plan with the given ID.
  111. *
  112. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  113. * @param null|array|string $opts
  114. *
  115. * @throws \Stripe\Exception\ApiErrorException if the request fails
  116. *
  117. * @return \Stripe\Plan
  118. */
  119. public static function retrieve($id, $opts = null)
  120. {
  121. $opts = \Stripe\Util\RequestOptions::parse($opts);
  122. $instance = new static($id, $opts);
  123. $instance->refresh();
  124. return $instance;
  125. }
  126. /**
  127. * Updates the specified plan by setting the values of the parameters passed. Any
  128. * parameters not provided are left unchanged. By design, you cannot change a
  129. * plan’s ID, amount, currency, or billing cycle.
  130. *
  131. * @param string $id the ID of the resource to update
  132. * @param null|array $params
  133. * @param null|array|string $opts
  134. *
  135. * @throws \Stripe\Exception\ApiErrorException if the request fails
  136. *
  137. * @return \Stripe\Plan the updated resource
  138. */
  139. public static function update($id, $params = null, $opts = null)
  140. {
  141. self::_validateParams($params);
  142. $url = static::resourceUrl($id);
  143. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  144. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  145. $obj->setLastResponse($response);
  146. return $obj;
  147. }
  148. }