From fc8bf78fc34deb5c9b9b732bc1fed74d323f160e Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 6 Nov 2024 14:44:12 +0530 Subject: [PATCH] Move wip chkpt --- .../components/pages/gallery/PlanSelector.tsx | 8 +- web/apps/photos/src/pages/gallery.tsx | 11 +- web/packages/new/photos/services/plan.ts | 117 ++++++++---------- 3 files changed, 61 insertions(+), 75 deletions(-) diff --git a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx index fe67b9c2c6..1a42658ec2 100644 --- a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx +++ b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx @@ -6,7 +6,9 @@ import type { PlansData, Subscription, } from "@/new/photos/services/plan"; -import billingService, { +import { + activateSubscription, + cancelSubscription, getPlansData, hasAddOnBonus, isSubscriptionActive, @@ -807,7 +809,7 @@ function StripeSubscriptionOptions({ const reactivate = async () => { try { setLoading(true); - await billingService.activateSubscription(); + await activateSubscription(); setDialogMessage({ title: t("success"), content: t("SUBSCRIPTION_ACTIVATE_SUCCESS"), @@ -846,7 +848,7 @@ function StripeSubscriptionOptions({ const cancel = async () => { try { setLoading(true); - await billingService.cancelSubscription(); + await cancelSubscription(); setDialogMessage({ title: t("success"), content: t("SUBSCRIPTION_CANCEL_SUCCESS"), diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx index f22ae33855..1a2c4abd37 100644 --- a/web/apps/photos/src/pages/gallery.tsx +++ b/web/apps/photos/src/pages/gallery.tsx @@ -39,6 +39,7 @@ import { getLocalTrashedFiles, sortFiles, } from "@/new/photos/services/files"; +import { verifySubscription } from "@/new/photos/services/plan"; import { filterSearchableFiles, setSearchCollectionsAndFiles, @@ -115,7 +116,7 @@ import { getAllLocalCollections, } from "services/collectionService"; import { syncFiles } from "services/fileService"; -import billingService, { redirectToCustomerPortal } from "services/plan"; +import { redirectToCustomerPortal } from "services/plan"; import { preFileInfoSync, sync } from "services/sync"; import { syncTrash } from "services/trashService"; import uploadManager from "services/upload/uploadManager"; @@ -1237,13 +1238,11 @@ export async function checkSubscriptionPurchase( router: NextRouter, setLoading: SetLoading, ) { - const { session_id: sessionId, status, reason } = router.query ?? {}; + const { session_id: sessionID, status, reason } = router.query ?? {}; - if (status == "success") { + if (status == "success" && typeof sessionID == "string") { try { - const subscription = await billingService.verifySubscription( - sessionId as string, - ); + const subscription = await verifySubscription(sessionID); setDialogMessage( getSubscriptionPurchaseSuccessMessage(subscription), ); diff --git a/web/packages/new/photos/services/plan.ts b/web/packages/new/photos/services/plan.ts index e94f247818..423c9fa304 100644 --- a/web/packages/new/photos/services/plan.ts +++ b/web/packages/new/photos/services/plan.ts @@ -14,8 +14,8 @@ import { } from "@ente/shared/storage/localStorage"; import { getToken } from "@ente/shared/storage/localStorage/helpers"; import isElectron from "is-electron"; -import type { BonusData, UserDetails } from "./user"; import { z } from "zod"; +import type { BonusData, UserDetails } from "./user"; const PlanPeriod = z.enum(["month", "year"]); @@ -73,71 +73,6 @@ const PlansData = z.object({ export type PlansData = z.infer; class billingService { - public async cancelSubscription() { - try { - const response = await HTTPService.post( - await apiURL("/billing/stripe/cancel-subscription"), - null, - null, - { - "X-Auth-Token": getToken(), - }, - ); - const { subscription } = response.data; - setData(LS_KEYS.SUBSCRIPTION, subscription); - } catch (e) { - log.error("subscription cancel failed", e); - throw e; - } - } - - public async activateSubscription() { - try { - const response = await HTTPService.post( - await apiURL("/billing/stripe/activate-subscription"), - null, - null, - { - "X-Auth-Token": getToken(), - }, - ); - const { subscription } = response.data; - setData(LS_KEYS.SUBSCRIPTION, subscription); - } catch (e) { - log.error("failed to activate subscription", e); - throw e; - } - } - - public async verifySubscription( - sessionID: string = null, - ): Promise { - try { - const token = getToken(); - if (!token) { - return; - } - const response = await HTTPService.post( - await apiURL("/billing/verify-subscription"), - { - paymentProvider: "stripe", - productID: null, - verificationData: sessionID, - }, - null, - { - "X-Auth-Token": token, - }, - ); - const { subscription } = response.data; - setData(LS_KEYS.SUBSCRIPTION, subscription); - return subscription; - } catch (e) { - log.error("Error while verifying subscription", e); - throw e; - } - } - public async leaveFamily() { if (!getToken()) { return; @@ -172,6 +107,56 @@ export const getPlansData = async (): Promise => { return z.object({ data: PlansData }).parse(await res.json()).data; }; +const SubscriptionResponse = z.object({ + data: z.object({ + subscription: Subscription, + }), +}); + +export const verifySubscription = async ( + sessionID: string, +): Promise => { + const res = await fetch(await apiURL("/billing/verify-subscription"), { + method: "POST", + headers: await authenticatedRequestHeaders(), + body: JSON.stringify({ + paymentProvider: "stripe", + productID: null, + verificationData: sessionID, + }), + }); + ensureOk(res); + const { subscription } = SubscriptionResponse.parse(await res.json()).data; + setData(LS_KEYS.SUBSCRIPTION, subscription); + return subscription; +}; + +export const activateSubscription = async () => { + const res = await fetch( + await apiURL("/billing/stripe/activate-subscription"), + { + method: "POST", + headers: await authenticatedRequestHeaders(), + }, + ); + ensureOk(res); + const { subscription } = SubscriptionResponse.parse(await res.json()).data; + setData(LS_KEYS.SUBSCRIPTION, subscription); +}; + +export const cancelSubscription = async () => { + const res = await fetch( + await apiURL("/billing/stripe/cancel-subscription"), + { + method: "POST", + headers: await authenticatedRequestHeaders(), + }, + ); + ensureOk(res); + const { subscription } = SubscriptionResponse.parse(await res.json()).data; + setData(LS_KEYS.SUBSCRIPTION, subscription); +}; + /** * Start the flow to purchase or update a subscription by redirecting the user * to the payments app.