W3cubDocs

/DOM

PaymentRequest.canMakePayment

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

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The PaymentRequest.canMakePayment() method of the PaymentRequest interface indicates whether the PaymentRequest object can make a payment before calling show(). It returns a Promise resolves to true if the user agent supports any of the payment methods supplied to the constructor, and false otherwise. if this method is called too often, the spec allows the user agents to return a promise rejected with a DOMException.

Syntax

PaymentRequest.canMakePayment()
    .then( boolean => { ... })
    .catch( error => { ... })

Returns

A Promise to a Boolean that resolves to true if the user agent supports any of the payment methods supplied in the PaymentRequest constructor.

Parameters

None

Examples

In the following example, is excerpted from a demo that asynchronously builds a PaymentRequest object for both Android 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

Specification Status Comment
Payment Request API
The definition of 'canMakePayment()' in that specification.
Candidate Recommendation Initial definition.

Browser CompatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 61
Disabled
61
Disabled
Disabled From version 61: this feature is behind the #web-payments preference (needs to be set to Enabled). To change preferences in Chrome, visit chrome://flags.
16 55
Disabled
55
Disabled
Disabled From version 55: this feature is behind the dom.payments.request.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No No ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support No 56
Disabled
56
Disabled
Disabled From version 56: this feature is behind the #web-payments preference (needs to be set to Enabled). To change preferences in Chrome, visit chrome://flags.
Yes 55
Disabled
55
Disabled
Disabled From version 55: this feature is behind the dom.payments.request.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No ? 6.0

See Also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/PaymentRequest/canMakePayment