fix(subscription): restrict payment methods by country

This commit is contained in:
2026-07-02 15:00:06 +08:00
parent 3032f28d64
commit 515fdcaa56
5 changed files with 116 additions and 20 deletions
@@ -3,10 +3,12 @@ import { describe, expect, it } from "vitest";
import { PaymentPlan } from "@/data/dto/payment"; import { PaymentPlan } from "@/data/dto/payment";
import { import {
canChooseSubscriptionPayChannel,
findSelectedSubscriptionPlan, findSelectedSubscriptionPlan,
getDefaultPayChannelForCountryCode, getDefaultPayChannelForCountryCode,
getDefaultSubscriptionPlanId, getDefaultSubscriptionPlanId,
getFirstRechargeOfferView, getFirstRechargeOfferView,
resolveSubscriptionPayChannel,
toCoinsOfferPlanViews, toCoinsOfferPlanViews,
toVipOfferPlanViews, toVipOfferPlanViews,
} from "../subscription-screen.helpers"; } from "../subscription-screen.helpers";
@@ -38,6 +40,34 @@ describe("subscription screen helpers", () => {
expect(getDefaultPayChannelForCountryCode(null)).toBe("stripe"); expect(getDefaultPayChannelForCountryCode(null)).toBe("stripe");
}); });
it("only allows payment method switching for Philippines users", () => {
expect(canChooseSubscriptionPayChannel("PH")).toBe(true);
expect(canChooseSubscriptionPayChannel("ph")).toBe(true);
expect(canChooseSubscriptionPayChannel("HK")).toBe(false);
expect(canChooseSubscriptionPayChannel(null)).toBe(false);
});
it("resolves subscription pay channel by country", () => {
expect(
resolveSubscriptionPayChannel({
countryCode: "PH",
requestedPayChannel: null,
}),
).toBe("ezpay");
expect(
resolveSubscriptionPayChannel({
countryCode: "PH",
requestedPayChannel: "stripe",
}),
).toBe("stripe");
expect(
resolveSubscriptionPayChannel({
countryCode: "HK",
requestedPayChannel: "ezpay",
}),
).toBe("stripe");
});
it("maps at most three VIP plans and all coin plans", () => { it("maps at most three VIP plans and all coin plans", () => {
const plans = [ const plans = [
makePlan({ planId: "vip_monthly", planName: "Monthly" }), makePlan({ planId: "vip_monthly", planName: "Monthly" }),
@@ -21,12 +21,14 @@ const PAYMENT_METHODS: Array<{
export interface SubscriptionPaymentMethodProps { export interface SubscriptionPaymentMethodProps {
value: PayChannel; value: PayChannel;
disabled?: boolean; disabled?: boolean;
caption?: string;
onChange: (channel: PayChannel) => void; onChange: (channel: PayChannel) => void;
} }
export function SubscriptionPaymentMethod({ export function SubscriptionPaymentMethod({
value, value,
disabled = false, disabled = false,
caption = "Stripe by default",
onChange, onChange,
}: SubscriptionPaymentMethodProps) { }: SubscriptionPaymentMethodProps) {
return ( return (
@@ -35,7 +37,7 @@ export function SubscriptionPaymentMethod({
<h2 id="payment-method-title" className={styles.title}> <h2 id="payment-method-title" className={styles.title}>
Payment Method Payment Method
</h2> </h2>
<span className={styles.caption}>Stripe by default</span> <span className={styles.caption}>{caption}</span>
</div> </div>
<div className={styles.options}> <div className={styles.options}>
{PAYMENT_METHODS.map((method) => { {PAYMENT_METHODS.map((method) => {
@@ -17,8 +17,9 @@ function toReturnTo(value: string | null): "chat" | null {
return value === "chat" ? "chat" : null; return value === "chat" ? "chat" : null;
} }
function toPayChannel(value: string | null): PayChannel { function toPayChannel(value: string | null): PayChannel | null {
return value === "ezpay" ? "ezpay" : "stripe"; if (value === "ezpay" || value === "stripe") return value;
return null;
} }
export function SubscriptionPageClient() { export function SubscriptionPageClient() {
@@ -1,4 +1,5 @@
import type { PaymentPlan } from "@/data/dto/payment"; import type { PayChannel, PaymentPlan } from "@/data/dto/payment";
import { getDefaultPayChannelForCountryCode } from "@/lib/payment/default_pay_channel";
export { getDefaultPayChannelForCountryCode } from "@/lib/payment/default_pay_channel"; export { getDefaultPayChannelForCountryCode } from "@/lib/payment/default_pay_channel";
import type { CoinsOfferPlanView } from "./components/subscription-coins-offer-section"; import type { CoinsOfferPlanView } from "./components/subscription-coins-offer-section";
@@ -12,6 +13,23 @@ export interface FirstRechargeOfferView {
subtitle: string; subtitle: string;
} }
export function canChooseSubscriptionPayChannel(
countryCode: string | null | undefined,
): boolean {
return countryCode?.trim().toUpperCase() === "PH";
}
export function resolveSubscriptionPayChannel(input: {
countryCode: string | null | undefined;
requestedPayChannel: PayChannel | null | undefined;
}): PayChannel {
if (!canChooseSubscriptionPayChannel(input.countryCode)) return "stripe";
return (
input.requestedPayChannel ??
getDefaultPayChannelForCountryCode(input.countryCode)
);
}
export function isVipPlan(plan: PaymentPlan): boolean { export function isVipPlan(plan: PaymentPlan): boolean {
return plan.vipDays !== null; return plan.vipDays !== null;
} }
+49 -4
View File
@@ -1,10 +1,11 @@
"use client"; "use client";
import { useEffect, useMemo } from "react"; import { useEffect, useMemo, useRef } from "react";
import { BackButton } from "@/app/_components"; import { BackButton } from "@/app/_components";
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";
import type { PayChannel } from "@/data/dto/payment"; import type { PayChannel } from "@/data/dto/payment";
import { useUserState } from "@/stores/user/user-context";
import { import {
SubscriptionCheckoutButton, SubscriptionCheckoutButton,
@@ -16,8 +17,10 @@ import {
import styles from "./components/subscription-screen.module.css"; import styles from "./components/subscription-screen.module.css";
import { import {
findSelectedSubscriptionPlan, findSelectedSubscriptionPlan,
canChooseSubscriptionPayChannel,
getFirstRechargeOfferView, getFirstRechargeOfferView,
getDefaultSubscriptionPlanId, getDefaultSubscriptionPlanId,
resolveSubscriptionPayChannel,
toCoinsOfferPlanViews, toCoinsOfferPlanViews,
toVipOfferPlanViews, toVipOfferPlanViews,
type SubscriptionType, type SubscriptionType,
@@ -30,15 +33,24 @@ export interface SubscriptionScreenProps {
subscriptionType?: SubscriptionType; subscriptionType?: SubscriptionType;
shouldResumePendingOrder?: boolean; shouldResumePendingOrder?: boolean;
returnTo?: "chat" | null; returnTo?: "chat" | null;
initialPayChannel?: PayChannel; initialPayChannel?: PayChannel | null;
} }
export function SubscriptionScreen({ export function SubscriptionScreen({
subscriptionType = "vip", subscriptionType = "vip",
shouldResumePendingOrder = false, shouldResumePendingOrder = false,
returnTo = null, returnTo = null,
initialPayChannel = "stripe", initialPayChannel = null,
}: SubscriptionScreenProps) { }: SubscriptionScreenProps) {
const userState = useUserState();
const countryCode = userState.currentUser?.countryCode;
const canChoosePaymentMethod =
canChooseSubscriptionPayChannel(countryCode);
const resolvedInitialPayChannel = resolveSubscriptionPayChannel({
countryCode,
requestedPayChannel: initialPayChannel,
});
const phDefaultPayChannelAppliedRef = useRef(false);
const { const {
payment, payment,
paymentDispatch, paymentDispatch,
@@ -49,7 +61,7 @@ export function SubscriptionScreen({
subscriptionType, subscriptionType,
shouldResumePendingOrder, shouldResumePendingOrder,
returnTo, returnTo,
initialPayChannel, initialPayChannel: resolvedInitialPayChannel,
}); });
const canSubscribeVip = subscriptionType === "vip"; const canSubscribeVip = subscriptionType === "vip";
@@ -89,6 +101,36 @@ export function SubscriptionScreen({
const canActivate = const canActivate =
selectedPlan !== null && payment.agreed && !isPaymentBusy; selectedPlan !== null && payment.agreed && !isPaymentBusy;
useEffect(() => {
if (isPaymentBusy) return;
if (!canChoosePaymentMethod) {
if (payment.payChannel === "stripe") return;
paymentDispatch({
type: "PaymentPayChannelChanged",
payChannel: "stripe",
});
return;
}
if (initialPayChannel !== null) return;
if (phDefaultPayChannelAppliedRef.current) return;
phDefaultPayChannelAppliedRef.current = true;
if (payment.payChannel === resolvedInitialPayChannel) return;
paymentDispatch({
type: "PaymentPayChannelChanged",
payChannel: resolvedInitialPayChannel,
});
}, [
canChoosePaymentMethod,
initialPayChannel,
isPaymentBusy,
payment.payChannel,
paymentDispatch,
resolvedInitialPayChannel,
]);
useEffect(() => { useEffect(() => {
const defaultPlanId = getDefaultSubscriptionPlanId({ const defaultPlanId = getDefaultSubscriptionPlanId({
canSubscribeVip, canSubscribeVip,
@@ -171,10 +213,12 @@ export function SubscriptionScreen({
/> />
</div> </div>
{canChoosePaymentMethod ? (
<section className={styles.paymentMethodSlot}> <section className={styles.paymentMethodSlot}>
<SubscriptionPaymentMethod <SubscriptionPaymentMethod
value={payment.payChannel} value={payment.payChannel}
disabled={isPaymentBusy} disabled={isPaymentBusy}
caption="Ezpay by default in the Philippines"
onChange={(payChannel) => { onChange={(payChannel) => {
paymentDispatch({ paymentDispatch({
type: "PaymentPayChannelChanged", type: "PaymentPayChannelChanged",
@@ -183,6 +227,7 @@ export function SubscriptionScreen({
}} }}
/> />
</section> </section>
) : null}
<div className={styles.ctaSlot}> <div className={styles.ctaSlot}>
<SubscriptionCheckoutButton <SubscriptionCheckoutButton