2017-02-17 185 views
2

我已成功创建了将数据发送到外部api(支付网关)的ajax代码。从外部api获取数据

问题是我怎么才能得到数据后,他们支付并显示“等待付款”按钮之前显示“谢谢”容器?

下面是我对AJAX发布数据代码:

$.ajax({ 
 
      url: 'creating_bill.php', 
 
      data: { 
 
       paid_amount : JSON.stringify(jumlah_semua), 
 
       email : emel, 
 
       mobile : telefon, 
 
       name : nama 
 
      }, 
 
      type: "POST", 
 
      dataType: "json", 
 
      success: function (data) { 
 
       confirm('Terima Kasih ! Sila buat pembayaran dengan segera.'); 
 
       console.log(data)    
 
       window.open(data.url, '_blank'); 
 
       
 
       setTimeout(function() 
 
       { 
 
        window.location = 'index.html'; 
 
       },10000); 
 
      }, 
 
      async: false, 
 
      error: function(data) { 
 
       handleRequestError(data); 
 
      } 
 
     }) 
 
}

这里的API文档链接,付款完成:BillPlz doc

,但我不知道如何开展工作。我如何发布数据并获取相同的Ajax请求中的数据?

基本上我的系统过程就是这样。

  1. 客户访问网站
  2. 客户,他们希望购买
  3. 客户确认的项目,并决定通过支付网关支付
  4. 客户重定向到支付网关的发票支付
  5. 系统添加项目在等待客户完成付款的同时,在我的网站上显示“等待”信息。
  6. 客户完成付款后,他们将回到我的网站并看到“谢谢您的付款”信息。

我在上面发布的代码是我用来将客户数据发布到支付网关api的代码。我现在的问题是,如何在等待客户完成付款时显示“等待”消息,并在付款完成后显示“谢谢”消息。

回答

2

所以你将不得不作出一个单独的请求,以检查用户是否已完成支付账单。基本上创建函数其中:

  • 发送一个请求,以检查账单支付
  • 如果该法案是支付它在1秒内再次调用自身(或一些其他的时间间隔)
  • 如果该法案支付它显示了“谢谢你”的消息,并重定向到指数(或任何你想以后做)

而且消除async: false可能是一个好主意,因为它会阻止浏览器的请求运行时。

你的代码应该是沿着线:

function checkBillStatus() { 
    $.ajax({ 
    ... 
    // Compose the ajax request to check the bill status 
    ... 
    success: function (data) { 
     // Here you need to check if the bill is paid 
     if (data.isBillPaid) { 
     console.log('Remove waiting for the payment message'); 
     console.log('Show thank you for the payment message'); 
     // Payment is done, redirecting to index 
     setTimeout(function() { 
      window.location = 'index.html'; 
     },10000); 
     } else { 
     // Repeat the request after a while 
     setTimeout(checkBillStatus, 1000); 
     } 
    } 
    }); 
} 

$.ajax({ 
    ... 
    success: function (data) { 
    confirm('Terima Kasih ! Sila buat pembayaran dengan segera.'); 
    console.log('Add waiting for the payment message'); 
    console.log('User confirmed the payment, redirecting to gateway'); 
    window.open(data.url, '_blank'); 

    setTimeout(checkBillStatus, 1000); 
    }, 
    async: true, 
    ... 
}); 

所以confirm后,你应该显示“等待”消息,那么代码打开一个门户页面,并设置超时检查该法案状态1秒。 checkBillStatus函数本身执行账单状态检查,如果没有付款,它会设置超时以在1秒内再次检查账单状态。等到账单付清为止。当它是,它显示'谢谢你'的消息,并重定向到索引。您将不得不依靠网关关闭打开的窗口。

+0

我不是很明白。我应该创建另一个Ajax请求从API获取数据?因为客户需要先付款。 –

+0

客户需要先支付什么?我认为您需要在客户付款后立即显示“等待”消息 – jetpackpony

+0

哦,不行,客户需要先付账单才能显示“谢谢”信息。 “等待”消息将在付款页面显示。 –