import { AbstractService, EndpointCreator, HttpMethod, Modules, NextRef, PreviousRef } from "../../../../core"; import { PaymentMethodInterface } from "./payment-method.interface"; import { StripeCustomerInterface } from "./stripe-customer.interface"; /** * Customer-facing billing service for managing subscriptions, payments, and usage */ export class StripeCustomerService extends AbstractService { // ============================================================================ // Customer Methods // ============================================================================ /** * Get the current user's billing customer record */ static async getCustomer(): Promise { const endpoint = new EndpointCreator({ endpoint: Modules.StripeCustomer, }); return this.callApi({ type: Modules.StripeCustomer, method: HttpMethod.GET, endpoint: endpoint.generate(), }); } /** * Create a billing customer for the current user */ static async createCustomer(): Promise { const endpoint = new EndpointCreator({ endpoint: Modules.StripeCustomer, }); return this.callApi({ type: Modules.StripeCustomer, method: HttpMethod.POST, endpoint: endpoint.generate(), }); } /** * Create a setup intent for adding payment methods */ static async createSetupIntent(): Promise<{ clientSecret: string }> { const endpoint = new EndpointCreator({ endpoint: Modules.Billing, childEndpoint: "setup-intent", }); return this.callApi({ type: Modules.Billing, method: HttpMethod.POST, endpoint: endpoint.generate(), }); } /** * Create a Stripe customer portal session URL */ static async createPortalSession(): Promise<{ url: string }> { const endpoint = new EndpointCreator({ endpoint: Modules.Billing, childEndpoint: "customers/portal-session", }); return this.callApi({ type: Modules.Billing, method: HttpMethod.POST, endpoint: endpoint.generate(), }); } // ============================================================================ // Payment Method Methods // ============================================================================ /** * List all payment methods for the current user */ static async listPaymentMethods(params?: { next?: NextRef; prev?: PreviousRef }): Promise { const endpoint = new EndpointCreator({ endpoint: Modules.StripeCustomer, childEndpoint: "payment-methods", }); return this.callApi({ type: Modules.StripePaymentMethod, method: HttpMethod.GET, endpoint: endpoint.generate(), next: params?.next, previous: params?.prev, }); } /** * Set the default payment method for the current user */ static async setDefaultPaymentMethod(params: { paymentMethodId: string }): Promise { const endpoint = new EndpointCreator({ endpoint: Modules.StripeCustomer, childEndpoint: `payment-methods/${params.paymentMethodId}/default`, }); return this.callApi({ type: Modules.StripeCustomer, method: HttpMethod.POST, endpoint: endpoint.generate(), }); } /** * Remove a payment method */ static async removePaymentMethod(params: { paymentMethodId: string }): Promise { const endpoint = new EndpointCreator({ endpoint: Modules.StripeCustomer, childEndpoint: `payment-methods/${params.paymentMethodId}`, }); await this.callApi({ type: Modules.StripeCustomer, method: HttpMethod.DELETE, endpoint: endpoint.generate(), }); } }