add team invitation functionality [WIP]
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import Stripe from "stripe";
|
||||
|
||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||
|
||||
export default async function checkSubscriptionByEmail(email: string) {
|
||||
if (!STRIPE_SECRET_KEY) return null;
|
||||
|
||||
const stripe = new Stripe(STRIPE_SECRET_KEY, {
|
||||
apiVersion: "2022-11-15",
|
||||
});
|
||||
|
||||
console.log("Request made to Stripe by:", email);
|
||||
const listByEmail = await stripe.customers.list({
|
||||
email: email.toLowerCase(),
|
||||
expand: ["data.subscriptions"],
|
||||
});
|
||||
|
||||
if (listByEmail?.data[0]?.subscriptions?.data[0]) {
|
||||
return {
|
||||
active: (listByEmail.data[0].subscriptions?.data[0] as any).plan.active,
|
||||
stripeSubscriptionId: listByEmail.data[0].subscriptions?.data[0].id,
|
||||
currentPeriodStart:
|
||||
listByEmail.data[0].subscriptions?.data[0].current_period_start * 1000,
|
||||
currentPeriodEnd:
|
||||
listByEmail.data[0].subscriptions?.data[0].current_period_end * 1000,
|
||||
quantity: (listByEmail?.data[0]?.subscriptions?.data[0] as any).quantity,
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import Stripe from "stripe";
|
||||
|
||||
export default async function updateCustomerEmail(
|
||||
stripeSecretKey: string,
|
||||
email: string,
|
||||
newEmail: string
|
||||
) {
|
||||
const stripe = new Stripe(stripeSecretKey, {
|
||||
apiVersion: "2022-11-15",
|
||||
});
|
||||
|
||||
const listByEmail = await stripe.customers.list({
|
||||
email: email.toLowerCase(),
|
||||
expand: ["data.subscriptions"],
|
||||
});
|
||||
|
||||
const customer = listByEmail.data.find((customer, i) => {
|
||||
const hasValidSubscription = customer.subscriptions?.data.some(
|
||||
(subscription) => {
|
||||
const NEXT_PUBLIC_TRIAL_PERIOD_DAYS =
|
||||
process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS;
|
||||
const secondsInTwoWeeks = NEXT_PUBLIC_TRIAL_PERIOD_DAYS
|
||||
? Number(NEXT_PUBLIC_TRIAL_PERIOD_DAYS) * 86400
|
||||
: 1209600;
|
||||
|
||||
const isNotCanceledOrHasTime = !(
|
||||
subscription.canceled_at &&
|
||||
new Date() >
|
||||
new Date((subscription.canceled_at + secondsInTwoWeeks) * 1000)
|
||||
);
|
||||
|
||||
return subscription?.items?.data[0].plan && isNotCanceledOrHasTime;
|
||||
}
|
||||
);
|
||||
|
||||
return (
|
||||
customer.email?.toLowerCase() === email.toLowerCase() &&
|
||||
hasValidSubscription
|
||||
);
|
||||
});
|
||||
|
||||
if (customer)
|
||||
await stripe.customers.update(customer?.id, {
|
||||
email: newEmail.toLowerCase(),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import Stripe from "stripe";
|
||||
|
||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||
|
||||
const updateSeats = async (subscriptionId: string, seats: number) => {
|
||||
if (!STRIPE_SECRET_KEY) {
|
||||
return;
|
||||
}
|
||||
|
||||
const stripe = new Stripe(STRIPE_SECRET_KEY, {
|
||||
apiVersion: "2022-11-15",
|
||||
});
|
||||
|
||||
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
|
||||
|
||||
const trialing = subscription.status === "trialing";
|
||||
|
||||
if (subscription) {
|
||||
await stripe.subscriptions.update(subscriptionId, {
|
||||
billing_cycle_anchor: trialing ? undefined : "now",
|
||||
proration_behavior: trialing ? undefined : "create_prorations",
|
||||
quantity: seats,
|
||||
} as Stripe.SubscriptionUpdateParams);
|
||||
}
|
||||
};
|
||||
|
||||
export default updateSeats;
|
||||
@@ -0,0 +1,65 @@
|
||||
import { prisma } from "../db";
|
||||
import { Subscription, User } from "@prisma/client";
|
||||
import checkSubscriptionByEmail from "./checkSubscriptionByEmail";
|
||||
|
||||
interface UserIncludingSubscription extends User {
|
||||
subscriptions: Subscription | null;
|
||||
}
|
||||
|
||||
export default async function verifySubscription(
|
||||
user?: UserIncludingSubscription | null
|
||||
) {
|
||||
if (!user || !user.subscriptions) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
!user.subscriptions.active ||
|
||||
new Date() > user.subscriptions.currentPeriodEnd
|
||||
) {
|
||||
const subscription = await checkSubscriptionByEmail(user.email as string);
|
||||
|
||||
if (
|
||||
!subscription ||
|
||||
!subscription.stripeSubscriptionId ||
|
||||
!subscription.currentPeriodEnd ||
|
||||
!subscription.currentPeriodStart ||
|
||||
!subscription.quantity
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const {
|
||||
active,
|
||||
stripeSubscriptionId,
|
||||
currentPeriodStart,
|
||||
currentPeriodEnd,
|
||||
quantity,
|
||||
} = subscription;
|
||||
|
||||
await prisma.subscription
|
||||
.upsert({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
create: {
|
||||
active,
|
||||
stripeSubscriptionId,
|
||||
currentPeriodStart: new Date(currentPeriodStart),
|
||||
currentPeriodEnd: new Date(currentPeriodEnd),
|
||||
quantity,
|
||||
userId: user.id,
|
||||
},
|
||||
update: {
|
||||
active,
|
||||
stripeSubscriptionId,
|
||||
currentPeriodStart: new Date(currentPeriodStart),
|
||||
currentPeriodEnd: new Date(currentPeriodEnd),
|
||||
quantity,
|
||||
},
|
||||
})
|
||||
.catch((err) => console.log(err));
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
Reference in New Issue
Block a user