recreate project
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?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.
|
||||
// #### There is an open source tool http://requestb.in/ that allows you to receive any web requests to a url given there.
|
||||
// #### NOTE: Please note that you need an https url for paypal webhooks. You can however override the url with https, and accept
|
||||
// any warnings your browser might show you. Also, please note that this is entirely for demo purposes, and you should not
|
||||
// be using this in production
|
||||
$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\PayPalConnectionException) {
|
||||
$data = $ex->getData();
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Created Webhook", "Webhook", null, $request, $ex);
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Created Webhook", "Webhook", null, $request, $ex);
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Created Webhook", "Webhook", null, $request, $ex);
|
||||
exit(1);
|
||||
}
|
||||
// Print Success Result
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Created Webhook", "Webhook", $output->getId(), $request, $output);
|
||||
|
||||
return $output;
|
||||
@@ -0,0 +1,26 @@
|
||||
<?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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Deleted all Webhooks", "WebhookList", null, null, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Delete all Webhook, as it may have exceed the maximum count.", "WebhookList", null, null, null);
|
||||
|
||||
return $output;
|
||||
@@ -0,0 +1,27 @@
|
||||
<?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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Delete a Webhook", "Webhook", null, $webhookId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Delete a Webhook", "Webhook", $webhook->getId(), null, null);
|
||||
|
||||
return $output;
|
||||
@@ -0,0 +1,28 @@
|
||||
<?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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Get a Webhook", "Webhook", null, $webhookId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Get a Webhook", "Webhook", $output->getId(), null, $output);
|
||||
|
||||
return $output;
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("List subscribed webhook event types", "WebhookEventTypeList", null, $webhookId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("List subscribed webhook event types", "WebhookEventTypeList",null, null, $output);
|
||||
|
||||
return $output;
|
||||
@@ -0,0 +1,29 @@
|
||||
<?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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("List all webhooks", "WebhookList", null, $webhookId, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("List all webhooks", "WebhookList",null, null, $output);
|
||||
|
||||
return $output;
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Search Webhook events", "WebhookEventList", null, null, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Search Webhook events", "WebhookEventList", null, $params, $output);
|
||||
|
||||
|
||||
return $output;
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Updated a Webhook", "Webhook", null, $patchRequest, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Updated a Webhook", "Webhook", $output->getId(), $patchRequest, $output);
|
||||
|
||||
return $output;
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
$apiContext = require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
// # Validate Webhook
|
||||
// PHP Currently does not support certificate chain validation, that is necessary to validate webhook directly, from received data
|
||||
// To resolve that, we need to use alternative, which includes making a GET call to obtain the data directly from PayPal.
|
||||
|
||||
//
|
||||
// ## Received Body from Webhook
|
||||
// Body received from webhook. This would be the data that you receive in the post request that comes from PayPal, to your webhook set URL.
|
||||
// This is a sample data, that represents the webhook event data.
|
||||
$bodyReceived = '{"id":"WH-36G56432PK518391U-9HW18392D95289106","create_time":"2015-06-01T20:21:13Z","resource_type":"sale","event_type":"PAYMENT.SALE.COMPLETED","summary":"Payment completed for $ 20.0 USD","resource":{"id":"2FY57107YS3937627","create_time":"2015-06-01T20:20:28Z","update_time":"2015-06-01T20:20:46Z","amount":{"total":"20.00","currency":"USD"},"payment_mode":"INSTANT_TRANSFER","state":"completed","protection_eligibility":"ELIGIBLE","protection_eligibility_type":"ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE","parent_payment":"PAY-2SV945219E505370PKVWL5DA","transaction_fee":{"value":"0.88","currency":"USD"},"links":[{"href":"https://api.sandbox.paypal.com/v1/payments/sale/2FY57107YS3937627","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/payments/sale/2FY57107YS3937627/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-2SV945219E505370PKVWL5DA","rel":"parent_payment","method":"GET"}]},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-36G56432PK518391U-9HW18392D95289106","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-36G56432PK518391U-9HW18392D95289106/resend","rel":"resend","method":"POST"}]}';
|
||||
|
||||
/**
|
||||
* This is one way to receive the entire body that you received from PayPal webhook. This is one of the way to retrieve that information.
|
||||
* Just uncomment the below line to read the data from actual request.
|
||||
*/
|
||||
/** @var String $bodyReceived */
|
||||
// $bodyReceived = file_get_contents('php://input');
|
||||
|
||||
// ### Validate Received Event Method
|
||||
// Call the validateReceivedEvent() method with provided body, and apiContext object to validate
|
||||
try {
|
||||
/** @var \PayPal\Api\WebhookEvent $output */
|
||||
$output = \PayPal\Api\WebhookEvent::validateAndGetReceivedEvent($bodyReceived, $apiContext);
|
||||
} catch (Exception $ex) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Validate Received Webhook Event", "WebhookEvent", null, $bodyReceived, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Validate Received Webhook Event", "WebhookEvent", $output->getId(), $bodyReceived, $output);
|
||||
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?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) {
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printError("Get List of All Webhook Event Types", "WebhookEventTypeList", null, null, $ex);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
||||
ResultPrinter::printResult("Get List of All Webhook Event Types", "WebhookEventTypeList", null, null, $output);
|
||||
|
||||
return $output;
|
||||
Reference in New Issue
Block a user