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