Class: Stripe::InvoiceService

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

Defined Under Namespace

Classes: AddLinesParams, AttachPaymentIntentParams, AttachPaymentParams, CreateParams, CreatePreviewParams, DeleteParams, FinalizeInvoiceParams, ListParams, MarkUncollectibleParams, PayParams, RemoveLinesParams, RetrieveParams, SearchParams, SendInvoiceParams, UpcomingParams, UpdateLinesParams, UpdateParams, VoidInvoiceParams

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
12
13
# File 'lib/stripe/services/invoice_service.rb', line 8

def initialize(requestor)
  super(requestor)
  @payments = Stripe::InvoicePaymentService.new(@requestor)
  @line_items = Stripe::InvoiceLineItemService.new(@requestor)
  @upcoming_lines = Stripe::InvoiceUpcomingLinesService.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

#paymentsObject (readonly)

Returns the value of attribute payments.



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

def payments
  @payments
end

#upcoming_linesObject (readonly)

Returns the value of attribute upcoming_lines.



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

def upcoming_lines
  @upcoming_lines
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.



6347
6348
6349
6350
6351
6352
6353
6354
6355
# File 'lib/stripe/services/invoice_service.rb', line 6347

def add_lines(invoice, params = {}, opts = {})
  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 Out of Band Payment, the payment is credited to the invoice immediately, increasing the amount_paid of the invoice and subsequently transitioning the status of the invoice to paid if necessary.

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: [Create an invoice payment](stripe.com/docs/invoicing/payments/create) to learn more.



6370
6371
6372
6373
6374
6375
6376
6377
6378
# File 'lib/stripe/services/invoice_service.rb', line 6370

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

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

Attaches a PaymentIntent to the invoice, adding it to the list of payments. 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 is attached, it is credited to the invoice immediately.

Related guide: [Create an invoice payment](stripe.com/docs/invoicing/payments/create)



6389
6390
6391
6392
6393
6394
6395
6396
6397
# File 'lib/stripe/services/invoice_service.rb', line 6389

def attach_payment_intent(invoice, params = {}, opts = {})
  request(
    method: :post,
    path: format("/v1/invoices/%<invoice>s/attach_payment_intent", { 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](#pay_invoice) or <a href=“#send_invoice”>send](stripe.com/docs/api#finalize_invoice) the invoice to your customers.



6400
6401
6402
# File 'lib/stripe/services/invoice_service.rb', line 6400

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 customer. 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.

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.

You can preview the effects of updating a subscription, including a preview of what proration 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 is to consider only proration line items where period is equal to the subscription_details.proration_date value passed in the request.

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)



6411
6412
6413
6414
6415
6416
6417
6418
6419
# File 'lib/stripe/services/invoice_service.rb', line 6411

def create_preview(params = {}, opts = {})
  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](stripe.com/docs/api#void_invoice).



6422
6423
6424
6425
6426
6427
6428
6429
6430
# File 'lib/stripe/services/invoice_service.rb', line 6422

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

#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.



6433
6434
6435
6436
6437
6438
6439
6440
6441
# File 'lib/stripe/services/invoice_service.rb', line 6433

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.



6444
6445
6446
# File 'lib/stripe/services/invoice_service.rb', line 6444

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.



6449
6450
6451
6452
6453
6454
6455
6456
6457
# File 'lib/stripe/services/invoice_service.rb', line 6449

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.



6460
6461
6462
6463
6464
6465
6466
6467
6468
# File 'lib/stripe/services/invoice_service.rb', line 6460

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.



6471
6472
6473
6474
6475
6476
6477
6478
6479
# File 'lib/stripe/services/invoice_service.rb', line 6471

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.



6482
6483
6484
6485
6486
6487
6488
6489
6490
# File 'lib/stripe/services/invoice_service.rb', line 6482

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](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.



6496
6497
6498
6499
6500
6501
6502
6503
6504
# File 'lib/stripe/services/invoice_service.rb', line 6496

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.



6509
6510
6511
6512
6513
6514
6515
6516
6517
# File 'lib/stripe/services/invoice_service.rb', line 6509

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

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

At any time, you can preview the upcoming invoice for a customer. 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.

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.

You can preview the effects of updating a subscription, including a preview of what proration 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 is to consider only proration line items where period is equal to the subscription_details.proration_date value passed in the request.

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)



6526
6527
6528
6529
6530
6531
6532
6533
6534
# File 'lib/stripe/services/invoice_service.rb', line 6526

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

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

Draft invoices are fully editable. Once an invoice is [finalized](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](stripe.com/docs/billing/invoices/reconciliation) invoices, pass auto_advance=false.



6542
6543
6544
6545
6546
6547
6548
6549
6550
# File 'lib/stripe/services/invoice_service.rb', line 6542

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.



6553
6554
6555
6556
6557
6558
6559
6560
6561
# File 'lib/stripe/services/invoice_service.rb', line 6553

def update_lines(invoice, params = {}, opts = {})
  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](stripe.com/docs/api#delete_invoice), 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=“#create_credit_note”>credit note](stripe.com/docs/api#create_invoice) instead. Stripe recommends that you consult with your legal counsel for advice specific to your business.



6566
6567
6568
6569
6570
6571
6572
6573
6574
# File 'lib/stripe/services/invoice_service.rb', line 6566

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