Search code examples
node.jspaypalpaypal-sandboxpaypal-webhooks

Paypal API call produces 415 status


I set up my express app to listen for Paypal webhooks of my sandbox app. Then I try to verify the integrity of the data via the verify-webhook-signature API endpoint. I use the request module for that. But all I get is a 415 Unsupported Media Type status code and an empty body. This is my code:

app.post('/webhooks', function (req, res) {

if (req.body.event_type == 'PAYMENT.CAPTURE.COMPLETED') {

    headers = {
        'Accept' : 'application/json',
        'Content-Type' : 'application/json',
        'Authorization' : 'Bearer <AUTH_TOKEN>'
    }

    // Get the data for the API call of the webhook request
    data = {
        'transmission_id': req.headers['paypal-transmission-id'],
        'transmission_time': req.headers['paypal-transmission-time'],
        'cert_url': req.headers['paypal-cert-url'],
        'auth_algo': req.headers['paypal-auth-algo'],
        'transmission_sig': req.headers['paypal-transmission-sig'],
        'webhook_id': '41G05244UL253035G',
        'webhook_event' : req.body
    }

    request.post({
        url: 'https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature',
        headers: headers,
        form: data,
    }, (error, response, body) => {

        if (error) {
            console.error(error)
            return
        }

        console.log(response.statusCode);

    });
}

res.sendStatus(200);
});

What is wrong with this data?

Edit: Changing 'form: data' to 'body: JSON.stringify(data)' did it.


Solution

  • Why are you sending back a form post? Don't post a form. Send back raw data.