67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
/**
|
|
* Payment 状态机:Actors(异步服务)
|
|
*/
|
|
import { fromPromise } from "xstate";
|
|
|
|
import {
|
|
CreatePaymentOrderResponse,
|
|
PayChannel,
|
|
PaymentOrderStatusResponse,
|
|
PaymentPlansResponse,
|
|
} from "@/data/dto/payment";
|
|
import { getPaymentRepository } from "@/data/repositories/payment_repository";
|
|
import { Result } from "@/utils/result";
|
|
|
|
import type { PaymentPlanCatalog } from "./payment-state";
|
|
|
|
export const loadCachedPaymentPlansActor =
|
|
fromPromise<
|
|
PaymentPlansResponse | null,
|
|
{ catalog: PaymentPlanCatalog }
|
|
>(async ({ input }) => {
|
|
if (input.catalog === "tip") return null;
|
|
const paymentRepo = getPaymentRepository();
|
|
const result = await paymentRepo.getCachedPlans();
|
|
if (Result.isErr(result)) throw result.error;
|
|
return result.data;
|
|
});
|
|
|
|
export const refreshPaymentPlansActor = fromPromise<
|
|
PaymentPlansResponse,
|
|
{ catalog: PaymentPlanCatalog }
|
|
>(
|
|
async ({ input }) => {
|
|
const paymentRepo = getPaymentRepository();
|
|
const result =
|
|
input.catalog === "tip"
|
|
? await paymentRepo.getTipPlans()
|
|
: await paymentRepo.getPlans();
|
|
if (Result.isErr(result)) throw result.error;
|
|
return result.data;
|
|
},
|
|
);
|
|
|
|
export const createPaymentOrderActor = fromPromise<
|
|
CreatePaymentOrderResponse,
|
|
{ planId: string; payChannel: PayChannel; autoRenew: boolean }
|
|
>(async ({ input }) => {
|
|
const paymentRepo = getPaymentRepository();
|
|
const result = await paymentRepo.createOrder(
|
|
input.planId,
|
|
input.payChannel,
|
|
input.autoRenew,
|
|
);
|
|
if (Result.isErr(result)) throw result.error;
|
|
return result.data;
|
|
});
|
|
|
|
export const pollPaymentOrderStatusActor = fromPromise<
|
|
PaymentOrderStatusResponse,
|
|
{ orderId: string }
|
|
>(async ({ input }) => {
|
|
const paymentRepo = getPaymentRepository();
|
|
const result = await paymentRepo.getOrderStatus(input.orderId);
|
|
if (Result.isErr(result)) throw result.error;
|
|
return result.data;
|
|
});
|