In today’s digital era, integrating payment gateways into web applications is a fundamental aspect of many businesses. Whether you’re running an e-commerce platform, a subscription-based service, or any other online venture that involves monetary transactions, providing a seamless payment experience for your users is crucial. Laravel, a popular PHP framework, simplifies this process with its powerful ecosystem of packages. Among these, Laravel Cashier stands out as an excellent choice for integrating payment gateways effortlessly, with Stripe being one of the most widely used.
In this guide, we’ll walk through the process of integrating Stripe into a Laravel 10 application using Laravel Cashier. By following these steps, you’ll be able to accept payments securely and efficiently, empowering your application to handle transactions with ease.
1. Setting Up Your Laravel Application:
Assuming you have a Laravel 10 application already set up, the first step is to install Laravel Cashier. You can do this via Composer by running the following command in your terminal:
bash
composer require laravel/cashier
Next, you’ll need to configure your Stripe API keys. Head over to the Stripe dashboard, create an account if you haven’t already, and obtain your API keys. Once you have them, add them to your .env
file:
STRIPE_KEY=your_stripe_public_key
STRIPE_SECRET=your_stripe_secret_key
2. Database Setup:
Laravel Cashier comes with migrations that you need to run to set up your database tables. Execute the following command:
php artisan migrate
This will create tables needed for handling subscriptions, customers, invoices, and other billing-related data.
3. Implementing Stripe Subscription:
With Laravel Cashier, setting up subscription-based billing is remarkably straightforward. You can define your subscription plans and manage subscribers effortlessly. Let’s create a simple subscription plan:
php
use Laravel\Cashier\Cashier;
Cashier::useSubscriptionModel(MySubscription::class);
// Define your plans
Cashier::subscription(‘basic’)
->name(‘Basic Plan’)
->description(‘Basic Monthly Subscription’)
->stripePlan(‘plan_id’)
->features([ ‘Feature 1’, ‘Feature 2’, ]);
4. Frontend Integration:
You’ll need to integrate Stripe Elements or Checkout on your frontend to securely collect payment information from your users. Laravel Cashier provides methods to generate payment forms easily:
php
<form action=”/subscribe” method=”post” id=”payment-form”>
@csrf
<div class=”form-row”>
<label for=”card-element”>
Credit or debit card
</label>
<div id=”card-element”>
<!– A Stripe Element will be inserted here. –>
</div>
<!– Used to display form errors. –>
<div id=”card-errors” role=”alert”></div>
</div>
<button>Submit Payment</button>
</form>
5. Handling Subscriptions:
Once the payment form is submitted, you can handle the subscription creation in your controller:
php
public function subscribe(Request $request)
{
$user = $request->user();
$user->newSubscription(‘default’, ‘plan_id’)->create($request->paymentMethod);
return redirect()->route(‘dashboard’)->with(‘success’, ‘Subscription successful!’);
}
6. Managing Subscriptions:
You can allow users to manage their subscriptions easily. For instance, they can upgrade, downgrade, or cancel their plans. Laravel Cashier provides methods to handle these actions effortlessly.
Integrating Stripe into your Laravel 10 application using Laravel Cashier streamlines the entire payment process, from setup to handling subscriptions. With its intuitive API and comprehensive documentation, Laravel Cashier empowers developers to build robust billing systems with ease. By following the steps outlined in this guide, you can enhance your application with secure and efficient payment capabilities, providing a seamless experience for your users.