【问题标题】:Get data from external api从外部api获取数据
【发布时间】:2017-07-06 15:52:47
【问题描述】:

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

问题是我如何在他们付款后获取数据并在显示“谢谢”容器之前显示“等待付款”按钮?

下面是我的 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 的代码。我现在的问题是,如何在等待客户完成付款时显示“等待”消息,并在付款完成时显示“谢谢”消息。

【问题讨论】:

    标签: javascript jquery json ajax api


    【解决方案1】:

    因此,您必须提出单独的请求来检查用户是否已完成支付账单。基本上你创建一个函数:

    • 发送请求以检查帐单是否已支付
    • 如果账单支付,它会在 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 秒内再次检查账单状态。以此类推,直到付清账单。如果是,它会显示“谢谢”消息并重定向到索引。

    您将不得不依靠网关来关闭打开的窗口。

    【讨论】:

    • 我不太明白。我应该创建另一个 ajax 请求以从 api 获取数据吗?因为客户需要先付款。
    • 客户需要先付款什么?我认为您需要在客户付款后立即显示“等待”消息
    • 哦,不,客户需要先支付账单,然后我才会显示“谢谢”消息。当他们在付款页面时,将显示“等待”消息。
    • 好吧,我不太明白你的问题是什么。你能试着改写这个问题吗?或者添加更多代码。或者可能详细分步描述您的代码的作用和不工作的地方
    • 谢谢!我已按照您的指示解决了我的问题!
    猜你喜欢
    • 1970-01-01
    • 2019-11-19
    • 2016-03-11
    • 2021-04-25
    • 2017-12-06
    • 2021-06-23
    • 2015-12-04
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多