-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(web): only grant developer credits on paid checkout #1931
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
+125
−58
Merged
Changes from all commits
Commits
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
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
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,10 +12,79 @@ import { addCreditsToAccount } from "@/lib/developer-credits"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const relevantEvents = new Set([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "checkout.session.completed", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "checkout.session.async_payment_succeeded", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "customer.subscription.updated", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "customer.subscription.deleted", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async function grantDeveloperCredits( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| session: Stripe.Checkout.Session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<Response> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { accountId, amountCents } = session.metadata ?? {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const paymentIntentId = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| typeof session.payment_intent === "string" ? session.payment_intent : null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!accountId || !amountCents || !paymentIntentId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error("Missing required metadata for developer credits:", { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| accountId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amountCents, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| paymentIntentId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response("Missing metadata", { status: 400 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Only grant credits once the payment has actually settled. Without this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // guard a checkout session (e.g. an unpaid/async payment) could grant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // credits before money is captured. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (session.payment_status !== "paid") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `Developer credits checkout not paid yet (payment_status=${session.payment_status}); skipping credit grant`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { accountId, paymentIntentId }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NextResponse.json({ received: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Processing developer credits purchase:", { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| accountId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amountCents, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| paymentIntentId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [existingTxn] = await db() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .select({ id: developerCreditTransactions.id }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .from(developerCreditTransactions) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .where( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq(developerCreditTransactions.accountId, accountId), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq(developerCreditTransactions.referenceId, paymentIntentId), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq(developerCreditTransactions.referenceType, "stripe_payment_intent"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .limit(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (existingTxn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Duplicate webhook delivery — transaction already exists:", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| existingTxn.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NextResponse.json({ received: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await addCreditsToAccount({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| accountId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amountCents: Number(amountCents), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| referenceId: paymentIntentId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| referenceType: "stripe_payment_intent", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| metadata: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amountCents: Number(amountCents), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stripeSessionId: session.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+73
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth validating
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Developer credits added successfully"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NextResponse.json({ received: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async function createGuestUser( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| email: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<typeof users.$inferSelect> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -146,63 +215,7 @@ export const POST = async (req: Request) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (session.metadata?.type === "developer_credits") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { accountId, amountCents } = session.metadata; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const paymentIntentId = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| typeof session.payment_intent === "string" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? session.payment_intent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!accountId || !amountCents || !paymentIntentId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error("Missing required metadata for developer credits:", { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| accountId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amountCents, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| paymentIntentId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response("Missing metadata", { status: 400 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Processing developer credits purchase:", { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| accountId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amountCents, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| paymentIntentId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [existingTxn] = await db() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .select({ id: developerCreditTransactions.id }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .from(developerCreditTransactions) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .where( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq(developerCreditTransactions.accountId, accountId), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq(developerCreditTransactions.referenceId, paymentIntentId), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eq( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| developerCreditTransactions.referenceType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "stripe_payment_intent", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .limit(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (existingTxn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Duplicate webhook delivery — transaction already exists:", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| existingTxn.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NextResponse.json({ received: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await addCreditsToAccount({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| accountId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amountCents: Number(amountCents), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| referenceId: paymentIntentId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| referenceType: "stripe_payment_intent", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| metadata: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amountCents: Number(amountCents), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stripeSessionId: session.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Developer credits added successfully"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NextResponse.json({ received: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await grantDeveloperCredits(session); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const customer = await stripe().customers.retrieve( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -352,6 +365,17 @@ export const POST = async (req: Request) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (event.type === "checkout.session.async_payment_succeeded") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Processing checkout.session.async_payment_succeeded event", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const session = event.data.object as Stripe.Checkout.Session; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (session.metadata?.type === "developer_credits") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await grantDeveloperCredits(session); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (event.type === "customer.subscription.updated") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Processing customer.subscription.updated event"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const subscription = event.data.object as Stripe.Subscription; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number.isFinite(duration)silently letsInfinitybypass this early cap — the old condition evaluatedInfinity > 300astrueand threwupgrade_required, butNumber.isFinite(Infinity)isfalse, so the check is skipped. The comment says finalize is the authoritative enforcement point, but if a client deliberately passesduration: Infinitythe pre-upload rejection no longer fires. DroppingNumber.isFinitewhile keepingtypeof duration === "number"restores the original behaviour sinceNaN > 300is alreadyfalse.Prompt To Fix With AI