我正在opencart上做一个支付模块。问题是支付网关需要一个只能更新订单历史但很难刷新浏览器的回调网址。我所做的是,在通过表单提交付款数据后,加载了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);
}
答案 0 :(得分:0)
我知道这是一个迟到的答案,但...... 如果我正确理解您的问题,则问题在于获取服务器状态,以便您可以更新加载的网页。这是Web开发人员面临的一个常见问题,问题在于http是一个请求响应协议,这意味着服务器必须等待客户端发出请求并响应该请求,然后连接关闭,服务器继续空闲等待下一个请求。结果是服务器无法将更新推送到客户端,除非他们要求更新。
这对您来说基本上意味着您必须使用连续轮询来检查操作的状态或使用客户端上的websockets以及服务器端的mqtt(或AMQP?)等发布者订阅者中间件这样服务器就可以将状态更改推送到客户端将订阅的频道。
对于罗嗦的答案以及缺少快速修复代码示例感到抱歉。在我看来,最快的方法是持续轮询服务器直到你得到响应,但是与发布者/订阅者模型相比,这对客户端和服务器来说都是资源密集型的。