Site Admin
Site Admin Founder of MeaningArticles
1509 Views

Laravel 8 Stripe Payment Gateway integration example

Hello Dev.

You are running an online store or some kind of paid carrier then probable you need to just accept credit card payments in your application. Stripe is one of the popular payment gateways which accept credit or debit card payment online. additionally, the consumer doesn’t need to leave your website to make the card payment. In this article, we learn Stripe payment gateway integration in Laravel.

In this post i will share you stripe payment gateway integration example in laravel 8, stripe payment gateway is integrated in many website for payment collection from client, In this time many e-commerce website and other shoping websites are use stripe payment gateway. So, here we will learn stripe payment gateway integration in laravel 8.

Let's start laravel 8 stripe payment gateway integration example step by step.

So let's start the lesson...

 

Step 1:  Install Laravel 8

First of all we want to get clean and new Laravel application the use of bellow command, now open your terminal OR command prompt and then fire bellow command and run it:

composer create-project --prefer-dist laravel/laravel laravel_stripe


Step 2: Install Stripe Package In Laravel

In this step we are installation in stripe Package in laravel 8 the usage of composer command.

composer require stripe/stripe-php


Step 3: Configuration of Stripe

Now, we're configure stripe payment gateway as below images and code.

First of all create account on stripe https://dashboard.stripe.com/login

And you may see stripe dashboard like below image, in this image you can see key and secret key of stripe.

Now, open .env file from your project and copy below code

.env

STRIPE_KEY=pk_test_xxxxxx
STRIPE_SECRET=sk_test_xxxxxx

After that, we need to set up the Stripe API key, open config/services.php file, and add below code.

services.php

'stripe' => [
     'secret' => env('STRIPE_SECRET'),
],


Step 4: Create Route

In this step create route as below.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StripeController;

Route::get('stripe', [StripeController::class, 'stripe']);
Route::post('stripe', [StripeController::class, 'stripePost'])->name('stripe.post');

 

Step 5: Add Controller

I have created StripeController in this example So, copy below code in your controller.

StripeController.php

<?php

namespace App\Http\Controllers;
use Session;
use Stripe;

use Illuminate\Http\Request;

class StripeController extends Controller
{
    public function stripe()
    {
        return view('stripe');
    }

    public function stripePost(Request $request)
    {
        Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
        Stripe\Charge::create ([
                "amount" => 100*100,
                "currency" => "INR",
                "source" => $request->stripeToken,
                "description" => "This payment is testing purpose of meaningarticles.com",
        ]);
   
        Session::flash('success', 'Payment Successful !');
           
        return back();
    }
}

 

Step 6: Create Blade File for View

In this step i have created stripe.blade.php for view purpose, so copy and paste below code.
stripe.blade.php

<!DOCTYPE html>
<html>

   <head>
      <title>Laravel 8 Stripe Payment Gateway Integration Example - meaningarticles.com</title>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>      
   </head>

   <body>
      <div class="container">         
         <div class="row">
          <h3 style="text-align: center;margin-top: 40px;margin-bottom: 40px;">Stripe Payment Gateway Integration Example - meaningarticles.com</h3>
            <div class="col-md-6 col-md-offset-3">
               <div class="panel panel-default credit-card-box">
                  <div class="panel-heading" >
                     <div class="row">
                        <h3>Payment Details</h3>
                        <div>                            
                           <img class="img-responsive pull-right" src="http://i76.imgup.net/accepted_c22e0.png">
                        </div>
                     </div>
                  </div>
                  <div class="panel-body">
                     @if (Session::has('success'))
                     <div class="alert alert-success text-center">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                        <p>{{ Session::get('success') }}</p><br>
                     </div>
                     @endif
                     <br>
                     <form role="form" action="{{ route('stripe.post') }}" method="post" class="require-validation" data-cc-on-file="false" data-stripe-publishable-key="{{ env('STRIPE_KEY') }}" id="payment-form">
                        @csrf
                        <div class='form-row row'>
                           <div class='col-xs-12 col-md-6 form-group required'>
                              <label class='control-label'>Name on Card</label> 
                              <input class='form-control' size='4' type='text'>
                           </div>
                           <div class='col-xs-12 col-md-6 form-group required'>
                              <label class='control-label'>Card Number</label> 
                              <input autocomplete='off' class='form-control card-number' size='20' type='text'>
                           </div>                           
                        </div>                        
                        <div class='form-row row'>
                           <div class='col-xs-12 col-md-4 form-group cvc required'>
                              <label class='control-label'>CVC</label> 
                              <input autocomplete='off' class='form-control card-cvc' placeholder='ex. 311' size='4' type='text'>
                           </div>
                           <div class='col-xs-12 col-md-4 form-group expiration required'>
                              <label class='control-label'>Expiration Month</label> 
                              <input class='form-control card-expiry-month' placeholder='MM' size='2' type='text'>
                           </div>
                           <div class='col-xs-12 col-md-4 form-group expiration required'>
                              <label class='control-label'>Expiration Year</label> 
                              <input class='form-control card-expiry-year' placeholder='YYYY' size='4' type='text'>
                           </div>
                        </div>
                      {{-- <div class='form-row row'>
                         <div class='col-md-12 error form-group hide'>
                            <div class='alert-danger alert'>Please correct the errors and try
                               again.
                            </div>
                         </div>
                      </div> --}}
                        <div class="form-row row">
                           <div class="col-xs-12">
                              <button class="btn btn-primary btn-lg btn-block" type="submit">Pay Now</button>
                           </div>
                        </div>
                     </form>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </body>   

</html>

Use below test Credential for testing
Testing Card Credential
Card Name: john doe
Card Number: 4242424242424242
Month: Any Future Month
Year: Any Future Year
CVV: 123

I hope it assists you, thanks for visiting my article if you like my article then share it with your friends on the social media platform.

Happy Coding.....