fix(subscription): return chat paywalls after payment

This commit is contained in:
2026-06-25 10:20:13 +08:00
parent 9555a173b5
commit c8f0827e40
7 changed files with 58 additions and 14 deletions
+9 -9
View File
@@ -56,6 +56,9 @@ export function ChatScreen() {
const inlineUpgradeCta = "Activate VIP to view private messages"; const inlineUpgradeCta = "Activate VIP to view private messages";
const externalBrowserPromptShownRef = useRef(false); const externalBrowserPromptShownRef = useRef(false);
const chatPaywallSubscriptionUrl = ROUTE_BUILDERS.subscription("vip", {
returnTo: "chat",
});
useEffect(() => { useEffect(() => {
if (!authState.hasInitialized || authState.isLoading) return; if (!authState.hasInitialized || authState.isLoading) return;
@@ -127,30 +130,27 @@ export function ChatScreen() {
} }
function handleUnlockPrivateMessage(): void { function handleUnlockPrivateMessage(): void {
const subscriptionUrl = ROUTE_BUILDERS.subscription("vip");
if (isGuest) { if (isGuest) {
router.push(ROUTE_BUILDERS.authWithRedirect(subscriptionUrl)); router.push(ROUTE_BUILDERS.authWithRedirect(chatPaywallSubscriptionUrl));
return; return;
} }
router.push(subscriptionUrl); router.push(chatPaywallSubscriptionUrl);
} }
function handleUnlockImagePaywall(): void { function handleUnlockImagePaywall(): void {
const subscriptionUrl = ROUTE_BUILDERS.subscription("vip");
if (isGuest) { if (isGuest) {
router.push(ROUTE_BUILDERS.authWithRedirect(subscriptionUrl)); router.push(ROUTE_BUILDERS.authWithRedirect(chatPaywallSubscriptionUrl));
return; return;
} }
router.push(subscriptionUrl); router.push(chatPaywallSubscriptionUrl);
} }
function handleMessageLimitUnlock(): void { function handleMessageLimitUnlock(): void {
const subscriptionUrl = ROUTE_BUILDERS.subscription("vip");
if (isGuest) { if (isGuest) {
router.push(ROUTE_BUILDERS.authWithRedirect(subscriptionUrl)); router.push(ROUTE_BUILDERS.authWithRedirect(chatPaywallSubscriptionUrl));
return; return;
} }
router.push(subscriptionUrl); router.push(chatPaywallSubscriptionUrl);
} }
return ( return (
@@ -24,11 +24,13 @@ export interface SubscriptionCheckoutButtonProps {
/** 是否可用(未选套餐 / 未勾选协议 → false) */ /** 是否可用(未选套餐 / 未勾选协议 → false) */
disabled?: boolean; disabled?: boolean;
subscriptionType: "vip" | "voice"; subscriptionType: "vip" | "voice";
returnTo?: "chat" | null;
} }
export function SubscriptionCheckoutButton({ export function SubscriptionCheckoutButton({
disabled = false, disabled = false,
subscriptionType, subscriptionType,
returnTo = null,
}: SubscriptionCheckoutButtonProps) { }: SubscriptionCheckoutButtonProps) {
const payment = usePaymentState(); const payment = usePaymentState();
const paymentDispatch = usePaymentDispatch(); const paymentDispatch = usePaymentDispatch();
@@ -67,6 +69,7 @@ export function SubscriptionCheckoutButton({
orderId: payment.currentOrderId, orderId: payment.currentOrderId,
paymentUrl, paymentUrl,
subscriptionType, subscriptionType,
returnTo,
onFailed: (errorMessage) => onFailed: (errorMessage) =>
paymentDispatch({ type: "PaymentLaunchFailed", errorMessage }), paymentDispatch({ type: "PaymentLaunchFailed", errorMessage }),
}); });
@@ -87,6 +90,7 @@ export function SubscriptionCheckoutButton({
payment.launchNonce, payment.launchNonce,
payment.payParams, payment.payParams,
paymentDispatch, paymentDispatch,
returnTo,
subscriptionType, subscriptionType,
]); ]);
@@ -150,6 +154,7 @@ export function SubscriptionCheckoutButton({
orderId: payment.currentOrderId, orderId: payment.currentOrderId,
paymentUrl: ezpayPaymentUrl, paymentUrl: ezpayPaymentUrl,
subscriptionType, subscriptionType,
returnTo,
onFailed: (errorMessage) => { onFailed: (errorMessage) => {
setIsConfirmingEzpay(false); setIsConfirmingEzpay(false);
paymentDispatch({ type: "PaymentLaunchFailed", errorMessage }); paymentDispatch({ type: "PaymentLaunchFailed", errorMessage });
@@ -241,6 +246,7 @@ interface RedirectToEzpayInput {
orderId: string | null; orderId: string | null;
paymentUrl: string; paymentUrl: string;
subscriptionType: "vip" | "voice"; subscriptionType: "vip" | "voice";
returnTo?: "chat" | null;
onFailed: (errorMessage: string) => void; onFailed: (errorMessage: string) => void;
} }
@@ -248,6 +254,7 @@ async function launchEzpayRedirect({
orderId, orderId,
paymentUrl, paymentUrl,
subscriptionType, subscriptionType,
returnTo,
onFailed, onFailed,
}: RedirectToEzpayInput): Promise<void> { }: RedirectToEzpayInput): Promise<void> {
log.debug("[subscription-checkout] launchEzpayRedirect START", { log.debug("[subscription-checkout] launchEzpayRedirect START", {
@@ -272,6 +279,7 @@ async function launchEzpayRedirect({
orderId, orderId,
payChannel: "ezpay", payChannel: "ezpay",
subscriptionType, subscriptionType,
...(returnTo ? { returnTo } : {}),
createdAt: Date.now(), createdAt: Date.now(),
}); });
if (Result.isErr(saveResult)) { if (Result.isErr(saveResult)) {
+11 -2
View File
@@ -9,11 +9,15 @@ import { ROUTES } from "@/router/routes";
type ReturnStatus = "loading" | "missing"; type ReturnStatus = "loading" | "missing";
function buildSubscriptionUrl(subscriptionType: "vip" | "voice"): string { function buildSubscriptionUrl(
subscriptionType: "vip" | "voice",
returnTo?: "chat",
): string {
const params = new URLSearchParams({ const params = new URLSearchParams({
type: subscriptionType, type: subscriptionType,
paymentReturn: "1", paymentReturn: "1",
}); });
if (returnTo) params.set("returnTo", returnTo);
return `${ROUTES.subscription}?${params.toString()}`; return `${ROUTES.subscription}?${params.toString()}`;
} }
@@ -29,7 +33,12 @@ export default function SubscriptionReturnPage() {
if (cancelled) return; if (cancelled) return;
if (result.success && result.data !== null) { if (result.success && result.data !== null) {
router.replace(buildSubscriptionUrl(result.data.subscriptionType)); router.replace(
buildSubscriptionUrl(
result.data.subscriptionType,
result.data.returnTo,
),
);
return; return;
} }
@@ -11,15 +11,21 @@ function toSubscriptionType(value: string | null): SubscriptionType {
return value === "voice" ? "voice" : "vip"; return value === "voice" ? "voice" : "vip";
} }
function toReturnTo(value: string | null): "chat" | null {
return value === "chat" ? "chat" : null;
}
export function SubscriptionPageClient() { export function SubscriptionPageClient() {
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const subscriptionType = toSubscriptionType(searchParams.get("type")); const subscriptionType = toSubscriptionType(searchParams.get("type"));
const shouldResumePendingOrder = searchParams.get("paymentReturn") === "1"; const shouldResumePendingOrder = searchParams.get("paymentReturn") === "1";
const returnTo = toReturnTo(searchParams.get("returnTo"));
return ( return (
<SubscriptionScreen <SubscriptionScreen
subscriptionType={subscriptionType} subscriptionType={subscriptionType}
shouldResumePendingOrder={shouldResumePendingOrder} shouldResumePendingOrder={shouldResumePendingOrder}
returnTo={returnTo}
/> />
); );
} }
+15 -1
View File
@@ -15,6 +15,7 @@
* 8. 协议复选框 * 8. 协议复选框
*/ */
import { useEffect, useMemo, useRef, useState } from "react"; import { useEffect, useMemo, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import { Checkbox, MobileShell } from "@/app/_components/core"; import { Checkbox, MobileShell } from "@/app/_components/core";
import { AppConstants } from "@/core/app_constants"; import { AppConstants } from "@/core/app_constants";
@@ -25,6 +26,7 @@ import {
usePaymentState, usePaymentState,
} from "@/stores/payment/payment-context"; } from "@/stores/payment/payment-context";
import { useUserDispatch, useUserState } from "@/stores/user/user-context"; import { useUserDispatch, useUserState } from "@/stores/user/user-context";
import { ROUTES } from "@/router/routes";
import { import {
SubscriptionBackLink, SubscriptionBackLink,
@@ -50,12 +52,15 @@ export type { SubscriptionType } from "./subscription-screen.helpers";
export interface SubscriptionScreenProps { export interface SubscriptionScreenProps {
subscriptionType?: SubscriptionType; subscriptionType?: SubscriptionType;
shouldResumePendingOrder?: boolean; shouldResumePendingOrder?: boolean;
returnTo?: "chat" | null;
} }
export function SubscriptionScreen({ export function SubscriptionScreen({
subscriptionType = "vip", subscriptionType = "vip",
shouldResumePendingOrder = false, shouldResumePendingOrder = false,
returnTo = null,
}: SubscriptionScreenProps) { }: SubscriptionScreenProps) {
const router = useRouter();
const user = useUserState(); const user = useUserState();
const userDispatch = useUserDispatch(); const userDispatch = useUserDispatch();
const payment = usePaymentState(); const payment = usePaymentState();
@@ -176,6 +181,14 @@ export function SubscriptionScreen({
? "Choose a voice package" ? "Choose a voice package"
: "Choose a subscription plan"; : "Choose a subscription plan";
const handlePaymentSuccessClose = () => {
setShowPaymentSuccessDialog(false);
paymentDispatch({ type: "PaymentReset" });
if (returnTo === "chat") {
router.replace(ROUTES.chat);
}
};
useEffect(() => { useEffect(() => {
if (payment.isLoadingPlans || payment.plans.length === 0) return; if (payment.isLoadingPlans || payment.plans.length === 0) return;
if (selectedPlan !== null) return; if (selectedPlan !== null) return;
@@ -272,6 +285,7 @@ export function SubscriptionScreen({
<SubscriptionCheckoutButton <SubscriptionCheckoutButton
disabled={!canActivate} disabled={!canActivate}
subscriptionType={subscriptionType} subscriptionType={subscriptionType}
returnTo={returnTo}
/> />
</div> </div>
@@ -312,7 +326,7 @@ export function SubscriptionScreen({
<SubscriptionPaymentSuccessDialog <SubscriptionPaymentSuccessDialog
open={showPaymentSuccessDialog} open={showPaymentSuccessDialog}
subscriptionType={subscriptionType} subscriptionType={subscriptionType}
onClose={() => setShowPaymentSuccessDialog(false)} onClose={handlePaymentSuccessClose}
/> />
</div> </div>
</MobileShell> </MobileShell>
@@ -10,6 +10,7 @@ const PendingPaymentOrderSchema = z.object({
orderId: z.string().min(1), orderId: z.string().min(1),
payChannel: z.literal("ezpay"), payChannel: z.literal("ezpay"),
subscriptionType: z.enum(["vip", "voice"]), subscriptionType: z.enum(["vip", "voice"]),
returnTo: z.enum(["chat"]).optional(),
createdAt: z.number(), createdAt: z.number(),
}); });
+8 -2
View File
@@ -30,8 +30,14 @@ export type StaticRoute = (typeof ROUTES)[keyof typeof ROUTES];
export const ROUTE_BUILDERS = { export const ROUTE_BUILDERS = {
chatDeviceId: (deviceId: string): `/chat/deviceid/${string}` => chatDeviceId: (deviceId: string): `/chat/deviceid/${string}` =>
`/chat/deviceid/${encodeURIComponent(deviceId)}` as const, `/chat/deviceid/${encodeURIComponent(deviceId)}` as const,
subscription: (type: "vip" | "voice"): `/subscription?type=${string}` => subscription: (
`${ROUTES.subscription}?type=${encodeURIComponent(type)}` as const, type: "vip" | "voice",
options: { returnTo?: "chat" } = {},
): `/subscription?${string}` => {
const params = new URLSearchParams({ type });
if (options.returnTo) params.set("returnTo", options.returnTo);
return `${ROUTES.subscription}?${params.toString()}` as const;
},
authWithRedirect: (redirectTo: string): `/auth?redirect=${string}` => authWithRedirect: (redirectTo: string): `/auth?redirect=${string}` =>
`${ROUTES.auth}?redirect=${encodeURIComponent(redirectTo)}` as const, `${ROUTES.auth}?redirect=${encodeURIComponent(redirectTo)}` as const,
} as const; } as const;