From e6e0315270334fde0f75f17de79a228e3eacd2fe Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Fri, 12 Mar 2021 23:04:20 +0530 Subject: [PATCH] updated subscription service --- src/services/subscriptionService.ts | 35 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/services/subscriptionService.ts b/src/services/subscriptionService.ts index 3d9a1e8d0..603aaf8d2 100644 --- a/src/services/subscriptionService.ts +++ b/src/services/subscriptionService.ts @@ -23,6 +23,7 @@ export interface Plan { } class SubscriptionService { private stripe; + private productID; public init() { let publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY; this.stripe = window['Stripe'](publishableKey); @@ -35,36 +36,48 @@ class SubscriptionService { console.error('failed to get plans', e); } } - public async buySubscription(priceID) { + public async buySubscription(productID) { try { - const response = await this.createCheckoutSession(priceID); + this.productID = productID; + const response = await this.createCheckoutSession(); await this.stripe.redirectToCheckout({ sessionId: response.data['sessionID'], }); } 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`, { - priceId: priceId, + productID: this.productID, }); } - public async getCheckoutSession(sessionId) { + public async verifySubscription(sessionID): Promise { try { - const session = await HTTPService.get( - `${ENDPOINT}/billing/checkout-session`, + const response = await HTTPService.post( + `${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) { - console.error('Error when fetching Checkout session', err); + console.error('Error while verifying subscription', err); } } + + public async redirectToCustomerPortal() { + return null; + } async getUsage() { try { const response = await HTTPService.get(