How to Integrate Stripe with Laravel: Complete Developer Guide
Learn how to implement secure Stripe payment processing in your Laravel application with this comprehensive step-by-step guide covering everything from basic payments to advanced subscription billing.
Share this article
Why Stripe + Laravel?
Stripe is the most popular payment processor for modern web applications, and Laravel provides excellent tools for building robust payment systems. This combination offers:
- Built-in Laravel Cashier package for subscription billing
- Excellent documentation and developer experience
- PCI compliance out of the box
- Support for global payments and currencies
- Advanced features like marketplace payments and Connect
Prerequisites
Before we start, make sure you have:
- Laravel 10+ application set up
- Stripe account (free to create)
- Basic knowledge of Laravel and PHP
- Composer installed
Step 1: Install Laravel Cashier
Laravel Cashier is the official package for Stripe integration. Install it using Composer:
composer require laravel/cashier
Publish the migration files:
php artisan vendor:publish --tag="cashier-migrations"
Step 2: Configure Environment Variables
Add your Stripe keys to your .env file. You can find these in your Stripe Dashboard:
STRIPE_KEY=pk_test_your_publishable_key
STRIPE_SECRET=sk_test_your_secret_key
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret
Step 3: Set Up the User Model
Add the Billable trait to your User model to enable Stripe functionality:
<?php
namespace App\Models;
use Laravel\Cashier\Billable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Billable;
// Your existing User model code...
}
Step 4: Create Payment Controller
Create a controller to handle payment processing:
php artisan make:controller PaymentController
Step 5: Implement One-Time Payment
Here's how to process a one-time payment:
public function processPayment(Request $request)
{
$request->validate([
'payment_method' => 'required|string',
'amount' => 'required|integer|min:50', // $0.50 minimum
]);
try {
$user = auth()->user();
$payment = $user->charge(
$request->amount,
$request->payment_method,
[
'return_url' => route('payment.success'),
]
);
return response()->json([
'success' => true,
'payment_intent' => $payment->id,
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage(),
], 400);
}
}
Step 6: Handle Subscription Billing
For recurring payments, you'll need to create subscription plans in Stripe and then subscribe users:
public function subscribe(Request $request)
{
$user = auth()->user();
$subscription = $user->newSubscription('default', 'price_premium_monthly')
->create($request->payment_method);
return redirect()->route('dashboard')
->with('success', 'Subscription created successfully!');
}
Step 7: Set Up Webhooks
Webhooks are crucial for handling subscription updates, failed payments, and other events. Laravel Cashier provides built-in webhook handling:
php artisan cashier:webhook
Add the webhook route to your routes/web.php file:
Route::post('/stripe/webhook', [WebhookController::class, 'handleWebhook']);
Security Best Practices
Important Security Considerations
- Never store credit card information in your database
- Always validate webhook signatures
- Use HTTPS in production
- Implement proper error handling
- Log all payment activities for auditing
Testing Your Integration
Stripe provides test card numbers for development:
- 4242424242424242 - Successful payment
- 4000000000000002 - Declined payment
- 4000000000003220 - 3D Secure authentication
Next Steps
Now that you have a basic Stripe integration working, consider implementing:
- Customer portal for subscription management
- Proration handling for plan changes
- Failed payment retry logic
- Invoice PDF generation
- Usage-based billing for metered subscriptions
Need Professional Help?
Our team has implemented Stripe integrations for hundreds of businesses. Get expert help with your payment integration project.
Share this article
Need Professional Help?
Our team specializes in custom integrations and can help with your specific requirements.
Get Expert Integration Support