InvoiceItem.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * Invoice Items represent the component lines of an <a href="https://stripe.com/docs/api/invoices">invoice</a>. An invoice item is added to an
  6. * invoice by creating or updating it with an <code>invoice</code> field, at which point it will be included as
  7. * <a href="https://stripe.com/docs/api/invoices/line_item">an invoice line item</a> within
  8. * <a href="https://stripe.com/docs/api/invoices/object#invoice_object-lines">invoice.lines</a>.
  9. *
  10. * Invoice Items can be created before you are ready to actually send the invoice. This can be particularly useful when combined
  11. * with a <a href="https://stripe.com/docs/api/subscriptions">subscription</a>. Sometimes you want to add a charge or credit to a customer, but actually charge
  12. * or credit the customer’s card only at the end of a regular billing cycle. This is useful for combining several charges
  13. * (to minimize per-transaction fees), or for having Stripe tabulate your usage-based billing totals.
  14. *
  15. * Related guides: <a href="https://stripe.com/docs/invoicing/integration">Integrate with the Invoicing API</a>, <a href="https://stripe.com/docs/billing/invoices/subscription#adding-upcoming-invoice-items">Subscription Invoices</a>.
  16. *
  17. * @property string $id Unique identifier for the object.
  18. * @property string $object String representing the object's type. Objects of the same type share the same value.
  19. * @property int $amount Amount (in the <code>currency</code> specified) of the invoice item. This should always be equal to <code>unit_amount * quantity</code>.
  20. * @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>.
  21. * @property string|\Stripe\Customer $customer The ID of the customer who will be billed when this invoice item is billed.
  22. * @property int $date Time at which the object was created. Measured in seconds since the Unix epoch.
  23. * @property null|string $description An arbitrary string attached to the object. Often useful for displaying to users.
  24. * @property bool $discountable If true, discounts will apply to this invoice item. Always false for prorations.
  25. * @property null|(string|\Stripe\Discount)[] $discounts The discounts which apply to the invoice item. Item discounts are applied before invoice discounts. Use <code>expand[]=discounts</code> to expand each discount.
  26. * @property null|string|\Stripe\Invoice $invoice The ID of the invoice this invoice item belongs to.
  27. * @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.
  28. * @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.
  29. * @property \Stripe\StripeObject $period
  30. * @property null|\Stripe\Plan $plan If the invoice item is a proration, the plan of the subscription that the proration was computed for.
  31. * @property null|\Stripe\Price $price The price of the invoice item.
  32. * @property bool $proration Whether the invoice item was created automatically as a proration adjustment when the customer switched plans.
  33. * @property int $quantity Quantity of units for the invoice item. If the invoice item is a proration, the quantity of the subscription that the proration was computed for.
  34. * @property null|string|\Stripe\Subscription $subscription The subscription that this invoice item has been created for, if any.
  35. * @property null|string $subscription_item The subscription item that this invoice item has been created for, if any.
  36. * @property null|\Stripe\TaxRate[] $tax_rates The tax rates which apply to the invoice item. When set, the <code>default_tax_rates</code> on the invoice do not apply to this invoice item.
  37. * @property null|string|\Stripe\TestHelpers\TestClock $test_clock ID of the test clock this invoice item belongs to.
  38. * @property null|int $unit_amount Unit amount (in the <code>currency</code> specified) of the invoice item.
  39. * @property null|string $unit_amount_decimal Same as <code>unit_amount</code>, but contains a decimal value with at most 12 decimal places.
  40. */
  41. class InvoiceItem extends ApiResource
  42. {
  43. const OBJECT_NAME = 'invoiceitem';
  44. use ApiOperations\Update;
  45. /**
  46. * Creates an item to be added to a draft invoice (up to 250 items per invoice). If
  47. * no invoice is specified, the item will be on the next invoice created for the
  48. * customer specified.
  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\InvoiceItem 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. * Deletes an invoice item, removing it from an invoice. Deleting invoice items is
  68. * only possible when they’re not attached to invoices, or if it’s attached to a
  69. * draft invoice.
  70. *
  71. * @param null|array $params
  72. * @param null|array|string $opts
  73. *
  74. * @throws \Stripe\Exception\ApiErrorException if the request fails
  75. *
  76. * @return \Stripe\InvoiceItem the deleted resource
  77. */
  78. public function delete($params = null, $opts = null)
  79. {
  80. self::_validateParams($params);
  81. $url = $this->instanceUrl();
  82. list($response, $opts) = $this->_request('delete', $url, $params, $opts);
  83. $this->refreshFrom($response, $opts);
  84. return $this;
  85. }
  86. /**
  87. * Returns a list of your invoice items. Invoice items are returned sorted by
  88. * creation date, with the most recently created invoice items appearing first.
  89. *
  90. * @param null|array $params
  91. * @param null|array|string $opts
  92. *
  93. * @throws \Stripe\Exception\ApiErrorException if the request fails
  94. *
  95. * @return \Stripe\Collection<\Stripe\InvoiceItem> of ApiResources
  96. */
  97. public static function all($params = null, $opts = null)
  98. {
  99. $url = static::classUrl();
  100. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  101. }
  102. /**
  103. * Retrieves the invoice item with the given ID.
  104. *
  105. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  106. * @param null|array|string $opts
  107. *
  108. * @throws \Stripe\Exception\ApiErrorException if the request fails
  109. *
  110. * @return \Stripe\InvoiceItem
  111. */
  112. public static function retrieve($id, $opts = null)
  113. {
  114. $opts = \Stripe\Util\RequestOptions::parse($opts);
  115. $instance = new static($id, $opts);
  116. $instance->refresh();
  117. return $instance;
  118. }
  119. /**
  120. * Updates the amount or description of an invoice item on an upcoming invoice.
  121. * Updating an invoice item is only possible before the invoice it’s attached to is
  122. * closed.
  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\InvoiceItem 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. }