Class: Stripe::InvoiceService

Inherits:
StripeService show all
Defined in:
lib/stripe/services/invoice_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from StripeService

#request, #request_stream

Constructor Details

#initialize(requestor) ⇒ InvoiceService

Returns a new instance of InvoiceService.



8
9
10
11
# File 'lib/stripe/services/invoice_service.rb', line 8

def initialize(requestor)
  super
  @line_items = Stripe::InvoiceLineItemService.new(@requestor)
end

Instance Attribute Details

#line_itemsObject (readonly)

Returns the value of attribute line_items.



6
7
8
# File 'lib/stripe/services/invoice_service.rb', line 6

def line_items
  @line_items
end

Instance Method Details

#add_lines(invoice, params = {}, opts = {}) ⇒ Object

Adds multiple line items to an invoice. This is only possible when an invoice is still a draft.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/stripe/services/invoice_service.rb', line 14

def add_lines(invoice, params = {}, opts = {})
  params = ::Stripe::InvoiceAddLinesParams.coerce_params(params) unless params.is_a?(Stripe::RequestParams)

  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/add_lines", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#attach_payment(invoice, params = {}, opts = {}) ⇒ Object

Attaches a PaymentIntent or an Out of Band Payment to the invoice, adding it to the list of payments.

For the PaymentIntent, when the PaymentIntent’s status changes to succeeded, the payment is credited to the invoice, increasing its amount_paid. When the invoice is fully paid, the invoice’s status becomes paid.

If the PaymentIntent’s status is already succeeded when it’s attached, it’s credited to the invoice immediately.

See: [Partial payments](docs.stripe.com/docs/invoicing/partial-payments) to learn more.



36
37
38
39
40
41
42
43
44
# File 'lib/stripe/services/invoice_service.rb', line 36

def attach_payment(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/attach_payment", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#create(params = {}, opts = {}) ⇒ Object

This endpoint creates a draft invoice for a given customer. The invoice remains a draft until you [finalize the invoice, which allows you to [pay](/api/invoices/pay) or <a href=“/api/invoices/send”>send](docs.stripe.com/api/invoices/finalize) the invoice to your customers.



47
48
49
# File 'lib/stripe/services/invoice_service.rb', line 47

def create(params = {}, opts = {})
  request(method: :post, path: "/v1/invoices", params: params, opts: opts, base_address: :api)
end

#create_preview(params = {}, opts = {}) ⇒ Object

At any time, you can preview the upcoming invoice for a subscription or subscription schedule. This will show you all the charges that are pending, including subscription renewal charges, invoice item charges, etc. It will also show you any discounts that are applicable to the invoice.

You can also preview the effects of creating or updating a subscription or subscription schedule, including a preview of any prorations that will take place. To ensure that the actual proration is calculated exactly the same as the previewed proration, you should pass the subscription_details.proration_date parameter when doing the actual subscription update.

The recommended way to get only the prorations being previewed on the invoice is to consider line items where parent.subscription_item_details.proration is true.

Note that when you are viewing an upcoming invoice, you are simply viewing a preview – the invoice has not yet been created. As such, the upcoming invoice will not show up in invoice listing calls, and you cannot use the API to pay or edit the invoice. If you want to change the amount that your customer will be billed, you can add, remove, or update pending invoice items, or update the customer’s discount.

Note: Currency conversion calculations use the latest exchange rates. Exchange rates may vary between the time of the preview and the time of the actual invoice creation. [Learn more](docs.stripe.com/currencies/conversions)



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stripe/services/invoice_service.rb', line 60

def create_preview(params = {}, opts = {})
  params = ::Stripe::InvoiceCreatePreviewParams.coerce_params(params) unless params.is_a?(Stripe::RequestParams)

  request(
    method: :post,
    path: "/v1/invoices/create_preview",
    params: params,
    opts: opts,
    base_address: :api
  )
end

#delete(invoice, params = {}, opts = {}) ⇒ Object

Permanently deletes a one-off invoice draft. This cannot be undone. Attempts to delete invoices that are no longer in a draft state will fail; once an invoice has been finalized or if an invoice is for a subscription, it must be [voided](docs.stripe.com/api/invoices/void).



73
74
75
76
77
78
79
80
81
# File 'lib/stripe/services/invoice_service.rb', line 73

def delete(invoice, params = {}, opts = {})
  request(
    method: :delete,
    path: format("/v1/invoices/%<invoice>s", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#detach_payment(invoice, params = {}, opts = {}) ⇒ Object

Detaches a payment from the invoice, removing it from the list of payments



84
85
86
87
88
89
90
91
92
# File 'lib/stripe/services/invoice_service.rb', line 84

def detach_payment(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/detach_payment", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#finalize_invoice(invoice, params = {}, opts = {}) ⇒ Object

Stripe automatically finalizes drafts before sending and attempting payment on invoices. However, if you’d like to finalize a draft invoice manually, you can do so using this method.



95
96
97
98
99
100
101
102
103
# File 'lib/stripe/services/invoice_service.rb', line 95

def finalize_invoice(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/finalize", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#list(params = {}, opts = {}) ⇒ Object

You can list all invoices, or list the invoices for a specific customer. The invoices are returned sorted by creation date, with the most recently created invoices appearing first.



106
107
108
# File 'lib/stripe/services/invoice_service.rb', line 106

def list(params = {}, opts = {})
  request(method: :get, path: "/v1/invoices", params: params, opts: opts, base_address: :api)
end

#mark_uncollectible(invoice, params = {}, opts = {}) ⇒ Object

Marking an invoice as uncollectible is useful for keeping track of bad debts that can be written off for accounting purposes.



111
112
113
114
115
116
117
118
119
# File 'lib/stripe/services/invoice_service.rb', line 111

def mark_uncollectible(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/mark_uncollectible", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#pay(invoice, params = {}, opts = {}) ⇒ Object

Stripe automatically creates and then attempts to collect payment on invoices for customers on subscriptions according to your [subscriptions settings](dashboard.stripe.com/account/billing/automatic). However, if you’d like to attempt payment on an invoice out of the normal collection schedule or for some other reason, you can do so.



122
123
124
125
126
127
128
129
130
# File 'lib/stripe/services/invoice_service.rb', line 122

def pay(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/pay", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#remove_lines(invoice, params = {}, opts = {}) ⇒ Object

Removes multiple line items from an invoice. This is only possible when an invoice is still a draft.



133
134
135
136
137
138
139
140
141
# File 'lib/stripe/services/invoice_service.rb', line 133

def remove_lines(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/remove_lines", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#retrieve(invoice, params = {}, opts = {}) ⇒ Object

Retrieves the invoice with the given ID.



144
145
146
147
148
149
150
151
152
# File 'lib/stripe/services/invoice_service.rb', line 144

def retrieve(invoice, params = {}, opts = {})
  request(
    method: :get,
    path: format("/v1/invoices/%<invoice>s", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#search(params = {}, opts = {}) ⇒ Object

Search for invoices you’ve previously created using Stripe’s [Search Query Language](docs.stripe.com/docs/search#search-query-language). Don’t use search in read-after-write flows where strict consistency is necessary. Under normal operating conditions, data is searchable in less than a minute. Occasionally, propagation of new or updated data can be up to an hour behind during outages. Search functionality is not available to merchants in India.



158
159
160
161
162
163
164
165
166
# File 'lib/stripe/services/invoice_service.rb', line 158

def search(params = {}, opts = {})
  request(
    method: :get,
    path: "/v1/invoices/search",
    params: params,
    opts: opts,
    base_address: :api
  )
end

#send_invoice(invoice, params = {}, opts = {}) ⇒ Object

Stripe will automatically send invoices to customers according to your [subscriptions settings](dashboard.stripe.com/account/billing/automatic). However, if you’d like to manually send an invoice to your customer out of the normal schedule, you can do so. When sending invoices that have already been paid, there will be no reference to the payment in the email.

Requests made in test-mode result in no emails being sent, despite sending an invoice.sent event.



171
172
173
174
175
176
177
178
179
# File 'lib/stripe/services/invoice_service.rb', line 171

def send_invoice(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/send", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#serialize_batch_pay(invoice, params = {}, opts = {}) ⇒ Object

Serializes an Invoice pay request into a batch job JSONL line.



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/stripe/services/invoice_service.rb', line 182

def serialize_batch_pay(invoice, params = {}, opts = {})
  item_id = SecureRandom.uuid
  stripe_version = opts[:stripe_version] || Stripe.api_version

  item = {
    id: item_id,
    params: params,
    stripe_version: stripe_version,
  }
  item[:path_params] = { invoice: invoice }
  item[:context] = opts[:stripe_context] if opts[:stripe_context]
  JSON.generate(item)
end

#serialize_batch_update(invoice, params = {}, opts = {}) ⇒ Object

Serializes an Invoice update request into a batch job JSONL line.



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/stripe/services/invoice_service.rb', line 197

def serialize_batch_update(invoice, params = {}, opts = {})
  item_id = SecureRandom.uuid
  stripe_version = opts[:stripe_version] || Stripe.api_version

  item = {
    id: item_id,
    params: params,
    stripe_version: stripe_version,
  }
  item[:path_params] = { invoice: invoice }
  item[:context] = opts[:stripe_context] if opts[:stripe_context]
  JSON.generate(item)
end

#update(invoice, params = {}, opts = {}) ⇒ Object

Draft invoices are fully editable. Once an invoice is [finalized](docs.stripe.com/docs/billing/invoices/workflow#finalized), monetary values, as well as collection_method, become uneditable.

If you would like to stop the Stripe Billing engine from automatically finalizing, reattempting payments on, sending reminders for, or [automatically reconciling](docs.stripe.com/docs/billing/invoices/reconciliation) invoices, pass auto_advance=false.



217
218
219
220
221
222
223
224
225
# File 'lib/stripe/services/invoice_service.rb', line 217

def update(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#update_lines(invoice, params = {}, opts = {}) ⇒ Object

Updates multiple line items on an invoice. This is only possible when an invoice is still a draft.



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/stripe/services/invoice_service.rb', line 228

def update_lines(invoice, params = {}, opts = {})
  params = ::Stripe::InvoiceUpdateLinesParams.coerce_params(params) unless params.is_a?(Stripe::RequestParams)

  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/update_lines", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end

#void_invoice(invoice, params = {}, opts = {}) ⇒ Object

Mark a finalized invoice as void. This cannot be undone. Voiding an invoice is similar to [deletion](docs.stripe.com/api/invoices/delete), however it only applies to finalized invoices and maintains a papertrail where the invoice can still be found.

Consult with local regulations to determine whether and how an invoice might be amended, canceled, or voided in the jurisdiction you’re doing business in. You might need to [issue another invoice or <a href=“/api/credit_notes/create”>credit note](docs.stripe.com/api/invoices/create) instead. Stripe recommends that you consult with your legal counsel for advice specific to your business.



243
244
245
246
247
248
249
250
251
# File 'lib/stripe/services/invoice_service.rb', line 243

def void_invoice(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/void", { invoice: CGI.escape(invoice) }),
    params: params,
    opts: opts,
    base_address: :api
  )
end