Skip to content

Commit

Permalink
fix(subscription): fixed subscription bug unexpected charge behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Materozzi committed Apr 9, 2024
1 parent 1cedfad commit b24f073
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 57 deletions.
2 changes: 2 additions & 0 deletions app/api/users/stripe/recharge/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextRequest } from "next/server"
import { PaymentType } from "@/constants/subscriptions"
import { getServerSession } from "next-auth/next"
import { z } from "zod"

Expand Down Expand Up @@ -57,6 +58,7 @@ export async function GET(req: NextRequest) {
],
metadata: {
userId: session.user.id,
type: PaymentType.RechargeTokens,
},
})

Expand Down
120 changes: 63 additions & 57 deletions app/api/webhooks/stripe/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { headers } from "next/headers"
import { proPlanTokens, tokensPerCent } from "@/constants/subscriptions"
import {
PaymentType,
proPlanTokens,
tokensPerCent,
} from "@/constants/subscriptions"
import Stripe from "stripe"

import { env } from "@/env.mjs"
Expand Down Expand Up @@ -27,42 +31,68 @@ export async function POST(req: Request) {
const session = event.data.object as Stripe.Checkout.Session

if (event.type === "checkout.session.completed") {
// Retrieve the subscription details from Stripe.
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string
)
// recharge tokens
if (session?.metadata?.type === PaymentType.RechargeTokens) {
const user = await db.user.findFirst({
where: {
id: session?.metadata?.userId,
},
})

const user = await db.user.findFirst({
where: {
id: session?.metadata?.userId,
},
})
if (!user) {
return new Response(i18n.t("Stripe payment user not found"), {
status: 400,
})
}

if (!user) {
return new Response(i18n.t("Stripe payment user not found"), {
status: 400,
await db.user.update({
where: {
id: user.id,
},
data: {
tokens:
Number(user.tokens) +
event.data.object["amount_total"] * tokensPerCent,
},
})
}
} else {
// Retrieve the subscription details from Stripe.
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string
)

// Update the user stripe into in our database.
// Since this is the initial subscription, we need to update
// the subscription id and customer id.
await db.user.update({
where: {
id: session?.metadata?.userId,
},
data: {
stripeSubscriptionId: subscription.id,
stripeCustomerId: subscription.customer as string,
stripePriceId: subscription.items.data[0].price.id,
stripeCurrentPeriodEnd: new Date(
subscription.current_period_end * 1000
),
tokens: user.stripeSubscriptionId
? user.tokens
: Number(user.tokens) + proPlanTokens,
},
})
const user = await db.user.findFirst({
where: {
id: session?.metadata?.userId,
},
})

if (!user) {
return new Response(i18n.t("Stripe payment user not found"), {
status: 400,
})
}

// Update the user stripe into in our database.
// Since this is the initial subscription, we need to update
// the subscription id and customer id.
await db.user.update({
where: {
id: session?.metadata?.userId,
},
data: {
stripeSubscriptionId: subscription.id,
stripeCustomerId: subscription.customer as string,
stripePriceId: subscription.items.data[0].price.id,
stripeCurrentPeriodEnd: new Date(
subscription.current_period_end * 1000
),
tokens: user.stripeSubscriptionId
? user.tokens
: Number(user.tokens) + proPlanTokens,
},
})
}
}

if (event.type === "invoice.payment_succeeded") {
Expand All @@ -85,29 +115,5 @@ export async function POST(req: Request) {
})
}

if (event.type === "charge.succeeded") {
const user = await db.user.findFirst({
where: {
id: session?.metadata?.userId,
},
})

if (!user) {
return new Response(i18n.t("Stripe payment user not found"), {
status: 400,
})
}

await db.user.update({
where: {
id: user.id,
},
data: {
tokens:
Number(user.tokens) + event.data.object["amount"] * tokensPerCent,
},
})
}

return SuccessResponse()
}
4 changes: 4 additions & 0 deletions constants/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const freePlanTokens = 20000
export const proPlanTokens = 100000
export const tokensPerCent = 2500

export enum PaymentType {
RechargeTokens = "recharge-tokens",
}

0 comments on commit b24f073

Please sign in to comment.