> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/tosspay/tosspay.authorize.md).

# TossPay.authorize

> 函数式 API `checkoutPayment`也可以通过它使用相同的功能。函数式 API 与 v2 相同 `{ params: { payToken } }` 会接收这种形式的参数。

### 功能说明

会打开 TossPay 支付窗口并执行用户认证。认证完成后会返回是否成功。

实际的支付批准需要在服务器端 `payToken`单独处理。

### 类型

```ts
TossPay.authorize(params: CheckoutPaymentOptions): Promise<CheckoutPaymentResult>;
```

**参数**

```ts
type CheckoutPaymentOptions = {
  /** 支付令牌。 */
  payToken: string;
};
```

**响应**

```ts
type CheckoutPaymentResult = {
  /** 是否认证成功。 */
  success: boolean;
  /** 认证失败时的原因。 */
  reason?: string;
};
```

### 示例代码

```js
import { TossPay } from "@apps-in-toss/web-framework";

async function handlePayment() {
  try {
    // 实际实现时，请替换为负责创建支付的 API 端点。
    const { payToken } = await fetch("/my-api/payment/create").then((res) =>
      res.json(),
    );

    const { success, reason } = await TossPay.authorize({ payToken });

    if (success) {
      // 实际实现时，请替换为执行支付的 API 端点。
      await fetch("/my-api/payment/execute", {
        method: "POST",
        body: JSON.stringify({ payToken }),
        headers: { "Content-Type": "application/json" },
      });
    } else {
      console.log("认证失败：", reason);
    }
  } catch (error) {
    console.error("支付认证过程中发生了错误：", error);
  }
}

document.querySelector("#pay-button").addEventListener("click", handlePayment);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/tosspay/tosspay.authorize.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
