added order message logging
This commit is contained in:
@@ -37,50 +37,41 @@ class HutkoCallbackModuleFrontController extends ModuleFrontController
|
||||
}
|
||||
try {
|
||||
// 1. Parse the incoming request body.
|
||||
$requestBody = $this->getRequestBody();
|
||||
|
||||
// If request body is empty, log and exit.
|
||||
if (empty($requestBody)) {
|
||||
PrestaShopLogger::addLog('Hutko Callback: Empty request body received.', 2, null, 'Cart', null, true);
|
||||
exit('Empty request');
|
||||
}
|
||||
// Assuming validateResponse returns true on success, or a string error message on failure.
|
||||
$isSignatureValid = $this->module->validateResponse($requestBody);
|
||||
if ($isSignatureValid !== true) {
|
||||
PrestaShopLogger::addLog('Hutko Callback: Invalid signature. Error: ' . $isSignatureValid, 2, null, 'Cart', null, true);
|
||||
exit('Invalid signature');
|
||||
}
|
||||
// PrestaShopLogger::addLog('Hutko Callback: ' . json_encode($requestBody), 1);
|
||||
$calbackContent = $this->getCallbackContent();
|
||||
|
||||
|
||||
$transaction_id = $requestBody['order_id'];
|
||||
$orderIdParamParts = explode($this->module->order_separator, $transaction_id);
|
||||
$orderId = (int)$orderIdParamParts[0]; // Ensure it's an integer
|
||||
|
||||
|
||||
$id_order_parts = explode($this->module->order_separator, $calbackContent['order_id']);
|
||||
$id_order = (int)$id_order_parts[0]; // Ensure it's an integer
|
||||
|
||||
// If we reached here, an order should exist. Load it.
|
||||
$order = new Order($orderId);
|
||||
$order = new Order($id_order);
|
||||
if (!Validate::isLoadedObject($order)) {
|
||||
PrestaShopLogger::addLog('Hutko Callback: Order could not be loaded for ID: ' . $orderId, 3, null, 'Order', $orderId, true);
|
||||
exit('Order not found after validation');
|
||||
PrestaShopLogger::addLog('Hutko Callback: Order could not be loaded for ID: ' . $id_order, 3, null, 'Order', $id_order, true);
|
||||
throw new Exception('Order not found after validation');
|
||||
}
|
||||
$this->context->currency = new Currency($order->id_currency);
|
||||
$this->context->customer = new Customer($order->id_customer);
|
||||
$this->context->language = new Language($order->id_lang);
|
||||
|
||||
// 7. Handle payment status from the callback.
|
||||
$orderStatusCallback = $requestBody['order_status'];
|
||||
$orderStatusCallback = $calbackContent['order_status'];
|
||||
$currentOrderState = (int)$order->getCurrentState();
|
||||
|
||||
switch ($orderStatusCallback) {
|
||||
case 'approved':
|
||||
// Only success state if no refunds was done.
|
||||
if ($requestBody['response_status'] == 'success' && (int)$requestBody['reversal_amount'] === 0) {
|
||||
if ($calbackContent['response_status'] == 'success' && (int)$calbackContent['reversal_amount'] === 0) {
|
||||
$expectedState = (int)Configuration::get('HUTKO_SUCCESS_STATUS_ID', null, null, null, Configuration::get('PS_OS_PAYMENT'));
|
||||
// Only change state if it's not already the success state or "Payment accepted".
|
||||
|
||||
if ($currentOrderState !== $expectedState) {
|
||||
$this->module->addPayment($requestBody, $order);
|
||||
$callbackAmount = $calbackContent['actual_amount'] ?? $calbackContent['amount'];
|
||||
$amountFloat = round($callbackAmount / 100, 2);
|
||||
$order->addOrderPayment($amountFloat, $this->module->displayName, $calbackContent['order_id'], $this->context->currency);
|
||||
$order->setCurrentState($expectedState);
|
||||
}
|
||||
} else {
|
||||
PrestaShopLogger::addLog('Hutko Callback: Unhandled response_status: ' . $requestBody['response_status']);
|
||||
}
|
||||
exit('OK');
|
||||
break;
|
||||
@@ -103,42 +94,44 @@ class HutkoCallbackModuleFrontController extends ModuleFrontController
|
||||
break;
|
||||
|
||||
case 'processing':
|
||||
// If the order is still processing, we might want to update its status
|
||||
// to a specific 'processing' state if available, or just acknowledge.
|
||||
// For now, if it's not already in a success/error state, set it to 'processing'.
|
||||
$processingState = (int)Configuration::get('PS_OS_PAYMENT'); // Or a custom 'processing' state
|
||||
if ($currentOrderState !== $processingState && $currentOrderState !== (int)Configuration::get('HUTKO_SUCCESS_STATUS_ID') && $currentOrderState !== (int)Configuration::get('PS_OS_ERROR')) {
|
||||
$order->setCurrentState($processingState);
|
||||
}
|
||||
exit('Processing');
|
||||
//no need to change status
|
||||
exit('Order ' . $orderStatusCallback);
|
||||
break;
|
||||
|
||||
default:
|
||||
// Log unexpected status and exit with an error.
|
||||
PrestaShopLogger::addLog('Hutko Callback: Unexpected order status received: ' . $orderStatusCallback . ' for order ID: ' . $orderId, 3, null, 'Order', $orderId, true);
|
||||
exit('Unexpected status');
|
||||
PrestaShopLogger::addLog('Hutko Callback: Unexpected order status received: ' . $orderStatusCallback . ' for order ID: ' . $id_order, 3, null, 'Order', $id_order, true);
|
||||
throw new Exception('Unexpected status');
|
||||
break;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// Log any uncaught exceptions and exit with the error message.
|
||||
PrestaShopLogger::addLog('Hutko Callback Error: ' . $e->getMessage(), 3, null, 'HutkoCallbackModuleFrontController', null, true);
|
||||
exit($e->getMessage());
|
||||
throw new Exception('Unknown error');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to parse the request body from POST or raw input.
|
||||
* Helper method to parse the request body from raw input.
|
||||
*
|
||||
* @return array The parsed request body.
|
||||
*/
|
||||
private function getRequestBody(): array
|
||||
private function getCallbackContent(): array
|
||||
{
|
||||
|
||||
$jsonBody = json_decode(file_get_contents("php://input"), true);
|
||||
if (is_array($jsonBody)) {
|
||||
return $jsonBody;
|
||||
$calbackContent = json_decode(file_get_contents("php://input"), true);
|
||||
if (!is_array($calbackContent) || !count($calbackContent)) {
|
||||
PrestaShopLogger::addLog('Hutko Callback: Empty request body received.', 2, null, 'Cart', null, true);
|
||||
throw new Exception('Empty request');
|
||||
}
|
||||
|
||||
return [];
|
||||
// Assuming validateResponse returns true on success, or a string error message on failure.
|
||||
$isSignatureValid = $this->module->validateResponse($calbackContent);
|
||||
if ($isSignatureValid !== true) {
|
||||
PrestaShopLogger::addLog('Hutko Callback: Invalid signature. Error: ' . $isSignatureValid, 2, null, 'Cart', null, true);
|
||||
throw new Exception('Invalid signature');
|
||||
}
|
||||
if (Configuration::get('HUTKO_SAVE_LOGS')) {
|
||||
$this->module->log('CalbackContent: ' .json_encode($calbackContent));
|
||||
}
|
||||
return $calbackContent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
||||
*/
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncode;
|
||||
|
||||
if (!defined('_PS_VERSION_')) {
|
||||
exit;
|
||||
@@ -59,5 +60,5 @@ class HutkoRedirectModuleFrontController extends ModuleFrontController
|
||||
'hutko_response' => $responseData['response'], // The URL of the Hutko payment gateway.
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user