Search code examples
paypalpaypal-subscriptions

PayPal webhook validation in Python


I'm trying to implement the webhook verification but I have an error "malformed json". Can you please look and tell me what I'm doing wrong? Thanks!

transmission_id = request.headers.get('PAYPAL-TRANSMISSION-ID')
transmission_time = request.headers.get('PAYPAL-TRANSMISSION-TIME')
transmission_sig = request.headers.get('PAYPAL-TRANSMISSION-SIG')
auth_algo = request.headers.get('PAYPAL-AUTH-ALGO')

from collections import OrderedDict
body_data = json.loads(request.get_data(as_text=True), object_pairs_hook=OrderedDict)


data = {
            'auth_algo': auth_algo, 
            'cert_url':request.headers.get('PAYPAL-CERT-URL'),
            'transmission_id': transmission_id,
            'transmission_sig': transmission_sig,
            'transmission_time': transmission_time,
            'webhook_id': PAYPAL_WEBHOOK_ID,
            'webhook_event': body_data
        }
        response = requests.post('https://api-m.sandbox.paypal.com/v1/notifications/verify-webhook-signature',
                            headers=plan_headers, data=data)

Solution

  • data= takes a string

    Form a JSON string with json.dumps( or use json=