From bd76c34b34d33fb4c00bfbac2466230dba35f34d Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 18 Dec 2025 15:24:47 +0800 Subject: [PATCH] bugfix --- unipay/init.py | 57 ++++++++++++++++++++++++++++++++++++++---------- unipay/paylog.py | 17 +++++---------- 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/unipay/init.py b/unipay/init.py index 97f6145..e37489f 100644 --- a/unipay/init.py +++ b/unipay/init.py @@ -1,6 +1,7 @@ # init.py import os from appPublic.log import debug,exception +from ahserver.configuredServer import add_startup from ahserver.serverenv import ServerEnv from .notify import get_provider, get_provider_channel from .paylog import PaymentLog, unipay_accounting @@ -124,18 +125,7 @@ async def refund_payment(request, params_kw=None): raise e # 回调入口:你可把厂商回调用各自 endpoint 再转发到这里,或在厂商控制台按各自 URL 配置 -async def payment_notify(request, provider, params_kw): - if PROVIDERS[provider] is None: - e = Exception(f'{provider} cannot pay') - exception(f'{e}') - return - try: - data = await PROVIDERS[provider].handle_notify(request, params_kw) - except Exception as e: - e = Exception(f'{provider} cannot pay') - exception(f'{e}') - return - +async def payment_notify_handle(request, data): logid = data.params.out_trade_no pl = PaymentLog(request._run_ns) plog = await pl.payed_log(logid) @@ -145,6 +135,48 @@ async def payment_notify(request, provider, params_kw): else: return "OK" +async def wechat_notify(request): + debug("wechat notify called .......") + provider = 'wechat' + if PROVIDERS[provider] is None: + e = Exception(f'{provider} cannot pay') + exception(f'{e}') + return + try: + data = await PROVIDERS[provider].handle_notify(request) + except Exception as e: + e = Exception(f'{provider} cannot pay') + exception(f'{e}') + return + try: + await unipay_accounting(request, data) + except Exception as e: + exception(f'{e}') + return {"code":"SUCCESS", "message":"OK"} + +async def alipay_notify(request): + debug("alipay notify called .......") + provider = 'alipay' + if PROVIDERS[provider] is None: + e = Exception(f'{provider} cannot pay') + exception(f'{e}') + return + try: + data = await PROVIDERS[provider].handle_notify(request) + except Exception as e: + e = Exception(f'{provider} cannot pay') + exception(f'{e}') + return + try: + await unipay_accounting(request, data) + except Exception as e: + exception(f'{e}') + return {"code":"SUCCESS", "message":"OK"} + +async def setup_callback_path(app): + app.router.add_post('/unipay/notify/wechat', wechat_notify) + app.router.add_post('/unipay/notify/alipay', wechat_notify) + # callback url= "/unipay/notify/{provider}" def load_unipay(): @@ -163,5 +195,6 @@ def load_unipay(): env.get_pay_fee = get_pay_fee env.sor_get_pay_fee = sor_get_pay_fee env.PaymentLog = PaymentLog + add_startup(setup_callback_path) diff --git a/unipay/paylog.py b/unipay/paylog.py index 55f22f5..fb1adae 100644 --- a/unipay/paylog.py +++ b/unipay/paylog.py @@ -36,6 +36,8 @@ class PaymentLog: "currency": currency, "payment_status": '0', "init_timestamp": timestampstr(), + "payed_timestamp": "1900-01-01 00:00:00", + "cancel_timestamp": "1900-01-01 00:00:00", "userid": userid } await sor.C('payment_log', ns.copy()) @@ -77,20 +79,12 @@ async def unipay_accounting(request, data): db = DBPools() dbname = env.get_module_dbname('unipay') async with db.sqlorContext(dbname) as sor: - recs = await sor.R('payment_log', {'id': logid}) + recs = await sor.R('payment_log', {'id': logid, 'payment_status': '0'}) if len(recs) < 1: - e = Exception(f'{logid} not found {data=}') + e = Exception(f'{logid} not found {data=} or accounted') exception(f'{e}') raise e r = recs[0] - if r.payment_status == '2': - e = Exception(f'{logid} payment_log status({r.payment_status}) is already accounted') - exception(f'{e}') - return True - if r.payment_status != '1': - e = Exception(f'{logid} payment_log status({r.payment_status}) not correct') - exception(f'{e}') - return False await env.recharge_accounting(sor, r.customerid, 'RECHARGE', @@ -100,7 +94,8 @@ async def unipay_accounting(request, data): r.pay_feerate ) await sor.U('payment_log', {'id': logid, - 'payment_status': '2', + 'payment_status': '1', + 'payed_timestamp': timestampstr(), 'channel_trade_id': trade_id })