Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

handle store subscriptions on account deletion

daniel31x13828125f11e33
diff --git a/apps/mobile/components/ActionSheets/DeleteAccountSheet.tsx b/apps/mobile/components/ActionSheets/DeleteAccountSheet.tsx
index a4ec205d..993ce089 100644
--- a/apps/mobile/components/ActionSheets/DeleteAccountSheet.tsx
+++ b/apps/mobile/components/ActionSheets/DeleteAccountSheet.tsx
@@ -1,5 +1,5 @@
import { useRef, useState } from "react";
-import { Alert, Text, TouchableOpacity, View } from "react-native";
+import { Alert, Linking, Text, TouchableOpacity, View } from "react-native";
import ActionSheet, {
ActionSheetRef,
SheetManager,
@@ -114,6 +114,37 @@ export default function DeleteAccountSheet() {
irreversible!
</Text>
+ {user?.subscription?.active &&
+ user.subscription.provider === "APPLE" ? (
+ <View className="border border-neutral-content p-3 rounded-md mt-5">
+ <Text className="text-base-content">
+ Deleting your account won't cancel your App Store subscription. To
+ stop future charges, cancel it from your Apple ID's subscription
+ settings.
+ </Text>
+ <TouchableOpacity
+ activeOpacity={0.7}
+ onPress={() =>
+ Linking.openURL("https://apps.apple.com/account/subscriptions")
+ }
+ >
+ <Text className="text-primary font-semibold mt-2">
+ Manage subscriptions
+ </Text>
+ </TouchableOpacity>
+ </View>
+ ) : null}
+
+ {user?.subscription?.active &&
+ user.subscription.provider === "GOOGLE" ? (
+ <View className="border border-neutral-content p-3 rounded-md mt-5">
+ <Text className="text-base-content">
+ Your Google Play subscription's auto-renewal will be cancelled
+ automatically when your account is deleted.
+ </Text>
+ </View>
+ ) : null}
+
{user?.hasPassword ? (
<>
<Text className="text-base-content mt-5 mb-2 font-semibold">
diff --git a/apps/web/lib/api/billing/googlePlay.ts b/apps/web/lib/api/billing/googlePlay.ts
index b1519be0..196407a0 100644
--- a/apps/web/lib/api/billing/googlePlay.ts
+++ b/apps/web/lib/api/billing/googlePlay.ts
@@ -134,6 +134,27 @@ export default async function getGoogleSubscriptionState(
};
}
+// Turns off auto-renew; the subscription stays paid-up until its period ends.
+// Used when an account is deleted, since the user can no longer benefit from it.
+// (App Store subscriptions have no equivalent — only the customer can cancel.)
+export async function cancelGoogleSubscription(purchaseToken: string) {
+ try {
+ await getClient().request({
+ url: `${API_BASE}/purchases/subscriptionsv2/tokens/${encodeURIComponent(
+ purchaseToken
+ )}:cancel`,
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ data: {},
+ });
+ } catch (error: any) {
+ const status = error?.response?.status;
+ // Already cancelled/expired or purged token — nothing left to cancel
+ if (status === 400 || status === 404 || status === 410) return;
+ throw error;
+ }
+}
+
// Unacknowledged purchases are auto-refunded by Google after 3 days, so this must
// run server-side even though the app also finishes the transaction client-side.
export async function acknowledgeGoogleSubscription(
diff --git a/apps/web/lib/api/controllers/users/userId/deleteUserById.ts b/apps/web/lib/api/controllers/users/userId/deleteUserById.ts
index fb487688..903b7c3d 100644
--- a/apps/web/lib/api/controllers/users/userId/deleteUserById.ts
+++ b/apps/web/lib/api/controllers/users/userId/deleteUserById.ts
@@ -6,6 +6,10 @@ import updateSeats from "@/lib/api/billing/updateSeats";
import { meiliClient } from "@linkwarden/lib/meilisearchClient";
import stripeSDK from "@/lib/api/billing/stripeSDK";
import { isStoreBillingConfigured } from "@/lib/api/billing/syncStoreSubscription";
+import {
+ cancelGoogleSubscription,
+ isGooglePlayConfigured,
+} from "@/lib/api/billing/googlePlay";
import transporter from "@linkwarden/lib/transporter";
export default async function deleteUserById(
@@ -222,6 +226,31 @@ export default async function deleteUserById(
}
}
+ // A Play Store subscription can be cancelled server-side (auto-renew is
+ // turned off; it stays paid-up until the period ends). App Store
+ // subscriptions have no cancellation API — the UI tells those users to
+ // cancel through Apple instead.
+ if (isGooglePlayConfigured()) {
+ try {
+ const subscription =
+ queryId === userId
+ ? user.subscriptions
+ : await prisma.subscription.findFirst({
+ where: { userId: queryId },
+ select: { provider: true, googlePurchaseToken: true },
+ });
+
+ if (
+ subscription?.provider === "GOOGLE" &&
+ subscription.googlePurchaseToken
+ ) {
+ await cancelGoogleSubscription(subscription.googlePurchaseToken);
+ }
+ } catch (err) {
+ console.log(err);
+ }
+ }
+
// Finally, delete the user
await prisma.user.delete({
where: { id: queryId },
diff --git a/apps/web/pages/settings/delete.tsx b/apps/web/pages/settings/delete.tsx
index 27403cfe..f330ef8f 100644
--- a/apps/web/pages/settings/delete.tsx
+++ b/apps/web/pages/settings/delete.tsx
@@ -76,6 +76,28 @@ export default function Delete() {
<p>{t("delete_warning")}</p>
+ {user?.subscription?.active &&
+ user.subscription.provider === "APPLE" && (
+ <p className="text-sm border border-primary rounded-md p-2">
+ {t("delete_account_apple_subscription_notice")}{" "}
+ <a
+ className="underline"
+ href="https://apps.apple.com/account/subscriptions"
+ target="_blank"
+ rel="noreferrer"
+ >
+ {t("manage_subscriptions")}
+ </a>
+ </p>
+ )}
+
+ {user?.subscription?.active &&
+ user.subscription.provider === "GOOGLE" && (
+ <p className="text-sm border border-primary rounded-md p-2">
+ {t("delete_account_google_subscription_notice")}
+ </p>
+ )}
+
<div>
<TextInput
value={password}
diff --git a/apps/web/public/locales/en/common.json b/apps/web/public/locales/en/common.json
index 69856019..88bb9198 100644
--- a/apps/web/public/locales/en/common.json
+++ b/apps/web/public/locales/en/common.json
@@ -133,6 +133,9 @@
"fill_required_fields": "Please fill the required fields.",
"deleting_message": "Deleting everything, please wait...",
"delete_warning": "This will permanently delete all the Links, Collections, Tags, and archived data you own. It will also log you out. This action is irreversible!",
+ "delete_account_apple_subscription_notice": "Deleting your account won't cancel your App Store subscription. To stop future charges, cancel it from your Apple ID's subscription settings.",
+ "delete_account_google_subscription_notice": "Your Google Play subscription's auto-renewal will be cancelled automatically when your account is deleted.",
+ "manage_subscriptions": "Manage subscriptions",
"optional": "Optional",
"feedback_help": "(but it really helps us improve!)",
"reason_for_cancellation": "Reason for cancellation",