跳转到主要内容

介绍

使用 SendGrid 自动发送支付确认、订阅更新和重要通知的事务性电子邮件。根据支付事件触发个性化电子邮件,使用动态内容和专业模板。
此集成需要具有邮件发送权限的 SendGrid API 密钥。

开始使用

1

打开 Webhook 部分

在您的 Dodo Payments 仪表板中,导航到 Webhooks → + 添加端点 并展开集成下拉菜单。
添加端点和集成下拉菜单
2

选择 SendGrid

选择 SendGrid 集成卡片。
3

输入 API 密钥

在配置中提供您的 SendGrid API 密钥。
4

配置转换

编辑转换代码以格式化 SendGrid 的邮件发送 API 的电子邮件。
5

测试并创建

使用示例有效负载进行测试,然后单击 创建 以激活电子邮件发送。
6

完成!

🎉 付款事件现在将自动通过 SendGrid 触发事务性电子邮件。

转换代码示例

付款确认电子邮件

payment_confirmation.js
function handler(webhook) {
  if (webhook.eventType === "payment.succeeded") {
    const p = webhook.payload.data;
    webhook.url = "https://api.sendgrid.com/v3/mail/send";
    webhook.payload = {
      personalizations: [
        {
          to: [{ email: p.customer.email }],
          dynamic_template_data: {
            customer_name: p.customer.name,
            payment_amount: (p.total_amount / 100).toFixed(2),
            payment_id: p.payment_id,
            payment_date: new Date(webhook.payload.timestamp).toLocaleDateString(),
            currency: p.currency || "USD"
          }
        }
      ],
      from: {
        email: "[email protected]",
        name: "Your Company"
      },
      template_id: "d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    };
  }
  return webhook;
}

订阅欢迎电子邮件

subscription_welcome.js
function handler(webhook) {
  if (webhook.eventType === "subscription.active") {
    const s = webhook.payload.data;
    webhook.url = "https://api.sendgrid.com/v3/mail/send";
    webhook.payload = {
      personalizations: [
        {
          to: [{ email: s.customer.email }],
          dynamic_template_data: {
            customer_name: s.customer.name,
            subscription_id: s.subscription_id,
            product_name: s.product_id,
            amount: (s.recurring_pre_tax_amount / 100).toFixed(2),
            frequency: s.payment_frequency_interval,
            next_billing: new Date(s.next_billing_date).toLocaleDateString()
          }
        }
      ],
      from: {
        email: "[email protected]",
        name: "Your Company"
      },
      template_id: "d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    };
  }
  return webhook;
}

付款失败通知

payment_failure.js
function handler(webhook) {
  if (webhook.eventType === "payment.failed") {
    const p = webhook.payload.data;
    webhook.url = "https://api.sendgrid.com/v3/mail/send";
    webhook.payload = {
      personalizations: [
        {
          to: [{ email: p.customer.email }],
          dynamic_template_data: {
            customer_name: p.customer.name,
            payment_amount: (p.total_amount / 100).toFixed(2),
            error_message: p.error_message || "Payment processing failed",
            payment_id: p.payment_id,
            retry_link: `https://yourdomain.com/retry-payment/${p.payment_id}`
          }
        }
      ],
      from: {
        email: "[email protected]",
        name: "Your Company Support"
      },
      template_id: "d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    };
  }
  return webhook;
}

提示

  • 使用 SendGrid 动态模板以实现个性化内容
  • 在模板变量中包含相关的支付数据
  • 设置正确的发件地址和发件人名称
  • 使用模板 ID 以保持电子邮件格式一致
  • 包含退订链接以确保合规性

故障排除

  • 验证 API 密钥是否具有邮件发送权限
  • 检查模板 ID 是否有效且处于活动状态
  • 确保收件人电子邮件地址有效
  • 查看 SendGrid 的发送限制和配额
  • 验证 JSON 结构是否符合 SendGrid API 格式
  • 检查所有必需字段是否存在
  • 确保模板数据变量格式正确
  • 验证发件电子邮件地址是否在 SendGrid 中经过验证