The first gateway is easy. The second one is where a payment system either gets a clean structure or turns into a tangle of if ($method === 'stripe') branches. This is the structure I use when one Laravel app has to take cards through Stripe, wallets through local providers, and bank transfers, all feeding the same orders.
Start with your own payments table
Do not let any gateway's data model become yours. Keep one payments table that every provider writes into, and treat the provider as an implementation detail.
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id')->constrained();
$table->string('gateway'); // stripe, paypal, jazzcash...
$table->string('reference')->nullable()->index(); // the provider's id
$table->unsignedBigInteger('amount'); // minor units, never floats
$table->char('currency', 3);
$table->string('status')->default('pending'); // pending, paid, failed, refunded
$table->json('payload')->nullable(); // raw provider data for support
$table->timestamps();
});
Store money as integers in the smallest unit. Floating point and currency do not mix.
One interface, many gateways
Define what your application needs from any provider, then write one small class per gateway.
interface PaymentGateway
{
/** Start a payment; return a URL to redirect to or a client secret. */
public function charge(Payment $payment): PaymentIntentResult;
/** Verify and translate an incoming webhook into a status change. */
public function handleWebhook(Request $request): ?WebhookResult;
}
Resolve the right one from config, so adding a gateway is a new class plus one line, not an edit to the checkout code.
// config/payments.php
'gateways' => [
'stripe' => App\Payments\StripeGateway::class,
'paypal' => App\Payments\PayPalGateway::class,
'jazzcash' => App\Payments\JazzCashGateway::class,
],
// app/Payments/GatewayManager.php
public function for(string $name): PaymentGateway
{
$class = config("payments.gateways.$name")
?? throw new InvalidArgumentException("Unknown gateway [$name].");
return app($class);
}
Cards: Stripe PaymentIntents
With Stripe, create the PaymentIntent on the server and hand only the client secret to the browser. The amount is decided by your server, never by the client.
public function charge(Payment $payment): PaymentIntentResult
{
$intent = $this->stripe->paymentIntents->create([
'amount' => $payment->amount,
'currency' => $payment->currency,
'metadata' => ['payment_id' => $payment->id],
], ['idempotency_key' => "payment-{$payment->id}"]);
$payment->update(['reference' => $intent->id]);
return PaymentIntentResult::clientSecret($intent->client_secret);
}
Wallets and local gateways: redirects and callbacks
Providers like JazzCash, Easypaisa and PayFast usually work differently: you build a signed request, redirect the customer to the provider's page, and receive the result back on a callback URL. The signature (a hash over the sorted fields plus a secret) is the part to get exactly right. Build it in one method, unit test it against the provider's sample values, and verify the same signature on the way back before you believe anything in the response.
Trust webhooks, not redirects
The customer's browser returning to your "thank you" page proves nothing: they can close the tab, or forge the URL. The webhook is the source of truth. Verify its signature, then update the payment.
public function handleWebhook(Request $request): ?WebhookResult
{
try {
$event = \Stripe\Webhook::constructEvent(
$request->getContent(),
$request->header('Stripe-Signature'),
config('services.stripe.webhook_secret'),
);
} catch (\Throwable $e) {
abort(400, 'Invalid signature');
}
return match ($event->type) {
'payment_intent.succeeded' => WebhookResult::paid($event->data->object->id),
'payment_intent.payment_failed' => WebhookResult::failed($event->data->object->id),
default => null,
};
}
Remember that webhook routes must be excluded from CSRF protection, and that they must read the raw request body, since a signature is computed over the exact bytes.
Make everything idempotent
Gateways retry webhooks, customers double-click, and queues re-run jobs. The same event arriving twice must not mark an order paid twice or ship it twice. Guard the state change, and record processed event ids.
DB::transaction(function () use ($result) {
$payment = Payment::where('reference', $result->reference)->lockForUpdate()->firstOrFail();
if ($payment->status === 'paid') {
return; // already handled
}
$payment->update(['status' => 'paid']);
OrderPaid::dispatch($payment->order)->afterCommit();
});
Test the unhappy paths
Fake the gateway in tests by binding a stub of your interface, and cover the cases that cost real money: a webhook with a bad signature, the same webhook twice, a payment that fails after the redirect, and an amount that does not match the order. Use each provider's sandbox for an end-to-end pass before going live, and keep secrets in environment variables, never in the repository.