Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jb/clean up stripe event forwarding #1437

Merged
merged 6 commits into from Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 49 additions & 31 deletions packages/skill-api/src/core/services/process-stripe-webhook.ts
Expand Up @@ -35,9 +35,6 @@ export async function receiveInternalStripeWebhooks({
params: SkillRecordingsHandlerParams
paymentOptions: PaymentOptions | undefined
}): Promise<OutgoingResponse> {
let event: any
let body: any

try {
const {
req,
Expand All @@ -58,31 +55,63 @@ 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},
}
}
}

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,
Expand Down Expand Up @@ -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')
Expand All @@ -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,
Expand Down