Răsfoiți Sursa

内购退款代码优化

linhaohong 8 luni în urmă
părinte
comite
5ba7b69e7a
1 a modificat fișierele cu 19 adăugiri și 12 ștergeri
  1. 19 12
      Controller/InAppPurchaseController.py

+ 19 - 12
Controller/InAppPurchaseController.py

@@ -145,7 +145,8 @@ class InAppPurchaseView(View):
                 logger.info(f"苹果内购认证交易订单orderID:{order_id}, 没有transaction_id")
                 pay_result_url = CommonService.get_payment_status_url(lang, 'fail')
                 return response.json(0, {'url': pay_result_url})
-            logger.info(f"苹果内购认证交易订单orderID:{order_id}, transaction_id:{transaction_id}, 时间戳: {int(time.time())}")
+            logger.info(
+                f"苹果内购认证交易订单orderID:{order_id}, transaction_id:{transaction_id}, 时间戳: {int(time.time())}")
 
             OrderPayLog.objects.create(order_id=order_id, order_no=transaction_id,
                                        business_name=f"内购验单",
@@ -234,7 +235,6 @@ class InAppPurchaseView(View):
                 redis_obj.del_data(key_coupon)
                 CouponModel.objects.filter(id=c_id).update(use_status=2, update_time=now_time)
 
-
             # 修改订阅状态
             if payload.rawType == "Auto-Renewable Subscription":
                 original_transaction_id = payload.originalTransactionId
@@ -274,7 +274,8 @@ class InAppPurchaseView(View):
             logger.info('苹果内购认证交易接口异常:{}'.
                         format('error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))))
             OrderPayLog.objects.create(order_id=order_id, business_name=f"内购验单异常",
-                                       created_time=int(time.time()), updated_time=int(time.time()), access_result="ERROR")
+                                       created_time=int(time.time()), updated_time=int(time.time()),
+                                       access_result="ERROR")
             pay_result_url = CommonService.get_payment_status_url(lang, 'fail')
             return response.json(0, {'url': pay_result_url})
 
@@ -561,7 +562,8 @@ class InAppPurchaseView(View):
                     # 查旧订单消息
                     ord_order_qs = Order_Model.objects.filter(original_transaction_id=original_transaction_id)
                     if not ord_order_qs.exists():
-                        logger.info(f"App Store服务器通知未查询到旧订单信息, originalTransactionId:{original_transaction_id}, 返回状态 400")
+                        logger.info(
+                            f"App Store服务器通知未查询到旧订单信息, originalTransactionId:{original_transaction_id}, 返回状态 400")
                         return HttpResponse(status=400)
 
                     # 解决云存充值成功, 由于一些原因返回500 导致苹果未扣款的问题
@@ -674,14 +676,18 @@ class InAppPurchaseView(View):
                 uid = orders_qs[0].UID
                 now_time = int(time.time())
                 put_time = now_time + 11.5 * 60 * 60
-                in_app_refund_qs = InAppRefund.objects.filter(transaction_id=transaction_id)
+                in_app_refund_qs = InAppRefund.objects.filter(transaction_id=transaction_id).values("refund_progress")
                 if in_app_refund_qs.exists():
-                    in_app_refund_qs.update(refund_progress=0, updated_time=now_time,
-                                            put_time=put_time, app_account_token=app_account_token)
-                InAppRefund.objects.create(transaction_id=transaction_id, orderID=orderID,
-                                           uid=uid, app_type=app_type, created_time=now_time,
-                                           updated_time=now_time, put_time=put_time,
-                                           app_account_token=app_account_token)
+                    if in_app_refund_qs[0]["refund_progress"] == 4:
+                        in_app_refund_qs.update(refund_progress=0, updated_time=now_time,
+                                                put_time=put_time, app_account_token=app_account_token)
+                    else:
+                        in_app_refund_qs.update(updated_time=now_time, app_account_token=app_account_token)
+                else:
+                    InAppRefund.objects.create(transaction_id=transaction_id, orderID=orderID,
+                                               uid=uid, app_type=app_type, created_time=now_time,
+                                               updated_time=now_time, put_time=put_time,
+                                               app_account_token=app_account_token)
                 return HttpResponse(status=200)
 
             elif str(decoded_payload.rawNotificationType) == "DID_CHANGE_RENEWAL_STATUS":
@@ -762,7 +768,8 @@ class InAppPurchaseView(View):
                     return HttpResponse(status=400)
                 logger.info(
                     'App Store服务器通知, 不批准退款, transaction_id:{}'.format(transaction_id))
-                InAppRefund.objects.filter(transaction_id=transaction_id).update(refund_progress=3)
+                InAppRefund.objects.filter(transaction_id=transaction_id).update(refund_progress=3,
+                                                                                 updated_time=int(time.time()))
 
             else:
                 logger.info(f"App Store服务器通知decoded_payload.rawNotificationType 未处理")