What is stripe ?

Stripe is a one of the online payment processing company and it's provide too many online payment service like online card payment, online checkout payment service, online subscription base payment service etc...

We will share with you in this article how to integrate stripe payment integration in your laravel applicaion. it can be helped you in integration payment system in your laravel application. stripe is provice very easy payment integration payment API and also have large or rich documentation about API or integration any other services.

If you before never integrate stripe payment gateway in your laravel application then don't worry we will share with you all things here step by step and very easy way. so, you can understand all the things. so, let's start and following all the steps.

Into the github and packages.org many packages are available related stripe integration but we are here user cartalyst/stripe-laravel. it is one of the best package for integration stripe payment gatway in laravel.

In laravel 6 The package requires PHP 7.2 and follows the FIG standard PSR-1, PSR-2 and PSR-4 to ensure a high level of interoperability between shared PHP code.

Step - 1 Install package

First we need to install cartalyst/stripe-laravel package using run following command in your terminal. we need "cartalyst/stripe-laravel": "11.0.*" version for laravel 6.

composer require "cartalyst/stripe-laravel": "11.0.*"

Step - 2 Set the Service Provider and Facade alias

After installing the package, open your Laravel config file located at config/app.php and add the following lines.

In the $providers array add the following service provider for this package.

Cartalyst\Stripe\Laravel\StripeServiceProvider::class,

In the $aliases array add the following facade for this package.

'Stripe' => Cartalyst\Stripe\Laravel\Facades\Stripe::class,

Step - 3 Set the Api Key

SignIn/signUp in your stripe payment dashboard clicking on this link Stripe Dashboard. and get your APIs key first.

Now you need to setup the Stripe API key, to do this open or create the config/services.php file, and add or update the 'stripe' array:

<?php

return [

    'stripe' => [
        'secret' => 'your-stripe-key-here',
    ],

];

Step - 4 Set the Api Version (optional)

This step is not necessary, but in case you need to use a previous version of Stripe you can do same process as above and add a 'version' key on the array:

<?php

return [

    'stripe' => [
        'secret'  => 'your-stripe-key-here',
        'version' => '2019-02-19',
    ],

];

Step - 5 Create route

After installed package and configure all the set up then next things is make following two route in routes/web.php file.

// Route for stripe payment form.
Route::get('stripe', 'StripeController@payWithStripe')->('stripform');
// Route for stripe post request.
Route::post('stripe', 'StripeController@postPaymentWithStripe')->('paywithstripe');

Step - 6 Create controller

After, done above two routes then we neet to create StripeController.php file help of following artisan command.

php artisan make:controller StripeController

After run this commant your StripeController.php file automatic created on app/Http/Controllers folder. just open it and write following code into that file.

<?php

namespace App\Http\Controllers;

use Validator;
use Illuminate\Http\Request;
use Cartalyst\Stripe\Laravel\Facades\Stripe;
use Stripe\Error\Card;

class StripeController extends Controller
{
    public function payWithStripe(Request $request)
    {
    	return view('stripe-payment-form');
    }

    public function postPaymentWithStripe(Request $request)
    {
        $validator = Validator::make($request->all(), [
        ]);
        $input = $request->all();

        $validation = [
            'card_no' => 'required',
            'exp_month' => 'required',
            'exp_year' => 'required',
            'cvv' => 'required',
            'amount' => 'required',
        ];

        $this->validate($request, $validation);           
        $stripe = Stripe::make('your-stripe-key-here');
        try {
            $token = $stripe->tokens()->create([
                'card' => [
                    'number'    => $request->get('card_no'),
                    'exp_month' => $request->get('exp_month'),
                    'exp_year'  => $request->get('exp_year'),
                    'cvc'       => $request->get('cvv'),
                ],
            ]);
            if (!isset($token['id'])) {
                \Session::put('error','The Stripe Token was not generated correctly');
                return redirect()->route('stripform');
            }
            $charge = $stripe->charges()->create([
                'card' => $token['id'],
                'currency' => 'USD',
                'amount'   => $request->get('amount'),
                'description' => 'Add in wallet',
            ]);
            if($charge['status'] == 'succeeded') {
                /**
                * Write Here Your Database insert logic.
                */
                \Session::put('success','Money add successfully in wallet');
                return redirect()->route('stripform');
            } else {
                \Session::put('error','Money not add in wallet!!');
                return redirect()->route('stripform');
            }
        } catch (Exception $e) {
            \Session::put('error',$e->getMessage());
            return redirect()->route('stripform');
        } catch(\Cartalyst\Stripe\Exception\CardErrorException $e) {
            \Session::put('error',$e->getMessage());
            return redirect()->route('stripform');
        } catch(\Cartalyst\Stripe\Exception\MissingParameterException $e) {
            \Session::put('error',$e->getMessage());
            return redirect()->route('stripform');
        }
    }
}

Step - 7 Create blade file

After done controller file then we create one laravel blade HTML file which simple card payment design. Now we are create one simple blade file in resources/views/stripe-payment-form.blade.php file and here we are make very simple html layout for make card payment.

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">{{ __('Stripe Payment') }}</div>

                <div class="card-body">
                    @if ($message = Session::get('success'))
                    <div class="custom-alerts alert alert-success">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                        {!! $message !!}
                    </div>
                    <?php Session::forget('success');?>
                    @endif
                    @if ($message = Session::get('error'))
                    <div class="custom-alerts alert alert-danger">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                        {!! $message !!}
                    </div>
                    <?php Session::forget('error');?>
                    @endif
                    <form method="POST" action="{{ route('paywithstripe') }}">
                        @csrf
                        <div class="form-group row">
                            <div class="col-md-12">
                                <input id="card_no" type="text" class="form-control @error('card_no') is-invalid @enderror" name="card_no" value="{{ old('card_no') }}" required autocomplete="card_no" placeholder="Card No." autofocus>
                                @error('card_no')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-md-6">
                                <input id="exp_month" type="text" class="form-control @error('exp_month') is-invalid @enderror" name="exp_month" value="{{ old('exp_month') }}" required autocomplete="exp_month" placeholder="Exp. Month (Eg. 02)" autofocus>
                                @error('exp_month')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                            <div class="col-md-6">
                                <input id="exp_year" type="text" class="form-control @error('exp_year') is-invalid @enderror" name="exp_year" value="{{ old('exp_year') }}" required autocomplete="exp_year" placeholder="Exp. Year (Eg. 2020)" autofocus>
                                @error('exp_year')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-md-12">
                                <input id="cvv" type="password" class="form-control @error('cvv') is-invalid @enderror" name="cvv" required autocomplete="current-password" placeholder="CVV">
                                @error('cvv')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-md-12">
                                <input id="amount" type="text" class="form-control @error('amount') is-invalid @enderror" name="amount" required autocomplete="current-password" placeholder="Amount">
                                @error('amount')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-primary btn-block">
                                    {{ __('PAY NOW') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Test card data

Card No : 4242424242424242 / 4012888888881881
Month : any future month
Year : any future Year
CVV : any 3 digit No.

Conclusion

As you can see, stripe payment gateway integration is very easy to use in laravel application help of cartalyst/stripe-laravel payckage.

We are hope this tutorials help everyone. if you have any issues or question reagarding stripe payment integration so please comment bellow. Thanks..


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

3 Comments

  • Image
    haithem
    March 24, 2020 Reply

    (cartalyst/stripe-laravel payckage) support bancontact payment ?

  • Image
    Amir
    February 14, 2020 Reply

    Hi,
    How can we get transaction_id in response?

  • Image
    shinn
    October 14, 2019 Reply

    why do you validate twice in funcion postPaymentWithStripe?

    Image
    Harsukh Makwana
    Tue October, 2019

    Oho, yes it is by mistakes.