implemented stripe for the cloud instance
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import Stripe from "stripe";
|
||||
|
||||
export default async function checkSubscription(
|
||||
stripeSecretKey: string,
|
||||
email: string,
|
||||
priceId: string
|
||||
) {
|
||||
const stripe = new Stripe(stripeSecretKey, {
|
||||
apiVersion: "2022-11-15",
|
||||
});
|
||||
|
||||
const listByEmail = await stripe.customers.list({
|
||||
email: email.toLowerCase(),
|
||||
expand: ["data.subscriptions"],
|
||||
});
|
||||
|
||||
let subscriptionCanceledAt: number | null | undefined;
|
||||
|
||||
const isSubscriber = listByEmail.data.some((customer, i) => {
|
||||
const hasValidSubscription = customer.subscriptions?.data.some(
|
||||
(subscription) => {
|
||||
const secondsInTwoWeeks = 1209600;
|
||||
|
||||
subscriptionCanceledAt = subscription.canceled_at;
|
||||
|
||||
const isNotCanceledOrHasTime = !(
|
||||
subscription.canceled_at &&
|
||||
new Date() >
|
||||
new Date((subscription.canceled_at + secondsInTwoWeeks) * 1000)
|
||||
);
|
||||
|
||||
return (
|
||||
subscription?.items?.data?.some(
|
||||
(subscriptionItem) => subscriptionItem?.plan?.id === priceId
|
||||
) && isNotCanceledOrHasTime
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
return (
|
||||
customer.email?.toLowerCase() === email.toLowerCase() &&
|
||||
hasValidSubscription
|
||||
);
|
||||
});
|
||||
|
||||
return {
|
||||
isSubscriber,
|
||||
subscriptionCanceledAt,
|
||||
};
|
||||
}
|
||||
@@ -3,10 +3,16 @@ import { AccountSettings } from "@/types/global";
|
||||
import bcrypt from "bcrypt";
|
||||
import removeFile from "@/lib/api/storage/removeFile";
|
||||
import createFile from "@/lib/api/storage/createFile";
|
||||
import updateCustomerEmail from "../../updateCustomerEmail";
|
||||
|
||||
export default async function updateUser(
|
||||
user: AccountSettings,
|
||||
userId: number
|
||||
sessionUser: {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
isSubscriber: boolean;
|
||||
}
|
||||
) {
|
||||
if (!user.username || !user.email)
|
||||
return {
|
||||
@@ -24,7 +30,7 @@ export default async function updateUser(
|
||||
const base64Data = profilePic.replace(/^data:image\/jpeg;base64,/, "");
|
||||
|
||||
await createFile({
|
||||
filePath: `uploads/avatar/${userId}.jpg`,
|
||||
filePath: `uploads/avatar/${sessionUser.id}.jpg`,
|
||||
data: base64Data,
|
||||
isBase64: true,
|
||||
});
|
||||
@@ -39,7 +45,7 @@ export default async function updateUser(
|
||||
};
|
||||
}
|
||||
} else if (profilePic == "") {
|
||||
removeFile({ filePath: `uploads/avatar/${userId}.jpg` });
|
||||
removeFile({ filePath: `uploads/avatar/${sessionUser.id}.jpg` });
|
||||
}
|
||||
|
||||
// Other settings
|
||||
@@ -49,7 +55,7 @@ export default async function updateUser(
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: {
|
||||
id: userId,
|
||||
id: sessionUser.id,
|
||||
},
|
||||
data: {
|
||||
name: user.name,
|
||||
@@ -64,6 +70,17 @@ export default async function updateUser(
|
||||
},
|
||||
});
|
||||
|
||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||
const PRICE_ID = process.env.PRICE_ID;
|
||||
|
||||
if (STRIPE_SECRET_KEY && PRICE_ID)
|
||||
await updateCustomerEmail(
|
||||
STRIPE_SECRET_KEY,
|
||||
PRICE_ID,
|
||||
sessionUser.email,
|
||||
user.email
|
||||
);
|
||||
|
||||
const { password, ...userInfo } = updatedUser;
|
||||
|
||||
const response: Omit<AccountSettings, "password"> = {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import Stripe from "stripe";
|
||||
import checkSubscription from "./checkSubscription";
|
||||
|
||||
export default async function paymentCheckout(
|
||||
stripeSecretKey: string,
|
||||
email: string,
|
||||
action: "register" | "login",
|
||||
priceId: string
|
||||
) {
|
||||
const stripe = new Stripe(stripeSecretKey, {
|
||||
apiVersion: "2022-11-15",
|
||||
});
|
||||
|
||||
// const a = await stripe.prices.retrieve("price_1NTn3PDaRUw6CJPLkw4dcwlJ");
|
||||
|
||||
// const listBySub = await stripe.subscriptions.list({
|
||||
// customer: "cus_OGUzJrRea8Qbxx",
|
||||
// });
|
||||
|
||||
const listByEmail = await stripe.customers.list({
|
||||
email: email.toLowerCase(),
|
||||
expand: ["data.subscriptions"],
|
||||
});
|
||||
|
||||
const isExistingCostomer = listByEmail?.data[0]?.id || undefined;
|
||||
|
||||
// const hasPreviouslySubscribed = listByEmail.data.find((customer, i) => {
|
||||
// const hasValidSubscription = customer.subscriptions?.data.some(
|
||||
// (subscription) => {
|
||||
// return subscription?.items?.data?.some(
|
||||
// (subscriptionItem) => subscriptionItem?.plan?.id === priceId
|
||||
// );
|
||||
// }
|
||||
// );
|
||||
|
||||
// return (
|
||||
// customer.email?.toLowerCase() === email.toLowerCase() &&
|
||||
// hasValidSubscription
|
||||
// );
|
||||
// });
|
||||
|
||||
// const previousSubscriptionId =
|
||||
// hasPreviouslySubscribed?.subscriptions?.data[0].id;
|
||||
|
||||
// if (previousSubscriptionId) {
|
||||
// console.log(previousSubscriptionId);
|
||||
// const subscription = await stripe.subscriptions.resume(
|
||||
// previousSubscriptionId
|
||||
// );
|
||||
// }
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
customer: isExistingCostomer ? isExistingCostomer : undefined,
|
||||
line_items: [
|
||||
{
|
||||
price: priceId,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
mode: "subscription",
|
||||
customer_email: isExistingCostomer ? undefined : email.toLowerCase(),
|
||||
success_url: "http://localhost:3000?session_id={CHECKOUT_SESSION_ID}",
|
||||
cancel_url: "http://localhost:3000/login",
|
||||
automatic_tax: {
|
||||
enabled: true,
|
||||
},
|
||||
subscription_data: {
|
||||
trial_period_days: 14,
|
||||
},
|
||||
});
|
||||
|
||||
return { response: session.url, status: 200 };
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import Stripe from "stripe";
|
||||
|
||||
export default async function updateCustomerEmail(
|
||||
stripeSecretKey: string,
|
||||
priceId: 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 secondsInTwoWeeks = 1209600;
|
||||
|
||||
const isNotCanceledOrHasTime = !(
|
||||
subscription.canceled_at &&
|
||||
new Date() >
|
||||
new Date((subscription.canceled_at + secondsInTwoWeeks) * 1000)
|
||||
);
|
||||
|
||||
return (
|
||||
subscription?.items?.data?.some(
|
||||
(subscriptionItem) => subscriptionItem?.plan?.id === priceId
|
||||
) && isNotCanceledOrHasTime
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
return (
|
||||
customer.email?.toLowerCase() === email.toLowerCase() &&
|
||||
hasValidSubscription
|
||||
);
|
||||
});
|
||||
|
||||
if (customer)
|
||||
await stripe.customers.update(customer?.id, {
|
||||
email: newEmail.toLowerCase(),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user