Invoice.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * Invoices are statements of amounts owed by a customer, and are either generated
  6. * one-off, or generated periodically from a subscription.
  7. *
  8. * They contain <a href="https://stripe.com/docs/api#invoiceitems">invoice
  9. * items</a>, and proration adjustments that may be caused by subscription
  10. * upgrades/downgrades (if necessary).
  11. *
  12. * If your invoice is configured to be billed through automatic charges, Stripe
  13. * automatically finalizes your invoice and attempts payment. Note that finalizing
  14. * the invoice, <a
  15. * href="https://stripe.com/docs/billing/invoices/workflow/#auto_advance">when
  16. * automatic</a>, does not happen immediately as the invoice is created. Stripe
  17. * waits until one hour after the last webhook was successfully sent (or the last
  18. * webhook timed out after failing). If you (and the platforms you may have
  19. * connected to) have no webhooks configured, Stripe waits one hour after creation
  20. * to finalize the invoice.
  21. *
  22. * If your invoice is configured to be billed by sending an email, then based on
  23. * your <a href="https://dashboard.stripe.com/account/billing/automatic'">email
  24. * settings</a>, Stripe will email the invoice to your customer and await payment.
  25. * These emails can contain a link to a hosted page to pay the invoice.
  26. *
  27. * Stripe applies any customer credit on the account before determining the amount
  28. * due for the invoice (i.e., the amount that will be actually charged). If the
  29. * amount due for the invoice is less than Stripe's <a
  30. * href="/docs/currencies#minimum-and-maximum-charge-amounts">minimum allowed
  31. * charge per currency</a>, the invoice is automatically marked paid, and we add
  32. * the amount due to the customer's running account balance which is applied to the
  33. * next invoice.
  34. *
  35. * More details on the customer's account balance are <a
  36. * href="https://stripe.com/docs/api/customers/object#customer_object-account_balance">here</a>.
  37. *
  38. * Related guide: <a href="https://stripe.com/docs/billing/invoices/sending">Send
  39. * Invoices to Customers</a>.
  40. *
  41. * @property string $id Unique identifier for the object.
  42. * @property string $object String representing the object's type. Objects of the same type share the same value.
  43. * @property null|string $account_country The country of the business associated with this invoice, most often the business creating the invoice.
  44. * @property null|string $account_name The public name of the business associated with this invoice, most often the business creating the invoice.
  45. * @property null|(string|\Stripe\TaxId)[] $account_tax_ids The account tax IDs associated with the invoice. Only editable when the invoice is a draft.
  46. * @property int $amount_due Final amount due at this time for this invoice. If the invoice's total is smaller than the minimum charge amount, for example, or if there is account credit that can be applied to the invoice, the <code>amount_due</code> may be 0. If there is a positive <code>starting_balance</code> for the invoice (the customer owes money), the <code>amount_due</code> will also take that into account. The charge that gets generated for the invoice will be for the amount specified in <code>amount_due</code>.
  47. * @property int $amount_paid The amount, in %s, that was paid.
  48. * @property int $amount_remaining The amount remaining, in %s, that is due.
  49. * @property null|int $application_fee_amount The fee in %s that will be applied to the invoice and transferred to the application owner's Stripe account when the invoice is paid.
  50. * @property int $attempt_count Number of payment attempts made for this invoice, from the perspective of the payment retry schedule. Any payment attempt counts as the first attempt, and subsequently only automatic retries increment the attempt count. In other words, manual payment attempts after the first attempt do not affect the retry schedule.
  51. * @property bool $attempted Whether an attempt has been made to pay the invoice. An invoice is not attempted until 1 hour after the <code>invoice.created</code> webhook, for example, so you might not want to display that invoice as unpaid to your users.
  52. * @property bool $auto_advance Controls whether Stripe will perform <a href="https://stripe.com/docs/billing/invoices/workflow/#auto_advance">automatic collection</a> of the invoice. When <code>false</code>, the invoice's state will not automatically advance without an explicit action.
  53. * @property null|string $billing_reason Indicates the reason why the invoice was created. <code>subscription_cycle</code> indicates an invoice created by a subscription advancing into a new period. <code>subscription_create</code> indicates an invoice created due to creating a subscription. <code>subscription_update</code> indicates an invoice created due to updating a subscription. <code>subscription</code> is set for all old invoices to indicate either a change to a subscription or a period advancement. <code>manual</code> is set for all invoices unrelated to a subscription (for example: created via the invoice editor). The <code>upcoming</code> value is reserved for simulated invoices per the upcoming invoice endpoint. <code>subscription_threshold</code> indicates an invoice created due to a billing threshold being reached.
  54. * @property null|string|\Stripe\Charge $charge ID of the latest charge generated for this invoice, if any.
  55. * @property null|string $collection_method Either <code>charge_automatically</code>, or <code>send_invoice</code>. When charging automatically, Stripe will attempt to pay this invoice using the default source attached to the customer. When sending an invoice, Stripe will email this invoice to the customer with payment instructions.
  56. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  57. * @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>.
  58. * @property null|\Stripe\StripeObject[] $custom_fields Custom fields displayed on the invoice.
  59. * @property string|\Stripe\Customer $customer The ID of the customer who will be billed.
  60. * @property null|\Stripe\StripeObject $customer_address The customer's address. Until the invoice is finalized, this field will equal <code>customer.address</code>. Once the invoice is finalized, this field will no longer be updated.
  61. * @property null|string $customer_email The customer's email. Until the invoice is finalized, this field will equal <code>customer.email</code>. Once the invoice is finalized, this field will no longer be updated.
  62. * @property null|string $customer_name The customer's name. Until the invoice is finalized, this field will equal <code>customer.name</code>. Once the invoice is finalized, this field will no longer be updated.
  63. * @property null|string $customer_phone The customer's phone number. Until the invoice is finalized, this field will equal <code>customer.phone</code>. Once the invoice is finalized, this field will no longer be updated.
  64. * @property null|\Stripe\StripeObject $customer_shipping The customer's shipping information. Until the invoice is finalized, this field will equal <code>customer.shipping</code>. Once the invoice is finalized, this field will no longer be updated.
  65. * @property null|string $customer_tax_exempt The customer's tax exempt status. Until the invoice is finalized, this field will equal <code>customer.tax_exempt</code>. Once the invoice is finalized, this field will no longer be updated.
  66. * @property null|\Stripe\StripeObject[] $customer_tax_ids The customer's tax IDs. Until the invoice is finalized, this field will contain the same tax IDs as <code>customer.tax_ids</code>. Once the invoice is finalized, this field will no longer be updated.
  67. * @property null|string|\Stripe\PaymentMethod $default_payment_method ID of the default payment method for the invoice. It must belong to the customer associated with the invoice. If not set, defaults to the subscription's default payment method, if any, or to the default payment method in the customer's invoice settings.
  68. * @property null|string|\Stripe\Account|\Stripe\AlipayAccount|\Stripe\BankAccount|\Stripe\BitcoinReceiver|\Stripe\Card|\Stripe\Source $default_source ID of the default payment source for the invoice. It must belong to the customer associated with the invoice and be in a chargeable state. If not set, defaults to the subscription's default source, if any, or to the customer's default source.
  69. * @property \Stripe\TaxRate[] $default_tax_rates The tax rates applied to this invoice, if any.
  70. * @property null|string $description An arbitrary string attached to the object. Often useful for displaying to users. Referenced as 'memo' in the Dashboard.
  71. * @property null|\Stripe\Discount $discount Describes the current discount applied to this invoice, if there is one. Not populated if there are multiple discounts.
  72. * @property null|(string|\Stripe\Discount)[] $discounts The discounts applied to the invoice. Line item discounts are applied before invoice discounts. Use <code>expand[]=discounts</code> to expand each discount.
  73. * @property null|int $due_date The date on which payment for this invoice is due. This value will be <code>null</code> for invoices where <code>collection_method=charge_automatically</code>.
  74. * @property null|int $ending_balance Ending customer balance after the invoice is finalized. Invoices are finalized approximately an hour after successful webhook delivery or when payment collection is attempted for the invoice. If the invoice has not been finalized yet, this will be null.
  75. * @property null|string $footer Footer displayed on the invoice.
  76. * @property null|string $hosted_invoice_url The URL for the hosted invoice page, which allows customers to view and pay an invoice. If the invoice has not been finalized yet, this will be null.
  77. * @property null|string $invoice_pdf The link to download the PDF for the invoice. If the invoice has not been finalized yet, this will be null.
  78. * @property null|\Stripe\ErrorObject $last_finalization_error The error encountered during the previous attempt to finalize the invoice. This field is cleared when the invoice is successfully finalized.
  79. * @property \Stripe\Collection $lines The individual line items that make up the invoice. <code>lines</code> is sorted as follows: invoice items in reverse chronological order, followed by the subscription, if any.
  80. * @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.
  81. * @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.
  82. * @property null|int $next_payment_attempt The time at which payment will next be attempted. This value will be <code>null</code> for invoices where <code>collection_method=send_invoice</code>.
  83. * @property null|string $number A unique, identifying string that appears on emails sent to the customer for this invoice. This starts with the customer's unique invoice_prefix if it is specified.
  84. * @property bool $paid Whether payment was successfully collected for this invoice. An invoice can be paid (most commonly) with a charge or with credit from the customer's account balance.
  85. * @property null|string|\Stripe\PaymentIntent $payment_intent The PaymentIntent associated with this invoice. The PaymentIntent is generated when the invoice is finalized, and can then be used to pay the invoice. Note that voiding an invoice will cancel the PaymentIntent.
  86. * @property int $period_end End of the usage period during which invoice items were added to this invoice.
  87. * @property int $period_start Start of the usage period during which invoice items were added to this invoice.
  88. * @property int $post_payment_credit_notes_amount Total amount of all post-payment credit notes issued for this invoice.
  89. * @property int $pre_payment_credit_notes_amount Total amount of all pre-payment credit notes issued for this invoice.
  90. * @property null|string $receipt_number This is the transaction number that appears on email receipts sent for this invoice.
  91. * @property int $starting_balance Starting customer balance before the invoice is finalized. If the invoice has not been finalized yet, this will be the current customer balance.
  92. * @property null|string $statement_descriptor Extra information about an invoice for the customer's credit card statement.
  93. * @property null|string $status The status of the invoice, one of <code>draft</code>, <code>open</code>, <code>paid</code>, <code>uncollectible</code>, or <code>void</code>. <a href="https://stripe.com/docs/billing/invoices/workflow#workflow-overview">Learn more</a>
  94. * @property \Stripe\StripeObject $status_transitions
  95. * @property null|string|\Stripe\Subscription $subscription The subscription that this invoice was prepared for, if any.
  96. * @property int $subscription_proration_date Only set for upcoming invoices that preview prorations. The time used to calculate prorations.
  97. * @property int $subtotal Total of all subscriptions, invoice items, and prorations on the invoice before any invoice level discount or tax is applied. Item discounts are already incorporated
  98. * @property null|int $tax The amount of tax on this invoice. This is the sum of all the tax amounts on this invoice.
  99. * @property \Stripe\StripeObject $threshold_reason
  100. * @property int $total Total after discounts and taxes.
  101. * @property null|\Stripe\StripeObject[] $total_discount_amounts The aggregate amounts calculated per discount across all line items.
  102. * @property \Stripe\StripeObject[] $total_tax_amounts The aggregate amounts calculated per tax rate for all line items.
  103. * @property null|int $webhooks_delivered_at Invoices are automatically paid or sent 1 hour after webhooks are delivered, or until all webhook delivery attempts have <a href="https://stripe.com/docs/billing/webhooks#understand">been exhausted</a>. This field tracks the time when webhooks for this invoice were successfully delivered. If the invoice had no webhooks to deliver, this will be set while the invoice is being created.
  104. */
  105. class Invoice extends ApiResource
  106. {
  107. const OBJECT_NAME = 'invoice';
  108. use ApiOperations\All;
  109. use ApiOperations\Create;
  110. use ApiOperations\Delete;
  111. use ApiOperations\Retrieve;
  112. use ApiOperations\Update;
  113. const BILLING_CHARGE_AUTOMATICALLY = 'charge_automatically';
  114. const BILLING_SEND_INVOICE = 'send_invoice';
  115. const BILLING_REASON_MANUAL = 'manual';
  116. const BILLING_REASON_SUBSCRIPTION = 'subscription';
  117. const BILLING_REASON_SUBSCRIPTION_CREATE = 'subscription_create';
  118. const BILLING_REASON_SUBSCRIPTION_CYCLE = 'subscription_cycle';
  119. const BILLING_REASON_SUBSCRIPTION_THRESHOLD = 'subscription_threshold';
  120. const BILLING_REASON_SUBSCRIPTION_UPDATE = 'subscription_update';
  121. const BILLING_REASON_UPCOMING = 'upcoming';
  122. const COLLECTION_METHOD_CHARGE_AUTOMATICALLY = 'charge_automatically';
  123. const COLLECTION_METHOD_SEND_INVOICE = 'send_invoice';
  124. const STATUS_DELETED = 'deleted';
  125. const STATUS_DRAFT = 'draft';
  126. const STATUS_OPEN = 'open';
  127. const STATUS_PAID = 'paid';
  128. const STATUS_UNCOLLECTIBLE = 'uncollectible';
  129. const STATUS_VOID = 'void';
  130. use ApiOperations\NestedResource;
  131. const PATH_LINES = '/lines';
  132. /**
  133. * @param null|array $params
  134. * @param null|array|string $opts
  135. *
  136. * @throws \Stripe\Exception\ApiErrorException if the request fails
  137. *
  138. * @return \Stripe\Invoice the upcoming invoice
  139. */
  140. public static function upcoming($params = null, $opts = null)
  141. {
  142. $url = static::classUrl() . '/upcoming';
  143. list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
  144. $obj = Util\Util::convertToStripeObject($response->json, $opts);
  145. $obj->setLastResponse($response);
  146. return $obj;
  147. }
  148. /**
  149. * @param string $id the ID of the invoice on which to retrieve the lines
  150. * @param null|array $params
  151. * @param null|array|string $opts
  152. *
  153. * @throws StripeExceptionApiErrorException if the request fails
  154. *
  155. * @return \Stripe\Collection the list of lines (InvoiceLineItem)
  156. */
  157. public static function allLines($id, $params = null, $opts = null)
  158. {
  159. return self::_allNestedResources($id, static::PATH_LINES, $params, $opts);
  160. }
  161. /**
  162. * @param null|array $params
  163. * @param null|array|string $opts
  164. *
  165. * @throws \Stripe\Exception\ApiErrorException if the request fails
  166. *
  167. * @return Invoice the finalized invoice
  168. */
  169. public function finalizeInvoice($params = null, $opts = null)
  170. {
  171. $url = $this->instanceUrl() . '/finalize';
  172. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  173. $this->refreshFrom($response, $opts);
  174. return $this;
  175. }
  176. /**
  177. * @param null|array $params
  178. * @param null|array|string $opts
  179. *
  180. * @throws \Stripe\Exception\ApiErrorException if the request fails
  181. *
  182. * @return Invoice the uncollectible invoice
  183. */
  184. public function markUncollectible($params = null, $opts = null)
  185. {
  186. $url = $this->instanceUrl() . '/mark_uncollectible';
  187. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  188. $this->refreshFrom($response, $opts);
  189. return $this;
  190. }
  191. /**
  192. * @param null|array $params
  193. * @param null|array|string $opts
  194. *
  195. * @throws \Stripe\Exception\ApiErrorException if the request fails
  196. *
  197. * @return Invoice the paid invoice
  198. */
  199. public function pay($params = null, $opts = null)
  200. {
  201. $url = $this->instanceUrl() . '/pay';
  202. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  203. $this->refreshFrom($response, $opts);
  204. return $this;
  205. }
  206. /**
  207. * @param null|array $params
  208. * @param null|array|string $opts
  209. *
  210. * @throws \Stripe\Exception\ApiErrorException if the request fails
  211. *
  212. * @return Invoice the sent invoice
  213. */
  214. public function sendInvoice($params = null, $opts = null)
  215. {
  216. $url = $this->instanceUrl() . '/send';
  217. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  218. $this->refreshFrom($response, $opts);
  219. return $this;
  220. }
  221. /**
  222. * @param null|array $params
  223. * @param null|array|string $opts
  224. *
  225. * @throws \Stripe\Exception\ApiErrorException if the request fails
  226. *
  227. * @return Invoice the voided invoice
  228. */
  229. public function voidInvoice($params = null, $opts = null)
  230. {
  231. $url = $this->instanceUrl() . '/void';
  232. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  233. $this->refreshFrom($response, $opts);
  234. return $this;
  235. }
  236. }