Coupon.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * A coupon contains information about a percent-off or amount-off discount you
  6. * might want to apply to a customer. Coupons may be applied to <a href="https://stripe.com/docs/api#subscriptions">subscriptions</a>, <a href="https://stripe.com/docs/api#invoices">invoices</a>,
  7. * <a href="https://stripe.com/docs/api/checkout/sessions">checkout sessions</a>, <a href="https://stripe.com/docs/api#quotes">quotes</a>, and more. Coupons do not work with conventional one-off <a href="https://stripe.com/docs/api#create_charge">charges</a> or <a href="https://stripe.com/docs/api/payment_intents">payment intents</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|int $amount_off Amount (in the <code>currency</code> specified) that will be taken off the subtotal of any invoices for this customer.
  12. * @property null|\Stripe\StripeObject $applies_to
  13. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  14. * @property null|string $currency If <code>amount_off</code> has been set, the three-letter <a href="https://stripe.com/docs/currencies">ISO code for the currency</a> of the amount to take off.
  15. * @property null|\Stripe\StripeObject $currency_options Coupons defined in each available currency option. Each key must be a three-letter <a href="https://www.iso.org/iso-4217-currency-codes.html">ISO currency code</a> and a <a href="https://stripe.com/docs/currencies">supported currency</a>.
  16. * @property string $duration One of <code>forever</code>, <code>once</code>, and <code>repeating</code>. Describes how long a customer who applies this coupon will get the discount.
  17. * @property null|int $duration_in_months If <code>duration</code> is <code>repeating</code>, the number of months the coupon applies. Null if coupon <code>duration</code> is <code>forever</code> or <code>once</code>.
  18. * @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.
  19. * @property null|int $max_redemptions Maximum number of times this coupon can be redeemed, in total, across all customers, before it is no longer valid.
  20. * @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.
  21. * @property null|string $name Name of the coupon displayed to customers on for instance invoices or receipts.
  22. * @property null|float $percent_off Percent that will be taken off the subtotal of any invoices for this customer for the duration of the coupon. For example, a coupon with percent_off of 50 will make a $ (or local equivalent)100 invoice $ (or local equivalent)50 instead.
  23. * @property null|int $redeem_by Date after which the coupon can no longer be redeemed.
  24. * @property int $times_redeemed Number of times this coupon has been applied to a customer.
  25. * @property bool $valid Taking account of the above properties, whether this coupon can still be applied to a customer.
  26. */
  27. class Coupon extends ApiResource
  28. {
  29. const OBJECT_NAME = 'coupon';
  30. use ApiOperations\Update;
  31. const DURATION_FOREVER = 'forever';
  32. const DURATION_ONCE = 'once';
  33. const DURATION_REPEATING = 'repeating';
  34. /**
  35. * You can create coupons easily via the <a
  36. * href="https://dashboard.stripe.com/coupons">coupon management</a> page of the
  37. * Stripe dashboard. Coupon creation is also accessible via the API if you need to
  38. * create coupons on the fly.
  39. *
  40. * A coupon has either a <code>percent_off</code> or an <code>amount_off</code> and
  41. * <code>currency</code>. If you set an <code>amount_off</code>, that amount will
  42. * be subtracted from any invoice’s subtotal. For example, an invoice with a
  43. * subtotal of <currency>100</currency> will have a final total of
  44. * <currency>0</currency> if a coupon with an <code>amount_off</code> of
  45. * <amount>200</amount> is applied to it and an invoice with a subtotal of
  46. * <currency>300</currency> will have a final total of <currency>100</currency> if
  47. * a coupon with an <code>amount_off</code> of <amount>200</amount> is applied to
  48. * it.
  49. *
  50. * @param null|array $params
  51. * @param null|array|string $options
  52. *
  53. * @throws \Stripe\Exception\ApiErrorException if the request fails
  54. *
  55. * @return \Stripe\Coupon the created resource
  56. */
  57. public static function create($params = null, $options = null)
  58. {
  59. self::_validateParams($params);
  60. $url = static::classUrl();
  61. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  62. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  63. $obj->setLastResponse($response);
  64. return $obj;
  65. }
  66. /**
  67. * You can delete coupons via the <a
  68. * href="https://dashboard.stripe.com/coupons">coupon management</a> page of the
  69. * Stripe dashboard. However, deleting a coupon does not affect any customers who
  70. * have already applied the coupon; it means that new customers can’t redeem the
  71. * coupon. You can also delete coupons via the API.
  72. *
  73. * @param null|array $params
  74. * @param null|array|string $opts
  75. *
  76. * @throws \Stripe\Exception\ApiErrorException if the request fails
  77. *
  78. * @return \Stripe\Coupon the deleted resource
  79. */
  80. public function delete($params = null, $opts = null)
  81. {
  82. self::_validateParams($params);
  83. $url = $this->instanceUrl();
  84. list($response, $opts) = $this->_request('delete', $url, $params, $opts);
  85. $this->refreshFrom($response, $opts);
  86. return $this;
  87. }
  88. /**
  89. * Returns a list of your coupons.
  90. *
  91. * @param null|array $params
  92. * @param null|array|string $opts
  93. *
  94. * @throws \Stripe\Exception\ApiErrorException if the request fails
  95. *
  96. * @return \Stripe\Collection<\Stripe\Coupon> of ApiResources
  97. */
  98. public static function all($params = null, $opts = null)
  99. {
  100. $url = static::classUrl();
  101. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  102. }
  103. /**
  104. * Retrieves the coupon with the given ID.
  105. *
  106. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  107. * @param null|array|string $opts
  108. *
  109. * @throws \Stripe\Exception\ApiErrorException if the request fails
  110. *
  111. * @return \Stripe\Coupon
  112. */
  113. public static function retrieve($id, $opts = null)
  114. {
  115. $opts = \Stripe\Util\RequestOptions::parse($opts);
  116. $instance = new static($id, $opts);
  117. $instance->refresh();
  118. return $instance;
  119. }
  120. /**
  121. * Updates the metadata of a coupon. Other coupon details (currency, duration,
  122. * amount_off) are, by design, not editable.
  123. *
  124. * @param string $id the ID of the resource to update
  125. * @param null|array $params
  126. * @param null|array|string $opts
  127. *
  128. * @throws \Stripe\Exception\ApiErrorException if the request fails
  129. *
  130. * @return \Stripe\Coupon the updated resource
  131. */
  132. public static function update($id, $params = null, $opts = null)
  133. {
  134. self::_validateParams($params);
  135. $url = static::resourceUrl($id);
  136. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  137. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  138. $obj->setLastResponse($response);
  139. return $obj;
  140. }
  141. }