写入数据库后无法重定向(OpenCart)

时间:2017-01-15 18:56:22

标签: php jquery redirect opencart opencart-module

我正在开发自定义OpenCart(版本:2.3.0.2)付款模块。

在后端(admin文件夹),我按预期运行完美。

与后端(目录文件夹)不同,行为流按预期工作,除非我使用 $this->model_checkout_order->addOrderHistory() 辅助函数将事务详细信息记录到数据库中;我可以成功地将详细信息写入数据库,但登录后我的重定向失败了。
因此,当我在上面注释帮助函数(用于向数据库写入付款细节)时,我的重定向工作有效。

我使用jQuery处理我的重定向,从我的目录控制器支付扩展中传递给定位置的URL。

以下是位于 opencart/catalog/controller/extension/payment/my-pay-module.php 的目录控制器付款扩展程序文件的内容:

<?php
class ControllerExtensionPaymentMyPayModule extends Controller {
    public function index() {
        $this->load->language('extension/payment/my-pay-module');

        $data['text_payment_details'] = $this->language->get('text_payment_details');
        $data['text_loading'] = $this->language->get('text_loading');

        $data['entry_issuer_hint'] = $this->language->get('entry_issuer_hint');
        $data['entry_instrument'] = $this->language->get('entry_instrument');

        $data['button_confirm'] = $this->language->get('button_confirm');
        $data['button_back'] = $this->language->get('button_back');

        return $this->load->view('extension/payment/my-pay-module', $data);
    }

    public function send() {
        $this->load->model('checkout/order');

        $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);

        $request  = 'merchant_id=' . urlencode($this->config->get('my_pay_module_merchant_id'));
        $request .= '&order_id=' . urlencode($this->session->data['order_id']);
        $request .= '&total_amount=' . urlencode($this->currency->format($order_info['total'], $order_info['currency_code'], 1.00000, false));
        $request .= '&instrument=' . urlencode($this->request->post['instrument']);
        $request .= '&issuer_hint=' . urlencode($this->request->post['issuer_hint']);
        $request .= '&user_phone_no=' . urlencode($order_info['telephone']);
        $request .= '&user_email=' . urlencode($order_info['email']);

        $curl = curl_init('https://example.com/v1/api');

        curl_setopt($curl, CURLOPT_PORT, 443);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $request);

        $response = curl_exec($curl);// JSON response: {"status":"200","success":"true","message":"Payment received successfully."}

        curl_close($curl);

        // Create response object
        $custPayResponse = array(
            'data'       => json_decode($response)
        );

        // Define redirect URL for success
        $success = array(
            'redirect'   => $this->url->link('checkout/success', '', true)
        );

        // Define redirect URL for failure
        $failed = array(
            'redirect'   => $this->url->link('checkout/failed', '', true)
        );

        // Check success and write to OpenCart
        if ($custPayResponse['data']->success == true) {

            // Set transaction details
            $reference = '';

            $reference .= 'Issuer Hint: ';

            if (isset($response)) {
                $reference .= $this->request->post['issuer_hint'] . "\n";
            }

            $reference .= 'Instrument: ';

            if (isset($response)) {
                $reference .= $this->request->post['instrument'] . "\n";
            }

            // Write to database
            $this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('my_pay_module_order_status_id'), $reference, true);

            // Merge response object and redirect URL (success)
            $GLOBALS['params'] = array_merge($custPayResponse, $success);
        } else {
            // Merge response object and redirect URL (failure)
            $GLOBALS['params'] = array_merge($custPayResponse, $failed);
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($GLOBALS['params']));
    }
}

以及我的目录视图文件的内容位于: opencart/catalog/view/theme/default/template/extension/payment/my-pay-module.tpl

<form class="form-horizontal">
  <fieldset id="payment">
    <legend><?php echo $text_payment_details; ?></legend>
    <div class="form-group required">
      <label class="col-sm-2 control-label" for="input-issuer-hint"><?php echo $entry_issuer_hint; ?></label>
      <div class="col-sm-10">
        <label class="radio-inline"><input type="radio" name="issuer_hint">Option1</label>
        <label class="radio-inline"><input type="radio" name="issuer_hint">Option2</label>
        <label class="radio-inline"><input type="radio" name="issuer_hint">Option3</label>
      </div>
    </div>
    <div class="form-group required">
      <label class="col-sm-2 control-label" for="input-pymt-instrument"><?php echo $entry_instrument; ?></label>
      <div class="col-sm-10">
        <input type="text" name="instrument" placeholder="<?php echo $entry_instrument; ?>" id="input-instrument" class="form-control" />
      </div>
    </div>
  </fieldset>
</form>
<div class="buttons">
  <div class="pull-right">
    <input type="button" value="<?php echo $button_confirm; ?>" id="button-confirm" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-primary" />
  </div>
</div>
<script type="text/javascript">
$('#button-confirm').bind('click', function() {
    $.ajax({
        url: 'index.php?route=extension/payment/my-pay-module/send',
        type: 'post',
        data: $('#payment :input'),
        dataType: 'json',
        cache: false,
        beforeSend: function() {
            $('#button-confirm').button('loading');
        },
        complete: function() {
            $('#button-confirm').button('reset');
        },
        success: function(json) {
            console.log(json);
            // console.log(JSON.stringify(json));

            if (json['redirect']) {
                // Sample expected result: http://localhost/opencart/index.php?route=checkout/success
                // Using a full location for that reason; not location.href as result is not as desired.
                location = json['redirect'];
            }
        }
    });
});
</script>

我在浏览器的控制台和OpenCart错误日志中都没有出错。

我做错了什么,我可以解决它吗?

2 个答案:

答案 0 :(得分:0)

假设收到的json看起来像那样:

json = {"redirect": "page2.html", "param2": "foo"};

//....
success: function(json) {
    console.log(json);
    // JSON.stringify(json); // you wanna keep the json, json['redirect'] will return a string anyway

    if (json['redirect']) {
        // you need to set location.href here, not the whole location object!
        location.href = json['redirect'];
    }
}
//...

答案 1 :(得分:0)

您在addOrderHistory()方法中遇到了一个错误 - 可能是由ocmod或vqmod引起的,或者可能是您的邮件设置引起的。任何致命错误都可能导致支付控制器挂起并且不提供ajax响应。如果启用了合理的日志记录级别,我几乎可以保证您在网络服务器的错误日志中发现与问题来源相关的错误。