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

PaymentRequest: paymentmethodchange event

· 52CE 我爱测

The **`paymentmethodchange`** event is delivered the Payment Request API to a PaymentRequest object when the user changes the payment method within a given payment handler.

Payment Request API

The paymentmethodchange event is delivered the Payment Request API to a PaymentRequest object when the user changes the payment method within a given payment handler.

For example, if the user switches from one credit card to another on their Apple Pay account, a paymentmethodchange event is fired to let you know about the change.

This event is not cancelable and does not bubble.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("paymentmethodchange", (event) => { })

Alternatively, assign to the event handler property of the interface:

onpaymentmethodchange = (event) => { }

Event type

A PaymentMethodChangeEvent. Inherits from Event.

PaymentMethodChangeEvent

Examples

Let's take a look at an example. This code creates a new PaymentRequest, adds a handler for the paymentmethodchange event by calling the request's EventTarget.addEventListener, then calls PaymentRequest.show to present the payment interface to the user.

The code assumes the existence of a method detailsForTransaction(), which will return an object that can be passed as the details argument to the PaymentRequest constructor.

const paymentRequest = new PaymentRequest(
  paymentMethods,
  detailsForTransaction(),
);

paymentRequest.addEventListener("paymentmethodchange", handlePaymentChange);

paymentRequest
  .show()
  .then((response) => response.complete("success"))
  .catch((err) => console.error(`Error handling payment request: ${err}`));

The event handler function itself, handlePaymentChange(), looks like this:

handlePaymentChange = (event) => {
  const detailsUpdate = {};

  if (event.methodName === "https://apple.com/apple-pay") {
    const serviceFeeInfo = calculateServiceFee(event.methodDetails);
    Object.assign(detailsUpdate, serviceFeeInfo);
  }

  event.updateWith(detailsUpdate);
};

This begins by looking at the event's PaymentMethodChangeEvent.methodName property; if that indicates that the user is trying to use Apple Pay, we pass the PaymentMethodChangeEvent.methodDetails into a function called calculateServiceFee(), which we might create to take the information about the transaction, such as the underlying credit card being used to service the Apple Pay request, and compute and return an object that specifies changes to be applied to the PaymentRequest in order to add any service fees that the payment method might require.

Before the event handler returns, it calls the event's PaymentRequestUpdateEvent.updateWith() method to integrate the changes into the request.

Specifications

Browser compatibility

See also

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

资料来源与适用范围

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

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

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