CodeEasydojo
Daily BugAll Challenges
0/5 Solved
#01Daily BugMediumJavaScript~6m

The Double-Charge Checkout

Prevent concurrent checkout requests for the same user from overdrawing their wallet balance.

#async#concurrency#payments

Incident Ticket

SEV-1
Reported ByPayments On-Call
EnvironmentNode.js 20 · Checkout API

Customers on slow mobile networks are tapping "Pay Now" twice and getting charged twice, even though we check their balance before deducting. Fix processPayment so concurrent calls for the same user cannot overdraw their balance.

Acceptance Criteria

  • Single valid payments charge the gateway once and return { success: true, remaining }.
  • Insufficient balance throws an Error without calling paymentGateway.charge().
  • Two concurrent calls of ₹80 on a ₹100 balance result in exactly 1 success, 1 rejection, and ₹20 remaining.
  • If paymentGateway.charge() rejects, user.balance is not lost and subsequent payments still work.

Progressive Hints

0/2 revealed
Hint 1: Where to look
Trace what happens when two calls enter processPayment at the same millisecond: both read user.balance before either one reaches user.balance -= amount across the await boundary.
Hint 2: The concept
JavaScript is single-threaded between await points, not across them. Either reserve the funds synchronously before await (and refund in catch), or serialize calls per user.id with an in-memory promise lock/queue (always releasing in finally).
processPayment.js
00:00
Loading editor...
Test Console

Ready to run test suite

Edit the code above and press Cmd/Ctrl + Enter or click Run Tests.

Bug Squashed

All checks passed · Saved locally

Time

1s

Attempts

1st Try

Hints

0

CodeEasy Bug Squashed 🐞
The Double-Charge Checkout
Fixed in 1s | 1st Try | 0 Hints
🟩🟩🟩
Try it: https://dojo.codeeasy.in/challenges/01-double-charge
Next Bug