In this blog post, we will guide you through automating the process of generating and emailing invoices with Laravel, specifically using the Laravel Cashier package. By the end of this post, you'll be able to automatically generate professional-looking invoices, customized to your needs, and have them automatically emailed to your customers.
Prerequisites
Before we dive into the main content, it's important to ensure you have the following prerequisites in place.
- Laravel Setup: You should have a Laravel application up and running. Laravel is a powerful PHP framework that simplifies the process of building complex web applications. Learn how to get started with Laravel.
- Laravel Cashier: Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services. It handles almost all of the boilerplate subscription billing code you are dreading writing. You should have it installed and configured with your Stripe account. Learn how to set up Cashier.
- Subscriptions: You should have your subscription models set up in Laravel. If you haven't done that, you can refer to our previous guide on How to set up recurring payments in Laravel which will guide you step-by-step on setting up subscriptions using Laravel and Cashier.
- Email Provider: Lastly, you should have access to an email provider for sending emails. In this guide, we will discuss some common email providers such as Mailgun and Sendgrid, which offer free tiers for small businesses or startups.
Once you have these prerequisites in place, you are ready to automate the process of invoice generation and emailing in your Laravel application. Let's begin.
Previewing the invoice
This will only be used for us to test and to see what the final invoice looks like as a PDF. It is a great way to quickly iterate and make changes to the invoice before we start automating the process.
Set up a new subscription
This article assumes you have already have a subscription for your user. If not, here's a quick reminder of how you would do that. After setting up Laravel Cashier, creating a new subscription for a user is quite straightforward. Here is an example:
$user = User::find(1);
$user->newSubscription('default', 'premium')->create($creditCardToken);
In the above example, we find the user and create a new subscription for them. The 'default'
in the newSubscription()
function is the name of the subscription, and 'premium'
is the specific Stripe plan's ID to which the user is subscribing. The $creditCardToken
variable is obtained when collecting the user's payment method in the frontend with Stripe's tools. Read more about setting up subscriptions for users here.
Preview the invoice
Now that we've created a subscription, we can generate an invoice for it. We'll create an HTTP route that fetches the first invoice and downloads it as a PDF.
Open your web.php
file located at routes/web.php
and add the following route:
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
Route::get('/invoice/preview', function () {
$user = Auth::user(); // fetch the authenticated user
$invoice = $user->invoices()->first(); // get the first invoice
if ($invoice) {
return $invoice->download([
'vendor' => 'Your Company',
'product' => 'Your Product',
'street' => 'Main Str. 1',
'location' => '2000 Antwerp, Belgium',
'phone' => '+32 499 00 00 00',
'email' => 'info@example.com',
'url' => 'https://example.com',
'vendorVat' => 'BE123456789',
]);
}
return new Response('No invoice found', 404);
});
In the above route, we're authenticating the user, fetching their first invoice, and using the downloadInvoice()
method to generate and download the invoice as a PDF.
You can now preview the invoice by navigating to http://localhost/invoice/preview
in your browser.
You should now see a generated PDF of a subscription invoice. In the next section, we'll delve into how you can automate the process of emailing them to your users.
Automating emailing of invoices
Having set up and customized our invoices, the next step is to automate the process of sending these invoices via email to our users. We can tap into webhooks to automatically react to new invoices and send them to your customers via email whenever they are created.
Top comments (0)