Fix the result mapping for a customer charge

Review the AI's codeEasy10 min

Your task

An assistant documented chargeCustomer as throwing on a declined card and rejecting amounts that are not positive. The code did neither. A decline came back as a plain object, and callers treated it as a success. Your team picked one contract and made every documented branch real.

An assistant wrote charge_customer_result(status, charge_id, decline_code, amount_cents), which maps the gateway's answer to that contract. It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • status: the gateway's status for the charge, like "succeeded" or "failed". It can be any string, including "".
  • charge_id: the gateway's charge id, or "".
  • decline_code: the gateway's reason for a decline, like "card_declined", or "".
  • amount_cents: the amount charged, in cents.
  • Return a list of two strings: the outcome ("ok", "declined" or "error"), then a detail.

Rules the tests check, in this order:

  1. If amount_cents is 0 or less, return ["error", "invalid_amount"], whatever the gateway said.
  2. If status is exactly "succeeded" (case matters), return ["ok", charge_id]. If charge_id is empty, return ["error", "missing_charge_id"] instead. decline_code is ignored here.
  3. Every other status is a decline. Return ["declined", detail]. The detail is decline_code, or status when decline_code is empty, or "unknown" when both are empty.

Examples

charge_customer_result(status: str, charge_id: str, decline_code: str, amount_cents: int) -> list[str]
Example 1
Inputcharge_customer_result("succeeded", "ch_123", "", 5000)
Output["ok", "ch_123"]
Example 2
Inputcharge_customer_result("failed", "", "card_declined", 5000)
Output["declined", "card_declined"]
Example 3
Inputcharge_customer_result("succeeded", "ch_9", "", -100)
Output["error", "invalid_amount"]