PaymentRequest: paymentmethodchange event - Web APIs 编辑

paymentmethodchange events are delivered by the Payment Request API to a PaymentRequest object when the user changes payment methods 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.

BubblesNo
CancelableNo
InterfacePaymentMethodChangeEvent
Event handler propertyonpaymentmethodchange

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 addEventListener(), then calls show() to present the payment interface to the user.

The code assumes the existence of a method detailsForShipping(), which returns a PaymentDetailsUpdate object containing the shipping options for the ground shipping method, in the form found in the PaymentShippingOption dictionary. By doing so, the payment form defaults to the ground shipping method.

const options = {
  requestShipping: true
};

const paymentRequest = new PaymentRequest(paymentMethods,
      detailsForShipping("ground"), options);

paymentRequest.addEventListener("paymentmethodchange", handlePaymentChange, false);

paymentRequest.show()
.then(response => response.complete("success"))
.catch(err => console.log("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);
}, false);

This begins by looking at the event's methodName property; if that indicates that the user is trying to use Apple Pay, we pass the 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 PaymentDetailsUpdate 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 PaymentMethodChangeEvent.updateWith.updateWith() method to integrate the changes into the request.

Specifications

SpecificationStatusComment
Payment Request API
The definition of 'paymentmethodchange' in that specification.
Candidate Recommendation 

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

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

词条统计

浏览:82 次

字数:6908

最后编辑:6 年前

编辑次数:0 次

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