lang 3 年之前
父节点
当前提交
5610ee4040
共有 1 个文件被更改,包括 20 次插入25 次删除
  1. 20 25
      Controller/PaymentCycle.py

+ 20 - 25
Controller/PaymentCycle.py

@@ -13,6 +13,8 @@ from Controller import CloudStorage
 from django.db.models import Q, F, Count
 from paypalrestsdk.notifications import WebhookEvent
 import logging
+import json
+
 
 #周期扣款相关
 class Paypal:
@@ -248,35 +250,28 @@ class PaypalCycleNotify(View):
 
 
     def do_paypal_webhook_notify(self, request_dict, request, response):
-        paymentId = request_dict.get('paymentId', None)
-        PayerID = request_dict.get('PayerID', None)
-        lang = request_dict.get('lang', 'en')
-        token = request_dict.get('token',None)
-        import json
         logger = logging.getLogger('info')
         json_str = request.body.decode("utf-8")
         logger.info('json_str----------------')
         logger.info(json_str)
-        return response.json(json_str)
 
-        paypalrestsdk.configure(PAYPAL_CRD)
-
-        # The payload body sent in the webhook event
-        event_body = '{"id":"WH-0G2756385H040842W-5Y612302CV158622M","create_time":"2015-05-18T15:45:13Z","resource_type":"sale","event_type":"PAYMENT.SALE.COMPLETED","summary":"Payment completed for $ 20.0 USD","resource":{"id":"4EU7004268015634R","create_time":"2015-05-18T15:44:02Z","update_time":"2015-05-18T15:44:21Z","amount":{"total":"20.00","currency":"USD"},"payment_mode":"INSTANT_TRANSFER","state":"completed","protection_eligibility":"ELIGIBLE","protection_eligibility_type":"ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE","parent_payment":"PAY-86C81811X5228590KKVNARQQ","transaction_fee":{"value":"0.88","currency":"USD"},"links":[{"href":"https://api.sandbox.paypal.com/v1/payments/sale/4EU7004268015634R","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/payments/sale/4EU7004268015634R/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-86C81811X5228590KKVNARQQ","rel":"parent_payment","method":"GET"}]},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-0G2756385H040842W-5Y612302CV158622M","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-0G2756385H040842W-5Y612302CV158622M/resend","rel":"resend","method":"POST"}]}'
-        # Paypal-Transmission-Id in webhook payload header
+        header = request.META
+        logger.info('json_str----------------')
+        # json_str = '{"id":"WH-9K090101YP819052B-3LD205733F387905X","event_version":"1.0","create_time":"2022-01-05T08:17:36.937Z","resource_type":"plan","event_type":"BILLING.PLAN.CREATED","summary":"A billing plan was created","resource":{"merchant_preferences":{"setup_fee":{"currency":"USD","value":"0.02"},"return_url":"http://127.0.0.1:8000/payCycle/paypalCycleReturn?lang=en","cancel_url":"http://127.0.0.1:8000/web/paid2/en_fail.html","auto_bill_amount":"YES","initial_fail_amount_action":"CANCEL","max_fail_attempts":"1"},"update_time":"2022-01-05T08:17:15.829Z","create_time":"2022-01-05T08:17:15.129Z","name":"One year package","description":"20220105161712830917","links":[{"href":"https://api.sandbox.paypal.com/v1/payments/billing-plans/P-4RE38399B6473962EFFIEW6I","rel":"self","method":"GET"}],"payment_definitions":[{"id":"PD-8DC78035HJ0448121FFIEW6I","name":"Video save for 7 day","type":"REGULAR","frequency":"Day","amount":{"currency":"USD","value":"0.02"},"cycles":"0","frequency_interval":"1"}],"id":"P-4RE38399B6473962EFFIEW6I","state":"ACTIVE","type":"INFINITE"},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-9K090101YP819052B-3LD205733F387905X","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-9K090101YP819052B-3LD205733F387905X/resend","rel":"resend","method":"POST"}]}'
+        json_obj = json.loads(json_str)
+        transmission_id = header.get('paypal-transmission-id',None)
+        transmission_time = header.get('paypal-transmission-id',None)
+        webhook_id = '9RW011891A1707801'
+        cert_url = header.get('paypal-cert-url',None)
+        transmission_sig = header.get('paypal-transmission-sig',None)
+        auth_algo = header.get('paypal-auth-algo',None)
+        resource_type = json_obj.get('resource_type')
+        if resource_type == 'plan':
+            paypalrestsdk.configure(PAYPAL_CRD)
+            response = paypalrestsdk.WebhookEvent.verify(
+                transmission_id, transmission_time, webhook_id, json_str, cert_url, transmission_sig, auth_algo)
 
-        transmission_id = "dfb3be50-fd74-11e4-8bf3-77339302725b"
-        # Paypal-Transmission-Time in webhook payload header
-        timestamp = "2015-05-18T15:45:13Z"
-        # Webhook id created
-        webhook_id = "4JH86294D6297924G"
-        # Paypal-Transmission-Sig in webhook payload header
-        actual_signature = "thy4/U002quzxFavHPwbfJGcc46E8rc5jzgyeafWm5mICTBdY/8rl7WJpn8JA0GKA+oDTPsSruqusw+XXg5RLAP7ip53Euh9Xu3UbUhQFX7UgwzE2FeYoY6lyRMiiiQLzy9BvHfIzNIVhPad4KnC339dr6y2l+mN8ALgI4GCdIh3/SoJO5wE64Bh/ueWtt8EVuvsvXfda2Le5a2TrOI9vLEzsm9GS79hAR/5oLexNz8UiZr045Mr5ObroH4w4oNfmkTaDk9Rj0G19uvISs5QzgmBpauKr7Nw++JI0pr/v5mFctQkoWJSGfBGzPRXawrvIIVHQ9Wer48GR2g9ZiApWg=="
-        # Paypal-Cert-Url in webhook payload header
-        cert_url = 'https://api.sandbox.paypal.com/v1/notifications/certs/CERT-360caa42-fca2a594-a5cafa77'
-        # PayPal-Auth-Algo in webhook payload header
-        auth_algo = 'sha256'
+        logger.info('response----------------')
+        logger.info(response)
+        return HttpResponse(json_obj.get('id'))
 
-        response = WebhookEvent.verify(
-            transmission_id, timestamp, webhook_id, event_body, cert_url, actual_signature, auth_algo)
-        print(response)