import type { AxiosRequestConfig } from 'axios' ; import type { HttpClient } from '../../http' ; import { BillingData } from './BillingInfo' ; import { BillingTeamData } from './BillingTeamData' ; import { CardData } from './CardData' ; import { InvoiceListData } from './InvoiceListData' ; import { Coupon , InvoiceUpcomingData } from './InvoiceUpcomingData' ; import { PlanData , PlanInterval } from './PlanData' ; import { SupportedCurrency } from './SupportedCurrency' ; export class BillingAPI { public static readonly DEFAULT_INVOICES_CHUNK_SIZE = 10 ; constructor ( private readonly client ) { } public static readonly URL = { BILLING : 'billing' , CURRENCIES : 'currencies' , INFO : 'info' , CARD : 'card' , COUPON : 'coupon' , INVOICES : 'invoices' , PLAN : 'plan' , LIST : 'list' , TEAMS : '/teams' , TEAM : 'team' , UPCOMING : 'upcoming' , } ; public async getBillingTeam ( teamId ) { const config = { method : 'get' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . TEAM }` , } ; const response = await this . client . sendJSON < BillingTeamData > ( config ) ; return response . data ; } public async putBillingInfo ( teamId , billingInfo ) { const config = { data : billingInfo , method : 'put' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . INFO }` , } ; const response = await this . client . sendJSON < BillingData > ( config ) ; return response . data ; } public async getBillingInfo ( teamId ) { const config = { method : 'get' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . INFO }` , } ; const response = await this . client . sendJSON < BillingData > ( config ) ; return response . data ; } public async putCard ( teamId , stripeToken ) { const config = { data : { stripeToken , } , method : 'put' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . CARD }` , } ; const response = await this . client . sendJSON < CardData > ( config ) ; return response . data ; } public async getCard ( teamId ) { const config = { method : 'get' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . CARD }` , } ; const response = await this . client . sendJSON < CardData > ( config ) ; return response . data ; } public async postCoupon ( teamId , coupon ) { const config = { data : { coupon , } , method : 'post' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . COUPON }` , } ; const response = await this . client . sendJSON < Coupon > ( config ) ; return response . data ; } public async deleteCoupon ( teamId , coupon ) { const config = { data : { coupon , } , method : 'delete' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . COUPON }` , } ; await this . client . sendJSON ( config ) ; } public async getCurrentPlan ( teamId ) { const config = { method : 'get' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . PLAN }` , } ; const response = await this . client . sendJSON < PlanData > ( config ) ; return response . data ; } public async putPlan ( teamId , planId ) { const config = { data : { planId } , method : 'put' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . PLAN }` , } ; const response = await this . client . sendJSON < PlanData > ( config ) ; return response . data ; } public async getAvailablePlans ( teamId , filter ) { const config = { params : filter , method : 'get' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . PLAN }/${ BillingAPI . URL . LIST }` , } ; const response = await this . client . sendJSON < PlanData [ ] > ( config ) ; return response . data ; } public async getSupportedCurrencies ( teamId ) { const config = { method : 'get' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . CURRENCIES }` , } ; const response = await this . client . sendJSON < SupportedCurrency [ ] > ( config ) ; return response . data ; } public async getUpcomingInvoice ( teamId ) { const config = { method : 'get' , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . INVOICES }/${ BillingAPI . URL . UPCOMING }` , } ; const response = await this . client . sendJSON < InvoiceUpcomingData > ( config ) ; return response . data ; } public async getInvoices ( teamId , limit = BillingAPI . DEFAULT_INVOICES_CHUNK_SIZE , startAfterInvoiceId ? ) { const config = { method : 'get' , params : { size : limit , start : startAfterInvoiceId , } , url : `${ BillingAPI . URL . TEAMS }/${ teamId }/${ BillingAPI . URL . BILLING }/${ BillingAPI . URL . INVOICES }` , } ; const response = await this . client . sendJSON < InvoiceListData > ( config ) ; return response . data ; } }