opencart で支払いモジュールを実行しています。問題は、支払いゲートウェイがコールバック URL を必要とすることです。この URL は注文履歴のみを更新できますが、ブラウザを更新するのは困難です。私が行ったことは、フォームを介して支払いデータを送信した後、SweetAlert ウィンドウが読み込まれ、40 秒ほど処理が表示されることです。これは、ユーザーが確認のために電話で受け取ったプロンプトから支払いを行うのに十分な時間であると思います.
支払いを行った後、支払いゲートウェイはシステムと通信して、支払いが成功したか失敗したかを確認し、成功した場合は注文ステータスを更新します。
私が抱えている問題は、40 秒後に SweetAlert が終了し、その後の ajax 呼び出しが行われないことです。これが私のコードです:
<script>
$(document).ready(function(){
$(document).on('click', '#mpesaPay', function(e){
var telephone = document.getElementById("lipanampesa_phone").value;
SwalConfirm(telephone);
e.preventDefault();
});
});
function SwalConfirm(telephone){
var telephone = telephone;
var message = 'Proceed to make payment with ' + telephone + '?';
swal({
title: 'Confirm Mobile Number',
text: message,
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Continue',
showLoaderOnConfirm: true,
preConfirm: function() {
return new Promise(function(resolve) {
$.ajax({
url: 'index.php?route=extension/payment/mpesa/simulation',
type: 'POST',
data: $('#lipa-na-mpesa :input'),
dataType: 'json',
cache: false,
})
.done(function(response){
swal({
title: "Processing, Please Wait",
text: "Check your phone NOW and enter your M-Pesa PIN to complete payment.",
showConfirmButton: false,
allowOutsideClick: false,
timer: 40000,
onOpen: function () {
swal.showLoading()
}
})
.then(function(json){
$.ajax({
type: "post",
url: "index.php?route=extension/payment/mpesa/confirm",
data: $('#lipa-na-mpesa :input'),
dataType: 'json',
cache: false,
})
.done(function(json) {
if (json['success']) {
location = json['success'];
}
if (json['error']['warning']) {
swal({
type: 'warning',
title: 'Payment Failed',
text: 'Please restart the checkout process again or contact us if there is a problem'
})
}
});
})
})
.fail(function(){
swal('Oops...', 'Something went wrong with ajax !', 'error');
});
});
},
allowOutsideClick: false
});
}
</script>
バックエンド コードは正常に機能しており、支払いが成功すると注文ステータスが更新されます。checkout/success
注文が支払われたことを確認した後、ユーザーをアクションにリダイレクトする方法のみが必要です。
これは、注文ステータスを確認し、成功した場合はリダイレクト リンクを提供するために 40 秒が経過した後に呼び出されることになっている関数です。
public function confirm() {
$this->load->model('checkout/order');
if (isset($this->request->post['order_id'])) {
$order_info = $this->model_checkout_order->getOrder($this->request->post['order_id']);
$order_status_id = $order_info['order_status_id'];
if ($order_status_id == $this->config->get('payment_mpesa_order_status_id')) {
$json['success'] = $this->url->link('checkout/success', '', true);
} else {
$json['error']['warning'] = 'Payment not received yet.';
}
} else {
$json['error']['warning'] = 'Payment not received yet.';
}
return json_encode($json);
}