In this article, we will guide you through the process of integrating Stripe subscription plans into a Laravel application using the Stripe PHP SDK. We will focus on creating subscription plans, handling Stripe Checkout sessions, and setting up payment processing.
Install Stripe PHP SDK
First, you need to install the Stripe PHP SDK in your Laravel application. You can do this by running the following command:
composer require stripe/stripe-php
This will install the latest version of the Stripe SDK, allowing you to interact with the Stripe API.
Set Up Stripe API Keys in .env
Next, add your Stripe API keys to your .env
file. You can find your API keys in the Stripe Dashboard under Developers > API keys.
STRIPE_KEY=your-publishable-key
STRIPE_SECRET=your-secret-key
Make sure to replace your-publishable-key
and your-secret-key
with the actual values from your Stripe account.
Set Up Stripe Configuration
In the config/services.php
file, add the Stripe configuration.
'stripe' => [
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
Create the Stripe Payment Controller
Now, let’s create a controller to handle Stripe payment processes. This controller will be responsible for creating checkout sessions and redirecting users to the Stripe Checkout page.
Run the following command to generate a controller:
php artisan make:controller PaymentController
Now, open the PaymentController.php
file and add the following code:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stripe\Stripe;
use Stripe\Checkout\Session;
use Stripe\PaymentIntent;
class PaymentController extends Controller
{
public function __construct()
{
Stripe::setApiKey(env('STRIPE_SECRET'));
}
/**
* Create a Stripe Checkout session.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function createCheckoutSession(Request $request)
{
$checkout_session = Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => 'Sample Product',
],
'unit_amount' => 2000, // Amount in cents (e.g., $20.00)
],
'quantity' => 1,
]],
'mode' => 'payment', // Use 'subscription' for recurring payments
'success_url' => route('payment.success') . '?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('payment.cancel'),
]);
return redirect()->away($checkout_session->url);
}
/**
* Handle the successful payment response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function success(Request $request)
{
// Retrieve the session ID from the query parameters
$session_id = $request->query('session_id');
$session = Session::retrieve($session_id);
// You can use session data to confirm payment or create a user order
// For now, we’ll just show a success message
return view('payment.success', ['session' => $session]);
}
/**
* Handle the canceled payment response.
*
* @return \Illuminate\Http\Response
*/
public function cancel()
{
// If the payment is canceled, show a cancellation page
return view('payment.cancel');
}
}
Define Routes
Next, define the routes for the Stripe Checkout session creation and payment success/cancel pages. Add these routes in your routes/web.php
file:
use App\Http\Controllers\PaymentController;
Route::get('payment/checkout', [PaymentController::class, 'createCheckoutSession'])->name('payment.checkout');
Route::get('payment/success', [PaymentController::class, 'success'])->name('payment.success');
Route::get('payment/cancel', [PaymentController::class, 'cancel'])->name('payment.cancel');
Create Success and Cancel Views
Now, create the resources/views/payment/success.blade.php
and resources/views/payment/cancel.blade.php
views.
resources/views/payment/success.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Payment Success</title>
</head>
<body>
<h1>Payment was successful!</h1>
<p>Payment Session ID: {{ $session->id }}</p>
</body>
</html>
resources/views/payment/cancel.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Payment Canceled</title>
</head>
<body>
<h1>Payment was canceled.</h1>
<p>Please try again later.</p>
</body>
</html>
Testing the Stripe Integration
To test your integration, follow these steps:
- Ensure your environment is set to
sandbox
(for testing) in the.env
file. - Run the application and visit
/payment/checkout
to initiate a payment. - You will be redirected to Stripe’s Checkout page where you can make a payment using the test card information provided by Stripe.
In this article, we demonstrated how to integrate Stripe subscription plans in a Laravel application using Stripe’s Checkout Session. We created a payment controller, handled successful and canceled payments, and set up routes and views to handle the process.