In this tutorial we are share with you how to integrate stripe payment gateway in laravel 5.4. in meny website required payment integration for online payment like paypal, strip, authorize.net, rozarpay etc...
Strip is one of the best payment gateway which integrate in many website. stripe payment gatway is easy to use in any website. in this tutorials we are share with you how to card payment integration by stripe.
First we are neee one stripe account for testing. so, first go this link and make you one accound and get your secret_id and key. because it is very important for integration stripe payment gateway
If you don't now how to create account in stripe and get your secret_key please watch it youtube video How to create stripe account
We are here done stripe card payment using this laravel package cartalyst/stripe-laravel
Step : 1 Install Required Packages
First we need to install required package cartalyst/stripe-laravel. so open your composer.json file and put following one package in require array
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*",
"laravelcollective/html": "5.3.*",
"cartalyst/stripe-laravel": "2.0.*"
},
Then after update your composer by run followign command :
composer update
Step : 2 Configuration Packages
Now, we need to configure package in app.php file. so open your confige/app.php file and put following code into it.
'providers' => [
....
....
Cartalyst\Stripe\Laravel\StripeServiceProvider::class,
],
'aliases' => [
....
....
'Stripe' => Cartalyst\Stripe\Laravel\Facades\Stripe::class,
],
Step : 3 Configuration .env
Now, open your .env file and put your strip account's STRIPE_KEY and STRIPE_SECRET like that.
STRIPE_KEY=XXXXXXXXXX
STRIPE_SECRET=XXXXXXXXXX
Step : 3 Create route for stripe payment
Now, we are required two route one for dispan payment form and another is required for make post request to stripe. so, open your routes/web.php file and put following two routes.
// Route for stripe payment form.
Route::get('stripe', 'StripeController@payWithStripe')->('stripform');
// Route for stripe post request.
Route::post('stripe', 'StripeController@postPaymentWithStripe')->('paywithstripe');
Step : 4 Create Controller
Now, create StripeController.php file in this path app/Http/Controllers
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('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');
}
}
\Session::put('error','All fields are required!!');
return redirect()->route('stripform');
}
}
Step : 5 Create View file
[ADDCODE]
And into the last step create your paywithstripe.blade.php file in this folder resources/views and put following blade file code.
@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
Use following dummy data for stripe payment testing in test mode. if your payment is make successfully then you can change your payment mode test to live and no any issue created in future.
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/stripe
If you face any problem then please write a comment or give some suggestions for improvement. Thanks...