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

# Discord

> Gửi thông báo Dodo Payments theo thời gian thực đến các kênh Discord của bạn.

## Giới thiệu

Giữ cho đội ngũ của bạn được thông báo nơi họ đã hợp tác. Tích hợp Discord cung cấp thông tin về thanh toán, đăng ký, tranh chấp và các sự kiện quan trọng khác trực tiếp đến bất kỳ kênh nào bạn chọn—không cần polling hay bảng điều khiển.

<Info>
  Hướng dẫn này giả định bạn có quyền truy cập phần Tích hợp trong bảng điều khiển Dodo Payments.
</Info>

## Bắt đầu

<Steps>
  <Step title="Open the Webhook Section">
    Trong bảng điều khiển Dodo Payments của bạn, mở <b>Webhooks → + Thêm Endpoint</b> và mở rộng menu thả xuống để hiển thị các tích hợp.

    <Frame>
      <img src="https://mintcdn.com/dodopayments/slbAEdrLLwKHfaRf/images/integrations/discord.png?fit=max&auto=format&n=slbAEdrLLwKHfaRf&q=85&s=dd9359ae214e43ca779c1c0a522f055b" alt="Thêm Endpoint và menu tích hợp thả xuống" style={{ maxHeight: '500px', width: 'auto' }} width="1808" height="938" data-path="images/integrations/discord.png" />
    </Frame>
  </Step>

  <Step title="Select Discord">
    Chọn thẻ <b>Discord</b> rồi nhấp <b>Connect your Discord workspace</b>.
  </Step>

  <Step title="Authorize the Bot">
    Cấp quyền yêu cầu để bot có thể đăng tin nhắn lên kênh bạn chọn.
  </Step>

  <Step title="Edit Transformation Code">
    Tùy chỉnh ánh xạ payload → embed theo nhu cầu của bạn — hoặc bắt đầu với các mẫu bên dưới.
  </Step>

  <Step title="Test & Create">
    Sử dụng payload mẫu để xem trước embed, rồi nhấn <b>Create</b>.
  </Step>

  <Step title="Done!">
    🎉 Kênh Discord của bạn sẽ bắt đầu nhận các cập nhật Dodo Payments theo thời gian thực.
  </Step>
</Steps>

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

### Nhúng Thanh toán Tối thiểu

```javascript payment_embed.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "payment.succeeded") {
    const p = webhook.payload.data;
    webhook.payload = {
      embeds: [{
        title: "✅ Payment Successful",
        description: `**Amount:** $${(p.total_amount / 100).toFixed(2)}\n**Customer:** ${p.customer.email}`,
        color: 0x2ecc71 // green
      }]
    };
  }
  return webhook;
}
```

### Sự kiện Đăng ký

```javascript subscription_embed.js icon="js" expandable theme={null}
function handler(webhook) {
  const s = webhook.payload.data;
  switch (webhook.eventType) {
    case "subscription.active":
      webhook.payload = {
        embeds: [{
          title: "📄 Subscription Activated",
          fields: [
            { name: "Customer", value: s.customer.email, inline: true },
            { name: "Product", value: s.product_id, inline: true },
            { name: "Next Billing", value: new Date(s.next_billing_date).toLocaleDateString(), inline: true }
          ],
          color: 0x2ecc71
        }]
      };
      break;
    case "subscription.cancelled":
      webhook.payload = {
        embeds: [{
          title: "⚠️ Subscription Cancelled",
          fields: [
            { name: "Customer", value: s.customer.email, inline: true },
            { name: "Product", value: s.product_id, inline: true }
          ],
          color: 0xf1c40f
        }]
      };
      break;
  }
  return webhook;
}
```

### Cảnh báo Tranh chấp

```javascript dispute_embed.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType.startsWith("dispute.")) {
    const d = webhook.payload.data;
    webhook.payload = {
      embeds: [{
        title: d.dispute_status === "won" ? "🏆 Dispute Won" : d.dispute_status === "lost" ? "❌ Dispute Lost" : "🚨 Dispute Update",
        fields: [
          { name: "Payment ID", value: d.payment_id, inline: true },
          { name: "Amount", value: `$${(d.amount / 100).toFixed(2)}`, inline: true },
          { name: "Status", value: d.dispute_status, inline: true }
        ],
        color: d.dispute_status === "won" ? 0x2ecc71 : d.dispute_status === "lost" ? 0xe74c3c : 0xe67e22
      }]
    };
  }
  return webhook;
}
```

## Mẹo

* Ưu tiên nhúng cho định dạng phong phú và màu sắc.
* Giữ tiêu đề ngắn gọn; đưa chi tiết vào các trường.
* Sử dụng màu sắc trực quan: xanh (thành công), đỏ (thất bại), cam (cảnh báo).

## Khắc phục sự cố

<AccordionGroup>
  <Accordion title="No messages in Discord">
    * Xác nhận bot đã có quyền truy cập vào kênh.
    * Kiểm tra xem phép biến đổi có trả về đối tượng JSON với `embeds`.
  </Accordion>

  <Accordion title="Transformation errors">
    * Xác thực mã của bạn trong trình chỉnh sửa – lỗi cú pháp sẽ chặn việc gửi.
    * Đảm bảo tên trường khớp với cấu trúc payload của webhook.
  </Accordion>
</AccordionGroup>
