feat(subscription): default pay channel by country
This commit is contained in:
@@ -4,6 +4,7 @@ import { PaymentPlan } from "@/data/dto/payment";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
findSelectedSubscriptionPlan,
|
findSelectedSubscriptionPlan,
|
||||||
|
getDefaultPayChannelForCountryCode,
|
||||||
getDefaultSubscriptionPlanId,
|
getDefaultSubscriptionPlanId,
|
||||||
toCoinsOfferPlanViews,
|
toCoinsOfferPlanViews,
|
||||||
toVipOfferPlanViews,
|
toVipOfferPlanViews,
|
||||||
@@ -28,6 +29,14 @@ function makePlan(
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("subscription screen helpers", () => {
|
describe("subscription screen helpers", () => {
|
||||||
|
it("defaults Philippines users to Ezpay and other countries to Stripe", () => {
|
||||||
|
expect(getDefaultPayChannelForCountryCode("PH")).toBe("ezpay");
|
||||||
|
expect(getDefaultPayChannelForCountryCode("ph")).toBe("ezpay");
|
||||||
|
expect(getDefaultPayChannelForCountryCode("HK")).toBe("stripe");
|
||||||
|
expect(getDefaultPayChannelForCountryCode("")).toBe("stripe");
|
||||||
|
expect(getDefaultPayChannelForCountryCode(null)).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" }),
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
import type { PaymentPlan } from "@/data/dto/payment";
|
import type { PayChannel, PaymentPlan } from "@/data/dto/payment";
|
||||||
|
|
||||||
import type { CoinsOfferPlanView } from "./components/subscription-coins-offer-section";
|
import type { CoinsOfferPlanView } from "./components/subscription-coins-offer-section";
|
||||||
import type { VipOfferPlanView } from "./components/subscription-vip-offer-section";
|
import type { VipOfferPlanView } from "./components/subscription-vip-offer-section";
|
||||||
|
|
||||||
export type SubscriptionType = "vip" | "topup";
|
export type SubscriptionType = "vip" | "topup";
|
||||||
|
|
||||||
|
export function getDefaultPayChannelForCountryCode(
|
||||||
|
countryCode: string | null | undefined,
|
||||||
|
): PayChannel {
|
||||||
|
return countryCode?.trim().toUpperCase() === "PH" ? "ezpay" : "stripe";
|
||||||
|
}
|
||||||
|
|
||||||
export function isVipPlan(plan: PaymentPlan): boolean {
|
export function isVipPlan(plan: PaymentPlan): boolean {
|
||||||
return plan.vipDays !== null;
|
return plan.vipDays !== null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
"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 { useUserState } from "@/stores/user/user-context";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
SubscriptionCheckoutButton,
|
SubscriptionCheckoutButton,
|
||||||
@@ -15,6 +16,7 @@ import {
|
|||||||
import styles from "./components/subscription-screen.module.css";
|
import styles from "./components/subscription-screen.module.css";
|
||||||
import {
|
import {
|
||||||
findSelectedSubscriptionPlan,
|
findSelectedSubscriptionPlan,
|
||||||
|
getDefaultPayChannelForCountryCode,
|
||||||
getDefaultSubscriptionPlanId,
|
getDefaultSubscriptionPlanId,
|
||||||
toCoinsOfferPlanViews,
|
toCoinsOfferPlanViews,
|
||||||
toVipOfferPlanViews,
|
toVipOfferPlanViews,
|
||||||
@@ -46,6 +48,8 @@ export function SubscriptionScreen({
|
|||||||
shouldResumePendingOrder,
|
shouldResumePendingOrder,
|
||||||
returnTo,
|
returnTo,
|
||||||
});
|
});
|
||||||
|
const user = useUserState();
|
||||||
|
const paymentMethodManuallySelectedRef = useRef(false);
|
||||||
const canSubscribeVip = subscriptionType === "vip";
|
const canSubscribeVip = subscriptionType === "vip";
|
||||||
|
|
||||||
const vipOfferPlans = useMemo(
|
const vipOfferPlans = useMemo(
|
||||||
@@ -96,6 +100,28 @@ export function SubscriptionScreen({
|
|||||||
vipOfferPlans,
|
vipOfferPlans,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (payment.status !== "ready") return;
|
||||||
|
if (paymentMethodManuallySelectedRef.current) return;
|
||||||
|
|
||||||
|
const countryCode = user.currentUser?.countryCode ?? "";
|
||||||
|
if (countryCode.trim().length === 0) return;
|
||||||
|
|
||||||
|
const defaultPayChannel =
|
||||||
|
getDefaultPayChannelForCountryCode(countryCode);
|
||||||
|
if (payment.payChannel === defaultPayChannel) return;
|
||||||
|
|
||||||
|
paymentDispatch({
|
||||||
|
type: "PaymentPayChannelChanged",
|
||||||
|
payChannel: defaultPayChannel,
|
||||||
|
});
|
||||||
|
}, [
|
||||||
|
payment.payChannel,
|
||||||
|
payment.status,
|
||||||
|
paymentDispatch,
|
||||||
|
user.currentUser?.countryCode,
|
||||||
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MobileShell>
|
<MobileShell>
|
||||||
<div className={styles.shell}>
|
<div className={styles.shell}>
|
||||||
@@ -136,12 +162,13 @@ export function SubscriptionScreen({
|
|||||||
<SubscriptionPaymentMethod
|
<SubscriptionPaymentMethod
|
||||||
value={payment.payChannel}
|
value={payment.payChannel}
|
||||||
disabled={isPaymentBusy}
|
disabled={isPaymentBusy}
|
||||||
onChange={(payChannel) =>
|
onChange={(payChannel) => {
|
||||||
|
paymentMethodManuallySelectedRef.current = true;
|
||||||
paymentDispatch({
|
paymentDispatch({
|
||||||
type: "PaymentPayChannelChanged",
|
type: "PaymentPayChannelChanged",
|
||||||
payChannel,
|
payChannel,
|
||||||
})
|
});
|
||||||
}
|
}}
|
||||||
/>
|
/>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ describe("User", () => {
|
|||||||
username: "guest_bbc90a38",
|
username: "guest_bbc90a38",
|
||||||
email: "device_bbc90a38b996187348f54cf2cb0b789e@guest.local",
|
email: "device_bbc90a38b996187348f54cf2cb0b789e@guest.local",
|
||||||
platform: "web",
|
platform: "web",
|
||||||
|
country: "Hong Kong",
|
||||||
|
countryCode: "HK",
|
||||||
intimacy: 0,
|
intimacy: 0,
|
||||||
dolBalance: 0,
|
dolBalance: 0,
|
||||||
personalityTraits: {
|
personalityTraits: {
|
||||||
@@ -29,6 +31,8 @@ describe("User", () => {
|
|||||||
username: "guest_bbc90a38",
|
username: "guest_bbc90a38",
|
||||||
email: "device_bbc90a38b996187348f54cf2cb0b789e@guest.local",
|
email: "device_bbc90a38b996187348f54cf2cb0b789e@guest.local",
|
||||||
platform: "web",
|
platform: "web",
|
||||||
|
country: "Hong Kong",
|
||||||
|
countryCode: "HK",
|
||||||
intimacy: 0,
|
intimacy: 0,
|
||||||
dolBalance: 0,
|
dolBalance: 0,
|
||||||
personalityTraits: {
|
personalityTraits: {
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ export class User {
|
|||||||
declare readonly username: string;
|
declare readonly username: string;
|
||||||
declare readonly email: string;
|
declare readonly email: string;
|
||||||
declare readonly platform: string;
|
declare readonly platform: string;
|
||||||
|
declare readonly country: string;
|
||||||
|
declare readonly countryCode: string;
|
||||||
declare readonly intimacy: number;
|
declare readonly intimacy: number;
|
||||||
declare readonly dolBalance: number;
|
declare readonly dolBalance: number;
|
||||||
declare readonly personalityTraits: PersonalityTraits;
|
declare readonly personalityTraits: PersonalityTraits;
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ export const UserViewSchema = z.object({
|
|||||||
id: z.string(),
|
id: z.string(),
|
||||||
username: z.string(),
|
username: z.string(),
|
||||||
email: z.string(),
|
email: z.string(),
|
||||||
|
country: z.string(),
|
||||||
|
countryCode: z.string(),
|
||||||
avatarUrl: z.string(),
|
avatarUrl: z.string(),
|
||||||
intimacy: z.number(),
|
intimacy: z.number(),
|
||||||
dolBalance: z.number(),
|
dolBalance: z.number(),
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ export const UserSchema = z.object({
|
|||||||
username: stringOrEmpty,
|
username: stringOrEmpty,
|
||||||
email: stringOrEmpty,
|
email: stringOrEmpty,
|
||||||
platform: stringOrEmpty,
|
platform: stringOrEmpty,
|
||||||
|
country: stringOrEmpty,
|
||||||
|
countryCode: stringOrEmpty,
|
||||||
intimacy: numberOrZero,
|
intimacy: numberOrZero,
|
||||||
dolBalance: numberOrZero,
|
dolBalance: numberOrZero,
|
||||||
personalityTraits: PersonalityTraitsSchema.default(PERSONALITY_TRAITS_DEFAULTS),
|
personalityTraits: PersonalityTraitsSchema.default(PERSONALITY_TRAITS_DEFAULTS),
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ function makeUser(overrides: Partial<UserView> = {}): UserView {
|
|||||||
id: "user-1",
|
id: "user-1",
|
||||||
username: "Elio Fan",
|
username: "Elio Fan",
|
||||||
email: "user@example.com",
|
email: "user@example.com",
|
||||||
|
country: "Hong Kong",
|
||||||
|
countryCode: "HK",
|
||||||
avatarUrl: "https://example.com/avatar.png",
|
avatarUrl: "https://example.com/avatar.png",
|
||||||
intimacy: 42,
|
intimacy: 42,
|
||||||
dolBalance: 7,
|
dolBalance: 7,
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ export function toView(u: {
|
|||||||
id: string;
|
id: string;
|
||||||
username: string;
|
username: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
|
country?: string;
|
||||||
|
countryCode?: string;
|
||||||
avatarUrl?: string;
|
avatarUrl?: string;
|
||||||
intimacy?: number;
|
intimacy?: number;
|
||||||
dolBalance?: number;
|
dolBalance?: number;
|
||||||
@@ -74,6 +76,8 @@ export function toView(u: {
|
|||||||
id: u.id,
|
id: u.id,
|
||||||
username: u.username,
|
username: u.username,
|
||||||
email: u.email ?? "",
|
email: u.email ?? "",
|
||||||
|
country: u.country ?? "",
|
||||||
|
countryCode: u.countryCode ?? "",
|
||||||
avatarUrl: u.avatarUrl ?? "",
|
avatarUrl: u.avatarUrl ?? "",
|
||||||
intimacy: u.intimacy ?? 0,
|
intimacy: u.intimacy ?? 0,
|
||||||
dolBalance: u.dolBalance ?? creditBalance,
|
dolBalance: u.dolBalance ?? creditBalance,
|
||||||
|
|||||||
Reference in New Issue
Block a user