About this template
To integrate a custom Stripe payment gateway in Laravel 8 or Laravel 9, you need both a backend and a frontend. This example template enables you to quickly set up the backend service. It is compatible with any price point you have established through the Stripe API. After adding the API key and directing the backend service to the price ID, you can activate the backend service by clicking the test button. Then, by integrating the Stripe frontend code into a Laravel component, you instantly create a custom payment page in Laravel. This method can be used to set up various types of payment pages in Laravel, including one-time payments and recurring subscriptions.
Introduction to the Stripe Payment Gateway Integration Template for Laravel
This template is designed to help you integrate Stripe's payment gateway into your Laravel application. It provides a backend service using FastAPI to create Stripe checkout sessions and retrieve their status. The template is suitable for setting up various types of payment pages in Laravel, including one-time payments and recurring subscriptions. By following the steps outlined in this article, you will be able to set up a custom Stripe payment gateway in your Laravel application.
Clicking Start with this Template
To begin using this template, click on the "Start with this Template" button on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code.
Initial Setup: Adding Environment Secrets
Before testing the application, you need to set up the required environment secrets. These are not set in your operating system but within the Lazy Builder under the Environment Secrets tab.
1. Obtain your Stripe secret key and publishable key from your Stripe dashboard.
2. Set the `STRIPE_SECRET_KEY` environment secret in the Lazy Builder with your Stripe secret key.
3. Set the `YOUR_DOMAIN` environment secret with the domain where your Laravel application is hosted.
Test: Pressing the Test Button
Once you have set up the environment secrets, press the "Test" button on the Lazy platform. This will deploy the app and launch the Lazy CLI. If the code requires any user input, you will be prompted to provide it through the Lazy CLI.
Using the App
After pressing the "Test" button, Lazy will provide you with a dedicated server link to use the API. If you're using FastAPI, Lazy will also provide a link to the API documentation.
Integrating the App
To integrate the backend service with your Laravel frontend, follow these steps:
1. Replace `"LAZY SERVER LINK"` in the provided PHP script with the endpoint URL of your published app that you received from Lazy.
2. Replace `"PRICE_ID"` with the actual price ID that you have set up in your Stripe dashboard.
3. Replace `"PUBLISHABLE STRIPE API KEY"` with your actual publishable API key from Stripe.
Here is the PHP script to insert into your Laravel controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Stripe\Stripe;
class CheckoutController extends Controller
{
public function create(Request $request)
{
$response = Http::post('LAZY SERVER LINK/create-checkout-session', [
'price_id' => 'PRICE_ID',
'months' => 1,
]);
$checkout_session = $response->json();
return view('checkout', ['sessionId' => $checkout_session['sessionId'], 'stripePublicKey' => 'PUBLISHABLE STRIPE API KEY']);
}
}
Next, add the following script to your 'checkout.blade.php' file:
<script src="https://js.stripe.com/v3/"></script>
<script>
var stripe = Stripe('{{ $stripePublicKey }}');
var sessionId = '{{ $sessionId }}';
stripe.redirectToCheckout({ sessionId: sessionId }).then(function (result) {
if (result.error) {
console.error(result.error.message);
}
});
</script>
By following these steps, you will have successfully integrated a custom Stripe payment gateway into your Laravel application using the Lazy template.