File size: 3,122 Bytes
3c88293 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | """Safe, deterministic payment tools for the local checkout simulation.
These identifiers are scenarios, not payment credentials. The public checkout
must never accept a PAN, expiry date, CVV, bank routing number, or similar
secret. Keeping the outcome table server-owned makes decline and retry
behaviour reproducible without pretending that a real processor is involved.
"""
from __future__ import annotations
from dataclasses import dataclass
PAYMENT_APPROVED = "APPROVED"
PAYMENT_DECLINED = "DECLINED"
LEGACY_TEST_CARD = "test-card"
SANDBOX_CARD_APPROVED = "sandbox-card-approved"
SANDBOX_CARD_DECLINED = "sandbox-card-declined"
SANDBOX_BANK_APPROVED = "sandbox-bank-approved"
DEFAULT_PAYMENT_METHOD = SANDBOX_CARD_APPROVED
@dataclass(frozen=True, slots=True)
class SandboxPaymentMethod:
identifier: str
label: str
description: str
outcome: str
decline_code: str | None = None
PAYMENT_METHODS: tuple[SandboxPaymentMethod, ...] = (
SandboxPaymentMethod(
identifier=SANDBOX_CARD_APPROVED,
label="Sandbox card · ending in 4242",
description="Simulated card authorization succeeds",
outcome=PAYMENT_APPROVED,
),
SandboxPaymentMethod(
identifier=SANDBOX_CARD_DECLINED,
label="Sandbox card · ending in 0002",
description="Simulated issuer decline for testing retry",
outcome=PAYMENT_DECLINED,
decline_code="sandbox-card-declined",
),
SandboxPaymentMethod(
identifier=SANDBOX_BANK_APPROVED,
label="Sandbox bank account",
description="Simulated bank authorization succeeds",
outcome=PAYMENT_APPROVED,
),
)
_BY_IDENTIFIER = {method.identifier: method for method in PAYMENT_METHODS}
def payment_method(identifier: str) -> SandboxPaymentMethod:
"""Return a supported public sandbox method or reject the identifier."""
if not isinstance(identifier, str):
raise ValueError("unsupported sandbox payment method")
try:
return _BY_IDENTIFIER[identifier]
except KeyError as exc:
raise ValueError("unsupported sandbox payment method") from exc
def payment_method_label(identifier: str) -> str:
"""Return a safe display label, including legacy persisted test attempts."""
if identifier == LEGACY_TEST_CARD:
return "Legacy sandbox test card"
return payment_method(identifier).label
def public_payment_methods() -> tuple[dict[str, str], ...]:
"""Return the non-secret method metadata needed by the checkout renderer."""
return tuple(
{
"identifier": method.identifier,
"label": method.label,
"description": method.description,
"outcome": method.outcome,
}
for method in PAYMENT_METHODS
)
__all__ = [
"DEFAULT_PAYMENT_METHOD",
"LEGACY_TEST_CARD",
"PAYMENT_APPROVED",
"PAYMENT_DECLINED",
"PAYMENT_METHODS",
"SANDBOX_BANK_APPROVED",
"SANDBOX_CARD_APPROVED",
"SANDBOX_CARD_DECLINED",
"SandboxPaymentMethod",
"payment_method",
"payment_method_label",
"public_payment_methods",
]
|