> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dodopayments.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Inngest

> 根据 Dodo Payments 事件触发 Inngest 中的无服务器函数和后台作业。

## 介绍

在支付事件发生时自动执行无服务器函数和后台作业。使用 Inngest 可靠的函数执行平台处理支付、发送通知、更新数据库和运行复杂的工作流。

<Info>
  此集成需要您从函数配置中获取 Inngest webhook URL。
</Info>

## 开始使用

<Steps>
  <Step title="Open the Webhook Section">
    在您的 Dodo Payments 仪表板中，导航到 <b>Webhooks → + 添加端点</b> 并展开集成下拉菜单。

    <Frame>
      <img src="https://mintcdn.com/dodopayments/slbAEdrLLwKHfaRf/images/integrations/inngest.png?fit=max&auto=format&n=slbAEdrLLwKHfaRf&q=85&s=b160678c00ceed38439160b4b253c8f3" alt="添加端点和集成下拉菜单" style={{ maxHeight: '500px', width: 'auto' }} width="1646" height="944" data-path="images/integrations/inngest.png" />
    </Frame>
  </Step>

  <Step title="Select Inngest">
    选择 <b>Inngest</b> 集成卡片。
  </Step>

  <Step title="Create Inngest Function">
    在 Inngest 中，创建一个新函数，并从函数配置中复制 webhook URL。
  </Step>

  <Step title="Paste Webhook URL">
    将 Inngest webhook URL 粘贴到端点配置中。
  </Step>

  <Step title="Configure Transformation">
    编辑转换代码以为您的 Inngest 函数格式化事件。
  </Step>

  <Step title="Test & Create">
    使用示例负载进行测试，然后点击 <b>Create</b> 以激活集成。
  </Step>

  <Step title="Done!">
    🎉 付款事件现在将自动触发您的 Inngest 函数。
  </Step>
</Steps>

## 转换代码示例

### 基本事件有效负载

```javascript basic_event.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "payment.succeeded") {
    const p = webhook.payload.data;
    webhook.payload = {
      name: "payment.succeeded",
      data: {
        payment_id: p.payment_id,
        amount: (p.total_amount / 100).toFixed(2),
        currency: p.currency || "USD",
        customer_email: p.customer.email,
        customer_name: p.customer.name,
        payment_method: p.payment_method || "unknown"
      },
      user: {
        email: p.customer.email
      },
      ts: Math.floor(new Date(webhook.payload.timestamp).getTime() / 1000)
    };
  }
  return webhook;
}
```

### 订阅事件处理程序

```javascript subscription_event.js icon="js" expandable theme={null}
function handler(webhook) {
  const s = webhook.payload.data;
  switch (webhook.eventType) {
    case "subscription.active":
      webhook.payload = {
        name: "subscription.started",
        data: {
          subscription_id: s.subscription_id,
          customer_email: s.customer.email,
          customer_name: s.customer.name,
          product_id: s.product_id,
          amount: (s.recurring_pre_tax_amount / 100).toFixed(2),
          frequency: s.payment_frequency_interval,
          next_billing: s.next_billing_date
        },
        user: {
          email: s.customer.email
        },
        ts: Math.floor(new Date(webhook.payload.timestamp).getTime() / 1000)
      };
      break;
    case "subscription.cancelled":
      webhook.payload = {
        name: "subscription.cancelled",
        data: {
          subscription_id: s.subscription_id,
          customer_email: s.customer.email,
          cancelled_at: s.cancelled_at,
          cancel_at_next_billing: s.cancel_at_next_billing_date
        },
        user: {
          email: s.customer.email
        },
        ts: Math.floor(new Date(webhook.payload.timestamp).getTime() / 1000)
      };
      break;
  }
  return webhook;
}
```

### 争议事件处理程序

```javascript dispute_event.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType.startsWith("dispute.")) {
    const d = webhook.payload.data;
    webhook.payload = {
      name: webhook.eventType,
      data: {
        dispute_id: d.dispute_id,
        payment_id: d.payment_id,
        amount: (d.amount / 100).toFixed(2),
        status: d.dispute_status,
        stage: d.dispute_stage,
        remarks: d.remarks || "",
        urgent: webhook.eventType === "dispute.opened"
      },
      user: {
        email: d.customer?.email || "unknown"
      },
      ts: Math.floor(new Date(webhook.payload.timestamp).getTime() / 1000)
    };
  }
  return webhook;
}
```

## 常见的 Inngest 用例

<AccordionGroup>
  <Accordion title="Payment Processing">
    * 发送确认邮件
    * 更新客户记录
    * 处理退款
    * 生成发票
    * 更新库存
  </Accordion>

  <Accordion title="Subscription Management">
    * 欢迎新订阅者
    * 处理取消
    * 发送续订提醒
    * 更新账单周期
    * 处理付款失败
  </Accordion>

  <Accordion title="Analytics & Reporting">
    * 更新收入指标
    * 跟踪客户行为
    * 生成报告
    * 将数据同步到分析平台
    * 计算流失率
  </Accordion>
</AccordionGroup>

## 提示

* 使用描述性事件名称以更好地组织函数
* 包含用户上下文以执行函数
* 为事件排序设置适当的时间戳
* 在事件之间保持数据结构一致
* 使用 Inngest 的重试和错误处理功能

## 故障排除

<AccordionGroup>
  <Accordion title="Functions not triggering">
    * 核实 webhook URL 是否正确且处于激活状态
    * 检查 Inngest 函数是否已部署且处于激活状态
    * 确保事件名称与函数触发器匹配
    * 查看 Inngest 函数日志以查找错误
  </Accordion>

  <Accordion title="Data not received correctly">
    * 检查负载结构是否符合 Inngest 的预期
    * 验证事件名称格式是否正确
    * 确保包含所有必填字段
    * 使用 Inngest 的 webhook 测试工具进行测试
  </Accordion>
</AccordionGroup>
