Stripe Payment Gateway Integration In Laravel 8 With Example




Laravel 8 Stripe Payment Gateway Integration Example

In this tutorial, We will discuss stripe payment gateway integration in laravel 8. You will learn stripe payment gateway in laravel 8 by an example. step by step explain the laravel 8 payment integration stripe example.

You need to create a Stripe Developer account and get more secrets of API from there. Then we will use the Stripe / Stripe-PHP Composer library for Stripe Payment Gateway in Laravel 8. We are showing step by step integration for Stripe Payment Gateway.

Stripe is a very popular and secure Internet payment gateway company that helps in accepting payments worldwide. Stripe provides you with a really good development interface to get you started and you have to pay a membership fee to know that it offers a free developer account before starting the code in your app.

So, let's follow some steps to make an example of the stripe payment gateway in the Laravel 8 application.

  • Install Laravel 8
  • Install stripe-php Package
  • Set Stripe API Key and SECRET
  • Add Routes
  • Create Controller File
  • Create Blade File

Step 1: Install Laravel 8

First Step, you need to download or install the laravel web application new setup on your local machine. you can use the following command to download or install the laravel application new setup:

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

If you want to install a specific version of laravel, you can follow the following tutorial.

Step 2: Install stripe-php Package

In this step, we need to install Stripe-PHP through the Composer Package Manager, so one of your terminal and fire bellow commands:

    composer require stripe/stripe-php

Step 3: Set Stripe API Key and SECRET

Now, we need to set the stripe key and secret. so first you can go on Stripe website and create a development stripe account key and secret and add bellow:



Step 4: Add Routes

Next step, we will add two routes for get request and another for post request and add the following routes into your web.php file:


    use Illuminate\Support\Facades\Route;
    use App\Http\Controllers\StripePaymentController;
    | Web Routes
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    Route::get('stripe', [StripePaymentController::class, 'stripe']);
    Route::post('stripe', [StripePaymentController::class, 'stripePost'])->name('');

Step 5: Create Controller File

in next step, now we have create new controller as StripePaymentController, So let's create controller:


    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use Session;
    use Stripe;
    class StripePaymentController extends Controller
         * success response method.
         * @return \Illuminate\Http\Response
        public function stripe()
            return view('stripe');
         * success response method.
         * @return \Illuminate\Http\Response
        public function stripePost(Request $request)
            Stripe\Charge::create ([
                    "amount" => 100 * 100,
                    "currency" => "usd",
                    "source" => $request->stripeToken,
                    "description" => "Test payment from" 
            Session::flash('success', 'Payment successful!');
            return back();

Step 6: Create Blade File

In last step, we have to create stripe.blade.php(resources/views/stripe.blade.php) for layout and write code of jquery to get token from stripe here and put bellow code:


    <!DOCTYPE html>
        <title>Laravel 8 - Stripe Payment Gateway Integration Example -</title>
        <link rel="stylesheet" href="" />
        <script src=""></script>
        <style type="text/css">
            .panel-title {
            display: inline;
            font-weight: bold;
            .display-table {
                display: table;
            .display-tr {
                display: table-row;
            .display-td {
                display: table-cell;
                vertical-align: middle;
                width: 61%;
    <div class="container">
        <h1>Laravel 8 - Stripe Payment Gateway Integration Example <br/> w3alert
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-default credit-card-box">
                    <div class="panel-heading display-table" >
                        <div class="row display-tr" >
                            <h3 class="panel-title display-td" >Payment Details</h3>
                            <div class="display-td" >                            
                                <img class="img-responsive pull-right" src="">
                    <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>
                                action="{{ route('') }}" 
                                data-stripe-publishable-key="{{ env('STRIPE_KEY') }}"
                            <div class='form-row row'>
                                <div class='col-xs-12 form-group required'>
                                    <label class='control-label'>Name on Card</label> <input
                                        class='form-control' size='4' type='text'>
                            <div class='form-row row'>
                                <div class='col-xs-12 form-group card required'>
                                    <label class='control-label'>Card Number</label> <input
                                        autocomplete='off' class='form-control card-number' size='20'
                            <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'
                                <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'
                                <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'
                            <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
                            <div class="row">
                                <div class="col-xs-12">
                                    <button class="btn btn-primary btn-lg btn-block" type="submit">Pay Now ($100)</button>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
    $(function() {
        var $form         = $(".require-validation");
        $('form.require-validation').bind('submit', function(e) {
            var $form         = $(".require-validation"),
            inputSelector = ['input[type=email]', 'input[type=password]',
                             'input[type=text]', 'input[type=file]',
                             'textarea'].join(', '),
            $inputs       = $form.find('.required').find(inputSelector),
            $errorMessage = $form.find('div.error'),
            valid         = true;
            $inputs.each(function(i, el) {
              var $input = $(el);
              if ($input.val() === '') {
            if (!$'cc-on-file')) {
                number: $('.card-number').val(),
                cvc: $('.card-cvc').val(),
                exp_month: $('.card-expiry-month').val(),
                exp_year: $('.card-expiry-year').val()
              }, stripeResponseHandler);
      function stripeResponseHandler(status, response) {
            if (response.error) {
            } else {
                /* token contains id, last4, and card type */
                var token = response['id'];
                $form.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");

Now you can run and check.