79 lines
1.8 KiB
TypeScript

import { EType } from "le-coffre-resources/dist/Admin/Subscription";
import BaseAdmin from "../BaseAdmin";
export interface IPostStripeParams {
type: EType;
nb_seats: number;
}
export type IPostStripeResponse = {
url: string;
};
export type IGetClientPortalSessionResponse = {
url: string;
cancel_at?: number;
};
export interface IGetCustomerBySubscriptionIdParams {
email: string;
name: string;
}
export default class Stripe extends BaseAdmin {
private static instance: Stripe;
private readonly baseURl = this.namespaceUrl.concat("/stripe");
private constructor() {
super();
}
public static getInstance() {
if (!this.instance) {
return new Stripe();
} else {
return this.instance;
}
}
public async post(body: IPostStripeParams) {
const url = new URL(this.baseURl);
try {
return await this.postRequest<IPostStripeResponse>(url, body as any);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async getStripeSubscriptionByUid(stripe_subscription_id: string) {
const url = new URL(this.baseURl.concat(`/${stripe_subscription_id}`));
try {
return await this.getRequest<IGetClientPortalSessionResponse>(url);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async getClientPortalSession(stripe_subscription_id: string) {
const url = new URL(this.baseURl.concat(`/${stripe_subscription_id}/client-portal`));
try {
return await this.getRequest<IGetClientPortalSessionResponse>(url);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async getCustomerBySubscriptionId(subscriptionId: string) {
const url = new URL(this.baseURl.concat(`/${subscriptionId}/customer`));
try {
return await this.getRequest<IGetCustomerBySubscriptionIdParams>(url);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
}