PaymentResponse.complete() - Web APIs 编辑

Secure context

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

The PaymentRequest method complete() of the Payment Request API notifies the user agent that the user interaction is over, and causes any remaining user interface to be closed. This method must be called after the user accepts the payment request and the Promise returned by the PaymentRequest.show() method is resolved.

Syntax

completePromise = paymentRequest.complete(result);

Parameters

result Optional

A DOMString indicating the state of the payment operation upon completion. It must be one of the following:

success
The payment was successfully processed. The user agent may or may not present some form of "payment successful" indication to the user.
fail
The payment was not successfully processed. The failure may or may not be announced to the user by the user agent, depending on its design.
unknown
The success or failure status of the transaction is unknown or irrelevant, and the user agent should not present any notification, even if it normally would. This is the default value.

Note: In older versions of the specification, an empty string, "", was used instead of unknown to indicate a completion without a known result state. See the Browser compatibility section below for details.

Return value

A Promise which resolves with no input value once the payment interface has been fully closed. If an error occurs, the promise instead rejects, returning one of the exceptions listed below.

Exceptions

AbortError
The document in which the payment request is taking place became inactive while the user interface was shown.
InvalidStateError
The payment has already completed, or complete() was called while a request to retry the payment is pending. You can't treat a payment as complete after requesting that the payment be tried again.

Examples

The following example sends payment information to a secure server using the Fetch API. It calls complete() with an answer appropriate to the status in the response.

// Initialization of PaymentRequest arguments are excerpted for the
//   sake of brevity.
var payment = new PaymentRequest(supportedInstruments, details, options);

payment.show().then(function(paymentResponse) {
  var fetchOptions = {
    method: 'POST',
    credentials: include,
    body: JSON.stringify(paymentResponse)
  };
  var serverPaymentRequest = new Request('secure/payment/endpoint');
  fetch(serverPaymentRequest, fetchOptions).then( response => {
    if (response.status < 400) {
      paymentResponse.complete("success");
    } else {
      paymentResponse.complete("fail");
    };
  }).catch( reason => {
    paymentResponse.complete("fail");
  });
}).catch(function(err) {
  console.error("Uh oh, something bad happened", err.message);
});

Specifications

SpecificationStatusComment
Payment Request API
The definition of 'PaymentResponse: complete' in that specification.
Candidate RecommendationInitial definition.

Browser Compatibility

BCD tables only load in the browser

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

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

发布评论

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

词条统计

浏览:20 次

字数:5278

最后编辑:7年前

编辑次数:0 次

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