PaymentRequest:canMakePayment() 方法
PaymentRequest 方法 canMakePayment() 可用於確定請求的配置方式是否與 使用者代理 支援的至少一種支付方式相容。
您可以在呼叫 show() 之前呼叫此方法,以便在使用者瀏覽器無法處理您接受的任何支付方式時,提供更流暢的使用者體驗。
例如,您可以呼叫 canMakePayment() 來確定瀏覽器是否允許使用者使用 Payment Request API 進行支付,如果不行,您可以回退到另一種支付方式,或者提供一種不支援 Payment Request API 的支付方式列表(甚至提供郵寄或電話支付的說明)。
語法
js
canMakePayment()
引數
無。
返回值
一個 Promise,其值為布林值。如果使用者代理支援在使用 PaymentRequest 建構函式例項化請求時提供的任何支付方式,則此 Promise 會解析為 true。如果無法處理付款,則 Promise 會收到一個值 false。
注意: 如果您呼叫此方法的頻率過高,瀏覽器可能會使用 DOMException 拒絕返回的 Promise。
示例
以下示例是 摘自一個演示 的內容,該演示非同步構建了一個同時支援 Apple Pay 和 Example Pay 的 PaymentRequest 物件。它將 canMakePayment() 的呼叫包裝在功能檢測中,並根據 Promise 的解析結果呼叫適當的回撥函式。
js
async function initPaymentRequest() {
const details = {
total: {
label: "Total",
amount: {
currency: "USD",
value: "0.00",
},
},
};
const supportsApplePay = new PaymentRequest(
[{ supportedMethods: "https://apple.com/apple-pay" }],
details,
).canMakePayment();
// Supports Apple Pay?
if (await supportsApplePay) {
// show Apple Pay logo, for instance
return;
}
// Otherwise, let's see if we can use Example Pay
const supportsExamplePay = await new PaymentRequest(
[{ supportedMethods: "https://example.com/pay" }],
details,
).canMakePayment();
if (supportsExamplePay) {
// show Example Pay support
return;
}
// Otherwise, make payments using HTML form element
}
規範
| 規範 |
|---|
| Payment Request API # dom-paymentrequest-canmakepayment |
瀏覽器相容性
載入中…