Chuyển đến nội dung chính

Giới thiệu

Tự động gửi email giao dịch cho xác nhận thanh toán, cập nhật đăng ký và thông báo quan trọng bằng cách sử dụng SendGrid. Kích hoạt email cá nhân hóa dựa trên các sự kiện thanh toán với nội dung động và mẫu chuyên nghiệp.
Việc tích hợp này yêu cầu một SendGrid API Key với quyền gửi mail (Mail Send).

Bắt đầu

1

Open the Webhook Section

Trong bảng điều khiển Dodo Payments của bạn, hãy điều hướng đến Webhooks → + Add Endpoint và mở rộng menu tích hợp.
Add Endpoint and integrations dropdown
2

Select SendGrid

Chọn thẻ tích hợp SendGrid.
3

Enter API Key

Cung cấp SendGrid API Key trong phần cấu hình.
4

Configure Transformation

Chỉnh sửa mã chuyển đổi để định dạng email phù hợp với SendGrid Mail Send API.
5

Test & Create

Kiểm tra bằng các payload mẫu và nhấp Create để kích hoạt việc gửi email.
6

Done!

🎉 Các sự kiện thanh toán giờ sẽ tự động kích hoạt các email giao dịch thông qua SendGrid.

Ví dụ Mã Biến đổi

Email Xác nhận Thanh toán

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;
}

Email Chào mừng Đăng ký

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;
}

Thông báo Thất bại Thanh toán

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;
}

Mẹo

  • Sử dụng mẫu động của SendGrid cho nội dung cá nhân hóa
  • Bao gồm dữ liệu thanh toán liên quan trong các biến mẫu
  • Đặt địa chỉ gửi và tên người gửi phù hợp
  • Sử dụng ID mẫu để định dạng email nhất quán
  • Bao gồm liên kết hủy đăng ký để tuân thủ

Khắc phục sự cố

  • Xác minh API Key có quyền Mail Send
  • Kiểm tra xem các ID mẫu có hợp lệ và đang hoạt động không
  • Đảm bảo địa chỉ email người nhận hợp lệ
  • Xem lại giới hạn và hạn ngạch gửi của SendGrid
  • Xác thực cấu trúc JSON phù hợp với định dạng API của SendGrid
  • Kiểm tra tất cả các trường bắt buộc đều có mặt
  • Đảm bảo các biến dữ liệu mẫu được định dạng đúng
  • Xác minh địa chỉ email người gửi đã được SendGrid xác thực