Skip to content

The open alternative to Salesforce, designed for AI.

Group pending invoice items monthly for invoice-paying subscriptions (#27826)

Marie289287e10d5d
diff --git a/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-webhook-subscription.service.ts b/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-webhook-subscription.service.ts
index 5a28e518664..d7dbbe67c70 100644
--- a/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-webhook-subscription.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-webhook-subscription.service.ts
@@ -13,6 +13,7 @@ import type Stripe from 'stripe';
import { getDeletedStripeSubscriptionItemIdsFromStripeSubscriptionEvent } from 'src/engine/core-modules/billing-webhook/utils/get-deleted-stripe-subscription-item-ids-from-stripe-subscription-event.util';
import { transformStripeSubscriptionEventToDatabaseCustomer } from 'src/engine/core-modules/billing-webhook/utils/transform-stripe-subscription-event-to-database-customer.util';
import { transformStripeSubscriptionEventToDatabaseSubscriptionItem } from 'src/engine/core-modules/billing-webhook/utils/transform-stripe-subscription-event-to-database-subscription-item.util';
+import { shouldSetPendingInvoiceItemInterval } from 'src/engine/core-modules/billing-webhook/utils/should-set-pending-invoice-item-interval.util';
import { transformStripeSubscriptionEventToDatabaseSubscription } from 'src/engine/core-modules/billing-webhook/utils/transform-stripe-subscription-event-to-database-subscription.util';
import {
BillingException,
@@ -28,6 +29,7 @@ import { BillingWebhookEvent } from 'src/engine/core-modules/billing/enums/billi
import { UsageLimitQuotaService } from 'src/engine/core-modules/usage-limit/services/usage-limit-quota.service';
import { StripeCustomerService } from 'src/engine/core-modules/billing/stripe/services/stripe-customer.service';
import { StripeSubscriptionScheduleService } from 'src/engine/core-modules/billing/stripe/services/stripe-subscription-schedule.service';
+import { StripeSubscriptionService } from 'src/engine/core-modules/billing/stripe/services/stripe-subscription.service';
import { type SubscriptionWithSchedule } from 'src/engine/core-modules/billing/types/billing-subscription-with-schedule.type';
import { buildBillingSubscriptionStateLockKey } from 'src/engine/core-modules/billing/utils/build-billing-subscription-state-lock-key.util';
import { resolveBillingPeriodBoundaryUpdate } from 'src/engine/core-modules/billing/utils/resolve-billing-period-boundary-update.util';
@@ -67,6 +69,7 @@ export class BillingWebhookSubscriptionService {
private readonly billingCustomerRepository: WorkspaceScopedRepository<BillingCustomerEntity>,
private readonly workspaceService: WorkspaceService,
private readonly stripeSubscriptionScheduleService: StripeSubscriptionScheduleService,
+ private readonly stripeSubscriptionService: StripeSubscriptionService,
private readonly usageLimitQuotaService: UsageLimitQuotaService,
private readonly workspaceCacheService: WorkspaceCacheService,
private readonly cacheLockService: CacheLockService,
@@ -267,6 +270,13 @@ export class BillingWebhookSubscriptionService {
workspaceId,
);
+ if (shouldSetPendingInvoiceItemInterval(subscriptionWithSchedule)) {
+ await this.stripeSubscriptionService.updateSubscription(
+ subscriptionWithSchedule.id,
+ { pending_invoice_item_interval: { interval: 'month' } },
+ );
+ }
+
return {
stripeSubscriptionId: data.object.id,
stripeCustomerId: data.object.customer,
diff --git a/packages/twenty-server/src/engine/core-modules/billing-webhook/utils/__tests__/should-set-pending-invoice-item-interval.util.spec.ts b/packages/twenty-server/src/engine/core-modules/billing-webhook/utils/__tests__/should-set-pending-invoice-item-interval.util.spec.ts
new file mode 100644
index 00000000000..6f78978f080
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/billing-webhook/utils/__tests__/should-set-pending-invoice-item-interval.util.spec.ts
@@ -0,0 +1,56 @@
+import { shouldSetPendingInvoiceItemInterval } from 'src/engine/core-modules/billing-webhook/utils/should-set-pending-invoice-item-interval.util';
+
+describe('shouldSetPendingInvoiceItemInterval', () => {
+ it('is true for an active invoice-paying subscription without an interval', () => {
+ expect(
+ shouldSetPendingInvoiceItemInterval({
+ collection_method: 'send_invoice',
+ pending_invoice_item_interval: null,
+ status: 'active',
+ }),
+ ).toBe(true);
+ });
+
+ it('is true for an invoice-paying subscription still in trial', () => {
+ expect(
+ shouldSetPendingInvoiceItemInterval({
+ collection_method: 'send_invoice',
+ pending_invoice_item_interval: null,
+ status: 'trialing',
+ }),
+ ).toBe(true);
+ });
+
+ it('keeps an interval already set on the subscription', () => {
+ expect(
+ shouldSetPendingInvoiceItemInterval({
+ collection_method: 'send_invoice',
+ pending_invoice_item_interval: {
+ interval: 'month',
+ interval_count: 3,
+ },
+ status: 'active',
+ }),
+ ).toBe(false);
+ });
+
+ it('is false for card-paying subscriptions', () => {
+ expect(
+ shouldSetPendingInvoiceItemInterval({
+ collection_method: 'charge_automatically',
+ pending_invoice_item_interval: null,
+ status: 'active',
+ }),
+ ).toBe(false);
+ });
+
+ it('is false for canceled subscriptions', () => {
+ expect(
+ shouldSetPendingInvoiceItemInterval({
+ collection_method: 'send_invoice',
+ pending_invoice_item_interval: null,
+ status: 'canceled',
+ }),
+ ).toBe(false);
+ });
+});
diff --git a/packages/twenty-server/src/engine/core-modules/billing-webhook/utils/should-set-pending-invoice-item-interval.util.ts b/packages/twenty-server/src/engine/core-modules/billing-webhook/utils/should-set-pending-invoice-item-interval.util.ts
new file mode 100644
index 00000000000..cf44d628469
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/billing-webhook/utils/should-set-pending-invoice-item-interval.util.ts
@@ -0,0 +1,18 @@
+/* @license Enterprise */
+
+import type Stripe from 'stripe';
+
+import { NOT_ENDED_SUBSCRIPTION_STATUSES } from 'src/engine/core-modules/billing/constants/not-ended-subscription-statuses.constant';
+import { type SubscriptionStatus } from 'src/engine/core-modules/billing/enums/billing-subscription-status.enum';
+
+export const shouldSetPendingInvoiceItemInterval = (
+ subscription: Pick<
+ Stripe.Subscription,
+ 'collection_method' | 'pending_invoice_item_interval' | 'status'
+ >,
+): boolean =>
+ subscription.collection_method === 'send_invoice' &&
+ subscription.pending_invoice_item_interval === null &&
+ NOT_ENDED_SUBSCRIPTION_STATUSES.includes(
+ subscription.status as SubscriptionStatus,
+ );