Introduction
Integrating payment gateways into a Laravel application is essential for building modern e-commerce platforms, subscription services, or any app that handles financial transactions. Laravel’s clean architecture and powerful ecosystem make it easy to work with popular payment providers like Stripe, PayPal, and Razorpay.
In this article, you’ll learn how to seamlessly integrate payment gateways in Laravel, along with best practices for handling payments securely and efficiently.
💳 Popular Payment Gateway Options
- Stripe: Developer-friendly with robust APIs.
- PayPal: Widely trusted and supports global currencies.
- Razorpay: Popular for Indian merchants with fast onboarding.
We’ll use Stripe for examples, but the general principles apply to most gateways.
1. Install Stripe PHP SDK
✅ Use Composer to install Stripe:
composer require stripe/stripe-php
✅ Add your Stripe keys to .env
:
STRIPE_KEY=your_publishable_key
STRIPE_SECRET=your_secret_key
2. Create a Payment Controller
✅ Generate a controller:
php artisan make:controller PaymentController
✅ Basic charge logic (Stripe):
use Stripe\Stripe;
use Stripe\Charge;
public function charge(Request $request) {
Stripe::setApiKey(env('STRIPE_SECRET'));
$charge = Charge::create([
'amount' => 1000, // Amount in cents
'currency' => 'usd',
'description' => 'Test Payment',
'source' => $request->stripeToken,
]);
return back()->with('success', 'Payment successful!');
}
3. Create Payment Form with Stripe.js
✅ Example Blade view:
<form action="/charge" method="POST" id="payment-form">
@csrf
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{ env('STRIPE_KEY') }}"
data-amount="1000"
data-name="My Laravel App"
data-description="Test payment"
data-currency="usd">
</script>
</form>
4. Define Payment Routes
✅ Add these to web.php
:
Route::get('/checkout', function () {
return view('checkout');
});
Route::post('/charge', [PaymentController::class, 'charge']);
5. Validate and Secure Transactions
✅ Recommendations:
- Always validate incoming data.
- Use HTTPS for all production payment requests.
- Store only transaction IDs, not full card details.
✅ Optional: Use Stripe Webhooks for verification
php artisan make:controller WebhookController
6. Supporting Subscriptions (Optional)
✅ Use Laravel Cashier for Stripe or Paddle:
composer require laravel/cashier
✅ Example subscription setup:
$user->newSubscription('default', 'price_id')->checkout();
Cashier handles invoicing, cancellations, and renewals.
7. Testing Payments
✅ Stripe test card:
Card Number: 4242 4242 4242 4242
Expiry: Any future date
CVC: Any 3 digits
✅ Use Stripe’s test mode for safe experimentation.
🧠 Conclusion
Integrating payment gateways in Laravel is straightforward thanks to its clean architecture and the availability of robust SDKs. Whether you’re accepting one-time payments, subscriptions, or donations, Laravel and tools like Stripe or Cashier make it secure and developer-friendly.
🔑 Key Takeaways:
- Use official SDKs for secure, up-to-date integration.
- Never store sensitive card data on your server.
- Use Laravel Cashier for subscriptions and billing features.
- Always test thoroughly in sandbox mode before going live.
By following best practices and using Laravel’s powerful tools, you can build a fully functional, secure, and scalable payment system. 💰