Skip to content

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

bug fixed

daniel31x13d91089ed4800
diff --git a/apps/web/lib/api/paymentCheckout.ts b/apps/web/lib/api/paymentCheckout.ts
index 2bc2ec81..643f8506 100644
--- a/apps/web/lib/api/paymentCheckout.ts
+++ b/apps/web/lib/api/paymentCheckout.ts
@@ -5,6 +5,7 @@ import stripeSDK from "./stripe/stripeSDK";
const REQUIRE_CC = process.env.NEXT_PUBLIC_REQUIRE_CC === "true";
const MANAGED_PAYMENTS_ENABLED =
process.env.MANAGED_PAYMENTS_ENABLED === "true";
+const TRIAL_PERIOD_DAYS = process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS || 14;
export default async function paymentCheckout(email: string, priceId: string) {
const stripe = stripeSDK();
@@ -19,9 +20,22 @@ export default async function paymentCheckout(email: string, priceId: string) {
},
});
+ if (!user) {
+ return { response: "User not found", status: 404 };
+ }
+
+ const trialEndTime =
+ new Date(user.createdAt).getTime() +
+ (1 + Number(TRIAL_PERIOD_DAYS)) * 86400000; // Add 1 to account for the current day
+
+ const daysLeft = Math.floor((trialEndTime - Date.now()) / 86400000);
+
const subscription = await verifySubscription(user);
- if (subscription) {
+ if (
+ subscription?.subscriptions?.active ||
+ subscription?.parentSubscription?.active
+ ) {
// To prevent users from creating multiple subscriptions
return { response: "/dashboard", status: 200 };
}
@@ -56,7 +70,13 @@ export default async function paymentCheckout(email: string, priceId: string) {
: 14,
},
}
- : {}),
+ : daysLeft > 0
+ ? {
+ subscription_data: {
+ trial_period_days: daysLeft,
+ },
+ }
+ : {}),
...(MANAGED_PAYMENTS_ENABLED
? {
managed_payments: {
diff --git a/apps/web/pages/subscribe.tsx b/apps/web/pages/subscribe.tsx
index c2d5a0de..244b3971 100644
--- a/apps/web/pages/subscribe.tsx
+++ b/apps/web/pages/subscribe.tsx
@@ -10,6 +10,7 @@ import { Trans, useTranslation } from "next-i18next";
import { useUser } from "@linkwarden/router/user";
import { Separator } from "@/components/ui/separator";
import Link from "next/link";
+import { cn } from "@/lib/utils";
const TRIAL_PERIOD_DAYS =
Number(process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS) || 14;
@@ -149,31 +150,40 @@ export default function Subscribe() {
</fieldset>
</div>
- <Button
- type="button"
- variant="accent"
- size="full"
- onClick={submit}
- disabled={submitLoader}
+ <div
+ className={cn(
+ "flex gap-3",
+ REQUIRE_CC || daysLeft <= 0 ? "flex-col" : "flex-row-reverse"
+ )}
>
- {t("complete_subscription")}
- </Button>
-
- {REQUIRE_CC ? (
- <div
- onClick={() => signOut()}
- className="w-fit mx-auto cursor-pointer text-neutral font-semibold "
- >
- {t("sign_out")}
- </div>
- ) : (
- <Link
- className="w-fit mx-auto cursor-pointer text-neutral font-semibold "
- href="/dashboard"
+ <Button
+ type="button"
+ variant="accent"
+ size="full"
+ onClick={submit}
+ disabled={submitLoader}
>
- {t("subscribe_later")}
- </Link>
- )}
+ {t("complete_subscription")}
+ </Button>
+
+ {REQUIRE_CC || daysLeft <= 0 ? (
+ <div
+ onClick={() => signOut()}
+ className="w-fit mx-auto cursor-pointer text-neutral font-semibold "
+ >
+ {t("sign_out")}
+ </div>
+ ) : (
+ <Button
+ className=""
+ variant="metal"
+ size="full"
+ onClick={() => router.push("/dashboard")}
+ >
+ {t("subscribe_later")}
+ </Button>
+ )}
+ </div>
</div>
</CenteredForm>
);