personal-logo
Freelancer Web Developer in Nepal with 12+ Years of Experience

Kokil Thapa is a skilled and passionate web developer specializing in full-stack development, with a focus on creating optimized, user-friendly websites and applications for businesses and individuals.

Advanced Payment Gateway Integrations in Laravel: Stripe, PayPal, Khalti, Fonepay, eSewa & More (2026 Expert Guide)

Payment gateways power modern e-commerce, SaaS platforms, subscription services, marketplaces, and digital ecosystems. In 2026, customers expect fast, seamless, secure, multi-currency, multi-gateway payment experiences. Laravel remains one of the most capable frameworks for implementing complex payment workflows—thanks to its clean architecture, middleware, queue system, event-driven capabilities, and rich ecosystem.

This guide provides an expert-level breakdown of how to integrate Stripe, PayPal, Khalti, Fonepay, and eSewa into Laravel using best practices for performance, security, error handling, and scalability.


1. Understanding Modern Payment Architecture

Before you start writing code, it’s essential to understand how payments flow and how modern gateways ensure security and reliability.

A. Payment Flow in 2026

Most gateways follow a two-step process:

  1. Create a payment request (authorization)
  2. Verify the payment via APIs or webhooks

B. Key Concepts

1. Idempotency Keys

Idempotency protects against double payments during:

  • Network drops
  • Page refresh
  • Mobile retry logic

2. Webhooks

These guarantee payment integrity, independent of user browser redirects.

3. PCI Compliance

Laravel developers must:

  • Never store raw card information
  • Use tokenized flows
  • Use PCI-certified processors

4. Audit Trails

Log every event: initiated → verified → failed → refunded.


2. Integrating Stripe in Laravel (2026 Standard)

A. Install Stripe

composer require stripe/stripe-php

B. Create Payment Intent

Stripe::setApiKey(config('services.stripe.secret')); $intent = \Stripe\PaymentIntent::create([ 'amount' => $order->amount * 100, 'currency' => 'usd', 'metadata' => ['order_id' => $order->id], ]);
$checkout = \Stripe\Checkout\Session::create([ 'payment_method_types' => ['card'], 'line_items' => [[ 'price_data' => [ 'currency' => 'usd', 'product_data' => ['name' => 'Order #' . $order->id], 'unit_amount' => $order->amount * 100, ], 'quantity' => 1, ]], 'mode' => 'payment', 'success_url' => route('payment.success'), 'cancel_url' => route('payment.cancel'), ]);

D. Stripe Webhooks

Route::post('/stripe/webhook', [WebhookController::class, 'handle']);
$event = \Stripe\Webhook::constructEvent( $payload, $sigHeader, config('services.stripe.webhook_secret') ); if ($event->type === 'payment_intent.succeeded') { $intent = $event->data->object; $order = Order::find($intent->metadata->order_id); $order->markAsPaid(); }

3. Integrating PayPal in Laravel

A. Install SDK

composer require paypal/rest-api-sdk-php

B. Create & Approve Orders

$order = $paypal->createOrder([ 'intent' => 'CAPTURE', 'purchase_units' => [[ 'amount' => ['currency_code' => 'USD', 'value' => $order->amount], ]], ]); return redirect($order->approveLink());

C. Capture Payment

$payment = $paypal->capture($token);

D. PayPal Webhooks

Listen for events like:

  • PAYMENT.CAPTURE.COMPLETED

4. Integrating Khalti in Laravel

A. Initiate Payment

$response = Http::post('https://khalti.com/api/v2/payment/initiate/', [ 'return_url' => route('khalti.callback'), 'website_url' => config('app.url'), 'amount' => $amount * 100, 'purchase_order_id' => $order->id, 'purchase_order_name' => 'Order #' . $order->id, ]);

B. Verify Payment

$response = Http::withToken(env('KHALTI_SECRET')) ->post('https://khalti.com/api/v2/payment/verify/', [ 'token' => $token, 'amount' => $amount * 100, ]);

5. Integrating Fonepay in Laravel

A. Generate Hash

$payload = "{$merchantCode},{$invoice},{$amount},{$purpose}"; $hash = hash_hmac('sha256', $payload, $secretKey);

B. Handle Callback

if ($status === 'SUCCESS') { $order->markAsPaid(); }

6. Integrating eSewa in Laravel

A. Payment Flow Overview

  1. User redirects to eSewa
  2. Payment processed
  3. Redirect back with refId
  4. Server verifies the payment

B. Payment Request

$payload = [ 'amt' => $order->amount, 'pdc' => 0, 'psc' => 0, 'txAmt' => 0, 'tAmt' => $order->amount, 'pid' => $order->invoice_id, 'scd' => env('ESEWA_MERCHANT_CODE'), 'su' => route('esewa.success'), 'fu' => route('esewa.failed'), ]; return redirect("https://esewa.com.np/epay/main?" . http_build_query($payload));

C. Verify Payment

$response = Http::asForm()->post('https://esewa.com.np/epay/transrec', [ 'amt' => $request->amount, 'scd' => env('ESEWA_MERCHANT_CODE'), 'rid' => $request->refId, 'pid' => $request->order_id, ]); if (str_contains($response->body(), 'Success')) { $order->markAsPaid(); }

7. Architecting a Multi-Gateway Payment System

A. Create Payment Interface

interface PaymentGateway { public function createPayment($order); public function verifyPayment($payload); }

B. Implement Gateways

class StripeGateway implements PaymentGateway {} class PayPalGateway implements PaymentGateway {} class KhaltiGateway implements PaymentGateway {} class FonepayGateway implements PaymentGateway {} class EsewaGateway implements PaymentGateway {}

C. Gateway Factory

class PaymentFactory { public static function make($gatewayName) { return match ($gatewayName) { 'stripe' => new StripeGateway, 'paypal' => new PayPalGateway, 'khalti' => new KhaltiGateway, 'fonepay' => new FonepayGateway, 'esewa' => new EsewaGateway, }; } }

8. Multi-Currency Payment Design

  • Store base prices in one currency
  • Convert dynamically at checkout
  • Sync FX rates using scheduled jobs

9. Fraud Prevention in 2026

  • Use reCAPTCHA v3
  • Use signed webhooks
  • Log all events
  • Enable 3D Secure 2 (Stripe default)
RateLimiter::for('checkout', fn() => Limit::perMinute(20)->by(request()->ip()) );

10. Multi-Tenant SaaS Payment Handling

  • Each tenant = separate customer in Stripe or PayPal
  • Use tenant_id in metadata
  • Centralize webhook handling

Conclusion

Building a secure, scalable, multi-gateway payment system in Laravel requires a strong architectural foundation, robust abstraction patterns, strict validation, secure verification flows, and careful attention to fraud prevention. When implemented correctly, Laravel becomes a powerhouse for e-commerce, SaaS, marketplaces, and local payment ecosystems such as those in Nepal.

For expert help building advanced Laravel payment systems, connect with a:
web developer in Nepal,
ecommerce developer in Nepal, and
legal tech developer in Nepal.

Quick Contact Options
Choose how you want to connect me: