As we all know in most web application either in Laravel 8 or any of the PHP Framework, we sometimes need to integrate payment gateway. Inside this article we will learn Stripe payment gateway integration in Laravel 8.
How to Integrate Stripe payment gateway in Laravel 8, we will see each steps in detailed concept.
Let’s get started.
Laravel Installation
We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.
Here is the command to create a laravel project-
composer create-project --prefer-dist laravel/laravel blog
To start the development server of Laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Stripe PHP Package Installation
We will install stripe package via composer into laravel 8 setup. Open laravel 8 project in terminal and hit this composer command.
$ composer require stripe/stripe-php
Set Stripe API Key and SECRET
Open up .env file of laravel 8 application. Into the file, set stripe api key and secret.
STRIPE_KEY=pk_test_MaQhP2b8XqG9R7PTL6VUmipt STRIPE_SECRET=sk_test_MtVxg0tJfCGJXQY8xZlu69Ie
This is our stripe sandbox detail, you can replace it with your own. If you are wondering to find stripe test account keys. Follow these steps –
- Login to https://dashboard.stripe.com/
- You need to enable Viewing test data to use Test account details
- Go to Developer >> Click on API keys
Create Routes
Next, we need to set Laravel 8 stripe payment gateway application routes. To configure routes, open up /routes/web.php file.
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\StripePaymentController; Route::get('stripe', [StripePaymentController::class, 'stripe']); Route::post('stripe', [StripePaymentController::class, 'stripePost'])->name('stripe.post');
Create Controller File
Create application controller in setup. To create controller file, back to terminal and hit this command to create file.
$ php artisan make:controller StripePaymentController
This command will creates a file inside /app/Http/Controllers/StripePaymentController.php. Open file into any of your editor and paste the given code.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Session; use Stripe; class StripePaymentController extends Controller { /** * success response method. * * @return \Illuminate\Http\Response */ public function stripe() { return view('stripe'); } /** * success response method. * * @return \Illuminate\Http\Response */ public function stripePost(Request $request) { Stripe\Stripe::setApiKey(env('STRIPE_SECRET')); Stripe\Charge::create ([ "amount" => 70 * 100, "currency" => "usd", "source" => $request->stripeToken, "description" => "Test payment via Stripe From onlinewebtutorblog.com" ]); Session::flash('success', 'Payment done successfully!'); return back(); } }
Create Blade File
Let’s create blade template file for setting the layout of payment details for user. Create a file stripe.blade.php inside /resources/views/stripe.blade.php. Open file and paste the given code
<!DOCTYPE html> <html> <head> <title>Laravel 8 - Stripe Payment Gateway Integration - onlinewebtutorblog.com</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <style type="text/css"> .panel-title { display: inline; font-weight: bold; } .display-table { display: table; } .display-tr { display: table-row; } .display-td { display: table-cell; vertical-align: middle; width: 61%; } </style> </head> <body> <div class="container"> <h3 style="text-align: center;">Laravel 8 - Stripe Payment Gateway Integration - onlinewebtutorblog.com</h3><br/> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="panel panel-default credit-card-box"> <div class="panel-body"> @if (Session::has('success')) <div class="alert alert-success text-center"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <p>{{ Session::get('success') }}</p> </div> @endif <form role="form" action="{{ route('stripe.post') }}" method="post" class="require-validation" data-cc-on-file="false" data-stripe-publishable-key="{{ env('STRIPE_KEY') }}" id="payment-form"> @csrf <div class='form-row row'> <div class='col-xs-12 form-group required'> <label class='control-label'>Name on Card</label> <input class='form-control' size='4' type='text'> </div> </div> <div class='form-row row'> <div class='col-xs-12 form-group card required'> <label class='control-label'>Card Number</label> <input autocomplete='off' class='form-control card-number' size='20' type='text'> </div> </div> <div class='form-row row'> <div class='col-xs-12 col-md-4 form-group cvc required'> <label class='control-label'>CVC</label> <input autocomplete='off' class='form-control card-cvc' placeholder='ex. 311' size='4' type='text'> </div> <div class='col-xs-12 col-md-4 form-group expiration required'> <label class='control-label'>Expiration Month</label> <input class='form-control card-expiry-month' placeholder='MM' size='2' type='text'> </div> <div class='col-xs-12 col-md-4 form-group expiration required'> <label class='control-label'>Expiration Year</label> <input class='form-control card-expiry-year' placeholder='YYYY' size='4' type='text'> </div> </div> <div class='form-row row'> <div class='col-md-12 error form-group hide'> <div class='alert-danger alert'>Please correct the errors and try again.</div> </div> </div> <div class="row"> <div class="col-xs-12"> <button class="btn btn-primary btn-lg btn-block" type="submit">Pay Now ($70)</button> </div> </div> </form> </div> </div> </div> </div> </div> </body> <script type="text/javascript" src="https://js.stripe.com/v2/"></script> <script type="text/javascript"> $(function() { var $form = $(".require-validation"); $('form.require-validation').bind('submit', function(e) { var $form = $(".require-validation"), inputSelector = ['input[type=text]'].join(', '), $inputs = $form.find('.required').find(inputSelector), $errorMessage = $form.find('div.error'), valid = true; $errorMessage.addClass('hide'); $('.has-error').removeClass('has-error'); $inputs.each(function(i, el) { var $input = $(el); if ($input.val() === '') { $input.parent().addClass('has-error'); $errorMessage.removeClass('hide'); e.preventDefault(); } }); if (!$form.data('cc-on-file')) { e.preventDefault(); Stripe.setPublishableKey($form.data('stripe-publishable-key')); Stripe.createToken({ number: $('.card-number').val(), cvc: $('.card-cvc').val(), exp_month: $('.card-expiry-month').val(), exp_year: $('.card-expiry-year').val() }, stripeResponseHandler); } }); function stripeResponseHandler(status, response) { if (response.error) { $('.error') .removeClass('hide') .find('.alert') .text(response.error.message); } else { /* token contains id, last4, and card type */ var token = response['id']; $form.find('input[type=text]').empty(); $form.append("<input type='hidden' name='stripeToken' value='" + token + "'/>"); $form.get(0).submit(); } } }); </script> </html>
Application Testing
Run this command into project terminal to start development server,
php artisan serve
URL – http://localhost:8000/stripe
Name: Sample Number: 4242 4242 4242 4242 CSV: 123 Expiration Month: 04 Expiration Year: 2024
We hope this article helped you to learn about stripe Payment Gateway Integration in Laravel 8 in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.
Hi there, I enjoy reading through your article. I like
to write a little comment to support you.