( request: NextApiRequest, response: NextApiResponse )
| 41 | } |
| 42 | |
| 43 | export default async function handler( |
| 44 | request: NextApiRequest, |
| 45 | response: NextApiResponse |
| 46 | ) { |
| 47 | const signature = request.headers['stripe-signature'] as string; |
| 48 | const body = await buffer(request); |
| 49 | |
| 50 | let event; |
| 51 | |
| 52 | try { |
| 53 | event = stripe.webhooks.constructEvent(body, signature, WEBHOOK_SECRET); |
| 54 | } catch (exception) { |
| 55 | return response.status(400).json({}); |
| 56 | } |
| 57 | |
| 58 | let communityId; |
| 59 | |
| 60 | switch (event.type) { |
| 61 | case 'checkout.session.completed': |
| 62 | communityId = await getCommunityId(event); |
| 63 | await promoteAccount(communityId); |
| 64 | break; |
| 65 | case 'invoice.paid': |
| 66 | communityId = await getCommunityId(event); |
| 67 | await promoteAccount(communityId); |
| 68 | break; |
| 69 | case 'invoice.payment_failed': |
| 70 | communityId = await getCommunityId(event); |
| 71 | await downgradeAccount(communityId); |
| 72 | break; |
| 73 | |
| 74 | default: |
| 75 | console.error(`Unhandled event type ${event.type}`); |
| 76 | } |
| 77 | |
| 78 | response.status(200).json({}); |
| 79 | } |
nothing calls this directly
no test coverage detected