zhangdongming 2 жил өмнө
parent
commit
c2aace990a
1 өөрчлөгдсөн 16 нэмэгдсэн , 11 устгасан
  1. 16 11
      Controller/PaymentCycle.py

+ 16 - 11
Controller/PaymentCycle.py

@@ -21,6 +21,7 @@ import json
 from paypalrestsdk import BillingPlan
 import datetime as date_time
 
+
 # 周期扣款相关
 class Paypal:
     # 检查是否有重复订阅
@@ -305,6 +306,11 @@ class PaypalCycleNotify(View):
                 red_url = "{SERVER_DOMAIN_SSL}web/paid2/en_fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
             return HttpResponseRedirect(red_url)
 
+    @staticmethod
+    def paypal_webhook_log(sub_id, **params):
+        params['agreement_id'] = sub_id
+        PaypalWebHookEvent.objects.create(**params)
+
     def do_paypal_webhook_notify(self, request_dict, request, response):
         logger = logging.getLogger('pay')
         logger.info('--------进入周期扣款钩子--------')
@@ -328,10 +334,19 @@ class PaypalCycleNotify(View):
             billing_agreement_id = paypal_body.get('billing_agreement_id')
             paypal_transaction_id = paypal_body.get('id')
             amount = paypal_body.get('amount')
+            PaypalWebHookEventInsert = {
+                'webhook_event_id': json_obj.get('id'),
+                'resource_type': json_obj.get('resource_type'),
+                'event_type': 1,
+                'summary': summary,
+                'trade_no': paypal_transaction_id,
+                'resource': json_agreement_str,
+                'created_time': int(time.time()),
+            }
+            self.paypal_webhook_log(billing_agreement_id, **PaypalWebHookEventInsert)
             if event_type != 'PAYMENT.SALE.COMPLETED':
                 logger.info('----钩子异常----')
             self.find_subscription_transactions(billing_agreement_id)
-            # self.get_plan_desc('P-4CG284532S612303METMEINY')
             if resource_type == 'sale' and paypal_body.get('state') == 'completed':
                 paypalrestsdk.configure(PAYPAL_CRD)
                 response = paypalrestsdk.WebhookEvent.verify(
@@ -348,15 +363,6 @@ class PaypalCycleNotify(View):
                 logger.info(paypal_body.get('state'))
                 return HttpResponse('Fail', status=500)
 
-            PaypalWebHookEventInsert = {
-                'webhook_event_id': json_obj.get('id'),
-                'resource_type': json_obj.get('resource_type'),
-                'event_type': 1,
-                'summary': summary,
-                'trade_no': paypal_transaction_id,
-                'resource': json_agreement_str,
-                'created_time': int(time.time()),
-            }
             if not billing_agreement_id:
                 # 记录钩子日志
                 PaypalWebHookEvent.objects.create(**PaypalWebHookEventInsert)
@@ -552,7 +558,6 @@ class PaypalCycleNotify(View):
         except Exception as e:
             logger.info('出错了~查询订阅的事务异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
-
     def do_subscription_break_notify(self, request_dict, request, response):
         logger = logging.getLogger('pay')
         logger.info('--------进入订阅失败,付款失败,暂停--------')