bug fixed
This commit is contained in:
parent
d042c82cb0
commit
f68582e28c
|
@ -1,22 +1,9 @@
|
||||||
import Stripe from "stripe";
|
import Stripe from "stripe";
|
||||||
|
|
||||||
const MONTHLY_PRICE_ID = process.env.MONTHLY_PRICE_ID;
|
|
||||||
const YEARLY_PRICE_ID = process.env.YEARLY_PRICE_ID;
|
|
||||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||||
|
|
||||||
export default async function checkSubscriptionByEmail(email: string) {
|
export default async function checkSubscriptionByEmail(email: string) {
|
||||||
let active: boolean | undefined,
|
if (!STRIPE_SECRET_KEY) return null;
|
||||||
stripeSubscriptionId: string | undefined,
|
|
||||||
currentPeriodStart: number | undefined,
|
|
||||||
currentPeriodEnd: number | undefined;
|
|
||||||
|
|
||||||
if (!STRIPE_SECRET_KEY)
|
|
||||||
return {
|
|
||||||
active,
|
|
||||||
stripeSubscriptionId,
|
|
||||||
currentPeriodStart,
|
|
||||||
currentPeriodEnd,
|
|
||||||
};
|
|
||||||
|
|
||||||
const stripe = new Stripe(STRIPE_SECRET_KEY, {
|
const stripe = new Stripe(STRIPE_SECRET_KEY, {
|
||||||
apiVersion: "2022-11-15",
|
apiVersion: "2022-11-15",
|
||||||
|
@ -28,26 +15,17 @@ export default async function checkSubscriptionByEmail(email: string) {
|
||||||
expand: ["data.subscriptions"],
|
expand: ["data.subscriptions"],
|
||||||
});
|
});
|
||||||
|
|
||||||
listByEmail.data.some((customer) => {
|
if (listByEmail?.data[0]?.subscriptions?.data[0]) {
|
||||||
customer.subscriptions?.data.some((subscription) => {
|
return {
|
||||||
subscription.current_period_end;
|
active: (listByEmail.data[0].subscriptions?.data[0] as any).plan.active,
|
||||||
|
stripeSubscriptionId: listByEmail.data[0].subscriptions?.data[0].id,
|
||||||
active =
|
currentPeriodStart:
|
||||||
subscription.items.data.some(
|
listByEmail.data[0].subscriptions?.data[0].current_period_start * 1000,
|
||||||
(e) =>
|
currentPeriodEnd:
|
||||||
(e.price.id === MONTHLY_PRICE_ID && e.price.active === true) ||
|
listByEmail.data[0].subscriptions?.data[0].current_period_end * 1000,
|
||||||
(e.price.id === YEARLY_PRICE_ID && e.price.active === true)
|
quantity: (listByEmail?.data[0]?.subscriptions?.data[0] as any).quantity,
|
||||||
) || false;
|
};
|
||||||
stripeSubscriptionId = subscription.id;
|
} else {
|
||||||
currentPeriodStart = subscription.current_period_start * 1000;
|
return null;
|
||||||
currentPeriodEnd = subscription.current_period_end * 1000;
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
active: active || false,
|
|
||||||
stripeSubscriptionId,
|
|
||||||
currentPeriodStart,
|
|
||||||
currentPeriodEnd,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,64 +9,56 @@ interface UserIncludingSubscription extends User {
|
||||||
export default async function verifySubscription(
|
export default async function verifySubscription(
|
||||||
user?: UserIncludingSubscription
|
user?: UserIncludingSubscription
|
||||||
) {
|
) {
|
||||||
if (!user) {
|
if (!user || !user.subscriptions) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscription = user.subscriptions;
|
if (
|
||||||
|
!user.subscriptions.active ||
|
||||||
|
new Date() > user.subscriptions.currentPeriodEnd
|
||||||
|
) {
|
||||||
|
const subscription = await checkSubscriptionByEmail(user.email as string);
|
||||||
|
|
||||||
const currentDate = new Date();
|
if (
|
||||||
|
!subscription ||
|
||||||
|
!subscription.stripeSubscriptionId ||
|
||||||
|
!subscription.currentPeriodEnd ||
|
||||||
|
!subscription.currentPeriodStart ||
|
||||||
|
!subscription.quantity
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (!subscription?.active || currentDate > subscription.currentPeriodEnd) {
|
|
||||||
const {
|
const {
|
||||||
active,
|
active,
|
||||||
stripeSubscriptionId,
|
stripeSubscriptionId,
|
||||||
currentPeriodStart,
|
currentPeriodStart,
|
||||||
currentPeriodEnd,
|
currentPeriodEnd,
|
||||||
} = await checkSubscriptionByEmail(user.email as string);
|
quantity,
|
||||||
|
} = subscription;
|
||||||
|
|
||||||
if (
|
await prisma.subscription
|
||||||
active &&
|
.upsert({
|
||||||
stripeSubscriptionId &&
|
|
||||||
currentPeriodStart &&
|
|
||||||
currentPeriodEnd
|
|
||||||
) {
|
|
||||||
await prisma.subscription
|
|
||||||
.upsert({
|
|
||||||
where: {
|
|
||||||
userId: user.id,
|
|
||||||
},
|
|
||||||
create: {
|
|
||||||
active,
|
|
||||||
stripeSubscriptionId,
|
|
||||||
currentPeriodStart: new Date(currentPeriodStart),
|
|
||||||
currentPeriodEnd: new Date(currentPeriodEnd),
|
|
||||||
userId: user.id,
|
|
||||||
},
|
|
||||||
update: {
|
|
||||||
active,
|
|
||||||
stripeSubscriptionId,
|
|
||||||
currentPeriodStart: new Date(currentPeriodStart),
|
|
||||||
currentPeriodEnd: new Date(currentPeriodEnd),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.catch((err) => console.log(err));
|
|
||||||
} else if (!active) {
|
|
||||||
const subscription = await prisma.subscription.findFirst({
|
|
||||||
where: {
|
where: {
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
},
|
},
|
||||||
});
|
create: {
|
||||||
|
active,
|
||||||
if (subscription)
|
stripeSubscriptionId,
|
||||||
await prisma.subscription.delete({
|
currentPeriodStart: new Date(currentPeriodStart),
|
||||||
where: {
|
currentPeriodEnd: new Date(currentPeriodEnd),
|
||||||
userId: user.id,
|
quantity,
|
||||||
},
|
userId: user.id,
|
||||||
});
|
},
|
||||||
|
update: {
|
||||||
return null;
|
active,
|
||||||
}
|
stripeSubscriptionId,
|
||||||
|
currentPeriodStart: new Date(currentPeriodStart),
|
||||||
|
currentPeriodEnd: new Date(currentPeriodEnd),
|
||||||
|
quantity,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.catch((err) => console.log(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
|
|
@ -59,30 +59,6 @@ export default async function webhook(
|
||||||
return res.status(400).send("Webhook signature verification failed.");
|
return res.status(400).send("Webhook signature verification failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// switch (event.type) {
|
|
||||||
// case "invoice.payment_succeeded":
|
|
||||||
// console.log(
|
|
||||||
// `Payment for ${event.data.object.customer_email} was successful!`
|
|
||||||
// );
|
|
||||||
// await handleCreateSubscription(event.data.object);
|
|
||||||
// break;
|
|
||||||
// case "customer.subscription.updated":
|
|
||||||
// console.log(
|
|
||||||
// `Subscription for ${event.data.object.customer_email} was updated.`
|
|
||||||
// );
|
|
||||||
// await handleUpdateSubscription(event.data.object);
|
|
||||||
// break;
|
|
||||||
// case "customer.subscription.deleted":
|
|
||||||
// console.log(
|
|
||||||
// `Subscription for ${event.data.object.customer_email} was deleted.`
|
|
||||||
// );
|
|
||||||
// await handleDeleteSubscription(event.data.object);
|
|
||||||
// break;
|
|
||||||
// default:
|
|
||||||
// // Unexpected event type
|
|
||||||
// console.log(`Unhandled event type ${event.type}.`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Handle the event based on its type
|
// Handle the event based on its type
|
||||||
const eventType = event.type;
|
const eventType = event.type;
|
||||||
const data = event.data.object;
|
const data = event.data.object;
|
||||||
|
|
Ŝarĝante…
Reference in New Issue