2017-01-15 5 views
1

私はカスタムOpenCart(バージョン:2.3.0.2)支払いモジュールを開発しています。データベースに書き込んだ後にリダイレクトできない(OpenCart)

バックエンド側(管理者用フォルダ)には、すべてが期待通りに動作しています。

バックエンド(カタログフォルダ)とは異なり、ビヘイビアフローは、$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エラーログの両方にエラーは表示されません。

私は間違っていると私はそれを修正することはできますか? - おそらくocmodまたはvqmodによって引き起こされる、または多分あなたのメール設定

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']; 
    } 
} 
//... 
+0

「location.href = ...」にしないでください。 – Jeff

+0

@ジェフ:あなたが示唆したように試みました。それは上記のナレーションと同じ結果です。 – nyedidikeke

+0

'JSON.stringify'は何ですか?あなたはそれを必要としません! - それはおそらく問題です - > json ['redirect']は偽になります – Jeff

答えて

0

はそのようなものになります。致命的なエラーが発生すると、支払いコントローラがハングアップし、Ajaxレスポンスが返される可能性があります。合理的なロギングレベルが有効になっている場合、問題の原因に関するWebサーバーのエラーログにエラーが見つかることをほとんど保証できます。

+0

受信したJSONは上記のスニペットのように表示されます。あなたが提案したように編集されました。それでも動作しません。この問題は、** $ this-> model_checkout_order-> addOrderHistory($ this-> session-> data ['order_id']、$ this-> config-data)を使用してデータベースに書き込むときに、 > get( 'my_pay_module_order_status_id')、$ reference、true); **。コントローラのスクリプトをコメントアウトすると、データはビューに渡され、リダイレクトは期待通りに機能しますが、ここで唯一の問題は、トランザクションの詳細をデータベースに保存しないことです。 – nyedidikeke

0

あなたはaddOrderHistory()メソッドのバグを持っている:受け取ったJSONを想定

+0

私は実際にそれを解決することができましたが、解決策とアプローチを投稿することはできませんでした。あなたはメール設定*の上ですが、AJAXからの致命的なエラー(** error:function()**を追加したもの)をトレースしています。 'addOrderHistory()'にバグはありません。しかし、大きな洞察力。 – nyedidikeke

+0

'addOrderHistory()'メソッドは電子メールを送信し、メール設定の問題によってハングアップする可能性があります。私はこの種の事をたくさん見たことがあり、メールは通常の容疑者です – billynoah

関連する問題