This sample will show you how to update an existing webhook forwarding URL (where the notifications should be POSTed to). To run this, you should have the ID provided back by PayPal when you first created your webhooks.
First, add the PayPal SDK and configure the environment (sandbox below).
var paypal = require('paypal-rest-sdk');
var clientId = 'YOUR APPLICATION CLIENT ID';
var secret = 'YOUR APPLICATION SECRET';
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': clientId,
'client_secret': secret
});
Next, set up the JSON structure and webhook details. Assign the ID for your webhook to webhookId
first. Next, in the webhookUpdate
, specify an operation of replace, set the path
to /url
to specify an update of that resource, and provide the new URL to replace it with under value
.
var webhookId = "YOUR WEBHOOK ID";
var webhookUpdate = [{
"op": "replace",
"path": "/url",
"value": "https://64fb54a2.ngrok.io"
}];
Lastly, call notification.webhook.replace(...)
, passing in webhookId
and webhookUpdate
.
paypal.notification.webhook.replace(webhookId, webhookUpdate, function (err, res) { if (err) { console.log(err); throw err; } else { console.log(JSON.stringify(res)); } });
If all succeeds, an object similar to the following should be provided back from PayPal and, in the case of this sample, displayed in the terminal with the newly updated information.
{
"id":"4U496984902512511",
"url":"https://64fb54a2.ngrok.io",
"event_types":[{
"name":"PAYMENT.SALE.DENIED",
"description":"A sale payment was denied"
}],
"links":[{
"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks/4U496984902512511",
"rel":"self",
"method":"GET"
},{
"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks/4U496984902512511",
"rel":"update",
"method":"PATCH"
},{
"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks/4U496984902512511",
"rel":"delete",
"method":"DELETE"
}],
"httpStatusCode":200
}