|
@@ -90,6 +90,10 @@ class AiView(View):
|
|
|
return self.do_ai_identification(request.POST, response)
|
|
|
elif operation == 'doPayPalCallBack': # paypal支付回调
|
|
|
return self.do_pay_by_paypal_callback(request_dict, response)
|
|
|
+ elif operation == 'doAlipayCallBack': # 支付宝支付回调
|
|
|
+ return self.do_alipay_callback(request_dict, response)
|
|
|
+ elif operation == 'doWechatCallBack': # 微信支付回调
|
|
|
+ return self.do_wechat_callback(request, response)
|
|
|
else:
|
|
|
token = request_dict.get('token', None)
|
|
|
# 设备主键uid
|
|
@@ -104,7 +108,7 @@ class AiView(View):
|
|
|
return self.do_change_ai_status(userID, request_dict, response)
|
|
|
elif operation == 'getAiStatus': # 获取AI开关状态
|
|
|
return self.getAiStatus(userID, request_dict, response)
|
|
|
- elif operation == 'commoditylist': # 修改云存状态,传送两个url,即getsignsts接口和storeplaylist接口
|
|
|
+ elif operation == 'commoditylist': # 获取AI套餐列表
|
|
|
return self.do_commodity_list(userID, request_dict, response)
|
|
|
elif operation == 'identification': # ai识别
|
|
|
return self.do_ai_identification(request_dict, response)
|
|
@@ -331,34 +335,34 @@ class AiView(View):
|
|
|
except Exception as e:
|
|
|
return False
|
|
|
|
|
|
- def do_commodity_list(self, userID, request_dict, response): # 查询套餐列表
|
|
|
+ def do_commodity_list(self, userID, request_dict, response):
|
|
|
uid = request_dict.get('uid', None)
|
|
|
lang = request_dict.get('lang', 'en')
|
|
|
nowTime = int(time.time())
|
|
|
- # DVR/NVR设备暂不返回云存套餐列表
|
|
|
+ # DVR/NVR设备暂不返回套餐列表
|
|
|
device_info_qs = Device_Info.objects.filter(Q(UID=uid), Q(Type__lte=4) | Q(Type=10001))
|
|
|
if device_info_qs.exists():
|
|
|
return response.json(0)
|
|
|
|
|
|
qs = AiStoreMeal.objects
|
|
|
|
|
|
- qs = qs.filter(is_show=1) #过滤隐藏套餐
|
|
|
+ qs = qs.filter(is_show=1) # 过滤隐藏套餐
|
|
|
qs = qs.annotate(ai_meal_id=F('id'))
|
|
|
qs = qs.values("ai_meal_id", "title", "content", "price", "effective_day", "currency",
|
|
|
- "virtual_price", "symbol", "pay_type")
|
|
|
-
|
|
|
- if qs.exists():
|
|
|
- res = list(qs)
|
|
|
- for key, val in enumerate(res):
|
|
|
- pay_types = Pay_Type.objects.filter(aistoremeal=res[key]['ai_meal_id']).values("id", "payment")
|
|
|
- res[key]['pay_type'] = list(pay_types)
|
|
|
- result = {
|
|
|
- 'meals': res,
|
|
|
- }
|
|
|
- return response.json(0, result)
|
|
|
- else:
|
|
|
+ "virtual_price", "symbol")
|
|
|
+ if not qs.exists():
|
|
|
return response.json(0)
|
|
|
|
|
|
+ # 查询每种套餐的所有支付方式
|
|
|
+ ai_meal_list = list(qs)
|
|
|
+ for ai_meal in ai_meal_list:
|
|
|
+ pay_type_qs = Pay_Type.objects.filter(aistoremeal=ai_meal['ai_meal_id']).values('id', 'payment')
|
|
|
+ ai_meal['pay_type'] = list(pay_type_qs)
|
|
|
+ result = {
|
|
|
+ 'meals': ai_meal_list,
|
|
|
+ }
|
|
|
+ return response.json(0, result)
|
|
|
+
|
|
|
def do_querylist(self, userID, request_dict, response):
|
|
|
page = request_dict.get('page', None)
|
|
|
line = request_dict.get('line', None)
|
|
@@ -562,6 +566,7 @@ class AiView(View):
|
|
|
redisObj.CONN.expire(orderID + 'do_notify', 60)
|
|
|
if not isLock:
|
|
|
return response.json(5)
|
|
|
+
|
|
|
try:
|
|
|
order_qs = Order_Model.objects.filter(orderID=orderID, status=0)
|
|
|
if not order_qs.exists():
|
|
@@ -577,42 +582,7 @@ class AiView(View):
|
|
|
pay_failed_url = CommonService.get_payment_status_url(lang, 'fail')
|
|
|
redisObj.del_data(key=orderID + 'do_notify')
|
|
|
return HttpResponseRedirect(pay_failed_url)
|
|
|
-
|
|
|
- nowTime = int(time.time())
|
|
|
- order_list = order_qs.values("UID", "channel", "commodity_code", "ai_rank__effective_day", "isSelectDiscounts",
|
|
|
- "userID__userID", "userID__username", "coupon_id")
|
|
|
- userid = order_list[0]['userID__userID']
|
|
|
- username = order_list[0]['userID__username']
|
|
|
- UID = order_list[0]['UID']
|
|
|
- channel = order_list[0]['channel']
|
|
|
- effective_day = order_list[0]['ai_rank__effective_day']
|
|
|
-
|
|
|
- ai_service_qs = AiService.objects.filter(Q(uid=UID), Q(channel=channel), Q(use_status=1))
|
|
|
- ai_service_dict = {'orders_id': orderID,
|
|
|
- 'uid': UID,
|
|
|
- 'channel': channel,
|
|
|
- 'detect_status': 1,
|
|
|
- 'addTime': nowTime,
|
|
|
- 'updTime': nowTime,
|
|
|
- 'detect_group': '1'
|
|
|
- }
|
|
|
- if ai_service_qs.exists(): # 有正在使用的套餐,套餐结束时间保存为套餐有效期
|
|
|
- ai_service_dict['endTime'] = effective_day * 24 * 60 * 60
|
|
|
- else:
|
|
|
- ai_service_dict['use_status'] = 1
|
|
|
- ai_service_dict['endTime'] = nowTime + effective_day * 24 * 60 * 60
|
|
|
- with transaction.atomic():
|
|
|
- # 更新设备主用户
|
|
|
- Device_Info.objects.filter(UID=UID, vodPrimaryUserID='', vodPrimaryMaster='').\
|
|
|
- update(vodPrimaryUserID=userid, vodPrimaryMaster=username)
|
|
|
- # 更新订单数据,返回支付成功url
|
|
|
- order_qs.update(status=1, updTime=nowTime)
|
|
|
- # 创建AiService数据
|
|
|
- AiService.objects.create(**ai_service_dict)
|
|
|
-
|
|
|
- pay_success_url = CommonService.get_payment_status_url(lang, 'success')
|
|
|
- redisObj.del_data(key=orderID + 'do_notify')
|
|
|
- return HttpResponseRedirect(pay_success_url)
|
|
|
+ return self.payment_success(orderID, lang, order_qs, redisObj)
|
|
|
except Exception as e:
|
|
|
logger.info('AI订单paypal支付回调异常:{}'.format(repr(e)))
|
|
|
order_qs.update(status=10)
|
|
@@ -620,6 +590,121 @@ class AiView(View):
|
|
|
redisObj.del_data(key=orderID + 'do_notify')
|
|
|
return HttpResponseRedirect(pay_failed_url)
|
|
|
|
|
|
+ def do_alipay_callback(self, request, response): # 支付宝支付回调
|
|
|
+ logger = logging.getLogger('info')
|
|
|
+ logger.info('AI订单---支付宝支付回调')
|
|
|
+ try:
|
|
|
+ data = request.POST.dict()
|
|
|
+ passback_params = data['passback_params']
|
|
|
+ parmap = dict([(k, v[0]) for k, v in parse_qs(unquote(passback_params)).items()])
|
|
|
+ lang = parmap['lang']
|
|
|
+ signature = data['sign']
|
|
|
+ data.pop('sign')
|
|
|
+ orderID = data['out_trade_no']
|
|
|
+
|
|
|
+ # redis加锁,防止订单重复
|
|
|
+ redisObj = RedisObject()
|
|
|
+ isLock = redisObj.CONN.setnx(orderID + 'do_notify', 1)
|
|
|
+ redisObj.CONN.expire(orderID + 'do_notify', 60)
|
|
|
+ if not isLock:
|
|
|
+ return response.json(5)
|
|
|
+
|
|
|
+ order_qs = Order_Model.objects.filter(orderID=orderID, status=0)
|
|
|
+ if not order_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+
|
|
|
+ aliPayObj = AliPayObject()
|
|
|
+ alipay = aliPayObj.conf()
|
|
|
+ success = alipay.verify(data, signature)
|
|
|
+ if not success or data["trade_status"] not in ("TRADE_SUCCESS", "TRADE_FINISHED"):
|
|
|
+ return response.json(0, signature)
|
|
|
+ return self.payment_success(orderID, lang, order_qs, redisObj)
|
|
|
+ except Exception as e:
|
|
|
+ logger.info('AI订单支付宝支付回调异常:{}'.format(repr(e)))
|
|
|
+ order_qs.update(status=10)
|
|
|
+ redisObj.del_data(key=orderID + 'do_notify')
|
|
|
+ pay_failed_url = CommonService.get_payment_status_url(lang, 'fail')
|
|
|
+ redisObj.del_data(key=orderID + 'do_notify')
|
|
|
+ return HttpResponseRedirect(pay_failed_url)
|
|
|
+
|
|
|
+ def do_wechat_callback(self, request, response): # 微信支付回调
|
|
|
+ logger = logging.getLogger('info')
|
|
|
+ logger.info('AI订单---微信支付回调')
|
|
|
+ try:
|
|
|
+ pay = WechatPayObject()
|
|
|
+ data = pay.weixinpay_call_back(request.body)
|
|
|
+ attach = data["attach"]
|
|
|
+ parmap = dict([(k, v[0]) for k, v in parse_qs(unquote(attach)).items()])
|
|
|
+ lang = parmap['lang']
|
|
|
+ trade_status = data['result_code'] # 业务结果 SUCCESS/FAIL
|
|
|
+ orderID = data['out_trade_no'] # 商户订单号
|
|
|
+ order_qs = Order_Model.objects.filter(orderID=orderID, status=0)
|
|
|
+ if not order_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+
|
|
|
+ if trade_status != 'SUCCESS':
|
|
|
+ order_qs.update(status=10)
|
|
|
+ return HttpResponse(pay.xml_to_dict({'return_code': 'FAIL'}))
|
|
|
+ check_sign = pay.get_notifypay(data)
|
|
|
+ if not check_sign:
|
|
|
+ return HttpResponse(pay.xml_to_dict({'return_code': 'FAIL', 'return_msg': '签名失败'}))
|
|
|
+
|
|
|
+ # redis加锁,防止订单重复
|
|
|
+ redisObj = RedisObject()
|
|
|
+ isLock = redisObj.CONN.setnx(orderID + 'do_notify', 1)
|
|
|
+ redisObj.CONN.expire(orderID + 'do_notify', 60)
|
|
|
+ if not isLock:
|
|
|
+ return response.json(5)
|
|
|
+
|
|
|
+ return self.payment_success(self, orderID, lang, order_qs, redisObj, True)
|
|
|
+ except Exception as e:
|
|
|
+ order_qs.update(status=10)
|
|
|
+ redisObj.del_data(key=orderID + 'do_notify')
|
|
|
+ return HttpResponse(pay.xml_to_dict({'return_code': 'FAIL', 'return_msg': repr(e)}))
|
|
|
+
|
|
|
+ def payment_success(self, orderID, lang, order_qs, redisObj, is_wechat_pay=False):
|
|
|
+ nowTime = int(time.time())
|
|
|
+ order_list = order_qs.values("UID", "channel", "commodity_code", "ai_rank__effective_day", "isSelectDiscounts",
|
|
|
+ "userID__userID", "userID__username", "coupon_id")
|
|
|
+ userid = order_list[0]['userID__userID']
|
|
|
+ username = order_list[0]['userID__username']
|
|
|
+ UID = order_list[0]['UID']
|
|
|
+ channel = order_list[0]['channel']
|
|
|
+ effective_day = order_list[0]['ai_rank__effective_day']
|
|
|
+
|
|
|
+ ai_service_qs = AiService.objects.filter(Q(uid=UID), Q(channel=channel), Q(use_status=1))
|
|
|
+ ai_service_dict = {'orders_id': orderID,
|
|
|
+ 'uid': UID,
|
|
|
+ 'channel': channel,
|
|
|
+ 'detect_status': 1,
|
|
|
+ 'addTime': nowTime,
|
|
|
+ 'updTime': nowTime,
|
|
|
+ 'detect_group': '1'
|
|
|
+ }
|
|
|
+ if ai_service_qs.exists(): # 有正在使用的套餐,套餐结束时间保存为套餐有效期
|
|
|
+ ai_service_dict['endTime'] = effective_day * 24 * 60 * 60
|
|
|
+ else:
|
|
|
+ ai_service_dict['use_status'] = 1
|
|
|
+ ai_service_dict['endTime'] = nowTime + effective_day * 24 * 60 * 60
|
|
|
+ with transaction.atomic():
|
|
|
+ # 更新设备主用户
|
|
|
+ Device_Info.objects.filter(UID=UID, vodPrimaryUserID='', vodPrimaryMaster=''). \
|
|
|
+ update(vodPrimaryUserID=userid, vodPrimaryMaster=username)
|
|
|
+ # 更新订单数据,返回支付成功url
|
|
|
+ order_qs.update(status=1, updTime=nowTime)
|
|
|
+ # 创建AiService数据
|
|
|
+ AiService.objects.create(**ai_service_dict)
|
|
|
+
|
|
|
+ pay_success_url = CommonService.get_payment_status_url(lang, 'success')
|
|
|
+ redisObj.del_data(key=orderID + 'do_notify')
|
|
|
+ if is_wechat_pay:
|
|
|
+ return HttpResponse("<xml>\
|
|
|
+ <return_code><![CDATA[SUCCESS]]></return_code>\
|
|
|
+ <return_msg><![CDATA[OK]]></return_msg>\
|
|
|
+ </xml>")
|
|
|
+ else:
|
|
|
+ return HttpResponseRedirect(pay_success_url)
|
|
|
+
|
|
|
def update_jpush_time(self,userID, request_dict, response):
|
|
|
logger = logging.getLogger('info')
|
|
|
msg_id = request_dict.get('msg_id', None)
|