Skip to content

Commit

Permalink
feat: stripe notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
zackpollard committed Jul 18, 2024
1 parent 1639075 commit 2c2cc33
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const channels = {
General: '994044917355663450',
BotSpam: '1159083520027787307',
GithubStatus: '1240662502912692236',
Stripe: '1263493037162627103',
};

const misc = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -113,4 +113,4 @@ app.post('/github-status/:slug', async (req, res) => {
res.status(200).send();
});

export const webhooks = app;
export const githubWebhooks = app;
58 changes: 58 additions & 0 deletions src/controllers/webhooks/stripe.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 StripeBase => {
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)) return;

const paymentIntent = body as StripePaymentIntent;
if (!isImmichPaymentIntent(paymentIntent)) return;

const licenseType = paymentIntent.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/${paymentIntent.id}`,
description: `Total: ${paymentIntent.amount} ${paymentIntent.currency}`,
});
await channel.send({ embeds: [embed] });
});

export const stripeWebhooks = app;
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
});
Expand Down

0 comments on commit 2c2cc33

Please sign in to comment.