306 lines
10 KiB
TypeScript
306 lines
10 KiB
TypeScript
/**
|
||
* Payment 状态机(XState v5)
|
||
*/
|
||
import { assign, setup } from "xstate";
|
||
|
||
import type { PaymentPlan } from "@/data/dto/payment";
|
||
|
||
import type { PaymentEvent } from "./payment-events";
|
||
import { initialState, type PaymentState } from "./payment-state";
|
||
import {
|
||
createPaymentOrderActor,
|
||
loadPaymentPlansActor,
|
||
loadPaymentVipStatusActor,
|
||
pollPaymentOrderStatusActor,
|
||
} from "./payment-machine.actors";
|
||
|
||
export type { PaymentEvent } from "./payment-events";
|
||
export type { PaymentState } from "./payment-state";
|
||
export { initialState } from "./payment-state";
|
||
|
||
const POLL_DELAY_MS = 4000;
|
||
|
||
function toErrorMessage(error: unknown): string {
|
||
return error instanceof Error ? error.message : String(error);
|
||
}
|
||
|
||
function defaultAutoRenewForPlan(planId: string): boolean {
|
||
return !planId.includes("lifetime") && !planId.startsWith("dol_");
|
||
}
|
||
|
||
function getDefaultPlanId(plans: readonly PaymentPlan[]): string {
|
||
return (
|
||
plans.find((plan) => plan.planId === "vip_monthly")?.planId ??
|
||
plans.find((plan) => plan.orderType.startsWith("vip_"))?.planId ??
|
||
plans[0]?.planId ??
|
||
""
|
||
);
|
||
}
|
||
|
||
function resetOrderState() {
|
||
return {
|
||
currentOrderId: null,
|
||
payParams: null,
|
||
orderStatus: null,
|
||
errorMessage: null,
|
||
} satisfies Partial<PaymentState>;
|
||
}
|
||
|
||
export const paymentMachine = setup({
|
||
types: {
|
||
context: {} as PaymentState,
|
||
events: {} as PaymentEvent,
|
||
},
|
||
actors: {
|
||
loadPlans: loadPaymentPlansActor,
|
||
createOrder: createPaymentOrderActor,
|
||
pollOrderStatus: pollPaymentOrderStatusActor,
|
||
loadVipStatus: loadPaymentVipStatusActor,
|
||
},
|
||
}).createMachine({
|
||
/** @xstate-layout N4IgpgJg5mDOIC5QAcCGBPAtmAdgFwGIAFDbfAGVQFccBjACwDFUBLAG0gG0AGAXURQB7WCzwtBOASAA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cVdZsPMmdFAJBHHaC4sL3JskMNGdp05fx43kiqWEgoER+r5YUMotKEiVHtrsQko5PKlAyjGfsmF7z52XBm42GhqQMm7L2TkVhpyslHGUWMw5BLlBPrkVk3S6YAxrc3fusKeE5DiZ61Mzk0hEhzmkct15eaUT4S0UeQ6gl2vkP1VON49BulJGYG6R7L7XwFOQm1m7-UIE0HhOG84Bx7quE6xAMQXyjp0e+bQPZuljOKYo2Yw62z9xKPUW95hEnfkaQUfu762naLiKyDInyIBAa2jeF8v9rzfmMAJHddIeylAATZKSJI5UYrQ2qvsRIew7JqIlKoQs70dqSCfbC34CymusEAA */
|
||
id: "payment",
|
||
initial: "idle",
|
||
context: initialState,
|
||
states: {
|
||
idle: {
|
||
on: {
|
||
PaymentInit: "loadingPlans",
|
||
PaymentRefreshVipStatus: "checkingVipStatus",
|
||
},
|
||
},
|
||
|
||
loadingPlans: {
|
||
invoke: {
|
||
src: "loadPlans",
|
||
onDone: {
|
||
target: "ready",
|
||
actions: assign(({ context, event }) => {
|
||
const plans = event.output.plans;
|
||
const selectedPlanId =
|
||
context.selectedPlanId || getDefaultPlanId(plans);
|
||
return {
|
||
plans,
|
||
selectedPlanId,
|
||
autoRenew: defaultAutoRenewForPlan(selectedPlanId),
|
||
errorMessage: null,
|
||
};
|
||
}),
|
||
},
|
||
onError: {
|
||
target: "ready",
|
||
actions: assign(({ event }) => ({
|
||
errorMessage: toErrorMessage(event.error),
|
||
})),
|
||
},
|
||
},
|
||
},
|
||
|
||
ready: {
|
||
on: {
|
||
PaymentInit: "loadingPlans",
|
||
PaymentPlanSelected: {
|
||
actions: assign(({ event }) => ({
|
||
...resetOrderState(),
|
||
selectedPlanId: event.planId,
|
||
autoRenew: defaultAutoRenewForPlan(event.planId),
|
||
})),
|
||
},
|
||
PaymentPayChannelChanged: {
|
||
actions: assign(({ event }) => ({
|
||
payChannel: event.payChannel,
|
||
errorMessage: null,
|
||
})),
|
||
},
|
||
PaymentAutoRenewChanged: {
|
||
actions: assign(({ event }) => ({
|
||
autoRenew: event.autoRenew,
|
||
errorMessage: null,
|
||
})),
|
||
},
|
||
PaymentAgreementChanged: {
|
||
actions: assign(({ event }) => ({
|
||
agreed: event.agreed,
|
||
errorMessage: null,
|
||
})),
|
||
},
|
||
PaymentCreateOrderSubmitted: [
|
||
{
|
||
guard: ({ context }) =>
|
||
context.agreed && context.selectedPlanId.length > 0,
|
||
target: "creatingOrder",
|
||
},
|
||
{
|
||
actions: assign({
|
||
errorMessage:
|
||
"Choose a plan and accept the agreement before continuing.",
|
||
}),
|
||
},
|
||
],
|
||
PaymentRefreshVipStatus: "checkingVipStatus",
|
||
PaymentErrorCleared: {
|
||
actions: assign({ errorMessage: null }),
|
||
},
|
||
},
|
||
},
|
||
|
||
creatingOrder: {
|
||
invoke: {
|
||
src: "createOrder",
|
||
input: ({ context }) => ({
|
||
planId: context.selectedPlanId,
|
||
payChannel: context.payChannel,
|
||
autoRenew: context.autoRenew,
|
||
}),
|
||
onDone: {
|
||
target: "pollingOrder",
|
||
actions: assign(({ context, event }) => ({
|
||
currentOrderId: event.output.orderId,
|
||
payParams: event.output.payParams,
|
||
orderStatus: "pending",
|
||
errorMessage: null,
|
||
launchNonce: context.launchNonce + 1,
|
||
})),
|
||
},
|
||
onError: {
|
||
target: "failed",
|
||
actions: assign(({ event }) => ({
|
||
errorMessage: toErrorMessage(event.error),
|
||
})),
|
||
},
|
||
},
|
||
},
|
||
|
||
pollingOrder: {
|
||
invoke: {
|
||
src: "pollOrderStatus",
|
||
input: ({ context }) => ({
|
||
orderId: context.currentOrderId ?? "",
|
||
}),
|
||
onDone: [
|
||
{
|
||
guard: ({ event }) => event.output.status === "paid",
|
||
target: "checkingVipStatus",
|
||
actions: assign(({ event }) => ({
|
||
orderStatus: event.output.status,
|
||
errorMessage: null,
|
||
})),
|
||
},
|
||
{
|
||
guard: ({ event }) => event.output.status === "failed",
|
||
target: "failed",
|
||
actions: assign(({ event }) => ({
|
||
orderStatus: event.output.status,
|
||
errorMessage: "Payment failed or was cancelled.",
|
||
})),
|
||
},
|
||
{
|
||
target: "waitingForPayment",
|
||
actions: assign(({ event }) => ({
|
||
orderStatus: event.output.status,
|
||
errorMessage: null,
|
||
})),
|
||
},
|
||
],
|
||
onError: {
|
||
target: "failed",
|
||
actions: assign(({ event }) => ({
|
||
errorMessage: toErrorMessage(event.error),
|
||
})),
|
||
},
|
||
},
|
||
},
|
||
|
||
waitingForPayment: {
|
||
after: {
|
||
[POLL_DELAY_MS]: "pollingOrder",
|
||
},
|
||
on: {
|
||
PaymentReset: {
|
||
target: "ready",
|
||
actions: assign(resetOrderState),
|
||
},
|
||
},
|
||
},
|
||
|
||
checkingVipStatus: {
|
||
invoke: {
|
||
src: "loadVipStatus",
|
||
onDone: [
|
||
{
|
||
guard: ({ context }) => context.orderStatus === "paid",
|
||
target: "paid",
|
||
actions: assign(({ event }) => ({
|
||
vipStatus: event.output,
|
||
errorMessage: null,
|
||
})),
|
||
},
|
||
{
|
||
target: "ready",
|
||
actions: assign(({ event }) => ({
|
||
vipStatus: event.output,
|
||
errorMessage: null,
|
||
})),
|
||
},
|
||
],
|
||
onError: [
|
||
{
|
||
guard: ({ context }) => context.orderStatus === "paid",
|
||
target: "paid",
|
||
actions: assign(({ event }) => ({
|
||
errorMessage: toErrorMessage(event.error),
|
||
})),
|
||
},
|
||
{
|
||
target: "ready",
|
||
actions: assign(({ event }) => ({
|
||
errorMessage: toErrorMessage(event.error),
|
||
})),
|
||
},
|
||
],
|
||
},
|
||
},
|
||
|
||
paid: {
|
||
on: {
|
||
PaymentReset: {
|
||
target: "ready",
|
||
actions: assign(resetOrderState),
|
||
},
|
||
PaymentRefreshVipStatus: "checkingVipStatus",
|
||
},
|
||
},
|
||
|
||
failed: {
|
||
on: {
|
||
PaymentCreateOrderSubmitted: [
|
||
{
|
||
guard: ({ context }) =>
|
||
context.agreed && context.selectedPlanId.length > 0,
|
||
target: "creatingOrder",
|
||
actions: assign(resetOrderState),
|
||
},
|
||
],
|
||
PaymentErrorCleared: {
|
||
target: "ready",
|
||
actions: assign({ errorMessage: null }),
|
||
},
|
||
PaymentReset: {
|
||
target: "ready",
|
||
actions: assign(resetOrderState),
|
||
},
|
||
},
|
||
},
|
||
},
|
||
on: {
|
||
PaymentLaunchFailed: {
|
||
target: ".failed",
|
||
actions: assign(({ event }) => ({
|
||
errorMessage: event.errorMessage,
|
||
})),
|
||
},
|
||
},
|
||
});
|
||
|
||
export type PaymentMachine = typeof paymentMachine;
|