I'm trying to figure out how to give a good response status to my API shot which was made to the prestashop. That's the code of validation.php:
<?php
class InpayValidationModuleFrontController extends ModuleFrontController
{
/**
* @see FrontController::postProcess()
*/
public function postProcess()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['invoiceCode'] && $_POST['status'] && $_POST['optData']) {
$apiHash = $_SERVER['HTTP_API_HASH'];
$query = http_build_query($_POST);
$hash = hash_hmac("sha512", $query, $this->module->secret_key);
if ($apiHash == $hash) {
PrestaShopLogger::addLog(json_encode(_PS_VERSION_), 1);
parse_str($_POST['optData'], $optData);
$id_cart = intval($optData['cartId']);
$query = "SELECT * from " . _DB_PREFIX_ . "orders where id_cart='" . $id_cart . "'";
//$query = "SELECT * from aps_orders where id_cart='67867'";
$row = Db::getInstance()->getRow($query);
if ($_POST['status'] == 'confirmed' && $row['current_state'] != null) {
$sql = "UPDATE " . _DB_PREFIX_ . "orders SET current_state='2' WHERE id_cart='" . $id_cart . "'";
if(Db::getInstance()->Execute($sql))
Tools::redirect(__FILE__,'payment_confirmation.tpl');
} else {
$cart = new Cart($id_cart);
if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 || !$this->module->active) {
//die('Cannot create order for this cart.');
Tools::redirect(__FILE__,'payment_cart_error.tpl');
}
$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer)) {
//die('No customer for this order.');
Tools::redirect(__FILE__,'payment_customer_error.tpl');
}
$currency = new Currency((int)($cart->id_currency));
$paid_amount = $_POST['amount'];
$order_amount = $cart->getOrderTotal(true, Cart::BOTH);
if ($_POST['status'] == 'confirmed') {
$paymentId = 2;
} elseif ($_POST['status'] == 'received') {
$paymentId = 11;
}
$result = $this->module->validateOrder(
$cart->id,
//Configuration::get('PS_OS_PAYMENT'),
$paymentId,
$order_amount,
$this->module->displayName,
'Invoice Code: ' . $_POST['invoiceCode'],
array(),
intval($currency->id),
false,
$customer->secure_key
);
//die($result);
Tools::redirect(__FILE__,'payment_confirmation.tpl');
}
} else {
return null;
}
}
}
}
While I use the die();
function the server returns 200 response status which is good... but the die isn't a function for production... When using redirect it gives me 302 and additional status of error. I tried doing hacks like:
header("HTTP/1.1 200 OK");
or
return http_response_code(200);
But the status is 500. I would appreciate any tip or help how to do that.
Cheers!
You are trying to redirect directly to the tpl file. You should redirect to a controller or url. For exampl, in the module cheque, in the payment.php there is:
Tools::redirect(Context::getContext()->link->getModuleLink('cheque', 'payment'));
or in validation.php:
Tools::redirect('index.php?controller=order-confirmation&id_cart='.(int)$cart->id.'&id_module='.(int)$this->module->id.'&id_order='.$this->module->currentOrder.'&key='.$customer->secure_key);
Also, Tools::redirect is defined as:
redirect($url, $base_uri = __PS_BASE_URI__, Link $link = null, $headers = null)
On the other hand, you can be trying to use the display function used in modules (for example):
return $this->display(__FILE__, 'file.tpl');
But in this case you should use the assign and setTemplate:
$this->context->smarty->assign(array(/* array of vars to use in tpl*/));
$this->setTemplate('file.tpl');