From 4a111fcbdda903290d157a024700b5a6d55515aa Mon Sep 17 00:00:00 2001 From: xingc Date: Tue, 6 May 2025 21:10:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(pay):=20=E7=A7=BB=E9=99=A4=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E6=B2=99=E7=9B=92=E6=A8=A1=E5=BC=8F=20v2=E6=B2=99?= =?UTF-8?q?=E7=9B=92=E6=A8=A1=E5=BC=8F=E5=B7=B2=E4=B8=8B=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/routers/pay/routes.py | 6 +++--- core/wechat/api.py | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/routers/pay/routes.py b/core/routers/pay/routes.py index e6e9dcf..4ae979f 100644 --- a/core/routers/pay/routes.py +++ b/core/routers/pay/routes.py @@ -61,10 +61,10 @@ async def create_order( request.client.host ) out_trade_no = generate_order_number() - prepay_id = wechat_pay_api.create_unified_order( + prepay_id = await wechat_pay_api.create_unified_order( **payload.model_dump(exclude_none=True, exclude={'product_id'}), out_trade_no=out_trade_no, - spbill_create_ip=client_ip + create_ip=client_ip ) payment_params = wechat_pay_api.generate_payment_params(prepay_id) db_crud = CRUD(db_session, Order) @@ -77,7 +77,7 @@ async def create_order( 'status': base_schema.OrderStatusType.WAIT_PAYMENT }, filters={ - 'out_trade_no': out_trade_no, + 'order_id': out_trade_no, } ) return {'data': {'payment_params': payment_params, 'order_id': order.order_id}} diff --git a/core/wechat/api.py b/core/wechat/api.py index 0d47951..e18a832 100644 --- a/core/wechat/api.py +++ b/core/wechat/api.py @@ -33,16 +33,17 @@ class BaseClient: r = await self.session.request( method, url, headers=headers, params=params, data=data, json=json, **kwargs ) - result = r.json() - if result.get('errcode'): - logger.debug('\n'.join([ - f'method : {method}' - f'url : {url}' - f'params : {params}' - f'body : {data or json}' - f'response: {r.text}' - ])) - raise WechatApiError('used result: {}'.format(result.get('errcode'))) + if 'json' in r.headers.get('Content-Type') or '': + result = r.json() + if result.get('errcode'): + logger.debug('\n'.join([ + f'method : {method}' + f'url : {url}' + f'params : {params}' + f'body : {data or json}' + f'response: {r.text}' + ])) + raise WechatApiError('used result: {}'.format(result.get('errcode'))) return r async def get(self, url, headers=None, params=None, **kwargs): @@ -144,8 +145,6 @@ class WechatPayApi(BaseClient): self.appi_key = app_key self.notify_url = notify_url self.base_url = 'https://api.mch.weixin.qq.com' - if debug: - self.base_url = f'{self.base_url}/sandboxnew' super().__init__() @staticmethod @@ -178,6 +177,7 @@ class WechatPayApi(BaseClient): total_fee: int, create_ip: str, ): + """https://pay.weixin.qq.com/doc/v2/merchant/4011936530""" url = self.base_url + '/pay/unifiedorder' params = { 'appid': self.appid,