大网数据广告活动
Web 标准与前端开发

PaymentRequest: canMakePayment() method

· 52CE 我爱测

The PaymentRequest method **`canMakePayment()`** determines whether or not the request is configured in a way that is compatible with at least one payment method supported by the user agent.

Payment Request API

The PaymentRequest method canMakePayment() determines whether or not the request is configured in a way that is compatible with at least one payment method supported by the user agent.

You can call this before calling PaymentRequest.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).

Syntax

canMakePayment()

Parameters

None.

Return value

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

[!NOTE] If you call this too often, the browser may reject the returned promise with a DOMException.

Examples

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

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
}

Specifications

Browser compatibility

See also

  • PaymentRequest.show

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。