|
| 1 | +import { AbstractModule } from '../../../core/abstract-module' |
| 2 | +import type { Labrinth } from './types' |
| 3 | + |
| 4 | +export class LabrinthBillingInternalModule extends AbstractModule { |
| 5 | + public getModuleID(): string { |
| 6 | + return 'labrinth_billing_internal' |
| 7 | + } |
| 8 | + |
| 9 | + /** |
| 10 | + * Get user's subscriptions |
| 11 | + * GET /_internal/billing/subscriptions |
| 12 | + */ |
| 13 | + public async getSubscriptions( |
| 14 | + userId?: string, |
| 15 | + ): Promise<Labrinth.Billing.Internal.UserSubscription[]> { |
| 16 | + const params = userId ? `?user_id=${userId}` : '' |
| 17 | + |
| 18 | + return this.client.request<Labrinth.Billing.Internal.UserSubscription[]>( |
| 19 | + `/billing/subscriptions${params}`, |
| 20 | + { |
| 21 | + api: 'labrinth', |
| 22 | + version: 'internal', |
| 23 | + method: 'GET', |
| 24 | + }, |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get available products for purchase |
| 30 | + * GET /_internal/billing/products |
| 31 | + */ |
| 32 | + public async getProducts(): Promise<Labrinth.Billing.Internal.Product[]> { |
| 33 | + return this.client.request<Labrinth.Billing.Internal.Product[]>('/billing/products', { |
| 34 | + api: 'labrinth', |
| 35 | + version: 'internal', |
| 36 | + method: 'GET', |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get Stripe customer information |
| 42 | + * GET /_internal/billing/customer |
| 43 | + */ |
| 44 | + public async getCustomer(): Promise<unknown> { |
| 45 | + return this.client.request<unknown>('/billing/customer', { |
| 46 | + api: 'labrinth', |
| 47 | + version: 'internal', |
| 48 | + method: 'GET', |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Edit a subscription (change product, interval, cancel, etc.) |
| 54 | + * PATCH /_internal/billing/subscription/{id} |
| 55 | + */ |
| 56 | + public async editSubscription( |
| 57 | + id: string, |
| 58 | + edit: Labrinth.Billing.Internal.EditSubscriptionRequest, |
| 59 | + dry?: boolean, |
| 60 | + ): Promise<Labrinth.Billing.Internal.EditSubscriptionResponse | void> { |
| 61 | + const params = dry ? '?dry=true' : '' |
| 62 | + |
| 63 | + return this.client.request<Labrinth.Billing.Internal.EditSubscriptionResponse | void>( |
| 64 | + `/billing/subscription/${id}${params}`, |
| 65 | + { |
| 66 | + api: 'labrinth', |
| 67 | + version: 'internal', |
| 68 | + method: 'PATCH', |
| 69 | + body: edit, |
| 70 | + }, |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get user's payment methods |
| 76 | + * GET /_internal/billing/payment_methods |
| 77 | + */ |
| 78 | + public async getPaymentMethods(): Promise<unknown[]> { |
| 79 | + return this.client.request<unknown[]>('/billing/payment_methods', { |
| 80 | + api: 'labrinth', |
| 81 | + version: 'internal', |
| 82 | + method: 'GET', |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Initiate flow to add a new payment method |
| 88 | + * POST /_internal/billing/payment_method |
| 89 | + */ |
| 90 | + public async addPaymentMethodFlow(): Promise<Labrinth.Billing.Internal.AddPaymentMethodFlowResponse> { |
| 91 | + return this.client.request<Labrinth.Billing.Internal.AddPaymentMethodFlowResponse>( |
| 92 | + '/billing/payment_method', |
| 93 | + { |
| 94 | + api: 'labrinth', |
| 95 | + version: 'internal', |
| 96 | + method: 'POST', |
| 97 | + }, |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Edit a payment method (set as primary) |
| 103 | + * PATCH /_internal/billing/payment_method/{id} |
| 104 | + */ |
| 105 | + public async editPaymentMethod( |
| 106 | + id: string, |
| 107 | + body: Labrinth.Billing.Internal.EditPaymentMethodRequest, |
| 108 | + ): Promise<void> { |
| 109 | + return this.client.request<void>(`/billing/payment_method/${id}`, { |
| 110 | + api: 'labrinth', |
| 111 | + version: 'internal', |
| 112 | + method: 'PATCH', |
| 113 | + body, |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Remove a payment method |
| 119 | + * DELETE /_internal/billing/payment_method/{id} |
| 120 | + */ |
| 121 | + public async removePaymentMethod(id: string): Promise<void> { |
| 122 | + return this.client.request<void>(`/billing/payment_method/${id}`, { |
| 123 | + api: 'labrinth', |
| 124 | + version: 'internal', |
| 125 | + method: 'DELETE', |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Get payment history (charges) |
| 131 | + * GET /_internal/billing/payments |
| 132 | + */ |
| 133 | + public async getPayments(userId?: string): Promise<Labrinth.Billing.Internal.Charge[]> { |
| 134 | + const params = userId ? `?user_id=${userId}` : '' |
| 135 | + |
| 136 | + return this.client.request<Labrinth.Billing.Internal.Charge[]>(`/billing/payments${params}`, { |
| 137 | + api: 'labrinth', |
| 138 | + version: 'internal', |
| 139 | + method: 'GET', |
| 140 | + }) |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Initiate a payment |
| 145 | + * POST /_internal/billing/payment |
| 146 | + */ |
| 147 | + public async initiatePayment( |
| 148 | + request: Labrinth.Billing.Internal.InitiatePaymentRequest, |
| 149 | + ): Promise<Labrinth.Billing.Internal.InitiatePaymentResponse> { |
| 150 | + return this.client.request<Labrinth.Billing.Internal.InitiatePaymentResponse>('/billing/payment', { |
| 151 | + api: 'labrinth', |
| 152 | + version: 'internal', |
| 153 | + method: 'POST', |
| 154 | + body: request, |
| 155 | + }) |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Refund a charge (Admin only) |
| 160 | + * POST /_internal/billing/charge/{id}/refund |
| 161 | + */ |
| 162 | + public async refundCharge( |
| 163 | + id: string, |
| 164 | + refund: Labrinth.Billing.Internal.RefundChargeRequest, |
| 165 | + ): Promise<void> { |
| 166 | + return this.client.request<void>(`/billing/charge/${id}/refund`, { |
| 167 | + api: 'labrinth', |
| 168 | + version: 'internal', |
| 169 | + method: 'POST', |
| 170 | + body: refund, |
| 171 | + }) |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Credit subscriptions (Admin only) |
| 176 | + * POST /_internal/billing/credit |
| 177 | + */ |
| 178 | + public async credit(request: Labrinth.Billing.Internal.CreditRequest): Promise<void> { |
| 179 | + return this.client.request<void>('/billing/credit', { |
| 180 | + api: 'labrinth', |
| 181 | + version: 'internal', |
| 182 | + method: 'POST', |
| 183 | + body: request, |
| 184 | + }) |
| 185 | + } |
| 186 | +} |
0 commit comments