105 lines
2.9 KiB
Python
105 lines
2.9 KiB
Python
from sqlor.dbpools import DBPools
|
|
from appPublic.timeUtils import timestampstr
|
|
from appPublic.log import debug, exception
|
|
from appPublic.dictObject import DictObject
|
|
from ahserver.serverenv import ServerEnv
|
|
|
|
class PaymentLog:
|
|
def __init__(self, env):
|
|
self.db = DBPools()
|
|
self.env = env
|
|
|
|
async def new_log(self,
|
|
userid, customerid, channel,
|
|
payment_name, amount, feerate,
|
|
client_ip, currency='CNY'):
|
|
dbname = self.env.get_module_dbname('unipay')
|
|
async with self.db.sqlorContext(dbname) as sor:
|
|
return await self.sor_new_log(sor, userid, customerid, channel,
|
|
payment_name, amount, feerate,
|
|
client_ip, currency=currency)
|
|
return None
|
|
async def sor_new_log(self, sor,
|
|
userid, customerid, channel,
|
|
payment_name, amount, feerate,
|
|
client_ip, currency='CNY'):
|
|
debug(f"{userid=}, {customerid=}, {amount=},{feerate=},{client_ip=}")
|
|
ns = {
|
|
"id": self.env.uuid(),
|
|
"customerid": customerid,
|
|
"channelid": channel,
|
|
"payment_name": payment_name,
|
|
"payer_client_ip": client_ip,
|
|
"amount_total": amount,
|
|
"pay_feerate": feerate,
|
|
"pay_fee": feerate * amount,
|
|
"currency": currency,
|
|
"payment_status": '0',
|
|
"init_timestamp": timestampstr(),
|
|
"payed_timestamp": "2000-01-01 00:00:00.001",
|
|
"cancel_timestamp": "2000-01-01 00:00:00.001",
|
|
"userid": userid
|
|
}
|
|
await sor.C('payment_log', ns.copy())
|
|
return DictObject(**ns)
|
|
|
|
async def cancel_log(self, logid):
|
|
dbname = self.env.get_module_dbname('unipay')
|
|
async with self.db.sqlorContext(dbname) as sor:
|
|
ns = {
|
|
"id": logid,
|
|
"cancel_timestamp": timestampstr()
|
|
}
|
|
await sor.U('payment_log', ns)
|
|
recs = await sor.R('payment_log', {"id": logid})
|
|
if len(recs) > 0:
|
|
return recs[0]
|
|
return None
|
|
return None
|
|
|
|
async def payed_log(self, logid):
|
|
dbname = self.env.get_module_dbname('unipay')
|
|
async with self.db.sqlorContext(dbname) as sor:
|
|
ns = {
|
|
"id": logid,
|
|
"payment_status":'1',
|
|
"payed_timestamp": timestampstr()
|
|
}
|
|
await sor.U('payment_log', ns)
|
|
recs = await sor.R('payment_log', {"id": logid})
|
|
if len(recs) > 0:
|
|
return recs[0]
|
|
return None
|
|
return None
|
|
|
|
async def unipay_accounting(request, data):
|
|
logid = data.out_trade_no
|
|
trade_id = data.trade_no
|
|
env = request._run_ns
|
|
db = DBPools()
|
|
dbname = env.get_module_dbname('unipay')
|
|
async with db.sqlorContext(dbname) as sor:
|
|
recs = await sor.R('payment_log', {'id': logid, 'payment_status': '0'})
|
|
if len(recs) < 1:
|
|
e = Exception(f'{logid} not found {data=} or accounted')
|
|
exception(f'{e}')
|
|
raise e
|
|
r = recs[0]
|
|
await env.recharge_accounting(sor,
|
|
r.customerid,
|
|
'RECHARGE',
|
|
r.id,
|
|
await env.get_business_date(sor),
|
|
r.amount_total,
|
|
r.pay_feerate
|
|
)
|
|
await sor.U('payment_log', {'id': logid,
|
|
'payment_status': '1',
|
|
'payed_timestamp': timestampstr(),
|
|
'channel_trade_id': trade_id
|
|
})
|
|
|
|
return True
|
|
exception(f'{db.e_except}')
|
|
return False
|