$url]); // request all webhooks foreach (WC_Tabby_Config::getConfiguredCountries() as $country) { // get list of registered hooks $hooks = static::getWebhooks($country); // bypass not authorized errors if (static::isNotAuthorized($hooks)) { static::ddlog("info", "Store code not authorized for merchant", null, ['code' => $country]); continue; } if (!is_array($hooks)) { $hooks = [$hooks]; } $registered = false; foreach ($hooks as $hook) { if (!is_object($hook)) continue; if (property_exists($hook, 'url') && $hook->url == $url) { if ($hook->is_test !== static::getIsTest()) { static::updateWebhook($hook, $country, $url); } $registered = true; } } if (!$registered) { static::registerWebhook($country, $url); } } } public static function isNotAuthorized($response) { if (is_object($response) && property_exists($response, 'errorType') && in_array($response->errorType, ['not_authorized', 'not_found'])) return true; return false; } public static function registerWebhook($code, $url) { $data = ['url' => $url, 'is_test' => static::getIsTest()]; static::ddlog("info", "Registering webhook", null, $data); return WC_Tabby_Api::request('webhooks', 'POST', $data, $code); } public static function updateWebhook($hook, $code, $url) { $data = ['url' => $url, 'is_test' => static::getIsTest()]; static::ddlog("info", "Updating webhook", null, $data); return WC_Tabby_Api::request('webhooks/' . $hook->id, 'PUT', $data, $code); } public static function getWebhooks($code) { return WC_Tabby_Api::request('webhooks', 'GET', null, $code); } public static function getIsTest() { return (bool)preg_match('#^sk_test#', WC_Tabby_Api::get_api_option('secret_key')); } public static function unregister() { } public static function ddlog($status = "error", $message = "Something went wrong", $e = null, $data = null) { return WC_Tabby_Api::ddlog($status, $message, $e, $data); } }