feat: stripe webhook notifications (#33)

This commit is contained in:
Zack Pollard
2024-07-18 15:41:19 +01:00
committed by GitHub
parent 1639075773
commit 371a25325b
4 changed files with 62 additions and 5 deletions
+1
View File
@@ -48,6 +48,7 @@ const channels = {
General: '994044917355663450',
BotSpam: '1159083520027787307',
GithubStatus: '1240662502912692236',
Stripe: '1263493037162627103',
};
const misc = {
@@ -1,6 +1,6 @@
import express from 'express';
import { bot } from '../main.js';
import { Constants } from '../constants.js';
import { bot } from '../../main.js';
import { Constants } from '../../constants.js';
import { EmbedBuilder, TextChannel } from 'discord.js';
const app = express.Router();
@@ -113,4 +113,4 @@ app.post('/github-status/:slug', async (req, res) => {
res.status(200).send();
});
export const webhooks = app;
export const githubWebhooks = app;
@@ -0,0 +1,55 @@
import express from 'express';
import { bot } from '../../main.js';
import { Constants } from '../../constants.js';
import { EmbedBuilder, TextChannel } from 'discord.js';
const app = express.Router();
type StripeBase = {
id: string;
object: string;
};
type StripePaymentIntent = StripeBase & {
amount: number;
currency: number;
created: number;
description: string;
};
const isStripePaymentIntent = (payload: StripeBase): payload is StripePaymentIntent => {
if ((payload as StripeBase).object === 'payment_intent') {
return true;
}
return false;
};
const isImmichPaymentIntent = (payload: StripePaymentIntent): payload is StripePaymentIntent => {
return ['immich-server', 'immich-client'].includes((payload as StripePaymentIntent).description);
};
app.post('/stripe-payments/:slug', async (req, res) => {
if (req.params.slug !== process.env.STRIPE_PAYMENT_SLUG) {
res.status(401).send();
return;
}
res.status(204).send();
const body = req.body;
console.log(JSON.stringify(body));
if (!isStripePaymentIntent(body) || !isImmichPaymentIntent(body)) return;
const licenseType = body.description.split('-')[1];
const channel = (await bot.channels.fetch(Constants.Channels.Stripe)) as TextChannel;
const embed = new EmbedBuilder({
title: `Immich ${licenseType} license purchased`,
author: { name: 'Stripe Payments', url: 'https://stripe.com' },
url: `https://dashboard.stripe.com/payments/${body.id}`,
description: `Total: ${body.amount} ${body.currency}`,
});
await channel.send({ embeds: [embed] });
});
export const stripeWebhooks = app;
+3 -2
View File
@@ -5,7 +5,8 @@ import { IntentsBitField, Partials } from 'discord.js';
import { Client } from 'discordx';
import { Constants } from './constants.js';
import express from 'express';
import { webhooks } from './controllers/webhooks.controller.js';
import { githubWebhooks } from './controllers/webhooks/github.controller.js';
import { stripeWebhooks } from './controllers/webhooks/stripe.controller.js';
export const bot = new Client({
// Discord intents
@@ -84,7 +85,7 @@ async function run() {
// Log in with your bot token
app.use(express.json());
app.use('/webhooks', webhooks);
app.use('/webhooks', [githubWebhooks, stripeWebhooks]);
app.listen(8080, () => {
console.log('Bot listening on port 8080');
});