2017-01-15 7 views
1

맞춤형 OpenCart (버전 : 2.3.0.2) 결제 모듈을 개발하고 있습니다.데이터베이스 (OpenCart) 작성 후 리디렉션 할 수 없습니다

백엔드 쪽 (admin 폴더)에서 예상대로 완벽하게 실행되고 있습니다.

백 엔드 (카탈로그 폴더)와 달리 동작 흐름은 $this->model_checkout_order->addOrderHistory() 도우미 기능을 사용하여 트랜잭션 세부 정보를 데이터베이스에 기록 할 때를 제외하고 예상대로 작동합니다. 내가 성공적으로 세부 정보를 데이터베이스에 쓸 수는 있지만 로그인 후 리디렉션은 트랜잭션이 실패합니다.
이와 같이 리디렉션은 위에서 설명한 헬퍼 기능 (데이터베이스로 지불 세부 정보를 작성하는 데 사용)을 주석 처리 할 때 효과적으로 작동합니다.

내 리디렉션을 jQuery로 처리하여 카탈로그 컨트롤러 지불 확장에서 지정된 위치의 URL을 전달합니다.

<?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/catalog/controller/extension/payment/my-pay-module.php에있는 내 카탈로그 컨트롤러 지불 확장 파일의 내용입니다

브라우저의 콘솔과 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

는 그런 일을 보인다. 치명적인 오류가 있으면 지불 컨트롤러가 중단되어 아약스 응답이 제공되지 않을 수 있습니다. 합리적인 로깅 수준이 설정된 경우 문제의 출처와 관련된 웹 서버의 오류 로그에 오류가 있음을 거의 보장 할 수 있습니다.

+0

받은 JSON은 위의 스 니펫과 같습니다. 당신이 제안한대로 편집; 여전히 작동하지 않습니다. 문제는 $'-> 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에서 치명적인 오류 (** 오류 : function() **이 추가됨)를 추적하여 추적했습니다. 'addOrderHistory()'에 버그가 없습니다. 좋은 통찰력. – nyedidikeke

+0

'addOrderHistory()'메소드는 메일을 보내므로 모든 메일 설정의 문제로 인해 이메일이 중단됩니다. 나는 이런 종류의 일을 여러 번 보았고 우편은 보통의 용의자 다. – billynoah