moved helper function to util added cancel and updated subscription logic and updated service to modify localStorage records
This commit is contained in:
parent
48bda44cd4
commit
4346d6ea7e
|
@ -3,7 +3,8 @@ import HTTPService from './HTTPService';
|
||||||
const ENDPOINT = getEndpoint();
|
const ENDPOINT = getEndpoint();
|
||||||
import { getToken } from 'utils/common/key';
|
import { getToken } from 'utils/common/key';
|
||||||
import { runningInBrowser } from 'utils/common/utilFunctions';
|
import { runningInBrowser } from 'utils/common/utilFunctions';
|
||||||
import { getData, setData, LS_KEYS } from 'utils/storage/localStorage';
|
import { setData, LS_KEYS } from 'utils/storage/localStorage';
|
||||||
|
import { convertBytesToGBs } from 'utils/billingUtil';
|
||||||
export interface Subscription {
|
export interface Subscription {
|
||||||
id: number;
|
id: number;
|
||||||
userID: number;
|
userID: number;
|
||||||
|
@ -22,22 +23,23 @@ export interface Plan {
|
||||||
period: string;
|
period: string;
|
||||||
stripeID: string;
|
stripeID: string;
|
||||||
}
|
}
|
||||||
const FREE_PLAN = 'free';
|
export const FREE_PLAN = 'free';
|
||||||
class SubscriptionService {
|
class SubscriptionService {
|
||||||
private stripe;
|
private stripe;
|
||||||
constructor() {
|
constructor() {
|
||||||
let publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY;
|
let publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY;
|
||||||
this.stripe = runningInBrowser() && window['Stripe'](publishableKey);
|
this.stripe = runningInBrowser() && window['Stripe'](publishableKey);
|
||||||
}
|
}
|
||||||
public async getPlans(): Promise<Plan[]> {
|
public async updatePlans() {
|
||||||
try {
|
try {
|
||||||
const response = await HTTPService.get(`${ENDPOINT}/billing/plans`);
|
const response = await HTTPService.get(`${ENDPOINT}/billing/plans`);
|
||||||
return response.data['plans'];
|
const plans = response.data['plans'];
|
||||||
|
setData(LS_KEYS.PLANS, plans);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('failed to get plans', e);
|
console.error('failed to get plans', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public async getUserSubscription() {
|
public async syncSubscription() {
|
||||||
try {
|
try {
|
||||||
const response = await HTTPService.get(
|
const response = await HTTPService.get(
|
||||||
`${ENDPOINT}/billing/subscription`,
|
`${ENDPOINT}/billing/subscription`,
|
||||||
|
@ -48,7 +50,6 @@ class SubscriptionService {
|
||||||
);
|
);
|
||||||
const subscription = response.data['subscription'];
|
const subscription = response.data['subscription'];
|
||||||
setData(LS_KEYS.SUBSCRIPTION, subscription);
|
setData(LS_KEYS.SUBSCRIPTION, subscription);
|
||||||
return subscription;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`failed to get user's subscription details`, e);
|
console.error(`failed to get user's subscription details`, e);
|
||||||
}
|
}
|
||||||
|
@ -61,12 +62,46 @@ class SubscriptionService {
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('unable to buy subscription', e);
|
console.error('unable to buy subscription', e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async updateSubscription(productID) {
|
||||||
|
try {
|
||||||
|
const response = await HTTPService.post(
|
||||||
|
`${ENDPOINT}/billing/stripe/update-subscription`,
|
||||||
|
{
|
||||||
|
productID,
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
'X-Auth-Token': getToken(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const subscription = response.data['subscription'];
|
||||||
|
setData(LS_KEYS.SUBSCRIPTION, subscription);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async cancelSubscription() {
|
||||||
|
try {
|
||||||
|
const response = await HTTPService.get(
|
||||||
|
`${ENDPOINT}/billing/stripe/cancel-subscription`,
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
'X-Auth-Token': getToken(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async createCheckoutSession(productID) {
|
private async createCheckoutSession(productID) {
|
||||||
return HTTPService.post(
|
return HTTPService.post(
|
||||||
`${ENDPOINT}/billing/create-checkout-session`,
|
`${ENDPOINT}/billing/stripe/create-checkout-session`,
|
||||||
{
|
{
|
||||||
productID,
|
productID,
|
||||||
},
|
},
|
||||||
|
@ -122,26 +157,11 @@ class SubscriptionService {
|
||||||
'X-Auth-Token': getToken(),
|
'X-Auth-Token': getToken(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return this.convertBytesToGBs(response.data.usage);
|
return convertBytesToGBs(response.data.usage);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('error getting usage', e);
|
console.error('error getting usage', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public convertBytesToGBs(bytes, precision?): string {
|
|
||||||
return (bytes / (1024 * 1024 * 1024)).toFixed(precision ?? 2);
|
|
||||||
}
|
|
||||||
public hasActivePaidPlan() {
|
|
||||||
const subscription: Subscription = getData(LS_KEYS.SUBSCRIPTION);
|
|
||||||
return (
|
|
||||||
subscription &&
|
|
||||||
this.planIsActive(subscription) &&
|
|
||||||
subscription.productID !== FREE_PLAN
|
|
||||||
);
|
|
||||||
}
|
|
||||||
public planIsActive(subscription): boolean {
|
|
||||||
return subscription && subscription.expiryTime > Date.now() * 1000;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new SubscriptionService();
|
export default new SubscriptionService();
|
||||||
|
|
Loading…
Reference in a new issue