Browse Source

paypal订阅,首次扣款判断订阅事务状态

zhangdongming 3 years ago
parent
commit
c75f7d6a2a
1 changed files with 27 additions and 0 deletions
  1. 27 0
      Controller/PaymentCycle.py

+ 27 - 0
Controller/PaymentCycle.py

@@ -5,6 +5,7 @@ from Service.CommonService import CommonService
 from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
 import requests
 import time
+import datetime as date_time
 import sys
 from Object.TokenObject import TokenObject
 from Object.UidTokenObject import UidTokenObject
@@ -177,6 +178,32 @@ class PaypalCycleNotify(View):
         logger.info('----订阅详情----')
         logger.info(billing_agreement_response)
         agreement_id = billing_agreement_response.id
+        # 列出订阅的事务 https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions
+        try:
+            billing_agreement = paypalrestsdk.BillingAgreement.find(agreement_id)
+            today = date_time.date.today()
+            oneday = date_time.timedelta(days=1)
+            yesterday = today - oneday
+            start_date = yesterday.strftime('%Y-%m-%d')
+            end_date = today.strftime('%Y-%m-%d')
+            transactions = billing_agreement.search_transactions(start_date, end_date)
+            if transactions.agreement_transaction_list:
+                agreement_transaction = transactions.agreement_transaction_list[0]
+                logger.info('-->订阅首次扣款,最新一条订阅事务{}'.format(agreement_transaction))
+                if agreement_transaction.status != 'Completed':
+                    logger.info('-->首次订阅扣款状态:{}'.format(agreement_transaction.status))
+                    red_url = "{SERVER_DOMAIN_SSL}web/paid2/fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
+                    if lang != 'cn':
+                        red_url = "{SERVER_DOMAIN_SSL}web/paid2/en_fail.html".format(
+                            SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
+                    return HttpResponseRedirect(red_url)
+        except Exception as e:
+            logger.info('出错了~查询订阅的事务异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            red_url = "{SERVER_DOMAIN_SSL}web/paid2/fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
+            if lang != 'cn':
+                red_url = "{SERVER_DOMAIN_SSL}web/paid2/en_fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
+            return HttpResponseRedirect(red_url)
+
         order_qs = Order_Model.objects.filter(orderID=orderID, status=0)
         order_list = order_qs.values("UID", "channel", "commodity_code", "rank", "isSelectDiscounts",
                                      "userID__userID",