update
This commit is contained in:
parent
4ed65b8210
commit
f5e1763d2e
@ -745,11 +745,32 @@ async def update_baidu_order_list(ns={}):
|
||||
}
|
||||
}
|
||||
|
||||
async def baidu_orderdetail_by_uuid(ns={}):
|
||||
order_id = ns.get('order_id')
|
||||
baidu_id = ns.get('baidu_id')
|
||||
method = 'POST'
|
||||
nss = {'uuids': [order_id], 'queryAccountId': baidu_id}
|
||||
ns_format = '&'.join(['%s=%s' % (k, v) for k, v in ns.items()])
|
||||
url = 'https://billing.baidubce.com/v1/order/getByUuid?%s' % ns_format
|
||||
method = 'POST'
|
||||
header = {
|
||||
"Host": "billing.baidubce.com",
|
||||
"ContentType": "application/json;charset=UTF-8"
|
||||
}
|
||||
header = await get_auth_header(method=method, url=url, header=header)
|
||||
async with aiohttp_client.request(
|
||||
method=method,
|
||||
url=url,
|
||||
headers=header,
|
||||
json=nss) as res:
|
||||
data_ = await res.json()
|
||||
return {
|
||||
'status': True,
|
||||
'msg': 'get baidu order detail success',
|
||||
'data': data_
|
||||
}
|
||||
|
||||
async def baidu_confirm_refund_order(ns={}):
|
||||
# ns = {
|
||||
# 'order_id': ["2996f0baf34c4a0a98e1da0b4e290a35"],
|
||||
# 'userid': 'y_xQK0G62dtZT5EneMQFT'
|
||||
# }
|
||||
import asyncio
|
||||
# 把 NEED_CONFIRM的订单同步到本地库,用于后续状态更新
|
||||
await update_baidu_order_list({'userid': ns.get('userid')})
|
||||
@ -764,18 +785,21 @@ async def baidu_confirm_refund_order(ns={}):
|
||||
'msg': '用户 %s 未绑定百度智能云账号' % ns.get('userid')
|
||||
}
|
||||
|
||||
orders = json.loads(ns.get('order_id')) if isinstance(ns.get('order_id'), str) else ns.get('order_id')
|
||||
refund_orders_base_list = json.loads(ns.get('order_id')) if isinstance(ns.get('order_id'), str) else ns.get('order_id')
|
||||
|
||||
for order_id in refund_orders_base_list:
|
||||
with open('baidu_refund_order_id.txt', 'a+') as f:
|
||||
f.write('%s 当前退订订单ID: %s ' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), order_id))
|
||||
|
||||
for order_id in orders:
|
||||
db = DBPools()
|
||||
async with db.sqlorContext('kboss') as sor:
|
||||
refund_status_li = await sor.R('baidu_orders', {'orderid': order_id})
|
||||
refundstatus = refund_status_li[0]['refundstatus']
|
||||
refund_id = refund_status_li[0]['id']
|
||||
|
||||
ns_record_li = await sor.R('user_action', {'orderid': order_id})
|
||||
ns_record_id = ns_record_li[0]['id'] if ns_record_li else None
|
||||
ns_record = {'id': ns_record_id} if ns_record_id else None
|
||||
# ns_record_li = await sor.R('user_action', {'orderid': order_id})
|
||||
# ns_record_id = ns_record_li[0]['id'] if ns_record_li else None
|
||||
# ns_record = {'id': ns_record_id} if ns_record_id else None
|
||||
|
||||
if not refundstatus:
|
||||
# data_ = {}
|
||||
@ -787,6 +811,7 @@ async def baidu_confirm_refund_order(ns={}):
|
||||
header = {
|
||||
"Host": "billing.baidubce.com"
|
||||
}
|
||||
await asyncio.sleep(0.5)
|
||||
header = await get_auth_header(method=method, url=url, header=header)
|
||||
async with aiohttp_client.request(
|
||||
method=method,
|
||||
@ -808,19 +833,8 @@ async def baidu_confirm_refund_order(ns={}):
|
||||
async with db.sqlorContext('kboss') as sor:
|
||||
await sor.U('baidu_orders', {'id': refund_id, 'refundstatus': '1'})
|
||||
|
||||
# 增加延迟
|
||||
await asyncio.sleep(4)
|
||||
|
||||
# 延迟2-3秒还是获取到 ready状态的订单,那就重复请求一次,目的是尽快刷新状态
|
||||
async with aiohttp_client.request(
|
||||
method=method,
|
||||
url=url,
|
||||
headers=header,
|
||||
json=paydata) as res:
|
||||
await res.json()
|
||||
|
||||
# 把 NEED_CONFIRM的本地库改为CREATED
|
||||
await update_baidu_order_list({'userid': ns.get('userid')})
|
||||
with open('baidu_refund_order_id.txt', 'a+') as f:
|
||||
f.write('远程退订成功: %s \n' % str(data_))
|
||||
else:
|
||||
ns_record = {
|
||||
'orderid': order_id,
|
||||
@ -828,43 +842,84 @@ async def baidu_confirm_refund_order(ns={}):
|
||||
'userid': ns.get('userid'),
|
||||
'reason': '产品退费失败, %s' % str(data_)[:400]
|
||||
}
|
||||
with open('baidu_refund_order_id.txt', 'a+') as f:
|
||||
f.write('远程退订失败: %s ' % str(data_))
|
||||
|
||||
db = DBPools()
|
||||
async with db.sqlorContext('kboss') as sor:
|
||||
await asyncio.sleep(3)
|
||||
order_status_dict = await baidu_orderdetail_by_uuid({'order_id': order_id, 'baidu_id': ns.get('baidu_id')})
|
||||
if order_status_dict.get('data') and order_status_dict['data'].get('orders') and order_status_dict['data']['orders'][0]['status'] == 'NEED_CONFIRM':
|
||||
await user_action_record(ns_record)
|
||||
return {
|
||||
'status': False,
|
||||
'msg': '产品退款出错!%s' % str(data_)[:400]
|
||||
# 从列表中移除该订单ID
|
||||
refund_orders_base_list.remove(order_id)
|
||||
ns_err_log = {
|
||||
'id': uuid(),
|
||||
'log_level': 'ERROR',
|
||||
'log_content': '检测到 产品仍在使用中 暂时不退费, 订单详情 %s' % str(order_status_dict['data']),
|
||||
'user_id': ns.get('userid'),
|
||||
'request_url': '/baiduc//baidu_confirm_refund_order.dspy',
|
||||
'request_params': json.dumps({'order_id': order_id, 'baidu_id': ns.get('baidu_id')}),
|
||||
}
|
||||
await sor.C('warn_error_log', ns_err_log)
|
||||
with open('baidu_refund_order_id.txt', 'a+') as f:
|
||||
f.write('检测到 产品仍在使用中 暂时不退费 %s \n' % str(order_status_dict['data']))
|
||||
else:
|
||||
ns_record['reason'] = '远程退款失败, 产品已释放, 订单状态 %s, 正常退费' % order_status_dict['data']['orders'][0]['status']
|
||||
await user_action_record(ns_record)
|
||||
with open('baidu_refund_order_id.txt', 'a+') as f:
|
||||
f.write('检测到 产品已释放,订单状态 %s, 继续进入退费程序 \n' % order_status_dict['data']['orders'][0]['status'])
|
||||
|
||||
if refundstatus == '2':
|
||||
"""
|
||||
退款状态为2, 说明退款成功, 无需继续处理
|
||||
"""
|
||||
refund_orders_base_list.remove(order_id)
|
||||
continue
|
||||
|
||||
# 分割
|
||||
# 增加延迟
|
||||
await asyncio.sleep(5)
|
||||
|
||||
# 把 NEED_CONFIRM的本地库改为CREATED
|
||||
await update_baidu_order_list({'userid': ns.get('userid')})
|
||||
|
||||
|
||||
for order_id in refund_orders_base_list:
|
||||
# 获取created状态后再去退款
|
||||
local_refund_status = await get_baidu_orderlist({'order_id': order_id, 'userid': ns.get('userid')})
|
||||
print('local_refund_status', local_refund_status)
|
||||
if local_refund_status.get('status'):
|
||||
db = DBPools()
|
||||
async with db.sqlorContext('kboss') as sor:
|
||||
refund_status_li = await sor.R('baidu_orders', {'orderid': order_id})
|
||||
refundstatus = refund_status_li[0]['refundstatus']
|
||||
refund_id = refund_status_li[0]['id']
|
||||
baidu_orders_status_update = """update baidu_orders set refundstatus='2' where id='%s';""" % refund_id
|
||||
await sor.sqlExe(baidu_orders_status_update, {})
|
||||
await sor.U('user_action', {'id': ns_record.get('id'), 'ordertype': 'REFUND', 'reason': '远程退款成功, 本地客户退款成功'})
|
||||
ns_record_li = await sor.R('user_action', {'orderid': order_id})
|
||||
ns_record_id = ns_record_li[0]['id'] if ns_record_li else None
|
||||
await sor.U('user_action', {'id': ns_record_id, 'ordertype': 'REFUND', 'reason': '远程退款成功, 本地客户退款成功'})
|
||||
continue
|
||||
|
||||
# return {
|
||||
# 'status': True,
|
||||
# 'msg': '百度云给平台退款成功,平台给客户退款成功'
|
||||
# }
|
||||
else:
|
||||
if local_refund_status.get('msg') == 'delay_order':
|
||||
return {
|
||||
'status': False,
|
||||
'msg': '百度远程订单还未生成, 请十秒后重试, %s' % str(local_refund_status)
|
||||
ns_err_log = {
|
||||
'id': uuid(),
|
||||
'log_level': 'ERROR',
|
||||
'log_content': '远程退款成功,本地退款失败 %s' % str(local_refund_status),
|
||||
'user_id': ns.get('userid'),
|
||||
'request_url': '/baiduc//baidu_confirm_refund_order.dspy',
|
||||
'request_params': json.dumps({'order_id': order_id, 'baidu_id': ns.get('baidu_id')}),
|
||||
}
|
||||
return {
|
||||
'status': False,
|
||||
'msg': '百度云退款成功,本机构给客户退款出错!, %s' % str(local_refund_status)
|
||||
await sor.C('warn_error_log', ns_err_log)
|
||||
|
||||
ns_record = {
|
||||
'orderid': order_id,
|
||||
'ordertype': 'REFUND',
|
||||
'userid': ns.get('userid'),
|
||||
'reason': '远程退款成功,本地退款失败, %s' % str(local_refund_status)
|
||||
}
|
||||
await user_action_record(ns_record)
|
||||
continue
|
||||
|
||||
return {
|
||||
'status': True,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user