@harrylabsj/kiwi-merchant-cloud 0.12.32 → 0.12.33
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/app/a2a/inquiry-nonce-store.d.ts +12 -1
- package/app/a2a/inquiry-nonce-store.js +49 -17
- package/app/a2a/inquiry-nonce-store.js.map +1 -1
- package/app/a2a/negotiate.d.ts +30 -1
- package/app/a2a/negotiate.js +121 -32
- package/app/a2a/negotiate.js.map +1 -1
- package/app/a2a/node.d.ts +3 -0
- package/app/a2a/node.js +13 -3
- package/app/a2a/node.js.map +1 -1
- package/app/a2a/open-inquiry-session.d.ts +1 -0
- package/app/a2a/open-inquiry-session.js +3 -1
- package/app/a2a/open-inquiry-session.js.map +1 -1
- package/app/a2a/open-inquiry-verifier.js +2 -2
- package/app/a2a/open-inquiry-verifier.js.map +1 -1
- package/app/a2a/server/errors.d.ts +4 -3
- package/app/a2a/server/errors.js +10 -2
- package/app/a2a/server/errors.js.map +1 -1
- package/app/a2a/server/merchant-handler.d.ts +41 -1
- package/app/a2a/server/merchant-handler.js +259 -89
- package/app/a2a/server/merchant-handler.js.map +1 -1
- package/app/a2a/server/merchant-procurement.d.ts +90 -0
- package/app/a2a/server/merchant-procurement.js +75 -0
- package/app/a2a/server/merchant-procurement.js.map +1 -0
- package/app/a2a/server/merchant-product-facts.d.ts +2 -0
- package/app/a2a/server/merchant-product-facts.js.map +1 -1
- package/app/a2a/server/negotiation-approvals.d.ts +11 -0
- package/app/a2a/server/negotiation-approvals.js +43 -0
- package/app/a2a/server/negotiation-approvals.js.map +1 -1
- package/app/a2a/server/pipeline.js +4 -1
- package/app/a2a/server/pipeline.js.map +1 -1
- package/app/a2a/server/server.js +4 -3
- package/app/a2a/server/server.js.map +1 -1
- package/app/a2a/server/types.d.ts +2 -1
- package/app/agent/alipay-native-view.d.ts +27 -0
- package/app/agent/alipay-native-view.js +113 -0
- package/app/agent/alipay-native-view.js.map +1 -0
- package/app/agent/buyer/buyer-tools.d.ts +20 -0
- package/app/agent/buyer/buyer-tools.js +241 -48
- package/app/agent/buyer/buyer-tools.js.map +1 -1
- package/app/agent/buyer/types.d.ts +1 -1
- package/app/agent/buyer/types.js.map +1 -1
- package/app/agent/chat-tui.js +16 -1
- package/app/agent/chat-tui.js.map +1 -1
- package/app/agent/kernel-builder.d.ts +2 -0
- package/app/agent/kernel-builder.js +14 -0
- package/app/agent/kernel-builder.js.map +1 -1
- package/app/agent/kernel.d.ts +8 -0
- package/app/agent/kernel.js +67 -1
- package/app/agent/kernel.js.map +1 -1
- package/app/buyer-core/a2a-accept-terms.d.ts +11 -0
- package/app/buyer-core/a2a-accept-terms.js +28 -0
- package/app/buyer-core/a2a-accept-terms.js.map +1 -0
- package/app/buyer-core/a2a-knp.d.ts +3 -0
- package/app/buyer-core/a2a-knp.js +3 -3
- package/app/buyer-core/a2a-knp.js.map +1 -1
- package/app/buyer-core/a2a-negotiator.d.ts +16 -0
- package/app/buyer-core/a2a-negotiator.js +53 -16
- package/app/buyer-core/a2a-negotiator.js.map +1 -1
- package/app/buyer-core/a2a-quote-fetcher.d.ts +0 -15
- package/app/buyer-core/a2a-quote-fetcher.js +10 -15
- package/app/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/app/buyer-core/merchant-index.d.ts +2 -2
- package/app/buyer-core/merchant-index.js +23 -5
- package/app/buyer-core/merchant-index.js.map +1 -1
- package/app/buyer-core/procurement/cases.d.ts +441 -0
- package/app/buyer-core/procurement/cases.js +457 -0
- package/app/buyer-core/procurement/cases.js.map +1 -0
- package/app/buyer-core/procurement/quote-comparison.d.ts +137 -0
- package/app/buyer-core/procurement/quote-comparison.js +106 -0
- package/app/buyer-core/procurement/quote-comparison.js.map +1 -0
- package/app/buyer-core/procurement/quote-normalizer.d.ts +45 -0
- package/app/buyer-core/procurement/quote-normalizer.js +153 -0
- package/app/buyer-core/procurement/quote-normalizer.js.map +1 -0
- package/app/buyer-core/procurement/requirements.d.ts +50 -0
- package/app/buyer-core/procurement/requirements.js +43 -0
- package/app/buyer-core/procurement/requirements.js.map +1 -0
- package/app/buyer-core/procurement/source-observation.d.ts +57 -0
- package/app/buyer-core/procurement/source-observation.js +54 -0
- package/app/buyer-core/procurement/source-observation.js.map +1 -0
- package/app/buyer-core/procurement/store.d.ts +21 -0
- package/app/buyer-core/procurement/store.js +68 -0
- package/app/buyer-core/procurement/store.js.map +1 -0
- package/app/buyer-core/protocol-recovery.d.ts +8 -0
- package/app/buyer-core/protocol-recovery.js +17 -2
- package/app/buyer-core/protocol-recovery.js.map +1 -1
- package/app/buyer-core/quote-quantity.d.ts +5 -0
- package/app/buyer-core/quote-quantity.js +15 -0
- package/app/buyer-core/quote-quantity.js.map +1 -0
- package/app/buyer-core/service.d.ts +71 -3
- package/app/buyer-core/service.js +271 -28
- package/app/buyer-core/service.js.map +1 -1
- package/app/buyer-core/store.d.ts +13 -0
- package/app/buyer-core/store.js +25 -3
- package/app/buyer-core/store.js.map +1 -1
- package/app/cli.js +13 -17
- package/app/cli.js.map +1 -1
- package/app/cloud/alipay-collection-http.d.ts +44 -0
- package/app/cloud/alipay-collection-http.js +111 -0
- package/app/cloud/alipay-collection-http.js.map +1 -0
- package/app/cloud/bootstrap.js +77 -12
- package/app/cloud/bootstrap.js.map +1 -1
- package/app/cloud/http-router.d.ts +2 -0
- package/app/cloud/http-router.js +5 -0
- package/app/cloud/http-router.js.map +1 -1
- package/app/cloud-sample/products.json +3 -3
- package/app/commerce/receipt/dispatcher.d.ts +2 -1
- package/app/commerce/receipt/dispatcher.js +18 -0
- package/app/commerce/receipt/dispatcher.js.map +1 -1
- package/app/commerce/receipt/receipts.d.ts +7 -0
- package/app/commerce/receipt/receipts.js +43 -5
- package/app/commerce/receipt/receipts.js.map +1 -1
- package/app/discovery/catalog-source/buyer-follows.d.ts +3 -3
- package/app/discovery/catalog-source/buyer-follows.js +6 -3
- package/app/discovery/catalog-source/buyer-follows.js.map +1 -1
- package/app/discovery/catalog-source/kiwi-record.d.ts +2 -0
- package/app/discovery/catalog-source/kiwi-record.js.map +1 -1
- package/app/discovery/catalog-source/kiwi-source.js +4 -0
- package/app/discovery/catalog-source/kiwi-source.js.map +1 -1
- package/app/handoff/alipay-collection.d.ts +23 -0
- package/app/handoff/alipay-collection.js +70 -0
- package/app/handoff/alipay-collection.js.map +1 -0
- package/app/handoff/order-receipts.js +2 -1
- package/app/handoff/order-receipts.js.map +1 -1
- package/app/handoff/ucp-checkout/channel.d.ts +1 -0
- package/app/handoff/ucp-checkout/channel.js +42 -17
- package/app/handoff/ucp-checkout/channel.js.map +1 -1
- package/app/http/merchant-management/api.d.ts +8 -0
- package/app/http/merchant-management/api.js +309 -3
- package/app/http/merchant-management/api.js.map +1 -1
- package/app/http/merchant-management/page.d.ts +1 -0
- package/app/http/merchant-management/page.js +333 -3
- package/app/http/merchant-management/page.js.map +1 -1
- package/app/http/server.js +9 -2
- package/app/http/server.js.map +1 -1
- package/app/mcp/cli.d.ts +3 -1
- package/app/mcp/cli.js +12 -2
- package/app/mcp/cli.js.map +1 -1
- package/app/mcp/merchant-runtime-assembly.js +1 -8
- package/app/mcp/merchant-runtime-assembly.js.map +1 -1
- package/app/mcp/procurement-tools.d.ts +10 -0
- package/app/mcp/procurement-tools.js +173 -0
- package/app/mcp/procurement-tools.js.map +1 -0
- package/app/mcp/server.d.ts +4 -0
- package/app/mcp/server.js +63 -9
- package/app/mcp/server.js.map +1 -1
- package/app/mcp/tools.d.ts +4 -1
- package/app/mcp/tools.js +76 -27
- package/app/mcp/tools.js.map +1 -1
- package/app/mcp/types.d.ts +1 -0
- package/app/mcp/types.js.map +1 -1
- package/app/merchant/application/actor.d.ts +2 -0
- package/app/merchant/application/actor.js +6 -0
- package/app/merchant/application/actor.js.map +1 -1
- package/app/merchant/negotiation-ops.d.ts +2 -0
- package/app/merchant/negotiation-ops.js +4 -0
- package/app/merchant/negotiation-ops.js.map +1 -1
- package/app/merchant/weixin-channel.d.ts +68 -0
- package/app/merchant/weixin-channel.js +313 -0
- package/app/merchant/weixin-channel.js.map +1 -0
- package/app/merchant/workbench-server.js +13 -0
- package/app/merchant/workbench-server.js.map +1 -1
- package/app/merchant-core/collection/alipay-store.d.ts +22 -0
- package/app/merchant-core/collection/alipay-store.js +125 -0
- package/app/merchant-core/collection/alipay-store.js.map +1 -0
- package/app/merchant-core/commercial/calculate.js +6 -6
- package/app/merchant-core/commercial/calculate.js.map +1 -1
- package/app/merchant-core/operating/repository.d.ts +0 -1
- package/app/merchant-core/operating/repository.js +2 -17
- package/app/merchant-core/operating/repository.js.map +1 -1
- package/app/merchant-core/opportunity/repository.d.ts +0 -1
- package/app/merchant-core/opportunity/repository.js +2 -17
- package/app/merchant-core/opportunity/repository.js.map +1 -1
- package/app/merchant-core/orders/model.d.ts +1 -8
- package/app/merchant-core/orders/model.js +10 -2
- package/app/merchant-core/orders/model.js.map +1 -1
- package/app/merchant-core/orders/repository.d.ts +0 -1
- package/app/merchant-core/orders/repository.js +3 -18
- package/app/merchant-core/orders/repository.js.map +1 -1
- package/app/merchant-core/orders/service.js +34 -8
- package/app/merchant-core/orders/service.js.map +1 -1
- package/app/merchant-core/proposal/engine.js +2 -0
- package/app/merchant-core/proposal/engine.js.map +1 -1
- package/app/merchant-core/proposal/publish.d.ts +0 -6
- package/app/merchant-core/proposal/publish.js +37 -3
- package/app/merchant-core/proposal/publish.js.map +1 -1
- package/app/merchant-core/proposal/repository.d.ts +0 -7
- package/app/merchant-core/proposal/repository.js +8 -17
- package/app/merchant-core/proposal/repository.js.map +1 -1
- package/app/merchant-core/proposal/types.d.ts +1 -0
- package/app/merchant-core/proposal/types.js.map +1 -1
- package/app/merchant-core/rfq/policy.d.ts +1 -0
- package/app/merchant-core/rfq/policy.js +6 -7
- package/app/merchant-core/rfq/policy.js.map +1 -1
- package/app/merchant-core/rfq/repository.d.ts +0 -1
- package/app/merchant-core/rfq/repository.js +2 -17
- package/app/merchant-core/rfq/repository.js.map +1 -1
- package/app/merchant-core/rfq/service.d.ts +2 -1
- package/app/merchant-core/rfq/service.js +2 -4
- package/app/merchant-core/rfq/service.js.map +1 -1
- package/app/merchant-core/storage/transaction.d.ts +4 -11
- package/app/merchant-core/storage/transaction.js +43 -65
- package/app/merchant-core/storage/transaction.js.map +1 -1
- package/app/negotiation/domain/objects.d.ts +2 -0
- package/app/negotiation/domain/objects.js +3 -0
- package/app/negotiation/domain/objects.js.map +1 -1
- package/app/negotiation/ledger/store.d.ts +4 -0
- package/app/negotiation/ledger/store.js +20 -0
- package/app/negotiation/ledger/store.js.map +1 -1
- package/app/product-init.d.ts +1 -0
- package/app/product-init.js +4 -1
- package/app/product-init.js.map +1 -1
- package/app/trust/identity/message-signature.d.ts +1 -1
- package/app/trust/identity/message-signature.js +6 -1
- package/app/trust/identity/message-signature.js.map +1 -1
- package/app/trust/identity/nonce.d.ts +5 -0
- package/app/trust/identity/nonce.js +2 -0
- package/app/trust/identity/nonce.js.map +1 -1
- package/app/weixin/ilink-client.d.ts +3 -2
- package/app/weixin/ilink-client.js +9 -2
- package/app/weixin/ilink-client.js.map +1 -1
- package/build-manifest.json +581 -356
- package/deployment-lock.json +2 -2
- package/package.json +1 -1
- package/runtime-package.json +1 -1
- package/shipping-sbom.cdx.json +6 -6
- package/shipping-source.json +12 -12
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
17
17
|
import type { WriteApprovalCandidate } from "../../agent/merchant/action-candidate.js";
|
|
18
|
+
import { type MerchantWeixinChannel } from "../../merchant/weixin-channel.js";
|
|
18
19
|
import type { MerchantPolicy } from "../../config/profile.js";
|
|
19
20
|
import type { MerchantAdminSessions } from "../../auth/merchant-sessions.js";
|
|
20
21
|
import { type createFileListingPublicationService } from "../../cloud/file-listing-publication.js";
|
|
@@ -31,10 +32,12 @@ import { type MerchantAccessMode } from "../../cloud/access/access-mode.js";
|
|
|
31
32
|
import type { PlatformEvidenceResult } from "../../cloud/onboarding/types.js";
|
|
32
33
|
import { type WorkbenchConfirmationStore } from "./webauthn-confirmation.js";
|
|
33
34
|
import { type A2aNegotiationRow } from "../../merchant/workbench-service.js";
|
|
35
|
+
import { type NegotiationOps } from "../../merchant/negotiation-ops.js";
|
|
34
36
|
import { type NegotiationObserver } from "../../merchant/negotiation-observer.js";
|
|
35
37
|
import { type OperationsReportBuilder } from "../../merchant/operations-report.js";
|
|
36
38
|
import type { ExactMerchantProduct } from "../../agent/merchant/types.js";
|
|
37
39
|
import type { MerchantOperationsSurface } from "../../merchant-core/ops/surface.js";
|
|
40
|
+
import { type AlipayCollectionStore } from "../../merchant-core/collection/alipay-store.js";
|
|
38
41
|
import type { GovernanceFeatureFlags } from "../../merchant-core/context-governance/feature-flags.js";
|
|
39
42
|
import { type ExactMoney } from "../../merchant/application/money.js";
|
|
40
43
|
import type { WorkbenchQuoteResult } from "../../merchant/quote-calculator.js";
|
|
@@ -71,6 +74,7 @@ export interface CatalogAccessSummary {
|
|
|
71
74
|
catalogSyncError?: string | null;
|
|
72
75
|
}
|
|
73
76
|
export interface MerchantManagementApiOptions {
|
|
77
|
+
weixinChannel?: MerchantWeixinChannel;
|
|
74
78
|
receiveCustomerIdentity?: ReturnType<typeof import("../../a2a/server/customer-identity-gateway.js").createCustomerIdentityReceiver>;
|
|
75
79
|
/** 本实例商家(服务端绑定;不来自请求)。 */
|
|
76
80
|
merchantId: string;
|
|
@@ -83,6 +87,10 @@ export interface MerchantManagementApiOptions {
|
|
|
83
87
|
sessions: MerchantAdminSessions;
|
|
84
88
|
/** 允许的写请求 Origin(精确匹配,含 scheme;请求未带 Origin 时仅 CSRF 防护)。 */
|
|
85
89
|
allowedOrigins?: readonly string[];
|
|
90
|
+
alipayCollection?: AlipayCollectionStore;
|
|
91
|
+
/** Exact principal of the direct admin login, not a Catalog gateway owner-role session. */
|
|
92
|
+
alipayCollectionOwnerActorId?: string;
|
|
93
|
+
alipayNegotiationApprovals?: Pick<NegotiationOps, "listApprovals" | "approve">;
|
|
86
94
|
/** 待审批候选(core 命令日志 pending 列表)。 */
|
|
87
95
|
listPending: () => WriteApprovalCandidate[];
|
|
88
96
|
/** 一次性候选确认凭证签发(core 凭证存储;执行层逐项核销)。 */
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
* operation_id 查询路径(§11.1),绝不当作失败自动重做。
|
|
39
39
|
*/
|
|
40
40
|
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
41
|
+
import { MerchantWeixinError } from "../../merchant/weixin-channel.js";
|
|
41
42
|
import { contentHash } from "../../agent/merchant/action-candidate.js";
|
|
42
43
|
import { ADMIN_SESSION_COOKIE } from "../../auth/merchant-sessions.js";
|
|
43
44
|
import { FileListingPublicationError, } from "../../cloud/file-listing-publication.js";
|
|
@@ -45,7 +46,7 @@ import { parseProductTable, ProductTableError, productTableDigest, } from "../..
|
|
|
45
46
|
import { convertTabularImport, renderImportTemplateCsv, renderImportTemplateXlsx, TABULAR_IMPORT_MAX_BYTES, } from "../../cloud/product-import-tabular.js";
|
|
46
47
|
import { normalizeTradePlatformRules, } from "../../handoff/trade-platform-rules.js";
|
|
47
48
|
import { MerchantImportDraftStore } from "./draft-store.js";
|
|
48
|
-
import { ActorContextError, assertVerifiedActor, createVerifiedActorContext, hasPermission, } from "../../merchant/application/actor.js";
|
|
49
|
+
import { ActorContextError, assertVerifiedActor, createVerifiedActorContext, hasPermission, isDirectMerchantOwner, } from "../../merchant/application/actor.js";
|
|
49
50
|
import { MANAGEMENT_API_MAJOR, MANAGEMENT_ERROR_STATUS, ManagementError, MerchantApplicationService, } from "../../merchant/application/service.js";
|
|
50
51
|
import { managementRequestDigest, MerchantManagementOperationStore } from "./operation-store.js";
|
|
51
52
|
import { OnboardingStore } from "../../cloud/onboarding/store.js";
|
|
@@ -57,8 +58,10 @@ import { checkStepSubmission, planWizard, WIZARD_STEPS } from "../../cloud/onboa
|
|
|
57
58
|
import { createWorkbenchProblem } from "./problem.js";
|
|
58
59
|
import { WorkbenchConfirmationError, } from "./webauthn-confirmation.js";
|
|
59
60
|
import { MerchantWorkbenchError, } from "../../merchant/workbench-service.js";
|
|
61
|
+
import { NegotiationOpsError } from "../../merchant/negotiation-ops.js";
|
|
60
62
|
import { NEGOTIATION_STATUS_FILTERS, } from "../../merchant/negotiation-observer.js";
|
|
61
63
|
import { parseReportPeriod, } from "../../merchant/operations-report.js";
|
|
64
|
+
import { AlipayCollectionConflict, isBoundedAlipayQrPng, } from "../../merchant-core/collection/alipay-store.js";
|
|
62
65
|
import { OrderFactError } from "../../merchant-core/orders/model.js";
|
|
63
66
|
import { OperatingPlanError } from "../../merchant-core/operating/types.js";
|
|
64
67
|
import { OpportunityError } from "../../merchant-core/opportunity/types.js";
|
|
@@ -81,6 +84,8 @@ import { WorkbenchEventProjectionError, } from "./event-projection.js";
|
|
|
81
84
|
import { GRANT_ACTIONS, MerchantGrantError, } from "../../merchant/grant-store.js";
|
|
82
85
|
const API_PREFIX = "/merchant/api";
|
|
83
86
|
const WORKBENCH_API_PREFIX = "/merchant/api/v1";
|
|
87
|
+
const ALIPAY_COLLECTION_PATH = "/collection/alipay";
|
|
88
|
+
const ALIPAY_APPROVAL_PATH = `${ALIPAY_COLLECTION_PATH}/approvals`;
|
|
84
89
|
const CATALOG_CONNECTION_SAFE_CODES = new Set([
|
|
85
90
|
"PUBLICATION_NOT_ACTIVE", "PUBLICATION_NOT_VERIFIED", "CATALOG_UNREACHABLE",
|
|
86
91
|
"CATALOG_MISMATCH", "BINDING_MERCHANT_MISMATCH", "BINDING_CLAIM_EXPIRED",
|
|
@@ -117,6 +122,51 @@ async function resolvePlatformRules(options) {
|
|
|
117
122
|
return normalizeTradePlatformRules(undefined);
|
|
118
123
|
}
|
|
119
124
|
}
|
|
125
|
+
function alipayCollectionSummary(config) {
|
|
126
|
+
const mask = (value) => {
|
|
127
|
+
const letters = Array.from(value);
|
|
128
|
+
return letters.length > 1 ? `${letters[0]}***` : "***";
|
|
129
|
+
};
|
|
130
|
+
return {
|
|
131
|
+
version: config?.version ?? null,
|
|
132
|
+
enabled: config?.enabled ?? false,
|
|
133
|
+
recipient_masked: config === undefined ? null : mask(config.recipient),
|
|
134
|
+
account_masked: config?.account === undefined ? null : mask(config.account),
|
|
135
|
+
has_qr: config?.qrPngBase64 !== undefined,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function alipayFields(body, allowed) {
|
|
139
|
+
const fields = requireObject(body, "body");
|
|
140
|
+
if (Object.keys(fields).some((key) => !allowed.includes(key))) {
|
|
141
|
+
// Field names are also user input; never echo them on this settings surface.
|
|
142
|
+
throw new ManagementError("invalid_input", "支付宝收款请求包含不支持的字段");
|
|
143
|
+
}
|
|
144
|
+
return fields;
|
|
145
|
+
}
|
|
146
|
+
function alipayExpectedVersion(value) {
|
|
147
|
+
if (value === null || (typeof value === "string" && /^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/.test(value)))
|
|
148
|
+
return value;
|
|
149
|
+
throw new ManagementError("invalid_input", "需要有效的收款配置版本(首次设置为 null)");
|
|
150
|
+
}
|
|
151
|
+
function alipayCollectionInput(fields) {
|
|
152
|
+
const enabled = fields["enabled"];
|
|
153
|
+
const recipient = requireString(fields["recipient"], "recipient").trim();
|
|
154
|
+
const account = fields["account"] === undefined ? undefined : requireString(fields["account"], "account").trim();
|
|
155
|
+
const qrPngBase64 = fields["qr_png_base64"];
|
|
156
|
+
if (typeof enabled !== "boolean" || recipient.length > 80 || /[\p{Cc}\p{Cf}\p{Cs}]/u.test(recipient)) {
|
|
157
|
+
throw new ManagementError("invalid_input", "收款主体名称或启用状态无效");
|
|
158
|
+
}
|
|
159
|
+
if (account !== undefined && (account.length > 128 || !/^(?:\+?[0-9]{5,20}|[A-Za-z0-9._+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+)$/.test(account))) {
|
|
160
|
+
throw new ManagementError("invalid_input", "支付宝账号须为手机号或邮箱");
|
|
161
|
+
}
|
|
162
|
+
if (qrPngBase64 !== undefined && !isBoundedAlipayQrPng(qrPngBase64)) {
|
|
163
|
+
throw new ManagementError("invalid_input", "仅支持不超过 256 KiB、宽高不超过 1024 像素的静态 PNG");
|
|
164
|
+
}
|
|
165
|
+
if (account === undefined && qrPngBase64 === undefined) {
|
|
166
|
+
throw new ManagementError("invalid_input", "请填写支付宝账号或上传官方静态收款码");
|
|
167
|
+
}
|
|
168
|
+
return { enabled, recipient, ...(account === undefined ? {} : { account }), ...(qrPngBase64 === undefined ? {} : { qrPngBase64 }) };
|
|
169
|
+
}
|
|
120
170
|
function deliveryField(body, key) {
|
|
121
171
|
const value = body[key];
|
|
122
172
|
if (typeof value !== "string" || value.trim() === "") {
|
|
@@ -153,6 +203,7 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
153
203
|
// CSRF 进程密钥:令牌 = HMAC(sessionId, key)。重启后页面重新拉取 /session
|
|
154
204
|
// 即可取到新令牌;跨进程不可推导,持 Cookie 者本就可推导(CSRF 威胁模型成立)。
|
|
155
205
|
const csrfKey = randomBytes(32);
|
|
206
|
+
const alipayPreviewKey = randomBytes(32);
|
|
156
207
|
const baseDeps = {
|
|
157
208
|
merchantId: options.merchantId,
|
|
158
209
|
generation: options.generation,
|
|
@@ -219,6 +270,10 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
219
270
|
const requestId = `req_${randomBytes(12).toString("hex")}`;
|
|
220
271
|
const rest = pathname === WORKBENCH_API_PREFIX ? "/" : pathname.slice(WORKBENCH_API_PREFIX.length);
|
|
221
272
|
try {
|
|
273
|
+
if (rest === "/channels/weixin" || rest.startsWith("/channels/weixin/")) {
|
|
274
|
+
await routeWeixin(req, res, method, rest);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
222
277
|
if (method === "GET") {
|
|
223
278
|
await routeWorkbenchV1Get(req, res, url, rest, requestId);
|
|
224
279
|
return;
|
|
@@ -262,6 +317,73 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
262
317
|
}
|
|
263
318
|
writeJson(res, 404, errorBody("not_found", "unknown management api path"));
|
|
264
319
|
}
|
|
320
|
+
async function routeWeixin(req, res, method, rest) {
|
|
321
|
+
const channel = options.weixinChannel;
|
|
322
|
+
if (channel === undefined)
|
|
323
|
+
throw new ManagementError("not_found", "微信通道未启用。");
|
|
324
|
+
const auth = requireActor(req);
|
|
325
|
+
const actor = assertVerifiedActor(auth.ctx, now());
|
|
326
|
+
if (actor.merchantId !== options.merchantId)
|
|
327
|
+
throw new ManagementError("not_found", "微信通道不存在。");
|
|
328
|
+
if (actor.role !== "owner")
|
|
329
|
+
throw new ManagementError("forbidden", "仅店主可管理微信通道。");
|
|
330
|
+
const base = "/channels/weixin";
|
|
331
|
+
if (method === "GET" && rest === base) {
|
|
332
|
+
writeJson(res, 200, channel.status());
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const action = rest.slice(base.length + 1);
|
|
336
|
+
if (method !== "POST" || !["begin", "poll", "cancel", "disconnect"].includes(action)) {
|
|
337
|
+
throw new ManagementError("not_found", "微信通道路由不存在。");
|
|
338
|
+
}
|
|
339
|
+
assertWriteGuards(req, auth.sessionId);
|
|
340
|
+
let fields;
|
|
341
|
+
try {
|
|
342
|
+
fields = objectFields(await readJsonBody(req, 2048), action === "poll" ? ["attempt_id", "verify_code"] : action === "cancel" ? ["attempt_id"] : []);
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
if (error instanceof BodyTooLarge || error instanceof ManagementError) {
|
|
346
|
+
throw new ManagementError("invalid_input", "微信通道请求字段无效或请求过大。");
|
|
347
|
+
}
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
const owner = {
|
|
351
|
+
sessionId: auth.sessionId,
|
|
352
|
+
generation: actor.generation,
|
|
353
|
+
current: () => {
|
|
354
|
+
if (actor.generation !== options.generation())
|
|
355
|
+
return false;
|
|
356
|
+
const session = options.sessions.getSession(auth.sessionId);
|
|
357
|
+
return session?.role === "owner" && session.merchant_id === options.merchantId &&
|
|
358
|
+
session.principal_id === actor.actorId;
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
try {
|
|
362
|
+
if (action === "begin") {
|
|
363
|
+
writeJson(res, 200, await channel.begin(owner));
|
|
364
|
+
}
|
|
365
|
+
else if (action === "disconnect") {
|
|
366
|
+
await channel.disconnect(owner);
|
|
367
|
+
writeJson(res, 200, { status: "disconnected" });
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
const id = requireString(fields["attempt_id"], "attempt_id");
|
|
371
|
+
if (action === "cancel") {
|
|
372
|
+
channel.cancel(owner, id);
|
|
373
|
+
writeJson(res, 200, { status: "cancelled" });
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
const code = fields["verify_code"];
|
|
377
|
+
writeJson(res, 200, await channel.poll(owner, id, code === undefined ? undefined : requireString(code, "verify_code")));
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
if (error instanceof MerchantWeixinError)
|
|
383
|
+
throw new ManagementError(error.code, error.message);
|
|
384
|
+
throw error;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
265
387
|
// ── 只读路由(认证即鉴权入口;业务授权在服务层逐次复核)────────────────
|
|
266
388
|
/** Workbench v1 读取面复用同一应用服务;未实现路由明确失败,不返回假数据。 */
|
|
267
389
|
function governanceRouteDisabled(rest) {
|
|
@@ -285,7 +407,60 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
285
407
|
throw error;
|
|
286
408
|
}
|
|
287
409
|
}
|
|
410
|
+
function requireAlipayCollectionOwner(req) {
|
|
411
|
+
const auth = requireActor(req);
|
|
412
|
+
const ctx = assertVerifiedActor(auth.ctx, now());
|
|
413
|
+
if (ctx.merchantId !== options.merchantId) {
|
|
414
|
+
throw new ManagementError("not_found", "resource does not belong to this merchant");
|
|
415
|
+
}
|
|
416
|
+
if (!isDirectMerchantOwner(ctx, options.alipayCollectionOwnerActorId)) {
|
|
417
|
+
throw new ManagementError("forbidden", "支付宝收款设置仅限直接登录的商家管理员");
|
|
418
|
+
}
|
|
419
|
+
return auth;
|
|
420
|
+
}
|
|
421
|
+
function requireAlipayCollection() {
|
|
422
|
+
const store = options.alipayCollection;
|
|
423
|
+
if (store === undefined || store.merchantId !== options.merchantId) {
|
|
424
|
+
throw new ManagementError("unavailable", "支付宝收款设置暂不可用");
|
|
425
|
+
}
|
|
426
|
+
return store;
|
|
427
|
+
}
|
|
428
|
+
function requireAlipayApprovals() {
|
|
429
|
+
const approvals = options.alipayNegotiationApprovals;
|
|
430
|
+
if (approvals === undefined)
|
|
431
|
+
throw new ManagementError("unavailable", "支付宝报价审批暂不可用");
|
|
432
|
+
return approvals;
|
|
433
|
+
}
|
|
288
434
|
async function routeWorkbenchV1Get(req, res, url, rest, requestId) {
|
|
435
|
+
if (rest === ALIPAY_COLLECTION_PATH) {
|
|
436
|
+
requireAlipayCollectionOwner(req);
|
|
437
|
+
const store = requireAlipayCollection();
|
|
438
|
+
try {
|
|
439
|
+
writeJson(res, 200, alipayCollectionSummary(store.current()), { "x-request-id": requestId });
|
|
440
|
+
}
|
|
441
|
+
catch {
|
|
442
|
+
throw new ManagementError("unavailable", "支付宝收款设置暂不可用");
|
|
443
|
+
}
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (rest === ALIPAY_APPROVAL_PATH) {
|
|
447
|
+
requireAlipayCollectionOwner(req);
|
|
448
|
+
const store = requireAlipayCollection();
|
|
449
|
+
const approvals = requireAlipayApprovals();
|
|
450
|
+
try {
|
|
451
|
+
const items = store.current()?.enabled === true ? approvals.listApprovals("pending")
|
|
452
|
+
.filter((item) => item.currency === "CNY").slice(0, 100).map((item) => ({
|
|
453
|
+
approval_id: item.approval_id, negotiation_id: item.negotiation_id,
|
|
454
|
+
sku: item.sku, quantity: item.quantity, unit: item.unit ?? null,
|
|
455
|
+
buyer_offer_minor: item.buyer_offer_minor, currency: item.currency, expires_at: item.expires_at ?? null,
|
|
456
|
+
})) : [];
|
|
457
|
+
writeJson(res, 200, { items }, { "x-request-id": requestId });
|
|
458
|
+
}
|
|
459
|
+
catch {
|
|
460
|
+
throw new ManagementError("unavailable", "支付宝报价审批暂不可用");
|
|
461
|
+
}
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
289
464
|
if (governanceRouteDisabled(rest)) {
|
|
290
465
|
writeWorkbenchProblem(res, "RESOURCE_NOT_FOUND", requestId, "资源不存在", "Workbench API 路由不存在或尚未实现。");
|
|
291
466
|
return;
|
|
@@ -903,7 +1078,136 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
903
1078
|
}
|
|
904
1079
|
writeWorkbenchProblem(res, "RESOURCE_NOT_FOUND", requestId, "资源不存在", "Workbench API 路由不存在或尚未实现。");
|
|
905
1080
|
}
|
|
1081
|
+
function alipayPreviewMac(auth, expectedVersion, draft, expiry) {
|
|
1082
|
+
return createHmac("sha256", alipayPreviewKey).update(JSON.stringify([
|
|
1083
|
+
auth.sessionId, auth.ctx.actorId, auth.ctx.merchantId, auth.ctx.generation, expectedVersion, draft, expiry,
|
|
1084
|
+
])).digest("base64url");
|
|
1085
|
+
}
|
|
1086
|
+
async function writeAlipayCollection(req, res, requestId, action) {
|
|
1087
|
+
try {
|
|
1088
|
+
const auth = requireAlipayCollectionOwner(req);
|
|
1089
|
+
assertWriteGuards(req, auth.sessionId);
|
|
1090
|
+
const store = requireAlipayCollection();
|
|
1091
|
+
const allowed = action === "disable" ? ["expected_version", "confirm_disable"]
|
|
1092
|
+
: ["expected_version", "enabled", "recipient", "account", "qr_png_base64"];
|
|
1093
|
+
if (action === "save")
|
|
1094
|
+
allowed.push("confirmation_ref", "confirm_owner_rights");
|
|
1095
|
+
const fields = alipayFields(await readJsonBody(req), allowed);
|
|
1096
|
+
const expectedVersion = alipayExpectedVersion(fields["expected_version"]);
|
|
1097
|
+
if (auth.ctx.generation !== requireAlipayCollectionOwner(req).ctx.generation) {
|
|
1098
|
+
throw new AlipayCollectionConflict();
|
|
1099
|
+
}
|
|
1100
|
+
const current = store.current();
|
|
1101
|
+
if ((current?.version ?? null) !== expectedVersion)
|
|
1102
|
+
throw new AlipayCollectionConflict();
|
|
1103
|
+
if (action === "disable") {
|
|
1104
|
+
if (fields["confirm_disable"] !== true)
|
|
1105
|
+
throw new ManagementError("invalid_input", "请明确确认停用收款方式");
|
|
1106
|
+
if (current?.enabled !== true)
|
|
1107
|
+
throw new AlipayCollectionConflict();
|
|
1108
|
+
const { version, ...details } = current;
|
|
1109
|
+
const disabled = store.replace(version, { ...details, enabled: false }, auth.ctx.actorId, now().toISOString());
|
|
1110
|
+
writeJson(res, 200, alipayCollectionSummary(disabled), { "x-request-id": requestId });
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
const draft = alipayCollectionInput(fields);
|
|
1114
|
+
if (action === "preview") {
|
|
1115
|
+
const expiry = now().getTime() + CONFIRMATION_TTL_MS;
|
|
1116
|
+
const token = `${expiry}.${alipayPreviewMac(auth, expectedVersion, draft, expiry)}`;
|
|
1117
|
+
writeJson(res, 200, { ...alipayCollectionSummary(draft), confirmation_ref: token }, { "x-request-id": requestId });
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
if (fields["confirm_owner_rights"] !== true) {
|
|
1121
|
+
throw new ManagementError("invalid_input", "请明确确认你有权使用该收款主体及账号或收款码");
|
|
1122
|
+
}
|
|
1123
|
+
const token = fields["confirmation_ref"];
|
|
1124
|
+
const parsed = typeof token === "string" ? /^(\d{1,15})\.([A-Za-z0-9_-]{43})$/.exec(token) : null;
|
|
1125
|
+
if (parsed === null || Number(parsed[1]) <= now().getTime() || !timingSafeEqual(Buffer.from(parsed[2]), Buffer.from(alipayPreviewMac(auth, expectedVersion, draft, Number(parsed[1]))))) {
|
|
1126
|
+
throw new ManagementError("precondition_changed", "收款信息、会话或确认有效期已变化,请重新预览");
|
|
1127
|
+
}
|
|
1128
|
+
const result = store.replace(expectedVersion, draft, auth.ctx.actorId, now().toISOString());
|
|
1129
|
+
writeJson(res, 200, alipayCollectionSummary(result), { "x-request-id": requestId });
|
|
1130
|
+
}
|
|
1131
|
+
catch (error) {
|
|
1132
|
+
if (error instanceof ManagementError || error instanceof ActorContextError)
|
|
1133
|
+
throw error;
|
|
1134
|
+
if (error instanceof AlipayCollectionConflict)
|
|
1135
|
+
throw new ManagementError("precondition_changed", error.message);
|
|
1136
|
+
if (error instanceof BodyTooLarge) {
|
|
1137
|
+
writeJson(res, 413, errorBody("invalid_input", "request body too large"), { "x-request-id": requestId });
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
// No unexpected exception (including SQLite diagnostics) is reflected or logged here.
|
|
1141
|
+
throw new ManagementError("unavailable", "支付宝收款设置暂不可用");
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
async function approveAlipayTotal(req, res, requestId) {
|
|
1145
|
+
const auth = requireAlipayCollectionOwner(req);
|
|
1146
|
+
assertWriteGuards(req, auth.sessionId);
|
|
1147
|
+
const approvals = requireAlipayApprovals();
|
|
1148
|
+
const store = requireAlipayCollection();
|
|
1149
|
+
try {
|
|
1150
|
+
const fields = alipayFields(await readJsonBody(req), [
|
|
1151
|
+
"approval_id", "expected_version", "approved_minor", "alipay_total_minor", "confirm_final_total",
|
|
1152
|
+
]);
|
|
1153
|
+
const approvalId = requireString(fields["approval_id"], "approval_id");
|
|
1154
|
+
const expectedVersion = alipayExpectedVersion(fields["expected_version"]);
|
|
1155
|
+
const approvedMinor = requirePositiveInteger(fields["approved_minor"], "approved_minor");
|
|
1156
|
+
const totalMinor = requirePositiveInteger(fields["alipay_total_minor"], "alipay_total_minor");
|
|
1157
|
+
if (!Number.isSafeInteger(approvedMinor) || !Number.isSafeInteger(totalMinor)) {
|
|
1158
|
+
throw new ManagementError("invalid_input", "单价与最终应付总额必须是安全整数分");
|
|
1159
|
+
}
|
|
1160
|
+
if (fields["confirm_final_total"] !== true) {
|
|
1161
|
+
throw new ManagementError("invalid_input", "请明确确认包含适用运费、税费及其他费用的 CNY 最终应付总额");
|
|
1162
|
+
}
|
|
1163
|
+
const current = store.current();
|
|
1164
|
+
if (auth.ctx.generation !== requireAlipayCollectionOwner(req).ctx.generation ||
|
|
1165
|
+
current?.enabled !== true || current.version !== expectedVersion)
|
|
1166
|
+
throw new AlipayCollectionConflict();
|
|
1167
|
+
const pending = approvals.listApprovals("pending").find((item) => item.approval_id === approvalId && item.currency === "CNY");
|
|
1168
|
+
if (pending === undefined)
|
|
1169
|
+
throw new ManagementError("not_found", "未找到待审批的人民币报价");
|
|
1170
|
+
const subtotal = approvedMinor * pending.quantity;
|
|
1171
|
+
if (subtotal <= 0 || !Number.isSafeInteger(subtotal) || totalMinor < subtotal) {
|
|
1172
|
+
throw new ManagementError("invalid_input", "最终应付总额不得低于已确认单价与数量计算的商品小计");
|
|
1173
|
+
}
|
|
1174
|
+
const receipt = approvals.approve({
|
|
1175
|
+
approval_id: approvalId, approved_minor: approvedMinor, alipay_total_minor: totalMinor, actor: auth.ctx.actorId,
|
|
1176
|
+
});
|
|
1177
|
+
writeJson(res, 200, {
|
|
1178
|
+
approval_id: receipt.approval_id, status: receipt.status,
|
|
1179
|
+
approved_minor: receipt.approved_minor, alipay_total_minor: receipt.alipay_total_minor,
|
|
1180
|
+
}, { "x-request-id": requestId });
|
|
1181
|
+
}
|
|
1182
|
+
catch (error) {
|
|
1183
|
+
if (error instanceof ManagementError || error instanceof ActorContextError)
|
|
1184
|
+
throw error;
|
|
1185
|
+
if (error instanceof AlipayCollectionConflict)
|
|
1186
|
+
throw new ManagementError("precondition_changed", error.message);
|
|
1187
|
+
if (error instanceof BodyTooLarge) {
|
|
1188
|
+
writeJson(res, 413, errorBody("invalid_input", "request body too large"), { "x-request-id": requestId });
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
if (error instanceof NegotiationOpsError) {
|
|
1192
|
+
if (error.code === "not_found" || error.code === "conflict") {
|
|
1193
|
+
throw new ManagementError("precondition_changed", "待审批报价已变更,请刷新后再试");
|
|
1194
|
+
}
|
|
1195
|
+
if (error.code === "forbidden" || error.code === "invalid_request") {
|
|
1196
|
+
throw new ManagementError("invalid_input", "金额不符合当前报价规则,请核对后再试");
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
throw new ManagementError("unavailable", "支付宝报价审批暂不可用");
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
906
1202
|
async function routeWorkbenchV1Post(req, res, rest, requestId, url) {
|
|
1203
|
+
if (rest === ALIPAY_APPROVAL_PATH) {
|
|
1204
|
+
await approveAlipayTotal(req, res, requestId);
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
if (rest === ALIPAY_COLLECTION_PATH || rest === `${ALIPAY_COLLECTION_PATH}/preview` || rest === `${ALIPAY_COLLECTION_PATH}/disable`) {
|
|
1208
|
+
await writeAlipayCollection(req, res, requestId, rest.endsWith("/preview") ? "preview" : rest.endsWith("/disable") ? "disable" : "save");
|
|
1209
|
+
return;
|
|
1210
|
+
}
|
|
907
1211
|
if (governanceRouteDisabled(rest)) {
|
|
908
1212
|
writeWorkbenchProblem(res, "RESOURCE_NOT_FOUND", requestId, "资源不存在", "Workbench API 路由不存在或尚未实现。");
|
|
909
1213
|
return;
|
|
@@ -2389,6 +2693,8 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
2389
2693
|
role: auth.session.role,
|
|
2390
2694
|
expires_at: auth.session.expires_at,
|
|
2391
2695
|
csrf_token: csrfTokenFor(auth.sessionId),
|
|
2696
|
+
can_manage_alipay_collection: options.alipayCollection?.merchantId === auth.ctx.merchantId &&
|
|
2697
|
+
auth.ctx.merchantId === options.merchantId && isDirectMerchantOwner(auth.ctx, options.alipayCollectionOwnerActorId),
|
|
2392
2698
|
});
|
|
2393
2699
|
return;
|
|
2394
2700
|
}
|
|
@@ -3834,12 +4140,12 @@ export function createMerchantManagementApiHandler(options) {
|
|
|
3834
4140
|
return expected.length === actual.length && timingSafeEqual(expected, actual);
|
|
3835
4141
|
}
|
|
3836
4142
|
// ── 输入与输出辅助 ─────────────────────────────────────────────────
|
|
3837
|
-
async function readJsonBody(req) {
|
|
4143
|
+
async function readJsonBody(req, maxBytes = MAX_BODY_BYTES) {
|
|
3838
4144
|
const chunks = [];
|
|
3839
4145
|
let size = 0;
|
|
3840
4146
|
for await (const chunk of req) {
|
|
3841
4147
|
size += chunk.length;
|
|
3842
|
-
if (size >
|
|
4148
|
+
if (size > maxBytes)
|
|
3843
4149
|
throw new BodyTooLarge();
|
|
3844
4150
|
chunks.push(chunk);
|
|
3845
4151
|
}
|