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

PaymentResponse: complete() method

· 52CE 我爱测

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.

Payment Request API

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

complete()
complete(result)

Parameters

  • result
    • : A string 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 DOMException
    • : Returned if the document in which the payment request is taking place became inactive while the user interface was shown.
  • InvalidStateError DOMException
    • : Returned if 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.
const payment = new PaymentRequest(supportedInstruments, details, options);

payment
  .show()
  .then((paymentResponse) => {
    const fetchOptions = {
      method: "POST",
      credentials: include,
      body: JSON.stringify(paymentResponse),
    };
    const 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((err) => {
    console.error("Uh oh, something bad happened", err.message);
  });

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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