# @dodopayments/express 中文文档

规范地址：https://npmcn.com/package/%40dodopayments/express/

版本：0\.2\.12
npm 元数据更新时间：2026-11-02T18:16:57.489Z
中文译文更新时间：2026-11-12T16:04:46.686Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40dodopayments/express
原文对照：https://npmcn.com/package/%40dodopayments/express/?lang=original
项目主页：https://github.com/dodopayments/dodo-adapters#readme
源码仓库：https://github.com/dodopayments/dodo-adapters

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：2.4 MB，10 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2025-07-22T06:45:27.371Z
- 最新版本发布：2026-11-02T18:16:57.107Z
- 近 30 天下载量：2847（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：17（近 12 个月 10 个）

## 中文简介

一个类型标记库, 用于导出 Handlers for Checkout, Customer Portal, 以及 Webhook 路由, 以方便与您的快件集成 .

## 安装

```sh
npm install @dodopayments/express
```

也可以使用：`pnpm add @dodopayments/express`、`yarn add @dodopayments/express`、`bun add @dodopayments/express`

## README

# `@dodopayments/express`

一个类型标记库, 用于导出 Handlers for Checkout, Customer Portal, 以及 Webhook 路由, 以方便与您的快件集成 .

> **AI代理整合指南:** 关于AI助手的详细指示和指导,见下文AI代理即时部分.

## 文档

详细文件可在 [Dodo 支付快递适配器](https://docs.dodopayments.com/developer-resources/express-adaptor)

## 安装

您可以通过 npm 或您选择的任何其他软件包管理器安装此软件包 :

```bash
npm install @dodopayments/express
```

## 快速启动

### 1\. 离职

```typescript
import { checkoutHandler } from "@dodopayments/express";

app.get(
  "/api/checkout",
  checkoutHandler({
    bearerToken: process.env.DODO_PAYMENTS_API_KEY,
    returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
    environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
    type: "static",
  }),
);

app.post(
  "/api/checkout",
  checkoutHandler({
    bearerToken: process.env.DODO_PAYMENTS_API_KEY,
    returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
    environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
    type: "dynamic",
  }),
);

app.post(
  "/api/checkout",
  checkoutHandler({
    bearerToken: process.env.DODO_PAYMENTS_API_KEY,
    returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
    environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
    type: "session",
  }),
);
```

* * *

### 2\. 客户门户路线处理器

```typescript
import { CustomerPortal } from "@dodopayments/express";

app.get(
  "/api/customer-portal",
  CustomerPortal({
    bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
    environment: "test_mode",
  }),
);
```

#### 查询参数

-   `customer_id` (需要):门户会话的客户身份(例如, `?customer_id=cus_123`(中文(简体) )
-   `send_email` (可选,布尔): 如果设置为 `true`,向客户发送带有门户链接的电子邮件.

如果 `customer_id` 缺少.

> **警卫:** 此处理器不认证呼叫者 。 它为任何事物建立了一个门户会话 `customer_id` 在查询字符串中,该门户链接可以访问客户的订阅、付款和账单细节。 只挂载在您自己的认证后, 并检查登录用户是否拥有请求 `customer_id` 在操作员运行之前.

* * *

### 3\. Webhook 路由处理器

```typescript
// app/api/webhook/dodo-payments/route.ts
import { Webhooks } from "@dodopayments/express";

app.post(
  "/api/webhook",
  Webhooks({
    webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_SECRET!,
    onPayload: async (payload) => {
      // handle the payload
    },
    // ... other event handlers for granular control
  }),
);
```

* * *

## 启动 LLM

```
You are an expert Express.js developer assistant. Your task is to guide a user through integrating the @dodopayments/express adapter into their existing Express.js project.

The @dodopayments/express adapter provides route handlers for Dodo Payments' Checkout, Customer Portal, and Webhook functionalities, designed to plug directly into an Express app.

First, install the necessary package. Use the package manager appropriate for the user's project (npm, yarn, or bun):

npm install @dodopayments/express

---

Here's how you should structure your response:

1. Ask the user which functionalities they want to integrate.

"Which parts of the @dodopayments/express adapter would you like to integrate into your project? You can choose one or more of the following:

- Checkout Route Handler (for handling product checkouts)
- Customer Portal Route Handler (for managing customer subscriptions/details)
- Webhook Route Handler (for receiving Dodo Payments webhook events)
- All (integrate all three)"

---

2. Based on the user's selection, provide detailed integration steps for each chosen functionality.

---

**If Checkout Route Handler is selected:**

**Purpose**: This handler manages different types of checkout flows. All checkout types (static, dynamic, and sessions) return JSON responses with checkout URLs for programmatic handling.

**Integration**:
Create routes in your Express app for static (GET), dynamic (POST), and checkout sessions (POST).


import { checkoutHandler } from '@dodopayments/express';

app.get('/api/checkout', checkoutHandler({
  bearerToken: process.env.DODO_PAYMENTS_API_KEY,
  returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
  environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
  type: "static"
}));

app.post('/api/checkout', checkoutHandler({
  bearerToken: process.env.DODO_PAYMENTS_API_KEY,
  returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
  environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
  type: "dynamic"
}));

// For checkout sessions
app.post('/api/checkout', checkoutHandler({
  bearerToken: process.env.DODO_PAYMENTS_API_KEY,
  returnUrl: process.env.DODO_PAYMENTS_RETURN_URL,
  environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
  type: "session"
}));

Config Options:

    bearerToken: Your Dodo Payments API key (recommended to be stored in DODO_PAYMENTS_API_KEY env variable).

    returnUrl (optional): URL to redirect the user after successful checkout.

    environment: "test_mode" or "live_mode"

    type: "static" (GET), "dynamic" (POST), or "session" (POST)

GET (static checkout) expects query parameters:

    productId (required)

    quantity, customer fields (fullName, email, etc.), and metadata (metadata_*) are optional.

    Returns: {"checkout_url": "https://checkout.dodopayments.com/..."}

POST (dynamic checkout) expects a JSON body with payment details (one-time or subscription). Reference the docs for the full POST schema:

    One-time payments: https://docs.dodopayments.com/api-reference/payments/post-payments

    Subscriptions: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions

    Returns: {"checkout_url": "https://checkout.dodopayments.com/..."}

POST (checkout sessions) - (Recommended) A more customizable checkout experience:

    Expects a JSON body with product_cart array and customer details.

    One-time payments: https://docs.dodopayments.com/api-reference/payments/post-payments

    Subscriptions: https://docs.dodopayments.com/api-reference/subscriptions/post-subscriptions

    Required fields for checkout sessions:
        product_cart (array): Array of products with product_id and quantity

    Returns: {"checkout_url": "https://checkout.dodopayments.com/session/..."}

If Customer Portal Route Handler is selected:

Purpose: This route allows customers to manage their subscriptions via the Dodo Payments portal.

Integration:

import { CustomerPortal } from "@dodopayments/express";

app.get('/api/customer-portal', CustomerPortal({
  bearerToken: process.env.DODO_PAYMENTS_API_KEY,
  environment: process.env.DODO_PAYMENTS_ENVIRONMENT,
}));

Query Parameters:

    customer_id (required): e.g., ?customer_id=cus_123

    send_email (optional): if true, customer is emailed the portal link

Returns 400 if customer_id is missing.

Security: this handler does not authenticate the caller. Protect the route with the app's existing authentication and verify that the signed-in user owns the requested customer_id before the handler runs.

If Webhook Route Handler is selected:

Purpose: Processes incoming webhook events from Dodo Payments to trigger events in your app.

Integration:

import { Webhooks } from "@dodopayments/express";

app.post('/api/webhook', Webhooks({
  webhookKey: process.env.DODO_PAYMENTS_WEBHOOK_KEY,
  onPayload: async (payload) => {
    // Handle generic payload
  },
  // You can also provide fine-grained handlers for each event type below
}));

Features:

    Only POST method is allowed — others return 405

    Signature verification is performed using webhookKey. Returns 401 if invalid.

    Zod-based payload validation. Returns 400 if invalid schema.

    All handlers are async functions.

Supported Webhook Event Handlers:

You may pass in any of the following handlers:

    onPayload

    onPaymentSucceeded

    onPaymentFailed

    onPaymentProcessing

    onPaymentCancelled

    onRefundSucceeded

    onRefundFailed

    onDisputeOpened, onDisputeExpired, onDisputeAccepted, onDisputeCancelled, onDisputeChallenged, onDisputeWon, onDisputeLost

    onSubscriptionActive, onSubscriptionOnHold, onSubscriptionRenewed, onSubscriptionPlanChanged, onSubscriptionCancelled, onSubscriptionFailed, onSubscriptionExpired, onSubscriptionUpdated

    onLicenseKeyCreated

    onAbandonedCheckoutDetected, onAbandonedCheckoutRecovered

    onDunningStarted, onDunningRecovered

    onCreditAdded, onCreditDeducted, onCreditExpired, onCreditRolledOver, onCreditRolloverForfeited, onCreditOverageCharged, onCreditManualAdjustment, onCreditBalanceLow

Environment Variable Setup:

Make sure to define these environment variables in your project:

DODO_PAYMENTS_API_KEY=your-api-key
DODO_PAYMENTS_WEBHOOK_KEY=your-webhook-secret
DODO_PAYMENTS_ENVIRONMENT="test_mode" or "live_mode"
DODO_PAYMENTS_RETURN_URL=your-return-url

Use these inside your code as:

process.env.DODO_PAYMENTS_API_KEY
process.env.DODO_PAYMENTS_WEBHOOK_SECRET

Security Note: Do NOT commit secrets to version control. Use .env files locally and secrets managers in deployment environments (e.g., AWS, Vercel, Heroku, etc.).
```

## 常见问题

### @dodopayments/express 是什么？

一个类型标记库, 用于导出 Handlers for Checkout, Customer Portal, 以及 Webhook 路由, 以方便与您的快件集成 \.

### 如何安装 @dodopayments/express？

在项目目录运行 npm install @dodopayments/express，或使用 pnpm add / yarn add / bun add。

### @dodopayments/express 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @dodopayments/express 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @dodopayments/express 可以商用吗？

@dodopayments/express 使用 未声明 许可。package\.json 没有声明许可证。没有许可证的代码默认保留全部权利，商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括，不构成法律意见，请以许可原文为准。

### @dodopayments/express 的下载量有多少？

根据 npm 官方下载统计，@dodopayments/express 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,847 次，日均约 95 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @dodopayments/express 还在维护吗？

最新版本 0\.2\.12 发布于 2026/11/03（10 天前）；至今共发布 17 个版本，其中过去 12 个月发布了 10 个；首次发布于 2025/07/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

