From 807b3a0fd15bce7bcf6d2568bdc5e270f5f5a77b Mon Sep 17 00:00:00 2001 From: Jehyson Bustamante Date: Thu, 28 Nov 2024 14:26:04 -0500 Subject: [PATCH] Revision: 61 - Increase in wait time during validation. - Se mejoran algunos logs. --- controllers/front/confirmation.php | 5 +++++ controllers/front/validation.php | 4 ++-- monei.php | 25 ++++++++++++++++++++----- src/Monei/Model/MoneiPayment.php | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/controllers/front/confirmation.php b/controllers/front/confirmation.php index cea5685..21e5760 100644 --- a/controllers/front/confirmation.php +++ b/controllers/front/confirmation.php @@ -17,6 +17,11 @@ public function initContent() try { if (!empty($moneiPaymentId) && $moneiStatus !== MoneiPaymentStatus::CANCELED) { + PrestaShopLogger::addLog( + 'MONEI - confirmation.php - initContent - Monei Payment ID: ' . $moneiPaymentId . ' - Monei Status: ' . $moneiStatus, + $this->module::LOG_SEVERITY_LEVELS['info'] + ); + $this->module->createOrUpdateOrder($moneiPaymentId, true); } else { Tools::redirect('index.php?controller=order'); diff --git a/controllers/front/validation.php b/controllers/front/validation.php index 5966930..851e0b7 100644 --- a/controllers/front/validation.php +++ b/controllers/front/validation.php @@ -26,7 +26,7 @@ public function postProcess() $requestBody = Tools::file_get_contents('php://input'); $sigHeader = $_SERVER['HTTP_MONEI_SIGNATURE']; - sleep(1); // Adding a delay to prevent processing too quickly and potentially generating duplicate orders. + sleep(3); // Adding a delay to prevent processing too quickly and potentially generating duplicate orders. try { $this->module->getMoneiClient()->verifySignature($requestBody, $sigHeader); @@ -51,7 +51,7 @@ public function postProcess() // Log the JSON array for debugging PrestaShopLogger::addLog( - 'MONEI - JSON Data: ' . json_encode($json_array), + 'MONEI - validation.php - postProcess - JSON Data: ' . json_encode($json_array), $this->module::LOG_SEVERITY_LEVELS['info'] ); diff --git a/monei.php b/monei.php index 2b15cbb..8c5f59a 100644 --- a/monei.php +++ b/monei.php @@ -1259,6 +1259,13 @@ public function createPayment(bool $tokenizeCard = false, int $moneiCardId = 0) // Save the information before sending it to the API PsOrderHelper::saveTransaction($moneiPayment, true); + if (!$this->moneiClient) { + throw new MoneiException('Monei client not initialized'); + } + if (!isset($this->moneiClient->payments)) { + throw new MoneiException('Monei client payments not initialized'); + } + $moneiPaymentResponse = $this->moneiClient->payments->createPayment($moneiPayment); return $moneiPaymentResponse; @@ -1274,6 +1281,13 @@ public function createPayment(bool $tokenizeCard = false, int $moneiCardId = 0) public function createOrUpdateOrder($moneiPaymentId, bool $redirectToConfirmationPage = false) { + if (!$this->moneiClient) { + throw new MoneiException('Monei client not initialized'); + } + if (!isset($this->moneiClient->payments)) { + throw new MoneiException('Monei client payments not initialized'); + } + $moneiPayment = $this->moneiClient->payments->getPayment($moneiPaymentId); $moneiOrderId = $moneiPayment->getOrderId(); @@ -1375,11 +1389,11 @@ public function createOrUpdateOrder($moneiPaymentId, bool $redirectToConfirmatio } // Create the order - if ($should_create_order) { + if ($should_create_order && !PsOrderHelper::orderExists($cartId)) { // Set a LOCK for slow servers $is_locked_info = MoneiClass::getLockInformation($moneiId); - if ($is_locked_info['locked'] == 0) { + if ($is_locked_info['locked'] == '0') { Db::getInstance()->update( 'monei', [ @@ -1388,15 +1402,16 @@ public function createOrUpdateOrder($moneiPaymentId, bool $redirectToConfirmatio ], 'id_monei = ' . (int)$moneiId ); - } elseif ($is_locked_info['locked'] == 1 && $is_locked_info['locked_at'] < (time() - 60)) { + } elseif ($is_locked_info['locked'] == '1' && $is_locked_info['locked_at'] < (time() - 60)) { $should_create_order = false; + $message = 'Slow server detected, order in creation process'; PrestaShopLogger::addLog( 'MONEI - monei:createOrUpdateOrder - ' . $message, self::LOG_SEVERITY_LEVELS['warning'] ); - } elseif ($is_locked_info['locked'] == 1 && $is_locked_info['locked_at'] > (time() - 60)) { + } elseif ($is_locked_info['locked'] == '1' && $is_locked_info['locked_at'] > (time() - 60)) { $message = 'Slow server detected, previous order creation process timed out'; Db::getInstance()->update( @@ -1404,7 +1419,7 @@ public function createOrUpdateOrder($moneiPaymentId, bool $redirectToConfirmatio [ 'locked_at' => time(), ], - 'id_monei = ' . (int)$moneiId + 'id_monei = ' . (int) $moneiId ); PrestaShopLogger::addLog( diff --git a/src/Monei/Model/MoneiPayment.php b/src/Monei/Model/MoneiPayment.php index e92c1fe..24c2a8d 100644 --- a/src/Monei/Model/MoneiPayment.php +++ b/src/Monei/Model/MoneiPayment.php @@ -223,7 +223,7 @@ public function getAccountId(): string */ public function getAuthorizationCode(): ?string { - return $this->container['authorization_code']; + return isset($this->container['authorization_code']) ? $this->container['authorization_code'] : ''; } /**