Browse Source

自动续费:查询订阅,取消订阅接口
去除优惠券冻结状态

lang 3 years ago
parent
commit
515656f3b1
4 changed files with 90 additions and 6 deletions
  1. 2 0
      Ansjer/urls.py
  2. 4 4
      Controller/CloudStorage.py
  3. 80 2
      Controller/PaymentCycle.py
  4. 4 0
      Object/ResponseObject.py

+ 2 - 0
Ansjer/urls.py

@@ -239,6 +239,8 @@ urlpatterns = [
 	url(r'^cloudstorage/(?P<operation>.*)$', CloudStorage.CloudStorageView.as_view()),
     url(r'^payCycle/(?P<operation>.*)$', PaymentCycle.PaypalCycleNotify.as_view()), #周期扣款
 	url(r'^paypalCycleNotify/(?P<operation>.*)$', PaymentCycle.PaypalCycleNotify.as_view()), #paypal周期扣款订阅通知
+	url(r'^paymentCycle/(?P<operation>.*)$', PaymentCycle.payCycle.as_view()), #paypal周期扣款
+
 
     #新增解密的接口
     url(r'^v3/account/changePwd$', UserController.v3ChangePwdView.as_view()),

+ 4 - 4
Controller/CloudStorage.py

@@ -1211,10 +1211,10 @@ class CloudStorageView(View):
                                            pay_url=subInfo['url'], isSelectDiscounts=is_select_discount,
                                            commodity_code=commodity_code, commodity_type=commodity_type,
                                            rank_id=rank, plan_id=subInfo['plan_id'],coupon_id=coupon_id)
-                if coupon_id:
-                    #冻结优惠券
-                    CouponModel.objects.filter(id=coupon_id, use_status=0, distributeTime__lte=nowTime,
-                                                           valid_time__gt=nowTime).update(use_status=1)
+                # if coupon_id:
+                #     #冻结优惠券
+                #     CouponModel.objects.filter(id=coupon_id, use_status=0, distributeTime__lte=nowTime,
+                #                                            valid_time__gt=nowTime).update(use_status=1)
 
                 return response.json(0, {"redirectUrl": subInfo['url'], "orderID": orderID})
             #正常扣款

+ 80 - 2
Controller/PaymentCycle.py

@@ -1,10 +1,12 @@
 from Ansjer.config import PAYPAL_CRD,SERVER_DOMAIN,SERVER_DOMAIN_SSL,PAYPAL_WEB_HOOK_ID
-from Model.models import PayCycleConfigModel,Order_Model, Store_Meal, UID_Bucket, PromotionRuleModel, Unused_Uid_Meal,Device_Info, CouponModel
+from Model.models import PayCycleConfigModel,Order_Model, Store_Meal, UID_Bucket, PromotionRuleModel, Unused_Uid_Meal,Device_Info, CouponModel, Order_Model
 from Service.CommonService import CommonService
 from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
 import requests
 import time
 import sys
+from Object.TokenObject import TokenObject
+from Object.UidTokenObject import UidTokenObject
 from Object.ResponseObject import ResponseObject
 import paypalrestsdk
 from paypalrestsdk import BillingAgreement
@@ -31,7 +33,6 @@ class Paypal:
             return False
         return True
 
-
     def subscriptions(store_info,lang,orderID,price):
         cycle_config = PayCycleConfigModel.objects.filter(id=store_info['cycle_config_id']).values()
         if not cycle_config:
@@ -564,6 +565,83 @@ class PaypalCycleNotify(View):
         exit()
 
 
+class payCycle(View):
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        return self.validation(request.GET, request, operation)
+
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        return self.validation(request.POST, request, operation)
+
+    def validation(self, request_dict, request, operation):
+        response = ResponseObject()
+        token = request_dict.get('token', None)
+        # 设备主键uid
+        tko = TokenObject(token)
+        response.lang = tko.lang
+        if tko.code != 0:
+            return response.json(tko.code)
+        userID = tko.userID
+        if operation is None:
+            return response.json(444, 'error path')
+        elif operation == 'queryPayCycle':  # paypal成功订阅回调
+            return self.do_query_pay_cycle(request_dict,userID, response)
+        elif operation == 'cancelPayCycle':  # 取消自动续费
+            return self.do_cancel_pay_cycle(request_dict,userID, response)
+    def do_query_pay_cycle(self, request_dict, userID, response):
+        lang = request_dict.get('lang', 'en')
+        orderObject = Order_Model.objects.filter(userID=userID,status=1,rank__lang__lang=lang).annotate(rank__title=F('rank__lang__title'), rank__content=F('rank__lang__content'))
+        orderObject = orderObject.filter(~Q(agreement_id = ''))
+        if not orderObject.exists():
+            return response.json(0, {'data':[], 'count': 0})
+        orderQuery = orderObject.values("orderID", "UID", "channel", "desc", "price", "currency",
+                "addTime",
+                "updTime", "paypal", "rank__day", "payType",
+                "rank__price", "status",
+                "rank__lang__content", "rank__title", "rank__currency",
+                "rank_id", "rank__expire","agreement_id").order_by('addTime')
+        new_data = []
+        values = []
+        for d in orderQuery:
+            if d['agreement_id'] not in values:
+                new_data.append(d)
+                values.append(d['agreement_id'])
+        count = len(new_data)
+        return response.json(0, {'data': new_data, 'count': count})
+
+    def do_cancel_pay_cycle(self, request_dict, userID, response):
+        orderID = request_dict.get('orderID', 'None')
+        orderObject = Order_Model.objects.filter(orderID=orderID)
+        orderObject = orderObject.filter(~Q(agreement_id = '')).values("agreement_id")
+        if not orderObject.exists():
+            return response.json(800)
+
+        paypalrestsdk.configure(PAYPAL_CRD)
+        BILLING_AGREEMENT_ID = orderObject[0]['agreement_id']
+        try:
+            billing_agreement = paypalrestsdk.BillingAgreement.find(BILLING_AGREEMENT_ID)
+            if billing_agreement.state != 'Active':
+                return response.json(10051)
+            cancel_note = {"note": "Canceling the agreement"}
+            if billing_agreement.cancel(cancel_note):
+                Order_Model.objects.filter(agreement_id=BILLING_AGREEMENT_ID).update(agreement_id='')
+                return response.json(10051)
+            else:
+                return response.json(10052)
+        except Exception as e:
+            return response.json(10052)
+
+
+
+
+
+
+
+
+
 
 
 

+ 4 - 0
Object/ResponseObject.py

@@ -107,6 +107,8 @@ class ResponseObject(object):
             10048: 'Subscribe to the failure',
             10049: 'The coupon is not available',
             10050: 'You have subscribed to this package, please do not subscribe again',
+            10051: 'Unsubscribe successfully',
+            10052: 'Unsubscribe failed',
         }
         data_cn = {
             0: '成功',
@@ -207,6 +209,8 @@ class ResponseObject(object):
             10048: '订阅失败',
             10049: '该优惠券不可用',
             10050: '您已订阅过此套餐,请不要重复订阅',
+            10051: '已取消订阅',
+            10052: '订阅取消失败',
         }
         if self.lang == 'cn':
             msg = data_cn