Product.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * Products describe the specific goods or services you offer to your customers.
  6. * For example, you might offer a Standard and Premium version of your goods or service; each version would be a separate Product.
  7. * They can be used in conjunction with <a href="https://stripe.com/docs/api#prices">Prices</a> to configure pricing in Payment Links, Checkout, and Subscriptions.
  8. *
  9. * Related guides: <a href="https://stripe.com/docs/billing/subscriptions/set-up-subscription">Set up a subscription</a>,
  10. * <a href="https://stripe.com/docs/payment-links">share a Payment Link</a>,
  11. * <a href="https://stripe.com/docs/payments/accept-a-payment#create-product-prices-upfront">accept payments with Checkout</a>,
  12. * 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 product is currently available for purchase.
  17. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  18. * @property null|string|\Stripe\Price $default_price The ID of the <a href="https://stripe.com/docs/api/prices">Price</a> object that is the default price for this product.
  19. * @property null|string $description The product's description, meant to be displayable to the customer. Use this field to optionally store a long form explanation of the product being sold for your own rendering purposes.
  20. * @property string[] $images A list of up to 8 URLs of images for this product, meant to be displayable to the customer.
  21. * @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.
  22. * @property \Stripe\StripeObject[] $marketing_features A list of up to 15 marketing features for this product. These are displayed in <a href="https://stripe.com/docs/payments/checkout/pricing-table">pricing tables</a>.
  23. * @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.
  24. * @property string $name The product's name, meant to be displayable to the customer.
  25. * @property null|\Stripe\StripeObject $package_dimensions The dimensions of this product for shipping purposes.
  26. * @property null|bool $shippable Whether this product is shipped (i.e., physical goods).
  27. * @property null|string $statement_descriptor Extra information about a product which will appear on your customer's credit card statement. In the case that multiple products are billed at once, the first statement descriptor will be used. Only used for subscription payments.
  28. * @property null|string|\Stripe\TaxCode $tax_code A <a href="https://stripe.com/docs/tax/tax-categories">tax code</a> ID.
  29. * @property string $type The type of the product. The product is either of type <code>good</code>, which is eligible for use with Orders and SKUs, or <code>service</code>, which is eligible for use with Subscriptions and Plans.
  30. * @property null|string $unit_label A label that represents units of this product. When set, this will be included in customers' receipts, invoices, Checkout, and the customer portal.
  31. * @property int $updated Time at which the object was last updated. Measured in seconds since the Unix epoch.
  32. * @property null|string $url A URL of a publicly-accessible webpage for this product.
  33. */
  34. class Product extends ApiResource
  35. {
  36. const OBJECT_NAME = 'product';
  37. use ApiOperations\NestedResource;
  38. use ApiOperations\Update;
  39. const TYPE_GOOD = 'good';
  40. const TYPE_SERVICE = 'service';
  41. /**
  42. * Creates a new product object.
  43. *
  44. * @param null|array $params
  45. * @param null|array|string $options
  46. *
  47. * @throws \Stripe\Exception\ApiErrorException if the request fails
  48. *
  49. * @return \Stripe\Product the created resource
  50. */
  51. public static function create($params = null, $options = null)
  52. {
  53. self::_validateParams($params);
  54. $url = static::classUrl();
  55. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  56. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  57. $obj->setLastResponse($response);
  58. return $obj;
  59. }
  60. /**
  61. * Delete a product. Deleting a product is only possible if it has no prices
  62. * associated with it. Additionally, deleting a product with <code>type=good</code>
  63. * is only possible if it has no SKUs associated with it.
  64. *
  65. * @param null|array $params
  66. * @param null|array|string $opts
  67. *
  68. * @throws \Stripe\Exception\ApiErrorException if the request fails
  69. *
  70. * @return \Stripe\Product the deleted resource
  71. */
  72. public function delete($params = null, $opts = null)
  73. {
  74. self::_validateParams($params);
  75. $url = $this->instanceUrl();
  76. list($response, $opts) = $this->_request('delete', $url, $params, $opts);
  77. $this->refreshFrom($response, $opts);
  78. return $this;
  79. }
  80. /**
  81. * Returns a list of your products. The products are returned sorted by creation
  82. * date, with the most recently created products appearing first.
  83. *
  84. * @param null|array $params
  85. * @param null|array|string $opts
  86. *
  87. * @throws \Stripe\Exception\ApiErrorException if the request fails
  88. *
  89. * @return \Stripe\Collection<\Stripe\Product> of ApiResources
  90. */
  91. public static function all($params = null, $opts = null)
  92. {
  93. $url = static::classUrl();
  94. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  95. }
  96. /**
  97. * Retrieves the details of an existing product. Supply the unique product ID from
  98. * either a product creation request or the product list, and Stripe will return
  99. * the corresponding product information.
  100. *
  101. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  102. * @param null|array|string $opts
  103. *
  104. * @throws \Stripe\Exception\ApiErrorException if the request fails
  105. *
  106. * @return \Stripe\Product
  107. */
  108. public static function retrieve($id, $opts = null)
  109. {
  110. $opts = \Stripe\Util\RequestOptions::parse($opts);
  111. $instance = new static($id, $opts);
  112. $instance->refresh();
  113. return $instance;
  114. }
  115. /**
  116. * Updates the specific product by setting the values of the parameters passed. Any
  117. * parameters not provided will be left unchanged.
  118. *
  119. * @param string $id the ID of the resource to update
  120. * @param null|array $params
  121. * @param null|array|string $opts
  122. *
  123. * @throws \Stripe\Exception\ApiErrorException if the request fails
  124. *
  125. * @return \Stripe\Product the updated resource
  126. */
  127. public static function update($id, $params = null, $opts = null)
  128. {
  129. self::_validateParams($params);
  130. $url = static::resourceUrl($id);
  131. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  132. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  133. $obj->setLastResponse($response);
  134. return $obj;
  135. }
  136. /**
  137. * @param null|array $params
  138. * @param null|array|string $opts
  139. *
  140. * @throws \Stripe\Exception\ApiErrorException if the request fails
  141. *
  142. * @return \Stripe\SearchResult<\Stripe\Product> the product search results
  143. */
  144. public static function search($params = null, $opts = null)
  145. {
  146. $url = '/v1/products/search';
  147. return static::_requestPage($url, \Stripe\SearchResult::class, $params, $opts);
  148. }
  149. const PATH_FEATURES = '/features';
  150. /**
  151. * @param string $id the ID of the product on which to retrieve the product features
  152. * @param null|array $params
  153. * @param null|array|string $opts
  154. *
  155. * @throws \Stripe\Exception\ApiErrorException if the request fails
  156. *
  157. * @return \Stripe\Collection<\Stripe\ProductFeature> the list of product features
  158. */
  159. public static function allFeatures($id, $params = null, $opts = null)
  160. {
  161. return self::_allNestedResources($id, static::PATH_FEATURES, $params, $opts);
  162. }
  163. /**
  164. * @param string $id the ID of the product on which to create the product feature
  165. * @param null|array $params
  166. * @param null|array|string $opts
  167. *
  168. * @throws \Stripe\Exception\ApiErrorException if the request fails
  169. *
  170. * @return \Stripe\ProductFeature
  171. */
  172. public static function createFeature($id, $params = null, $opts = null)
  173. {
  174. return self::_createNestedResource($id, static::PATH_FEATURES, $params, $opts);
  175. }
  176. /**
  177. * @param string $id the ID of the product to which the product feature belongs
  178. * @param string $featureId the ID of the product feature to delete
  179. * @param null|array $params
  180. * @param null|array|string $opts
  181. *
  182. * @throws \Stripe\Exception\ApiErrorException if the request fails
  183. *
  184. * @return \Stripe\ProductFeature
  185. */
  186. public static function deleteFeature($id, $featureId, $params = null, $opts = null)
  187. {
  188. return self::_deleteNestedResource($id, static::PATH_FEATURES, $featureId, $params, $opts);
  189. }
  190. /**
  191. * @param string $id the ID of the product to which the product feature belongs
  192. * @param string $featureId the ID of the product feature to retrieve
  193. * @param null|array $params
  194. * @param null|array|string $opts
  195. *
  196. * @throws \Stripe\Exception\ApiErrorException if the request fails
  197. *
  198. * @return \Stripe\ProductFeature
  199. */
  200. public static function retrieveFeature($id, $featureId, $params = null, $opts = null)
  201. {
  202. return self::_retrieveNestedResource($id, static::PATH_FEATURES, $featureId, $params, $opts);
  203. }
  204. }