add subscription webhook
This commit is contained in:
@@ -17,5 +17,5 @@ export default async function getUsers() {
|
||||
},
|
||||
});
|
||||
|
||||
return { response: users, status: 200 };
|
||||
return { response: users.sort((a: any, b: any) => a.id - b.id), status: 200 };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import Stripe from "stripe";
|
||||
import { prisma } from "./db";
|
||||
|
||||
type Data = {
|
||||
id: string;
|
||||
active: boolean;
|
||||
quantity: number;
|
||||
periodStart: number;
|
||||
periodEnd: number;
|
||||
};
|
||||
|
||||
export default async function handleSubscription({
|
||||
id,
|
||||
active,
|
||||
quantity,
|
||||
periodStart,
|
||||
periodEnd,
|
||||
}: Data) {
|
||||
const subscription = await prisma.subscription.findUnique({
|
||||
where: {
|
||||
stripeSubscriptionId: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (subscription) {
|
||||
await prisma.subscription.update({
|
||||
where: {
|
||||
stripeSubscriptionId: id,
|
||||
},
|
||||
data: {
|
||||
active,
|
||||
quantity,
|
||||
currentPeriodStart: new Date(periodStart * 1000),
|
||||
currentPeriodEnd: new Date(periodEnd * 1000),
|
||||
},
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
if (!process.env.STRIPE_SECRET_KEY)
|
||||
throw new Error("Missing Stripe secret key");
|
||||
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
|
||||
apiVersion: "2022-11-15",
|
||||
});
|
||||
|
||||
const subscription = await stripe.subscriptions.retrieve(id);
|
||||
const customerId = subscription.customer;
|
||||
|
||||
const customer = await stripe.customers.retrieve(customerId.toString());
|
||||
const email = (customer as Stripe.Customer).email;
|
||||
|
||||
if (!email) throw new Error("Email not found");
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) throw new Error("User not found");
|
||||
|
||||
const userId = user.id;
|
||||
|
||||
await prisma.subscription
|
||||
.upsert({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
create: {
|
||||
active,
|
||||
stripeSubscriptionId: id,
|
||||
quantity,
|
||||
currentPeriodStart: new Date(periodStart * 1000),
|
||||
currentPeriodEnd: new Date(periodEnd * 1000),
|
||||
user: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
update: {
|
||||
active,
|
||||
stripeSubscriptionId: id,
|
||||
quantity,
|
||||
currentPeriodStart: new Date(periodStart * 1000),
|
||||
currentPeriodEnd: new Date(periodEnd * 1000),
|
||||
},
|
||||
})
|
||||
.catch((err) => console.log(err));
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,18 @@ export default async function paymentCheckout(
|
||||
|
||||
const NEXT_PUBLIC_TRIAL_PERIOD_DAYS =
|
||||
process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS;
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
customer: isExistingCustomer ? isExistingCustomer : undefined,
|
||||
line_items: [
|
||||
{
|
||||
price: priceId,
|
||||
quantity: 1,
|
||||
adjustable_quantity: {
|
||||
enabled: true,
|
||||
minimum: 1,
|
||||
maximum: Number(process.env.STRIPE_MAX_QUANTITY || 100),
|
||||
},
|
||||
},
|
||||
],
|
||||
mode: "subscription",
|
||||
|
||||
Reference in New Issue
Block a user