CreditNote.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe;
  4. /**
  5. * Issue a credit note to adjust an invoice's amount after the invoice is finalized.
  6. *
  7. * Related guide: <a href="https://stripe.com/docs/billing/invoices/credit-notes">Credit notes</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 int $amount The integer amount in cents (or local equivalent) representing the total amount of the credit note, including tax.
  12. * @property int $amount_shipping This is the sum of all the shipping amounts.
  13. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  14. * @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>.
  15. * @property string|\Stripe\Customer $customer ID of the customer.
  16. * @property null|string|\Stripe\CustomerBalanceTransaction $customer_balance_transaction Customer balance transaction related to this credit note.
  17. * @property int $discount_amount The integer amount in cents (or local equivalent) representing the total amount of discount that was credited.
  18. * @property \Stripe\StripeObject[] $discount_amounts The aggregate amounts calculated per discount for all line items.
  19. * @property null|int $effective_at The date when this credit note is in effect. Same as <code>created</code> unless overwritten. When defined, this value replaces the system-generated 'Date of issue' printed on the credit note PDF.
  20. * @property string|\Stripe\Invoice $invoice ID of the invoice.
  21. * @property \Stripe\Collection<\Stripe\CreditNoteLineItem> $lines Line items that make up the credit note
  22. * @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.
  23. * @property null|string $memo Customer-facing text that appears on the credit note PDF.
  24. * @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.
  25. * @property string $number A unique number that identifies this particular credit note and appears on the PDF of the credit note and its associated invoice.
  26. * @property null|int $out_of_band_amount Amount that was credited outside of Stripe.
  27. * @property string $pdf The link to download the PDF of the credit note.
  28. * @property null|string $reason Reason for issuing this credit note, one of <code>duplicate</code>, <code>fraudulent</code>, <code>order_change</code>, or <code>product_unsatisfactory</code>
  29. * @property null|string|\Stripe\Refund $refund Refund related to this credit note.
  30. * @property null|\Stripe\StripeObject $shipping_cost The details of the cost of shipping, including the ShippingRate applied to the invoice.
  31. * @property string $status Status of this credit note, one of <code>issued</code> or <code>void</code>. Learn more about <a href="https://stripe.com/docs/billing/invoices/credit-notes#voiding">voiding credit notes</a>.
  32. * @property int $subtotal The integer amount in cents (or local equivalent) representing the amount of the credit note, excluding exclusive tax and invoice level discounts.
  33. * @property null|int $subtotal_excluding_tax The integer amount in cents (or local equivalent) representing the amount of the credit note, excluding all tax and invoice level discounts.
  34. * @property \Stripe\StripeObject[] $tax_amounts The aggregate amounts calculated per tax rate for all line items.
  35. * @property int $total The integer amount in cents (or local equivalent) representing the total amount of the credit note, including tax and all discount.
  36. * @property null|int $total_excluding_tax The integer amount in cents (or local equivalent) representing the total amount of the credit note, excluding tax, but including discounts.
  37. * @property string $type Type of this credit note, one of <code>pre_payment</code> or <code>post_payment</code>. A <code>pre_payment</code> credit note means it was issued when the invoice was open. A <code>post_payment</code> credit note means it was issued when the invoice was paid.
  38. * @property null|int $voided_at The time that the credit note was voided.
  39. */
  40. class CreditNote extends ApiResource
  41. {
  42. const OBJECT_NAME = 'credit_note';
  43. use ApiOperations\NestedResource;
  44. use ApiOperations\Update;
  45. const REASON_DUPLICATE = 'duplicate';
  46. const REASON_FRAUDULENT = 'fraudulent';
  47. const REASON_ORDER_CHANGE = 'order_change';
  48. const REASON_PRODUCT_UNSATISFACTORY = 'product_unsatisfactory';
  49. const STATUS_ISSUED = 'issued';
  50. const STATUS_VOID = 'void';
  51. const TYPE_POST_PAYMENT = 'post_payment';
  52. const TYPE_PRE_PAYMENT = 'pre_payment';
  53. /**
  54. * Issue a credit note to adjust the amount of a finalized invoice. For a
  55. * <code>status=open</code> invoice, a credit note reduces its
  56. * <code>amount_due</code>. For a <code>status=paid</code> invoice, a credit note
  57. * does not affect its <code>amount_due</code>. Instead, it can result in any
  58. * combination of the following:.
  59. *
  60. * <ul> <li>Refund: create a new refund (using <code>refund_amount</code>) or link
  61. * an existing refund (using <code>refund</code>).</li> <li>Customer balance
  62. * credit: credit the customer’s balance (using <code>credit_amount</code>) which
  63. * will be automatically applied to their next invoice when it’s finalized.</li>
  64. * <li>Outside of Stripe credit: record the amount that is or will be credited
  65. * outside of Stripe (using <code>out_of_band_amount</code>).</li> </ul>
  66. *
  67. * For post-payment credit notes the sum of the refund, credit and outside of
  68. * Stripe amounts must equal the credit note total.
  69. *
  70. * You may issue multiple credit notes for an invoice. Each credit note will
  71. * increment the invoice’s <code>pre_payment_credit_notes_amount</code> or
  72. * <code>post_payment_credit_notes_amount</code> depending on its
  73. * <code>status</code> at the time of credit note creation.
  74. *
  75. * @param null|array $params
  76. * @param null|array|string $options
  77. *
  78. * @throws \Stripe\Exception\ApiErrorException if the request fails
  79. *
  80. * @return \Stripe\CreditNote the created resource
  81. */
  82. public static function create($params = null, $options = null)
  83. {
  84. self::_validateParams($params);
  85. $url = static::classUrl();
  86. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  87. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  88. $obj->setLastResponse($response);
  89. return $obj;
  90. }
  91. /**
  92. * Returns a list of credit notes.
  93. *
  94. * @param null|array $params
  95. * @param null|array|string $opts
  96. *
  97. * @throws \Stripe\Exception\ApiErrorException if the request fails
  98. *
  99. * @return \Stripe\Collection<\Stripe\CreditNote> of ApiResources
  100. */
  101. public static function all($params = null, $opts = null)
  102. {
  103. $url = static::classUrl();
  104. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  105. }
  106. /**
  107. * Retrieves the credit note object with the given identifier.
  108. *
  109. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  110. * @param null|array|string $opts
  111. *
  112. * @throws \Stripe\Exception\ApiErrorException if the request fails
  113. *
  114. * @return \Stripe\CreditNote
  115. */
  116. public static function retrieve($id, $opts = null)
  117. {
  118. $opts = \Stripe\Util\RequestOptions::parse($opts);
  119. $instance = new static($id, $opts);
  120. $instance->refresh();
  121. return $instance;
  122. }
  123. /**
  124. * Updates an existing credit note.
  125. *
  126. * @param string $id the ID of the resource to update
  127. * @param null|array $params
  128. * @param null|array|string $opts
  129. *
  130. * @throws \Stripe\Exception\ApiErrorException if the request fails
  131. *
  132. * @return \Stripe\CreditNote the updated resource
  133. */
  134. public static function update($id, $params = null, $opts = null)
  135. {
  136. self::_validateParams($params);
  137. $url = static::resourceUrl($id);
  138. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  139. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  140. $obj->setLastResponse($response);
  141. return $obj;
  142. }
  143. /**
  144. * @param null|array $params
  145. * @param null|array|string $opts
  146. *
  147. * @throws \Stripe\Exception\ApiErrorException if the request fails
  148. *
  149. * @return \Stripe\CreditNote the previewed credit note
  150. */
  151. public static function preview($params = null, $opts = null)
  152. {
  153. $url = static::classUrl() . '/preview';
  154. list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
  155. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  156. $obj->setLastResponse($response);
  157. return $obj;
  158. }
  159. /**
  160. * @param null|array $params
  161. * @param null|array|string $opts
  162. *
  163. * @throws \Stripe\Exception\ApiErrorException if the request fails
  164. *
  165. * @return \Stripe\Collection<\Stripe\CreditNoteLineItem> list of credit note line items
  166. */
  167. public static function previewLines($params = null, $opts = null)
  168. {
  169. $url = static::classUrl() . '/preview/lines';
  170. list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
  171. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  172. $obj->setLastResponse($response);
  173. return $obj;
  174. }
  175. /**
  176. * @param null|array $params
  177. * @param null|array|string $opts
  178. *
  179. * @throws \Stripe\Exception\ApiErrorException if the request fails
  180. *
  181. * @return \Stripe\CreditNote the voided credit note
  182. */
  183. public function voidCreditNote($params = null, $opts = null)
  184. {
  185. $url = $this->instanceUrl() . '/void';
  186. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  187. $this->refreshFrom($response, $opts);
  188. return $this;
  189. }
  190. const PATH_LINES = '/lines';
  191. /**
  192. * @param string $id the ID of the credit note on which to retrieve the credit note line items
  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\Collection<\Stripe\CreditNoteLineItem> the list of credit note line items
  199. */
  200. public static function allLines($id, $params = null, $opts = null)
  201. {
  202. return self::_allNestedResources($id, static::PATH_LINES, $params, $opts);
  203. }
  204. }