Ensures json fields on each call (#2893)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/2894/head^2
Omar López 2022-05-26 10:01:12 -06:00 committed by GitHub
parent 9710c6faa6
commit 24a3bfdf78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import { PaymentType, Prisma } from "@prisma/client"; import { PaymentType, Prisma } from "@prisma/client";
import Stripe from "stripe"; import Stripe from "stripe";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { z } from "zod";
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug"; import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
import { getErrorFromUnknown } from "@calcom/lib/errors"; import { getErrorFromUnknown } from "@calcom/lib/errors";
@ -17,8 +18,15 @@ export type PaymentInfo = {
id?: string | null; id?: string | null;
}; };
let paymentFeePercentage: number | undefined; const stripeKeysSchema = z.object({
let paymentFeeFixed: number | undefined; payment_fee_fixed: z.number(),
payment_fee_percentage: z.number(),
});
const stripeCredentialSchema = z.object({
stripe_user_id: z.string(),
stripe_publishable_key: z.string(),
});
export async function handlePayment( export async function handlePayment(
evt: CalendarEvent, evt: CalendarEvent,
@ -35,13 +43,10 @@ export async function handlePayment(
} }
) { ) {
const appKeys = await getAppKeysFromSlug("stripe"); const appKeys = await getAppKeysFromSlug("stripe");
if (typeof appKeys.payment_fee_fixed === "number") paymentFeePercentage = appKeys.payment_fee_fixed; const { payment_fee_fixed, payment_fee_percentage } = stripeKeysSchema.parse(appKeys);
if (typeof appKeys.payment_fee_percentage === "number") paymentFeeFixed = appKeys.payment_fee_percentage;
const paymentFee = Math.round( const paymentFee = Math.round(selectedEventType.price * payment_fee_percentage + payment_fee_fixed);
selectedEventType.price * parseFloat(`${paymentFeePercentage}`) + parseInt(`${paymentFeeFixed}`) const { stripe_user_id, stripe_publishable_key } = stripeCredentialSchema.parse(stripeCredential.key);
);
const { stripe_user_id, stripe_publishable_key } = stripeCredential.key as Stripe.OAuthToken;
const params: Stripe.PaymentIntentCreateParams = { const params: Stripe.PaymentIntentCreateParams = {
amount: selectedEventType.price, amount: selectedEventType.price,