dongtiao2105 2017-11-27 11:43
浏览 80
已采纳

Prestashop支付模块返回500服务器状态

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!

  • 写回答

1条回答 默认 最新

  • duankuaiwang2706 2017-11-27 12:48
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题