On your Laravel project, you can easily add a stripe payment gateway to get payment from customers.
Step 1: First of all, you need to install the package.
composer requires stripe/stripe-php
Step 2: Create a Stripe account to get Client Secret and Client Key
Visit This Site: https://dashboard.stripe.com/dashboard
Step 3: add this on .env
File
STRIPE_KEY=pk_test_reFxwbsm9cdCKASdTfxAR
STRIPE_SECRET=sk_test_oQMFWteJiPd4wj4AtgApY
Step 4: Add those methods on your controller
// Checkout View Page
public function stripe(): View
{
return view('stripe');
}
// Checkout Post button Page
public function stripeCheckout(Request $request)
{
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$redirectUrl = route('stripe.checkout.success').'?session_id={CHECKOUT_SESSION_ID}';
$response = $stripe->checkout->sessions->create([
'success_url' => $redirectUrl,
'customer_email' => 'demo@gmail.com',
'payment_method_types' => ['link', 'card'],
'line_items' => [
[
'price_data' => [
'product_data' => [
'name' => $request->product,
],
'unit_amount' => 100 * $request->price,
'currency' => 'USD',
],
'quantity' => 1
],
],
'mode' => 'payment',
'allow_promotion_codes' => true
]);
return redirect($response['url']);
}
// Success Return Page
public function stripeCheckoutSuccess(Request $request)
{
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$session = $stripe->checkout->sessions->retrieve($request->session_id);
info($session);
return redirect()->route('stripe.index')
->with('success', 'Payment successful.');
}
Step 5: Create Routes:
Route::controller(StripePaymentController::class)->group(function(){
Route::get('stripe', 'stripe')->name('stripe.index');
Route::get('stripe/checkout', 'stripeCheckout')->name('stripe.checkout');
Route::get('stripe/checkout/success', 'stripeCheckoutSuccess')->name('stripe.checkout.success');
});
Step 6: Call this route on checkout button/purchase button
<a href="{{ route('stripe.checkout', ['price' => 1000, 'product' => 'Platinum']) }}" class="btn btn-primary">Checkout</a>
Now run your project and use https:://
requests.
Test with this:
Name: Test
Number: 4242 4242 4242 4242
CSV: 123
Expiration Month: 12
Expiration Year: 2028
Top comments (1)
This is how a structure the it, in order to be able to select between different payment processors: How to Add and Implement Payment Processing Interfaces in Laravel 11: The Part 1 with Hardcoded Binding