From fc89a90afee0f7c3571c4058b181eb0c9c2ab454 Mon Sep 17 00:00:00 2001 From: Luiz Dalcico <74252574+luizdalciconfeio@users.noreply.github.com> Date: Wed, 8 Feb 2023 10:48:04 -0300 Subject: [PATCH] Fixing webhook check and creation (#148) * fixing webhook check and creation * removing comment --- .../lib/Legacy/Functions.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php b/modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php index d7bda11..45c2dd4 100644 --- a/modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php +++ b/modules/addons/NFEioServiceInvoices/lib/Legacy/Functions.php @@ -264,18 +264,20 @@ function gnfe_issue_nfe($postfields) { $_storageKey = Addon::I()->configuration()->storageKey; $storage = new Storage($_storageKey); - logModuleCall('NFEioServiceInvoices', 'gnfe_issue_nfe - gnfe_webhook_id', $gnfe_webhook_id, $webhook_url); - if ($gnfe_webhook_id) { $check_webhook = $this->gnfe_check_webhook($gnfe_webhook_id); + if ($check_webhook == null) { return (object) ['message' => 'Erro ao checar a existência de um webhook já cadastrado']; } + + if ($check_webhook == "ERRO 400" || $check_webhook == "ERRO 404") + { + $gnfe_webhook_id = null; + } } - logModuleCall('NFEioServiceInvoices', 'gnfe_issue_nfe - check_webhook', $check_webhook); - if ($gnfe_webhook_id and (string) $check_webhook['hooks']['url'] !== (string) $webhook_url) { $delete_webhook = $this->gnfe_delete_webhook($gnfe_webhook_id); @@ -290,6 +292,8 @@ function gnfe_issue_nfe($postfields) { if (!$gnfe_webhook_id) { $create_webhook = $this->gnfe_create_webhook($webhook_url); + logModuleCall('NFEioServiceInvoices', 'gnfe_issue_nfe - gnfe_create_webhook', $create_webhook, $webhook_url); + if ($create_webhook == null) { return (object) ['message' => 'Erro ao criar novo webhook']; @@ -299,7 +303,6 @@ function gnfe_issue_nfe($postfields) { $storage->set('webhook_id', $create_webhook['hooks']['id']); $storage->set('webhook_secret', $create_webhook['hooks']['secret']); - logModuleCall($_storageKey, 'webhook_id', $create_webhook); } } @@ -688,6 +691,11 @@ function gnfe_check_webhook($id) { { return json_decode($response, true); } + elseif ($info['http_code'] == 400 || $info['http_code'] == 404) + { + logModuleCall('NFEioServiceInvoices', 'gnfe_check_webhook', $id, "ERRO " . $info['http_code']); + return "ERRO " . $info['http_code']; + } else { logModuleCall('NFEioServiceInvoices', 'gnfe_check_webhook', $id, $info['http_code']); }