fix: use correct stripe client for each fund on webhooks

This commit is contained in:
Artur
2024-09-30 10:08:29 -03:00
parent 2ea7885eab
commit 838a3773fc
6 changed files with 12 additions and 10 deletions

View File

@@ -7,4 +7,4 @@ export const config = {
},
}
export default getStripeWebhookHandler(env.STRIPE_FIRO_WEBHOOK_SECRET)
export default getStripeWebhookHandler('firo', env.STRIPE_FIRO_WEBHOOK_SECRET)

View File

@@ -7,4 +7,4 @@ export const config = {
},
}
export default getStripeWebhookHandler(env.STRIPE_GENERAL_WEBHOOK_SECRET)
export default getStripeWebhookHandler('general', env.STRIPE_GENERAL_WEBHOOK_SECRET)

View File

@@ -7,4 +7,4 @@ export const config = {
},
}
export default getStripeWebhookHandler(env.STRIPE_MONERO_WEBHOOK_SECRET)
export default getStripeWebhookHandler('monero', env.STRIPE_MONERO_WEBHOOK_SECRET)

View File

@@ -7,4 +7,4 @@ export const config = {
},
}
export default getStripeWebhookHandler(env.STRIPE_PRIVACY_GUIDES_WEBHOOK_SECRET)
export default getStripeWebhookHandler('privacyguides', env.STRIPE_PRIVACY_GUIDES_WEBHOOK_SECRET)

View File

@@ -37,9 +37,9 @@ const btcpayApi = axios.create({
const stripe: Record<FundSlug, Stripe> = {
monero: new Stripe(env.STRIPE_MONERO_SECRET_KEY, { apiVersion: '2024-04-10' }),
firo: new Stripe(env.STRIPE_MONERO_SECRET_KEY, { apiVersion: '2024-04-10' }),
privacyguides: new Stripe(env.STRIPE_MONERO_SECRET_KEY, { apiVersion: '2024-04-10' }),
general: new Stripe(env.STRIPE_MONERO_SECRET_KEY, { apiVersion: '2024-04-10' }),
firo: new Stripe(env.STRIPE_FIRO_SECRET_KEY, { apiVersion: '2024-04-10' }),
privacyguides: new Stripe(env.STRIPE_PRIVACY_GUIDES_SECRET_KEY, { apiVersion: '2024-04-10' }),
general: new Stripe(env.STRIPE_GENERAL_SECRET_KEY, { apiVersion: '2024-04-10' }),
}
export { prisma, keycloak, transporter, btcpayApi, stripe }

View File

@@ -1,13 +1,14 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { FundSlug } from '@prisma/client'
import Stripe from 'stripe'
import getRawBody from 'raw-body'
import dayjs from 'dayjs'
import { btcpayApi as _btcpayApi, prisma, stripe } from '../../server/services'
import { btcpayApi as _btcpayApi, prisma, stripe as _stripe } from '../../server/services'
import { DonationMetadata } from '../../server/types'
import { sendDonationConfirmationEmail } from './mailing'
export function getStripeWebhookHandler(secret: string) {
export function getStripeWebhookHandler(fundSlug: FundSlug, secret: string) {
return async (req: NextApiRequest, res: NextApiResponse) => {
let event: Stripe.Event
@@ -15,7 +16,8 @@ export function getStripeWebhookHandler(secret: string) {
const signature = req.headers['stripe-signature']
try {
event = stripe.monero.webhooks.constructEvent(await getRawBody(req), signature!, secret)
const stripe = _stripe[fundSlug]
event = stripe.webhooks.constructEvent(await getRawBody(req), signature!, secret)
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, (err as any).message)
res.status(400).end()