Surfe.be - passive income

How to integrate stripe payment gateway in laravel

Stripe Payment Gateway

Today i have integrated Stripe Payment Gateway in my project. We all know that it is must needed thing in our e-commerce website. Today i am going to show you how to integrate Stripe Payment Gateway with laravel .

If you are operative in the e-commerce circuit, then you must have known the importance of online transaction. As it is the most viable and fastest way to settle payments anywhere in the world. And because of evolving internet technology, today anyone can easily buy or sell products and can settle payments with online transaction all around the world.

But online transaction brings high risks of serious security threats also. Therefore robust security mechanism for online payments is required to avoid potential malicious attempts. That is where the importance of Laravel Stripepayment gateways comes into play. As these gateways helps businesses to focus and capitalize on the benefits of online transactions, while keeping their customers and systems secured from the external threats.

Stipe sign up

Visit Developer.stripe.com to create your business account, so that you can buy/sell products easily over the internet and transfer funds to your account. Next create a stripe developer mode and get important credentials like client_key and secret_key, to test stripe integration with your laravel app.

Set up Stripe

We need following package for integrate Stripe payment in our laravel application. add following package in your composer.json fil

  1. 1.Install Required Packages
  2. 2.Configuration of config/app.php file
  3. 3.Create Required Route
  4. 4.Create Required Controller
  5. 5.Finally Create View File

stripe payment gateway

 

Install Required Packages

We need following package for integrate Stripe payment in our laravel application. add following package in your composer.json file.

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

after completing it run following command  

php artisan vendore:publlish

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

stripe
STRIPE_KEY=XXXXXXXXXX
STRIPE_SECRET=XXXXXXXXXX

Configuration of config/app.php file

Go to your config/app.php file and add following code

stripe
'providers' => [
    Cartalyst\Stripe\Laravel\StripeServiceProvider::class,
],
'aliases' => [
    'Stripe' => Cartalyst\Stripe\Laravel\Facades\Stripe::class,
],

Create Required Route

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

Create Controller

Run the following command to create required controller

controller
php artisan make:controller StripeController

Now copy this following code and paste it into your StripeController.php file.

<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use Cartalyst\Stripe\Laravel\Facades\Stripe;
use Stripe\Error\Card;
use Validator;
use URL; 
use Session; 
use Redirect; 
use Input; 
use App\User;

class StripeController extends HomeController
{
    
    public function __construct()
    {
        parent::__construct();
        $this->user = new User;
    }
    
    public function payWithStripe()
    {
        return view('.paywithstripe');
    }
    
    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('put 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','try again . stripe key error!!!');
                    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 added 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','please fill out those field first!!');
        return redirect()->route('addmoney.paywithstripe');
    }    
}

Now we are in final step . Just we have to create our view file. So go to this following directory and create a blade file whose name will be 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

That’s it. Hope this tutorial will help you. If you like this tutorial please leave a comment and don’t forget to share . 

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST