/** * Creates a webhook * * @return bool|\PayPal\Api\Webhook */ public function createWebhook() { $webhook = new \PayPal\Api\Webhook(); $webhook->setUrl(Mage::helper('iways_paypalplus')->getWebhooksUrl()); $webhookEventTypes = array(); foreach (Mage::getModel('iways_paypalplus/webhook_event')->getSupportedWebhookEvents() as $webhookEvent) { $webhookEventType = new \PayPal\Api\WebhookEventType(); $webhookEventType->setName($webhookEvent); $webhookEventTypes[] = $webhookEventType; } $webhook->setEventTypes($webhookEventTypes); try { $webhookData = $webhook->create($this->_apiContext); $this->saveWebhookId($webhookData->getId()); return $webhookData; } catch (PayPal\Exception\PayPalConnectionException $ex) { if ($ex->getData()) { $data = Mage::helper('core')->jsonDecode($ex->getData()); if ($data['name'] == self::WEBHOOK_URL_ALREADY_EXISTS) { return true; } } Mage::helper('iways_paypalplus')->handleException($ex); return false; } catch (Exception $e) { Mage::logException($e); return false; } return false; }
// 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);
/** * Creates a webhook * * @return bool|\PayPal\Api\Webhook */ public function createWebhook() { $webhook = new \PayPal\Api\Webhook(); $webhook->setUrl(Mage::helper('iways_paypalplus')->getWebhooksUrl()); $webhookEventTypes = array(); foreach (Mage::getModel('iways_paypalplus/webhook_event')->getSupportedWebhookEvents() as $webhookEvent) { $webhookEventType = new \PayPal\Api\WebhookEventType(); $webhookEventType->setName($webhookEvent); $webhookEventTypes[] = $webhookEventType; } $webhook->setEventTypes($webhookEventTypes); $webhookData = $webhook->create($this->_apiContext); $this->saveWebhookId($webhookData->getId()); return $webhookData; }