updated subscription service
This commit is contained in:
parent
6c43967018
commit
e6e0315270
|
@ -23,6 +23,7 @@ export interface Plan {
|
||||||
}
|
}
|
||||||
class SubscriptionService {
|
class SubscriptionService {
|
||||||
private stripe;
|
private stripe;
|
||||||
|
private productID;
|
||||||
public init() {
|
public init() {
|
||||||
let publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY;
|
let publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY;
|
||||||
this.stripe = window['Stripe'](publishableKey);
|
this.stripe = window['Stripe'](publishableKey);
|
||||||
|
@ -35,36 +36,48 @@ class SubscriptionService {
|
||||||
console.error('failed to get plans', e);
|
console.error('failed to get plans', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public async buySubscription(priceID) {
|
public async buySubscription(productID) {
|
||||||
try {
|
try {
|
||||||
const response = await this.createCheckoutSession(priceID);
|
this.productID = productID;
|
||||||
|
const response = await this.createCheckoutSession();
|
||||||
await this.stripe.redirectToCheckout({
|
await this.stripe.redirectToCheckout({
|
||||||
sessionId: response.data['sessionID'],
|
sessionId: response.data['sessionID'],
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error('unable to buy subscription', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async createCheckoutSession(priceId) {
|
private async createCheckoutSession() {
|
||||||
return HTTPService.post(`${ENDPOINT}/billing/create-checkout-session`, {
|
return HTTPService.post(`${ENDPOINT}/billing/create-checkout-session`, {
|
||||||
priceId: priceId,
|
productID: this.productID,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getCheckoutSession(sessionId) {
|
public async verifySubscription(sessionID): Promise<Subscription> {
|
||||||
try {
|
try {
|
||||||
const session = await HTTPService.get(
|
const response = await HTTPService.post(
|
||||||
`${ENDPOINT}/billing/checkout-session`,
|
`${ENDPOINT}/billing/verify-subscription`,
|
||||||
{
|
{
|
||||||
sessionId: sessionId,
|
paymentProvider: 'stripe',
|
||||||
|
productID: this.productID,
|
||||||
|
VerificationData: sessionID,
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
'X-Auth-Token': getToken(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return JSON.stringify(session, null, 2);
|
console.log(response.data['subscription']);
|
||||||
|
return response.data['subscription'];
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error when fetching Checkout session', err);
|
console.error('Error while verifying subscription', err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async redirectToCustomerPortal() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
async getUsage() {
|
async getUsage() {
|
||||||
try {
|
try {
|
||||||
const response = await HTTPService.get(
|
const response = await HTTPService.get(
|
||||||
|
|
Loading…
Reference in a new issue