@tbox.cn/app-module-ai-payment 0.1.0 → 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.
- package/INTEGRATION.md +1 -1
- package/README.md +2 -2
- package/dist/{chunk-QDS6RD3T.js → chunk-ERHTVWF5.js} +5 -6
- package/dist/index.js +1 -1
- package/dist/server/index.d.ts +0 -1
- package/dist/server/index.js +1 -1
- package/package.json +5 -5
- package/src/server/config.ts +6 -7
- package/tbox.module.json +1 -1
- package/tests/ai-payment.test.ts +22 -13
package/INTEGRATION.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
安装 module-ai-payment 后,通过 CLI 维护双端装配。支付宝端内同时更新 miniapp-container-alipay,使容器提供 app.callContainer。H5 不直接访问 my.cloud;订单金额由支付后端固定,不接受模型或卡片传入金额。
|
|
6
6
|
|
|
7
|
-
在服务端环境注入 AI_PAY_AGENT_PID
|
|
7
|
+
在服务端环境注入 AI_PAY_AGENT_PID。不区分运行环境,在应用根目录的 config/ 下携带 kya.json:
|
|
8
8
|
|
|
9
9
|
```json
|
|
10
10
|
{ "kyaid": "已审批的智能体标识" }
|
package/README.md
CHANGED
|
@@ -10,9 +10,9 @@ createPaymentOrder 只准备待支付卡与已有买家身份,不发起真实
|
|
|
10
10
|
|
|
11
11
|
## 服务端配置
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
统一读取应用根目录下 config/kya.json 的非空字符串 kyaid;文件不可读取、JSON 无效或字段无效时使用默认 agentId `2026000116664809`,不区分环境。买家身份不区分环境:优先使用已有非匿名身份中的有效 `2088` 数字 ID,否则兜底为测试账号 `2088052281674865`。AI_PAY_AGENT_PID 为收款商家 PID,通过环境变量注入,与卡片已有的买家身份分开。
|
|
14
14
|
|
|
15
|
-
配置在工具执行期读取,从当前工作目录沿 .tbox/app.json 标记定位生成应用根目录(兼容 apps/server
|
|
15
|
+
配置在工具执行期读取,从当前工作目录沿 .tbox/app.json 标记定位生成应用根目录(兼容 apps/server 启动)。商家 PID 缺失时 Web 仍可模拟,端内支付明确拒绝。
|
|
16
16
|
|
|
17
17
|
## 小程序容器
|
|
18
18
|
|
|
@@ -52,16 +52,15 @@ async function paymentAppRoot(start = process.cwd()) {
|
|
|
52
52
|
}
|
|
53
53
|
async function loadPaymentConfig(options = {}) {
|
|
54
54
|
const env = options.env ?? process.env;
|
|
55
|
-
const mode = env.NODE_ENV === "production" ? "real" : "mock";
|
|
56
55
|
let agentId = "2026000116664809";
|
|
57
|
-
|
|
58
|
-
const config = JSON.parse(await readFile(resolve(options.appDir ?? await paymentAppRoot(), "kya.json"), "utf8"));
|
|
56
|
+
try {
|
|
57
|
+
const config = JSON.parse(await readFile(resolve(options.appDir ?? await paymentAppRoot(), "config", "kya.json"), "utf8"));
|
|
59
58
|
const id = config?.kyaid;
|
|
60
|
-
if (typeof id
|
|
61
|
-
|
|
59
|
+
if (typeof id === "string" && id.trim()) agentId = id.trim();
|
|
60
|
+
} catch {
|
|
62
61
|
}
|
|
63
62
|
const agentPid = env.AI_PAY_AGENT_PID?.trim();
|
|
64
|
-
return {
|
|
63
|
+
return { ...agentPid ? { bizContext: JSON.stringify({ cashierScene: "agentPay", agentPid, agentId }) } : {} };
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
// src/server/tools/index.ts
|
package/dist/index.js
CHANGED
package/dist/server/index.d.ts
CHANGED
package/dist/server/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-module-ai-payment",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "AI 支付体验:Web 模拟收银台与端内云容器创单",
|
|
6
6
|
"exports": {
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"./package.json": "./package.json"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@tbox.cn/app-agent-sdk-client": "0.1.
|
|
23
|
-
"@tbox.cn/app-agent-sdk-server": "0.2.
|
|
24
|
-
"@tbox.cn/app-contracts": "0.
|
|
25
|
-
"@tbox.cn/app-contracts-mall": "0.
|
|
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
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"zod": "3.25.76",
|
package/src/server/config.ts
CHANGED
|
@@ -14,14 +14,13 @@ export async function paymentAppRoot(start = process.cwd()): Promise<string> {
|
|
|
14
14
|
|
|
15
15
|
export async function loadPaymentConfig(options: { env?: NodeJS.ProcessEnv; appDir?: string } = {}) {
|
|
16
16
|
const env = options.env ?? process.env;
|
|
17
|
-
|
|
17
|
+
// TODO:这是临时体验的智能体 ID,使用时请在平台申请真实 ID,并写入 config/kya.json 的 kyaid。
|
|
18
18
|
let agentId = '2026000116664809';
|
|
19
|
-
|
|
20
|
-
const config: unknown = JSON.parse(await readFile(resolve(options.appDir ?? await paymentAppRoot(), 'kya.json'), 'utf8'));
|
|
19
|
+
try {
|
|
20
|
+
const config: unknown = JSON.parse(await readFile(resolve(options.appDir ?? await paymentAppRoot(), 'config', 'kya.json'), 'utf8'));
|
|
21
21
|
const id = (config as { kyaid?: unknown } | null)?.kyaid;
|
|
22
|
-
if (typeof id
|
|
23
|
-
|
|
24
|
-
}
|
|
22
|
+
if (typeof id === 'string' && id.trim()) agentId = id.trim();
|
|
23
|
+
} catch { /* 配置不可读取时使用默认智能体标识 */ }
|
|
25
24
|
const agentPid = env.AI_PAY_AGENT_PID?.trim();
|
|
26
|
-
return {
|
|
25
|
+
return { ...(agentPid ? { bizContext: JSON.stringify({ cashierScene: 'agentPay', agentPid, agentId }) } : {}) };
|
|
27
26
|
}
|
package/tbox.module.json
CHANGED
package/tests/ai-payment.test.ts
CHANGED
|
@@ -8,14 +8,14 @@ import { join } from 'node:path';
|
|
|
8
8
|
|
|
9
9
|
describe('体验商品准备', () => {
|
|
10
10
|
it('mock 工具只准备卡和固定测试买家,不创单不查单', async () => {
|
|
11
|
-
const tools = createAiPaymentTools({ loadConfig: async () => ({
|
|
11
|
+
const tools = createAiPaymentTools({ loadConfig: async () => ({}) });
|
|
12
12
|
const out = await tools.createPaymentOrder.execute!({}, createToolExecContext({ identity: { userId: 'anonymous_example', source: 'anonymous' } } as never) as never) as Record<string, unknown>;
|
|
13
13
|
expect(out).toMatchObject({ subject: '测试购买商品', totalAmountText: '0.01', buyerId: '2088052281674865' });
|
|
14
14
|
expect(out.tradeNo).toBeUndefined();
|
|
15
15
|
expect(Object.keys(tools)).toEqual(['createPaymentOrder']);
|
|
16
16
|
});
|
|
17
|
-
it
|
|
18
|
-
const tools = createAiPaymentTools({ loadConfig: async () => ({
|
|
17
|
+
it('优先使用有效买家,否则使用固定兜底', async () => {
|
|
18
|
+
const tools = createAiPaymentTools({ loadConfig: async () => ({}) });
|
|
19
19
|
for (const identity of [
|
|
20
20
|
{ userId: '2088000000000002', source: 'external' },
|
|
21
21
|
{ userId: 'anonymous_example', source: 'anonymous' },
|
|
@@ -26,23 +26,32 @@ describe('体验商品准备', () => {
|
|
|
26
26
|
expect(out.buyerId).toBe(identity.userId === '2088000000000002' ? identity.userId : '2088052281674865');
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
|
-
it('
|
|
29
|
+
it('读取 config/kya.json,商家 PID 与买家独立', async () => {
|
|
30
30
|
const appDir = await mkdtemp(join(tmpdir(), 'payment-config-'));
|
|
31
31
|
try {
|
|
32
32
|
await mkdir(join(appDir, '.tbox'));
|
|
33
33
|
await writeFile(join(appDir, '.tbox/app.json'), '{}');
|
|
34
34
|
await mkdir(join(appDir, 'apps/server'), { recursive: true });
|
|
35
35
|
expect(await paymentAppRoot(join(appDir, 'apps/server'))).toBe(appDir);
|
|
36
|
-
await
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
await mkdir(join(appDir, 'config'));
|
|
37
|
+
await writeFile(join(appDir, 'kya.json'), JSON.stringify({ kyaid: 'wrong-root-agent' }));
|
|
38
|
+
await writeFile(join(appDir, 'config/kya.json'), JSON.stringify({ kyaid: 'registered-agent' }));
|
|
39
|
+
for (const NODE_ENV of ['production', 'development', 'test']) {
|
|
40
|
+
const out = await loadPaymentConfig({ appDir, env: { NODE_ENV, AI_PAY_AGENT_PID: 'merchant' } });
|
|
41
|
+
expect(JSON.parse(out.bizContext!)).toEqual({ cashierScene: 'agentPay', agentPid: 'merchant', agentId: 'registered-agent' });
|
|
42
|
+
}
|
|
41
43
|
} finally { await rm(appDir, { recursive: true }); }
|
|
42
44
|
});
|
|
43
|
-
it('
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
it('文件缺失、JSON 损坏或字段无效时使用默认 ID', async () => {
|
|
46
|
+
const appDir = await mkdtemp(join(tmpdir(), 'payment-fallback-'));
|
|
47
|
+
try {
|
|
48
|
+
const readId = async () => JSON.parse((await loadPaymentConfig({ appDir, env: { AI_PAY_AGENT_PID: 'merchant' } })).bizContext!).agentId;
|
|
49
|
+
expect(await readId()).toBe('2026000116664809');
|
|
50
|
+
await mkdir(join(appDir, 'config'));
|
|
51
|
+
for (const content of ['{', '{}', 'null', '{"kyaid":123}', '{"kyaid":" "}']) {
|
|
52
|
+
await writeFile(join(appDir, 'config/kya.json'), content);
|
|
53
|
+
expect(await readId()).toBe('2026000116664809');
|
|
54
|
+
}
|
|
55
|
+
} finally { await rm(appDir, { recursive: true }); }
|
|
47
56
|
});
|
|
48
57
|
});
|