feat(payment): implement coffee tip plans and update payment flow

This commit is contained in:
2026-07-14 11:48:46 +08:00
parent f9c15bd91f
commit 3a4f24cb06
30 changed files with 632 additions and 56 deletions
@@ -0,0 +1,51 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const httpClientMock = vi.hoisted(() => vi.fn());
vi.mock("../http_client", () => ({
httpClient: httpClientMock,
}));
import { PaymentApi } from "../payment_api";
describe("PaymentApi", () => {
beforeEach(() => {
httpClientMock.mockReset();
});
it("loads public coffee tip plans from the dedicated endpoint", async () => {
httpClientMock.mockResolvedValue({
success: true,
data: {
plans: [
{
planId: "tip_coffee_usd_19_99",
planName: "Large Coffee",
orderType: "tip",
tipType: "coffee_large",
description: "Buy Elio a large coffee",
amountCents: 1999,
currency: "USD",
autoRenew: false,
isFirstRechargeOffer: false,
firstRechargeDiscountPercent: 0,
},
],
},
});
const response = await new PaymentApi().getTipPlans();
expect(httpClientMock).toHaveBeenCalledWith("/api/payment/tip-plans");
expect(response.toJson()).toEqual({
plans: [
{
planId: "tip_coffee_usd_19_99",
planName: "Large Coffee",
amountCents: 1999,
currency: "USD",
},
],
});
});
});
+3
View File
@@ -67,6 +67,9 @@ export class ApiPath {
/** 获取商品套餐列表 */
static readonly paymentPlans = `${ApiPath._payment}/plans`;
/** 获取咖啡打赏套餐列表 */
static readonly paymentTipPlans = `${ApiPath._payment}/tip-plans`;
// ============ 聊天相关 ============
/** 发送消息 */
static readonly chatSend = `${ApiPath._chat}/send`;
+11
View File
@@ -8,6 +8,7 @@ import {
CreatePaymentOrderResponse,
PaymentOrderStatusResponse,
PaymentPlansResponse,
TipPaymentPlansResponse,
} from "@/data/dto/payment";
import { ApiPath } from "./api_path";
@@ -25,6 +26,16 @@ export class PaymentApi {
);
}
/** 获取咖啡打赏套餐列表。 */
async getTipPlans(): Promise<TipPaymentPlansResponse> {
const env = await httpClient<ApiEnvelope<unknown>>(
ApiPath.paymentTipPlans,
);
return TipPaymentPlansResponse.fromJson(
unwrap(env) as Record<string, unknown>,
);
}
/**
* 创建支付订单
*/