From ff5fc5eba6cbf0efd672ea6e04bf10431258b887 Mon Sep 17 00:00:00 2001 From: wwwwwwwww <526125649@qq.com> Date: Thu, 11 Jun 2026 10:59:16 +0800 Subject: [PATCH] 1 --- video-gen-api/app/services/payment.py | 117 ++++++++++---------------- 1 file changed, 43 insertions(+), 74 deletions(-) diff --git a/video-gen-api/app/services/payment.py b/video-gen-api/app/services/payment.py index e8e2ffca..9ee92dcc 100644 --- a/video-gen-api/app/services/payment.py +++ b/video-gen-api/app/services/payment.py @@ -57,87 +57,56 @@ class DailyFileHandler(logging.FileHandler): self._file_handler = logging.FileHandler( self.baseFilename, mode="a", encoding=self.encoding ) - if self._file_handler: - self._file_handler.emit(record) - else: - super().emit(record) + self._file_handler.setFormatter(self.formatter) + self._current_date = date_str + self.stream = self._file_handler.stream + super().emit(record) -# Add daily file handler -_log_handler = DailyFileHandler(_log_dir) -_log_formatter = logging.Formatter( - "[%(asctime)s] %(levelname)s %(message)s", - datefmt="%Y-%m-%d %H:%M:%S" -) -_log_handler.setFormatter(_log_formatter) -logger.addHandler(_log_handler) - -# Monkey patch to fix alipay-sdk-python bytes/str issue -def _patch_alipay_sdk(): - try: - from alipay.aop.api.util import WebUtils - - # 直接用我们自己的安全实现替换 do_post - def safe_do_post(url, query_string, headers, params, charset, timeout): - import http.client - import socket - from urllib.parse import urlparse - - parse_result = urlparse(url) - if parse_result.scheme == 'https': - conn = http.client.HTTPSConnection( - parse_result.hostname, - parse_result.port or 443, - timeout=timeout - ) - else: - conn = http.client.HTTPConnection( - parse_result.hostname, - parse_result.port or 80, - timeout=timeout - ) - - try: - body = query_string.encode(charset) if params is None else params - conn.request( - 'POST', - parse_result.path + ('?' + parse_result.query if parse_result.query else ''), - body, - headers - ) - response = conn.getresponse() - if response.status == 200: - response_body = response.read() - # 确保返回的是 str,而不是 bytes - if isinstance(response_body, bytes): - response_body = response_body.decode(charset) - return response_body - else: - response_body = response.read() - if isinstance(response_body, bytes): - response_body = response_body.decode(charset) - raise Exception(f"invalid http status {response.status}, detail body: {response_body}") - except socket.timeout: - raise Exception("timeout") - finally: - conn.close() - - # Apply patch - 直接替换,避免原函数的问题 - WebUtils.do_post = safe_do_post - logger.info("Successfully replaced alipay-sdk-python WebUtils.do_post with safe implementation") - - except ImportError: - pass - except Exception as e: - logger.warning(f"Failed to patch alipay-sdk-python: {e}") - -# Apply the patch when module is loaded -_patch_alipay_sdk() +_handler = DailyFileHandler(_log_dir) +_handler.setFormatter(logging.Formatter( + "[%(asctime)s] %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S" +)) +if not logger.handlers: + logger.addHandler(_handler) # Orders pending payment for longer than this are auto-cancelled ORDER_EXPIRE_MINUTES = 5 +# --------------------------------------------------------------------------- +# Monkey-patch alipay-sdk-python WebUtils.do_post to fix bytes concatenation bug +# The SDK's error handling does: '...' + response.read() +# but response.read() returns bytes, causing TypeError on Python 3 +# --------------------------------------------------------------------------- +def _patch_alipay_webutils(): + try: + from alipay.aop.api.util import WebUtils + _original_do_post = WebUtils.do_post + + def _patched_do_post(url, query_string, headers, params, charset, timeout=30): + try: + return _original_do_post(url, query_string, headers, params, charset, timeout) + except TypeError as e: + if "can only concatenate str (not 'bytes') to str" in str(e): + # Decode bytes response to string and retry + import http.client as _http + from urllib.parse import urlparse as _urlparse + parsed = _urlparse(url) + conn = _http.HTTPSConnection(parsed.hostname, context=__import__('ssl').create_default_context()) + conn.request("POST", parsed.path + "?" + query_string, params, headers) + resp = conn.getresponse() + body = resp.read().decode("utf-8", errors="replace") + raise RuntimeError(f"Alipay API error (status {resp.status}): {body}") from e + raise + + WebUtils.do_post = _patched_do_post + except ImportError: + pass + +_patch_alipay_webutils() + + # --------------------------------------------------------------------------- # Config helpers – read from system_configs table (admin panel) # ---------------------------------------------------------------------------