On This Page
Why Redirect Success URLs Cannot Confirm Payments
During our BabyBoo e-commerce final project (built with Next.js 14 App Router and an Express.js + Prisma ORM + PostgreSQL backend), one subtle failure mode appeared early during payment sandbox testing: users closing the browser before the finish redirect or payment gateways firing handle_notification webhooks twice for the same settlement event.
If an endpoint only blindly updates order status or trusts frontend query parameters after /v2/charge Virtual Account bank transfer, orders either stay stuck in waiting_payment or re-trigger post-payment side effects on retry webhooks.
Why server-side webhooks outrank client redirect params
Preventing unpaid or duplicate order states during async Midtrans VA callbacks.
Backend Notification Guard & Atomic State Checks
We anchored payment transitions strictly inside POST /v1/api/checkouts/pay/midtrans/handle_notification using idempotent state guards and Prisma $transaction. Once an order reaches payment_verified, duplicate settlement callbacks return 200 OK immediately without re-executing fulfillment logic.
// Verified checkout calculation & idempotent state guard pattern
export type OrderPaymentStatus = "waiting_payment" | "payment_verified" | "cancelled";
export function resolveNextPaymentStatus(
current: OrderPaymentStatus,
transactionStatus: string,
): OrderPaymentStatus | null {
if (current === "payment_verified") {
return null;
}
if (transactionStatus === "settlement" || transactionStatus === "capture") {
return "payment_verified";
}
if (transactionStatus === "expire" || transactionStatus === "cancel" || transactionStatus === "deny") {
return "cancelled";
}
return current;
}Trade-Off: Supporting Manual Receipt Fallback Safely
Because bootcamp testing and buyers in smaller stores sometimes prefer manual transfer confirmation alongside automatic VA settlement, the backend also supported a Multer receipt upload path alongside /v2/charge. Keeping both paths tied to the same order status enum prevented split-brain verification states inside the admin panel.
Checklist for Payment Webhook Endpoints
- Verify Midtrans signature fields on the Express server before mutating any Prisma record.
- Wrap item deduction and payment state transitions inside a single atomic database transaction.
- Return
200 OKcleanly on already-verified orders so Midtrans stops retrying without corrupting records.