@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.
- package/INTEGRATION.md +15 -0
- package/README.md +18 -2
- package/dist/ai-payment-PNOA2J6W.css +156 -0
- package/dist/chunk-ERHTVWF5.js +175 -0
- package/dist/client/index.d.ts +15 -0
- package/dist/client/index.js +147 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +14 -0
- package/dist/server/index.d.ts +131 -0
- package/dist/server/index.js +16 -0
- package/package.json +66 -3
- package/skills/ai-payment/SKILL.md +15 -0
- package/src/client/cards/index.ts +5 -0
- package/src/client/cards/payment-order/index.tsx +109 -0
- package/src/client/index.ts +21 -0
- package/src/client/payment.ts +14 -0
- package/src/client/styles/ai-payment.css +156 -0
- package/src/client/types/css.d.ts +2 -0
- package/src/index.ts +1 -0
- package/src/server/actions/index.ts +41 -0
- package/src/server/cards/payment-order/meta.ts +18 -0
- package/src/server/config.ts +26 -0
- package/src/server/contracts.ts +50 -0
- package/src/server/index.ts +29 -0
- package/src/server/ports.ts +37 -0
- package/src/server/tools/index.ts +27 -0
- package/tbox.module.json +48 -0
- package/tests/actions.test.ts +32 -0
- package/tests/ai-payment.test.ts +57 -0
- package/tests/cards-render.test.tsx +22 -0
- package/tsconfig.json +8 -0
- package/tsup.config.ts +36 -0
package/INTEGRATION.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# AI 支付接入
|
|
2
|
+
|
|
3
|
+
本页说明生成应用如何接入体验组件。运行契约见 [README.md](README.md)。
|
|
4
|
+
|
|
5
|
+
安装 module-ai-payment 后,通过 CLI 维护双端装配。支付宝端内同时更新 miniapp-container-alipay,使容器提供 app.callContainer。H5 不直接访问 my.cloud;订单金额由支付后端固定,不接受模型或卡片传入金额。
|
|
6
|
+
|
|
7
|
+
在服务端环境注入 AI_PAY_AGENT_PID。不区分运行环境,在应用根目录的 config/ 下携带 kya.json:
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{ "kyaid": "已审批的智能体标识" }
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
提示词可加入:“用户想体验 AI 支付时,展示测试购买商品卡,引导用户点击去支付;不代付、不承诺退款。”模块携带的 ai-payment skill 同样提供此指引。
|
|
14
|
+
|
|
15
|
+
替换商家创单实现时,在 H5 支付模块中适配业务接口并解析 tradeNo,保持商品金额、买家身份及收款主体的服务端校验;本期仅提供固定 0.01 元体验接口。
|
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# AI 支付体验组件
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
本包提供固定 0.01 元测试商品卡及支付交互。安装装配规则见 [INTEGRATION.md](INTEGRATION.md)。
|
|
4
|
+
|
|
5
|
+
## 运行逻辑
|
|
6
|
+
|
|
7
|
+
createPaymentOrder 只准备待支付卡与已有买家身份,不发起真实交易。订单以图标、标题、描述、金额展示;Web 点击后从底部半屏打开模拟收银台,仅模拟成功且不扣款;支付宝小程序内点击后由 H5 支付模块组装接口参数,经 app.callContainer 调用云容器创单,再以 tradeNO 和 bizContext 拉起 tradePay。取消后复用当前交易号,避免重复创单。
|
|
8
|
+
|
|
9
|
+
支付成功仅表示收银台返回成功,本组件不查单、不承担到账证明,也不自动退款。支付结果仅在原订单卡更新状态。
|
|
10
|
+
|
|
11
|
+
## 服务端配置
|
|
12
|
+
|
|
13
|
+
统一读取应用根目录下 config/kya.json 的非空字符串 kyaid;文件不可读取、JSON 无效或字段无效时使用默认 agentId `2026000116664809`,不区分环境。买家身份不区分环境:优先使用已有非匿名身份中的有效 `2088` 数字 ID,否则兜底为测试账号 `2088052281674865`。AI_PAY_AGENT_PID 为收款商家 PID,通过环境变量注入,与卡片已有的买家身份分开。
|
|
14
|
+
|
|
15
|
+
配置在工具执行期读取,从当前工作目录沿 .tbox/app.json 标记定位生成应用根目录(兼容 apps/server 启动)。商家 PID 缺失时 Web 仍可模拟,端内支付明确拒绝。
|
|
16
|
+
|
|
17
|
+
## 小程序容器
|
|
18
|
+
|
|
19
|
+
需要携带 app.callContainer 能力的支付宝容器。云环境 env-00jy61udznyh,云服务 pay-demo;固定 POST /api/alipay/aipay/create,业务请求头 Content-Type: application/json、X-API-Token: fortest。买家 ID 优先来自已有登录上下文,缺失或格式不符时使用固定兜底,不新增登录调用。云环境须关联调用方小程序。
|
|
@@ -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,175 @@
|
|
|
1
|
+
// src/server/index.ts
|
|
2
|
+
import { loadModuleSkills } from "@tbox.cn/app-agent-sdk-server";
|
|
3
|
+
|
|
4
|
+
// src/server/contracts.ts
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
var PAYMENT_SERVICE = "payment.main";
|
|
7
|
+
var paymentOrderCardDataSchema = z.object({
|
|
8
|
+
tradeNo: z.string().min(1).optional(),
|
|
9
|
+
buyerId: z.string().min(1).optional(),
|
|
10
|
+
subject: z.string().min(1),
|
|
11
|
+
totalAmountText: z.string().min(1),
|
|
12
|
+
payState: z.enum(["created", "paying", "paid", "cancelled", "failed"]),
|
|
13
|
+
/** AI 付 tradePay 拉起上下文(provider 组装的不透明 JSON 字符串;可选——非 AI 付形态缺省) */
|
|
14
|
+
bizContext: z.string().min(1).optional()
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// src/server/cards/payment-order/meta.ts
|
|
18
|
+
var meta = {
|
|
19
|
+
cardType: "payment-order",
|
|
20
|
+
dataSchema: paymentOrderCardDataSchema,
|
|
21
|
+
displayName: "AI \u652F\u4ED8\u4F53\u9A8C",
|
|
22
|
+
description: "\u6D4B\u8BD5\u5546\u54C1\u5F85\u652F\u4ED8\u5361\uFF08\u53BB\u652F\u4ED8\u62C9\u8D77\u652F\u4ED8\u5B9D\u6536\u94F6\u53F0\uFF09",
|
|
23
|
+
sampleData: {
|
|
24
|
+
subject: "\u6D4B\u8BD5\u8D2D\u4E70\u5546\u54C1",
|
|
25
|
+
totalAmountText: "0.01",
|
|
26
|
+
payState: "created",
|
|
27
|
+
bizContext: '{"cashierScene":"agentPay","agentPid":"2088051636299393","agentId":"2026000116664809"}'
|
|
28
|
+
},
|
|
29
|
+
schemaVersion: 1
|
|
30
|
+
};
|
|
31
|
+
var meta_default = meta;
|
|
32
|
+
|
|
33
|
+
// src/server/tools/index.ts
|
|
34
|
+
import { createTool, requireToolRequestContext, withCardEmissionHint } from "@tbox.cn/app-agent-sdk-server";
|
|
35
|
+
import { z as z2 } from "zod";
|
|
36
|
+
|
|
37
|
+
// src/server/config.ts
|
|
38
|
+
import { access, readFile } from "fs/promises";
|
|
39
|
+
import { dirname, resolve } from "path";
|
|
40
|
+
async function paymentAppRoot(start = process.cwd()) {
|
|
41
|
+
let current = resolve(start);
|
|
42
|
+
while (true) {
|
|
43
|
+
try {
|
|
44
|
+
await access(resolve(current, ".tbox/app.json"));
|
|
45
|
+
return current;
|
|
46
|
+
} catch {
|
|
47
|
+
}
|
|
48
|
+
const parent = dirname(current);
|
|
49
|
+
if (parent === current) return resolve(start);
|
|
50
|
+
current = parent;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function loadPaymentConfig(options = {}) {
|
|
54
|
+
const env = options.env ?? process.env;
|
|
55
|
+
let agentId = "2026000116664809";
|
|
56
|
+
try {
|
|
57
|
+
const config = JSON.parse(await readFile(resolve(options.appDir ?? await paymentAppRoot(), "config", "kya.json"), "utf8"));
|
|
58
|
+
const id = config?.kyaid;
|
|
59
|
+
if (typeof id === "string" && id.trim()) agentId = id.trim();
|
|
60
|
+
} catch {
|
|
61
|
+
}
|
|
62
|
+
const agentPid = env.AI_PAY_AGENT_PID?.trim();
|
|
63
|
+
return { ...agentPid ? { bizContext: JSON.stringify({ cashierScene: "agentPay", agentPid, agentId }) } : {} };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/server/tools/index.ts
|
|
67
|
+
function createAiPaymentTools(options = {}) {
|
|
68
|
+
const tool = createTool({
|
|
69
|
+
id: "createPaymentOrder",
|
|
70
|
+
description: "\u5C55\u793A\u6D4B\u8BD5\u8D2D\u4E70\u5546\u54C1\u7684\u5F85\u652F\u4ED8\u5361\uFF0C\u56FA\u5B9A 0.01 \u5143\u3002\u7528\u6237\u70B9\u51FB\u53BB\u652F\u4ED8\u540E\u521B\u5355\u5E76\u62C9\u8D77\u6536\u94F6\u53F0\u3002",
|
|
71
|
+
inputSchema: z2.object({}),
|
|
72
|
+
execute: async (_args, execCtx) => {
|
|
73
|
+
const reqCtx = requireToolRequestContext(execCtx, "createPaymentOrder");
|
|
74
|
+
const config = await (options.loadConfig ?? loadPaymentConfig)();
|
|
75
|
+
const userId = reqCtx.identity?.source !== "anonymous" ? reqCtx.identity?.userId : void 0;
|
|
76
|
+
const buyerId = userId && /^2088\d+$/.test(userId) ? userId : "2088052281674865";
|
|
77
|
+
const facts = {
|
|
78
|
+
subject: "\u6D4B\u8BD5\u8D2D\u4E70\u5546\u54C1",
|
|
79
|
+
totalAmountText: "0.01",
|
|
80
|
+
payState: "created",
|
|
81
|
+
...buyerId ? { buyerId } : {},
|
|
82
|
+
...config.bizContext ? { bizContext: config.bizContext } : {}
|
|
83
|
+
};
|
|
84
|
+
return { ...facts, cards: [{ cardType: "payment-order", data: facts, note: "\u6D4B\u8BD5\u8D2D\u4E70\u5546\u54C1 0.01 \u5143\uFF0C\u7B49\u5F85\u7528\u6237\u70B9\u51FB\u652F\u4ED8" }] };
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return { createPaymentOrder: withCardEmissionHint(tool) };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/server/actions/index.ts
|
|
91
|
+
import { z as z3 } from "zod";
|
|
92
|
+
var DEMO_TOTAL_AMOUNT_TEXT = "0.01";
|
|
93
|
+
var DEMO_SUBJECT = "\u6D4B\u8BD5\u8D2D\u4E70\u5546\u54C1";
|
|
94
|
+
function createAiPaymentActions() {
|
|
95
|
+
const reportAction = {
|
|
96
|
+
actionId: "ai-payment.report-payment-result",
|
|
97
|
+
moduleId: "ai-payment",
|
|
98
|
+
cardType: "payment-order",
|
|
99
|
+
label: "\u652F\u4ED8\u7ED3\u679C\u4E0A\u62A5",
|
|
100
|
+
singleUse: true,
|
|
101
|
+
hidden: true,
|
|
102
|
+
kind: "moduleAction",
|
|
103
|
+
inputSchema: z3.object({ status: z3.enum(["success", "cancel", "failure", "unavailable"]), paymentRef: z3.string().optional() }),
|
|
104
|
+
async execute(input, _reqCtx) {
|
|
105
|
+
void _reqCtx;
|
|
106
|
+
const payStateMap = { success: "paid", cancel: "cancelled", failure: "failed", unavailable: "created" };
|
|
107
|
+
const textMap = {
|
|
108
|
+
success: "\u6536\u94F6\u53F0\u8FD4\u56DE\u652F\u4ED8\u6210\u529F",
|
|
109
|
+
cancel: "\u5DF2\u53D6\u6D88\u652F\u4ED8\uFF0C\u53EF\u91CD\u65B0\u53D1\u8D77",
|
|
110
|
+
failure: "\u6536\u94F6\u53F0\u672A\u8FD4\u56DE\u652F\u4ED8\u6210\u529F\uFF0C\u8BF7\u52FF\u91CD\u590D\u521B\u5EFA\u8BA2\u5355",
|
|
111
|
+
unavailable: "\u8BF7\u5728\u652F\u4ED8\u5B9D\u58F3\u73AF\u5883\u5185\u5B8C\u6210\u652F\u4ED8"
|
|
112
|
+
};
|
|
113
|
+
const data = {
|
|
114
|
+
...input.paymentRef ? { tradeNo: input.paymentRef } : {},
|
|
115
|
+
subject: DEMO_SUBJECT,
|
|
116
|
+
totalAmountText: DEMO_TOTAL_AMOUNT_TEXT,
|
|
117
|
+
payState: payStateMap[input.status]
|
|
118
|
+
};
|
|
119
|
+
return { text: textMap[input.status], update: { data } };
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
return { "ai-payment.report-payment-result": reportAction };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/server/ports.ts
|
|
126
|
+
import { isExpectedProviderAbsence } from "@tbox.cn/app-contracts";
|
|
127
|
+
import { mallScopeOf } from "@tbox.cn/app-contracts-mall";
|
|
128
|
+
var PaymentNotConfiguredError = class extends Error {
|
|
129
|
+
constructor(message) {
|
|
130
|
+
super(message);
|
|
131
|
+
this.name = "PaymentNotConfiguredError";
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
async function resolvePaymentPort(reqCtx, ctx) {
|
|
135
|
+
try {
|
|
136
|
+
const resolved = await ctx.integrations?.resolveService?.({
|
|
137
|
+
service: PAYMENT_SERVICE,
|
|
138
|
+
scope: mallScopeOf(reqCtx),
|
|
139
|
+
request: reqCtx
|
|
140
|
+
});
|
|
141
|
+
if (!resolved) throw new PaymentNotConfiguredError("payment.main \u69FD\u672A\u914D\u7F6E\uFF08integrations registry\uFF09");
|
|
142
|
+
return resolved;
|
|
143
|
+
} catch (e) {
|
|
144
|
+
if (isExpectedProviderAbsence(e)) {
|
|
145
|
+
throw new PaymentNotConfiguredError("\u652F\u4ED8\u670D\u52A1\u672A\u914D\u7F6E\u6216\u5B9E\u4F8B\u7F3A\u5931");
|
|
146
|
+
}
|
|
147
|
+
throw e;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/server/index.ts
|
|
152
|
+
var tools = createAiPaymentTools();
|
|
153
|
+
var cards = { "payment-order": meta_default };
|
|
154
|
+
var serverModule = {
|
|
155
|
+
declaration: { moduleId: "ai-payment" },
|
|
156
|
+
cards,
|
|
157
|
+
skills: Object.fromEntries((await loadModuleSkills("@tbox.cn/app-module-ai-payment", import.meta.url)).map((s) => [s.name, s])),
|
|
158
|
+
preTools: {},
|
|
159
|
+
register(ctx) {
|
|
160
|
+
for (const tool of Object.values(tools)) ctx.tools.register(tool);
|
|
161
|
+
ctx.cards.registerMap(cards);
|
|
162
|
+
const actions = createAiPaymentActions();
|
|
163
|
+
for (const action of Object.values(actions)) ctx.cardActions.register(action);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
var server_default = serverModule;
|
|
167
|
+
|
|
168
|
+
export {
|
|
169
|
+
createAiPaymentTools,
|
|
170
|
+
createAiPaymentActions,
|
|
171
|
+
PaymentNotConfiguredError,
|
|
172
|
+
resolvePaymentPort,
|
|
173
|
+
serverModule,
|
|
174
|
+
server_default
|
|
175
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CardComponent } from '@tbox.cn/app-contracts';
|
|
2
|
+
import { ClientContext } from '@tbox.cn/app-agent-sdk-client';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* module-ai-payment 客户端入口(与服务端 serverModule 对称)。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare const clientModule: {
|
|
9
|
+
cards: {
|
|
10
|
+
readonly 'payment-order': CardComponent;
|
|
11
|
+
};
|
|
12
|
+
register(ctx: ClientContext): void;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { clientModule, clientModule as default };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// src/client/index.ts
|
|
2
|
+
import "../ai-payment-PNOA2J6W.css";
|
|
3
|
+
|
|
4
|
+
// src/client/cards/payment-order/index.tsx
|
|
5
|
+
import { useEffect, useRef, useState } from "react";
|
|
6
|
+
import { callJsapi, isInAlipayShell, mapPaymentResultCode, markSelfRendering, sendCardAction } from "@tbox.cn/app-agent-sdk-client";
|
|
7
|
+
|
|
8
|
+
// src/client/payment.ts
|
|
9
|
+
import { callCapability } from "@tbox.cn/app-agent-sdk-client";
|
|
10
|
+
async function createPaymentOrder(buyerId) {
|
|
11
|
+
const response = await callCapability("app.callContainer", {
|
|
12
|
+
path: "/api/alipay/aipay/create",
|
|
13
|
+
method: "POST",
|
|
14
|
+
dataType: "json",
|
|
15
|
+
header: { "Content-Type": "application/json", "X-API-Token": "fortest", "X-Alipay-Service": "pay-demo" },
|
|
16
|
+
data: { buyerId }
|
|
17
|
+
});
|
|
18
|
+
if (response?.statusCode !== 200 || typeof response.data?.tradeNo !== "string" || !response.data.tradeNo.trim()) {
|
|
19
|
+
throw new Error("\u4F53\u9A8C\u8BA2\u5355\u521B\u5EFA\u5931\u8D25");
|
|
20
|
+
}
|
|
21
|
+
return { tradeNo: response.data.tradeNo };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/client/cards/payment-order/index.tsx
|
|
25
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
26
|
+
function PaymentOrderCard(props) {
|
|
27
|
+
const { data, cardId, isHistory, actionTokens } = props;
|
|
28
|
+
const [pending, setPending] = useState(false);
|
|
29
|
+
const [cashier, setCashier] = useState(false);
|
|
30
|
+
const [paid, setPaid] = useState(false);
|
|
31
|
+
const [message, setMessage] = useState("");
|
|
32
|
+
const tradeNo = useRef(data.tradeNo);
|
|
33
|
+
const busy = useRef(false);
|
|
34
|
+
const cashierRef = useRef(null);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!cashier) return;
|
|
37
|
+
const dialog = cashierRef.current;
|
|
38
|
+
dialog?.showModal();
|
|
39
|
+
return () => dialog?.close();
|
|
40
|
+
}, [cashier]);
|
|
41
|
+
const disabled = isHistory || paid || data.payState === "paid" || data.payState === "paying" || pending;
|
|
42
|
+
const report = (status) => {
|
|
43
|
+
if (status === "success") setPaid(true);
|
|
44
|
+
const token = actionTokens?.["ai-payment.report-payment-result"];
|
|
45
|
+
if (token) sendCardAction(cardId, { type: "moduleAction", actionId: "ai-payment.report-payment-result", contextToken: token }, { status, ...tradeNo.current ? { paymentRef: tradeNo.current } : {} });
|
|
46
|
+
};
|
|
47
|
+
const pay = async () => {
|
|
48
|
+
if (disabled || busy.current) return;
|
|
49
|
+
if (!isInAlipayShell()) {
|
|
50
|
+
setCashier(true);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
busy.current = true;
|
|
54
|
+
setPending(true);
|
|
55
|
+
setMessage("");
|
|
56
|
+
try {
|
|
57
|
+
if (!data.bizContext) throw new Error("AI \u652F\u4ED8\u5546\u5BB6\u914D\u7F6E\u7F3A\u5931");
|
|
58
|
+
if (!data.buyerId || !/^2088\d+$/.test(data.buyerId)) throw new Error("\u7F3A\u5C11\u6709\u6548\u7684\u4E70\u5BB6\u652F\u4ED8\u5B9D\u8EAB\u4EFD");
|
|
59
|
+
if (!tradeNo.current) {
|
|
60
|
+
const order = await createPaymentOrder(data.buyerId);
|
|
61
|
+
if (!order?.tradeNo) throw new Error("\u4F53\u9A8C\u8BA2\u5355\u521B\u5EFA\u5931\u8D25");
|
|
62
|
+
tradeNo.current = order.tradeNo;
|
|
63
|
+
}
|
|
64
|
+
const result = await callJsapi("tradePay", { tradeNO: tradeNo.current, bizContext: data.bizContext });
|
|
65
|
+
console.info("[ai-payment] tradePay result:", result);
|
|
66
|
+
const status = mapPaymentResultCode(result?.resultCode);
|
|
67
|
+
if (status === "failure") {
|
|
68
|
+
sendCardAction(cardId, { type: "toast", content: "\u652F\u4ED8\u7ED3\u679C\u672A\u786E\u8BA4\uFF0C\u8BF7\u52FF\u91CD\u590D\u4E0B\u5355", tone: "error" });
|
|
69
|
+
} else {
|
|
70
|
+
setMessage(status === "success" ? "\u6536\u94F6\u53F0\u8FD4\u56DE\u652F\u4ED8\u6210\u529F" : "\u5DF2\u53D6\u6D88\u652F\u4ED8");
|
|
71
|
+
}
|
|
72
|
+
if (status === "success") report(status);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
sendCardAction(cardId, { type: "toast", content: error instanceof Error ? error.message : "\u652F\u4ED8\u8C03\u7528\u5931\u8D25\uFF0C\u8BF7\u52FF\u91CD\u590D\u4E0B\u5355", tone: "error" });
|
|
75
|
+
} finally {
|
|
76
|
+
busy.current = false;
|
|
77
|
+
setPending(false);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const completed = paid || data.payState === "paid";
|
|
81
|
+
return /* @__PURE__ */ jsxs("div", { className: "ap-card", "data-ap-pay-state": paid ? "paid" : data.payState, children: [
|
|
82
|
+
/* @__PURE__ */ jsxs("div", { className: "ap-order-heading", children: [
|
|
83
|
+
/* @__PURE__ */ jsx("span", { children: "\u5F85\u652F\u4ED8\u8BA2\u5355" }),
|
|
84
|
+
/* @__PURE__ */ jsx("span", { className: "ap-order-status", children: completed ? "\u5DF2\u652F\u4ED8" : "\u5F85\u4ED8\u6B3E" })
|
|
85
|
+
] }),
|
|
86
|
+
/* @__PURE__ */ jsx("ul", { className: "ap-order-list", "aria-label": "\u652F\u4ED8\u8BA2\u5355", children: /* @__PURE__ */ jsxs("li", { className: "ap-order-item", children: [
|
|
87
|
+
/* @__PURE__ */ jsx("span", { className: "ap-order-icon", "aria-hidden": "true", children: /* @__PURE__ */ jsxs("svg", { viewBox: "0 0 32 32", fill: "none", children: [
|
|
88
|
+
/* @__PURE__ */ jsx("path", { d: "M8 11h16l2 16H6l2-16Z", fill: "currentColor", opacity: ".2" }),
|
|
89
|
+
/* @__PURE__ */ jsx("path", { d: "M12 12V9a4 4 0 0 1 8 0v3M8 11h16l2 16H6l2-16Z", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round" })
|
|
90
|
+
] }) }),
|
|
91
|
+
/* @__PURE__ */ jsxs("div", { className: "ap-order-details", children: [
|
|
92
|
+
/* @__PURE__ */ jsx("div", { className: "ap-card__title", children: data.subject }),
|
|
93
|
+
/* @__PURE__ */ jsx("p", { className: "ap-order-description", children: "\u6807\u51C6\u6B3E \xB7 1 \u4EF6" })
|
|
94
|
+
] }),
|
|
95
|
+
/* @__PURE__ */ jsxs("span", { className: "ap-order-amount", children: [
|
|
96
|
+
"\xA5",
|
|
97
|
+
data.totalAmountText
|
|
98
|
+
] })
|
|
99
|
+
] }) }),
|
|
100
|
+
/* @__PURE__ */ jsx("button", { type: "button", className: "ap-card__btn ap-order-pay", disabled, onClick: () => void pay(), children: completed ? "\u5DF2\u652F\u4ED8" : pending ? "\u652F\u4ED8\u5904\u7406\u4E2D\u2026" : "\u53BB\u652F\u4ED8" }),
|
|
101
|
+
message && /* @__PURE__ */ jsx("p", { role: "status", className: "ap-card__hint", children: message }),
|
|
102
|
+
cashier && /* @__PURE__ */ jsxs("dialog", { ref: cashierRef, className: "ap-cashier", "aria-label": "\u6A21\u62DF\u652F\u4ED8\u5B9D\u6536\u94F6\u53F0", onCancel: () => setCashier(false), children: [
|
|
103
|
+
/* @__PURE__ */ jsx("p", { className: "ap-cashier-notice", children: "\u5546\u5708\u52A9\u624B\u6B63\u5728\u4E3A\u60A8\u4E0B\u5355\uFF0C\u8BF7\u786E\u8BA4" }),
|
|
104
|
+
/* @__PURE__ */ jsxs("div", { className: "ap-cashier-panel", children: [
|
|
105
|
+
/* @__PURE__ */ jsxs("header", { className: "ap-cashier-header", children: [
|
|
106
|
+
/* @__PURE__ */ jsx("button", { type: "button", className: "ap-cashier-close", "aria-label": "\u5173\u95ED\u6536\u94F6\u53F0", onClick: () => setCashier(false), children: "\xD7" }),
|
|
107
|
+
/* @__PURE__ */ jsx("span", { className: "ap-cashier-brand", children: "\u652F AI\u4ED8" })
|
|
108
|
+
] }),
|
|
109
|
+
/* @__PURE__ */ jsxs("div", { className: "ap-cashier-summary", children: [
|
|
110
|
+
/* @__PURE__ */ jsx("p", { className: "ap-cashier-merchant", children: "\u793A\u4F8B\u5546\u5BB6" }),
|
|
111
|
+
/* @__PURE__ */ jsxs("div", { className: "ap-cashier-amount", children: [
|
|
112
|
+
"\xA5 ",
|
|
113
|
+
data.totalAmountText
|
|
114
|
+
] }),
|
|
115
|
+
/* @__PURE__ */ jsx("p", { className: "ap-cashier-product", children: data.subject })
|
|
116
|
+
] }),
|
|
117
|
+
/* @__PURE__ */ jsxs("div", { className: "ap-cashier-method", children: [
|
|
118
|
+
/* @__PURE__ */ jsx("span", { className: "ap-cashier-method-icon", "aria-hidden": "true", children: "\u652F" }),
|
|
119
|
+
/* @__PURE__ */ jsx("span", { children: "\u652F\u4ED8\u5B9D\u4F59\u989D\uFF08\u6A21\u62DF\uFF09" })
|
|
120
|
+
] }),
|
|
121
|
+
/* @__PURE__ */ jsx("button", { type: "button", className: "ap-card__btn ap-cashier-pay", "data-ap-confirm": true, disabled, onClick: () => {
|
|
122
|
+
setCashier(false);
|
|
123
|
+
setMessage("\u6A21\u62DF\u652F\u4ED8\u6210\u529F");
|
|
124
|
+
report("success");
|
|
125
|
+
}, children: "\u4ED8\u6B3E" }),
|
|
126
|
+
/* @__PURE__ */ jsx("p", { className: "ap-cashier-disclaimer", children: "\u6A21\u62DF\u6536\u94F6\u53F0\uFF0C\u4EC5\u4F53\u9A8C\uFF0C\u4E0D\u4F1A\u5B9E\u9645\u6263\u6B3E" })
|
|
127
|
+
] })
|
|
128
|
+
] })
|
|
129
|
+
] });
|
|
130
|
+
}
|
|
131
|
+
var payment_order_default = markSelfRendering(PaymentOrderCard);
|
|
132
|
+
|
|
133
|
+
// src/client/index.ts
|
|
134
|
+
var cards = {
|
|
135
|
+
"payment-order": payment_order_default
|
|
136
|
+
};
|
|
137
|
+
var clientModule = {
|
|
138
|
+
cards,
|
|
139
|
+
register(ctx) {
|
|
140
|
+
ctx.cards.registerMap(cards);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
var client_default = clientModule;
|
|
144
|
+
export {
|
|
145
|
+
clientModule,
|
|
146
|
+
client_default as default
|
|
147
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { AiPaymentTools, PaymentNotConfiguredError, createAiPaymentActions, createAiPaymentTools, resolvePaymentPort, default as serverModule } from './server/index.js';
|
|
2
|
+
import '@tbox.cn/app-sdk-server';
|
|
3
|
+
import '@tbox.cn/app-contracts';
|
|
4
|
+
import 'zod';
|
|
5
|
+
import '@tbox.cn/app-agent-sdk-server';
|
|
6
|
+
import '@tbox.cn/app-contracts-mall';
|
|
7
|
+
import '@mastra/core/tools';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PaymentNotConfiguredError,
|
|
3
|
+
createAiPaymentActions,
|
|
4
|
+
createAiPaymentTools,
|
|
5
|
+
resolvePaymentPort,
|
|
6
|
+
serverModule
|
|
7
|
+
} from "./chunk-ERHTVWF5.js";
|
|
8
|
+
export {
|
|
9
|
+
PaymentNotConfiguredError,
|
|
10
|
+
createAiPaymentActions,
|
|
11
|
+
createAiPaymentTools,
|
|
12
|
+
resolvePaymentPort,
|
|
13
|
+
serverModule
|
|
14
|
+
};
|