How to integrate stripe payment gateway in Laravel 5.4 Application ?
In this post we will give you information about How to integrate stripe payment gateway in Laravel 5.4 Application ?. Hear we will give you detail about How to integrate stripe payment gateway in Laravel 5.4 Application ?And how to use it also give you demo for it if it is necessary.
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' => [
....
CartalystStripeLaravelStripeServiceProvider::class,
],
'aliases' => [
....
'Stripe' => CartalystStripeLaravelFacadesStripe::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 AppHttpControllers;
use AppHttpRequests;
use IlluminateHttpRequest;
use Validator;
use URL;
use Session;
use Redirect;
use Input;
use AppUser;
use CartalystStripeLaravelFacadesStripe;
use StripeErrorCard;
class AddMoneyController extends HomeController
{
public function __construct()
{
parent::__construct();
$this->user = new User;
}
/**
* Show the application paywith stripe.
*
* @return IlluminateHttpResponse
*/
public function payWithStripe()
{
return view('.paywithstripe');
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
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(CartalystStripeExceptionCardErrorException $e) {
Session::put('error',$e->getMessage());
return redirect()->route('addmoney.paywithstripe');
} catch(CartalystStripeExceptionMissingParameterException $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 >
<div >
<div >
<div >
@if ($message = Session::get('success'))
<div >
<button type="button" data-dismiss="alert" aria-hidden="true"></button>
{!! $message !!}
</div>
<?php Session::forget('success');?>
@endif
@if ($message = Session::get('error'))
<div >
<button type="button" data-dismiss="alert" aria-hidden="true"></button>
{!! $message !!}
</div>
<?php Session::forget('error');?>
@endif
<div >Paywith Stripe</div>
<div >
<form method="POST" id="payment-form" role="form" action="{!! URL::route('addmoney/stripe') !!}" >
{{ csrf_field() }}
<div >
<label for="card_no" >Card No</label>
<div >
<input id="card_no" type="text" name="card_no" value="{{ old('card_no') }}" autofocus>
@if ($errors->has('card_no'))
<span >
<strong>{{ $errors->first('card_no') }}</strong>
</span>
@endif
</div>
</div>
<div >
<label for="ccExpiryMonth" >Expiry Month</label>
<div >
<input id="ccExpiryMonth" type="text" name="ccExpiryMonth" value="{{ old('ccExpiryMonth') }}" autofocus>
@if ($errors->has('ccExpiryMonth'))
<span >
<strong>{{ $errors->first('ccExpiryMonth') }}</strong>
</span>
@endif
</div>
</div>
<div >
<label for="ccExpiryYear" >Expiry Year</label>
<div >
<input id="ccExpiryYear" type="text" name="ccExpiryYear" value="{{ old('ccExpiryYear') }}" autofocus>
@if ($errors->has('ccExpiryYear'))
<span >
<strong>{{ $errors->first('ccExpiryYear') }}</strong>
</span>
@endif
</div>
</div>
<div >
<label for="cvvNumber" >CVV No.</label>
<div >
<input id="cvvNumber" type="text" name="cvvNumber" value="{{ old('cvvNumber') }}" autofocus>
@if ($errors->has('cvvNumber'))
<span >
<strong>{{ $errors->first('cvvNumber') }}</strong>
</span>
@endif
</div>
</div>
<div >
<label for="amount" >Amount</label>
<div >
<input id="amount" type="text" name="amount" value="{{ old('amount') }}" autofocus>
@if ($errors->has('amount'))
<span >
<strong>{{ $errors->first('amount') }}</strong>
</span>
@endif
</div>
</div>
<div >
<div >
<button type="submit" >
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
Hope this code and post will helped you for implement How to integrate stripe payment gateway in Laravel 5.4 Application ?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs