rap-2-h
rap-2-h

Reputation: 31948

Laravel cashier with Stripe: invoice number

Not sure it's the same elsewhere, but in my country, invoice numbers must be sequential for a service in a company.

My Laravel application sells a service via subscription. It must produce invoices with sequential numbers (abcdef-0001, abcdef-0002, etc.). I use Laravel Cashier with Stripe because both are easy to configure. Now I want to generate invoices. But Invoices in Laravel Cashier have no sequential numbers nowhere. I can access to one user "invoices" with:

$invoices = $user->invoices();

But it seems it's not really invoices, just payment informations from Stripe. Calling this function for each user to get the total number of invoices, then calculate the sequential number is not an option.

How can I handle a sequential number for invoices easily without breaking the simple model built by Laravel Cashier (+Stripe)? Is there a proper way to do this kind of thing? Or do I have to re-develop a whole system for such a need?

Upvotes: 3

Views: 2979

Answers (2)

Moritz Friedrich
Moritz Friedrich

Reputation: 1481

Invoices in Laravel Cashier are actually an abstraction over the Stripe invoice objects. While Stripe provides a sequential invoice number, Cashier does not expose it for some reason, but there's an escape hatch: By calling asStripeInvoice() on the invoice object, you can retrieve the underlying Stripe object.

$lastInvoice = $billable->invoices()->last();

// A sequential invoice number as provided by Stripe, 
// something like "6EEE0995-0001"
$invoiceNumber = $lastInvoice->asStripeInvoice()->number;

Upvotes: 0

VikingCode
VikingCode

Reputation: 1052

Just create your own model BizInvoice, give it a field invoiceID (like you like it .. you can query the last invoice... get id ... increment by 1 with custom logic or just use the default id field) and make a relation with user model and Laravel cashier payments model.

This way you have an unique (real) invoices number(id) that you could access through user or/and payments

Upvotes: 1

Related Questions