File

apps/recallassess/recallassess-api/src/api/shared/stripe/services/stripe-payment.service.ts

Description

Facade for Stripe payment flows. Implementation is split under {@code ./stripe-payment/} (max ~1500 lines per file).

Index

Methods

Constructor

constructor(billing: StripePaymentBillingHelpersService, invoiceWebhooks: StripePaymentInvoiceWebhookService, checkout: StripePaymentCheckoutService, syncRefund: StripePaymentSyncRefundService, subscriptionAdmin: StripePaymentSubscriptionAdminService, customerSubscription: StripePaymentCustomerSubscriptionService)
Parameters :
Name Type Optional
billing StripePaymentBillingHelpersService No
invoiceWebhooks StripePaymentInvoiceWebhookService No
checkout StripePaymentCheckoutService No
syncRefund StripePaymentSyncRefundService No
subscriptionAdmin StripePaymentSubscriptionAdminService No
customerSubscription StripePaymentCustomerSubscriptionService No

Methods

Async createCheckoutSession
createCheckoutSession(dto: CreateCheckoutSessionDto)
Parameters :
Name Type Optional
dto CreateCheckoutSessionDto No
Returns : unknown
Async createPaymentIntent
createPaymentIntent(dto: CreatePaymentIntentDto)
Parameters :
Name Type Optional
dto CreatePaymentIntentDto No
Returns : unknown
extractStripePaymentIdsFromInvoice
extractStripePaymentIdsFromInvoice(inv: Stripe.Invoice)
Parameters :
Name Type Optional
inv Stripe.Invoice No
Returns : literal type
Async getInvoiceStatus
getInvoiceStatus(invoiceId: number)
Parameters :
Name Type Optional
invoiceId number No
Returns : unknown
Async handleCheckoutSessionCompleted
handleCheckoutSessionCompleted(session: Stripe.Checkout.Session)
Parameters :
Name Type Optional
session Stripe.Checkout.Session No
Returns : unknown
Async handleCustomerSubscriptionCreated
handleCustomerSubscriptionCreated(subscription: Stripe.Subscription)
Parameters :
Name Type Optional
subscription Stripe.Subscription No
Returns : Promise<void>
Async handleCustomerSubscriptionUpdated
handleCustomerSubscriptionUpdated(subscription: Stripe.Subscription)
Parameters :
Name Type Optional
subscription Stripe.Subscription No
Returns : Promise<void>
Async handleInvoiceCreated
handleInvoiceCreated(invoice: Stripe.Invoice)
Parameters :
Name Type Optional
invoice Stripe.Invoice No
Returns : Promise<void>
Async handleInvoicePaid
handleInvoicePaid(invoice: Stripe.Invoice)
Parameters :
Name Type Optional
invoice Stripe.Invoice No
Returns : Promise<void>
Async handleInvoicePaymentFailed
handleInvoicePaymentFailed(stripeInvoice: Stripe.Invoice)
Parameters :
Name Type Optional
stripeInvoice Stripe.Invoice No
Returns : Promise<void>
Async handleInvoicePaymentPaid
handleInvoicePaymentPaid(eventObject: unknown)
Parameters :
Name Type Optional
eventObject unknown No
Returns : Promise<void>
Async handlePaymentFailed
handlePaymentFailed(paymentIntent: Stripe.PaymentIntent)
Parameters :
Name Type Optional
paymentIntent Stripe.PaymentIntent No
Returns : unknown
Async handlePaymentMethodAttached
handlePaymentMethodAttached(paymentMethod: Stripe.PaymentMethod)
Parameters :
Name Type Optional
paymentMethod Stripe.PaymentMethod No
Returns : Promise<void>
Async handlePaymentSucceeded
handlePaymentSucceeded(paymentIntent: Stripe.PaymentIntent)
Parameters :
Name Type Optional
paymentIntent Stripe.PaymentIntent No
Returns : unknown
Async handleRefundCreated
handleRefundCreated(refund: Stripe.Refund)
Parameters :
Name Type Optional
refund Stripe.Refund No
Returns : Promise<boolean>
Async handleSetupIntentSucceeded
handleSetupIntentSucceeded(setupIntent: Stripe.SetupIntent)
Parameters :
Name Type Optional
setupIntent Stripe.SetupIntent No
Returns : Promise<void>
Async previewImmediateExpiredRecoveryCharge
previewImmediateExpiredRecoveryCharge(companyId: number, fromDate?: string)
Parameters :
Name Type Optional
companyId number No
fromDate string Yes
Returns : unknown
Async refreshPaidSubscriptionLinkedPackageFromCatalog
refreshPaidSubscriptionLinkedPackageFromCatalog(companyId: number)
Parameters :
Name Type Optional
companyId number No
Returns : Promise<void>
Async runAdminCreateStripeSubscription
runAdminCreateStripeSubscription(companyId: number)
Parameters :
Name Type Optional
companyId number No
Returns : Promise<literal type>
Async runBatchStripeSubscriptionSync
runBatchStripeSubscriptionSync(options?: literal type)
Parameters :
Name Type Optional
options literal type Yes
Returns : unknown
Async runImmediateExpiredRecoveryCharge
runImmediateExpiredRecoveryCharge(companyId: number, options?: literal type)
Parameters :
Name Type Optional
companyId number No
options literal type Yes
Returns : unknown
Async sendBillingSubscriptionResumeEmail
sendBillingSubscriptionResumeEmail(companyId: number, subscription: Stripe.Subscription, options?: literal type)
Parameters :
Name Type Optional
companyId number No
subscription Stripe.Subscription No
options literal type Yes
Returns : Promise<void>
Async sendBillingSubscriptionScheduledCancelEmail
sendBillingSubscriptionScheduledCancelEmail(companyId: number, subscription: Stripe.Subscription, options?: literal type)
Parameters :
Name Type Optional
companyId number No
subscription Stripe.Subscription No
options literal type Yes
Returns : Promise<void>
Async syncCompanyInvoicesAndRefundsFromStripe
syncCompanyInvoicesAndRefundsFromStripe(companyId: number)
Parameters :
Name Type Optional
companyId number No
Returns : Promise<literal type>
Async updateAdminStripeNextBillingDate
updateAdminStripeNextBillingDate(companyId: number, nextBillingDate: string)
Parameters :
Name Type Optional
companyId number No
nextBillingDate string No
Returns : Promise<literal type>
import { Injectable } from "@nestjs/common";
import Stripe from "stripe";
import { CreateCheckoutSessionDto, CreatePaymentIntentDto } from "../dto/payment.dto";
import { StripePaymentBillingHelpersService } from "./stripe-payment/stripe-payment-billing-helpers.service";
import { StripePaymentCheckoutService } from "./stripe-payment/stripe-payment-checkout.service";
import { StripePaymentCustomerSubscriptionService } from "./stripe-payment/stripe-payment-customer-subscription.service";
import { StripePaymentInvoiceWebhookService } from "./stripe-payment/stripe-payment-invoice-webhook.service";
import { StripePaymentSubscriptionAdminService } from "./stripe-payment/stripe-payment-subscription-admin.service";
import { StripePaymentSyncRefundService } from "./stripe-payment/stripe-payment-sync-refund.service";

export type { ImmediateRecoveryQuote } from "./stripe-payment/stripe-payment.types";

/**
 * Facade for Stripe payment flows. Implementation is split under {@code ./stripe-payment/} (max ~1500 lines per file).
 */
@Injectable()
export class StripePaymentService {
  constructor(
    private readonly billing: StripePaymentBillingHelpersService,
    private readonly invoiceWebhooks: StripePaymentInvoiceWebhookService,
    private readonly checkout: StripePaymentCheckoutService,
    private readonly syncRefund: StripePaymentSyncRefundService,
    private readonly subscriptionAdmin: StripePaymentSubscriptionAdminService,
    private readonly customerSubscription: StripePaymentCustomerSubscriptionService,
  ) {}

  extractStripePaymentIdsFromInvoice(inv: Stripe.Invoice): {
    paymentIntentId: string | null;
    chargeId: string | null;
    checkoutSessionId: string | null;
  } {
    return this.billing.extractPaymentIntentAndChargeIds(inv);
  }

  async handleInvoicePaid(invoice: Stripe.Invoice): Promise<void> {
    return this.invoiceWebhooks.handleInvoicePaid(invoice);
  }

  async handleInvoicePaymentFailed(stripeInvoice: Stripe.Invoice): Promise<void> {
    return this.invoiceWebhooks.handleInvoicePaymentFailed(stripeInvoice);
  }

  async handleInvoicePaymentPaid(eventObject: unknown): Promise<void> {
    return this.invoiceWebhooks.handleInvoicePaymentPaid(eventObject);
  }

  async handlePaymentMethodAttached(paymentMethod: Stripe.PaymentMethod): Promise<void> {
    return this.invoiceWebhooks.handlePaymentMethodAttached(paymentMethod);
  }

  async handleSetupIntentSucceeded(setupIntent: Stripe.SetupIntent): Promise<void> {
    return this.invoiceWebhooks.handleSetupIntentSucceeded(setupIntent);
  }

  async handleInvoiceCreated(invoice: Stripe.Invoice): Promise<void> {
    return this.invoiceWebhooks.handleInvoiceCreated(invoice);
  }

  async updateAdminStripeNextBillingDate(
    companyId: number,
    nextBillingDate: string,
  ): Promise<{ success: boolean; message: string; next_billing_date: string }> {
    return this.syncRefund.updateAdminStripeNextBillingDate(companyId, nextBillingDate);
  }

  async syncCompanyInvoicesAndRefundsFromStripe(companyId: number): Promise<{
    success: boolean;
    message: string;
    invoices_created: number;
    refunds_created: number;
  }> {
    return this.syncRefund.syncCompanyInvoicesAndRefundsFromStripe(companyId);
  }

  async handleRefundCreated(refund: Stripe.Refund): Promise<boolean> {
    return this.syncRefund.handleRefundCreated(refund);
  }

  async sendBillingSubscriptionScheduledCancelEmail(
    companyId: number,
    subscription: Stripe.Subscription,
    options?: { triggeredBy?: string; subscriptionRowId?: number },
  ): Promise<void> {
    return this.customerSubscription.sendBillingSubscriptionScheduledCancelEmail(companyId, subscription, options);
  }

  async sendBillingSubscriptionResumeEmail(
    companyId: number,
    subscription: Stripe.Subscription,
    options?: { triggeredBy?: string; subscriptionRowId?: number },
  ): Promise<void> {
    return this.customerSubscription.sendBillingSubscriptionResumeEmail(companyId, subscription, options);
  }

  async createCheckoutSession(dto: CreateCheckoutSessionDto) {
    return this.checkout.createCheckoutSession(dto);
  }

  async createPaymentIntent(dto: CreatePaymentIntentDto) {
    return this.checkout.createPaymentIntent(dto);
  }

  async getInvoiceStatus(invoiceId: number) {
    return this.checkout.getInvoiceStatus(invoiceId);
  }

  async handleCheckoutSessionCompleted(session: Stripe.Checkout.Session) {
    return this.checkout.handleCheckoutSessionCompleted(session);
  }

  async handlePaymentSucceeded(paymentIntent: Stripe.PaymentIntent) {
    return this.checkout.handlePaymentSucceeded(paymentIntent);
  }

  async handlePaymentFailed(paymentIntent: Stripe.PaymentIntent) {
    return this.checkout.handlePaymentFailed(paymentIntent);
  }

  async runBatchStripeSubscriptionSync(options?: { dryRun?: boolean; recoverExpiredCompanies?: boolean }) {
    return this.subscriptionAdmin.runBatchStripeSubscriptionSync(options);
  }

  async refreshPaidSubscriptionLinkedPackageFromCatalog(companyId: number): Promise<void> {
    return this.subscriptionAdmin.refreshPaidSubscriptionLinkedPackageFromCatalog(companyId);
  }

  async runAdminCreateStripeSubscription(companyId: number): Promise<{
    success: boolean;
    message: string;
    next_period_end_iso?: string | null;
  }> {
    return this.subscriptionAdmin.runAdminCreateStripeSubscription(companyId);
  }

  async runImmediateExpiredRecoveryCharge(
    companyId: number,
    options?: { forceRecreateStripeSubscription?: boolean; recoveryFromDate?: string },
  ) {
    return this.subscriptionAdmin.runImmediateExpiredRecoveryCharge(companyId, options);
  }

  async previewImmediateExpiredRecoveryCharge(companyId: number, fromDate?: string) {
    return this.subscriptionAdmin.previewImmediateExpiredRecoveryCharge(companyId, fromDate);
  }

  async handleCustomerSubscriptionCreated(subscription: Stripe.Subscription): Promise<void> {
    return this.customerSubscription.handleCustomerSubscriptionCreated(subscription);
  }

  async handleCustomerSubscriptionUpdated(subscription: Stripe.Subscription): Promise<void> {
    return this.customerSubscription.handleCustomerSubscriptionUpdated(subscription);
  }
}

results matching ""

    No results matching ""