How to integrate stripe payment gateway in laravel application with example

How to integrate stripe payment gateway in laravel application with example

Hello guys, in this article i am going to write about how to integrate stripe payment gateway in laravel application with example. Payment gateway integration is an important task for a web developer. I think all the developers should have the experience of integration of payment gateware.

After reading this article you will not get any difficulty in the integration of stripe payment gateway in your laravel web application.

Most of the payment gateway provides integration methods and their api documentation but not all documentation are esay to integrate. So here i am with you to help you in the stripe payment gateway application in laravel.

We all know about the stripe payment gateway. It is very popular and secure. And all the payment gateways provide sandbox and production environment for gateway. So in the integration process we will use sandbox or testing environment.

So for the integration first we need a stripe payment developer account. From where we can get api key and secret which will be needed for authentication of the api uses. Then after we will use stripe/stripe-php laravel package.

After getting ready with the developer account and keys, start the integration process step by step.

Step 1:  Install stripe-php Package

For the installation of the laravel stripe package you will need to run the following composer command in your terminal. It will download required package files in your application.

composer require stripe/stripe-php

Step 2: Set Stripe API Key and Secret

Before starting any step do you remember you have already created a stripe developer account. From there you will need to copy api key and secret and add in the environment(.env) file.




Step 3: Create Routes

In the third step we will create routes to handle the multiple requests required in the payment process. Here we willcreate two route. So add these routes into the routes/api.php file.


Route::get('stripe', 'StripePaymentController@stripe');
Route::post('stripe', 'StripePaymentController@stripePost')->name('');

Step 4: Create controller file

In this step we will create a controller file and the path of this controller file would be app/Http/Controllers/StripePaymentController.php and open this newly created controller file and copy the following lines of code into this controller file.


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 Description." 
        Session::flash('success', 'Payment Successful!');
        return back();

Step 5: Create View (Blade File)

After creating all the logics into the controller file, we will create view for this example. So let’s create  view file stripe.blade.php under the resources/views directory and put the follwing code into this view file.


<!DOCTYPE html>
    <title>How to integrate stripe payment gateway in laravel application with 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>How to integrate stripe payment gateway in laravel application with example</h1>
    <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 + "'/>");

After completing this step you have done stripe payment gateway integration process in your laravel application.

For testing purpose you can get sample card details for various result from the official stripe website .