Create payment notification webhooks

Webhooks are HTTP callbacks that receive notification messages for events. A webhook only needs to be created once for each application that you wish to subscribe to events for.

Note: To test your URI endpoints with webhook data, use the webhooks simulator

To configure your webhook listener to receive notification messages for payment state changes and other events:

1. Define webhooks to which you would like to subscribe.
2. Create webhook.

Define webhooks

To subscribe your app to webhooks, create an object that contains the event_types (the webhook names) and the url to which those notifications should be sent.

var webhook_json = {
  url: 'https://f615ef32.ngrok.io',
  event_types: [{
    name: 'PAYMENT.SALE.COMPLETED'
  },{
    name: 'PAYMENT.SALE.DENIED'
  }]
};
$webhook = new \PayPal\Api\Webhook();

// Set webhook notification URL
$webhook->setUrl("https://f615ef32.ngrok.io");

// Set webhooks to subscribe to
$webhookEventTypes = array();
$webhookEventTypes[] = new \PayPal\Api\WebhookEventType(
  '{
    "name":"PAYMENT.SALE.COMPLETED"
  }'
);

$webhookEventTypes[] = new \PayPal\Api\WebhookEventType(
  '{
    "name":"PAYMENT.SALE.DENIED"
  }'
);

$webhook->setEventTypes($webhookEventTypes);
webhook = Webhook({
  "url": "https://f615ef32.ngrok.io",
  "event_types": [{
    "name": "PAYMENT.SALE.CREATED"
  },{
    "name": "PAYMENT.SALE.DENIED"
  }]
})
webhook = Webhook.new({
  :url => "https://f6153ef32.ngrok.io",
  :event_types => [{
    :name => "PAYMENT.PAYOUTS-ITEM.FAILED"
  },{
    :name => "PAYMENT.PAYOUTS-ITEM.HELD"
  }]
})
import java.util.ArrayList;
import java.util.List;

import com.paypal.api.payments.EventType;
import com.paypal.api.payments.Webhook;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;

List eventTypes = new ArrayList(); 
   
EventType eventType1 = new EventType(); 
eventType1.setName("VAULT.CREDIT-CARD.CREATED"); 

EventType eventType2 = new EventType(); 
eventType2.setName("VAULT.CREDIT-CARD.UPDATED"); 

eventTypes.add(eventType1); 
eventTypes.add(eventType2);

Webhook webhook = new Webhook(); 
webhook.setUrl("https://f6153ef32.ngrok.io"); 
webhook.setEventTypes(eventTypes);
public static Webhook GetNewWebhook(){
  return new Webhook{
    url = GetNewWebhookUrl(),
    event_types = new List{
      new WebhookEventType{
        name = "PAYMENT.SALE.CREATED"
      },
      new WebhookEventType{
        name = "PAYMENT.SALE.DENIED"
      }
    }
  };
}

Note: A full list of webhook events names is available here.

Create webhook

After you create the webhook definition object, call the webhook create method to create the webhook. The endpoint defined in the webhook starts receiving notifications when the events occur in the app.

paypal.notification.webhook.create(webhook_json, function (error, webhook) {
  if (error) {
    console.error(JSON.stringify(error.response));
    throw error;
  } else {
    console.log('Create webhook Response');
    console.log(webhook);
  }
});
try {
  $output = $webhook->create($apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
  echo $ex->getCode();
  echo $ex->getData();
  die($ex);
} catch (Exception $ex) {
  die($ex);
}
if webhook.create():
  print("Webhook[%s] created successfully" % (webhook.id))
else:
  print(webhook.error)
begin
  webhook = webhook.create
  logger.info "Webhook[#{webhook.id}] created successfully"
rescue ResourceNotFound => err
  logger.error webhook.error.inspect
end
try{
  Webhook createdWebhook = webhook.create(apiContext, webhook);
  System.out.println("Webhook successfully created with ID " + createdWebhook.getId());
} catch (PayPalRESTException e) {
  System.err.println(e.getDetails());
}
var apiContext = Configuration.GetAPIContext();
var webhook = GetNewWebhook();

this.flow.AddNewRequest("Create webhook", webhook);
var createdWebhook = webhook.Create(apiContext);

Additional information