首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >计时器超时后加载另一个甜蜜警报窗口

计时器超时后加载另一个甜蜜警报窗口
EN

Stack Overflow用户
提问于 2018-01-19 23:00:30
回答 1查看 487关注 0票数 1

我正在做一个opencart上的支付模块。问题是,支付网关需要一个回调url,该url只能更新订单历史记录,但很难刷新浏览器。我所做的是,通过表单提交支付数据后,加载SweetAlert窗口,显示大约40秒的处理。我假设这是足够的时间让用户从他在电话上收到的确认提示进行付款。

支付完成后,支付网关与我的系统通信,确认支付是否成功,如果支付成功,则通过更新订单状态来确认支付失败。

我遇到的问题是,在40秒之后,SweetAlert关闭,后续的ajax调用没有进行。下面是我的代码:

代码语言:javascript
运行
复制
<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秒后调用的函数,用于检查订单状态并在成功时提供重定向链接

代码语言:javascript
运行
复制
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);
}
EN

回答 1

Stack Overflow用户

发布于 2018-02-23 04:17:21

我知道这是个迟来的答复但是..。如果我没弄错的话,问题出在获取服务器状态,这样你就可以更新加载的网页了。这是web开发人员面临的常见问题,问题在于http是请求响应协议,这意味着服务器必须等待客户端发出请求并对该请求做出响应,之后连接关闭,服务器继续空闲地等待下一个请求。其结果是,服务器无法将更新推送到客户端,除非客户端请求更新。

这基本上意味着您必须使用连续轮询来检查操作的状态,或者在客户端使用websockets和像mqtt (或AMQP?)这样的发布者和订阅者中间件。服务器端,以便服务器可以将状态更改推送到客户端将订阅的通道。

对于冗长的回答和缺乏快速修复代码示例,很抱歉。在我看来,最快的方法是使用轮询服务器,直到你得到响应,然而,与发布者/订阅者模型相比,这在客户端和服务器上都是资源密集型的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48343892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档