1
1
import { PaymentType , Prisma } from "@prisma/client" ;
2
2
import Stripe from "stripe" ;
3
3
import { v4 as uuidv4 } from "uuid" ;
4
+ import { z } from "zod" ;
4
5
5
6
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug" ;
6
7
import { getErrorFromUnknown } from "@calcom/lib/errors" ;
@@ -17,8 +18,15 @@ export type PaymentInfo = {
17
18
id ?: string | null ;
18
19
} ;
19
20
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
+ } ) ;
22
30
23
31
export async function handlePayment (
24
32
evt : CalendarEvent ,
@@ -35,13 +43,10 @@ export async function handlePayment(
35
43
}
36
44
) {
37
45
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 ) ;
40
47
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 ) ;
45
50
46
51
const params : Stripe . PaymentIntentCreateParams = {
47
52
amount : selectedEventType . price ,
0 commit comments