How to Add Stripe Payment Getaway on Laravel | Latest 2024

armanrahman

Arman Rahman

Posted on August 21, 2024

How to Add Stripe Payment Getaway on Laravel | Latest 2024

On your Laravel project, you can easily add a stripe payment gateway to get payment from customers.

Image

Step 1: First of all, you need to install the package.

composer requires stripe/stripe-php
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Stripe account to get Client Secret and Client Key

Visit This Site: https://dashboard.stripe.com/dashboard

Image

Step 3: add this on .env File

STRIPE_KEY=pk_test_reFxwbsm9cdCKASdTfxAR
STRIPE_SECRET=sk_test_oQMFWteJiPd4wj4AtgApY
Enter fullscreen mode Exit fullscreen mode

Step 4: Add those methods on your controller

// Checkout View Page
    public function stripe(): View
    {
        return view('stripe');
    }

// Checkout Post button Page
    public function stripeCheckout(Request $request)
    {
        $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
        $redirectUrl = route('stripe.checkout.success').'?session_id={CHECKOUT_SESSION_ID}';
        $response =  $stripe->checkout->sessions->create([
                'success_url' => $redirectUrl,
                'customer_email' => 'demo@gmail.com',
                'payment_method_types' => ['link', 'card'],
                'line_items' => [
                    [
                        'price_data'  => [
                            'product_data' => [
                                'name' => $request->product,
                            ],
                            'unit_amount'  => 100 * $request->price,
                            'currency'     => 'USD',
                        ],
                        'quantity'    => 1
                    ],
                ],
                'mode' => 'payment',
                'allow_promotion_codes' => true
            ]);
        return redirect($response['url']);
    }

// Success Return Page
    public function stripeCheckoutSuccess(Request $request)
    {
        $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
        $session = $stripe->checkout->sessions->retrieve($request->session_id);
        info($session);
        return redirect()->route('stripe.index')

                         ->with('success', 'Payment successful.');
    }
Enter fullscreen mode Exit fullscreen mode

Step 5: Create Routes:

Route::controller(StripePaymentController::class)->group(function(){
    Route::get('stripe', 'stripe')->name('stripe.index');
    Route::get('stripe/checkout', 'stripeCheckout')->name('stripe.checkout');
    Route::get('stripe/checkout/success', 'stripeCheckoutSuccess')->name('stripe.checkout.success');

});
Enter fullscreen mode Exit fullscreen mode

Step 6: Call this route on checkout button/purchase button

<a href="{{ route('stripe.checkout', ['price' => 1000, 'product' => 'Platinum']) }}" class="btn btn-primary">Checkout</a>
Enter fullscreen mode Exit fullscreen mode

Image

Now run your project and use https::// requests.

Test with this:

Name: Test
Number: 4242 4242 4242 4242
CSV: 123
Expiration Month: 12
Expiration Year: 2028

💖 💪 🙅 🚩
armanrahman
Arman Rahman

Posted on August 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related