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 <peeroke@gmail.com>
Co-authored-by: zomars <zomars@me.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Joe Au-Yeung 2022-06-20 15:09:22 -04:00 committed by GitHub
parent 89ef6c3f13
commit b339736a6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 4 deletions

View File

@ -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"
}

View File

@ -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,

View File

@ -1,13 +1,26 @@
import Stripe from "stripe";
import { z } from "zod";
export type PaymentData = Stripe.Response<Stripe.PaymentIntent> & {
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<typeof stripeDataSchema>;
const stripePrivateKey = process.env.STRIPE_PRIVATE_KEY!;