Checkout offers Cash on Delivery and Stripe's hosted card checkout. All amounts are in PKR and include the existing Rs. 200 delivery fee. COD orders start with pending payment. Stripe orders are created with paid payment status only after Stripe confirms success.
Set STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET, and NEXT_PUBLIC_APP_URL in Vercel. The app URL must be the deployed HTTPS origin. Hosted Checkout does not require a browser publishable key.
Configure the Stripe webhook endpoint as https://YOUR_DOMAIN/api/stripe/webhook and subscribe to:
payment_intent.succeededpayment_intent.canceled(one “l”)payment_intent.payment_failed
Use matching test or live keys and webhook endpoints. Keep secrets out of Git.
- Use a Stripe test secret key in
.env.localand setNEXT_PUBLIC_APP_URL=http://localhost:3000. - Install the official CLI with
npm install -g --allow-scripts=@stripe/cli @stripe/cli. - Run
stripe loginand approve the browser prompt. Recent CLI versions may also requirestripe login --complete-device. - Run
npm run stripe:listen. This runs the requested listener, uses the configured test secret key when available, and writes its own signing secret into.env.localwithout printing it. The CLI signing secret is different from the Vercel/Dashboard endpoint secret. - Start or restart the app with
npm run devon port 3000. Keep the listener running. - Sign in, add products, verify the checkout email, select Stripe, and pay with test card
4242 4242 4242 4242, a future expiry, and any CVC. Use4000 0025 0000 3155for authentication and4000 0000 0000 9995for a decline. - Verify that a successful Stripe order is paid, a COD order is pending, and declined/canceled attempts leave the cart and stock unchanged. Resending a success event must not create a second order or reduce stock again.
Direct CLI equivalent:
stripe listen --events payment_intent.succeeded,payment_intent.canceled,payment_intent.payment_failed --forward-to localhost:3000/api/stripe/webhook
Only use test cards in Stripe test mode. The return page checks payment server-side and waits for the paid order. Closing the browser does not prevent the signed webhook from completing the order. If a product becomes unavailable between opening Stripe and payment, fulfillment rolls back and the payment is refunded with an idempotent refund request.
MongoDB must support transactions, as it already does for COD checkout. Restart the app after schema changes so the Stripe Checkout and unique payment-intent indexes can initialize.
References: https://docs.stripe.com/checkout/quickstart, https://docs.stripe.com/webhooks, https://docs.stripe.com/testing