In this tutorial you will learn about the Laravel 7/6 Stripe Payment Gateway Integration Example and its application with practical example.
In this Laravel 7/6 stripe payment gateway integration tutorial, I’ll show you how to integrate stripe payment gateway in laravel 7. In this tutorial you will learn to integrate stripe in laravel 7. In this step by step tutorial I’ll share laravel 7 stripe integration example.
Stripe Payment Gateway
Stripe is one of the most popular payment gateway, that allows us to accept payment worldwide. Stripe is very simple, hassle free and easy to integrate payment gateway. Integrating stripe payment gateway with laravel is a breeze. After integrating stripe in laravel you will be able to collect payment via simple payment form which allow customer to provide card information like card number, expiry date and a CVC code.
Laravel 7/6 Stripe Payment Gateway Integration Example
In this tutorial, you will learn to integrate stripe payment gateway in your laravel 7 project. Follow this step by step tutorial to learn stripe payment gateway integration in laravel 7.
Laravel 7/6 Stripe Payment Gateway Integration Example
- Step 1: Install Laravel Fresh Setup
- Step 2: Install stripe Package
- Step 3: Set Secret Credential
- Step 4: Make Route
- Step 5: Create Controller
- Step 6: Create Blade View file
- Step 7: Run Development Server
Step 1: Install Laravel Fresh Project
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Install Stripe package
Now, we need to install stripe-laravel package in our project. Open your terminal and switch to project directory and use the following composer command to install stripe-laravel package.
1 |
composer require cartalyst/stripe-laravel |
After that, we need to register the provider and aliases. Go to the app/config/app.php and put the below lines here :
1 2 3 4 5 6 7 8 9 |
‘providers’ => [ .......... Cartalyst\Stripe\Laravel\StripeServiceProvider::class ], ‘aliases’ => [ .......... 'Stripe' => Cartalyst\Stripe\Laravel\Facades\Stripe::class ], |
Step 3: Set Secret Credential
Now, we need to set stripe key and secret key in our project’s .env file. In stripe dashboard switch to the test mode and get the stripe key and secret. Now, open .env file located in project’s root folder and set the stripe key and secret as following –
.env file
1 2 |
STRIPE_KEY=pk_test_xxxxxxxxxxxxxxxxxxx STRIPE_SECRET=sk_test_xxxxxxxxxxxxxx |
You will also need to set up the Stripe API key, Let’s open or create the config/services.php
file, and add or update the 'stripe'
array as following:
1 2 3 |
'stripe' => [ 'secret' => env('STRIPE_SECRET'), ], |
Step 4: Make Route
After this, we need to add following two routes in “routes/web.php” to display barcode. Lets open “routes/web.php” file and add following route.
routes/web.php
1 2 |
Route::get('stripe', 'StripeController@index'); Route::post('store', 'StripeController@store'); |
Step 5: Create Controller
Now, lets create a payment controller using following command
1 |
php artisan make:controller StripeController |
Go to app/Http/Controller/StripeController and put the below code :
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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Redirect,Response,Stripe; class StripeController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('stripe'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $stripe = Stripe::charges()->create([ 'source' => $request->get('tokenId'), 'currency' => 'USD', 'amount' => $request->get('amount') * 100 ]); return $stripe; } } |
Step 6: Create Blade View file
In this step, we will create view/blade file to accept stripe payment. Lets create a “stripe.blade.php” file in “resources/views/” directory and put the following code in it.
resources/views/stripe.blade.php
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 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>Laravel Stripe Payment Gateway Integration</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <style> .container{ padding: 0.5%; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"><pre id="token_response"></pre></div> </div> <div class="row"> <div class="col-md-4"> <button class="btn btn-primary btn-block" onclick="pay(10)">Pay $10</button> </div> <div class="col-md-4"> <button class="btn btn-success btn-block" onclick="pay(50)">Pay $50</button> </div> <div class="col-md-4"> <button class="btn btn-info btn-block" onclick="pay(100)">Pay $100</button> </div> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <script src="https://checkout.stripe.com/checkout.js"></script> <script type="text/javascript"> $(document).ready(function () { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); }); function pay(amount) { var handler = StripeCheckout.configure({ key: 'pk_test_5f6jfFP2ZV5U9TXQYG0vtqFJ00eFVWNoRX', // your publisher key id locale: 'auto', token: function (token) { // You can access the token ID with `token.id`. // Get the token ID to your server-side code for use. console.log('Token Created!!'); console.log(token) $('#token_response').html(JSON.stringify(token)); $.ajax({ url: '{{ url("store") }}', method: 'post', data: { tokenId: token.id, amount: amount }, success: (response) => { console.log(response) }, error: (error) => { console.log(error); alert('Oops! Something went wrong') } }) } }); handler.open({ name: 'Demo Site', description: '2 widgets', amount: amount * 100 }); } </script> </body> </html> |
Step 7: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 3 4 5 |
http://localhost:8000/stripe Or direct hit in your browser http://localhost/blog/public/stripe |
Testing Card Credential
1 2 3 4 |
Card No : 4242424242424242 Month : any future month Year : any future Year CVV : 123 |