Stripe Payment Integration With Laravel 5.4

Stripe Payment Integration With Laravel 5.4

In this post we will give you information about Stripe Payment Integration With Laravel 5.4. Hear we will give you detail about Stripe Payment Integration With Laravel 5.4And how to use it also give you demo for it if it is necessary.

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' => [
	....
    ....
    CartalystStripeLaravelStripeServiceProvider::class,
],
'aliases' => [
	....
    ....
    'Stripe' => CartalystStripeLaravelFacadesStripe::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', '[email protected]')->('stripform');
// Route for stripe post request.
Route::post('stripe', '[email protected]')->('paywithstripe');

Step : 4 Create Controller

Now, create StripeController.php file in this path app/Http/Controllers


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('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(CartalystStripeExceptionCardErrorException $e) {
                Session::put('error',$e->getMessage());
                return redirect()->route('stripform');
            } catch(CartalystStripeExceptionMissingParameterException $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 >
    <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

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…

Hope this code and post will helped you for implement Stripe Payment Integration With Laravel 5.4. 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

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US