Files
calendar/packages/features/ee/billing/api/webhook/_invoice.payment_failed.test.ts
T
sean-brydonGitHubunknown <>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
f4abbb2de1 feat: refactor billing to strategy implemention (#27828)
* factory and statergie

* chore: use correct method of DI

* feat: add onchagne

* add logic to HWM stat

* add webhook resolver methods to each statergy

* move seat tracking + webhooks over to own statergy

* Move to factory base approach

* move logic to correct class

* rename create -> createByTeamId

* fix: remove debug `true ||` overrides from IS_STRIPE_ENABLED and IS_TEAM_BILLING_ENABLED

Remove accidentally committed debug overrides that short-circuited
IS_STRIPE_ENABLED and IS_TEAM_BILLING_ENABLED to always be true,
bypassing Stripe credential checks. This would break self-hosted
instances without Stripe configured.

Identified by cubic (https://cubic.dev)

Co-Authored-By: unknown <>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-10 13:11:36 -03:00

92 lines
2.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { buildMonthlyProrationMetadata } from "../../lib/proration-utils";
import type { SWHMap } from "./__handler";
import handler from "./_invoice.payment_failed";
const onPaymentFailed = vi.fn().mockResolvedValue({ handled: true });
const createBySubscriptionId = vi.fn().mockResolvedValue({ onPaymentFailed });
const getPaymentIntentFailureReason = vi.fn().mockResolvedValue("card_declined");
vi.mock("@calcom/ee/billing/di/containers/Billing", () => ({
getBillingProviderService: () => ({
getPaymentIntentFailureReason,
}),
getSeatBillingStrategyFactory: () => ({ createBySubscriptionId }),
}));
describe("invoice.payment_failed webhook", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("records proration failure with payment intent reason", async () => {
const data = {
object: {
subscription: "sub_123",
payment_intent: "pi_123",
status: "open",
lines: {
data: [
{
metadata: buildMonthlyProrationMetadata({ prorationId: "pr_123" }),
},
],
},
},
} as unknown as SWHMap["invoice.payment_failed"]["data"];
const result = await handler(data);
expect(getPaymentIntentFailureReason).toHaveBeenCalledWith("pi_123");
expect(createBySubscriptionId).toHaveBeenCalledWith("sub_123");
expect(onPaymentFailed).toHaveBeenCalledWith(
{ lines: data.object.lines },
"card_declined"
);
expect(result).toEqual({ success: true, handled: true });
});
it("skips when no subscription on invoice", async () => {
const data = {
object: {
subscription: null,
payment_intent: "pi_123",
status: "open",
lines: { data: [] },
},
} as unknown as SWHMap["invoice.payment_failed"]["data"];
const result = await handler(data);
expect(createBySubscriptionId).not.toHaveBeenCalled();
expect(result).toEqual({ success: true, message: "not a subscription invoice" });
});
it("uses invoice status as fallback when no payment intent", async () => {
const data = {
object: {
subscription: "sub_456",
payment_intent: null,
status: "open",
lines: {
data: [
{
metadata: buildMonthlyProrationMetadata({ prorationId: "pr_456" }),
},
],
},
},
} as unknown as SWHMap["invoice.payment_failed"]["data"];
const result = await handler(data);
expect(getPaymentIntentFailureReason).not.toHaveBeenCalled();
expect(onPaymentFailed).toHaveBeenCalledWith(
{ lines: data.object.lines },
"open"
);
expect(result).toEqual({ success: true, handled: true });
});
});