chore: App-router-migration(/auth/verify) add missing metadata / page & extract components into /module (#16444)
* create StripeRepository and simplify code * verify-email-change: extract getServerSideProps * add verify-email-change to app router * auth/verify: Add metadata in app router * auth/verify: extract client component into /modules * fix type checks for stripe * fix stripe session handler * improve getServerSideProps for verify-email-change * add title to verify-email-change page metadata * add title to verify page metadata * refactor * add safety * fix * remove references to pages for verify-email --------- Co-authored-by: Joe Au-Yeung <[email protected]> Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
12 files changed
+553
-459
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import stripe from "@calcom/app-store/stripepayment/lib/server";
|
||||
import {
|
||||
ZStripeCheckoutSessionInputSchema,
|
||||
type TStripeCheckoutSessionInputSchema,
|
||||
} from "@calcom/trpc/server/routers/publicViewer/stripeCheckoutSession.schema";
|
||||
|
||||
export class StripeRepository {
|
||||
static async getCheckoutSession(input: TStripeCheckoutSessionInputSchema) {
|
||||
const { checkoutSessionId, stripeCustomerId } = input;
|
||||
|
||||
// Moved the following data checks to superRefine
|
||||
ZStripeCheckoutSessionInputSchema.parse(input);
|
||||
|
||||
let customerId: string;
|
||||
let isPremiumUsername = false;
|
||||
let hasPaymentFailed = false;
|
||||
if (checkoutSessionId) {
|
||||
try {
|
||||
const session = await stripe.checkout.sessions.retrieve(checkoutSessionId);
|
||||
if (typeof session.customer !== "string") {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
customerId = session.customer;
|
||||
isPremiumUsername = true;
|
||||
hasPaymentFailed = session.payment_status !== "paid";
|
||||
} catch (e) {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
customerId = stripeCustomerId!;
|
||||
}
|
||||
|
||||
try {
|
||||
const customer = await stripe.customers.retrieve(customerId);
|
||||
if (customer.deleted) {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
hasPaymentFailed,
|
||||
isPremiumUsername,
|
||||
customer: {
|
||||
username: customer.metadata.username,
|
||||
email: customer.metadata.email,
|
||||
stripeCustomerId: customerId,
|
||||
},
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,13 @@
|
||||
import stripe from "@calcom/app-store/stripepayment/lib/server";
|
||||
import { StripeRepository } from "@calcom/lib/server/repository/stripe";
|
||||
|
||||
import type { TStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema";
|
||||
import { ZStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema";
|
||||
|
||||
type StripeCheckoutSessionOptions = {
|
||||
input: TStripeCheckoutSessionInputSchema;
|
||||
};
|
||||
|
||||
export const stripeCheckoutSessionHandler = async ({ input }: StripeCheckoutSessionOptions) => {
|
||||
const { checkoutSessionId, stripeCustomerId } = input;
|
||||
|
||||
// Moved the following data checks to superRefine
|
||||
ZStripeCheckoutSessionInputSchema.parse(input);
|
||||
|
||||
let customerId: string;
|
||||
let isPremiumUsername = false;
|
||||
let hasPaymentFailed = false;
|
||||
if (checkoutSessionId) {
|
||||
try {
|
||||
const session = await stripe.checkout.sessions.retrieve(checkoutSessionId);
|
||||
if (typeof session.customer !== "string") {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
customerId = session.customer;
|
||||
isPremiumUsername = true;
|
||||
hasPaymentFailed = session.payment_status !== "paid";
|
||||
} catch (e) {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
customerId = stripeCustomerId!;
|
||||
}
|
||||
|
||||
try {
|
||||
const customer = await stripe.customers.retrieve(customerId);
|
||||
if (customer.deleted) {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
hasPaymentFailed,
|
||||
isPremiumUsername,
|
||||
customer: {
|
||||
username: customer.metadata.username,
|
||||
email: customer.metadata.email,
|
||||
stripeCustomerId: customerId,
|
||||
},
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
return await StripeRepository.getCheckoutSession(input);
|
||||
};
|
||||
|
||||
export default stripeCheckoutSessionHandler;
|
||||
Reference in new issue
Block a user