weixin_33714884 2017-02-17 08:06 采纳率: 0%
浏览 13

从外部API获取数据

i have successful created ajax code to send data to external api (payment gateway).

The problem is how can i get data after they pay and show "waiting payment" button before display "Thank you" container ?

Below is my code for ajax post data :

$.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);
            }
        })   
}

Here is the api doc link for payment completion : BillPlz doc

But i have no idea how its work. How can i post the data and get the data back in same ajax request ?

Basically my system process is like this.

  1. Customer visit the website
  2. Customer add item that they want to buy
  3. Customer confirm the item and decide to pay via payment gateway
  4. Customer redirect to payment gateway invoice to pay
  5. System show 'waiting' message at my website while waiting customer to complete their payment.
  6. After customer complete their payment then they will back to my website and see "thank you for your payment" message.

The code i post above is a code that i use to post customer data to payment gateway api. My problem now is, how can i show "waiting" message while waiting for customer to complete their payment and show "Thank you" message when payment has been completed.

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33744141 2017-02-17 08:18
    关注

    So you will have to make a separate request to check if the user has completed paying the bill. Basically you create a function which:

    • sends a request to check if the bill is paid
    • if the bill is not paid it calls itself again in 1 second (or some other interval)
    • if the bill is paid it shows the "Thank you" message and redirects to index (or whatever you want to do after)

    Also removing async: false is probably a good idea since it blocks the browser while the request is running.

    Your code should be along the lines of:

    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,
      ...
    });
    

    So after the confirm you should show the "waiting" message, then the code opens a gateway page and sets a timeout to check the bill status in 1 second. The checkBillStatus function itself performs a bill's status check and if it is not paid, it sets timeout to check the bill status again in 1 second. And so on until the bill is paid. When it is, it displays the 'thank you' message and redirects to index.

    You will have to rely on the gateway to close the opened window.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么