```python import re _AMOUNT_RE = re.compile(r"^(0|[1-9][0-9]*)(\.[0-9]{1,2})?$") _V1_FIELDS = ("orderNo", "amount", "reason", "notifyUrl") _V2_FIELDS = ("order_id", "amount_cents", "reason", "notify_url") def _require_non_empty_str(value, name): if not isinstance(value, str): raise ValueError("%s must be a string" % name) if value == "": raise ValueError("%s must be a non-empty string" % name) return value def _parse_yuan_to_cents(value): if not isinstance(value, str): raise ValueError("amount must be a decimal yuan string") if _AMOUNT_RE.fullmatch(value) is None: raise ValueError("amount has invalid format: %r" % (value,)) whole, _, frac = value.partition(".") cents = int(whole) * 100 + (int(frac.ljust(2, "0")) if frac else 0) if cents <= 0: raise ValueError("amount must be greater than 0") return cents def _format_cents(cents): return "%d.%02d" % (cents // 100, cents % 100) def _read_reason(body): if "reason" not in body: return "" value = body["reason"] if not isinstance(value, str): raise ValueError("reason must be a string") return value def _read_notify_url(body, key): if key not in body or body[key] is None: return None value = body[key] if not isinstance(value, str) or value == "": raise ValueError("%s must be a non-empty string or null" % key) return value def _check_fields(body, allowed): unknown = set(body) - set(allowed) if unknown: raise ValueError( "unknown field(s): %s" % ", ".join(sorted(str(k) for k in unknown)) ) def _validate_common_args(version, body, service): if version not in ("v1", "v2"): raise ValueError("unsupported version: %r" % (version,)) if not isinstance(body, dict): raise ValueError("body must be a dict") if not callable(service): raise ValueError("service must be callable") def _handle_v1(body, service): _check_fields(body, _V1_FIELDS) if "orderNo" not in body: raise ValueError("orderNo is required") if "amount" not in body: raise ValueError("amount is required") order_id = _require_non_empty_str(body["orderNo"], "orderNo") amount_cents = _parse_yuan_to_cents(body["amount"]) reason = _read_reason(body) notify_url = _read_notify_url(body, "notifyUrl") canonical = { "order_id": order_id, "amount_cents": amount_cents, "reason": reason, "notify_url": notify_url, } result = service(canonical) if not isinstance(result, dict): raise ValueError("service must return a dict") if "refund_id" not in result or "status" not in result: raise ValueError("service result missing refund_id or status") return { "refundNo": result["refund_id"], "status": result["status"], "amount": _format_cents(amount_cents), } def _handle_v2(body, service): _check_fields(body, _V2_FIELDS) if "order_id" not in body: raise ValueError("order_id is required") if "amount_cents" not in body: raise ValueError("amount_cents is required") order_id = _require_non_empty_str(body["order_id"], "order_id") raw_cents = body["amount_cents"] if isinstance(raw_cents, bool) or not isinstance(raw_cents, int): raise ValueError("amount_cents must be a positive integer") if raw_cents <= 0: raise ValueError("amount_cents must be greater than 0") amount_cents = raw_cents reason = _read_reason(body) notify_url = _read_notify_url(body, "notify_url") canonical = { "order_id": order_id, "amount_cents": amount_cents, "reason": reason, "notify_url": notify_url, } result = service(canonical) if not isinstance(result, dict): raise ValueError("service must return a dict") response = dict(result) response["amount_cents"] = amount_cents return response def handle_refund(version, body, service): _validate_common_args(version, body, service) if version == "v1": return _handle_v1(body, service) return _handle_v2(body, service) ```