Ever wondered what happens between clicking "Pay" and receiving a digital gift card code in your inbox? From the outside it looks instant and simple. Behind the scenes, it is a chain of steps where one small mistake can mean a lost sale, a double delivery, or a leaked code.
In this guide, I'll walk through the typical flow and the practical lessons developers should keep in mind when building it.
The basic flow
- Order placed: The customer picks a product and starts checkout.
- Payment confirmed: The payment gateway sends a confirmation, usually through a webhook.
- Code allocated: The store reserves a code from its stock or requests one from a supplier.
- Delivery: The code is emailed or shown in the customer's account.
- Redemption: The customer enters the code on the platform, such as Steam, PlayStation, or Roblox.
1. Confirm payment on the server, not in the browser
A common beginner mistake is delivering the code when the customer lands on the "success" page. That page can be reached, refreshed, or faked. Only release a code after your server receives a verified confirmation from the payment gateway.
2. Verify and deduplicate webhooks
Gateways can retry a webhook or send the same event more than once. If your code handles each call as a new payment, one order could deliver two codes.
Here is a simplified example (Node.js style pseudocode):
app.post("/webhook", async (req, res) => {
// 1. Verify the signature using your gateway's official library
const event = verifyWebhookSignature(req);
// 2. Skip events we have already processed
const alreadyDone = await db.processedEvents.exists(event.id);
if (alreadyDone) return res.sendStatus(200);
// 3. Allocate and deliver inside one transaction
await db.transaction(async (tx) => {
const code = await tx.codes.allocate(event.orderId);
await tx.orders.markPaid(event.orderId);
await tx.processedEvents.add(event.id);
await queueEmail(event.orderId, code);
});
res.sendStatus(200);
});
The key ideas are signature verification, a stored event ID, and a transaction so that allocation and order status never get out of sync.
3. Protect codes at rest
Gift card codes are as good as cash. Treat them that way:
- Store them encrypted.
- Never write full codes to logs or error trackers.
- Limit which parts of your system and which staff can view them.
- Mask codes in admin screens where possible.
4. Handle stock and failures gracefully
What if the supplier is slow, or stock runs out right after payment? Plan for it:
- Reserve a code at checkout and release it if payment fails or times out.
- Retry delivery safely without creating duplicates.
- Show the order status clearly in the customer's account.
- Give customers an order ID so support can find their purchase quickly.
5. Add basic fraud checks
Digital goods are a common target for fraud because delivery is instant. Simple checks go a long way:
- Watch for many orders from one IP or device in a short time.
- Flag mismatched billing and location data for review.
- Rate limit order and login endpoints.
- Review unusual high-value orders before delivery.
6. Be clear with customers
Good communication reduces support tickets and builds trust:
- Show clear delivery and refund policies before payment.
- Explain how to redeem the code on the official platform.
- Remind buyers never to share their codes.
Quick checklist
Payment verified server-side
Webhook signature checked
Duplicate events ignored
Allocation and order update in one transaction
Codes encrypted and kept out of logs
Retry and failure handling in place
Basic fraud checks and rate limits enabled
Final thoughts
Digital delivery feels effortless to the customer, but it relies on careful payment handling, secure storage, and solid error handling. Get these basics right and you will avoid most of the painful bugs.
What's the trickiest part of e-commerce development you've faced? Share it in the comments!
I work on a digital gift card store, Gamingty.
Top comments (0)