protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) { if (!class_exists('Conekta')) { error_log("Plugin miss Conekta PHP lib dependency. Clone the repository using 'git clone --recursive git@github.com:conekta/conekta-magento.git'", 0); throw new Mage_Payment_Model_Info_Exception("Payment module unavailable. Please contact system administrator."); } Conekta::setApiKey(Mage::getStoreConfig('payment/webhook/privatekey')); Conekta::setApiVersion("1.0.0"); Conekta::setLocale(Mage::app()->getLocale()->getLocaleCode()); $url = new Varien_Data_Form_Element_Text(); $data = array('name' => $element->getName(), 'html_id' => $element->getId()); $url->setData($data); $webhook_url = Mage::getBaseUrl() . "index.php/webhook/ajax/listener"; if (!empty($element->getValue())) { $url_string = $element->getValue(); } else { $url_string = $webhook_url; } $url->setValue($url_string); $events = array("events" => array("charge.created", "charge.paid", "charge.under_fraud_review", "charge.fraudulent", "charge.refunded", "charge.created", "customer.created", "customer.updated", "customer.deleted", "webhook.created", "webhook.updated", "webhook.deleted", "charge.chargeback.created", "charge.chargeback.updated", "charge.chargeback.under_review", "charge.chargeback.lost", "charge.chargeback.won", "payout.created", "payout.retrying", "payout.paid_out", "payout.failed", "plan.created", "plan.updated", "plan.deleted", "subscription.created", "subscription.paused", "subscription.resumed", "subscription.canceled", "subscription.expired", "subscription.updated", "subscription.paid", "subscription.payment_failed", "payee.created", "payee.updated", "payee.deleted", "payee.payout_method.created", "payee.payout_method.updated", "payee.payout_method.deleted")); $error = false; $error_message = null; try { $different = true; $webhooks = Conekta_Webhook::where(); foreach ($webhooks as $webhook) { if (strpos($webhook->webhook_url, $url_string) !== false) { $different = false; } } if ($different) { $webhook = Conekta_Webhook::create(array_merge(array("url" => $url_string), $events)); } } catch (Exception $e) { $error = true; $error_message = $e->getMessage(); } $url->setForm($element->getForm()); $html = $url->getElementHtml(); $javaScript = "\n <script type=\"text/javascript\">\n Event.observe(window, 'load', function() {\n alert('" . $error_message . "');\n });\n </script>"; if ($error) { $html .= $javaScript; } return $html; }
private function _createWebhook() { require_once dirname(__FILE__) . '/lib/conekta-php/lib/Conekta.php'; Conekta::setApiKey(Configuration::get('CONEKTA_MODE') ? Configuration::get('CONEKTA_PRIVATE_KEY_LIVE') : Configuration::get('CONEKTA_PRIVATE_KEY_TEST')); Conekta::setApiVersion("1.0.0"); Conekta::setLocale('en'); $events = array("events" => array("charge.paid")); // Reset error message Configuration::deleteByName('CONEKTA_WEBHOOK_ERROR_MESSAGE'); // Obtain user input $url = Tools::safeOutput(Tools::getValue('conekta_webhook')); // Obtain stored value $config_url = Tools::safeOutput(Configuration::get('CONEKTA_WEBHOOK')); $is_valid_url = !empty($url) && !filter_var($url, FILTER_VALIDATE_URL) === false; $failed_attempts = (int) Configuration::get('CONEKTA_WEBHOOK_FAILED_ATTEMPTS'); // If input is valid, has not been stored and has not failed more than 5 times if ($is_valid_url && $config_url != $url && ($failed_attempts < 5 && $url != Configuration::get('CONEKTA_WEBHOOK_FAILED_URL'))) { try { $different = true; $webhooks = Conekta_Webhook::where(); $urls = array(); foreach ($webhooks as $webhook) { array_push($urls, $webhook->webhook_url); } if (!in_array($url_string, $urls)) { if (Configuration::get('CONEKTA_MODE')) { $mode = array("production_enabled" => 1); } else { $mode = array("development_enabled" => 1); } $webhook = Conekta_Webhook::create(array_merge(array("url" => $url), $mode, $events)); Configuration::updateValue('CONEKTA_WEBHOOK', $url); // delete error variables Configuration::deleteByName('CONEKTA_WEBHOOK_FAILED_ATTEMPTS'); Configuration::deleteByName('CONEKTA_WEBHOOK_FAILED_URL'); Configuration::deleteByName('CONEKTA_WEBHOOK_ERROR_MESSAGE'); } } catch (Exception $e) { Configuration::updateValue('CONEKTA_WEBHOOK_ERROR_MESSAGE', $e->message_to_purchaser); } } else { if ($url == Configuration::get('CONEKTA_WEBHOOK_FAILED_URL')) { Configuration::updateValue('CONEKTA_WEBHOOK_ERROR_MESSAGE', "Webhook was already register, try changing webhook!"); Configuration::deleteByName('CONEKTA_WEBHOOK_FAILED_ATTEMPTS'); $failed_attempts = 0; } elseif ($failed_attempts >= 5) { Configuration::updateValue('CONEKTA_WEBHOOK_ERROR_MESSAGE', "Maximum failed attempts reached!"); } elseif (!$is_valid_url) { Configuration::updateValue('CONEKTA_WEBHOOK_ERROR_MESSAGE', "Not a valid url!"); } else { Configuration::updateValue('CONEKTA_WEBHOOK_ERROR_MESSAGE', "Webhook was already registered in your shop!"); } } if (!empty(Configuration::get('CONEKTA_WEBHOOK_ERROR_MESSAGE'))) { $failed_attempts = $failed_attempts + 1; Configuration::updateValue('CONEKTA_WEBHOOK_FAILED_ATTEMPTS', $failed_attempts); Configuration::updateValue('CONEKTA_WEBHOOK_FAILED_URL', $url); } }