unipay/unipay/payfee.py
2025-12-15 15:34:12 +08:00

80 lines
2.1 KiB
Python

# payfee
from ahserver.serverenv import ServerEnv
from sqlor.dbpools import DBPools
async def get_pay_fee(providerid, amount):
db = DBPools()
env = ServerEnv()
dbname = env.get_module_dbname('unipay')
async with db.sqlorContext(dbname) as sor:
return await sor_get_pay_fee(sor, providerid, amount)
return None
async def get_paychannels():
env = ServerEnv()
db = DBPools()
dbname = env.get_module_dbname('unipay')
async with db.sqlorContext(dbname) as sor:
return await sor_get_paychannels(sor)
return None
async def sor_get_paychannels(sor):
env = ServerEnv()
dbname = env.get_module_dbname('unipay')
biz_date = env.get_business_date(sor)
sql = """select
a.id, a.name, b.fee_rate
from paychannel a left join payfee b
on a.id = b.channelid
where
a.enabled_date <= ${biz_date}$
and a.expired_date >= ${biz_date}$
and b.enabled_date <= ${biz_date}$
and b.expired_date >= ${biz_date}$
"""
recs = await sor.sqlExe(sql, {'channelid': channelid,
'biz_date': biz_date})
return recs
async def get_pay_feerate(sor, channelid):
env = ServerEnv()
db = DBPools()
dbname = env.get_module_dbname('unipay')
async with db.sqlorContext(dbname) as sor:
return await sor_get_pay_feerate(sor, channelid)
return None
async def sor_get_pay_feerate(sor, channelid):
env = ServerEnv()
dbname = env.get_module_dbname('unipay')
biz_date = env.get_business_date(sor)
sql = """select
a.id, a.name, b.fee_rate
from paychannel a left join payfee b
on a.id = b.channelid
where
a.enabled_date <= ${biz_date}$
and a.expired_date >= ${biz_date}$
and b.enabled_date <= ${biz_date}$
and b.expired_date >= ${biz_date}$
and a.id = ${channelid}$
"""
recs = await sor.sqlExe(sql, {'channelid': channelid,
'biz_date': biz_date})
if len(recs) > 0:
return recs[0]
return None
async def sor_get_pay_fee(sor, providerid, amount):
rec = await sor_get_pay_feerate(sor, providerid)
if rec is None:
e = Exception(f'{providerid} pay channel not found')
exception(f'Exception:{e}')
raise e
if rec.fee_rate is None:
e = Exception(f'{providerid} channel has not defined fee rate')
exception(f'Exception:{e}')
raise e
return rec.fee_rate * amount