Stripe Payment Gateway Integration in Laravel 8

Reading Time: 7 minutes

As we all know in most web application either in Laravel 8 or any of the PHP Framework, we sometimes need to integrate payment gateway. Inside this article we will learn Stripe payment gateway integration in Laravel 8.

How to Integrate Stripe payment gateway in Laravel 8, we will see each steps in detailed concept.

Let’s get started.

Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

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

To start the development server of Laravel –

php artisan serve


Assuming laravel already installed inside your system.

Stripe PHP Package Installation

We will install stripe package via composer into laravel 8 setup. Open laravel 8 project in terminal and hit this composer command.

$ composer require stripe/stripe-php

Set Stripe API Key and SECRET

Open up .env file of laravel 8 application. Into the file, set stripe api key and secret.


This is our stripe sandbox detail, you can replace it with your own. If you are wondering to find stripe test account keys. Follow these steps –

  • You need to enable Viewing test data to use Test account details
  • Go to Developer >> Click on API keys

Create Routes

Next, we need to set Laravel 8 stripe payment gateway application routes. To configure routes, open up /routes/web.php file.

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

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

Create Controller File

Create application controller in setup. To create controller file, back to terminal and hit this command to create file.

$ php artisan make:controller StripePaymentController

This command will creates a file inside /app/Http/Controllers/StripePaymentController.php. Open file into any of your editor and paste the given code.

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" => 70 * 100,
                "currency" => "usd",
                "source" => $request->stripeToken,
                "description" => "Test payment via Stripe From" 
        Session::flash('success', 'Payment done successfully!');
        return back();

Create Blade File

Let’s create blade template file for setting the layout of payment details for user. Create a file stripe.blade.php inside /resources/views/stripe.blade.php. Open file and paste the given code

<!DOCTYPE html>
    <title>Laravel 8 - Stripe Payment Gateway Integration -</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">
    <h3 style="text-align: center;">Laravel 8 - Stripe Payment Gateway Integration -</h3><br/>
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-default credit-card-box">
                <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 ($70)</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=text]'].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 + "'/>");

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL – http://localhost:8000/stripe

Name: Sample
Number: 4242 4242 4242 4242
CSV: 123
Expiration Month: 04
Expiration Year: 2024

We hope this article helped you to learn about stripe Payment Gateway Integration in Laravel 8 in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

1 thought on “Stripe Payment Gateway Integration in Laravel 8”

Comments are closed.