맞춤형 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'];
}
}
//...
'location.href = ...'이 아니어야합니까? – Jeff
@ 제프 : 당신이 제안한대로 시도했다; 그것은 위에 서술 한 것과 같은 결과입니다. – nyedidikeke
'JSON.stringify '는 무엇을위한 것인가? 너는 필요 없어! - 그리고 아마도 그게 문제 일 겁니다. -> json [ 'redirect']는 거짓이됩니다. – Jeff