diff --git a/packages/skill-api/src/core/services/process-stripe-webhook.ts b/packages/skill-api/src/core/services/process-stripe-webhook.ts index 6bb652d02..006756901 100644 --- a/packages/skill-api/src/core/services/process-stripe-webhook.ts +++ b/packages/skill-api/src/core/services/process-stripe-webhook.ts @@ -35,9 +35,6 @@ export async function receiveInternalStripeWebhooks({ params: SkillRecordingsHandlerParams paymentOptions: PaymentOptions | undefined }): Promise { - let event: any - let body: any - try { const { req, @@ -58,24 +55,14 @@ export async function receiveInternalStripeWebhooks({ const _paymentOptions = paymentOptions || { stripeCtx: {stripe: defaultStripe}, } - const stripe = paymentOptions?.stripeCtx.stripe || defaultStripe - body = req.body - event = req.body.event + const event = req.body.event return await processStripeWebhook(event, { nextAuthOptions, paymentOptions: _paymentOptions, }) } catch (error: any) { - console.log( - `webhook/stripe-internal error: ${JSON.stringify({ - message: error.message, - event: event, - body: body, - })}`, - ) - return { status: 500, body: {error: true, message: error.message}, @@ -83,6 +70,48 @@ export async function receiveInternalStripeWebhooks({ } } +const determineEventProcessor = (siteName: string) => { + if (siteName === 'testing-javascript') { + const internalStripeWebhookEndpoint = z + .string() + .parse(process.env.TESTING_JAVASCRIPT_INTERNAL_STRIPE_URL) + const skillSecret = z + .string({ + required_error: 'TJS_SKILL_SECRET must be set in this environemnt', + }) + .parse(process.env.TJS_SKILL_SECRET) + + return { + handler: 'testing-javascript' as const, + details: { + skillSecret, + webhookEndpoint: internalStripeWebhookEndpoint, + }, + } + } else if (siteName === 'epic-react') { + const internalStripeWebhookEndpoint = z + .string() + .parse(process.env.EPIC_REACT_INTERNAL_STRIPE_URL) + const skillSecret = z + .string({ + required_error: 'ER_SKILL_SECRET must be set in this environemnt', + }) + .parse(process.env.ER_SKILL_SECRET) + + return { + handler: 'epic-react' as const, + details: { + skillSecret, + webhookEndpoint: internalStripeWebhookEndpoint, + }, + } + } else { + return { + handler: 'self' as const, + } + } +} + export async function receiveStripeWebhooks({ params, paymentOptions, @@ -135,25 +164,14 @@ export async function receiveStripeWebhooks({ }) } - // 1. verify and extract details from Stripe webhook request - // 2. send details to inngest if available - // 3. tell the appropriate app to handle it... - // 4. return a 200 - const {siteName: targetSiteName} = z .object({siteName: z.string().default(METADATA_MISSING_SITE_NAME)}) .parse(event.data.object.metadata) - if (targetSiteName === 'testing-javascript') { - // send event to TJS processing endpoint - const internalStripeWebhookEndpoint = z - .string() - .parse(process.env.TESTING_JAVASCRIPT_INTERNAL_STRIPE_URL) - const skillSecret = z - .string({ - required_error: 'TJS_SKILL_SECRET must be set in this environemnt', - }) - .parse(process.env.TJS_SKILL_SECRET) + const {handler, details} = determineEventProcessor(targetSiteName) + + if (handler !== 'self') { + const {skillSecret, webhookEndpoint} = details const payloadString = JSON.stringify({event}) const contentLength = Buffer.byteLength(payloadString, 'utf8') @@ -165,13 +183,13 @@ export async function receiveStripeWebhooks({ }) // not awaiting the fetch so that endpoint can return 200 right away - await nodeFetch(internalStripeWebhookEndpoint, { + await nodeFetch(webhookEndpoint, { method: 'POST', headers, body: payloadString, }) - return {status: 200, body: `handled by ${targetSiteName}`} + return {status: 200, body: `handled by ${handler}`} } else { return await processStripeWebhook(event, { nextAuthOptions,