lang 3 år sedan
förälder
incheckning
f15c371990
1 ändrade filer med 30 tillägg och 2 borttagningar
  1. 30 2
      Controller/PaymentCycle.py

+ 30 - 2
Controller/PaymentCycle.py

@@ -11,6 +11,8 @@ from django.views.generic.base import View
 from django.db import transaction
 from Controller import CloudStorage
 from django.db.models import Q, F, Count
+from paypalrestsdk.notifications import WebhookEvent
+import logging
 
 #周期扣款相关
 class Paypal:
@@ -120,7 +122,7 @@ class PaypalCycleNotify(View):
         elif operation == 'paypalCycleReturn':  # paypal成功订阅回调
             return self.do_paypal_cycle_return(request_dict, response)
         elif operation == 'paypalCycleNotify':  # paypal 周期付款回调
-            return self.do_paypal_webhook_notify(request_dict, response)
+            return self.do_paypal_webhook_notify(request_dict,request, response)
     def do_paypal_cycle_return(self, request_dict, response):
         lang = request_dict.get('lang', 'en')
         token = request_dict.get('token',None)
@@ -245,9 +247,35 @@ class PaypalCycleNotify(View):
             return HttpResponseRedirect(red_url)
 
 
-    def do_paypal_webhook_notify(self, request_dict, response):
+    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)
+        logger = logging.getLogger('info')
+        logger.info(request.GET)
+        logger.info(request.POST)
+        import json
+        return HttpResponse(json.dumps(request.GET, ensure_ascii=False))
+        return response.json(request.GET)
         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
+
+        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'
+
+        response = WebhookEvent.verify(
+            transmission_id, timestamp, webhook_id, event_body, cert_url, actual_signature, auth_algo)
+        print(response)