PaymentRequest.canMakePayment() - Web APIs 编辑

Secure context

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

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 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

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

canPay = await paymentRequest.canMakePayment();

Returns

A Promise to a Boolean 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.

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

Parameters

None

Examples

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
}

Specifications

SpecificationStatusComment
Payment Request API
The definition of 'canMakePayment()' in that specification.
Candidate RecommendationInitial definition.

Browser Compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:55 次

字数:4699

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文