跳转到主要内容

介绍

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

开始使用

1

Open the Webhook Section

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

Select SendGrid

选择 SendGrid 集成卡片。
3

Enter API Key

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

Configure Transformation

编辑转换代码以将电子邮件格式化为 SendGrid 的 Mail Send API 所需的格式。
5

Test & Create

使用示例负载进行测试,然后点击 Create 激活电子邮件发送。
6

Done!

🎉 付款事件现在将自动通过 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: "payments@yourdomain.com",
        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: "welcome@yourdomain.com",
        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: "support@yourdomain.com",
        name: "Your Company Support"
      },
      template_id: "d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    };
  }
  return webhook;
}

提示

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

故障排除

  • 验证 API 密钥是否具有 Mail Send 权限
  • 检查模板 ID 是否有效且处于启用状态
  • 确保收件人电子邮箱地址有效
  • 查看 SendGrid 的发送限制和配额
  • 验证 JSON 结构是否与 SendGrid API 格式一致
  • 检查所有必填字段是否存在
  • 确保模板数据变量格式正确
  • 确认发件人邮箱地址已在 SendGrid 中通过验证