@h-ai/payment 0.1.0-alpha5

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [2026] [idealworld.group]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # @h-ai/payment
2
+
3
+ 统一支付模块,支持微信支付、支付宝和 Stripe,提供 Provider 模式的后端支付处理与多端客户端调起能力。
4
+
5
+ ## 支持的 Provider
6
+
7
+ | Provider | 支付场景 |
8
+ | -------- | -------------------------------- |
9
+ | 微信支付 | JSAPI / H5 / App / Native (扫码) |
10
+ | 支付宝 | JSAPI / H5 / App / Native |
11
+ | Stripe | Checkout Session(国际支付) |
12
+
13
+ ## 依赖
14
+
15
+ - `@h-ai/audit` — 审计日志(支付操作自动审计),可选;已初始化时自动写入审计日志
16
+
17
+ ## 快速开始
18
+
19
+ ### Node.js 服务端
20
+
21
+ ```typescript
22
+ import { payment } from '@h-ai/payment'
23
+
24
+ // 初始化(会自动注册可用的 Provider)
25
+ await payment.init({
26
+ wechat: { mchId: '...', apiV3Key: '...', serialNo: '...', privateKey: '...', appId: '...' },
27
+ alipay: { appId: '...', privateKey: '...', alipayPublicKey: '...' },
28
+ })
29
+
30
+ // 创建订单
31
+ const result = await payment.createOrder('wechat', {
32
+ orderNo: 'ORD001',
33
+ amount: 100,
34
+ description: '测试商品',
35
+ tradeType: 'jsapi',
36
+ userId: 'openid-xxx',
37
+ notifyUrl: 'https://api.example.com/payment/notify/wechat',
38
+ })
39
+
40
+ // 查询订单
41
+ const query = await payment.queryOrder('wechat', 'ORD001')
42
+
43
+ await payment.close()
44
+ ```
45
+
46
+ ### 浏览器客户端
47
+
48
+ ```typescript
49
+ import { invokePayment } from '@h-ai/payment/client'
50
+
51
+ // 使用服务端返回的订单数据调起支付
52
+ await invokePayment(orderData)
53
+ ```
54
+
55
+ ## API 契约
56
+
57
+ 通过 `@h-ai/payment/api` 导出端点定义,与 `@h-ai/api-client` 和 `@h-ai/kit` 配合使用:
58
+
59
+ ```typescript
60
+ import { paymentEndpoints } from '@h-ai/payment/api'
61
+ ```
62
+
63
+ ## 配置
64
+
65
+ ```typescript
66
+ await payment.init({
67
+ wechat: {
68
+ mchId: '商户号',
69
+ apiV3Key: 'API v3 密钥',
70
+ serialNo: '证书序列号',
71
+ privateKey: '私钥内容',
72
+ appId: '应用 ID',
73
+ },
74
+ alipay: {
75
+ appId: '应用 ID',
76
+ privateKey: '私钥',
77
+ alipayPublicKey: '支付宝公钥',
78
+ signType: 'RSA2', // 可选,默认 RSA2
79
+ sandbox: false, // 可选,默认 false
80
+ },
81
+ stripe: {
82
+ secretKey: 'sk_xxx',
83
+ webhookSecret: 'whsec_xxx',
84
+ },
85
+ })
86
+ ```
87
+
88
+ 只配置需要的 Provider 即可,未配置的 Provider 不会注册。
89
+
90
+ ## 审计日志
91
+
92
+ 关键支付操作执行成功后会自动写入审计日志(依赖 `@h-ai/audit`),无需额外配置。审计写入失败仅输出 warn 日志,不影响支付操作。
93
+
94
+ | 操作 | `action` | 记录内容 |
95
+ | -------- | ---------------- | --------------------------------------- |
96
+ | 创建订单 | `create_order` | provider、amount、tradeType |
97
+ | 支付回调 | `payment_notify` | provider、transactionId、status、amount |
98
+ | 退款 | `refund` | provider、refundNo、amount |
99
+ | 关闭订单 | `close_order` | provider |
100
+
101
+ > 订单查询(`queryOrder`)为只读操作,不写审计日志。
102
+
103
+ ## 错误处理
104
+
105
+ ```typescript
106
+ const result = await payment.createOrder('wechat', orderInput)
107
+ if (!result.success) {
108
+ switch (result.error.code) {
109
+ case HaiPaymentError.PROVIDER_NOT_FOUND:
110
+ // Provider 未注册
111
+ break
112
+ case HaiPaymentError.CREATE_ORDER_FAILED:
113
+ // 创建订单失败
114
+ break
115
+ }
116
+ }
117
+ ```
118
+
119
+ 常用错误码:
120
+
121
+ | 错误码 | code | 说明 |
122
+ | -------------------------------------- | ----------------- | --------------- |
123
+ | `HaiPaymentError.CREATE_ORDER_FAILED` | `hai:payment:001` | 创建订单失败 |
124
+ | `HaiPaymentError.NOT_INITIALIZED` | `hai:payment:010` | 模块未初始化 |
125
+ | `HaiPaymentError.PROVIDER_NOT_FOUND` | `hai:payment:030` | Provider 未注册 |
126
+ | `HaiPaymentError.NOTIFY_VERIFY_FAILED` | `hai:payment:050` | 回调验签失败 |
127
+ | `HaiPaymentError.CONFIG_ERROR` | `hai:payment:070` | 配置无效 |
128
+
129
+ ## 测试
130
+
131
+ ```bash
132
+ pnpm --filter @h-ai/payment test
133
+ ```
134
+
135
+ ## License
136
+
137
+ Apache-2.0
@@ -0,0 +1,211 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * @h-ai/payment/api — 支付 API 契约
5
+ *
6
+ * 定义支付相关的 API 端点契约(EndpointDef),
7
+ * 供服务端 kit.fromContract() 和客户端 api.call() 共享。
8
+ *
9
+ * @module payment-api
10
+ */
11
+
12
+ /** 创建订单入参 Schema */
13
+ declare const CreatePaymentOrderInputSchema: z.ZodObject<{
14
+ provider: z.ZodEnum<{
15
+ wechat: "wechat";
16
+ alipay: "alipay";
17
+ stripe: "stripe";
18
+ }>;
19
+ amount: z.ZodNumber;
20
+ description: z.ZodString;
21
+ tradeType: z.ZodEnum<{
22
+ jsapi: "jsapi";
23
+ h5: "h5";
24
+ app: "app";
25
+ native: "native";
26
+ mini_program: "mini_program";
27
+ }>;
28
+ userId: z.ZodOptional<z.ZodString>;
29
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
30
+ }, z.core.$strip>;
31
+ /** 创建订单出参 Schema */
32
+ declare const CreatePaymentOrderOutputSchema: z.ZodObject<{
33
+ provider: z.ZodString;
34
+ tradeType: z.ZodString;
35
+ clientParams: z.ZodRecord<z.ZodString, z.ZodUnknown>;
36
+ prepayId: z.ZodOptional<z.ZodString>;
37
+ }, z.core.$strip>;
38
+ /** 查询订单出参 Schema */
39
+ declare const QueryOrderOutputSchema: z.ZodObject<{
40
+ orderNo: z.ZodString;
41
+ transactionId: z.ZodOptional<z.ZodString>;
42
+ status: z.ZodEnum<{
43
+ pending: "pending";
44
+ paid: "paid";
45
+ closed: "closed";
46
+ refunded: "refunded";
47
+ failed: "failed";
48
+ }>;
49
+ amount: z.ZodNumber;
50
+ paidAt: z.ZodOptional<z.ZodString>;
51
+ }, z.core.$strip>;
52
+ /** 退款入参 Schema */
53
+ declare const RefundInputSchema: z.ZodObject<{
54
+ provider: z.ZodString;
55
+ orderNo: z.ZodString;
56
+ refundNo: z.ZodString;
57
+ amount: z.ZodNumber;
58
+ reason: z.ZodOptional<z.ZodString>;
59
+ }, z.core.$strip>;
60
+ /** 退款出参 Schema */
61
+ declare const RefundOutputSchema: z.ZodObject<{
62
+ refundNo: z.ZodString;
63
+ refundId: z.ZodString;
64
+ status: z.ZodEnum<{
65
+ success: "success";
66
+ failed: "failed";
67
+ processing: "processing";
68
+ }>;
69
+ }, z.core.$strip>;
70
+ type CreatePaymentOrderInput = z.infer<typeof CreatePaymentOrderInputSchema>;
71
+ type CreatePaymentOrderOutput = z.infer<typeof CreatePaymentOrderOutputSchema>;
72
+ type QueryOrderOutput = z.infer<typeof QueryOrderOutputSchema>;
73
+ type RefundInput = z.infer<typeof RefundInputSchema>;
74
+ type RefundOutput = z.infer<typeof RefundOutputSchema>;
75
+ /**
76
+ * 支付 API 端点契约
77
+ */
78
+ declare const paymentEndpoints: {
79
+ /** 创建支付订单 */
80
+ readonly createOrder: {
81
+ method: "POST";
82
+ path: string;
83
+ input: z.ZodObject<{
84
+ provider: z.ZodEnum<{
85
+ wechat: "wechat";
86
+ alipay: "alipay";
87
+ stripe: "stripe";
88
+ }>;
89
+ amount: z.ZodNumber;
90
+ description: z.ZodString;
91
+ tradeType: z.ZodEnum<{
92
+ jsapi: "jsapi";
93
+ h5: "h5";
94
+ app: "app";
95
+ native: "native";
96
+ mini_program: "mini_program";
97
+ }>;
98
+ userId: z.ZodOptional<z.ZodString>;
99
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
100
+ }, z.core.$strip>;
101
+ output: z.ZodObject<{
102
+ provider: z.ZodString;
103
+ tradeType: z.ZodString;
104
+ clientParams: z.ZodRecord<z.ZodString, z.ZodUnknown>;
105
+ prepayId: z.ZodOptional<z.ZodString>;
106
+ }, z.core.$strip>;
107
+ requireAuth: true;
108
+ meta: {
109
+ summary: string;
110
+ tags: string[];
111
+ };
112
+ };
113
+ /** 查询订单状态 */
114
+ readonly queryOrder: {
115
+ method: "GET";
116
+ path: string;
117
+ input: z.ZodObject<{
118
+ orderNo: z.ZodString;
119
+ }, z.core.$strip>;
120
+ output: z.ZodObject<{
121
+ orderNo: z.ZodString;
122
+ transactionId: z.ZodOptional<z.ZodString>;
123
+ status: z.ZodEnum<{
124
+ pending: "pending";
125
+ paid: "paid";
126
+ closed: "closed";
127
+ refunded: "refunded";
128
+ failed: "failed";
129
+ }>;
130
+ amount: z.ZodNumber;
131
+ paidAt: z.ZodOptional<z.ZodString>;
132
+ }, z.core.$strip>;
133
+ requireAuth: true;
134
+ meta: {
135
+ summary: string;
136
+ tags: string[];
137
+ };
138
+ };
139
+ /** 微信支付回调 */
140
+ readonly notifyWechat: {
141
+ method: "POST";
142
+ path: string;
143
+ input: z.ZodObject<{}, z.core.$strip>;
144
+ output: z.ZodObject<{
145
+ code: z.ZodString;
146
+ message: z.ZodString;
147
+ }, z.core.$strip>;
148
+ requireAuth: false;
149
+ meta: {
150
+ summary: string;
151
+ tags: string[];
152
+ };
153
+ };
154
+ /** 支付宝回调 */
155
+ readonly notifyAlipay: {
156
+ method: "POST";
157
+ path: string;
158
+ input: z.ZodObject<{}, z.core.$strip>;
159
+ output: z.ZodObject<{
160
+ code: z.ZodString;
161
+ message: z.ZodString;
162
+ }, z.core.$strip>;
163
+ requireAuth: false;
164
+ meta: {
165
+ summary: string;
166
+ tags: string[];
167
+ };
168
+ };
169
+ /** Stripe Webhook */
170
+ readonly notifyStripe: {
171
+ method: "POST";
172
+ path: string;
173
+ input: z.ZodObject<{}, z.core.$strip>;
174
+ output: z.ZodObject<{
175
+ received: z.ZodBoolean;
176
+ }, z.core.$strip>;
177
+ requireAuth: false;
178
+ meta: {
179
+ summary: string;
180
+ tags: string[];
181
+ };
182
+ };
183
+ /** 发起退款 */
184
+ readonly refund: {
185
+ method: "POST";
186
+ path: string;
187
+ input: z.ZodObject<{
188
+ provider: z.ZodString;
189
+ orderNo: z.ZodString;
190
+ refundNo: z.ZodString;
191
+ amount: z.ZodNumber;
192
+ reason: z.ZodOptional<z.ZodString>;
193
+ }, z.core.$strip>;
194
+ output: z.ZodObject<{
195
+ refundNo: z.ZodString;
196
+ refundId: z.ZodString;
197
+ status: z.ZodEnum<{
198
+ success: "success";
199
+ failed: "failed";
200
+ processing: "processing";
201
+ }>;
202
+ }, z.core.$strip>;
203
+ requireAuth: true;
204
+ meta: {
205
+ summary: string;
206
+ tags: string[];
207
+ };
208
+ };
209
+ };
210
+
211
+ export { type CreatePaymentOrderInput, CreatePaymentOrderInputSchema, type CreatePaymentOrderOutput, CreatePaymentOrderOutputSchema, type QueryOrderOutput, QueryOrderOutputSchema, type RefundInput, RefundInputSchema, type RefundOutput, RefundOutputSchema, paymentEndpoints };
@@ -0,0 +1,96 @@
1
+ import { z } from 'zod';
2
+
3
+ // src/api/payment-api-contract.ts
4
+ var CreatePaymentOrderInputSchema = z.object({
5
+ provider: z.enum(["wechat", "alipay", "stripe"]),
6
+ amount: z.number().int().min(1),
7
+ description: z.string().min(1),
8
+ tradeType: z.enum(["jsapi", "h5", "app", "native", "mini_program"]),
9
+ userId: z.string().optional(),
10
+ metadata: z.record(z.string(), z.string()).optional()
11
+ });
12
+ var CreatePaymentOrderOutputSchema = z.object({
13
+ provider: z.string(),
14
+ tradeType: z.string(),
15
+ clientParams: z.record(z.string(), z.unknown()),
16
+ prepayId: z.string().optional()
17
+ });
18
+ var QueryOrderOutputSchema = z.object({
19
+ orderNo: z.string(),
20
+ transactionId: z.string().optional(),
21
+ status: z.enum(["pending", "paid", "closed", "refunded", "failed"]),
22
+ amount: z.number(),
23
+ paidAt: z.string().optional()
24
+ });
25
+ var RefundInputSchema = z.object({
26
+ provider: z.string(),
27
+ orderNo: z.string(),
28
+ refundNo: z.string(),
29
+ amount: z.number().int().min(1),
30
+ reason: z.string().optional()
31
+ });
32
+ var RefundOutputSchema = z.object({
33
+ refundNo: z.string(),
34
+ refundId: z.string(),
35
+ status: z.enum(["processing", "success", "failed"])
36
+ });
37
+ var paymentEndpoints = {
38
+ /** 创建支付订单 */
39
+ createOrder: {
40
+ method: "POST",
41
+ path: "/payment/create",
42
+ input: CreatePaymentOrderInputSchema,
43
+ output: CreatePaymentOrderOutputSchema,
44
+ requireAuth: true,
45
+ meta: { summary: "Create payment order", tags: ["payment"] }
46
+ },
47
+ /** 查询订单状态 */
48
+ queryOrder: {
49
+ method: "GET",
50
+ path: "/payment/query",
51
+ input: z.object({ orderNo: z.string() }),
52
+ output: QueryOrderOutputSchema,
53
+ requireAuth: true,
54
+ meta: { summary: "Query order status", tags: ["payment"] }
55
+ },
56
+ /** 微信支付回调 */
57
+ notifyWechat: {
58
+ method: "POST",
59
+ path: "/payment/notify/wechat",
60
+ input: z.object({}),
61
+ output: z.object({ code: z.string(), message: z.string() }),
62
+ requireAuth: false,
63
+ meta: { summary: "Wechat payment notification", tags: ["payment"] }
64
+ },
65
+ /** 支付宝回调 */
66
+ notifyAlipay: {
67
+ method: "POST",
68
+ path: "/payment/notify/alipay",
69
+ input: z.object({}),
70
+ output: z.object({ code: z.string(), message: z.string() }),
71
+ requireAuth: false,
72
+ meta: { summary: "Alipay payment notification", tags: ["payment"] }
73
+ },
74
+ /** Stripe Webhook */
75
+ notifyStripe: {
76
+ method: "POST",
77
+ path: "/payment/notify/stripe",
78
+ input: z.object({}),
79
+ output: z.object({ received: z.boolean() }),
80
+ requireAuth: false,
81
+ meta: { summary: "Stripe webhook", tags: ["payment"] }
82
+ },
83
+ /** 发起退款 */
84
+ refund: {
85
+ method: "POST",
86
+ path: "/payment/refund",
87
+ input: RefundInputSchema,
88
+ output: RefundOutputSchema,
89
+ requireAuth: true,
90
+ meta: { summary: "Request refund", tags: ["payment"] }
91
+ }
92
+ };
93
+
94
+ export { CreatePaymentOrderInputSchema, CreatePaymentOrderOutputSchema, QueryOrderOutputSchema, RefundInputSchema, RefundOutputSchema, paymentEndpoints };
95
+ //# sourceMappingURL=index.js.map
96
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/api/payment-api-contract.ts"],"names":[],"mappings":";;;AAcO,IAAM,6BAAA,GAAgC,EAAE,MAAA,CAAO;AAAA,EACpD,UAAU,CAAA,CAAE,IAAA,CAAK,CAAC,QAAA,EAAU,QAAA,EAAU,QAAQ,CAAC,CAAA;AAAA,EAC/C,QAAQ,CAAA,CAAE,MAAA,GAAS,GAAA,EAAI,CAAE,IAAI,CAAC,CAAA;AAAA,EAC9B,WAAA,EAAa,CAAA,CAAE,MAAA,EAAO,CAAE,IAAI,CAAC,CAAA;AAAA,EAC7B,SAAA,EAAW,EAAE,IAAA,CAAK,CAAC,SAAS,IAAA,EAAM,KAAA,EAAO,QAAA,EAAU,cAAc,CAAC,CAAA;AAAA,EAClE,MAAA,EAAQ,CAAA,CAAE,MAAA,EAAO,CAAE,QAAA,EAAS;AAAA,EAC5B,QAAA,EAAU,CAAA,CAAE,MAAA,CAAO,CAAA,CAAE,MAAA,IAAU,CAAA,CAAE,MAAA,EAAQ,CAAA,CAAE,QAAA;AAC7C,CAAC;AAGM,IAAM,8BAAA,GAAiC,EAAE,MAAA,CAAO;AAAA,EACrD,QAAA,EAAU,EAAE,MAAA,EAAO;AAAA,EACnB,SAAA,EAAW,EAAE,MAAA,EAAO;AAAA,EACpB,YAAA,EAAc,EAAE,MAAA,CAAO,CAAA,CAAE,QAAO,EAAG,CAAA,CAAE,SAAS,CAAA;AAAA,EAC9C,QA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;AAAA,IACZ,MAAA,EAAQ,MAAA;AAAA,IACR,IAAA,EAAM,wBAAA;AAAA,IACN,KAAA,EAAO,CAAA,CAAE,MAAA,CAAO,EAAE,CAAA;AAAA,IAClB,MAAA,EAAQ,EAAE,MAAA,CAAO,EAAE,UAAU,CAAA,CAAE,OAAA,IAAW,CAAA;AAAA,IAC1C,WAAA,EAAa,KAAA;AAAA,IACb,MAAM,EAAE,OAAA,EAAS,kBAAkB,IAAA,EAAM,CAAC,SAAS,CAAA;AAAE,GACvD;AAAA;AAAA,EAGA,MAAA,EAAQ;AAAA,IACN,MAAA,EAAQ,MAAA;AAAA,IACR,IAAA,EAAM,iBAAA;AAAA,IACN,KAAA,EAAO,iBAAA;AAAA,IACP,MAAA,EAAQ,kBAAA;AAAA,IACR,WAAA,EAAa,IAAA;AAAA,IACb,MAAM,EAAE,OAAA,EAAS,kBAAkB,IAAA,EAAM,CAAC,SAAS,CAAA;AAAE;AAEzD","file":"index.js","sourcesContent":["/**\n * @h-ai/payment/api — 支付 API 契约\n *\n * 定义支付相关的 API 端点契约(EndpointDef),\n * 供服务端 kit.fromContract() 和客户端 api.call() 共享。\n *\n * @module payment-api\n */\n\nimport { z } from 'zod'\n\n// ─── Schemas ───\n\n/** 创建订单入参 Schema */\nexport const CreatePaymentOrderInputSchema = z.object({\n provider: z.enum(['wechat', 'alipay', 'stripe']),\n amount: z.number().int().min(1),\n description: z.string().min(1),\n tradeType: z.enum(['jsapi', 'h5', 'app', 'native', 'mini_program']),\n userId: z.string().optional(),\n metadata: z.record(z.string(), z.string()).optional(),\n})\n\n/** 创建订单出参 Schema */\nexport const CreatePaymentOrderOutputSchema = z.object({\n provider: z.string(),\n tradeType: z.string(),\n clientParams: z.record(z.string(), z.unknown()),\n prepayId: z.string().optional(),\n})\n\n/** 查询订单出参 Schema */\nexport const QueryOrderOutputSchema = z.object({\n orderNo: z.string(),\n transactionId: z.string().optional(),\n status: z.enum(['pending', 'paid', 'closed', 'refunded', 'failed']),\n amount: z.number(),\n paidAt: z.string().optional(),\n})\n\n/** 退款入参 Schema */\nexport const RefundInputSchema = z.object({\n provider: z.string(),\n orderNo: z.string(),\n refundNo: z.string(),\n amount: z.number().int().min(1),\n reason: z.string().optional(),\n})\n\n/** 退款出参 Schema */\nexport const RefundOutputSchema = z.object({\n refundNo: z.string(),\n refundId: z.string(),\n status: z.enum(['processing', 'success', 'failed']),\n})\n\n// ─── 推导类型 ───\n\nexport type CreatePaymentOrderInput = z.infer<typeof CreatePaymentOrderInputSchema>\nexport type CreatePaymentOrderOutput = z.infer<typeof CreatePaymentOrderOutputSchema>\nexport type QueryOrderOutput = z.infer<typeof QueryOrderOutputSchema>\nexport type RefundInput = z.infer<typeof RefundInputSchema>\nexport type RefundOutput = z.infer<typeof RefundOutputSchema>\n\n// ─── EndpointDef(内联定义,避免循环依赖) ───\n\ninterface EndpointDef<TInput, TOutput> {\n method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'\n path: string\n input: z.ZodType<TInput>\n output: z.ZodType<TOutput>\n requireAuth?: boolean\n meta?: { summary?: string, tags?: string[] }\n}\n\n/**\n * 支付 API 端点契约\n */\nexport const paymentEndpoints = {\n /** 创建支付订单 */\n createOrder: {\n method: 'POST',\n path: '/payment/create',\n input: CreatePaymentOrderInputSchema,\n output: CreatePaymentOrderOutputSchema,\n requireAuth: true,\n meta: { summary: 'Create payment order', tags: ['payment'] },\n } satisfies EndpointDef<CreatePaymentOrderInput, CreatePaymentOrderOutput>,\n\n /** 查询订单状态 */\n queryOrder: {\n method: 'GET',\n path: '/payment/query',\n input: z.object({ orderNo: z.string() }),\n output: QueryOrderOutputSchema,\n requireAuth: true,\n meta: { summary: 'Query order status', tags: ['payment'] },\n } satisfies EndpointDef<{ orderNo: string }, QueryOrderOutput>,\n\n /** 微信支付回调 */\n notifyWechat: {\n method: 'POST',\n path: '/payment/notify/wechat',\n input: z.object({}),\n output: z.object({ code: z.string(), message: z.string() }),\n requireAuth: false,\n meta: { summary: 'Wechat payment notification', tags: ['payment'] },\n } satisfies EndpointDef<Record<string, never>, { code: string, message: string }>,\n\n /** 支付宝回调 */\n notifyAlipay: {\n method: 'POST',\n path: '/payment/notify/alipay',\n input: z.object({}),\n output: z.object({ code: z.string(), message: z.string() }),\n requireAuth: false,\n meta: { summary: 'Alipay payment notification', tags: ['payment'] },\n } satisfies EndpointDef<Record<string, never>, { code: string, message: string }>,\n\n /** Stripe Webhook */\n notifyStripe: {\n method: 'POST',\n path: '/payment/notify/stripe',\n input: z.object({}),\n output: z.object({ received: z.boolean() }),\n requireAuth: false,\n meta: { summary: 'Stripe webhook', tags: ['payment'] },\n } satisfies EndpointDef<Record<string, never>, { received: boolean }>,\n\n /** 发起退款 */\n refund: {\n method: 'POST',\n path: '/payment/refund',\n input: RefundInputSchema,\n output: RefundOutputSchema,\n requireAuth: true,\n meta: { summary: 'Request refund', tags: ['payment'] },\n } satisfies EndpointDef<RefundInput, RefundOutput>,\n} as const\n"]}