|
@@ -1948,35 +1948,53 @@ class CloudStorageView(View):
|
|
|
|
|
|
def do_refund(self, request_dict, response):
|
|
|
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)
|
|
|
try:
|
|
|
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():
|
|
|
return response.json(173)
|
|
|
|
|
|
- # 支付状态不为支付成功和退款失败
|
|
|
+ # 支付状态不为支付成功和退款失败和部分退款
|
|
|
status = order_qs[0]['status']
|
|
|
- if status != 1 and status != 4:
|
|
|
+ if status != 1 and status != 4 and status != 6:
|
|
|
return response.json(805)
|
|
|
|
|
|
+ refund_amount = float(refund_amount)
|
|
|
now_time = int(time.time())
|
|
|
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'] # 货币
|
|
|
+
|
|
|
+ # 判断是否符合付款条件
|
|
|
+ if refunded_amount == float(price): # 已全额退款
|
|
|
+ return response.json(805)
|
|
|
+ if refund_amount + refunded_amount > float(price): # 退款金额超出订单价格
|
|
|
+ return response.json(805)
|
|
|
+ # 部分退款标识
|
|
|
+ is_partial_refund = True if(float(price) > refund_amount + refunded_amount) else False
|
|
|
+
|
|
|
out_request_no = str(time.strftime('%Y%m%d%H%M%S', time.localtime(now_time))) # 退款请求号
|
|
|
# 根据支付类型处理退款
|
|
|
if payType == 1: # PayPal
|
|
|
+ paymentID = order_qs[0]['paymentID'] # PayPal PAYMENT_ID
|
|
|
+ if not paymentID:
|
|
|
+ return response.json(805)
|
|
|
paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
payment = paypalrestsdk.Payment.find(paymentID)
|
|
|
- print(payment)
|
|
|
+ print('payment', payment)
|
|
|
related_resources = payment['transactions'][0]['related_resources']
|
|
|
+ print('related_resources: ', related_resources)
|
|
|
if not related_resources:
|
|
|
return response.json(805)
|
|
|
sale = related_resources[0]['sale']
|
|
|
- if sale['state'] != 'completed':
|
|
|
+ print('sale: ', sale)
|
|
|
+ # 没退款过 'state' 不为 'completed' 或 已退款过但 'state' 不为 'partially_refunded'
|
|
|
+ if (refunded_amount == 0 and sale['state'] != 'completed') or \
|
|
|
+ (refunded_amount != 0 and sale['state'] != 'partially_refunded'):
|
|
|
return response.json(805)
|
|
|
sale_id = sale['id']
|
|
|
paypalSale = paypalrestsdk.Sale.find(sale_id)
|
|
@@ -1986,23 +2004,30 @@ class CloudStorageView(View):
|
|
|
'currency': currency
|
|
|
}
|
|
|
})
|
|
|
- status = 5 if refund.success() else 4
|
|
|
+ refund_success = True if refund.success() else False
|
|
|
elif payType == 2: # 支付宝
|
|
|
aliPayObj = AliPayObject()
|
|
|
alipay = aliPayObj.conf()
|
|
|
refund_response = alipay.api_alipay_trade_refund(refund_amount=refund_amount, out_trade_no=orderID,
|
|
|
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: # 微信
|
|
|
wechatPayObj = WechatPayObject()
|
|
|
- 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
|
|
|
+ refund_fee = int(refund_amount * 100) # 退款金额,单位为分,只能为整数
|
|
|
+ success = wechatPayObj.refund(out_trade_no=orderID, out_refund_no=out_request_no,
|
|
|
+ total_fee=refund_fee, refund_fee=refund_fee)
|
|
|
+ refund_success = True if success else False
|
|
|
else: # 不支持退款的支付类型
|
|
|
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'] = refunded_amount + refund_amount
|
|
|
+ else:
|
|
|
+ update_dict['status'] = 4
|
|
|
+ order_qs.update(**update_dict)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
print(e)
|