メインコンテンツへスキップ

はじめに

SendGridを使用して、支払い確認、サブスクリプションの更新、重要な通知のためのトランザクションメールを自動的に送信します。支払いイベントに基づいて、動的コンテンツとプロフェッショナルなテンプレートを使用してパーソナライズされたメールをトリガーします。
この統合には、Mail Send権限を持つSendGrid APIキーが必要です。

始め方

1

Webhookセクションを開く

Dodo Paymentsダッシュボードで、Webhooks → + エンドポイントを追加に移動し、統合のドロップダウンを展開します。
エンドポイントを追加と統合のドロップダウン
2

SendGridを選択

SendGrid統合カードを選択します。
3

APIキーを入力

設定にSendGrid APIキーを提供します。
4

変換を設定

SendGridのMail Send 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キーにMail Send権限があることを確認
  • テンプレートIDが有効でアクティブであることを確認
  • 受信者のメールアドレスが有効であることを確認
  • SendGridの送信制限とクォータを確認
  • JSON構造がSendGrid APIフォーマットに一致していることを確認
  • すべての必須フィールドが存在することを確認
  • テンプレートデータ変数が正しくフォーマットされていることを確認
  • 送信元メールアドレスがSendGridで確認されていることを確認