-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add to env feat: add more params to stripe client feat: add stripe webhook feat: update parser
- Loading branch information
Showing
7 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ SLACK_POSTMAN_WEBHOOK_URL= | |
|
||
STRIPE_SECRET_KEY= | ||
STRIPE_PUBLISHABLE_KEY= | ||
STRIPE_WEBHOOK_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { NextRequest } from 'next/server'; | ||
import type Stripe from 'stripe'; | ||
|
||
import { env } from '@/env.mjs'; | ||
import { HttpBadRequestError, HttpInternalServerError } from '@/lib/errors'; | ||
import { stripe } from '@/lib/services/stripe/client'; | ||
import { fulfillOrder } from '@/lib/services/stripe/order'; | ||
|
||
export async function POST(req: NextRequest) { | ||
const signature = req.headers.get('stripe-signature'); | ||
|
||
if (!signature) { | ||
return new HttpBadRequestError({ | ||
message: 'Missing Stripe signature', | ||
}).toNextResponse(); | ||
} | ||
|
||
let event: Stripe.Event; | ||
|
||
try { | ||
event = stripe.webhooks.constructEvent( | ||
await req.text(), | ||
signature, | ||
env.STRIPE_WEBHOOK_SECRET, | ||
); | ||
} catch (error) { | ||
return new HttpBadRequestError({ | ||
message: 'Invalid payload', | ||
}).toNextResponse(); | ||
} | ||
|
||
try { | ||
switch (event.type) { | ||
case 'checkout.session.completed': { | ||
const session = await stripe.checkout.sessions.retrieve( | ||
event.data.object.id, | ||
{ | ||
expand: ['line_items'], | ||
}, | ||
); | ||
|
||
if (session.payment_status === 'paid') { | ||
await fulfillOrder(session); | ||
} | ||
|
||
break; | ||
} | ||
|
||
default: | ||
break; | ||
} | ||
} catch (error) { | ||
return new HttpInternalServerError({ | ||
error, | ||
}).toNextResponse(); | ||
} | ||
|
||
return new Response('OK'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Stripe } from 'stripe'; | ||
|
||
import { DatabaseError } from '@/lib/errors'; | ||
import { z } from 'zod'; | ||
|
||
import { createSupabaseServiceClient } from '../supabase/service'; | ||
|
||
const checkoutSessionSchema = z.object({ | ||
id: z.string(), | ||
line_items: z.object({ | ||
data: z.array( | ||
z.object({ | ||
price: z.object({ | ||
transform_quantity: z.object({ | ||
divide_by: z.number(), | ||
}), | ||
}), | ||
}), | ||
), | ||
}), | ||
metadata: z.object({ | ||
accountId: z.string().min(1).transform(Number), | ||
userId: z.string().min(1), | ||
}), | ||
}); | ||
|
||
export const fulfillOrder = async (session: Stripe.Checkout.Session) => { | ||
const validatedSession = checkoutSessionSchema.safeParse(session); | ||
|
||
if (!validatedSession.success) { | ||
throw validatedSession.error; | ||
} | ||
|
||
const supabase = createSupabaseServiceClient(); | ||
|
||
const currentAccountCreditsQuery = await supabase | ||
.from('account') | ||
.select('ai_credit') | ||
.eq('id', validatedSession.data.metadata.accountId) | ||
.single(); | ||
|
||
if (currentAccountCreditsQuery.error) { | ||
throw new DatabaseError(currentAccountCreditsQuery.error); | ||
} | ||
|
||
const updateAccountQuery = await supabase | ||
.from('account') | ||
.update({ | ||
ai_credit: | ||
(currentAccountCreditsQuery.data.ai_credit ?? 0) + | ||
validatedSession.data.line_items.data[0].price.transform_quantity | ||
.divide_by, | ||
}) | ||
.eq('id', validatedSession.data.metadata.accountId); | ||
|
||
if (updateAccountQuery.error) { | ||
throw new DatabaseError(updateAccountQuery.error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters