This repository has been archived on 2026-04-06. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
PayPal-PHP-SDK/sample/notifications/createWebhook.php
japatel fb9584ba66 More Webhook API Changes
- Updated API Models with Fixes
- Updated Samples
- Additional Functional Tests
2014-12-12 17:00:33 -06:00

78 lines
2.6 KiB
PHP

<?php
// # Create Webhook Sample
//
// This sample code demonstrate how you can create a webhook, as documented here at:
// https://developer.paypal.com/webapps/developer/docs/api/#create-a-webhook
// API used: POST /v1/notifications/webhooks
require __DIR__ . '/../bootstrap.php';
// Create a new instance of Webhook object
$webhook = new \PayPal\Api\Webhook();
// # Basic Information
// {
// "url":"https://requestb.in/10ujt3c1",
// "event_types":[
// {
// "name":"PAYMENT.AUTHORIZATION.CREATED"
// },
// {
// "name":"PAYMENT.AUTHORIZATION.VOIDED"
// }
// ]
// }
// Fill up the basic information that is required for the webhook
// The URL should be actually accessible over the internet. Having a localhost here would not work.
$webhook->setUrl("https://requestb.in/10ujt3c1?uniqid=" . uniqid());
// # Event Types
// Event types correspond to what kind of notifications you want to receive on the given URL.
$webhookEventTypes = array();
$webhookEventTypes[] = new \PayPal\Api\WebhookEventType(
'{
"name":"PAYMENT.AUTHORIZATION.CREATED"
}'
);
$webhookEventTypes[] = new \PayPal\Api\WebhookEventType(
'{
"name":"PAYMENT.AUTHORIZATION.VOIDED"
}'
);
$webhook->setEventTypes($webhookEventTypes);
// For Sample Purposes Only.
$request = clone $webhook;
// ### Create Webhook
try {
$output = $webhook->create($apiContext);
} catch (Exception $ex) {
// ^ Ignore workflow code segment
if ($ex instanceof \PayPal\Exception\PPConnectionException) {
$data = $ex->getData();
ResultPrinter::printError("Created Webhook Failed. Checking if it is Webhook Number Limit Exceeded. Trying to delete all existing webhooks", "Webhook", "Please Use <a style='color: red;' href='DeleteAllWebhooks.php' >Delete All Webhooks</a> Sample to delete all existing webhooks in sample", $request, $ex);
if (strpos($data,'WEBHOOK_NUMBER_LIMIT_EXCEEDED') !== false) {
require 'DeleteAllWebhooks.php';
try {
$output = $webhook->create($apiContext);
} catch (Exception $ex) {
ResultPrinter::printError("Created Webhook", "Webhook", null, $request, $ex);
exit(1);
}
} else {
ResultPrinter::printError("Created Webhook", "Webhook", null, $request, $ex);
exit(1);
}
} else {
ResultPrinter::printError("Created Webhook", "Webhook", null, $request, $ex);
exit(1);
}
// Print Success Result
}
ResultPrinter::printResult("Created Webhook", "Webhook", $output->getId(), $request, $output);
return $output;