commit
5f60e9833e
|
@ -184,27 +184,20 @@ export default async function deleteUserById(
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (user.subscriptions?.id) {
|
if (user.subscriptions?.id) {
|
||||||
const listByEmail = await stripe.customers.list({
|
const deleted = await stripe.subscriptions.cancel(
|
||||||
email: user.email?.toLowerCase(),
|
user.subscriptions.stripeSubscriptionId,
|
||||||
expand: ["data.subscriptions"],
|
{
|
||||||
});
|
cancellation_details: {
|
||||||
|
comment: body.cancellation_details?.comment,
|
||||||
|
feedback: body.cancellation_details?.feedback,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
if (listByEmail.data[0].subscriptions?.data[0].id) {
|
return {
|
||||||
const deleted = await stripe.subscriptions.cancel(
|
response: deleted,
|
||||||
listByEmail.data[0].subscriptions?.data[0].id,
|
status: 200,
|
||||||
{
|
};
|
||||||
cancellation_details: {
|
|
||||||
comment: body.cancellation_details?.comment,
|
|
||||||
feedback: body.cancellation_details?.feedback,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
response: deleted,
|
|
||||||
status: 200,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} else if (user.parentSubscription?.id && user && user.emailVerified) {
|
} else if (user.parentSubscription?.id && user && user.emailVerified) {
|
||||||
await updateSeats(
|
await updateSeats(
|
||||||
user.parentSubscription.stripeSubscriptionId,
|
user.parentSubscription.stripeSubscriptionId,
|
||||||
|
|
|
@ -7,6 +7,11 @@ type Data = {
|
||||||
quantity: number;
|
quantity: number;
|
||||||
periodStart: number;
|
periodStart: number;
|
||||||
periodEnd: number;
|
periodEnd: number;
|
||||||
|
action:
|
||||||
|
| "customer.subscription.created"
|
||||||
|
| "customer.subscription.updated"
|
||||||
|
| "customer.subscription.deleted"
|
||||||
|
| "customer.subscription.cancelled";
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function handleSubscription({
|
export default async function handleSubscription({
|
||||||
|
@ -15,6 +20,7 @@ export default async function handleSubscription({
|
||||||
quantity,
|
quantity,
|
||||||
periodStart,
|
periodStart,
|
||||||
periodEnd,
|
periodEnd,
|
||||||
|
action,
|
||||||
}: Data) {
|
}: Data) {
|
||||||
const subscription = await prisma.subscription.findUnique({
|
const subscription = await prisma.subscription.findUnique({
|
||||||
where: {
|
where: {
|
||||||
|
@ -57,7 +63,13 @@ export default async function handleSubscription({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!user) throw new Error("User not found");
|
if (!user) {
|
||||||
|
if (action === "customer.subscription.deleted") {
|
||||||
|
return "User not found or deleted";
|
||||||
|
} else {
|
||||||
|
throw new Error("User not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const userId = user.id;
|
const userId = user.id;
|
||||||
|
|
||||||
|
|
|
@ -72,6 +72,7 @@ export default async function webhook(
|
||||||
quantity: data?.quantity ?? 1,
|
quantity: data?.quantity ?? 1,
|
||||||
periodStart: data.current_period_start,
|
periodStart: data.current_period_start,
|
||||||
periodEnd: data.current_period_end,
|
periodEnd: data.current_period_end,
|
||||||
|
action: "customer.subscription.created",
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -82,6 +83,7 @@ export default async function webhook(
|
||||||
quantity: data?.quantity ?? 1,
|
quantity: data?.quantity ?? 1,
|
||||||
periodStart: data.current_period_start,
|
periodStart: data.current_period_start,
|
||||||
periodEnd: data.current_period_end,
|
periodEnd: data.current_period_end,
|
||||||
|
action: "customer.subscription.updated",
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -92,6 +94,7 @@ export default async function webhook(
|
||||||
quantity: data?.lines?.data[0]?.quantity ?? 1,
|
quantity: data?.lines?.data[0]?.quantity ?? 1,
|
||||||
periodStart: data.current_period_start,
|
periodStart: data.current_period_start,
|
||||||
periodEnd: data.current_period_end,
|
periodEnd: data.current_period_end,
|
||||||
|
action: "customer.subscription.deleted",
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -102,6 +105,7 @@ export default async function webhook(
|
||||||
quantity: data?.lines?.data[0]?.quantity ?? 1,
|
quantity: data?.lines?.data[0]?.quantity ?? 1,
|
||||||
periodStart: data.current_period_start,
|
periodStart: data.current_period_start,
|
||||||
periodEnd: data.current_period_end,
|
periodEnd: data.current_period_end,
|
||||||
|
action: "customer.subscription.cancelled",
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
Ŝarĝante…
Reference in New Issue