feat(data): add paywall payment APIs
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* 创建支付订单请求
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
export const PayChannelSchema = z.enum(["stripe", "ezpay"]);
|
||||
|
||||
export const CreatePaymentOrderRequestSchema = z.object({
|
||||
planId: z.string(),
|
||||
payChannel: PayChannelSchema,
|
||||
autoRenew: z.boolean(),
|
||||
});
|
||||
|
||||
export type PayChannel = z.output<typeof PayChannelSchema>;
|
||||
export type CreatePaymentOrderRequestInput = z.input<
|
||||
typeof CreatePaymentOrderRequestSchema
|
||||
>;
|
||||
export type CreatePaymentOrderRequestData = z.output<
|
||||
typeof CreatePaymentOrderRequestSchema
|
||||
>;
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* 创建支付订单响应
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
export const CreatePaymentOrderResponseSchema = z.object({
|
||||
orderId: z.string(),
|
||||
payParams: z.record(z.string(), z.unknown()),
|
||||
});
|
||||
|
||||
export type CreatePaymentOrderResponseInput = z.input<
|
||||
typeof CreatePaymentOrderResponseSchema
|
||||
>;
|
||||
export type CreatePaymentOrderResponseData = z.output<
|
||||
typeof CreatePaymentOrderResponseSchema
|
||||
>;
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @file Payment schema barrel.
|
||||
*/
|
||||
|
||||
export * from "./create_payment_order_request";
|
||||
export * from "./create_payment_order_response";
|
||||
export * from "./payment_order_status_response";
|
||||
export * from "./payment_plan";
|
||||
export * from "./payment_plans_response";
|
||||
export * from "./payment_vip_status_response";
|
||||
export * from "./payment_websocket_event";
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 支付订单状态响应
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
export const PaymentOrderStatusSchema = z.enum(["pending", "paid", "failed"]);
|
||||
|
||||
export const PaymentOrderStatusResponseSchema = z.object({
|
||||
orderId: z.string(),
|
||||
status: PaymentOrderStatusSchema,
|
||||
orderType: z.string(),
|
||||
planId: z.string(),
|
||||
});
|
||||
|
||||
export type PaymentOrderStatus = z.output<typeof PaymentOrderStatusSchema>;
|
||||
export type PaymentOrderStatusResponseInput = z.input<
|
||||
typeof PaymentOrderStatusResponseSchema
|
||||
>;
|
||||
export type PaymentOrderStatusResponseData = z.output<
|
||||
typeof PaymentOrderStatusResponseSchema
|
||||
>;
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 支付套餐
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
const PaymentPlanWireSchema = z.object({
|
||||
plan_id: z.string(),
|
||||
plan_name: z.string(),
|
||||
order_type: z.string(),
|
||||
amount_cents: z.number(),
|
||||
currency: z.string(),
|
||||
vip_days: z.number().nullable(),
|
||||
dol_amount: z.number().nullable(),
|
||||
});
|
||||
|
||||
export const PaymentPlanSchema = z
|
||||
.preprocess((value) => {
|
||||
if (!value || typeof value !== "object") return value;
|
||||
const data = value as Record<string, unknown>;
|
||||
if ("planId" in data) {
|
||||
return {
|
||||
plan_id: data.planId,
|
||||
plan_name: data.planName,
|
||||
order_type: data.orderType,
|
||||
amount_cents: data.amountCents,
|
||||
currency: data.currency,
|
||||
vip_days: data.vipDays,
|
||||
dol_amount: data.dolAmount,
|
||||
};
|
||||
}
|
||||
return value;
|
||||
}, PaymentPlanWireSchema)
|
||||
.transform((data) => ({
|
||||
planId: data.plan_id,
|
||||
planName: data.plan_name,
|
||||
orderType: data.order_type,
|
||||
amountCents: data.amount_cents,
|
||||
currency: data.currency,
|
||||
vipDays: data.vip_days,
|
||||
dolAmount: data.dol_amount,
|
||||
}));
|
||||
|
||||
export type PaymentPlanInput = z.input<typeof PaymentPlanSchema>;
|
||||
export type PaymentPlanData = z.output<typeof PaymentPlanSchema>;
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 支付套餐列表响应
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
import { PaymentPlanSchema } from "./payment_plan";
|
||||
|
||||
export const PaymentPlansResponseSchema = z.object({
|
||||
plans: z.array(PaymentPlanSchema),
|
||||
});
|
||||
|
||||
export type PaymentPlansResponseInput = z.input<
|
||||
typeof PaymentPlansResponseSchema
|
||||
>;
|
||||
export type PaymentPlansResponseData = z.output<
|
||||
typeof PaymentPlansResponseSchema
|
||||
>;
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* VIP 状态响应
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
export const PaymentVipStatusResponseSchema = z.object({
|
||||
isVip: z.boolean(),
|
||||
vipExpiresAt: z.string().nullable(),
|
||||
});
|
||||
|
||||
export type PaymentVipStatusResponseInput = z.input<
|
||||
typeof PaymentVipStatusResponseSchema
|
||||
>;
|
||||
export type PaymentVipStatusResponseData = z.output<
|
||||
typeof PaymentVipStatusResponseSchema
|
||||
>;
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 支付 WebSocket 事件
|
||||
*/
|
||||
import { z } from "zod";
|
||||
|
||||
export const PaymentSuccessVipEventSchema = z.object({
|
||||
type: z.literal("payment_success"),
|
||||
orderId: z.string(),
|
||||
payType: z.literal("vip"),
|
||||
planName: z.string(),
|
||||
vipExpiresAt: z.string().nullable(),
|
||||
});
|
||||
|
||||
export const PaymentSuccessDolEventSchema = z.object({
|
||||
type: z.literal("payment_success"),
|
||||
orderId: z.string(),
|
||||
payType: z.literal("dol"),
|
||||
planName: z.string(),
|
||||
dolAmount: z.number(),
|
||||
dolBalance: z.number(),
|
||||
});
|
||||
|
||||
export const PaymentFailedEventSchema = z.object({
|
||||
type: z.literal("payment_failed"),
|
||||
orderId: z.string(),
|
||||
info: z.string(),
|
||||
});
|
||||
|
||||
export const PaymentSuccessEventSchema = z.discriminatedUnion("payType", [
|
||||
PaymentSuccessVipEventSchema,
|
||||
PaymentSuccessDolEventSchema,
|
||||
]);
|
||||
|
||||
export const PaymentWebSocketEventSchema = z.union([
|
||||
PaymentSuccessEventSchema,
|
||||
PaymentFailedEventSchema,
|
||||
]);
|
||||
|
||||
export type PaymentSuccessVipEventData = z.output<
|
||||
typeof PaymentSuccessVipEventSchema
|
||||
>;
|
||||
export type PaymentSuccessDolEventData = z.output<
|
||||
typeof PaymentSuccessDolEventSchema
|
||||
>;
|
||||
export type PaymentFailedEventData = z.output<typeof PaymentFailedEventSchema>;
|
||||
export type PaymentSuccessEventData = z.output<typeof PaymentSuccessEventSchema>;
|
||||
export type PaymentWebSocketEventInput = z.input<
|
||||
typeof PaymentWebSocketEventSchema
|
||||
>;
|
||||
export type PaymentWebSocketEventData = z.output<
|
||||
typeof PaymentWebSocketEventSchema
|
||||
>;
|
||||
Reference in New Issue
Block a user