@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 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。开发环境使用 mock;生产环境设置 NODE_ENV=production,并在应用根目录携带:
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
- NODE_ENV=production 使用真实环境,读取应用根目录 kya.json 的非空字符串 kyaid;其余环境使用固定体验 agentId。买家身份不区分环境:优先使用已有非匿名身份中的有效 `2088` 数字 ID,否则兜底为测试账号 `2088052281674865`。AI_PAY_AGENT_PID 为收款商家 PID,通过环境变量注入,与卡片已有的买家身份分开。
13
+ 统一读取应用根目录下 config/kya.json 的非空字符串 kyaid;文件不可读取、JSON 无效或字段无效时使用默认 agentId `2026000116664809`,不区分环境。买家身份不区分环境:优先使用已有非匿名身份中的有效 `2088` 数字 ID,否则兜底为测试账号 `2088052281674865`。AI_PAY_AGENT_PID 为收款商家 PID,通过环境变量注入,与卡片已有的买家身份分开。
14
14
 
15
- 配置在工具执行期读取,从当前工作目录沿 .tbox/app.json 标记定位生成应用根目录(兼容 apps/server 启动)。真实环境缺少 kya.json 或 kyaid 时显性报错;商家 PID 缺失时 Web 仍可模拟,端内支付明确拒绝。
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
- if (mode === "real") {
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 !== "string" || !id.trim()) throw new Error("kya.json \u7F3A\u5C11\u975E\u7A7A kyaid");
61
- agentId = id.trim();
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 { mode, ...agentPid ? { bizContext: JSON.stringify({ cashierScene: "agentPay", agentPid, agentId }) } : {} };
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
@@ -4,7 +4,7 @@ import {
4
4
  createAiPaymentTools,
5
5
  resolvePaymentPort,
6
6
  serverModule
7
- } from "./chunk-QDS6RD3T.js";
7
+ } from "./chunk-ERHTVWF5.js";
8
8
  export {
9
9
  PaymentNotConfiguredError,
10
10
  createAiPaymentActions,
@@ -57,7 +57,6 @@ declare function loadPaymentConfig(options?: {
57
57
  appDir?: string;
58
58
  }): Promise<{
59
59
  bizContext?: string | undefined;
60
- mode: string;
61
60
  }>;
62
61
 
63
62
  declare function createAiPaymentTools(options?: {
@@ -5,7 +5,7 @@ import {
5
5
  resolvePaymentPort,
6
6
  serverModule,
7
7
  server_default
8
- } from "../chunk-QDS6RD3T.js";
8
+ } from "../chunk-ERHTVWF5.js";
9
9
  export {
10
10
  PaymentNotConfiguredError,
11
11
  createAiPaymentActions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tbox.cn/app-module-ai-payment",
3
- "version": "0.1.0",
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.5",
23
- "@tbox.cn/app-agent-sdk-server": "0.2.2",
24
- "@tbox.cn/app-contracts": "0.12.4",
25
- "@tbox.cn/app-contracts-mall": "0.12.5"
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",
@@ -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
- const mode = env.NODE_ENV === 'production' ? 'real' : 'mock';
17
+ // TODO:这是临时体验的智能体 ID,使用时请在平台申请真实 ID,并写入 config/kya.json 的 kyaid。
18
18
  let agentId = '2026000116664809';
19
- if (mode === 'real') {
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 !== 'string' || !id.trim()) throw new Error('kya.json 缺少非空 kyaid');
23
- agentId = id.trim();
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 { mode, ...(agentPid ? { bizContext: JSON.stringify({ cashierScene: 'agentPay', agentPid, agentId }) } : {}) };
25
+ return { ...(agentPid ? { bizContext: JSON.stringify({ cashierScene: 'agentPay', agentPid, agentId }) } : {}) };
27
26
  }
package/tbox.module.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "id": "module-ai-payment",
4
4
  "name": "AI 支付",
5
5
  "description": "0.01 元测试商品,Web 模拟支付与端内云容器创单支付",
6
- "version": "0.1.0",
6
+ "version": "0.1.1",
7
7
  "kind": "business",
8
8
  "risk": {
9
9
  "level": "medium",
@@ -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 () => ({ mode: 'mock' }) });
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.each(['mock', 'real'])('%s 配置均优先使用有效买家,否则使用固定兜底', async (mode) => {
18
- const tools = createAiPaymentTools({ loadConfig: async () => ({ mode }) });
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('真实环境读取 kya.json,商家 PID 与买家独立', async () => {
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 writeFile(join(appDir, 'kya.json'), JSON.stringify({ kyaid: 'registered-agent' }));
37
- const out = await loadPaymentConfig({ appDir, env: { NODE_ENV: 'production', AI_PAY_AGENT_PID: 'merchant' } });
38
- expect(JSON.parse(out.bizContext!)).toEqual({ cashierScene: 'agentPay', agentPid: 'merchant', agentId: 'registered-agent' });
39
- await writeFile(join(appDir, 'kya.json'), '{}');
40
- await expect(loadPaymentConfig({ appDir, env: { NODE_ENV: 'production' } })).rejects.toThrow('kyaid');
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('非生产环境使用 mock 固定 ID', async () => {
44
- expect(JSON.parse((await loadPaymentConfig({ env: { NODE_ENV: 'development', AI_PAY_AGENT_PID: 'merchant' } })).bizContext!).agentId).toBe('2026000116664809');
45
- expect((await loadPaymentConfig({ env: {} })).mode).toBe('mock');
46
- expect((await loadPaymentConfig({ env: { NODE_ENV: 'test' } })).mode).toBe('mock');
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
  });