> ## 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.

# 循环

> 通过基于 Dodo Payments 事件的 Loops 发送事务性电子邮件并管理客户沟通。

## 介绍

在支付事件发生时自动发送事务性电子邮件并管理客户沟通。通过 Loops 的电子邮件基础设施发送支付确认、订阅更新和重要通知。

<Info>
  此集成需要您的 Loops API 密钥进行身份验证。
</Info>

## 开始使用

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

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

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

  <Step title="Enter API Key">
    在配置中提供您的 Loops API 密钥。
  </Step>

  <Step title="Configure Transformation">
    编辑转换代码以格式化适用于 Loops API 的电子邮件。
  </Step>

  <Step title="Test & Create">
    使用示例有效负载进行测试，然后点击 <b>Create</b> 以激活电子邮件发送。
  </Step>

  <Step title="Done!">
    🎉 付款事件现已通过 Loops 自动触发事务性电子邮件。
  </Step>
</Steps>

## 转换代码示例

### 支付确认电子邮件

```javascript payment_confirmation.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "payment.succeeded") {
    const p = webhook.payload.data;
    webhook.url = "https://api.loops.so/v1/events/send";
    webhook.payload = {
      eventName: "payment_confirmation",
      email: p.customer.email,
      properties: {
        customer_name: p.customer.name,
        payment_id: p.payment_id,
        amount: (p.total_amount / 100).toFixed(2),
        currency: p.currency || "USD",
        payment_method: p.payment_method || "unknown",
        payment_date: new Date(webhook.payload.timestamp).toLocaleDateString()
      }
    };
  }
  return webhook;
}
```

### 订阅欢迎电子邮件

```javascript subscription_welcome.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "subscription.active") {
    const s = webhook.payload.data;
    webhook.url = "https://api.loops.so/v1/events/send";
    webhook.payload = {
      eventName: "subscription_welcome",
      email: s.customer.email,
      properties: {
        customer_name: s.customer.name,
        subscription_id: s.subscription_id,
        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
      }
    };
  }
  return webhook;
}
```

### 支付失败通知

```javascript payment_failure.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "payment.failed") {
    const p = webhook.payload.data;
    webhook.url = "https://api.loops.so/v1/events/send";
    webhook.payload = {
      eventName: "payment_failed",
      email: p.customer.email,
      properties: {
        customer_name: p.customer.name,
        payment_id: p.payment_id,
        amount: (p.total_amount / 100).toFixed(2),
        error_message: p.error_message || "Payment processing failed",
        retry_link: `https://yourdomain.com/retry-payment/${p.payment_id}`
      }
    };
  }
  return webhook;
}
```

## 提示

* 使用描述性事件名称以更好地组织电子邮件模板
* 包含相关客户属性以实现个性化
* 在 Loops 仪表板中为每个事件设置电子邮件模板
* 在事件之间使用一致的属性命名
* 在上线前测试电子邮件发送

## 故障排除

<AccordionGroup>
  <Accordion title="Emails not being sent">
    * 验证 API 密钥是否正确且处于激活状态
    * 检查事件名称是否与 Loops 模板匹配
    * 确保收件人电子邮件地址有效
    * 审查 Loops 发送限制和配额
  </Accordion>

  <Accordion title="Transformation errors">
    * 验证 JSON 结构是否符合 Loops API 格式
    * 检查所有必填字段是否存在
    * 确保事件名称格式正确
    * 验证 API 密钥权限
  </Accordion>
</AccordionGroup>
