@tbox.cn/app-module-ai-payment 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,131 @@
1
+ import * as _tbox_cn_app_sdk_server from '@tbox.cn/app-sdk-server';
2
+ import * as _tbox_cn_app_contracts from '@tbox.cn/app-contracts';
3
+ import { RequestContext, CardActionResult } from '@tbox.cn/app-contracts';
4
+ import * as zod from 'zod';
5
+ import { z } from 'zod';
6
+ import * as _tbox_cn_app_agent_sdk_server from '@tbox.cn/app-agent-sdk-server';
7
+ import { ServerContext } from '@tbox.cn/app-agent-sdk-server';
8
+ import { MallScope } from '@tbox.cn/app-contracts-mall';
9
+ import * as _mastra_core_tools from '@mastra/core/tools';
10
+
11
+ /** 支付卡数据与历史支付 Port 兼容类型。当前体验链路经容器点击创单。 */
12
+
13
+ /** 支付域 DTO(AI 支付 Demo 语义:tradeStatus 三态移植,保持支付侧权威词汇)。 */
14
+ type PaymentTradeStatus = 'WAIT_BUYER_PAY' | 'TRADE_SUCCESS' | 'TRADE_CLOSED';
15
+ interface PaymentQueryContext {
16
+ readonly scope: MallScope;
17
+ readonly subjectId?: string;
18
+ readonly requestContext: RequestContext;
19
+ }
20
+ interface PaymentPort {
21
+ /**
22
+ * 买家身份取 q.requestContext.identity.userId(可信上下文——身份不经模型传参)。
23
+ * bizContext(可选透传):AI 付场景 my.tradePay 拉起上下文({cashierScene:'agentPay',
24
+ * agentPid, agentId} 的 JSON 字符串,仅 ai 付内使用)——由 provider 依实例 config 组装的
25
+ * 不透明字符串,模块/卡面零解析透传到客户端 tradePay 入参(Code security:拉起上下文
26
+ * 服务端合成物,客户端不在安全决策点消费)。
27
+ */
28
+ createOrder(q: PaymentQueryContext, detail?: {
29
+ remark?: string;
30
+ }): Promise<{
31
+ tradeNo: string;
32
+ bizContext?: string;
33
+ }>;
34
+ queryOrder(q: PaymentQueryContext, tradeNo: string): Promise<{
35
+ tradeNo: string;
36
+ status: PaymentTradeStatus;
37
+ totalAmountText?: string;
38
+ subCode?: string;
39
+ subMsg?: string;
40
+ }>;
41
+ }
42
+
43
+ /**
44
+ * payment.main 槽解析(请求级;register 只注册不解析——铁律)。模块零外呼(铁律):
45
+ * 唯一出口 = integrations registry 槽位解析。支付无 InMemory 降级形态(资损初始化路径),
46
+ * 预期缺席转为 PaymentNotConfiguredError 上抛,由工具层收口为可读文案;其余错误上抛
47
+ * (配置即声明)。
48
+ */
49
+
50
+ declare class PaymentNotConfiguredError extends Error {
51
+ constructor(message: string);
52
+ }
53
+ declare function resolvePaymentPort(reqCtx: RequestContext, ctx: ServerContext): Promise<PaymentPort>;
54
+
55
+ declare function loadPaymentConfig(options?: {
56
+ env?: NodeJS.ProcessEnv;
57
+ appDir?: string;
58
+ }): Promise<{
59
+ bizContext?: string | undefined;
60
+ }>;
61
+
62
+ declare function createAiPaymentTools(options?: {
63
+ loadConfig?: typeof loadPaymentConfig;
64
+ }): {
65
+ readonly createPaymentOrder: _tbox_cn_app_agent_sdk_server.Tool<{}, unknown, unknown, unknown, _mastra_core_tools.ToolExecutionContext<unknown, unknown, unknown>, "createPaymentOrder", unknown>;
66
+ };
67
+ type AiPaymentTools = ReturnType<typeof createAiPaymentTools>;
68
+
69
+ type CallbackInput = {
70
+ status: 'success' | 'cancel' | 'failure' | 'unavailable';
71
+ paymentRef?: string;
72
+ };
73
+ declare function createAiPaymentActions(): {
74
+ readonly 'ai-payment.report-payment-result': {
75
+ actionId: string;
76
+ moduleId: string;
77
+ cardType: string;
78
+ label: string;
79
+ singleUse: boolean;
80
+ hidden: boolean;
81
+ kind: "moduleAction";
82
+ inputSchema: z.ZodObject<{
83
+ status: z.ZodEnum<["success", "cancel", "failure", "unavailable"]>;
84
+ paymentRef: z.ZodOptional<z.ZodString>;
85
+ }, "strip", z.ZodTypeAny, {
86
+ status: "success" | "cancel" | "failure" | "unavailable";
87
+ paymentRef?: string | undefined;
88
+ }, {
89
+ status: "success" | "cancel" | "failure" | "unavailable";
90
+ paymentRef?: string | undefined;
91
+ }>;
92
+ execute(input: CallbackInput, _reqCtx: RequestContext): Promise<CardActionResult>;
93
+ };
94
+ };
95
+
96
+ declare const serverModule: {
97
+ declaration: {
98
+ moduleId: string;
99
+ };
100
+ cards: {
101
+ readonly 'payment-order': _tbox_cn_app_contracts.CardMeta<zod.ZodObject<{
102
+ tradeNo: zod.ZodOptional<zod.ZodString>;
103
+ buyerId: zod.ZodOptional<zod.ZodString>;
104
+ subject: zod.ZodString;
105
+ totalAmountText: zod.ZodString;
106
+ payState: zod.ZodEnum<["created", "paying", "paid", "cancelled", "failed"]>;
107
+ bizContext: zod.ZodOptional<zod.ZodString>;
108
+ }, "strip", zod.ZodTypeAny, {
109
+ subject: string;
110
+ totalAmountText: string;
111
+ payState: "failed" | "created" | "cancelled" | "paying" | "paid";
112
+ tradeNo?: string | undefined;
113
+ buyerId?: string | undefined;
114
+ bizContext?: string | undefined;
115
+ }, {
116
+ subject: string;
117
+ totalAmountText: string;
118
+ payState: "failed" | "created" | "cancelled" | "paying" | "paid";
119
+ tradeNo?: string | undefined;
120
+ buyerId?: string | undefined;
121
+ bizContext?: string | undefined;
122
+ }>>;
123
+ };
124
+ skills: {
125
+ [k: string]: _tbox_cn_app_sdk_server.LocalSkill;
126
+ };
127
+ preTools: {};
128
+ register(ctx: ServerContext): void;
129
+ };
130
+
131
+ export { type AiPaymentTools, PaymentNotConfiguredError, createAiPaymentActions, createAiPaymentTools, serverModule as default, resolvePaymentPort, serverModule };
@@ -0,0 +1,16 @@
1
+ import {
2
+ PaymentNotConfiguredError,
3
+ createAiPaymentActions,
4
+ createAiPaymentTools,
5
+ resolvePaymentPort,
6
+ serverModule,
7
+ server_default
8
+ } from "../chunk-ERHTVWF5.js";
9
+ export {
10
+ PaymentNotConfiguredError,
11
+ createAiPaymentActions,
12
+ createAiPaymentTools,
13
+ server_default as default,
14
+ resolvePaymentPort,
15
+ serverModule
16
+ };
package/package.json CHANGED
@@ -1,6 +1,69 @@
1
1
  {
2
2
  "name": "@tbox.cn/app-module-ai-payment",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "description": "AI 支付体验:Web 模拟收银台与端内云容器创单",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ },
11
+ "./server": {
12
+ "types": "./dist/server/index.d.ts",
13
+ "import": "./dist/server/index.js"
14
+ },
15
+ "./client": {
16
+ "types": "./dist/client/index.d.ts",
17
+ "import": "./dist/client/index.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "dependencies": {
22
+ "@tbox.cn/app-agent-sdk-client": "0.1.6",
23
+ "@tbox.cn/app-agent-sdk-server": "0.2.3",
24
+ "@tbox.cn/app-contracts": "0.13.0",
25
+ "@tbox.cn/app-contracts-mall": "0.13.0"
26
+ },
27
+ "peerDependencies": {
28
+ "zod": "3.25.76",
29
+ "react": "18.3.1",
30
+ "@phosphor-icons/react": "2.1.10"
31
+ },
32
+ "devDependencies": {
33
+ "@types/react": "18.3.31",
34
+ "@types/node": "24.13.3",
35
+ "typescript": "5.9.3",
36
+ "vitest": "4.1.10",
37
+ "zod": "3.25.76",
38
+ "tsup": "8.5.1",
39
+ "happy-dom": "20.11.2",
40
+ "react-dom": "18.3.1",
41
+ "@types/react-dom": "18.3.7"
42
+ },
43
+ "files": [
44
+ "dist",
45
+ "src",
46
+ "tests",
47
+ "tbox.module.json",
48
+ "tsconfig.json",
49
+ "tsup.config.ts",
50
+ "skills",
51
+ "README.md",
52
+ "INTEGRATION.md"
53
+ ],
54
+ "engines": {
55
+ "node": ">=20.0.0"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ },
60
+ "license": "MIT",
61
+ "sideEffects": [
62
+ "**/*.css"
63
+ ],
64
+ "scripts": {
65
+ "typecheck": "tsc --noEmit",
66
+ "test": "vitest run",
67
+ "build": "tsup"
68
+ }
6
69
  }
@@ -0,0 +1,15 @@
1
+ ---
2
+ name: ai-payment
3
+ description: AI 支付体验商品卡的展示与用户点击支付指引。
4
+ routingTerms:
5
+ - AI 支付
6
+ - AI 付
7
+ - 支付体验
8
+ - 测试购买
9
+ ---
10
+
11
+ # AI 支付体验
12
+
13
+ 用户想体验支付时调用 createPaymentOrder,展示固定 0.01 元的“测试购买商品”卡,引导用户点击“去支付”。工具只准备卡片,真实创单发生在用户点击后。
14
+
15
+ 浏览器收银台仅模拟成功,不实际扣款;支付宝小程序内调用真实收银台。不承诺自动退款,不调用查单工具,不声称替用户支付。卡面成功表示收银台返回成功,不构成服务端到账证明。
@@ -0,0 +1,5 @@
1
+ /**
2
+ * module-ai-payment 客户端卡片聚合(0.4.0 D6:一卡一目录;default as named——
3
+ * 注册面消费 markSelfRendering 置位后的组件,shopping-guide / parking 先例)。
4
+ */
5
+ export { default as PaymentOrderCard } from './payment-order';
@@ -0,0 +1,109 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import { callJsapi, isInAlipayShell, mapPaymentResultCode, markSelfRendering, sendCardAction } from '@tbox.cn/app-agent-sdk-client';
3
+ import { createPaymentOrder } from '../../payment';
4
+ import type { PaymentOrderCardData } from '../../../server/contracts';
5
+
6
+ export function PaymentOrderCard(props: { data: PaymentOrderCardData; cardId: string; isHistory: boolean; actionTokens?: Record<string, string> }) {
7
+ const { data, cardId, isHistory, actionTokens } = props;
8
+ const [pending, setPending] = useState(false);
9
+ const [cashier, setCashier] = useState(false);
10
+ const [paid, setPaid] = useState(false);
11
+ const [message, setMessage] = useState('');
12
+ const tradeNo = useRef(data.tradeNo);
13
+ const busy = useRef(false);
14
+ const cashierRef = useRef<HTMLDialogElement>(null);
15
+ useEffect(() => {
16
+ if (!cashier) return;
17
+ const dialog = cashierRef.current;
18
+ dialog?.showModal();
19
+ return () => dialog?.close();
20
+ }, [cashier]);
21
+ const disabled = isHistory || paid || data.payState === 'paid' || data.payState === 'paying' || pending;
22
+ const report = (status: 'success' | 'cancel' | 'failure') => {
23
+ if (status === 'success') setPaid(true);
24
+ const token = actionTokens?.['ai-payment.report-payment-result'];
25
+ if (token) sendCardAction(cardId, { type: 'moduleAction', actionId: 'ai-payment.report-payment-result', contextToken: token }, { status, ...(tradeNo.current ? { paymentRef: tradeNo.current } : {}) });
26
+ };
27
+ const pay = async () => {
28
+ if (disabled || busy.current) return;
29
+ if (!isInAlipayShell()) { setCashier(true); return; }
30
+ busy.current = true;
31
+ setPending(true);
32
+ setMessage('');
33
+ try {
34
+ if (!data.bizContext) throw new Error('AI 支付商家配置缺失');
35
+ if (!data.buyerId || !/^2088\d+$/.test(data.buyerId)) throw new Error('缺少有效的买家支付宝身份');
36
+ if (!tradeNo.current) {
37
+ const order = await createPaymentOrder(data.buyerId);
38
+ if (!order?.tradeNo) throw new Error('体验订单创建失败');
39
+ tradeNo.current = order.tradeNo;
40
+ }
41
+ const result = await callJsapi<{ resultCode?: string }>('tradePay', { tradeNO: tradeNo.current, bizContext: data.bizContext });
42
+ console.info('[ai-payment] tradePay result:', result);
43
+ const status = mapPaymentResultCode(result?.resultCode);
44
+ if (status === 'failure') {
45
+ sendCardAction(cardId, { type: 'toast', content: '支付结果未确认,请勿重复下单', tone: 'error' });
46
+ } else {
47
+ setMessage(status === 'success' ? '收银台返回支付成功' : '已取消支付');
48
+ }
49
+ if (status === 'success') report(status);
50
+ } catch (error) {
51
+ sendCardAction(cardId, { type: 'toast', content: error instanceof Error ? error.message : '支付调用失败,请勿重复下单', tone: 'error' });
52
+ } finally { busy.current = false; setPending(false); }
53
+ };
54
+ const completed = paid || data.payState === 'paid';
55
+ return (
56
+ <div className="ap-card" data-ap-pay-state={paid ? 'paid' : data.payState}>
57
+ <div className="ap-order-heading">
58
+ <span>待支付订单</span>
59
+ <span className="ap-order-status">{completed ? '已支付' : '待付款'}</span>
60
+ </div>
61
+ <ul className="ap-order-list" aria-label="支付订单">
62
+ <li className="ap-order-item">
63
+ <span className="ap-order-icon" aria-hidden="true">
64
+ <svg viewBox="0 0 32 32" fill="none">
65
+ <path d="M8 11h16l2 16H6l2-16Z" fill="currentColor" opacity=".2" />
66
+ <path d="M12 12V9a4 4 0 0 1 8 0v3M8 11h16l2 16H6l2-16Z" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
67
+ </svg>
68
+ </span>
69
+ <div className="ap-order-details">
70
+ <div className="ap-card__title">{data.subject}</div>
71
+ <p className="ap-order-description">标准款 · 1 件</p>
72
+ </div>
73
+ <span className="ap-order-amount">¥{data.totalAmountText}</span>
74
+ </li>
75
+ </ul>
76
+ <button type="button" className="ap-card__btn ap-order-pay" disabled={disabled} onClick={() => void pay()}>
77
+ {completed ? '已支付' : pending ? '支付处理中…' : '去支付'}
78
+ </button>
79
+ {message && <p role="status" className="ap-card__hint">{message}</p>}
80
+ {cashier && (
81
+ <dialog ref={cashierRef} className="ap-cashier" aria-label="模拟支付宝收银台" onCancel={() => setCashier(false)}>
82
+ <p className="ap-cashier-notice">商圈助手正在为您下单,请确认</p>
83
+ <div className="ap-cashier-panel">
84
+ <header className="ap-cashier-header">
85
+ <button type="button" className="ap-cashier-close" aria-label="关闭收银台" onClick={() => setCashier(false)}>×</button>
86
+ <span className="ap-cashier-brand">支 AI付</span>
87
+ </header>
88
+ <div className="ap-cashier-summary">
89
+ <p className="ap-cashier-merchant">示例商家</p>
90
+ <div className="ap-cashier-amount">¥ {data.totalAmountText}</div>
91
+ <p className="ap-cashier-product">{data.subject}</p>
92
+ </div>
93
+ <div className="ap-cashier-method">
94
+ <span className="ap-cashier-method-icon" aria-hidden="true">支</span>
95
+ <span>支付宝余额(模拟)</span>
96
+ </div>
97
+ <button type="button" className="ap-card__btn ap-cashier-pay" data-ap-confirm disabled={disabled} onClick={() => {
98
+ setCashier(false);
99
+ setMessage('模拟支付成功');
100
+ report('success');
101
+ }}>付款</button>
102
+ <p className="ap-cashier-disclaimer">模拟收银台,仅体验,不会实际扣款</p>
103
+ </div>
104
+ </dialog>
105
+ )}
106
+ </div>
107
+ );
108
+ }
109
+ export default markSelfRendering(PaymentOrderCard);
@@ -0,0 +1,21 @@
1
+ /**
2
+ * module-ai-payment 客户端入口(与服务端 serverModule 对称)。
3
+ */
4
+ import './styles/ai-payment.css'; // 模块自持 CSS(副作用 import,首行保证先行加载)
5
+
6
+ import type { CardComponent } from '@tbox.cn/app-contracts';
7
+ import type { ClientContext, ClientModule } from '@tbox.cn/app-agent-sdk-client';
8
+ import { PaymentOrderCard } from './cards';
9
+
10
+ const cards = {
11
+ 'payment-order': PaymentOrderCard as CardComponent,
12
+ } as const;
13
+
14
+ export const clientModule = {
15
+ cards,
16
+ register(ctx: ClientContext): void {
17
+ ctx.cards.registerMap(cards);
18
+ },
19
+ } satisfies ClientModule;
20
+
21
+ export default clientModule;
@@ -0,0 +1,14 @@
1
+ import { callCapability } from '@tbox.cn/app-agent-sdk-client';
2
+
3
+ /** 支付业务契约留在 H5;容器只透传云容器请求和响应。 */
4
+ export async function createPaymentOrder(buyerId: string): Promise<{ tradeNo: string }> {
5
+ const response = await callCapability<{ statusCode: number; data: { tradeNo?: unknown } | null }>('app.callContainer', {
6
+ path: '/api/alipay/aipay/create', method: 'POST', dataType: 'json',
7
+ header: { 'Content-Type': 'application/json', 'X-API-Token': 'fortest', 'X-Alipay-Service': 'pay-demo' },
8
+ data: { buyerId },
9
+ });
10
+ if (response?.statusCode !== 200 || typeof response.data?.tradeNo !== 'string' || !response.data.tradeNo.trim()) {
11
+ throw new Error('体验订单创建失败');
12
+ }
13
+ return { tradeNo: response.data.tradeNo };
14
+ }
@@ -0,0 +1,156 @@
1
+ /*
2
+ * module-ai-payment 域样式(ap- 前缀,模块自持零跨模块共享)。
3
+ * 尺寸按仓库 750 设计稿 2x px 编写,经 pxtorem 转换;卡框描边和圆角由 SDK 统一。
4
+ * 机械规则:禁 color-mix / !important;--tbox-* 语义变量恒带 fallback(消费形态);
5
+ * 不声明元素级 --tbox-asset-*(品牌 IP 素材唯一归宿 = 主题包,guard-asset-cascade 拦截)。
6
+ */
7
+ .ap-card {
8
+ --ap-accent: #096bfa;
9
+ --ap-text: var(--tbox-text-primary, #182235);
10
+ --ap-text-2: var(--tbox-text-secondary, #6f7c8f);
11
+ --ap-surface: var(--tbox-bg-card, #fff);
12
+
13
+ box-sizing: border-box;
14
+ width: 100%;
15
+ padding: 28px;
16
+ background: var(--ap-surface);
17
+ color: var(--ap-text);
18
+ font: inherit;
19
+ }
20
+ .ap-card *,
21
+ .ap-card *::before,
22
+ .ap-card *::after {
23
+ box-sizing: border-box;
24
+ }
25
+ .ap-card__title {
26
+ font-size: 28px;
27
+ line-height: 40px;
28
+ font-weight: 600;
29
+ color: var(--ap-text);
30
+ }
31
+ .ap-card__btn {
32
+ margin-top: 24px;
33
+ width: 100%;
34
+ min-height: 72px;
35
+ padding: 12px 24px;
36
+ border: 0;
37
+ border-radius: 999px;
38
+ font-family: inherit;
39
+ font-size: 27px;
40
+ font-weight: 600;
41
+ line-height: 40px;
42
+ color: #fff;
43
+ background: var(--ap-accent);
44
+ cursor: pointer;
45
+ }
46
+ .ap-card__btn:disabled {
47
+ background: var(--tbox-bg-subtle, #f2f4f8);
48
+ color: var(--tbox-text-muted, #9aa4b4);
49
+ cursor: not-allowed;
50
+ }
51
+ .ap-card__btn:focus-visible,
52
+ .ap-cashier-close:focus-visible {
53
+ outline: 4px solid var(--ap-accent);
54
+ outline-offset: 4px;
55
+ }
56
+ .ap-card__hint {
57
+ margin: 16px 0 0;
58
+ font-size: 24px;
59
+ line-height: 36px;
60
+ overflow-wrap: anywhere;
61
+ color: var(--ap-text-2);
62
+ }
63
+
64
+ .ap-order-heading {
65
+ display: flex;
66
+ align-items: center;
67
+ justify-content: space-between;
68
+ gap: 24px;
69
+ font-size: 30px;
70
+ line-height: 44px;
71
+ font-weight: 700;
72
+ }
73
+ .ap-order-status {
74
+ color: var(--tbox-text-secondary, #6f7c8f);
75
+ font-size: 22px;
76
+ line-height: 32px;
77
+ font-weight: 400;
78
+ white-space: nowrap;
79
+ }
80
+ .ap-order-list { list-style: none; margin: 24px 0 0; padding: 0; }
81
+ .ap-order-item { display: flex; align-items: center; gap: 20px; }
82
+ .ap-order-icon {
83
+ display: grid;
84
+ place-items: center;
85
+ flex: 0 0 88px;
86
+ height: 88px;
87
+ border-radius: 16px;
88
+ background: #eef4fc;
89
+ color: var(--ap-accent);
90
+ }
91
+ .ap-order-icon svg { width: 64px; height: 64px; }
92
+ .ap-order-details { flex: 1; min-width: 0; }
93
+ .ap-order-details .ap-card__title { overflow-wrap: anywhere; }
94
+ .ap-order-description { margin: 8px 0 0; color: var(--tbox-text-secondary, #6f7c8f); font-size: 24px; line-height: 36px; }
95
+ .ap-order-amount { flex-shrink: 0; color: var(--ap-text); font-size: 32px; line-height: 44px; font-weight: 600; font-variant-numeric: tabular-nums; }
96
+ .ap-order-pay { border-radius: 28px; }
97
+ .ap-cashier {
98
+ position: fixed;
99
+ inset: auto 0 0;
100
+ margin: 0 auto;
101
+ width: min(100%, 960px);
102
+ max-width: 100%;
103
+ max-height: 85dvh;
104
+ box-sizing: border-box;
105
+ padding: 0;
106
+ border: 0;
107
+ background: transparent;
108
+ color: var(--tbox-text-primary, #182235);
109
+ font: inherit;
110
+ text-align: center;
111
+ overflow-y: auto;
112
+ animation: ap-cashier-enter 180ms ease-out;
113
+ }
114
+ .ap-cashier-notice {
115
+ margin: 0 20px 16px;
116
+ padding: 18px 24px;
117
+ border-radius: 999px;
118
+ background: var(--tbox-bg-card, #fff);
119
+ color: var(--tbox-text-secondary, #6f7c8f);
120
+ font-size: 24px;
121
+ line-height: 1.5;
122
+ text-align: left;
123
+ }
124
+ .ap-cashier-panel {
125
+ min-height: min(50dvh, 840px);
126
+ box-sizing: border-box;
127
+ padding: 32px 40px calc(40px + env(safe-area-inset-bottom, 0px));
128
+ border-radius: 40px 40px 0 0;
129
+ background: var(--tbox-bg-card, #fff);
130
+ }
131
+ .ap-cashier::backdrop { background: rgba(0, 0, 0, 0.35); }
132
+ .ap-cashier-header { position: relative; display: flex; align-items: center; justify-content: center; min-height: 56px; }
133
+ .ap-cashier-close {
134
+ position: absolute;
135
+ left: -8px;
136
+ top: -8px;
137
+ width: 72px;
138
+ height: 72px;
139
+ border: 0;
140
+ border-radius: 16px;
141
+ background: transparent;
142
+ color: var(--tbox-text-secondary, #6f7c8f);
143
+ font-size: 52px;
144
+ cursor: pointer;
145
+ }
146
+ .ap-cashier-brand { color: var(--ap-accent); font-size: 36px; font-weight: 600; }
147
+ .ap-cashier-summary { margin: 36px 0 48px; }
148
+ .ap-cashier-merchant { margin: 0 0 12px; color: var(--tbox-text-secondary, #6f7c8f); font-size: 24px; line-height: 36px; }
149
+ .ap-cashier-amount { font-size: 64px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
150
+ .ap-cashier-product { margin: 12px 0 0; color: var(--tbox-text-secondary, #6f7c8f); font-size: 24px; line-height: 36px; }
151
+ .ap-cashier-method { display: flex; align-items: center; gap: 20px; padding: 24px; border-radius: 24px; background: var(--tbox-bg-subtle, #f7f9fc); text-align: left; font-size: 28px; }
152
+ .ap-cashier-method-icon { display: grid; place-items: center; width: 60px; height: 60px; border-radius: 50%; background: var(--ap-accent); color: #fff; font-weight: 600; }
153
+ .ap-cashier-pay { margin-top: 40px; }
154
+ .ap-cashier-disclaimer { margin: 24px 0 0; color: var(--tbox-text-secondary, #6f7c8f); font-size: 24px; line-height: 36px; }
155
+ @keyframes ap-cashier-enter { from { transform: translateY(100%); } to { transform: translateY(0); } }
156
+ @media (prefers-reduced-motion: reduce) { .ap-cashier { animation: none; } }
@@ -0,0 +1,2 @@
1
+ /** 模块自持 CSS(经 tsup loader: { '.css': 'copy' } 出货,样式在应用侧生效) */
2
+ declare module '*.css';
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './server/index';
@@ -0,0 +1,41 @@
1
+ import { z } from 'zod';
2
+ import type { CardActionResult, RequestContext } from '@tbox.cn/app-contracts';
3
+ import type { PaymentOrderCardData } from '../contracts';
4
+
5
+ /** 金额/品名服务端常量(与 tools 同源维护;P0 定价权红线——金额不可来自动作入参/模型) */
6
+ const DEMO_TOTAL_AMOUNT_TEXT = '0.01';
7
+ const DEMO_SUBJECT = '测试购买商品';
8
+
9
+ type CallbackInput = { status: 'success' | 'cancel' | 'failure' | 'unavailable'; paymentRef?: string };
10
+
11
+ export function createAiPaymentActions() {
12
+ const reportAction = {
13
+ actionId: 'ai-payment.report-payment-result',
14
+ moduleId: 'ai-payment',
15
+ cardType: 'payment-order',
16
+ label: '支付结果上报',
17
+ singleUse: true,
18
+ hidden: true,
19
+ kind: 'moduleAction' as const,
20
+ inputSchema: z.object({ status: z.enum(['success', 'cancel', 'failure', 'unavailable']), paymentRef: z.string().optional() }),
21
+ async execute(input: CallbackInput, _reqCtx: RequestContext): Promise<CardActionResult> {
22
+ void _reqCtx;
23
+ const payStateMap = { success: 'paid', cancel: 'cancelled', failure: 'failed', unavailable: 'created' } as const;
24
+ const textMap = {
25
+ success: '收银台返回支付成功',
26
+ cancel: '已取消支付,可重新发起',
27
+ failure: '收银台未返回支付成功,请勿重复创建订单',
28
+ unavailable: '请在支付宝壳环境内完成支付',
29
+ };
30
+ const data: PaymentOrderCardData = {
31
+ ...(input.paymentRef ? { tradeNo: input.paymentRef } : {}),
32
+ subject: DEMO_SUBJECT,
33
+ totalAmountText: DEMO_TOTAL_AMOUNT_TEXT,
34
+ payState: payStateMap[input.status],
35
+ };
36
+ return { text: textMap[input.status], update: { data } } satisfies CardActionResult;
37
+ },
38
+ };
39
+
40
+ return { 'ai-payment.report-payment-result': reportAction } as const;
41
+ }
@@ -0,0 +1,18 @@
1
+ import type { CardMeta } from '@tbox.cn/app-contracts';
2
+ import { paymentOrderCardDataSchema } from '../../contracts';
3
+
4
+ const meta: CardMeta<typeof paymentOrderCardDataSchema> = {
5
+ cardType: 'payment-order',
6
+ dataSchema: paymentOrderCardDataSchema,
7
+ displayName: 'AI 支付体验',
8
+ description: '测试商品待支付卡(去支付拉起支付宝收银台)',
9
+ sampleData: {
10
+ subject: '测试购买商品',
11
+ totalAmountText: '0.01',
12
+ payState: 'created',
13
+ bizContext: '{"cashierScene":"agentPay","agentPid":"2088051636299393","agentId":"2026000116664809"}',
14
+ },
15
+ schemaVersion: 1,
16
+ };
17
+
18
+ export default meta;
@@ -0,0 +1,26 @@
1
+ import { access, readFile } from 'node:fs/promises';
2
+ import { dirname, resolve } from 'node:path';
3
+
4
+ /** CLI 启动时 cwd 可为 apps/server,沿应用标记定位配置根目录。 */
5
+ export async function paymentAppRoot(start = process.cwd()): Promise<string> {
6
+ let current = resolve(start);
7
+ while (true) {
8
+ try { await access(resolve(current, '.tbox/app.json')); return current; } catch { /* 继续查找应用根 */ }
9
+ const parent = dirname(current);
10
+ if (parent === current) return resolve(start);
11
+ current = parent;
12
+ }
13
+ }
14
+
15
+ export async function loadPaymentConfig(options: { env?: NodeJS.ProcessEnv; appDir?: string } = {}) {
16
+ const env = options.env ?? process.env;
17
+ // TODO:这是临时体验的智能体 ID,使用时请在平台申请真实 ID,并写入 config/kya.json 的 kyaid。
18
+ let agentId = '2026000116664809';
19
+ try {
20
+ const config: unknown = JSON.parse(await readFile(resolve(options.appDir ?? await paymentAppRoot(), 'config', 'kya.json'), 'utf8'));
21
+ const id = (config as { kyaid?: unknown } | null)?.kyaid;
22
+ if (typeof id === 'string' && id.trim()) agentId = id.trim();
23
+ } catch { /* 配置不可读取时使用默认智能体标识 */ }
24
+ const agentPid = env.AI_PAY_AGENT_PID?.trim();
25
+ return { ...(agentPid ? { bizContext: JSON.stringify({ cashierScene: 'agentPay', agentPid, agentId }) } : {}) };
26
+ }