PaymentRequest.onpaymentmethodchange

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.

For some payment handlers (e.g., Apple Pay), the onpaymentmethodchange event of the PaymentRequest interface is fired whenever the user swiches payment instrument (e.g., switches from a credit card to a store card).

Syntax

PaymentRequest.addEventListener('paymentmethodchange', paymentmethodchangeEvent => { ... });

PaymentRequest.onpaymentmethodchange = function(paymentmethodchangeEvent) { ... };

Examples

The paymentmethodchange event is triggered by a user-agent controlled interaction (i.e., the end-user switches from a debit to a credit card in the payment UI). To make sure you receive the event, you should add event listeners to PaymentRequest object after instantiation, but before you call show().

request.onpaymentmethodchange = ev => {
  const { type: cardType } = ev.methodDetails;
  const newStuff = {};
  if (ev.methodName === "https://apple.com/apple-pay") {
    switch (cardType) {
      case "store":
        // do Apple Pay specific handling for store card...
        // methodDetails contains the store card information
        const result = calculateDiscount(ev.methodDetails);
        Object.assign(newStuff, result);
        break;
    }
  }
  // finally...
  ev.updateWith(newStuff);
};
const response = await request.show();

 

Specifications

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

Browser Compatibility

No compatibility data found. Please contribute data for "api.PaymentRequest.onpaymentmethodchange" (depth: 1) to the MDN compatibility data repository.

Document Tags and Contributors

Contributors to this page: marcoscaceres
Last updated by: marcoscaceres,