From 3bd42559991858fe85692557b3fa1489d38ccbd9 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 11 Mar 2024 13:18:55 -0500 Subject: [PATCH 1/5] feat(er): forward epic-react stripe events from EW --- .../core/services/process-stripe-webhook.ts | 60 +++++++++++++++---- 1 file changed, 48 insertions(+), 12 deletions(-) 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..a39a5cf97 100644 --- a/packages/skill-api/src/core/services/process-stripe-webhook.ts +++ b/packages/skill-api/src/core/services/process-stripe-webhook.ts @@ -83,6 +83,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: 'TJS_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, @@ -144,16 +186,10 @@ export async function receiveStripeWebhooks({ .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 +201,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, From d4bfa8a5c5be28b906cfe817d4c6400a497f7be2 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 11 Mar 2024 14:01:30 -0500 Subject: [PATCH 2/5] cleanup: remove temprorary comments --- .../skill-api/src/core/services/process-stripe-webhook.ts | 5 ----- 1 file changed, 5 deletions(-) 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 a39a5cf97..1c3f87a5e 100644 --- a/packages/skill-api/src/core/services/process-stripe-webhook.ts +++ b/packages/skill-api/src/core/services/process-stripe-webhook.ts @@ -177,11 +177,6 @@ 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) From f9a0a361bb3a42c65de0aa63e46376da2e70817f Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 11 Mar 2024 14:02:18 -0500 Subject: [PATCH 3/5] cleanup: remove unused stripe variable --- packages/skill-api/src/core/services/process-stripe-webhook.ts | 1 - 1 file changed, 1 deletion(-) 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 1c3f87a5e..e452f5ac0 100644 --- a/packages/skill-api/src/core/services/process-stripe-webhook.ts +++ b/packages/skill-api/src/core/services/process-stripe-webhook.ts @@ -58,7 +58,6 @@ export async function receiveInternalStripeWebhooks({ const _paymentOptions = paymentOptions || { stripeCtx: {stripe: defaultStripe}, } - const stripe = paymentOptions?.stripeCtx.stripe || defaultStripe body = req.body event = req.body.event From e145f671504fd26db476a45b93c12fdaa0fd8114 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 12 Mar 2024 19:44:02 -0500 Subject: [PATCH 4/5] fix(er): fix typo in parsing error message --- packages/skill-api/src/core/services/process-stripe-webhook.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e452f5ac0..afcb39489 100644 --- a/packages/skill-api/src/core/services/process-stripe-webhook.ts +++ b/packages/skill-api/src/core/services/process-stripe-webhook.ts @@ -106,7 +106,7 @@ const determineEventProcessor = (siteName: string) => { .parse(process.env.EPIC_REACT_INTERNAL_STRIPE_URL) const skillSecret = z .string({ - required_error: 'TJS_SKILL_SECRET must be set in this environemnt', + required_error: 'ER_SKILL_SECRET must be set in this environemnt', }) .parse(process.env.ER_SKILL_SECRET) From 3b77bd2b2e65a9a81aaa1899715f19fbdf548413 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 15 Mar 2024 11:38:34 -0500 Subject: [PATCH 5/5] cleanup: remove some debugging leftovers --- .../src/core/services/process-stripe-webhook.ts | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) 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 afcb39489..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, @@ -59,22 +56,13 @@ export async function receiveInternalStripeWebhooks({ 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},