Billing - fix duplicate customer in stripe + subscription constraint violation (#13091)

closes https://github.com/twentyhq/core-team-issues/issues/982
This commit is contained in:
Etienne
2025-07-08 11:17:59 +02:00
committed by GitHub
parent 67f0b98002
commit 56607c0449
7 changed files with 25 additions and 6 deletions

View File

@ -1,10 +1,13 @@
/* @license Enterprise */
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import Stripe from 'stripe';
import { isDefined } from 'twenty-shared/utils';
import { Repository } from 'typeorm';
import { BillingCustomer } from 'src/engine/core-modules/billing/entities/billing-customer.entity';
import { BillingPlanKey } from 'src/engine/core-modules/billing/enums/billing-plan-key.enum';
import { StripeSDKService } from 'src/engine/core-modules/billing/stripe/stripe-sdk/services/stripe-sdk.service';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
@ -18,6 +21,8 @@ export class StripeCheckoutService {
constructor(
private readonly twentyConfigService: TwentyConfigService,
private readonly stripeSDKService: StripeSDKService,
@InjectRepository(BillingCustomer, 'core')
private readonly billingCustomerRepository: Repository<BillingCustomer>,
) {
if (!this.twentyConfigService.get('IS_BILLING_ENABLED')) {
return;
@ -56,6 +61,11 @@ export class StripeCheckoutService {
},
});
await this.billingCustomerRepository.save({
stripeCustomerId: customer.id,
workspaceId,
});
stripeCustomerId = customer.id;
}

View File

@ -1,7 +1,9 @@
/* @license Enterprise */
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BillingCustomer } from 'src/engine/core-modules/billing/entities/billing-customer.entity';
import { StripeBillingMeterEventService } from 'src/engine/core-modules/billing/stripe/services/stripe-billing-meter-event.service';
import { StripeBillingMeterService } from 'src/engine/core-modules/billing/stripe/services/stripe-billing-meter.service';
import { StripeBillingPortalService } from 'src/engine/core-modules/billing/stripe/services/stripe-billing-portal.service';
@ -16,7 +18,11 @@ import { StripeSDKModule } from 'src/engine/core-modules/billing/stripe/stripe-s
import { DomainManagerModule } from 'src/engine/core-modules/domain-manager/domain-manager.module';
@Module({
imports: [DomainManagerModule, StripeSDKModule],
imports: [
DomainManagerModule,
StripeSDKModule,
TypeOrmModule.forFeature([BillingCustomer], 'core'),
],
providers: [
StripeSubscriptionItemService,
StripeWebhookService,