Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Ref

Manav Rathi3a5189e715f6
diff --git a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx
index 1f7de409c9..5a009be8ac 100644
--- a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx
+++ b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx
@@ -41,7 +41,7 @@ import type {
PlansResponse,
Subscription,
} from "services/billingService";
-import billingService from "services/billingService";
+import billingService, { redirectToPaymentsApp } from "services/billingService";
import { getFamilyPortalRedirectURL } from "services/userService";
import { SetLoading } from "types/gallery";
import { BonusData, UserDetails } from "types/user";
@@ -60,7 +60,6 @@ import {
planForSubscription,
planSelectionOutcome,
updatePaymentMethod,
- updateSubscription,
} from "utils/billing";
interface PlanSelectorProps {
@@ -173,7 +172,7 @@ function PlanSelectorCard(props: PlanSelectorCardProps) {
case "buyPlan":
try {
props.setLoading(true);
- await billingService.buySubscription(plan.stripeID);
+ await redirectToPaymentsApp(plan.stripeID, "buy");
} catch (e) {
props.setLoading(false);
setDialogMessage({
@@ -190,14 +189,25 @@ function PlanSelectorCard(props: PlanSelectorCardProps) {
content: t("UPDATE_SUBSCRIPTION_MESSAGE"),
proceed: {
text: t("UPDATE_SUBSCRIPTION"),
- action: updateSubscription.bind(
- null,
- plan,
- setDialogMessage,
- props.setLoading,
- props.closeModal,
- ),
variant: "accent",
+ action: async () => {
+ try {
+ setLoading(true);
+ await redirectToPaymentsApp(
+ plan.stripeID,
+ "update",
+ );
+ } catch (err) {
+ setDialogMessage({
+ title: t("error"),
+ content: t("SUBSCRIPTION_UPDATE_FAILED"),
+ close: { variant: "critical" },
+ });
+ } finally {
+ setLoading(false);
+ closeModal();
+ }
+ },
},
close: { text: t("cancel") },
});
diff --git a/web/apps/photos/src/services/billingService.ts b/web/apps/photos/src/services/billingService.ts
index 362b958444..eccaad7275 100644
--- a/web/apps/photos/src/services/billingService.ts
+++ b/web/apps/photos/src/services/billingService.ts
@@ -71,26 +71,6 @@ class billingService {
}
}
- public async buySubscription(productID: string) {
- try {
- const paymentToken = await getPaymentToken();
- await this.redirectToPayments(paymentToken, productID, "buy");
- } catch (e) {
- log.error("unable to buy subscription", e);
- throw e;
- }
- }
-
- public async updateSubscription(productID: string) {
- try {
- const paymentToken = await getPaymentToken();
- await this.redirectToPayments(paymentToken, productID, "update");
- } catch (e) {
- log.error("subscription update failed", e);
- throw e;
- }
- }
-
public async cancelSubscription() {
try {
const response = await HTTPService.post(
@@ -176,23 +156,9 @@ class billingService {
}
}
- public async redirectToPayments(
- paymentToken: string,
- productID: string,
- action: string,
- ) {
- try {
- const redirectURL = this.getRedirectURL();
- window.location.href = `${paymentsAppOrigin()}?productID=${productID}&paymentToken=${paymentToken}&action=${action}&redirectURL=${redirectURL}`;
- } catch (e) {
- log.error("unable to get payments url", e);
- throw e;
- }
- }
-
public async redirectToCustomerPortal() {
try {
- const redirectURL = this.getRedirectURL();
+ const redirectURL = completionRedirectURL();
const response = await HTTPService.get(
await apiURL("/billing/stripe/customer-portal"),
{ redirectURL },
@@ -206,18 +172,36 @@ class billingService {
throw e;
}
}
-
- public getRedirectURL() {
- if (isElectron()) {
- return `${paymentsAppOrigin()}/desktop-redirect`;
- } else {
- return `${window.location.origin}/gallery`;
- }
- }
}
export default new billingService();
+/**
+ * Start the flow to purchase or update a subscription by redirecting the user
+ * to the payments app.
+ *
+ * @param productID The Stripe product ID of the plan to purchase.
+ *
+ * @param action buy or update.
+ */
+export const redirectToPaymentsApp = async (
+ productID: string,
+ action: "buy" | "update",
+) => {
+ const paymentToken = await getPaymentToken();
+ const redirectURL = completionRedirectURL();
+ window.location.href = `${paymentsAppOrigin()}?productID=${productID}&paymentToken=${paymentToken}&action=${action}&redirectURL=${redirectURL}`;
+};
+
+/**
+ * Return the URL to which the payments app should redirect back on completion
+ * of the flow.
+ */
+const completionRedirectURL = () =>
+ isElectron()
+ ? `${paymentsAppOrigin()}/desktop-redirect`
+ : `${window.location.origin}/gallery`;
+
/**
* Fetch and return a one-time token that can be used to authenticate user's
* requests to the payments app.
diff --git a/web/apps/photos/src/utils/billing/index.ts b/web/apps/photos/src/utils/billing/index.ts
index 77429c820a..5820c659de 100644
--- a/web/apps/photos/src/utils/billing/index.ts
+++ b/web/apps/photos/src/utils/billing/index.ts
@@ -159,27 +159,6 @@ export function isSubscriptionPastDue(subscription: Subscription) {
export const isPopularPlan = (plan: Plan) =>
plan.storage === 100 * 1024 * 1024 * 1024; /* 100 GB */
-export async function updateSubscription(
- plan: Plan,
- setDialogMessage: SetDialogBoxAttributes,
- setLoading: SetLoading,
- closePlanSelectorModal: () => null,
-) {
- try {
- setLoading(true);
- await billingService.updateSubscription(plan.stripeID);
- } catch (err) {
- setDialogMessage({
- title: t("error"),
- content: t("SUBSCRIPTION_UPDATE_FAILED"),
- close: { variant: "critical" },
- });
- } finally {
- setLoading(false);
- closePlanSelectorModal();
- }
-}
-
export async function cancelSubscription(
setDialogMessage: SetDialogBoxAttributes,
closePlanSelectorModal: () => void,