PromotionCode.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * A Promotion Code represents a customer-redeemable code for a <a href="https://stripe.com/docs/api#coupons">coupon</a>. It can be used to
  6. * create multiple codes for a single coupon.
  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 bool $active Whether the promotion code is currently active. A promotion code is only active if the coupon is also valid.
  11. * @property string $code The customer-facing code. Regardless of case, this code must be unique across all active promotion codes for each customer.
  12. * @property \Stripe\Coupon $coupon A coupon contains information about a percent-off or amount-off discount you 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>, <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>.
  13. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  14. * @property null|string|\Stripe\Customer $customer The customer that this promotion code can be used by.
  15. * @property null|int $expires_at Date at which the promotion code can no longer be redeemed.
  16. * @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.
  17. * @property null|int $max_redemptions Maximum number of times this promotion code can be redeemed.
  18. * @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.
  19. * @property \Stripe\StripeObject $restrictions
  20. * @property int $times_redeemed Number of times this promotion code has been used.
  21. */
  22. class PromotionCode extends ApiResource
  23. {
  24. const OBJECT_NAME = 'promotion_code';
  25. use ApiOperations\Update;
  26. /**
  27. * A promotion code points to a coupon. You can optionally restrict the code to a
  28. * specific customer, redemption limit, and expiration date.
  29. *
  30. * @param null|array $params
  31. * @param null|array|string $options
  32. *
  33. * @throws \Stripe\Exception\ApiErrorException if the request fails
  34. *
  35. * @return \Stripe\PromotionCode the created resource
  36. */
  37. public static function create($params = null, $options = null)
  38. {
  39. self::_validateParams($params);
  40. $url = static::classUrl();
  41. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  42. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  43. $obj->setLastResponse($response);
  44. return $obj;
  45. }
  46. /**
  47. * Returns a list of your promotion codes.
  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\Collection<\Stripe\PromotionCode> of ApiResources
  55. */
  56. public static function all($params = null, $opts = null)
  57. {
  58. $url = static::classUrl();
  59. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  60. }
  61. /**
  62. * Retrieves the promotion code with the given ID. In order to retrieve a promotion
  63. * code by the customer-facing <code>code</code> use <a
  64. * href="/docs/api/promotion_codes/list">list</a> with the desired
  65. * <code>code</code>.
  66. *
  67. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  68. * @param null|array|string $opts
  69. *
  70. * @throws \Stripe\Exception\ApiErrorException if the request fails
  71. *
  72. * @return \Stripe\PromotionCode
  73. */
  74. public static function retrieve($id, $opts = null)
  75. {
  76. $opts = \Stripe\Util\RequestOptions::parse($opts);
  77. $instance = new static($id, $opts);
  78. $instance->refresh();
  79. return $instance;
  80. }
  81. /**
  82. * Updates the specified promotion code by setting the values of the parameters
  83. * passed. Most fields are, by design, not editable.
  84. *
  85. * @param string $id the ID of the resource to update
  86. * @param null|array $params
  87. * @param null|array|string $opts
  88. *
  89. * @throws \Stripe\Exception\ApiErrorException if the request fails
  90. *
  91. * @return \Stripe\PromotionCode the updated resource
  92. */
  93. public static function update($id, $params = null, $opts = null)
  94. {
  95. self::_validateParams($params);
  96. $url = static::resourceUrl($id);
  97. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  98. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  99. $obj->setLastResponse($response);
  100. return $obj;
  101. }
  102. }