This commit is contained in:
yumoqing 2025-12-18 15:24:47 +08:00
parent 3c94e3c7a4
commit bd76c34b34
2 changed files with 51 additions and 23 deletions

View File

@ -1,6 +1,7 @@
# init.py # init.py
import os import os
from appPublic.log import debug,exception from appPublic.log import debug,exception
from ahserver.configuredServer import add_startup
from ahserver.serverenv import ServerEnv from ahserver.serverenv import ServerEnv
from .notify import get_provider, get_provider_channel from .notify import get_provider, get_provider_channel
from .paylog import PaymentLog, unipay_accounting from .paylog import PaymentLog, unipay_accounting
@ -124,18 +125,7 @@ async def refund_payment(request, params_kw=None):
raise e raise e
# 回调入口:你可把厂商回调用各自 endpoint 再转发到这里,或在厂商控制台按各自 URL 配置 # 回调入口:你可把厂商回调用各自 endpoint 再转发到这里,或在厂商控制台按各自 URL 配置
async def payment_notify(request, provider, params_kw): async def payment_notify_handle(request, data):
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
logid = data.params.out_trade_no logid = data.params.out_trade_no
pl = PaymentLog(request._run_ns) pl = PaymentLog(request._run_ns)
plog = await pl.payed_log(logid) plog = await pl.payed_log(logid)
@ -145,6 +135,48 @@ async def payment_notify(request, provider, params_kw):
else: else:
return "OK" 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}" # callback url= "/unipay/notify/{provider}"
def load_unipay(): def load_unipay():
@ -163,5 +195,6 @@ def load_unipay():
env.get_pay_fee = get_pay_fee env.get_pay_fee = get_pay_fee
env.sor_get_pay_fee = sor_get_pay_fee env.sor_get_pay_fee = sor_get_pay_fee
env.PaymentLog = PaymentLog env.PaymentLog = PaymentLog
add_startup(setup_callback_path)

View File

@ -36,6 +36,8 @@ class PaymentLog:
"currency": currency, "currency": currency,
"payment_status": '0', "payment_status": '0',
"init_timestamp": timestampstr(), "init_timestamp": timestampstr(),
"payed_timestamp": "1900-01-01 00:00:00",
"cancel_timestamp": "1900-01-01 00:00:00",
"userid": userid "userid": userid
} }
await sor.C('payment_log', ns.copy()) await sor.C('payment_log', ns.copy())
@ -77,20 +79,12 @@ async def unipay_accounting(request, data):
db = DBPools() db = DBPools()
dbname = env.get_module_dbname('unipay') dbname = env.get_module_dbname('unipay')
async with db.sqlorContext(dbname) as sor: 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: if len(recs) < 1:
e = Exception(f'{logid} not found {data=}') e = Exception(f'{logid} not found {data=} or accounted')
exception(f'{e}') exception(f'{e}')
raise e raise e
r = recs[0] 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, await env.recharge_accounting(sor,
r.customerid, r.customerid,
'RECHARGE', 'RECHARGE',
@ -100,7 +94,8 @@ async def unipay_accounting(request, data):
r.pay_feerate r.pay_feerate
) )
await sor.U('payment_log', {'id': logid, await sor.U('payment_log', {'id': logid,
'payment_status': '2', 'payment_status': '1',
'payed_timestamp': timestampstr(),
'channel_trade_id': trade_id 'channel_trade_id': trade_id
}) })