In this article, we are going to explain “How to integrate Stripe Payment gateway in laravel”. In many websites required payment gateway like Paypal,Stripe,Authorize etc.
Stripe payment:-
Stripe is one of the most popular payment gateway which is integrated in many websites, Stripe payment is easy to integrate and use. Stripe is very simple and most powerful and flexible tools.
Step 1:- First we need to install required packages cartalyst/stripe-laravel, So open your composer.json file and put following one package in require array.
1 2 3 4 5 6 7 8 9 10 11 | "require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "5.6.*", "laravel/tinker": "^1.0", "paypal/rest-api-sdk-php": "*", "laravelcollective/html": "^5.4.0", "cartalyst/stripe-laravel": "8.0.x" }, |
Then After update composer by run following command.
1 2 3 | composer update |
It will take some time and installed required package “cartalyst/stripe-laravel”
Step 2:- Now need to configure package in config/app.php, So open app.php file and paste below code in app.php file.
1 2 3 4 5 6 7 8 9 10 11 12 | 'providers' => [ .... .... Cartalyst\Stripe\Laravel\StripeServiceProvider::class, ], 'aliases' => [ .... .... 'Stripe' => Cartalyst\Stripe\Laravel\Facades\Stripe::class, ], |
Step 3:- Now in this step need to configure STRIPE_KEY and STRIPE_SECRATE in .env file.
1 2 3 4 | STRIPE_KEY=XXXXXXXXXX STRIPE_SECRET=XXXXXXXXXX |
Step 4:- Now we are require two route one for display payment form and second is required for post request to stripe, So open your web.php file and put the following code in routes/web.php.
1 2 3 4 5 6 | // Route for stripe payment form. Route::get('stripe', array('as' => 'stripe.stripe','uses' => 'StripeController@payWithStripe')); // Route for stripe post request. Route::post('stripe', array('as' => 'stripe.stripePost','uses' => 'StripeController@postPaymentWithStripe')); |
Step 5:- Now create StripeController.php file in app/http/controllers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | <?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 StripeController extends Controller { /** * 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('sk_test_K0ThsZWtj2g1AorbBQDcV2h5'); 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('stripe.stripe'); } $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('stripe.stripe'); } else { \Session::put('error','Money not add in wallet!!'); return redirect()->route('stripe.stripe'); } } catch (Exception $e) { \Session::put('error',$e->getMessage()); return redirect()->route('stripe.stripe'); } catch(\Cartalyst\Stripe\Exception\CardErrorException $e) { \Session::put('error',$e->getMessage()); return redirect()->route('stripe.stripe'); } catch(\Cartalyst\Stripe\Exception\MissingParameterException $e) { \Session::put('error',$e->getMessage()); return redirect()->route('stripe.stripe'); } } \Session::put('error','All fields are required!!'); return redirect()->route('stripe.stripe'); } } ?> |
Step 6:- And in the last step create view file in resources/view folder paywithstripe.blade.php and paste below code in view file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | <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="{{route('stripe.stripePost') }}" > {{ 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> |
Now stripe payment all steps completed and ready to test, for testing first need to start serve, for that run below command and then test payment.
1 2 3 | php artisan serve |
And now you can open below url for testing….
1 2 3 | http://localhost:8000/stripe |
Now use dummy/test credit card for payment. if your payment is successfully done then you can change your payment mode test to live.
1 2 3 4 5 6 | Card No : 4242424242424242 / 4012888888881881 Month : any future month Year : any future Year CVV : any 3 digit No. |
We hope this post is very helpful for integrate stripe in laravel. If you need paypal or ccavenue payment gateway then you can also read paypal payment gateway integration and CCAvenue payment gateway etc.
Thanks!!
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.
Leave a Reply