2021-03-09 16:23:13 +00:00
|
|
|
import { getEndpoint } from 'utils/common/apiUtil';
|
|
|
|
import HTTPService from './HTTPService';
|
|
|
|
const ENDPOINT = getEndpoint();
|
2021-03-12 09:12:19 +00:00
|
|
|
import { getToken } from 'utils/common/key';
|
2021-03-12 12:30:33 +00:00
|
|
|
import { ExecFileOptionsWithStringEncoding } from 'node:child_process';
|
2021-03-12 22:48:44 +00:00
|
|
|
import { runningInBrowser } from 'utils/common/utilFunctions';
|
|
|
|
import { getData, LS_KEYS } from 'utils/storage/localStorage';
|
2021-03-11 16:31:20 +00:00
|
|
|
export interface Subscription {
|
|
|
|
id: number;
|
|
|
|
userID: number;
|
|
|
|
productID: string;
|
|
|
|
storage: number;
|
|
|
|
originalTransactionID: string;
|
|
|
|
expiryTime: number;
|
|
|
|
paymentProvider: string;
|
|
|
|
}
|
2021-03-12 12:30:33 +00:00
|
|
|
export interface Plan {
|
|
|
|
id: string;
|
|
|
|
androidID: string;
|
|
|
|
iosID: string;
|
|
|
|
storage: number;
|
|
|
|
price: string;
|
|
|
|
period: string;
|
2021-03-12 15:09:17 +00:00
|
|
|
stripeID: string;
|
2021-03-12 12:30:33 +00:00
|
|
|
}
|
2021-03-12 22:48:44 +00:00
|
|
|
const FREE_PLAN = 'free';
|
2021-03-09 16:23:13 +00:00
|
|
|
class SubscriptionService {
|
|
|
|
private stripe;
|
2021-03-12 22:48:44 +00:00
|
|
|
constructor() {
|
2021-03-09 16:23:13 +00:00
|
|
|
let publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY;
|
2021-03-12 22:48:44 +00:00
|
|
|
this.stripe = runningInBrowser() && window['Stripe'](publishableKey);
|
2021-03-09 16:23:13 +00:00
|
|
|
}
|
2021-03-12 12:30:33 +00:00
|
|
|
public async getPlans(): Promise<Plan[]> {
|
|
|
|
try {
|
|
|
|
const response = await HTTPService.get(`${ENDPOINT}/billing/plans`);
|
|
|
|
return response.data['plans'];
|
|
|
|
} catch (e) {
|
|
|
|
console.error('failed to get plans', e);
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 17:34:20 +00:00
|
|
|
public async buySubscription(productID) {
|
2021-03-09 16:23:13 +00:00
|
|
|
try {
|
2021-03-12 22:48:44 +00:00
|
|
|
const response = await this.createCheckoutSession(productID);
|
2021-03-12 11:43:47 +00:00
|
|
|
await this.stripe.redirectToCheckout({
|
2021-03-12 15:09:17 +00:00
|
|
|
sessionId: response.data['sessionID'],
|
2021-03-09 16:23:13 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2021-03-12 17:34:20 +00:00
|
|
|
console.error('unable to buy subscription', e);
|
2021-03-09 16:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 22:48:44 +00:00
|
|
|
private async createCheckoutSession(productID) {
|
2021-03-09 16:23:13 +00:00
|
|
|
return HTTPService.post(`${ENDPOINT}/billing/create-checkout-session`, {
|
2021-03-12 22:48:44 +00:00
|
|
|
productID,
|
2021-03-09 16:23:13 +00:00
|
|
|
});
|
|
|
|
}
|
2021-03-10 03:25:00 +00:00
|
|
|
|
2021-03-12 17:34:20 +00:00
|
|
|
public async verifySubscription(sessionID): Promise<Subscription> {
|
2021-03-10 03:25:00 +00:00
|
|
|
try {
|
2021-03-12 17:34:20 +00:00
|
|
|
const response = await HTTPService.post(
|
|
|
|
`${ENDPOINT}/billing/verify-subscription`,
|
2021-03-12 11:43:47 +00:00
|
|
|
{
|
2021-03-12 17:34:20 +00:00
|
|
|
paymentProvider: 'stripe',
|
2021-03-12 22:48:44 +00:00
|
|
|
productID: null,
|
2021-03-12 17:34:20 +00:00
|
|
|
VerificationData: sessionID,
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
'X-Auth-Token': getToken(),
|
2021-03-12 11:43:47 +00:00
|
|
|
}
|
2021-03-10 03:25:00 +00:00
|
|
|
);
|
2021-03-12 17:34:20 +00:00
|
|
|
return response.data['subscription'];
|
2021-03-10 03:25:00 +00:00
|
|
|
} catch (err) {
|
2021-03-12 17:34:20 +00:00
|
|
|
console.error('Error while verifying subscription', err);
|
2021-03-10 03:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 17:34:20 +00:00
|
|
|
|
|
|
|
public async redirectToCustomerPortal() {
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-11 16:04:52 +00:00
|
|
|
async getUsage() {
|
|
|
|
try {
|
|
|
|
const response = await HTTPService.get(
|
|
|
|
`${ENDPOINT}/billing/usage`,
|
|
|
|
{ startTime: 0, endTime: Date.now() * 1000 },
|
|
|
|
{
|
2021-03-12 03:16:35 +00:00
|
|
|
'X-Auth-Token': getToken(),
|
2021-03-11 16:04:52 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
return this.convertBytesToGBs(response.data.usage);
|
|
|
|
} catch (e) {
|
|
|
|
console.error('error getting usage', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public convertBytesToGBs(bytes): string {
|
|
|
|
return (bytes / (1024 * 1024 * 1024)).toFixed(2);
|
|
|
|
}
|
2021-03-12 22:48:44 +00:00
|
|
|
public isOnFreePlan() {
|
|
|
|
const subscription: Subscription = getData(LS_KEYS.SUBSCRIPTION);
|
|
|
|
return subscription?.productID === FREE_PLAN;
|
|
|
|
}
|
2021-03-09 16:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new SubscriptionService();
|