import stripe billing service dynamically in credit-service (#25058)
Co-authored-by: Morgan <[email protected]>
This commit is contained in:
1 parent
9d39742802
commit
421d03eb1a
4 files changed
+100
-87
No files matched your search
@@ -1,18 +1,14 @@
|
||||
import Stripe from "stripe";
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import * as EmailManager from "@calcom/emails/billing-email-service";
|
||||
import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId";
|
||||
import { CreditsRepository } from "@calcom/lib/server/repository/credits";
|
||||
import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
|
||||
import { TeamRepository } from "@calcom/features/ee/teams/repositories/TeamRepository";
|
||||
import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
|
||||
import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId";
|
||||
import { CreditsRepository } from "@calcom/lib/server/repository/credits";
|
||||
import { CreditType } from "@calcom/prisma/enums";
|
||||
|
||||
import { CreditService } from "./credit-service";
|
||||
import { StripeBillingService } from "./stripe-billing-service";
|
||||
import { InternalTeamBilling } from "./teams/internal-team-billing";
|
||||
|
||||
const MOCK_TX = {
|
||||
@@ -35,7 +31,32 @@ vi.mock("@calcom/prisma", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("stripe");
|
||||
const mockStripe = vi.hoisted(() => ({
|
||||
prices: {
|
||||
list: vi.fn().mockResolvedValue({ data: [] }),
|
||||
retrieve: vi.fn().mockResolvedValue({ id: "price_123", unit_amount: 1000 }),
|
||||
},
|
||||
customers: {
|
||||
create: vi.fn().mockResolvedValue({ id: "cus_123" }),
|
||||
},
|
||||
subscriptions: {
|
||||
cancel: vi.fn(),
|
||||
retrieve: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
checkout: {
|
||||
sessions: {
|
||||
retrieve: vi.fn(),
|
||||
},
|
||||
},
|
||||
paymentIntents: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@calcom/features/ee/payments/server/stripe", () => ({
|
||||
default: mockStripe,
|
||||
}));
|
||||
|
||||
vi.mock("@calcom/lib/server/i18n", () => {
|
||||
return {
|
||||
@@ -104,18 +125,13 @@ CreditsRepository.findCreditBalance.mockResolvedValueOnce({
|
||||
|
||||
describe("CreditService", () => {
|
||||
let creditService: CreditService;
|
||||
let stripeMock: Partial<Stripe>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
|
||||
stripeMock = {
|
||||
prices: {
|
||||
list: vi.fn().mockResolvedValue({ data: [] }),
|
||||
retrieve: vi.fn().mockResolvedValue({ id: "price_123", unit_amount: 1000 }),
|
||||
},
|
||||
};
|
||||
vi.mocked(Stripe).mockImplementation(() => stripeMock as Stripe);
|
||||
mockStripe.prices.retrieve.mockResolvedValue({ id: "price_123", unit_amount: 1000 });
|
||||
mockStripe.customers.create.mockResolvedValue({ id: "cus_123" });
|
||||
|
||||
creditService = new CreditService();
|
||||
|
||||
vi.mocked(CreditsRepository.findCreditExpenseLogByExternalRef).mockResolvedValue(null);
|
||||
@@ -461,12 +477,7 @@ describe("CreditService", () => {
|
||||
mockTeamBillingService.getSubscriptionStatus
|
||||
);
|
||||
|
||||
const mockStripeBillingService = {
|
||||
getPrice: vi.fn().mockResolvedValue({ unit_amount: 1000 }),
|
||||
};
|
||||
vi.spyOn(StripeBillingService.prototype, "getPrice").mockImplementation(
|
||||
mockStripeBillingService.getPrice
|
||||
);
|
||||
mockStripe.prices.retrieve.mockResolvedValue({ id: "price_123", unit_amount: 1000 });
|
||||
|
||||
const result = await creditService.getMonthlyCredits(1);
|
||||
expect(result).toBe(1500); // (3 members * 1000 price) / 2
|
||||
|
||||
@@ -1,35 +1,44 @@
|
||||
import Stripe from "stripe";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
import { StripeBillingService } from "./stripe-billing-service";
|
||||
|
||||
vi.mock("stripe");
|
||||
const mockStripe = vi.hoisted(() => ({
|
||||
subscriptions: {
|
||||
cancel: vi.fn(),
|
||||
retrieve: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
checkout: {
|
||||
sessions: {
|
||||
retrieve: vi.fn(),
|
||||
},
|
||||
},
|
||||
customers: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
paymentIntents: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
prices: {
|
||||
retrieve: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@calcom/features/ee/payments/server/stripe", () => ({
|
||||
default: mockStripe,
|
||||
}));
|
||||
|
||||
describe("StripeBillingService", () => {
|
||||
let stripeBillingService: StripeBillingService;
|
||||
let stripeMock: any;
|
||||
|
||||
beforeEach(() => {
|
||||
stripeMock = {
|
||||
subscriptions: {
|
||||
cancel: vi.fn(),
|
||||
retrieve: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
checkout: {
|
||||
sessions: {
|
||||
retrieve: vi.fn(),
|
||||
},
|
||||
},
|
||||
};
|
||||
(Stripe as any).mockImplementation(() => stripeMock);
|
||||
stripeBillingService = new StripeBillingService();
|
||||
});
|
||||
|
||||
it("should cancel a subscription", async () => {
|
||||
const subscriptionId = "sub_123";
|
||||
await stripeBillingService.handleSubscriptionCancel(subscriptionId);
|
||||
expect(stripeMock.subscriptions.cancel).toHaveBeenCalledWith(subscriptionId);
|
||||
expect(mockStripe.subscriptions.cancel).toHaveBeenCalledWith(subscriptionId);
|
||||
});
|
||||
|
||||
it("should update a subscription", async () => {
|
||||
@@ -38,14 +47,14 @@ describe("StripeBillingService", () => {
|
||||
subscriptionItemId: "item_123",
|
||||
membershipCount: 5,
|
||||
};
|
||||
stripeMock.subscriptions.retrieve.mockResolvedValue({
|
||||
mockStripe.subscriptions.retrieve.mockResolvedValue({
|
||||
items: {
|
||||
data: [{ id: "item_123", quantity: 3 }],
|
||||
},
|
||||
});
|
||||
await stripeBillingService.handleSubscriptionUpdate(args);
|
||||
expect(stripeMock.subscriptions.retrieve).toHaveBeenCalledWith(args.subscriptionId);
|
||||
expect(stripeMock.subscriptions.update).toHaveBeenCalledWith(args.subscriptionId, {
|
||||
expect(mockStripe.subscriptions.retrieve).toHaveBeenCalledWith(args.subscriptionId);
|
||||
expect(mockStripe.subscriptions.update).toHaveBeenCalledWith(args.subscriptionId, {
|
||||
items: [{ quantity: args.membershipCount, id: args.subscriptionItemId }],
|
||||
});
|
||||
});
|
||||
@@ -56,7 +65,7 @@ describe("StripeBillingService", () => {
|
||||
subscriptionItemId: "item_123",
|
||||
membershipCount: 5,
|
||||
};
|
||||
stripeMock.subscriptions.retrieve.mockResolvedValue({
|
||||
mockStripe.subscriptions.retrieve.mockResolvedValue({
|
||||
items: {
|
||||
data: [],
|
||||
},
|
||||
@@ -68,21 +77,21 @@ describe("StripeBillingService", () => {
|
||||
|
||||
it("should return true if checkout session is paid", async () => {
|
||||
const paymentId = "pay_123";
|
||||
stripeMock.checkout.sessions.retrieve.mockResolvedValue({
|
||||
mockStripe.checkout.sessions.retrieve.mockResolvedValue({
|
||||
payment_status: "paid",
|
||||
});
|
||||
const result = await stripeBillingService.checkoutSessionIsPaid(paymentId);
|
||||
expect(result).toBe(true);
|
||||
expect(stripeMock.checkout.sessions.retrieve).toHaveBeenCalledWith(paymentId);
|
||||
expect(mockStripe.checkout.sessions.retrieve).toHaveBeenCalledWith(paymentId);
|
||||
});
|
||||
|
||||
it("should return false if checkout session is not paid", async () => {
|
||||
const paymentId = "pay_123";
|
||||
stripeMock.checkout.sessions.retrieve.mockResolvedValue({
|
||||
mockStripe.checkout.sessions.retrieve.mockResolvedValue({
|
||||
payment_status: "unpaid",
|
||||
});
|
||||
const result = await stripeBillingService.checkoutSessionIsPaid(paymentId);
|
||||
expect(result).toBe(false);
|
||||
expect(stripeMock.checkout.sessions.retrieve).toHaveBeenCalledWith(paymentId);
|
||||
expect(mockStripe.checkout.sessions.retrieve).toHaveBeenCalledWith(paymentId);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import Stripe from "stripe";
|
||||
import type Stripe from "stripe";
|
||||
|
||||
import stripe from "@calcom/features/ee/payments/server/stripe";
|
||||
import logger from "@calcom/lib/logger";
|
||||
|
||||
import type { BillingService } from "./billing-service";
|
||||
@@ -8,9 +9,7 @@ import { SubscriptionStatus } from "./repository/IBillingRepository";
|
||||
export class StripeBillingService implements BillingService {
|
||||
private stripe: Stripe;
|
||||
constructor() {
|
||||
this.stripe = new Stripe(process.env.STRIPE_PRIVATE_KEY!, {
|
||||
apiVersion: "2020-08-27",
|
||||
});
|
||||
this.stripe = stripe;
|
||||
}
|
||||
|
||||
async createCustomer(args: Parameters<BillingService["createCustomer"]>[0]) {
|
||||
|
||||
+32
-38
@@ -1,6 +1,5 @@
|
||||
import prismock from "../../../../../../tests/libs/__mocks__/prisma";
|
||||
|
||||
import Stripe from "stripe";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
|
||||
@@ -18,35 +17,36 @@ vi.stubEnv("STRIPE_ORG_PRODUCT_ID", "test-stripe-org-product-id");
|
||||
vi.stubEnv("NEXT_PUBLIC_ORGANIZATIONS_MIN_SELF_SERVE_SEATS", "30");
|
||||
vi.stubEnv("NEXT_PUBLIC_ORGANIZATIONS_SELF_SERVE_PRICE_NEW", "37");
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
const STRIPE_ORG_PRODUCT_ID = process.env.STRIPE_ORG_PRODUCT_ID!;
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
|
||||
const STRIPE_ORG_MONTHLY_PRICE_ID = process.env.STRIPE_ORG_MONTHLY_PRICE_ID!;
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
|
||||
const checkoutSessionsCreate = vi.fn();
|
||||
const pricesCreate = vi.fn();
|
||||
const customersCreate = vi.fn();
|
||||
const mockSharedStripe = vi.hoisted(() => ({
|
||||
prices: {
|
||||
create: vi.fn(),
|
||||
retrieve: vi.fn().mockResolvedValue({ id: "price_123", unit_amount: 1000 }),
|
||||
},
|
||||
customers: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
subscriptions: {
|
||||
cancel: vi.fn(),
|
||||
retrieve: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
checkout: {
|
||||
sessions: {
|
||||
create: vi.fn(),
|
||||
retrieve: vi.fn(),
|
||||
},
|
||||
},
|
||||
paymentIntents: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
function MockStripe() {
|
||||
return {
|
||||
checkout: {
|
||||
sessions: {
|
||||
create: checkoutSessionsCreate,
|
||||
},
|
||||
},
|
||||
prices: {
|
||||
create: pricesCreate,
|
||||
},
|
||||
customers: {
|
||||
create: customersCreate,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
vi.mock("stripe", () => ({
|
||||
default: vi.fn(),
|
||||
vi.mock("@calcom/features/ee/payments/server/stripe", () => ({
|
||||
default: mockSharedStripe,
|
||||
}));
|
||||
|
||||
const mockInput = {
|
||||
@@ -137,9 +137,7 @@ function expectStripePriceCreated({
|
||||
product: string;
|
||||
recurring: { interval: string };
|
||||
}) {
|
||||
// @ts-expect-error We don't care about arguments to Stripe
|
||||
const stripe = new Stripe();
|
||||
expect(stripe.prices.create).toHaveBeenCalledWith(
|
||||
expect(mockSharedStripe.prices.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
// Multiply by 100 is requirement of Stripe.
|
||||
unit_amount: amount * 100,
|
||||
@@ -151,9 +149,7 @@ function expectStripePriceCreated({
|
||||
}
|
||||
|
||||
function expectStripePriceNotCreated() {
|
||||
// @ts-expect-error We don't care about arguments to Stripe
|
||||
const stripe = new Stripe();
|
||||
expect(stripe.prices.create).not.toHaveBeenCalled();
|
||||
expect(mockSharedStripe.prices.create).not.toHaveBeenCalled();
|
||||
}
|
||||
|
||||
function expectStripeSubscriptionCreated({
|
||||
@@ -165,8 +161,7 @@ function expectStripeSubscriptionCreated({
|
||||
priceId: string;
|
||||
quantity: number;
|
||||
}) {
|
||||
// @ts-expect-error We don't care about arguments to Stripe
|
||||
expect(new Stripe().checkout.sessions.create).toHaveBeenCalledWith(
|
||||
expect(mockSharedStripe.checkout.sessions.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
customer: customerId,
|
||||
line_items: [
|
||||
@@ -191,9 +186,8 @@ describe("createWithPaymentIntent handler", () => {
|
||||
await prismock.reset();
|
||||
mockStripe();
|
||||
function mockStripe() {
|
||||
// @ts-expect-error Not all props are defined on MockStripe
|
||||
vi.mocked(Stripe).mockImplementation(MockStripe);
|
||||
checkoutSessionsCreate.mockImplementation(() => {
|
||||
// Set up the shared Stripe instance mock implementations
|
||||
mockSharedStripe.checkout.sessions.create.mockImplementation(() => {
|
||||
const sessionId = `test-session-id-${uuidv4()}`;
|
||||
lastCreatedSessionId = sessionId;
|
||||
return {
|
||||
@@ -202,7 +196,7 @@ describe("createWithPaymentIntent handler", () => {
|
||||
};
|
||||
});
|
||||
|
||||
pricesCreate.mockImplementation(() => {
|
||||
mockSharedStripe.prices.create.mockImplementation(() => {
|
||||
const priceId = `test-price-id-${uuidv4()}`;
|
||||
lastCreatedPriceId = priceId;
|
||||
return {
|
||||
@@ -210,7 +204,7 @@ describe("createWithPaymentIntent handler", () => {
|
||||
};
|
||||
});
|
||||
|
||||
customersCreate.mockImplementation(() => {
|
||||
mockSharedStripe.customers.create.mockImplementation(() => {
|
||||
const customerId = `test-customer-id-${uuidv4()}`;
|
||||
lastCreatedCustomerId = customerId;
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user