How to Implement Stripe Payment Gateway in Laravel

Minahil Faisal
 import os
import logging
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import stripe
import uvicorn

# Constants
STRIPE_SECRET_KEY = os.environ['STRIPE_SECRET_KEY']
YOUR_DOMAIN = os.environ['YOUR_DOMAIN']

# Configure Stripe API key
stripe.api_key = STRIPE_SECRET_KEY

# FastAPI app initialization
app = FastAPI()

# CORS configuration
origins = ["*"]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,

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.
Category
Technology
Last published
June 15, 2024

More templates like this

Login and Registration

This powerful app skeleton is a good starting place for apps that require login and registration

Laravel
Fast API
Flask

Stripe API Testing

This application utilizes Flask for the backend and JavaScript with Stripe API for the frontend. It allows users to test various Stripe functionalities such as adding a card to a customer, creating a charge, and retrieving customer details. The backend interacts with the Stripe API using the stripe library, while the frontend provides a simple form for users to input their Stripe API secret and an optional customer ID. Upon submitting the form, the frontend sends a POST request to the backend, which then executes the specified tests and returns the results to be displayed on the page. Made by BaranDev[https://github.com/BaranDev]

Flask
Stripe

Connect Payout with Stripe API

This app uses the Stripe API to create payouts for Connect Stripe Accouts and allows users to modify the payout schedule. It includes a Flask web service with an endpoint for this purpose. The backend makes API calls to create a transfer of funds and update the payout schedule using the Stripe API and the submitted form data.

Flask
Stripe
Python
Home
/
How to Implement Stripe Payment Gateway in Laravel