|
@@ -1948,24 +1948,36 @@ class CloudStorageView(View):
|
|
|
|
|
|
def do_refund(self, request_dict, response):
|
|
def do_refund(self, request_dict, response):
|
|
orderID = request_dict.get('orderID', None) # 商户订单号
|
|
orderID = request_dict.get('orderID', None) # 商户订单号
|
|
- if not orderID:
|
|
|
|
|
|
+ refund_amount = request_dict.get('refund_amount', None) # 退款金额
|
|
|
|
+ if not all([orderID, refund_amount]):
|
|
return response.json(444)
|
|
return response.json(444)
|
|
try:
|
|
try:
|
|
order_qs = Order_Model.objects.filter(orderID=orderID).\
|
|
order_qs = Order_Model.objects.filter(orderID=orderID).\
|
|
- values('status', 'payType', 'price', 'currency', 'paymentID')
|
|
|
|
|
|
+ values('status', 'payType', 'price', 'refunded_amount', 'currency', 'paymentID')
|
|
if not order_qs.exists():
|
|
if not order_qs.exists():
|
|
return response.json(173)
|
|
return response.json(173)
|
|
|
|
|
|
- # 支付状态不为支付成功和退款失败
|
|
|
|
|
|
+ # 支付状态不为支付成功和退款失败和部分退款
|
|
status = order_qs[0]['status']
|
|
status = order_qs[0]['status']
|
|
- if status != 1 and status != 4:
|
|
|
|
|
|
+ if status != 1 and status != 4 and status != 6:
|
|
return response.json(805)
|
|
return response.json(805)
|
|
|
|
|
|
|
|
+ refund_amount = float(refund_amount)
|
|
now_time = int(time.time())
|
|
now_time = int(time.time())
|
|
payType = order_qs[0]['payType']
|
|
payType = order_qs[0]['payType']
|
|
- refund_amount = order_qs[0]['price'] # 退款金额
|
|
|
|
- currency = order_qs[0]['currency'] # 货币
|
|
|
|
- paymentID = order_qs[0]['paymentID'] # 退款id
|
|
|
|
|
|
+ price = order_qs[0]['price'] # 订单金额
|
|
|
|
+ refunded_amount = order_qs[0]['refunded_amount'] # 已退款金额
|
|
|
|
+ currency = order_qs[0]['currency'] # 货币
|
|
|
|
+ paymentID = order_qs[0]['paymentID'] # 退款id
|
|
|
|
+
|
|
|
|
+ # 判断是否符合付款条件
|
|
|
|
+ if refunded_amount == float(price): # 已全额退款
|
|
|
|
+ return response.json(805)
|
|
|
|
+ if float(refund_amount) + refunded_amount >= float(price): # 退款金额超出订单价格
|
|
|
|
+ return response.json(805)
|
|
|
|
+ # 部分退款标识
|
|
|
|
+ is_partial_refund = True if(float(price) > float(refund_amount) + refunded_amount) else False
|
|
|
|
+
|
|
out_request_no = str(time.strftime('%Y%m%d%H%M%S', time.localtime(now_time))) # 退款请求号
|
|
out_request_no = str(time.strftime('%Y%m%d%H%M%S', time.localtime(now_time))) # 退款请求号
|
|
# 根据支付类型处理退款
|
|
# 根据支付类型处理退款
|
|
if payType == 1: # PayPal
|
|
if payType == 1: # PayPal
|
|
@@ -1986,23 +1998,30 @@ class CloudStorageView(View):
|
|
'currency': currency
|
|
'currency': currency
|
|
}
|
|
}
|
|
})
|
|
})
|
|
- status = 5 if refund.success() else 4
|
|
|
|
|
|
+ refund_success = True if refund.success() else False
|
|
elif payType == 2: # 支付宝
|
|
elif payType == 2: # 支付宝
|
|
aliPayObj = AliPayObject()
|
|
aliPayObj = AliPayObject()
|
|
alipay = aliPayObj.conf()
|
|
alipay = aliPayObj.conf()
|
|
refund_response = alipay.api_alipay_trade_refund(refund_amount=refund_amount, out_trade_no=orderID,
|
|
refund_response = alipay.api_alipay_trade_refund(refund_amount=refund_amount, out_trade_no=orderID,
|
|
out_request_no=out_request_no)
|
|
out_request_no=out_request_no)
|
|
- # 退款成功,修改订单支付状态为'退款成功',否则改为'退款失败'
|
|
|
|
- status = 5 if refund_response['code'] == '10000' else 4
|
|
|
|
|
|
+ refund_success = True if(refund_response['code'] == '10000') else False
|
|
elif payType == 3: # 微信
|
|
elif payType == 3: # 微信
|
|
wechatPayObj = WechatPayObject()
|
|
wechatPayObj = WechatPayObject()
|
|
refund_amount = int(float(refund_amount) * 100) # 退款金额,单位为分,只能为整数
|
|
refund_amount = int(float(refund_amount) * 100) # 退款金额,单位为分,只能为整数
|
|
- refund_success = wechatPayObj.refund(out_trade_no=orderID, out_refund_no=out_request_no,
|
|
|
|
- total_fee=refund_amount, refund_fee=refund_amount)
|
|
|
|
- status = 5 if refund_success else 4
|
|
|
|
|
|
+ success = wechatPayObj.refund(out_trade_no=orderID, out_refund_no=out_request_no,
|
|
|
|
+ total_fee=refund_amount, refund_fee=refund_amount)
|
|
|
|
+ refund_success = True if success else False
|
|
else: # 不支持退款的支付类型
|
|
else: # 不支持退款的支付类型
|
|
return response.json(805)
|
|
return response.json(805)
|
|
- order_qs.update(status=status, updTime=now_time) # 更新订单状态
|
|
|
|
|
|
+
|
|
|
|
+ # 更新订单状态和已退款金额
|
|
|
|
+ update_dict = {'updTime': now_time}
|
|
|
|
+ if refund_success:
|
|
|
|
+ update_dict['status'] = 6 if is_partial_refund else 5
|
|
|
|
+ update_dict['refunded_amount'] = float(refund_amount)
|
|
|
|
+ else:
|
|
|
|
+ update_dict['status'] = 4
|
|
|
|
+ order_qs.update(**update_dict)
|
|
return response.json(0)
|
|
return response.json(0)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(e)
|
|
print(e)
|