Fix the result mapping for a customer charge
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:
- If
amount_centsis 0 or less, return["error", "invalid_amount"], whatever the gateway said. - If
statusis exactly"succeeded"(case matters), return["ok", charge_id]. Ifcharge_idis empty, return["error", "missing_charge_id"]instead.decline_codeis ignored here. - Every other status is a decline. Return
["declined", detail]. The detail isdecline_code, orstatuswhendecline_codeis 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
Input
charge_customer_result("succeeded", "ch_123", "", 5000)Output
["ok", "ch_123"]Example 2
Input
charge_customer_result("failed", "", "card_declined", 5000)Output
["declined", "card_declined"]Example 3
Input
charge_customer_result("succeeded", "ch_9", "", -100)Output
["error", "invalid_amount"]