Introduction to Laravel Cashier
Laravel Cashier is a powerful library for handling subscription billing and one-time payments in Laravel applications. It provides an elegant, easy-to-use interface for managing billing services like Stripe and Paddle. With Laravel Cashier, developers can quickly integrate payment processing into their applications, allowing them to focus on creating engaging user experiences.
Benefits of Using Laravel Cashier
Laravel Cashier offers several advantages for developers looking to incorporate billing functionality into their applications:
Easy Billing Integration
Laravel Cashier simplifies the process of integrating billing services with your application. With just a few lines of code, you can set up recurring payments, one-time charges, and more.
Subscription Management
Managing subscriptions is a breeze with Laravel Cashier. It offers an intuitive API for creating, updating, and canceling subscriptions. This makes it easy to handle various subscription plans and manage customer subscriptions.
Invoices and Receipts
Laravel Cashier generates invoices and receipts for your customers, helping you maintain accurate records and provide excellent customer service.
Multiple Payment Gateways
Laravel Cashier supports multiple payment gateways, including Stripe and Paddle, giving you flexibility when choosing a billing provider.
Laravel Cashier Installation and Setup
To get started with Laravel Cashier, follow these steps:
Installing Laravel Cashier
Install the Laravel Cashier package using Composer:
composer require laravel/cashier
Configuring Your Environment
Next, configure your environment by adding your payment gateway API keys and webhook secrets to your .env
file:
STRIPE_KEY=your_stripe_key
STRIPE_SECRET=your_stripe_secret
PADDLE_VENDOR_ID=your_paddle_vendor_id
PADDLE_VENDOR_AUTH_CODE=your_paddle_vendor_auth_code
Migrations and Models
Run the migrations to create the necessary database tables:
php artisan migrate
Finally, add the Billable
trait to your User model:
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use Billable;
}
Creating Subscriptions with Laravel Cashier
Laravel Cashier makes it easy to manage subscriptions for your application:
Subscription Plans
To create subscription plans, you’ll need to configure them in your payment gateway (Stripe or Paddle). Once you’ve set up your plans, Laravel Cashier can interact with them seamlessly.
Creating New Subscriptions
To create a new subscription for a user, use the newSubscription
method:
$user->newSubscription('main', 'plan_id')->create($paymentMethod);
Here, 'main'
is the subscription name, 'plan_id'
is the plan identifier from your payment gateway, and $paymentMethod
is the customer’s payment method.
Managing Subscription Status
Laravel Cashier provides various methods to manage and check the status of subscriptions:
$user->subscribed('main')
: Check if the user is subscribed to a plan.$user->subscription('main')->cancel()
: Cancel a subscription.$user->subscription('main')->resume()
: Resume a canceled subscription.
Handling One-Time Payments with Laravel Cashier
Laravel Cashier can also handle one-time payments:
Single Charges
To charge a customer for a one-time payment, use the charge
method:
$user->charge($amount, $paymentMethod);
Here, $amount
is the charge amount, and $paymentMethod
is the customer’s payment method.
Storing Payment Methods
Laravel Cashier allows you to store customer payment methods for later use:
$user->addPaymentMethod($paymentMethod);
You can then retrieve the stored payment methods:
$paymentMethods = $user->paymentMethods();
Working with Invoices in Laravel Cashier
Managing invoices is simple with Laravel Cashier:
Generating Invoices
To generate an invoice, use the invoice
method:
$user->invoice();
You can also generate an invoice for a specific subscription:
$user->subscription('main')->invoice();
Customizing Invoice Appearance
Laravel Cashier allows you to customize the appearance of your invoices using custom views. Simply publish the Cashier views and modify the templates as needed:
php artisan vendor:publish --tag="cashier-views"
Handling Webhooks for Laravel Cashier
Laravel Cashier supports handling webhook events from your payment gateway. To set up webhooks, first define a route for the webhook endpoint:
Route::post(
'webhook/{gateway}',
'\Laravel\Cashier\Http\Controllers\WebhookController@handleWebhook'
);
Then, configure your payment gateway to send webhook events to this endpoint.
Testing Laravel Cashier Applications
Laravel Cashier includes a built-in testing mode, making it easy to test your billing integration. To enable testing, update your .env
file with your gateway’s test API keys.
Laravel Cashier Alternatives
While Laravel Cashier is an excellent choice for managing billing in Laravel applications, there are alternatives available, such as Chargebee and Recurly. These services offer additional features and integrations that may better suit your needs.
Conclusion
Laravel Cashier is a powerful and flexible library for managing subscription billing and one-time payments in Laravel applications. With its simple API and support for multiple payment gateways, it makes it easy for developers to create robust and feature-rich billing systems.
FAQs
Is Laravel Cashier free to use?
Yes, Laravel Cashier is an open-source library and is free to use. However, you may incur charges from your payment gateway provider (Stripe or Paddle) for processing payments.
Can Laravel Cashier handle multiple currencies?
Yes, Laravel Cashier can handle multiple currencies. You’ll need to configure your payment gateway to support the currencies you’d like to accept and then manage currency conversions within your application.
Does Laravel Cashier support other payment gateways besides Stripe and Paddle?
Laravel Cashier currently supports Stripe and Paddle as its primary payment gateways. If you need to use a different payment gateway, you may need to develop a custom integration or consider using an alternative billing library.
How secure is Laravel Cashier?
Laravel Cashier itself doesn’t store any sensitive payment information. Instead, it interacts with your payment gateway (Stripe or Paddle), which handles payment processing and storage of sensitive data. As long as you follow best practices for securing your Laravel application and maintain your payment gateway’s security standards, Laravel Cashier should provide a secure transaction experience.
Can I use Laravel Cashier for one-time payments and donations?
Yes, Laravel Cashier can handle one-time payments using the charge
method, making it suitable for processing single payments or donations.
Is Laravel Cashier compatible with all Laravel versions?
Laravel Cashier is compatible with Laravel 6.0 and above. Make sure to check the official Laravel Cashier documentation for the latest compatibility information and updates.
Can I offer discounts or coupons with Laravel Cashier?
Yes, Laravel Cashier allows you to apply discounts or coupons to your subscriptions. You’ll need to create the discounts or coupons in your payment gateway (Stripe or Paddle) and then apply them in your application using the provided coupon codes.
Can I use Laravel Cashier for handling taxes?
Yes, Laravel Cashier provides tax handling capabilities. You can configure tax rates and apply them to your subscriptions and one-time charges. You’ll also need to manage tax reporting and compliance according to your local regulations.