Skip to content

Commit

Permalink
Revision: 61 - Increase in wait time during validation.
Browse files Browse the repository at this point in the history
- Se mejoran algunos logs.
  • Loading branch information
presteamshop committed Nov 28, 2024
1 parent d3a4006 commit 807b3a0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions controllers/front/confirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions controllers/front/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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']
);

Expand Down
25 changes: 20 additions & 5 deletions monei.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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',
[
Expand All @@ -1388,23 +1402,24 @@ 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(
'monei',
[
'locked_at' => time(),
],
'id_monei = ' . (int)$moneiId
'id_monei = ' . (int) $moneiId
);

PrestaShopLogger::addLog(
Expand Down
2 changes: 1 addition & 1 deletion src/Monei/Model/MoneiPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] : '';
}

/**
Expand Down

0 comments on commit 807b3a0

Please sign in to comment.