Laravel Cashier Stripe Multiple Products Invoice
In this article, I will show you how Laravel Cashier (stripe) can make an invoice for multiple products easily 💸.
This was a hidden feature in Laravel cashier, I did submit a PR (#7865) to library documentation and it got merged so now it lives where it should be 🥳
This feature was added in this PR (#1213) so all the credit goes to driesvints.
Laravel with any kind of authentification mechanism (ex: Breeze)
The method is `tabPrice` which allows you to add invoice items to the customer's "tab" (up to 250 items per invoice) and then invoice him, So with that, you will generate a detailed invoice that contains multiple products which is very useful if you are working with a shopping cart for example.
$user->tabPrice('price_mclaren_gtr', 1); $user->tabPrice('price_mustang_shelby', 1); $user->tabPrice('price_bugatti_chiron', 1); // here you will charge the customer $invoice = $user->invoice();
The `tabPrice` method works with pre-defined prices. This will provide you with better analytics and data in your Stripe dashboard about your sales by product.
You can also create prices on the fly likewise :
$price = $user->stripe()->prices->create([ 'currency' => 'USD', 'product_data' => [ 'name' => 'Maclaren P1 GTR', ], 'unit_amount' => 499, ]);
if you want to know the alternative way of doing this :
Cashier::stripe()->invoiceItems->create([ 'customer' => $user->stripe_id, 'price' => 'price_mclaren_gtr', ]); Cashier::stripe()->invoiceItems->create([ 'customer' => $user->stripe_id, 'price' => 'price_mustang_shelby', ]); Cashier::stripe()->invoiceItems->create([ 'customer' => $user->stripe_id, 'price' => 'price_bugatti_chiron', ]); $invoice = Cashier::stripe()->invoices->create([ 'customer' => $user->stripe_id, ]); Cashier::stripe()->invoices->pay($invoice->id, ); $invoice = $user->invoices()->first();
Well, it's quite long 😏.
Small tip: you can call the stripe() method on the user instance directly 😉.
If you did get to this extra mile and still want to see more, I got your back check the demo project on my GitHub Safemood.
Hope this was helpful to you, for more subscribe to my newsletter to get instant updates.
Feel free to contact me if you did have a problem with the demo project or just wanna say Hi! You can find me on Twitter or LinkedIn.