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

PaymentResponse: payerdetailchange event

· 52CE 我爱测

A **`payerdetailchange`** event is fired by the Payment Request API to a PaymentResponse object when the user makes changes to their personal information while filling out a payment request form. This can happen when the payer is retrying to submit its details after an error has

Payment Request API

A payerdetailchange event is fired by the Payment Request API to a PaymentResponse object when the user makes changes to their personal information while filling out a payment request form. This can happen when the payer is retrying to submit its details after an error has been detected.

The event handler for payerdetailchange should check each value in the form that has changed and ensure that the values are valid. If any are invalid, appropriate error messages should be configured and the PaymentResponse.retry method should be called on the PaymentResponse to ask the user to update the invalid entries.

This event is not cancelable and does not bubble.

Syntax

Use the event name in EventTarget/addEventListener:

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

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

onpayerdetailchange = (event) => { }

Event type

A PaymentRequestUpdateEvent. Inherits from Event.

PaymentRequestUpdateEvent

Examples

In the example below, onpayerdetailchange is used to set up a listener for the payerdetailchange event (but note that this replaces other event listeners added using onpayerdetailchange, if any) in order to validate the information entered by the user, requesting that any mistakes be corrected.

// Options for PaymentRequest(), indicating that shipping address,
// payer email address, name, and phone number all be collected.

const options = {
  requestShipping: true,
  requestPayerEmail: true,
  requestPayerName: true,
  requestPayerPhone: true,
};
const request = new PaymentRequest(methods, details, options);
const response = request.show();

// Get the data from the response

let {
  payerName: oldPayerName,
  payerEmail: oldPayerEmail,
  payerPhone: oldPayerPhone,
} = response;

// Set up a handler for payerdetailchange events, to
// request corrections as needed.

response.onpayerdetailchange = async (ev) => {
  const promisesToValidate = [];
  const { payerName, payerEmail, payerPhone } = response;

  // Validate each value which changed by calling a function
  // that validates each type of data, returning a promise which
  // resolves if the data is valid.

  if (oldPayerName !== payerName) {
    promisesToValidate.push(validateName(payerName));
    oldPayerName = payerName;
  }
  if (oldPayerEmail !== payerEmail) {
    promisesToValidate.push(validateEmail(payerEmail));
    oldPayerEmail = payerEmail;
  }
  if (oldPayerPhone !== payerPhone) {
    promisesToValidate.push(validatePhone(payerPhone));
    oldPayerPhone = payerPhone;
  }

  // As each validation promise resolves, add the results of the
  // validation to the errors list

  const errors = await Promise.all(promisesToValidate).then((results) =>
    results.reduce((errors, result) => Object.assign(errors, result)),
  );

  // If we found any errors, wait for them to be corrected

  if (Object.getOwnPropertyNames(errors).length) {
    await response.retry(errors);
  } else {
    // We have a good payment; send the data to the server
    await fetch("/pay-for-things/", { method: "POST", body: response.json() });
    response.complete("success");
  }
};

await response.retry({
  payer: {
    email: "invalid domain.",
    phone: "invalid number.",
  },
});

Using addEventListener()

You could also set up the event handler using the addEventListener() method:

response.addEventListener("payerdetailchange", async (ev) => {
  // …
});

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

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

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