Skip to content

Commit 4114975

Browse files
zomarskodiakhq[bot]
andcommitted
Ensures json fields on each call (#2893)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 1bd6d03 commit 4114975

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

apps/web/ee/lib/stripe/server.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PaymentType, Prisma } from "@prisma/client";
22
import Stripe from "stripe";
33
import { v4 as uuidv4 } from "uuid";
4+
import { z } from "zod";
45

56
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
67
import { getErrorFromUnknown } from "@calcom/lib/errors";
@@ -17,8 +18,15 @@ export type PaymentInfo = {
1718
id?: string | null;
1819
};
1920

20-
let paymentFeePercentage: number | undefined;
21-
let paymentFeeFixed: number | undefined;
21+
const stripeKeysSchema = z.object({
22+
payment_fee_fixed: z.number(),
23+
payment_fee_percentage: z.number(),
24+
});
25+
26+
const stripeCredentialSchema = z.object({
27+
stripe_user_id: z.string(),
28+
stripe_publishable_key: z.string(),
29+
});
2230

2331
export async function handlePayment(
2432
evt: CalendarEvent,
@@ -35,13 +43,10 @@ export async function handlePayment(
3543
}
3644
) {
3745
const appKeys = await getAppKeysFromSlug("stripe");
38-
if (typeof appKeys.payment_fee_fixed === "number") paymentFeePercentage = appKeys.payment_fee_fixed;
39-
if (typeof appKeys.payment_fee_percentage === "number") paymentFeeFixed = appKeys.payment_fee_percentage;
46+
const { payment_fee_fixed, payment_fee_percentage } = stripeKeysSchema.parse(appKeys);
4047

41-
const paymentFee = Math.round(
42-
selectedEventType.price * parseFloat(`${paymentFeePercentage}`) + parseInt(`${paymentFeeFixed}`)
43-
);
44-
const { stripe_user_id, stripe_publishable_key } = stripeCredential.key as Stripe.OAuthToken;
48+
const paymentFee = Math.round(selectedEventType.price * payment_fee_percentage + payment_fee_fixed);
49+
const { stripe_user_id, stripe_publishable_key } = stripeCredentialSchema.parse(stripeCredential.key);
4550

4651
const params: Stripe.PaymentIntentCreateParams = {
4752
amount: selectedEventType.price,

0 commit comments

Comments
 (0)