function paymill_install_webhooks() { load_paymill(); // this function-call can and should be used whenever working with Paymill API if (get_option('paymill_webhook_id') != false) { // webhook exists? try { $GLOBALS['paymill_loader']->request_webhook->setId(get_option('paymill_webhook_id')); $GLOBALS['paymill_loader']->request_webhook->setUrl(get_site_url() . '/?wc-api=WC_Gateway_Paymill_Gateway'); $GLOBALS['paymill_loader']->request_webhook->setActive(true); $GLOBALS['paymill_loader']->request_webhook->setEventTypes(array('subscription.created', 'subscription.deleted', 'subscription.failed', 'subscription.succeeded')); $webhook = $GLOBALS['paymill_loader']->request->update($GLOBALS['paymill_loader']->request_webhook); update_option('paymill_webhook_id', $webhook->getId()); return __('Webhook successfully updated.', 'paymill'); } catch (Exception $e) { // cache outdated } // cache is outdated, delete option delete_option('paymill_webhook_id'); // retrieve orphaned webhooks containing our URL try { $GLOBALS['paymill_loader']->request_webhook->setFilter(array('url' => get_site_url() . '/?wc-api=WC_Gateway_Paymill_Gateway')); $GLOBALS['paymill_loader']->request_webhook = new $GLOBALS['paymill_loader']->request_webhook(); // re-init class $webhooks = $GLOBALS['paymill_loader']->request->getAll($GLOBALS['paymill_loader']->request_webhook); } catch (Exception $e) { echo __($e->getMessage(), 'paymill'); } // orphaned webhooks found, delete them try { if ($webhooks != false) { foreach ($webhooks as $hook) { $GLOBALS['paymill_loader']->request_webhook = new $GLOBALS['paymill_loader']->request_webhook(); // re-init class $GLOBALS['paymill_loader']->request_webhook->setId($hook['id']); $response = $GLOBALS['paymill_loader']->request->delete($GLOBALS['paymill_loader']->request_webhook); } } } catch (Exception $e) { echo __($e->getMessage(), 'paymill'); } } // still here? create new webhook try { $GLOBALS['paymill_loader']->request_webhook = new $GLOBALS['paymill_loader']->request_webhook(); // re-init class $GLOBALS['paymill_loader']->request_webhook->setUrl(get_site_url() . '/?wc-api=WC_Gateway_Paymill_Gateway'); $GLOBALS['paymill_loader']->request_webhook->setActive(true); $GLOBALS['paymill_loader']->request_webhook->setEventTypes(array('subscription.created', 'subscription.deleted', 'subscription.failed', 'subscription.succeeded')); $webhook = $GLOBALS['paymill_loader']->request->create($GLOBALS['paymill_loader']->request_webhook); add_option('paymill_webhook_id', $webhook->getId()); echo __('Webhook successfully created.', 'paymill'); } catch (Exception $e) { echo __($e->getMessage(), 'paymill'); } paymill_check_webhook(); }
public function section_maintenance_desc() { echo paymill_check_webhook(); }