```python import re _V1_AMOUNT_RE = re.compile(r"(0|[1-9][0-9]*)(\.[0-9]{1,2})?") _V1_ALLOWED_FIELDS = frozenset({"orderNo", "amount", "reason", "notifyUrl"}) _V2_ALLOWED_FIELDS = frozenset({"order_id", "amount_cents", "reason", "notify_url"}) def _validate_order_id(value, field): if not isinstance(value, str) or value == "": raise ValueError(f"{field} must be a non-empty string") return value def _validate_reason(value): if not isinstance(value, str): raise ValueError("reason must be a string") return value def _validate_notify_url(value, field): if value is None: return None if not isinstance(value, str) or value == "": raise ValueError(f"{field} must be a non-empty string or null") return value def _yuan_str_to_cents(raw): # Decimal-string only: rejects float/int/bool, scientific notation, # leading zeros, negatives, NaN and >2-decimal precision without any # binary-float parsing. if isinstance(raw, bool) or not isinstance(raw, str): raise ValueError("amount must be a decimal string") if _V1_AMOUNT_RE.fullmatch(raw) is None: raise ValueError("amount has invalid format or precision") int_part, _sep, frac_part = raw.partition(".") cents = int(int_part) * 100 + int((frac_part + "00")[:2]) if cents <= 0: raise ValueError("amount must be greater than zero") return cents def _cents_to_yuan_str(cents): return "{}.{:02d}".format(cents // 100, cents % 100) def handle_refund(version, body, service): if version not in ("v1", "v2"): raise ValueError("unsupported version, only 'v1' or 'v2' allowed") if not isinstance(body, dict): raise ValueError("body must be a dict") if version == "v1": unknown = set(body) - _V1_ALLOWED_FIELDS if unknown: raise ValueError(f"unknown v1 fields: {sorted(unknown)}") if "orderNo" not in body: raise ValueError("missing required field: orderNo") if "amount" not in body: raise ValueError("missing required field: amount") canonical = { "order_id": _validate_order_id(body["orderNo"], "orderNo"), "amount_cents": _yuan_str_to_cents(body["amount"]), "reason": _validate_reason(body.get("reason", "")), "notify_url": _validate_notify_url(body.get("notifyUrl", None), "notifyUrl"), } else: unknown = set(body) - _V2_ALLOWED_FIELDS if unknown: raise ValueError(f"unknown v2 fields: {sorted(unknown)}") if "order_id" not in body: raise ValueError("missing required field: order_id") if "amount_cents" not in body: raise ValueError("missing required field: amount_cents") raw_cents = body["amount_cents"] if isinstance(raw_cents, bool) or not isinstance(raw_cents, int) or raw_cents <= 0: raise ValueError("amount_cents must be a positive integer") canonical = { "order_id": _validate_order_id(body["order_id"], "order_id"), "amount_cents": raw_cents, "reason": _validate_reason(body.get("reason", "")), "notify_url": _validate_notify_url(body.get("notify_url", None), "notify_url"), } # Snapshot the canonical amount before invoking the service so that the # response reflects this request even if the callee mutates the dict. amount_cents = canonical["amount_cents"] result = service(canonical) if not isinstance(result, dict): raise ValueError("service result must be a dict") if version == "v1": # Whitelist projection: legacy merchants only ever see the three # fields, so created_at / internal_ticket / future fields cannot leak. return { "refundNo": result["refund_id"], "status": result["status"], "amount": _cents_to_yuan_str(amount_cents), } response = dict(result) response["amount_cents"] = amount_cents return response ```