From b339736a6fc32e36bcfc66c7d9fd03707fe3aa90 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Mon, 20 Jun 2022 15:09:22 -0400 Subject: [PATCH] Save currency to db (#3086) * Save currency to db * Add missing translation * Get currency from user credentials server side * Adds stripe data schema Co-authored-by: Peer Richelsen Co-authored-by: zomars Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- apps/web/public/static/locales/en/common.json | 3 ++- apps/web/server/routers/viewer/eventTypes.tsx | 22 +++++++++++++++++++ packages/stripe/server.ts | 19 +++++++++++++--- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index 8e5acbc1e0..84e3b47d3b 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -910,5 +910,6 @@ "add_exchange2013": "Connect Exchange 2013 Server", "add_exchange2016": "Connect Exchange 2016 Server", "specific_issue": "Have a specific issue", - "browse_our_docs": "browse our docs" + "browse_our_docs": "browse our docs", + "attendee_name": "Attendee's name" } diff --git a/apps/web/server/routers/viewer/eventTypes.tsx b/apps/web/server/routers/viewer/eventTypes.tsx index 2b4fbba90e..bbc5374255 100644 --- a/apps/web/server/routers/viewer/eventTypes.tsx +++ b/apps/web/server/routers/viewer/eventTypes.tsx @@ -6,6 +6,7 @@ import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug"; import { _DestinationCalendarModel, _EventTypeCustomInputModel, _EventTypeModel } from "@calcom/prisma/zod"; import { stringOrNumber } from "@calcom/prisma/zod-utils"; import { createEventTypeInput } from "@calcom/prisma/zod/custom/eventtype"; +import { stripeDataSchema } from "@calcom/stripe/server"; import { createProtectedRouter } from "@server/createRouter"; import { viewerRouter } from "@server/routers/viewer"; @@ -265,6 +266,7 @@ export const eventTypesRouter = createProtectedRouter() users, id, hashedLink, + price, ...rest } = input; assertValidUrl(input.successRedirectUrl); @@ -314,6 +316,26 @@ export const eventTypesRouter = createProtectedRouter() }; } + if (price) { + const paymentCredential = await ctx.prisma.credential.findFirst({ + where: { + userId: ctx.user.id, + type: { + contains: "_payment", + }, + }, + select: { + type: true, + key: true, + }, + }); + + if (paymentCredential?.type === "stripe_payment") { + const { default_currency } = stripeDataSchema.parse(paymentCredential.key); + data.currency = default_currency; + } + } + const connectedLink = await ctx.prisma.hashedLink.findFirst({ where: { eventTypeId: input.id, diff --git a/packages/stripe/server.ts b/packages/stripe/server.ts index ffb7a57ec4..7194436a3e 100644 --- a/packages/stripe/server.ts +++ b/packages/stripe/server.ts @@ -1,13 +1,26 @@ import Stripe from "stripe"; +import { z } from "zod"; export type PaymentData = Stripe.Response & { stripe_publishable_key: string; stripeAccount: string; }; -export type StripeData = Stripe.OAuthToken & { - default_currency: string; -}; +export const stripeOAuthTokenSchema = z.object({ + access_token: z.string().optional(), + scope: z.string().optional(), + livemode: z.boolean().optional(), + token_type: z.literal("bearer").optional(), + refresh_token: z.string().optional(), + stripe_user_id: z.string().optional(), + stripe_publishable_key: z.string().optional(), +}); + +export const stripeDataSchema = stripeOAuthTokenSchema.extend({ + default_currency: z.string(), +}); + +export type StripeData = z.infer; const stripePrivateKey = process.env.STRIPE_PRIVATE_KEY!;