Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible solution for checkout modules #61

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions controllers/front/confirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exit;
}

use Monei\MoneiException;
use Monei\Model\MoneiPaymentStatus;

class MoneiConfirmationModuleFrontController extends ModuleFrontController
Expand All @@ -20,10 +21,10 @@ public function initContent()
} else {
Tools::redirect('index.php?controller=order');
}
} catch (Exception $ex) {
} catch (MoneiException $ex) {
PrestaShopLogger::addLog(
'MONEI - Exception - validation.php - postProcess: ' . $ex->getMessage() . ' - ' . $ex->getFile(),
PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR
'MONEI - Exception - confirmation.php - initContent: ' . $ex->getMessage() . ' - ' . $ex->getFile(),
$this->module::LOG_SEVERITY_LEVELS['error']
);

$this->context->cookie->monei_error = $ex->getMessage();
Expand Down
2 changes: 1 addition & 1 deletion controllers/front/redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function postProcess()
$this->context->cookie->monei_error = 'API: ' . $ex->getMessage();
Tools::redirect($this->context->link->getModuleLink($this->module->name, 'errors'));
} catch (Exception $ex) {
$this->context->cookie->monei_error = 'API: ' . $ex->getMessage();
$this->context->cookie->monei_error = $ex->getMessage();
Tools::redirect($this->context->link->getModuleLink($this->module->name, 'errors'));
}

Expand Down
39 changes: 30 additions & 9 deletions controllers/front/validation.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
use Monei\ApiException;
use Monei\Model\MoneiPayment;
use Monei\MoneiException;
use Monei\Traits\ValidationHelpers;

if (!defined('_PS_VERSION_')) {
Expand All @@ -14,46 +15,66 @@ class MoneiValidationModuleFrontController extends ModuleFrontController
public function postProcess()
{
// If the module is not active anymore, no need to process anything.
if ($this->module->active == false) {
if (!$this->module->active) {
die('Module is not active');
}

$data = Tools::file_get_contents('php://input');
if (!isset($_SERVER['HTTP_MONEI_SIGNATURE'])) {
die('HTTP_MONEI_SIGNATURE is not set');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not expose what is the reason, just throw Unauthorized error

}

$requestBody = Tools::file_get_contents('php://input');
$sigHeader = $_SERVER['HTTP_MONEI_SIGNATURE'];

try {
$this->module->getMoneiClient()->verifySignature($requestBody, $sigHeader);
} catch (ApiException $e) {
PrestaShopLogger::addLog(
'MONEI - Exception - validation.php - postProcess: ' . $e->getMessage() . ' - ' . $e->getFile(),
$this->module::LOG_SEVERITY_LEVELS['error']
);

header('HTTP/1.1 401 Unauthorized');
echo '<h1>Unauthorized</h1>';
echo $e->getMessage();
exit;
}

try {
// Check if the data is a valid JSON
$json_array = $this->vJSON($data);
$json_array = $this->vJSON($requestBody);
if (!$json_array) {
throw new ApiException('Invalid JSON');
}

// Log the JSON array for debugging
PrestaShopLogger::addLog(
'MONEI - JSON Data: ' . json_encode($json_array),
PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE
$this->module::LOG_SEVERITY_LEVELS['info']
);

// Parse the JSON to a MoneiPayment object
$moneiPayment = new MoneiPayment($json_array);

// Create or update the order
$this->module->createOrUpdateOrder($moneiPayment);
// The ID is sent instead of the object, as if the card token is to be saved, it must be queried via the API and cannot be done from the object.
// https://docs.monei.com/docs/guides/save-payment-method/#2-obtain-and-store-payment-token
$this->module->createOrUpdateOrder($moneiPayment->getId());

// Log the order creation/update for debugging
PrestaShopLogger::addLog(
'MONEI - Order created/updated for Payment ID: ' . $moneiPayment->getId(),
PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE
$this->module::LOG_SEVERITY_LEVELS['info']
);
} catch (Exception $ex) {
} catch (MoneiException $ex) {
PrestaShopLogger::addLog(
'MONEI - Exception - validation.php - postProcess: ' . $ex->getMessage() . ' - ' . $ex->getFile(),
PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR
$this->module::LOG_SEVERITY_LEVELS['error']
);

header('HTTP/1.1 400 Bad Request');
echo '<h1>Internal Monei Exception</h1>';
echo $ex->getMessage();
exit;
}

exit;
Expand Down
Loading