better stripe logic
This commit is contained in:
parent
8dfd1598f3
commit
09ea45eec0
|
@ -2,8 +2,7 @@ import Stripe from "stripe";
|
||||||
|
|
||||||
export default async function checkSubscription(
|
export default async function checkSubscription(
|
||||||
stripeSecretKey: string,
|
stripeSecretKey: string,
|
||||||
email: string,
|
email: string
|
||||||
priceId: string
|
|
||||||
) {
|
) {
|
||||||
const stripe = new Stripe(stripeSecretKey, {
|
const stripe = new Stripe(stripeSecretKey, {
|
||||||
apiVersion: "2022-11-15",
|
apiVersion: "2022-11-15",
|
||||||
|
@ -33,11 +32,7 @@ export default async function checkSubscription(
|
||||||
new Date((subscription.canceled_at + secondsInTwoWeeks) * 1000)
|
new Date((subscription.canceled_at + secondsInTwoWeeks) * 1000)
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return subscription?.items?.data[0].plan && isNotCanceledOrHasTime;
|
||||||
subscription?.items?.data?.some(
|
|
||||||
(subscriptionItem) => subscriptionItem?.plan?.id === priceId
|
|
||||||
) && isNotCanceledOrHasTime
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -160,12 +160,10 @@ export default async function updateUser(
|
||||||
}
|
}
|
||||||
|
|
||||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||||
const PRICE_ID = process.env.PRICE_ID;
|
|
||||||
|
|
||||||
if (STRIPE_SECRET_KEY && PRICE_ID && emailEnabled)
|
if (STRIPE_SECRET_KEY && emailEnabled)
|
||||||
await updateCustomerEmail(
|
await updateCustomerEmail(
|
||||||
STRIPE_SECRET_KEY,
|
STRIPE_SECRET_KEY,
|
||||||
PRICE_ID,
|
|
||||||
sessionUser.email,
|
sessionUser.email,
|
||||||
user.email as string
|
user.email as string
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import Stripe from "stripe";
|
import Stripe from "stripe";
|
||||||
import checkSubscription from "./checkSubscription";
|
|
||||||
|
|
||||||
export default async function paymentCheckout(
|
export default async function paymentCheckout(
|
||||||
stripeSecretKey: string,
|
stripeSecretKey: string,
|
||||||
|
|
|
@ -2,7 +2,6 @@ import Stripe from "stripe";
|
||||||
|
|
||||||
export default async function updateCustomerEmail(
|
export default async function updateCustomerEmail(
|
||||||
stripeSecretKey: string,
|
stripeSecretKey: string,
|
||||||
priceId: string,
|
|
||||||
email: string,
|
email: string,
|
||||||
newEmail: string
|
newEmail: string
|
||||||
) {
|
) {
|
||||||
|
@ -30,11 +29,7 @@ export default async function updateCustomerEmail(
|
||||||
new Date((subscription.canceled_at + secondsInTwoWeeks) * 1000)
|
new Date((subscription.canceled_at + secondsInTwoWeeks) * 1000)
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return subscription?.items?.data[0].plan && isNotCanceledOrHasTime;
|
||||||
subscription?.items?.data?.some(
|
|
||||||
(subscriptionItem) => subscriptionItem?.plan?.id === priceId
|
|
||||||
) && isNotCanceledOrHasTime
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -91,7 +91,6 @@ export const authOptions: AuthOptions = {
|
||||||
// Using the `...rest` parameter to be able to narrow down the type based on `trigger`
|
// Using the `...rest` parameter to be able to narrow down the type based on `trigger`
|
||||||
async jwt({ token, trigger, session, user }) {
|
async jwt({ token, trigger, session, user }) {
|
||||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||||
const PRICE_ID = process.env.PRICE_ID;
|
|
||||||
|
|
||||||
const NEXT_PUBLIC_TRIAL_PERIOD_DAYS =
|
const NEXT_PUBLIC_TRIAL_PERIOD_DAYS =
|
||||||
process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS;
|
process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS;
|
||||||
|
@ -108,13 +107,11 @@ export const authOptions: AuthOptions = {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
STRIPE_SECRET_KEY &&
|
STRIPE_SECRET_KEY &&
|
||||||
PRICE_ID &&
|
|
||||||
(trigger || subscriptionIsTimesUp || !token.isSubscriber)
|
(trigger || subscriptionIsTimesUp || !token.isSubscriber)
|
||||||
) {
|
) {
|
||||||
const subscription = await checkSubscription(
|
const subscription = await checkSubscription(
|
||||||
STRIPE_SECRET_KEY,
|
STRIPE_SECRET_KEY,
|
||||||
token.email as string,
|
token.email as string
|
||||||
PRICE_ID
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (subscription.subscriptionCanceledAt) {
|
if (subscription.subscriptionCanceledAt) {
|
||||||
|
|
Ŝarĝante…
Reference in New Issue