forked from LiveCarta/PayPal-PHP-SDK
More Webhook API Changes
- Updated API Models with Fixes - Updated Samples - Additional Functional Tests
This commit is contained in:
24
sample/notifications/DeleteAllWebhooks.php
Normal file
24
sample/notifications/DeleteAllWebhooks.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
// # Delete All Webhook Sample
|
||||
// This is a sample helper method, to delete all existing webhooks, because of limited number of webhooks that are allowed per app.
|
||||
// To properly use the sample, change the clientId and Secret from bootstrap.php file with your own app ClientId and Secret.
|
||||
|
||||
// ## Get Webhook Instance
|
||||
|
||||
/** @var \PayPal\Api\WebhookList $webhookList */
|
||||
$webhookList = require 'ListWebhooks.php';
|
||||
|
||||
// ### Delete Webhook
|
||||
try {
|
||||
foreach ($webhookList->getWebhooks() as $webhook) {
|
||||
$webhook->delete($apiContext);
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
ResultPrinter::printError("Deleted all Webhooks", "WebhookList", null, null, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ResultPrinter::printResult("Delete all Webhook, as it may have exceed the maximum count.", "WebhookList", null, null, null);
|
||||
|
||||
return $output;
|
||||
25
sample/notifications/DeleteWebhook.php
Normal file
25
sample/notifications/DeleteWebhook.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
// # Delete Webhook Sample
|
||||
//
|
||||
// This sample code demonstrate how to use this call to search for all webhook events., as documented here at:
|
||||
// https://developer.paypal.com/docs/api/#delete-a-webhook
|
||||
// API used: DELETE v1/notifications/webhooks/<Webhook-Id>
|
||||
|
||||
// ## Get Webhook Instance
|
||||
|
||||
/** @var \PayPal\Api\Webhook $webhook */
|
||||
$webhook = require 'CreateWebhook.php';
|
||||
|
||||
|
||||
// ### Delete Webhook
|
||||
try {
|
||||
$output = $webhook->delete($apiContext);
|
||||
} catch (Exception $ex) {
|
||||
ResultPrinter::printError("Delete a Webhook", "Webhook", null, $webhookId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ResultPrinter::printResult("Delete a Webhook", "Webhook", $webhook->getId(), null, null);
|
||||
|
||||
return $output;
|
||||
26
sample/notifications/GetWebhook.php
Normal file
26
sample/notifications/GetWebhook.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
// # Get Webhook Sample
|
||||
//
|
||||
// This sample code demonstrate how you can get a webhook, as documented here at:
|
||||
// https://developer.paypal.com/webapps/developer/docs/api/#get-a-webhook
|
||||
// API used: GET /v1/notifications/webhooks/<Webhook-Id>
|
||||
|
||||
// ## Get Webhook ID.
|
||||
// In samples we are using CreateWebhook.php sample to get the created instance of webhook.
|
||||
// However, in real case scenario, we could use just the ID from database or retrieved from the form.
|
||||
/** @var \PayPal\Api\Webhook $webhook */
|
||||
$webhook = require 'CreateWebhook.php';
|
||||
$webhookId = $webhook->getId();
|
||||
|
||||
// ### Get Webhook
|
||||
try {
|
||||
$output = \PayPal\Api\Webhook::get($webhookId, $apiContext);
|
||||
} catch (Exception $ex) {
|
||||
ResultPrinter::printError("Get a Webhook", "Webhook", null, $webhookId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ResultPrinter::printResult("Get a Webhook", "Webhook", $output->getId(), null, $output);
|
||||
|
||||
return $output;
|
||||
26
sample/notifications/ListSubscribedWebhookEventTypes.php
Normal file
26
sample/notifications/ListSubscribedWebhookEventTypes.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
// # Get Webhook Sample
|
||||
//
|
||||
// This sample code demonstrate how you can get a webhook, as documented here at:
|
||||
// https://developer.paypal.com/webapps/developer/docs/api/#get-a-webhook
|
||||
// API used: GET /v1/notifications/webhooks/<Webhook-Id>
|
||||
|
||||
// ## List Subscribed Event Types
|
||||
// Use this call to retrieve the list of events types that are subscribed to a webhook.
|
||||
|
||||
/** @var \PayPal\Api\Webhook $webhook */
|
||||
$webhook = require 'CreateWebhook.php';
|
||||
$webhookId = $webhook->getId();
|
||||
|
||||
// ### Get List of Subscribed Event Types
|
||||
try {
|
||||
$output = \PayPal\Api\WebhookEventType::subscribedEventTypes($webhookId, $apiContext);
|
||||
} catch (Exception $ex) {
|
||||
ResultPrinter::printError("List subscribed webhook event types", "WebhookEventTypeList", null, $webhookId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ResultPrinter::printResult("List subscribed webhook event types", "WebhookEventTypeList",null, null, $output);
|
||||
|
||||
return $output;
|
||||
27
sample/notifications/ListWebhooks.php
Normal file
27
sample/notifications/ListWebhooks.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
// # Get All Webhooks Sample
|
||||
//
|
||||
// Use this call to list all the webhooks, as documented here at:
|
||||
// https://developer.paypal.com/webapps/developer/docs/api/#list-all-webhooks
|
||||
// API used: GET /v1/notifications/webhooks
|
||||
|
||||
// ## List Webhooks
|
||||
|
||||
// This step is not necessarily required. We are creating a webhook for sample purpose only, so that we would not
|
||||
// get an empty list at any point.
|
||||
// In real case, you dont need to create any webhook to make this API call.
|
||||
/** @var \PayPal\Api\Webhook $webhook */
|
||||
$webhook = require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
// ### Get List of All Webhooks
|
||||
try {
|
||||
$output = \PayPal\Api\Webhook::getAll($apiContext);
|
||||
} catch (Exception $ex) {
|
||||
ResultPrinter::printError("List all webhooks", "WebhookList", null, $webhookId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ResultPrinter::printResult("List all webhooks", "WebhookList",null, null, $output);
|
||||
|
||||
return $output;
|
||||
31
sample/notifications/SearchWebhookEvents.php
Normal file
31
sample/notifications/SearchWebhookEvents.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
// # Search Webhook Events Sample
|
||||
//
|
||||
// This sample code demonstrate how to use this call to search for all webhook events., as documented here at:
|
||||
// https://developer.paypal.com/docs/api/#search-webhook-events
|
||||
// API used: GET /v1/notifications/webhooks-events
|
||||
|
||||
// ## Get Webhook Instance
|
||||
// ## PLEASE NOTE:
|
||||
// Creating webhook is sample purposes only. In real scenario, you dont need to create a new webhook everytime you want to search
|
||||
// for a webhook events. This is made in a sample just to make sure there is minimum of one webhook to listen to.
|
||||
/** @var \PayPal\Api\Webhook $webhook */
|
||||
$webhook = require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
$params = array(
|
||||
// 'start_time'=>'2014-12-06T11:00:00Z',
|
||||
// 'end_time'=>'2014-12-12T11:00:00Z'
|
||||
);
|
||||
|
||||
// ### Search Webhook events
|
||||
try {
|
||||
$output = \PayPal\Api\WebhookEvent::all($params, $apiContext);
|
||||
} catch (Exception $ex) {
|
||||
ResultPrinter::printError("Search Webhook events", "WebhookEventList", null, null, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ResultPrinter::printResult("Search Webhook events", "WebhookEventList", null, $params, $output);
|
||||
|
||||
return $output;
|
||||
55
sample/notifications/UpdateWebhook.php
Normal file
55
sample/notifications/UpdateWebhook.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
// # Update Webhook Sample
|
||||
//
|
||||
// This sample code demonstrate how to use this call to update a webhook; supports the replace operation only, as documented here at:
|
||||
// https://developer.paypal.com/webapps/developer/docs/api/#update-a-webhook
|
||||
// API used: PATCH v1/notifications/webhooks/<Webhook-Id>
|
||||
|
||||
// ## Get Webhook ID.
|
||||
// In samples we are using CreateWebhook.php sample to get the created instance of webhook.
|
||||
// However, in real case scenario, we could use just the ID from database or use an already existing webhook.
|
||||
/** @var \PayPal\Api\Webhook $webhook */
|
||||
$webhook = require 'CreateWebhook.php';
|
||||
// Updating the webhook as per given request
|
||||
//
|
||||
// [
|
||||
// {
|
||||
// "op":"replace",
|
||||
// "path":"/url",
|
||||
// "value":"https://requestb.in/10ujt3c1"
|
||||
// },
|
||||
// {
|
||||
// "op":"replace",
|
||||
// "path":"/event_types",
|
||||
// "value":[
|
||||
// {
|
||||
// "name":"PAYMENT.SALE.REFUNDED"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
$patch = new \PayPal\Api\Patch();
|
||||
$patch->setOp("replace")
|
||||
->setPath("/url")
|
||||
->setValue("https://requestb.in/10ujt3c1?uniqid=". uniqid());
|
||||
|
||||
$patch2 = new \PayPal\Api\Patch();
|
||||
$patch2->setOp("replace")
|
||||
->setPath("/event_types")
|
||||
->setValue(json_decode('[{"name":"PAYMENT.SALE.REFUNDED"}]'));
|
||||
|
||||
$patchRequest = new \PayPal\Api\PatchRequest();
|
||||
$patchRequest->addPatch($patch)->addPatch($patch2);
|
||||
|
||||
// ### Get Webhook
|
||||
try {
|
||||
$output = $webhook->update($patchRequest, $apiContext);
|
||||
} catch (Exception $ex) {
|
||||
ResultPrinter::printError("Updated a Webhook", "Webhook", null, $patchRequest, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ResultPrinter::printResult("Updated a Webhook", "Webhook", $output->getId(), $patchRequest, $output);
|
||||
|
||||
return $output;
|
||||
21
sample/notifications/WebhookEventTypesList.php
Normal file
21
sample/notifications/WebhookEventTypesList.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
// # Get Reference List of all Webhook Event Types
|
||||
//
|
||||
// This sample code demonstrate how you can get reference list of all webhook event types, as documented here at:
|
||||
// https://developer.paypal.com/webapps/developer/docs/api/#get-a-reference-list-of-webhook-event-types
|
||||
// API used: GET /v1/notifications/webhooks-event-types
|
||||
|
||||
$apiContext = require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
// ### Get List of all Webhook event types
|
||||
try {
|
||||
$output = \PayPal\Api\WebhookEventType::availableEventTypes($apiContext);
|
||||
} catch (Exception $ex) {
|
||||
ResultPrinter::printError("Get List of All Webhook Event Types", "WebhookEventTypeList", null, null, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ResultPrinter::printResult("Get List of All Webhook Event Types", "WebhookEventTypeList", null, null, $output);
|
||||
|
||||
return $output;
|
||||
77
sample/notifications/createWebhook.php
Normal file
77
sample/notifications/createWebhook.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?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;
|
||||
Reference in New Issue
Block a user