LogoMkSaaS Docs

Notification

Learn how to set up and use notifications in MkSaaS

MkSaaS supports sending notifications when a user successfully completes a payment. This allows your team to receive real-time purchase alerts in your preferred tools. Currently, we support Discord and Feishu. More notification channels (such as Slack, or Telegram) will be supported in the future. The modular design makes it easy to integrate additional services as needed.

Setup

To enable notifications, you need to configure the following environment variables in your .env file:

.env
DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..."
FEISHU_WEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/..."

How to Obtain Webhook URLs

Discord

  1. Go to your Discord server and open the channel where you want to receive notifications.
  2. Click the gear icon to open Channel Settings.
  3. Select Integrations > Webhooks > New Webhook.
  4. Set a name and (optionally) an avatar for the webhook.
  5. Copy the Webhook URL and paste it into your .env file as DISCORD_WEBHOOK_URL.

For more details, see the Discord Webhooks Guide.

Feishu

  1. Go to your Feishu group chat.
  2. Click the group name > Group Settings > Bot Management.
  3. Add a new Custom Bot and enable Webhooks.
  4. Copy the generated Webhook URL and paste it into your .env file as FEISHU_WEBHOOK_URL.

For more details, see the Feishu Webhook Documentation.

If you are setting up the environment, now you can go back to the Environment Setup guide and continue. The rest of this guide can be read later.

Environment Setup

Set up environment variables


Notification System Structure

The notification system in MkSaaS is modular and easy to extend:

discord.ts
feishu.ts
notification.ts
  • discord.ts: Sends purchase notifications to Discord using the configured webhook.
  • feishu.ts: Sends purchase notifications to Feishu using the configured webhook.
  • notification.ts: Central entry point to send notifications to all enabled channels.

Core Features

  • Real-time notifications for successful payments
  • Supports Discord and Feishu out of the box
  • Easy to add more notification channels in the future
  • Robust error handling to avoid interrupting payment flow

Usage

To send a notification after a successful payment, use the following utility:

import { sendNotification } from '@/notification';

// Example usage after payment success
await sendNotification(sessionId, customerId, userName, amount);

This will automatically send messages to both Discord and Feishu if their respective webhook URLs are configured.

Message Format

The notification messages include the following information:

  • Username: The customer's username
  • Amount: The purchase amount in dollars
  • Customer ID: Stripe customer ID for reference
  • Session ID: Stripe checkout session ID for tracking
  • Timestamp: When the purchase occurred

Discord Message

Discord notifications are sent as rich embeds with a green color and structured fields for easy reading.

Feishu Message

Feishu notifications are sent as plain text messages with all the purchase details formatted clearly.

Customization & Extensibility

The notification system is designed to be extensible. To add a new notification channel:

  1. Create a new file in the src/notification directory (e.g., slack.ts).
  2. Implement a function to send messages to the new channel:
src/notification/slack.ts
export async function sendMessageToSlack(
  sessionId: string,
  customerId: string,
  userName: string,
  amount: number
): Promise<void> {
  try {
    const webhookUrl = process.env.SLACK_WEBHOOK_URL;

    if (!webhookUrl) {
      console.warn('SLACK_WEBHOOK_URL is not set, skipping Slack notification');
      return;
    }

    // Your Slack message implementation
    // ...
  } catch (error) {
    console.error('<< Failed to send Slack notification:', error);
  }
}
  1. Import and call this function in notification.ts:
src/notification/notification.ts
import { sendMessageToSlack } from './slack';

export async function sendNotification(
  sessionId: string,
  customerId: string,
  userName: string,
  amount: number
): Promise<void> {
  // Existing Discord and Feishu calls
  await sendMessageToDiscord(sessionId, customerId, userName, amount);
  await sendMessageToFeishu(sessionId, customerId, userName, amount);

  // Add new Slack notification
  await sendMessageToSlack(sessionId, customerId, userName, amount);
}

Best Practices

  1. Keep Webhook URLs Secret: Never expose your webhook URLs publicly or commit them to version control.
  2. Monitor Delivery: Check your notification channels to ensure messages are delivered as expected.
  3. Error Handling: The system logs errors but does not interrupt the payment flow if a notification fails.
  4. Rate Limiting: Be aware of webhook rate limits for each service to avoid being blocked.
  5. Testing: Test your webhooks in a development environment before deploying to production.
  6. Extensibility: Follow the existing pattern when adding new notification channels.

Next Steps

Now that you understand how to work with notifications in MkSaaS, explore these related integrations: