2017-02-17 184 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

哦,不行,客戶需要先付賬單才能顯示「謝謝」信息。 「等待」消息將在付款頁面顯示。 –