Laravel 8 Stripe Payment Gateway Integrate Example
In this tutorial, we will tell you how to integrate Stripe payment gateway in Laravel Framework(Laravel 8 stripe payment gateway integrate example).
The Stripe payment gateway is a popular payment gateway method and it is easily used for our project. so many developers prefer that payment gateway method.
Stripe is a secure payment method and it’s work-based using API. it provides two environments for the user like a sandbox(Test) and live. so you can learn laravel 8 stripe integration using the following steps.
Overview
Step 1: Install Laravel 8
Step 2: Install Package
Step 3: Get and Set Stripe API Key and SECRET
Step 4: Create Route
Step 5: Create Controller
Step 6: Create Blade File
Step 1: Install Laravel 7
We are going to install laravel 7, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel8_stripe |
Step 2: Install Package
Now, we are going to install the stripe package using the below command.
1 | composer require stripe/stripe-php |
Step 3: Get and Set Stripe API Key and SECRET
Now, we have to need the API Key and SECRET Key. so we will go to the official Stripe site and after then login get API Key and SECRET Key after then we will set in .env file.
1 2 | STRIPE_KEY=pk_test_xxxxxxxxxxxxxxxxxxx STRIPE_SECRET=sk_test_xxxxxxxxxxxxxx |
Step 4: Create Route
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('stripe', 'StripeController@stripe'); Route::post('payment', 'StripeController@payStripe'); ?> |
Step 5: Create Controller
Now, We will create the controller using the below command and paste the below code in this controller.
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 | php artisan make:controller StripeController <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Stripe; class StripeController extends Controller { public function stripe() { return view('stripe'); } public function payStripe(Request $request) { $this->validate($request, [ 'card_no' => 'required', 'expiry_month' => 'required', 'expiry_year' => 'required', 'cvv' => 'required', ]); $stripe = Stripe\Stripe::setApiKey(env('STRIPE_SECRET')); try { $response = \Stripe\Token::create(array( "card" => array( "number" => $request->input('card_no'), "exp_month" => $request->input('expiry_month'), "exp_year" => $request->input('expiry_year'), "cvc" => $request->input('cvv') ))); if (!isset($response['id'])) { return redirect()->route('addmoney.paymentstripe'); } $charge = \Stripe\Charge::create([ 'card' => $response['id'], 'currency' => 'USD', 'amount' => 100 * 100, 'description' => 'wallet', ]); if($charge['status'] == 'succeeded') { return redirect('stripe')->with('success', 'Payment Success!'); } else { return redirect('stripe')->with('error', 'something went to wrong.'); } } catch (Exception $e) { return $e->getMessage(); } } } ?> |
Step 6: Create Blade File
So finally, we will create the stripe.tpl file in the “resources/views/” directory.
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 | <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-12"> <div class="text-center"> <h2>Pay for Event</h2> <br> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif @if(session()->has('error')) <div class="alert alert-danger"> {{ session()->get('error') }} </div> @endif @if(session()->has('success')) <div class="alert alert-success"> {{ session()->get('success') }} </div> @endif <div class="row"> <div class="col-lg-3"> </div> <div class="col-lg-6"> <form action="{{url('payment')}}" data-cc-on-file="false" data-stripe-publishable-key="pk_test_iS5sLGz5CONWxJ8KHhBzHHvD" name="frmStripe" id="frmstripe" method="post"> {{ csrf_field() }} <div class="row"> <div class="col-lg-12 form-group"> <label>Name on Card</label> <input class="form-control" size="4" type="text"> </div> </div> <div class="row"> <div class="col-lg-12 form-group"> <label>Card Number</label> <input autocomplete="off" class="form-control" size="20" type="text" name="card_no"> </div> </div> <div class="row"> <div class="col-lg-4 form-group"> <label>CVC</label> <input autocomplete="off" class="form-control" placeholder="ex. 311" size="3" type="text" name="cvv"> </div> <div class="col-lg-4 form-group"> <label>Expiration</label> <input class="form-control" placeholder="MM" size="2" type="text" name="expiry_month"> </div> <div class="col-lg-4 form-group"> <label> </label> <input class="form-control" placeholder="YYYY" size="4" type="text" name="expiry_year"> </div> </div> <div class="row"> <div class="col-lg-12"> <div class="form-control total btn btn-primary"> Total: <span class="amount">$20</span> </div> </div> </div> <div class="row"> <div class="col-lg-12 form-group"> <button class="form-control btn btn-success submit-button" type="submit" style="margin-top: 10px;">Pay »</button> </div> </div> </form> </div> </div> </body> </html> |
Read Also
Laravel 8 S3 File Upload Tutorial With Example
Laravel 8 Newsletter Tutorial With Example
Laravel 8 Multiple Authentication Example Tutorial