How to Integrate Stripe Payment Gateway in Laravel 5.4?

By Hardik Savani November 5, 2023 Category : PHP Laravel

Today, I am going to show you How to integrate stripe payment gateway in our Laravel application. In this tutorial i explain step by step example code of How to integrate stripe payment gateway.

Here i give you full example of How to integrate stripe payment gateway step by step like create laravel project, migration, model, route, blade file etc. So you have to just follow few step as listed bellow.

Follow Bellow Few Step:

1)Install Laravel Application

2)Database Configuration

3)Install Required Packages

4)Set Stripe client

5)create route

6)create controller

7)create view file

Preview:

Step 1 : Install Laravel Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Step 2 : Database Configuration

In this step, we require to make database configuration, you have to add following details on your .env file.

1.Database Username

1.Database Password

1.Database Name

In .env file also available host and port details, you can configure all details as in your system, So you can put like as bellow:

.env

DB_HOST=localhost

DB_DATABASE=homestead

DB_USERNAME=homestead

DB_PASSWORD=secret

Step 3 : Install Required Packages

We have required following packages for integrate stripe payment in our laravel application.

add following two package in your composer.json file.

"cartalyst/stripe-laravel": "2.0.*",

then after run following command in your terminal

php artisan vendore:publlish

When done installation proccess then after open your .env file and set following value for your Stripe accound

STRIPE_KEY=XXXXXXXXXX

STRIPE_SECRET=XXXXXXXXXX

Step 4: Set following two line in your config/app.php

'providers' => [

....

Cartalyst\Stripe\Laravel\StripeServiceProvider::class,

],

'aliases' => [

....

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

],

Step 5: Create Route

In this is step we need to create route for stripe payment. so open your routes/web.php file and add following route.

routes/web.php

Route::get('addmoney/stripe', array('as' => 'addmoney.paywithstripe','uses' => 'AddMoneyController@payWithStripe',));

Route::post('addmoney/stripe', array('as' => 'addmoney.stripe','uses' => 'AddMoneyController@postPaymentWithStripe',));

Step 6: Create Controller

In this point, now we should create new controller as AddMoneyController in this path app/Http/Controllers/AddMoneyController.php. this controller will manage layout and payment post request, So run bellow command for generate new controller:

php artisan make:controller AddMoneyController

Ok, now put bellow content in controller file:

app/Http/Controllers/AddMoneyController.php

<?php


namespace App\Http\Controllers;

use App\Http\Requests;

use Illuminate\Http\Request;

use Validator;

use URL;

use Session;

use Redirect;

use Input;

use App\User;

use Cartalyst\Stripe\Laravel\Facades\Stripe;

use Stripe\Error\Card;

class AddMoneyController extends HomeController

{

public function __construct()

{

parent::__construct();

$this->user = new User;

}

/**

* Show the application paywith stripe.

*

* @return \Illuminate\Http\Response

*/

public function payWithStripe()

{

return view('.paywithstripe');

}

/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function postPaymentWithStripe(Request $request)

{

$validator = Validator::make($request->all(), [

'card_no' => 'required',

'ccExpiryMonth' => 'required',

'ccExpiryYear' => 'required',

'cvvNumber' => 'required',

'amount' => 'required',

]);

$input = $request->all();

if ($validator->passes()) {

$input = array_except($input,array('_token'));

$stripe = Stripe::make('set here your stripe secret key');

try {

$token = $stripe->tokens()->create([

'card' => [

'number' => $request->get('card_no'),

'exp_month' => $request->get('ccExpiryMonth'),

'exp_year' => $request->get('ccExpiryYear'),

'cvc' => $request->get('cvvNumber'),

],

]);

if (!isset($token['id'])) {

\Session::put('error','The Stripe Token was not generated correctly');

return redirect()->route('addmoney.paywithstripe');

}

$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('addmoney.paywithstripe');

} else {

\Session::put('error','Money not add in wallet!!');

return redirect()->route('addmoney.paywithstripe');

}

} catch (Exception $e) {

\Session::put('error',$e->getMessage());

return redirect()->route('addmoney.paywithstripe');

} catch(\Cartalyst\Stripe\Exception\CardErrorException $e) {

\Session::put('error',$e->getMessage());

return redirect()->route('addmoney.paywithstripe');

} catch(\Cartalyst\Stripe\Exception\MissingParameterException $e) {

\Session::put('error',$e->getMessage());

return redirect()->route('addmoney.paywithstripe');

}

}

\Session::put('error','All fields are required!!');

return redirect()->route('addmoney.paywithstripe');

}

}

Step 7: Create View

In Last step, let's create paywithstripe.blade.php(resources/views/paywithstripe.blade.php) for layout and we will write design code here and also form for pay amount by stripe, So put following code:

resources/views/paywithstripe.blade.php

@extends('layouts.app')

@section('content')

<div class="container">

<div class="row">

<div class="col-md-8 col-md-offset-2">

<div class="panel panel-default">

@if ($message = Session::get('success'))

<div class="custom-alerts alert alert-success fade in">

<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 fade in">

<button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>

{!! $message !!}

</div>

<?php Session::forget('error');?>

@endif

<div class="panel-heading">Paywith Stripe</div>

<div class="panel-body">

<form class="form-horizontal" method="POST" id="payment-form" role="form" action="{!! URL::route('addmoney/stripe') !!}" >

{{ csrf_field() }}

<div class="form-group{{ $errors->has('card_no') ? ' has-error' : '' }}">

<label for="card_no" class="col-md-4 control-label">Card No</label>

<div class="col-md-6">

<input id="card_no" type="text" class="form-control" name="card_no" value="{{ old('card_no') }}" autofocus>

@if ($errors->has('card_no'))

<span class="help-block">

<strong>{{ $errors->first('card_no') }}</strong>

</span>

@endif

</div>

</div>

<div class="form-group{{ $errors->has('ccExpiryMonth') ? ' has-error' : '' }}">

<label for="ccExpiryMonth" class="col-md-4 control-label">Expiry Month</label>

<div class="col-md-6">

<input id="ccExpiryMonth" type="text" class="form-control" name="ccExpiryMonth" value="{{ old('ccExpiryMonth') }}" autofocus>

@if ($errors->has('ccExpiryMonth'))

<span class="help-block">

<strong>{{ $errors->first('ccExpiryMonth') }}</strong>

</span>

@endif

</div>

</div>

<div class="form-group{{ $errors->has('ccExpiryYear') ? ' has-error' : '' }}">

<label for="ccExpiryYear" class="col-md-4 control-label">Expiry Year</label>

<div class="col-md-6">

<input id="ccExpiryYear" type="text" class="form-control" name="ccExpiryYear" value="{{ old('ccExpiryYear') }}" autofocus>

@if ($errors->has('ccExpiryYear'))

<span class="help-block">

<strong>{{ $errors->first('ccExpiryYear') }}</strong>

</span>

@endif

</div>

</div>

<div class="form-group{{ $errors->has('cvvNumber') ? ' has-error' : '' }}">

<label for="cvvNumber" class="col-md-4 control-label">CVV No.</label>

<div class="col-md-6">

<input id="cvvNumber" type="text" class="form-control" name="cvvNumber" value="{{ old('cvvNumber') }}" autofocus>

@if ($errors->has('cvvNumber'))

<span class="help-block">

<strong>{{ $errors->first('cvvNumber') }}</strong>

</span>

@endif

</div>

</div>

<div class="form-group{{ $errors->has('amount') ? ' has-error' : '' }}">

<label for="amount" class="col-md-4 control-label">Amount</label>

<div class="col-md-6">

<input id="amount" type="text" class="form-control" name="amount" value="{{ old('amount') }}" autofocus>

@if ($errors->has('amount'))

<span class="help-block">

<strong>{{ $errors->first('amount') }}</strong>

</span>

@endif

</div>

</div>

<div class="form-group">

<div class="col-md-6 col-md-offset-4">

<button type="submit" class="btn btn-primary">

Paywith Stripe

</button>

</div>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

@endsection

Dommy card data for stripe testing

Card No : 4242424242424242 / 4012888888881881

Month : any future month

Year : any future Year

CVV : any 3 digit No.

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000

I hope it can help you...

Video

Shares