feat: support send message to feishu
This commit is contained in:
parent
a7a5a8a6a4
commit
e610fe7335
@ -127,9 +127,15 @@ NEXT_PUBLIC_DATAFAST_ANALYTICS_DOMAIN=""
|
|||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Discord
|
# Discord
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
DISCORD_WEBHOOK_URL=""
|
||||||
NEXT_PUBLIC_DISCORD_WIDGET_SERVER_ID=""
|
NEXT_PUBLIC_DISCORD_WIDGET_SERVER_ID=""
|
||||||
NEXT_PUBLIC_DISCORD_WIDGET_CHANNEL_ID=""
|
NEXT_PUBLIC_DISCORD_WIDGET_CHANNEL_ID=""
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
# Feishu
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
FEISHU_WEBHOOK_URL=""
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Affiliate
|
# Affiliate
|
||||||
# https://mksaas.com/docs/affiliate
|
# https://mksaas.com/docs/affiliate
|
||||||
|
@ -67,7 +67,6 @@ export async function sendMessageToDiscord(
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// throw new Error(`Discord webhook request failed with status ${response.status}`);
|
|
||||||
console.error(
|
console.error(
|
||||||
`<< Failed to send Discord notification for user ${userName}:`,
|
`<< Failed to send Discord notification for user ${userName}:`,
|
||||||
response
|
response
|
55
src/notification/feishu.ts
Normal file
55
src/notification/feishu.ts
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* Send a message to Feishu when a user makes a purchase
|
||||||
|
* @param sessionId The Stripe checkout session ID
|
||||||
|
* @param customerId The Stripe customer ID
|
||||||
|
* @param userName The username of the customer
|
||||||
|
* @param amount The purchase amount in the currency's main unit (e.g., dollars, not cents)
|
||||||
|
*/
|
||||||
|
export async function sendMessageToFeishu(
|
||||||
|
sessionId: string,
|
||||||
|
customerId: string,
|
||||||
|
userName: string,
|
||||||
|
amount: number
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const webhookUrl = process.env.FEISHU_WEBHOOK_URL;
|
||||||
|
|
||||||
|
if (!webhookUrl) {
|
||||||
|
console.warn(
|
||||||
|
'FEISHU_WEBHOOK_URL is not set, skipping Feishu notification'
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format the message
|
||||||
|
const message = {
|
||||||
|
msg_type: 'text',
|
||||||
|
content: {
|
||||||
|
text: `🎉 New Purchase\nUsername: ${userName}\nAmount: $${amount.toFixed(2)}\nCustomer ID: \`${customerId}\`\nSession ID: \`${sessionId}\``,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Send the webhook request
|
||||||
|
const response = await fetch(webhookUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(message),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error(
|
||||||
|
`<< Failed to send Feishu notification for user ${userName}:`,
|
||||||
|
response
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`<< Successfully sent Feishu notification for user ${userName}`
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('<< Failed to send Feishu notification:', error);
|
||||||
|
// Don't rethrow the error to avoid interrupting the payment flow
|
||||||
|
}
|
||||||
|
}
|
24
src/notification/notification.ts
Normal file
24
src/notification/notification.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { sendMessageToDiscord } from './discord';
|
||||||
|
import { sendMessageToFeishu } from './feishu';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a notification when a user makes a purchase
|
||||||
|
* @param sessionId The Stripe checkout session ID
|
||||||
|
* @param customerId The Stripe customer ID
|
||||||
|
* @param userName The username of the customer
|
||||||
|
* @param amount The purchase amount in the currency's main unit (e.g., dollars, not cents)
|
||||||
|
*/
|
||||||
|
export async function sendNotification(
|
||||||
|
sessionId: string,
|
||||||
|
customerId: string,
|
||||||
|
userName: string,
|
||||||
|
amount: number
|
||||||
|
): Promise<void> {
|
||||||
|
console.log('sendNotification', sessionId, customerId, userName, amount);
|
||||||
|
|
||||||
|
// Send message to Discord channel
|
||||||
|
await sendMessageToDiscord(sessionId, customerId, userName, amount);
|
||||||
|
|
||||||
|
// Send message to Feishu group
|
||||||
|
await sendMessageToFeishu(sessionId, customerId, userName, amount);
|
||||||
|
}
|
@ -1,12 +1,12 @@
|
|||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
import { getDb } from '@/db';
|
import { getDb } from '@/db';
|
||||||
import { payment, session, user } from '@/db/schema';
|
import { payment, session, user } from '@/db/schema';
|
||||||
import { sendMessageToDiscord } from '@/lib/discord';
|
|
||||||
import {
|
import {
|
||||||
findPlanByPlanId,
|
findPlanByPlanId,
|
||||||
findPlanByPriceId,
|
findPlanByPriceId,
|
||||||
findPriceInPlan,
|
findPriceInPlan,
|
||||||
} from '@/lib/price-plan';
|
} from '@/lib/price-plan';
|
||||||
|
import { sendNotification } from '@/notification/notification';
|
||||||
import { desc, eq } from 'drizzle-orm';
|
import { desc, eq } from 'drizzle-orm';
|
||||||
import { Stripe } from 'stripe';
|
import { Stripe } from 'stripe';
|
||||||
import {
|
import {
|
||||||
@ -626,9 +626,9 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
`<< Created one-time payment record for user ${userId}, price: ${priceId}`
|
`<< Created one-time payment record for user ${userId}, price: ${priceId}`
|
||||||
);
|
);
|
||||||
|
|
||||||
// Send message to Discord channel
|
// Send notification
|
||||||
const amount = session.amount_total ? session.amount_total / 100 : 0;
|
const amount = session.amount_total ? session.amount_total / 100 : 0;
|
||||||
await sendMessageToDiscord(session.id, customerId, userId, amount);
|
await sendNotification(session.id, customerId, userId, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user