-
Notifications
You must be signed in to change notification settings - Fork 1
Fetch and cache active stripe subscription #50
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
Merged
Jackson57279
merged 2 commits into
main
from
cursor/fetch-and-cache-active-stripe-subscription-fff9
Aug 9, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,137 @@ | ||
| import type { VercelRequest, VercelResponse } from '@vercel/node'; | ||
| import type Stripe from 'stripe'; | ||
| import { stripe } from './_utils/stripe'; | ||
| import { kvPutJson } from './_utils/kv'; | ||
|
|
||
| // Allowed events that can affect subscription state | ||
| const allowedEvents: string[] = [ | ||
| 'checkout.session.completed', | ||
| 'customer.subscription.created', | ||
| 'customer.subscription.updated', | ||
| 'customer.subscription.deleted', | ||
| 'customer.subscription.paused', | ||
| 'customer.subscription.resumed', | ||
| 'customer.subscription.pending_update_applied', | ||
| 'customer.subscription.pending_update_expired', | ||
| 'customer.subscription.trial_will_end', | ||
| 'invoice.paid', | ||
| 'invoice.payment_failed', | ||
| 'invoice.payment_action_required', | ||
| 'invoice.upcoming', | ||
| 'invoice.marked_uncollectible', | ||
| 'invoice.payment_succeeded', | ||
| 'payment_intent.succeeded', | ||
| 'payment_intent.payment_failed', | ||
| 'payment_intent.canceled', | ||
| ]; | ||
|
|
||
| export type StripeSubCache = | ||
| | { | ||
| subscriptionId: string | null; | ||
| status: Stripe.Subscription.Status; | ||
| priceId: string | null; | ||
| currentPeriodStart: number | null; | ||
| currentPeriodEnd: number | null; | ||
| cancelAtPeriodEnd: boolean; | ||
| paymentMethod: { | ||
| brand: string | null; | ||
| last4: string | null; | ||
| } | null; | ||
| } | ||
| | { | ||
| status: 'none'; | ||
| }; | ||
|
|
||
| async function syncStripeDataToKV(customerId: string) { | ||
| const subscriptions = await stripe.subscriptions.list({ | ||
| customer: customerId, | ||
| // Fetch more than one to avoid caching a stale/cancelled sub | ||
| limit: 100, | ||
| status: 'all', | ||
| expand: ['data.default_payment_method'], | ||
| }); | ||
|
|
||
| const allSubs = subscriptions.data ?? []; | ||
|
|
||
| // Prefer active or trialing subscriptions, most recent by current period start | ||
| const candidates = allSubs | ||
| .filter((s) => s.status === 'active' || s.status === 'trialing') | ||
| .sort((a, b) => (b.current_period_start ?? 0) - (a.current_period_start ?? 0)); | ||
|
|
||
| const subscription = candidates[0]; | ||
|
|
||
| if (!subscription) { | ||
| const subData: StripeSubCache = { status: 'none' }; | ||
| await kvPutJson(`stripe:customer:${customerId}`, subData); | ||
| return subData; | ||
| } | ||
|
|
||
| const subData: StripeSubCache = { | ||
| subscriptionId: subscription.id, | ||
| status: subscription.status, | ||
| priceId: subscription.items.data[0]?.price?.id ?? null, | ||
| currentPeriodStart: subscription.current_period_start ?? null, | ||
| currentPeriodEnd: subscription.current_period_end ?? null, | ||
| cancelAtPeriodEnd: subscription.cancel_at_period_end ?? false, | ||
| paymentMethod: | ||
| subscription.default_payment_method && typeof subscription.default_payment_method !== 'string' | ||
| ? { | ||
| brand: subscription.default_payment_method.card?.brand ?? null, | ||
| last4: subscription.default_payment_method.card?.last4 ?? null, | ||
| } | ||
| : null, | ||
| }; | ||
|
|
||
| await kvPutJson(`stripe:customer:${customerId}`, subData); | ||
| return subData; | ||
| } | ||
|
|
||
| export default async function handler(req: VercelRequest, res: VercelResponse) { | ||
| if (req.method !== 'POST') { | ||
| res.setHeader('Allow', 'POST'); | ||
| return res.status(405).send('Method Not Allowed'); | ||
| } | ||
|
|
||
| const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET; | ||
| if (!webhookSecret) { | ||
| return res.status(500).send('Missing STRIPE_WEBHOOK_SECRET'); | ||
| } | ||
|
|
||
| const sig = req.headers['stripe-signature'] as string | undefined; | ||
| if (!sig) return res.status(400).send('Missing stripe-signature header'); | ||
|
|
||
| const rawBody = (req as any).rawBody || (req.body && typeof req.body === 'string' ? req.body : undefined); | ||
| // When deployed on Vercel, set functions config: { api: { bodyParser: false } } | ||
| if (!rawBody) { | ||
| // Vercel automatically provides rawBody when bodyParser is disabled | ||
| // If unavailable, we can't verify the signature | ||
| return res.status(400).send('Raw body required'); | ||
| } | ||
|
|
||
| try { | ||
| const event = stripe.webhooks.constructEvent(rawBody, sig, webhookSecret); | ||
|
|
||
| if (allowedEvents.includes(event.type)) { | ||
| const obj: any = event.data?.object ?? {}; | ||
|
||
| const customerId = obj.customer as string | undefined; | ||
| if (customerId && typeof customerId === 'string') { | ||
| await syncStripeDataToKV(customerId); | ||
| } | ||
| } | ||
|
|
||
| return res.status(200).json({ received: true }); | ||
| } catch (err: unknown) { | ||
| const errorMessage = | ||
| typeof err === 'object' && err !== null && 'message' in err | ||
| ? (err as { message?: string }).message | ||
| : String(err); | ||
| console.error('[STRIPE WEBHOOK] Error', errorMessage); | ||
| return res.status(400).send(`Webhook Error: ${errorMessage || 'Unknown error'}`); | ||
| } | ||
| } | ||
|
|
||
| export const config = { | ||
| api: { | ||
| bodyParser: false, | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.