Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor ProPlan component and enhance subscription management features

Bill Yangddcb5704d180
diff --git a/client/src/app/settings/subscription/components/ProPlan.tsx b/client/src/app/settings/subscription/components/ProPlan.tsx
index 8128c9edb..88ff7ff8a 100644
--- a/client/src/app/settings/subscription/components/ProPlan.tsx
+++ b/client/src/app/settings/subscription/components/ProPlan.tsx
@@ -45,7 +45,7 @@ export function ProPlan() {
}
: null;
- const handleManageSubscription = async () => {
+ const createPortalSession = async (flowType?: string) => {
setActionError(null);
setIsProcessing(true);
try {
@@ -59,6 +59,7 @@ export function ProPlan() {
credentials: "include",
body: JSON.stringify({
returnUrl: window.location.href,
+ flowType,
}),
}
);
@@ -83,6 +84,11 @@ export function ProPlan() {
}
};
+ const handleChangePlan = () => createPortalSession("subscription_update");
+ const handleViewSubscription = () => createPortalSession();
+ const handleCancelSubscription = () =>
+ createPortalSession("subscription_cancel");
+
const getFormattedPrice = () => {
if (!currentPlanDetails) return "$0/month";
return `${currentPlanDetails.price}/${
@@ -130,13 +136,22 @@ export function ProPlan() {
{formatRenewalDate()}
</p>
</div>
- <Button
- variant="outline"
- onClick={handleManageSubscription}
- disabled={isProcessing}
- >
- {isProcessing ? "Processing..." : "Change Plan"}
- </Button>
+ <div className="space-x-2">
+ <Button
+ variant="success"
+ onClick={handleChangePlan}
+ disabled={isProcessing}
+ >
+ {isProcessing ? "Processing..." : "Change Plan"}
+ </Button>
+ <Button
+ variant="outline"
+ onClick={handleViewSubscription}
+ disabled={isProcessing}
+ >
+ View Details
+ </Button>
+ </div>
</div>
<div className="space-y-2">
@@ -165,6 +180,18 @@ export function ProPlan() {
</p>
</div>
)}
+
+ <div className="flex justify-end pt-2 border-t border-neutral-200 dark:border-neutral-800">
+ <Button
+ variant="ghost"
+ onClick={handleCancelSubscription}
+ disabled={isProcessing}
+ size="sm"
+ className="dark:hover:bg-red-700/60"
+ >
+ Cancel Subscription
+ </Button>
+ </div>
</div>
</CardContent>
</Card>
diff --git a/server/src/api/stripe/createPortalSession.ts b/server/src/api/stripe/createPortalSession.ts
index 5305c4aaf..e3a40cd36 100644
--- a/server/src/api/stripe/createPortalSession.ts
+++ b/server/src/api/stripe/createPortalSession.ts
@@ -7,13 +7,17 @@ import Stripe from "stripe";
interface PortalRequestBody {
returnUrl: string;
+ flowType?:
+ | "subscription_update"
+ | "subscription_cancel"
+ | "payment_method_update";
}
export async function createPortalSession(
request: FastifyRequest<{ Body: PortalRequestBody }>,
reply: FastifyReply
) {
- const { returnUrl } = request.body;
+ const { returnUrl, flowType } = request.body;
const userId = request.user?.id;
if (!userId) {
@@ -44,13 +48,68 @@ export async function createPortalSession(
.send({ error: "User or Stripe customer ID not found" });
}
- // 2. Create a Stripe Billing Portal Session
- const portalSession = await (
- stripe as Stripe
- ).billingPortal.sessions.create({
+ // 2. Create a Stripe Billing Portal Session, with optional direct flow
+ const sessionConfig: Stripe.BillingPortal.SessionCreateParams = {
customer: user.stripeCustomerId,
return_url: returnUrl, // The user will be redirected here after managing their billing
- });
+ };
+
+ // If a specific flow is requested, add it to the configuration
+ if (flowType) {
+ if (flowType === "subscription_update") {
+ // For subscription_update flow, we need to fetch the subscription ID first
+ const subscriptions = await (stripe as Stripe).subscriptions.list({
+ customer: user.stripeCustomerId,
+ status: "active",
+ limit: 1,
+ });
+
+ if (subscriptions.data.length === 0) {
+ return reply
+ .status(404)
+ .send({ error: "No active subscription found" });
+ }
+
+ const subscriptionId = subscriptions.data[0].id;
+
+ sessionConfig.flow_data = {
+ type: "subscription_update",
+ subscription_update: {
+ subscription: subscriptionId,
+ },
+ };
+ } else if (flowType === "subscription_cancel") {
+ // For subscription_cancel flow, we need to fetch the subscription ID first
+ const subscriptions = await (stripe as Stripe).subscriptions.list({
+ customer: user.stripeCustomerId,
+ status: "active",
+ limit: 1,
+ });
+
+ if (subscriptions.data.length === 0) {
+ return reply
+ .status(404)
+ .send({ error: "No active subscription found" });
+ }
+
+ const subscriptionId = subscriptions.data[0].id;
+
+ sessionConfig.flow_data = {
+ type: "subscription_cancel",
+ subscription_cancel: {
+ subscription: subscriptionId,
+ },
+ };
+ } else if (flowType === "payment_method_update") {
+ sessionConfig.flow_data = {
+ type: "payment_method_update",
+ };
+ }
+ }
+
+ const portalSession = await (
+ stripe as Stripe
+ ).billingPortal.sessions.create(sessionConfig);
// 3. Return the Billing Portal Session URL
return reply.send({ portalUrl: portalSession.url });