Designing Idempotent Laravel Payment Tables Before Integrating
In production Laravel applications, payment fulfillment bugs rarely originate in the HTTP call to create an order—they come from race conditions when a customer closes or reloads the return page just as `UPIGateway-Webhook-Dispatcher/2.0` posts your callback. Enforcing strict uniqueness invariants in your database migration eliminates duplicate credits cleanly.
<?php
// database/migrations/create_upi_orders_table.php
Schema::create('upi_orders', function (Blueprint $table) {
$table->id();
$table->string('order_id')->unique();
$table->foreignId('user_id')->constrained();
$table->decimal('amount', 12, 2);
$table->enum('status', ['PENDING', 'SUCCESS', 'FAILED'])->default('PENDING');
$table->string('utr', 32)->nullable()->unique();
$table->timestamps();
});
Step 1: Order Creation Controller Using Laravel `Http::post`
<?php
public function initiateCheckout(Request $request)
{
$planPrice = 499.00; // Derived strictly from server plan config
$orderId = 'LRV_' . time() . '_' . $request->user()->id;
DB::table('upi_orders')->insert([
'order_id' => $orderId,
'user_id' => $request->user()->id,
'amount' => $planPrice,
'status' => 'PENDING',
'created_at' => now(),
'updated_at' => now(),
]);
$res = Http::timeout(15)->post('https://upigateway.dev/api/create-order', [
'user_token' => config('services.upigateway.token'),
'amount' => (string) $planPrice,
'order_id' => $orderId,
'customer_mobile' => (string) ($request->user()->phone ?? '9999999999'),
'redirect_url' => route('checkout.return', ['order_id' => $orderId]),
'remark1' => 'Laravel Subscription',
])->json();
if (!empty($res['status']) && !empty($res['result']['payment_url'])) {
return redirect()->away($res['result']['payment_url']);
}
return back()->withErrors(['payment' => $res['message'] ?? 'Unable to create order']);
}
Step 2: Locked Webhook Verification & Reconciliation Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
class UpiWebhookController extends Controller
{
public function handle(Request $request)
{
$orderId = (string) $request->input('order_id', '');
if ($orderId === '' || $request->input('status') !== 'SUCCESS') {
return response()->json(['ignored' => true], 200);
}
$verify = Http::timeout(10)->post('https://upigateway.dev/api/check-order-status', [
'user_token' => config('services.upigateway.token'),
'order_id' => $orderId,
])->json();
if (
!empty($verify['status']) &&
($verify['result']['status'] ?? '') === 'SUCCESS' &&
($verify['result']['txnStatus'] ?? '') === 'COMPLETED' &&
!empty($verify['result']['utr'])
) {
$paid = (float) ($verify['result']['amount'] ?? 0);
$utr = (string) $verify['result']['utr'];
DB::transaction(function () use ($orderId, $paid, $utr) {
$order = DB::table('upi_orders')->where('order_id', $orderId)->lockForUpdate()->first();
if ($order && $order->status === 'PENDING' && $paid >= (float) $order->amount) {
DB::table('upi_orders')
->where('id', $order->id)
->update(['status' => 'SUCCESS', 'utr' => $utr, 'updated_at' => now()]);
}
});
}
return response()->json(['received' => true], 200);
}
}
Why `lockForUpdate()` Paired with `utr->unique()` Plus CSRF Exclusion Matters
Notice how `lockForUpdate()` blocks parallel threads on the same `order_id` from reading `PENDING` simultaneously, while `$table->string('utr')->unique()` stops duplicate 12-digit UTRs across orders. Remember to exempt `/webhooks/upigateway` from CSRF token middleware inside `bootstrap/app.php` so server POST dispatches receive `200 OK` without a `419 Page Expired` error.