By Hardik Savani March 31, 2022 Category : LaravelPauseUnmuteLoaded: 5.32%FullscreenHi,
I am going to explain to you an example of laravel 9 stripe integration. This tutorial will give you a simple example of stripe payment gateway integration in laravel 9. I would like to share with you laravel 9 stripe payment gateway example. it's a simple example of laravel 9 payment integration stripe example.
You need to create a stripe developer account and need to get API key and secret from there. Then we will use stripe/stripe-php composer library for the stripe payment gateway in laravel 9. I write step by step integration for the stripe payment gateway.
Stripe is a very popular and secure internet payment gateway company that helps to accept payments worldwide. Stripe provides really nice development interface to start and you don’t have to pay subscription charges to learn it provides a free developer account, before starting to code in your app.
I will give you an example from scratch to implement a stripe payment gateway in the laravel 9 application. You just need to follow a few steps to get a full example to pay.
Step 1: Install Laravel 9
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
in next step, now we have create new controller as StripePaymentController and write both method on it like as bellow, So let's create both controller:
app/Http/Controllers/StripePaymentController.php
<?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" => 100 * 100, "currency" => "usd", "source" => $request->stripeToken, "description" => "Test payment from itsolutionstuff.com." ]); Session::flash('success', 'Payment successful!'); return back(); }}
If you must need to pass customer name and address with shipping address then you can use bellow method code:
In this step, we will create two routes for get request and another for post request. So, let's add new route on that file.
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\StripePaymentController; /*|--------------------------------------------------------------------------| 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::controller(StripePaymentController::class)->group(function(){ Route::get('stripe', 'stripe'); Route::post('stripe', 'stripePost')->name('stripe.post');});
Step 6: Create Blade File
In Last step, let's create stripe.blade.php(resources/views/stripe.blade.php) for layout and write code of jquery to get token from stripe here and put following code: