perf: move stripe to billing service out of app store (#20376)

* add more methods to billingService

* update profile handler

* stripe customer handler

* verify email

* move imports + move generic methods to billing service

* move to strip ebilling service class

* push changes to mocks

* fix enum

* fix typo

* correctly update customer metadata

* fix userId type to number

* fix types on billing services

* fix type errors due to return methods differing

* fix return types
This commit is contained in:
sean-brydon authored and GitHub committed 2025-03-31 09:20:27 +00:00
1 parent f15d347804
commit 5cd2ba1362
17 files changed
+116 -58

No files matched your search

@@ -3,7 +3,7 @@ import type { NextApiRequest } from "next";
import { createMocks } from "node-mocks-http";
import { describe, it, expect, vi, beforeEach } from "vitest";
import stripe from "@calcom/app-store/stripepayment/lib/server";
import stripe from "@calcom/features/ee/payments/server/stripe";
import { stripeWebhookHandler, HttpCode } from "./__handler";
@@ -11,7 +11,7 @@ vi.mock("micro", () => ({
buffer: vi.fn(),
}));
vi.mock("@calcom/app-store/stripepayment/lib/server", () => ({
vi.mock("@calcom/features/ee/payments/server/stripe", () => ({
default: {
webhooks: {
constructEvent: vi.fn(),
@@ -3,7 +3,7 @@ import { buffer } from "micro";
import type { NextApiRequest } from "next";
import type Stripe from "stripe";
import stripe from "@calcom/app-store/stripepayment/lib/server";
import stripe from "@calcom/features/ee/payments/server/stripe";
import { HttpError } from "@calcom/lib/http-error";
/** Stripe Webhook Handler Mappings */
@@ -31,6 +31,19 @@ export interface BillingService {
metadata?: Record<string, string | number>;
mode?: "subscription" | "setup" | "payment";
allowPromotionCodes?: boolean;
customerUpdate?: {
address?: "auto" | "never";
};
automaticTax?: {
enabled: boolean;
};
discounts?: Array<{
coupon: string;
}>;
subscriptionData?: {
metadata?: Record<string, string | number>;
trial_period_days?: number;
};
}): Promise<{ checkoutUrl: string | null; sessionId: string }>;
// Price management
@@ -42,5 +55,11 @@ export interface BillingService {
productId: string;
metadata?: Record<string, string | number>;
}): Promise<{ priceId: string }>;
getPrice(priceId: string): Promise<Stripe.Price | null>;
getSubscriptionStatus(subscriptionId: string): Promise<Stripe.Subscription.Status | null>;
getCheckoutSession(checkoutSessionId: string): Promise<Stripe.Checkout.Session | null>;
getCustomer(customerId: string): Promise<Stripe.Customer | Stripe.DeletedCustomer | null>;
getSubscriptions(customerId: string): Promise<Stripe.Subscription[] | null>;
updateCustomer(args: { customerId: string; email: string; userId?: number }): Promise<void>;
}
@@ -50,6 +50,10 @@ export class StripeBillingService implements BillingService {
metadata,
mode = "subscription",
allowPromotionCodes = true,
customerUpdate,
automaticTax,
discounts,
subscriptionData,
} = args;
const session = await this.stripe.checkout.sessions.create({
@@ -65,6 +69,10 @@ export class StripeBillingService implements BillingService {
},
],
allow_promotion_codes: allowPromotionCodes,
customer_update: customerUpdate,
automatic_tax: automaticTax,
discounts,
subscription_data: subscriptionData,
});
return {
@@ -122,4 +130,32 @@ export class StripeBillingService implements BillingService {
return subscription.status;
}
async getCheckoutSession(checkoutSessionId: string) {
const checkoutSession = await this.stripe.checkout.sessions.retrieve(checkoutSessionId);
return checkoutSession;
}
async getCustomer(customerId: string) {
const customer = await this.stripe.customers.retrieve(customerId);
return customer;
}
async getSubscriptions(customerId: string) {
const subscriptions = await this.stripe.subscriptions.list({ customer: customerId });
return subscriptions.data;
}
async updateCustomer(args: Parameters<BillingService["updateCustomer"]>[0]) {
const { customerId, email, userId } = args;
const metadata: { email?: string; userId?: number } = {};
if (email) metadata.email = email;
if (userId) metadata.userId = userId;
await this.stripe.customers.update(customerId, { metadata });
}
async getPrice(priceId: string) {
const price = await this.stripe.prices.retrieve(priceId);
return price;
}
}
+1 -1
View File
@@ -3,11 +3,11 @@ import { buffer } from "micro";
import type { NextApiRequest, NextApiResponse } from "next";
import type Stripe from "stripe";
import stripe from "@calcom/app-store/stripepayment/lib/server";
import { sendAttendeeRequestEmailAndSMS, sendOrganizerRequestEmail } from "@calcom/emails";
import { doesBookingRequireConfirmation } from "@calcom/features/bookings/lib/doesBookingRequireConfirmation";
import { getAllCredentials } from "@calcom/features/bookings/lib/getAllCredentialsForUsersOnEvent/getAllCredentials";
import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirmation";
import stripe from "@calcom/features/ee/payments/server/stripe";
import EventManager from "@calcom/lib/EventManager";
import { IS_PRODUCTION } from "@calcom/lib/constants";
import { getErrorFromUnknown } from "@calcom/lib/errors";
@@ -2,7 +2,8 @@ import prismock from "../../../../../tests/libs/__mocks__/prisma";
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import stripe from "@calcom/app-store/stripepayment/lib/server";
import stripe from "@calcom/features/ee/payments/server/stripe";
import { BillingPeriod } from "@calcom/prisma/zod-utils";
import {
getTeamWithPaymentMetadata,
@@ -41,7 +42,7 @@ vi.mock("@calcom/lib/constant", () => {
};
});
vi.mock("@calcom/app-store/stripepayment/lib/server", () => {
vi.mock("@calcom/features/ee/payments/server/stripe", () => {
return {
default: {
checkout: {
@@ -182,7 +183,7 @@ describe("purchaseTeamOrOrgSubscription", () => {
userId: user.id,
isOrg: true,
pricePerSeat: 100,
billingPeriod: "MONTHLY",
billingPeriod: BillingPeriod.MONTHLY,
})
).toEqual({ url: "SESSION_URL" });
@@ -253,7 +254,7 @@ describe("purchaseTeamOrOrgSubscription", () => {
userId: user.id,
isOrg: true,
pricePerSeat: 100,
billingPeriod: "ANNUALLY",
billingPeriod: BillingPeriod.ANNUALLY,
})
).toEqual({ url: "SESSION_URL" });
@@ -324,7 +325,8 @@ describe("purchaseTeamOrOrgSubscription", () => {
seatsToChargeFor,
userId: user.id,
isOrg: true,
billingPeriod: "ANNUALLY",
billingPeriod: BillingPeriod.ANNUALLY,
pricePerSeat: null,
})
).toEqual({ url: "SESSION_URL" });
+1 -1
View File
@@ -2,8 +2,8 @@ import type Stripe from "stripe";
import { z } from "zod";
import { getStripeCustomerIdFromUserId } from "@calcom/app-store/stripepayment/lib/customer";
import stripe from "@calcom/app-store/stripepayment/lib/server";
import { getDubCustomer } from "@calcom/features/auth/lib/dub";
import stripe from "@calcom/features/ee/payments/server/stripe";
import {
IS_PRODUCTION,
MINIMUM_NUMBER_OF_ORG_SEATS,