安全上下文
此特征只可用于 安全上下文 (HTTPS),在某些或所有 支持浏览器 .

PaymentRequest 方法 canMakePayment() determines whether or not the request is configured in a way that is compatible with at least one payment method supported by the 用户代理 . You can call this before calling show() to provide a streamlined user experience when the user's browser can't handle any of the payment methods you accept.

For instance, you might call canMakePayment() to determine if the browser will let the user pay using Payment Request API, and if it won't, you could fall back to another payment method, or offer a list of methods that aren't handled by Payment Request API (or even provide instructions for paying by mail or by phone).

句法

paymentRequest.canMakePayment()
    .then( canPay => { ... })
    .catch( error => { ... })
canPay = await paymentRequest.canMakePayment();
					

返回

A Promise 布尔 that resolves to true if the user agent supports any of the payment methods supplied when instantiating the request using the PaymentRequest constructor. If the payment can't be processed, the promise receives a value of false .

注意: If you call this too often, the browser may reject the returned promise with a DOMException .

参数

None

范例

In the following example, is excerpted from a demo that asynchronously builds a PaymentRequest object for both Apple Pay and credit cards. It wraps the call to canMakePayment() in feature detection, and calls an appropriate callback depending on the resolution of the Promise .

async function initPaymentRquest() {
  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 basic card
  const supportsBasicCard = await new PaymentRequest(
    [{ supportedMethods: "basic-card" }],
    details
  ).canMakePayment();
  if (supportsBasicCard) {
    // show basic card support
    return;
  }
  // Otherwise, make payments using HTML form element
}
					

规范

规范 状态 注释
支付请求 API
The definition of 'canMakePayment()' in that specification.
候选推荐 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
canMakePayment() Chrome 61 Edge 16 Firefox 55 注意事项 Disabled
55 注意事项 Disabled
Available only in nightly builds.
Disabled ). To change preferences in Firefox, visit about:config.
IE 不支持 No Opera 不支持 No Safari 11.1 WebView Android 不支持 No Chrome Android 53 Firefox Android 55 注意事项 Disabled
55 注意事项 Disabled
Available only in nightly builds.
Disabled ). To change preferences in Firefox, visit about:config.
Opera Android 不支持 No Safari iOS 11.3 Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改: