@harrylabsj/kiwi 0.12.32 → 0.12.34
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/README.md +8 -0
- package/dist/a2a/inquiry-nonce-store.d.ts +12 -1
- package/dist/a2a/inquiry-nonce-store.js +49 -17
- package/dist/a2a/inquiry-nonce-store.js.map +1 -1
- package/dist/a2a/negotiate.d.ts +30 -1
- package/dist/a2a/negotiate.js +121 -32
- package/dist/a2a/negotiate.js.map +1 -1
- package/dist/a2a/node.d.ts +3 -0
- package/dist/a2a/node.js +13 -3
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/open-inquiry-session.d.ts +1 -0
- package/dist/a2a/open-inquiry-session.js +3 -1
- package/dist/a2a/open-inquiry-session.js.map +1 -1
- package/dist/a2a/open-inquiry-verifier.js +2 -2
- package/dist/a2a/open-inquiry-verifier.js.map +1 -1
- package/dist/a2a/server/errors.d.ts +4 -3
- package/dist/a2a/server/errors.js +10 -2
- package/dist/a2a/server/errors.js.map +1 -1
- package/dist/a2a/server/merchant-handler.d.ts +41 -1
- package/dist/a2a/server/merchant-handler.js +259 -89
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/merchant-procurement.d.ts +90 -0
- package/dist/a2a/server/merchant-procurement.js +75 -0
- package/dist/a2a/server/merchant-procurement.js.map +1 -0
- package/dist/a2a/server/merchant-product-facts.d.ts +2 -0
- package/dist/a2a/server/merchant-product-facts.js.map +1 -1
- package/dist/a2a/server/negotiation-approvals.d.ts +11 -0
- package/dist/a2a/server/negotiation-approvals.js +43 -0
- package/dist/a2a/server/negotiation-approvals.js.map +1 -1
- package/dist/a2a/server/pipeline.js +4 -1
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/server.js +4 -3
- package/dist/a2a/server/server.js.map +1 -1
- package/dist/a2a/server/types.d.ts +2 -1
- package/dist/agent/alipay-native-view.d.ts +27 -0
- package/dist/agent/alipay-native-view.js +113 -0
- package/dist/agent/alipay-native-view.js.map +1 -0
- package/dist/agent/buyer/buyer-tools.d.ts +20 -0
- package/dist/agent/buyer/buyer-tools.js +241 -48
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/buyer/types.d.ts +1 -1
- package/dist/agent/buyer/types.js.map +1 -1
- package/dist/agent/chat-tui.js +16 -1
- package/dist/agent/chat-tui.js.map +1 -1
- package/dist/agent/kernel-builder.d.ts +2 -0
- package/dist/agent/kernel-builder.js +14 -0
- package/dist/agent/kernel-builder.js.map +1 -1
- package/dist/agent/kernel.d.ts +8 -0
- package/dist/agent/kernel.js +67 -1
- package/dist/agent/kernel.js.map +1 -1
- package/dist/buyer-core/a2a-accept-terms.d.ts +11 -0
- package/dist/buyer-core/a2a-accept-terms.js +28 -0
- package/dist/buyer-core/a2a-accept-terms.js.map +1 -0
- package/dist/buyer-core/a2a-knp.d.ts +3 -0
- package/dist/buyer-core/a2a-knp.js +3 -3
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +16 -0
- package/dist/buyer-core/a2a-negotiator.js +53 -16
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/a2a-quote-fetcher.d.ts +0 -15
- package/dist/buyer-core/a2a-quote-fetcher.js +10 -15
- package/dist/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/dist/buyer-core/merchant-index.d.ts +2 -2
- package/dist/buyer-core/merchant-index.js +23 -5
- package/dist/buyer-core/merchant-index.js.map +1 -1
- package/dist/buyer-core/procurement/cases.d.ts +441 -0
- package/dist/buyer-core/procurement/cases.js +457 -0
- package/dist/buyer-core/procurement/cases.js.map +1 -0
- package/dist/buyer-core/procurement/quote-comparison.d.ts +137 -0
- package/dist/buyer-core/procurement/quote-comparison.js +106 -0
- package/dist/buyer-core/procurement/quote-comparison.js.map +1 -0
- package/dist/buyer-core/procurement/quote-normalizer.d.ts +45 -0
- package/dist/buyer-core/procurement/quote-normalizer.js +153 -0
- package/dist/buyer-core/procurement/quote-normalizer.js.map +1 -0
- package/dist/buyer-core/procurement/requirements.d.ts +50 -0
- package/dist/buyer-core/procurement/requirements.js +43 -0
- package/dist/buyer-core/procurement/requirements.js.map +1 -0
- package/dist/buyer-core/procurement/source-observation.d.ts +57 -0
- package/dist/buyer-core/procurement/source-observation.js +54 -0
- package/dist/buyer-core/procurement/source-observation.js.map +1 -0
- package/dist/buyer-core/procurement/store.d.ts +21 -0
- package/dist/buyer-core/procurement/store.js +68 -0
- package/dist/buyer-core/procurement/store.js.map +1 -0
- package/dist/buyer-core/protocol-recovery.d.ts +8 -0
- package/dist/buyer-core/protocol-recovery.js +17 -2
- package/dist/buyer-core/protocol-recovery.js.map +1 -1
- package/dist/buyer-core/quote-quantity.d.ts +5 -0
- package/dist/buyer-core/quote-quantity.js +15 -0
- package/dist/buyer-core/quote-quantity.js.map +1 -0
- package/dist/buyer-core/service.d.ts +71 -3
- package/dist/buyer-core/service.js +271 -28
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/buyer-core/store.d.ts +13 -0
- package/dist/buyer-core/store.js +25 -3
- package/dist/buyer-core/store.js.map +1 -1
- package/dist/cli.js +13 -17
- package/dist/cli.js.map +1 -1
- package/dist/cloud/alipay-collection-http.d.ts +44 -0
- package/dist/cloud/alipay-collection-http.js +111 -0
- package/dist/cloud/alipay-collection-http.js.map +1 -0
- package/dist/cloud/bootstrap.js +98 -12
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/cloud/http-router.d.ts +2 -0
- package/dist/cloud/http-router.js +5 -0
- package/dist/cloud/http-router.js.map +1 -1
- package/dist/commerce/receipt/dispatcher.d.ts +2 -1
- package/dist/commerce/receipt/dispatcher.js +18 -0
- package/dist/commerce/receipt/dispatcher.js.map +1 -1
- package/dist/commerce/receipt/receipts.d.ts +7 -0
- package/dist/commerce/receipt/receipts.js +43 -5
- package/dist/commerce/receipt/receipts.js.map +1 -1
- package/dist/discovery/catalog-source/buyer-follows.d.ts +3 -3
- package/dist/discovery/catalog-source/buyer-follows.js +6 -3
- package/dist/discovery/catalog-source/buyer-follows.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-record.d.ts +2 -0
- package/dist/discovery/catalog-source/kiwi-record.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-source.js +4 -0
- package/dist/discovery/catalog-source/kiwi-source.js.map +1 -1
- package/dist/handoff/alipay-collection.d.ts +23 -0
- package/dist/handoff/alipay-collection.js +70 -0
- package/dist/handoff/alipay-collection.js.map +1 -0
- package/dist/handoff/order-receipts.js +2 -1
- package/dist/handoff/order-receipts.js.map +1 -1
- package/dist/handoff/ucp-checkout/channel.d.ts +1 -0
- package/dist/handoff/ucp-checkout/channel.js +42 -17
- package/dist/handoff/ucp-checkout/channel.js.map +1 -1
- package/dist/http/customer-support-buyer.d.ts +24 -0
- package/dist/http/customer-support-buyer.js +306 -0
- package/dist/http/customer-support-buyer.js.map +1 -0
- package/dist/http/merchant-management/api.d.ts +12 -0
- package/dist/http/merchant-management/api.js +425 -6
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/page.d.ts +2 -0
- package/dist/http/merchant-management/page.js +630 -3
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/http/server.js +9 -2
- package/dist/http/server.js.map +1 -1
- package/dist/mcp/cli.d.ts +3 -1
- package/dist/mcp/cli.js +12 -2
- package/dist/mcp/cli.js.map +1 -1
- package/dist/mcp/merchant-runtime-assembly.d.ts +3 -0
- package/dist/mcp/merchant-runtime-assembly.js +9 -8
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/procurement-tools.d.ts +10 -0
- package/dist/mcp/procurement-tools.js +173 -0
- package/dist/mcp/procurement-tools.js.map +1 -0
- package/dist/mcp/server.d.ts +4 -0
- package/dist/mcp/server.js +63 -9
- package/dist/mcp/server.js.map +1 -1
- package/dist/mcp/tools.d.ts +4 -1
- package/dist/mcp/tools.js +76 -27
- package/dist/mcp/tools.js.map +1 -1
- package/dist/mcp/types.d.ts +1 -0
- package/dist/mcp/types.js.map +1 -1
- package/dist/merchant/ai-runtime/owner-storage-admission.js.map +1 -1
- package/dist/merchant/application/actor.d.ts +2 -0
- package/dist/merchant/application/actor.js +6 -0
- package/dist/merchant/application/actor.js.map +1 -1
- package/dist/merchant/negotiation-ops.d.ts +2 -0
- package/dist/merchant/negotiation-ops.js +4 -0
- package/dist/merchant/negotiation-ops.js.map +1 -1
- package/dist/merchant/weixin-channel.d.ts +224 -0
- package/dist/merchant/weixin-channel.js +435 -0
- package/dist/merchant/weixin-channel.js.map +1 -0
- package/dist/merchant/weixin-owner-agent-profile.d.ts +8 -0
- package/dist/merchant/weixin-owner-agent-profile.js +29 -0
- package/dist/merchant/weixin-owner-agent-profile.js.map +1 -0
- package/dist/merchant/weixin-owner-agent.d.ts +42 -0
- package/dist/merchant/weixin-owner-agent.js +97 -0
- package/dist/merchant/weixin-owner-agent.js.map +1 -0
- package/dist/merchant/weixin-owner-conversation.d.ts +156 -0
- package/dist/merchant/weixin-owner-conversation.js +559 -0
- package/dist/merchant/weixin-owner-conversation.js.map +1 -0
- package/dist/merchant/weixin-owner-digest.d.ts +30 -0
- package/dist/merchant/weixin-owner-digest.js +30 -0
- package/dist/merchant/weixin-owner-digest.js.map +1 -0
- package/dist/merchant/weixin-owner-inventory.d.ts +28 -0
- package/dist/merchant/weixin-owner-inventory.js +48 -0
- package/dist/merchant/weixin-owner-inventory.js.map +1 -0
- package/dist/merchant/weixin-owner-source.d.ts +22 -0
- package/dist/merchant/weixin-owner-source.js +104 -0
- package/dist/merchant/weixin-owner-source.js.map +1 -0
- package/dist/merchant/workbench-server.d.ts +4 -0
- package/dist/merchant/workbench-server.js +75 -9
- package/dist/merchant/workbench-server.js.map +1 -1
- package/dist/merchant-core/collection/alipay-store.d.ts +22 -0
- package/dist/merchant-core/collection/alipay-store.js +125 -0
- package/dist/merchant-core/collection/alipay-store.js.map +1 -0
- package/dist/merchant-core/commercial/calculate.js +6 -6
- package/dist/merchant-core/commercial/calculate.js.map +1 -1
- package/dist/merchant-core/operating/repository.d.ts +0 -1
- package/dist/merchant-core/operating/repository.js +2 -17
- package/dist/merchant-core/operating/repository.js.map +1 -1
- package/dist/merchant-core/operations-jobs/service.d.ts +11 -0
- package/dist/merchant-core/operations-jobs/service.js +65 -34
- package/dist/merchant-core/operations-jobs/service.js.map +1 -1
- package/dist/merchant-core/opportunity/repository.d.ts +0 -1
- package/dist/merchant-core/opportunity/repository.js +2 -17
- package/dist/merchant-core/opportunity/repository.js.map +1 -1
- package/dist/merchant-core/ops/surface.d.ts +1 -0
- package/dist/merchant-core/ops/surface.js +9 -0
- package/dist/merchant-core/ops/surface.js.map +1 -1
- package/dist/merchant-core/orders/model.d.ts +1 -8
- package/dist/merchant-core/orders/model.js +10 -2
- package/dist/merchant-core/orders/model.js.map +1 -1
- package/dist/merchant-core/orders/repository.d.ts +0 -1
- package/dist/merchant-core/orders/repository.js +3 -18
- package/dist/merchant-core/orders/repository.js.map +1 -1
- package/dist/merchant-core/orders/service.js +34 -8
- package/dist/merchant-core/orders/service.js.map +1 -1
- package/dist/merchant-core/proposal/engine.js +2 -0
- package/dist/merchant-core/proposal/engine.js.map +1 -1
- package/dist/merchant-core/proposal/publish.d.ts +0 -6
- package/dist/merchant-core/proposal/publish.js +37 -3
- package/dist/merchant-core/proposal/publish.js.map +1 -1
- package/dist/merchant-core/proposal/repository.d.ts +0 -7
- package/dist/merchant-core/proposal/repository.js +8 -17
- package/dist/merchant-core/proposal/repository.js.map +1 -1
- package/dist/merchant-core/proposal/types.d.ts +1 -0
- package/dist/merchant-core/proposal/types.js.map +1 -1
- package/dist/merchant-core/rfq/policy.d.ts +1 -0
- package/dist/merchant-core/rfq/policy.js +6 -7
- package/dist/merchant-core/rfq/policy.js.map +1 -1
- package/dist/merchant-core/rfq/repository.d.ts +0 -1
- package/dist/merchant-core/rfq/repository.js +2 -17
- package/dist/merchant-core/rfq/repository.js.map +1 -1
- package/dist/merchant-core/rfq/service.d.ts +2 -1
- package/dist/merchant-core/rfq/service.js +2 -4
- package/dist/merchant-core/rfq/service.js.map +1 -1
- package/dist/merchant-core/storage/transaction.d.ts +4 -11
- package/dist/merchant-core/storage/transaction.js +43 -65
- package/dist/merchant-core/storage/transaction.js.map +1 -1
- package/dist/merchant-core/support/manual-service.d.ts +114 -0
- package/dist/merchant-core/support/manual-service.js +300 -0
- package/dist/merchant-core/support/manual-service.js.map +1 -0
- package/dist/negotiation/domain/objects.d.ts +2 -0
- package/dist/negotiation/domain/objects.js +3 -0
- package/dist/negotiation/domain/objects.js.map +1 -1
- package/dist/negotiation/ledger/store.d.ts +6 -0
- package/dist/negotiation/ledger/store.js +28 -0
- package/dist/negotiation/ledger/store.js.map +1 -1
- package/dist/net/safe-http.d.ts +2 -0
- package/dist/net/safe-http.js +1 -1
- package/dist/net/safe-http.js.map +1 -1
- package/dist/product-init.d.ts +1 -0
- package/dist/product-init.js +4 -1
- package/dist/product-init.js.map +1 -1
- package/dist/trust/identity/message-signature.d.ts +1 -1
- package/dist/trust/identity/message-signature.js +6 -1
- package/dist/trust/identity/message-signature.js.map +1 -1
- package/dist/trust/identity/nonce.d.ts +5 -0
- package/dist/trust/identity/nonce.js +2 -0
- package/dist/trust/identity/nonce.js.map +1 -1
- package/dist/weixin/ilink-client.d.ts +3 -2
- package/dist/weixin/ilink-client.js +58 -20
- package/dist/weixin/ilink-client.js.map +1 -1
- package/dist/weixin/types.d.ts +3 -1
- package/dist/weixin/types.js.map +1 -1
- package/package.json +14 -14
|
@@ -24,6 +24,8 @@
|
|
|
24
24
|
* 每个出站回复落 merchant 侧 Ledger(append-only,§22)。这是 `kiwi agent serve`
|
|
25
25
|
* 的确定性 merchant 行为;未来可替换为 LLM 驱动的 Negotiation Engine(同一 handler 接缝)。
|
|
26
26
|
*/
|
|
27
|
+
import { merchantProcurementTerms, readProcurementProjection } from "./merchant-procurement.js";
|
|
28
|
+
import { statSync as statReceiptLedger } from "node:fs";
|
|
27
29
|
import { StreamGateError } from "../../merchant-core/customer-delivery/stream-gate.js";
|
|
28
30
|
import { BoundedMap, BoundedSet } from "../../runtime/bounded-memory.js";
|
|
29
31
|
import { checkedProductStock, checkedSourceEvidence, quoteFactsStillValid, recoverQuoteSourceSnapshot } from "./merchant-product-facts.js";
|
|
@@ -37,6 +39,8 @@ import { WORKBENCH_CURRENCY_TABLE_VERSION } from "../../merchant/application/mon
|
|
|
37
39
|
import { ADVISOR_BUYER_MESSAGE_MAX_CHARS, safeAdvisorMessage, } from "./negotiation-advisor.js";
|
|
38
40
|
import { calculateWorkbenchQuote } from "../../merchant/quote-calculator.js";
|
|
39
41
|
import { isOpenInquiryIdentity } from "../open-inquiry-identity.js";
|
|
42
|
+
import { assertSafeTargetUrl } from "../client/url-policy.js";
|
|
43
|
+
import { parseAlipayCollectionLink, parseAlipayCollectionTerms } from "../../handoff/alipay-collection.js";
|
|
40
44
|
import { QuoteHistoryStore, delegationFromMetadata, parseHistoryQueryExtension, isQuoteHistoryRecall, quoteHistoryNote, validateDelegationShape, } from "../../merchant-core/customer-memory/quote-history.js";
|
|
41
45
|
import { thumbprintFromOpenInquiryIdentity, } from "../../commerce/receipt/receipts.js";
|
|
42
46
|
import { createNegotiationPhase, isTerminalPhase, transitionPhase, } from "../../negotiation/state/phase.js";
|
|
@@ -276,13 +280,41 @@ function offerTerms(opts, now) {
|
|
|
276
280
|
valid_until: new Date(validUntil).toISOString(),
|
|
277
281
|
};
|
|
278
282
|
}
|
|
283
|
+
function alipayBuyerIdentity(identity) {
|
|
284
|
+
return identity.trim() !== "" && !isOpenInquiryIdentity(identity);
|
|
285
|
+
}
|
|
286
|
+
function positiveMinorAmount(value) {
|
|
287
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
288
|
+
}
|
|
289
|
+
function alipayRoute(origin, negotiationId, offerId, version) {
|
|
290
|
+
try {
|
|
291
|
+
const base = assertSafeTargetUrl(origin, { allowLoopback: false });
|
|
292
|
+
if (base.protocol !== "https:" || base.href !== `${base.origin}/`)
|
|
293
|
+
return undefined;
|
|
294
|
+
const ids = [negotiationId, offerId, version];
|
|
295
|
+
if (ids.some((id) => id.trim() === "" || id === "." || id === ".."))
|
|
296
|
+
return undefined;
|
|
297
|
+
const url = `${base.origin}/collect/alipay/${ids.map((id) => encodeURIComponent(id)).join("/")}`;
|
|
298
|
+
return parseAlipayCollectionLink(url) !== null ? url : undefined;
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function recipientSummary(recipient) {
|
|
305
|
+
const chars = Array.from(recipient.trim());
|
|
306
|
+
const first = chars[0];
|
|
307
|
+
return first !== undefined && chars.length > 1 && /^\p{L}$/u.test(first) ? `${first}***` : "***";
|
|
308
|
+
}
|
|
279
309
|
function buildAgreement(input) {
|
|
310
|
+
const agreedTerms = input.agreed_terms;
|
|
280
311
|
return {
|
|
281
312
|
type: "accepted_nonbinding_agreement",
|
|
282
313
|
agreement_id: newAgreementId(),
|
|
283
314
|
negotiation_id: input.negotiation_id,
|
|
284
315
|
accepted_offer_id: input.accepted_offer_id,
|
|
285
316
|
agreed_terms: input.agreed_terms,
|
|
317
|
+
...(agreedTerms.kiwi_alipay_collection !== undefined ? { handoff_destination: agreedTerms.handoff_destination } : {}),
|
|
286
318
|
terms_digest: contentDigest(input.agreed_terms),
|
|
287
319
|
accepted_by: ["buyer", "merchant"],
|
|
288
320
|
created_at: input.created_at,
|
|
@@ -374,6 +406,31 @@ const envelopeReply = (reply) => ({
|
|
|
374
406
|
export function createMerchantHandler(options) {
|
|
375
407
|
const { ledger, now, sender, counterparty } = options;
|
|
376
408
|
const offerPriceMinor = options.offerPriceMinor ?? MERCHANT_OFFER_PRICE_MINOR;
|
|
409
|
+
const currentAlipay = () => {
|
|
410
|
+
try {
|
|
411
|
+
const config = options.alipayCollection;
|
|
412
|
+
const current = config?.current();
|
|
413
|
+
if (config !== undefined && current?.enabled === true && current.recipient.trim() !== "") {
|
|
414
|
+
return { origin: config.origin, version: current.version, summary: recipientSummary(current.recipient) };
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
catch {
|
|
418
|
+
// A private settings read failure must not surface details or enable collection.
|
|
419
|
+
}
|
|
420
|
+
return undefined;
|
|
421
|
+
};
|
|
422
|
+
const collectionForTerms = (terms, negotiationId, offerId) => {
|
|
423
|
+
const collection = parseAlipayCollectionTerms(terms);
|
|
424
|
+
const current = currentAlipay();
|
|
425
|
+
if (collection === null || current === undefined || collection.version !== current.version ||
|
|
426
|
+
collection.recipient_summary !== current.summary || collection.expires_at !== terms.valid_until ||
|
|
427
|
+
!(Date.parse(collection.expires_at) > Date.parse(now())))
|
|
428
|
+
return undefined;
|
|
429
|
+
const destination = alipayRoute(current.origin, negotiationId, offerId, current.version);
|
|
430
|
+
if (destination === undefined || destination !== terms.handoff_destination)
|
|
431
|
+
return undefined;
|
|
432
|
+
return { version: collection.version, totalMinor: collection.total_minor, currency: collection.currency, expiresAt: collection.expires_at };
|
|
433
|
+
};
|
|
377
434
|
const allowDemoPriceFallback = options.allowDemoPriceFallback ?? false;
|
|
378
435
|
const unitPolicy = unitConversionPolicy({
|
|
379
436
|
aliases: options.unitAliases,
|
|
@@ -726,11 +783,11 @@ export function createMerchantHandler(options) {
|
|
|
726
783
|
// 两次 append 间原子提交,因此按“预写证据补齐”:任一中断点重试都复用
|
|
727
784
|
// 同一商家协议号与摘要;终态但无证据的历史窗口保持拒绝(不生成替代协议)。
|
|
728
785
|
/** 查找本磋商已落账的协议证据(offer_id 与 terms_digest 强匹配)。 */
|
|
729
|
-
const findAgreementEvidence = (negotiationId, acceptedOfferId, termsDigest) => {
|
|
786
|
+
const findAgreementEvidence = (negotiationId, acceptedOfferId, termsDigest, persistedEvents) => {
|
|
730
787
|
if (acceptedOfferId === "" || termsDigest === "")
|
|
731
788
|
return undefined;
|
|
732
789
|
try {
|
|
733
|
-
for (const event of ledger.events(negotiationId)) {
|
|
790
|
+
for (const event of persistedEvents ?? ledger.events(negotiationId)) {
|
|
734
791
|
if (event.event_kind !== "system")
|
|
735
792
|
continue;
|
|
736
793
|
const result = event.outcome.kind === "ok" ? event.outcome.result : undefined;
|
|
@@ -785,13 +842,13 @@ export function createMerchantHandler(options) {
|
|
|
785
842
|
occurred_at: now(),
|
|
786
843
|
});
|
|
787
844
|
};
|
|
788
|
-
const recordAgreementReceipt = (negotiationId, agreement, identity) => {
|
|
845
|
+
const recordAgreementReceipt = (negotiationId, agreement, identity, recovering = false) => {
|
|
789
846
|
const receipts = options.commerceReceipts;
|
|
790
847
|
const binding = thumbprintFromOpenInquiryIdentity(identity);
|
|
791
848
|
if (receipts === undefined || binding === undefined)
|
|
792
849
|
return;
|
|
793
850
|
const agreementId = String(agreement.agreement_id);
|
|
794
|
-
|
|
851
|
+
const receipt = {
|
|
795
852
|
event_id: `merchant-agreement:${agreementId}`,
|
|
796
853
|
event_type: "merchant_agreement_issued",
|
|
797
854
|
merchant_id: receipts.merchantId,
|
|
@@ -801,31 +858,46 @@ export function createMerchantHandler(options) {
|
|
|
801
858
|
terms_digest: String(agreement.terms_digest),
|
|
802
859
|
buyer_binding: binding,
|
|
803
860
|
occurred_at: String(agreement.created_at),
|
|
804
|
-
}
|
|
861
|
+
};
|
|
862
|
+
if (recovering)
|
|
863
|
+
receipts.dispatcher.record(receipt);
|
|
864
|
+
else
|
|
865
|
+
void receipts.dispatcher.enqueueAndDeliver(receipt).catch(() => { });
|
|
805
866
|
};
|
|
867
|
+
let scannedVersion;
|
|
806
868
|
const recoverReceipts = () => {
|
|
807
|
-
|
|
869
|
+
const dispatcher = options.commerceReceipts?.dispatcher;
|
|
870
|
+
if (!dispatcher)
|
|
808
871
|
return;
|
|
809
|
-
|
|
872
|
+
const version = ledger.receiptDirectoryVersion();
|
|
873
|
+
if (version === scannedVersion)
|
|
874
|
+
return;
|
|
875
|
+
let complete = true;
|
|
876
|
+
for (const file of ledger.receiptLedgerFiles()) {
|
|
810
877
|
try {
|
|
811
|
-
const
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
878
|
+
const stat = statReceiptLedger(file, { bigint: true });
|
|
879
|
+
dispatcher.replayLedger(file, `${stat.ino}:${stat.size}:${stat.mtimeNs}`, () => {
|
|
880
|
+
const events = ledger.readReceiptLedger(file);
|
|
881
|
+
const terminal = [...events].reverse().find((event) => event.state_transition !== undefined);
|
|
882
|
+
for (const event of events) {
|
|
883
|
+
if (event.event_kind === "message_sent" && event.wire_payload !== undefined) {
|
|
884
|
+
recordEntryReceipt(event.wire_payload, true);
|
|
885
|
+
}
|
|
886
|
+
if (terminal?.state_transition?.to_phase !== "AGREEMENT_REACHED" || event.event_kind !== "system")
|
|
887
|
+
continue;
|
|
888
|
+
const result = event.outcome.kind === "ok" ? event.outcome.result : undefined;
|
|
889
|
+
if (result?.kind === "agreement_issued" && result.agreement !== null && typeof result.agreement === "object") {
|
|
890
|
+
recordAgreementReceipt(event.negotiation_id, result.agreement, String(result.buyer_binding ?? ""), true);
|
|
891
|
+
}
|
|
822
892
|
}
|
|
823
|
-
}
|
|
893
|
+
});
|
|
824
894
|
}
|
|
825
895
|
catch {
|
|
826
|
-
|
|
896
|
+
complete = false;
|
|
827
897
|
}
|
|
828
898
|
}
|
|
899
|
+
if (complete)
|
|
900
|
+
scannedVersion = version;
|
|
829
901
|
};
|
|
830
902
|
/** 终态重放:证据命中时按原协议幂等返回(B6)。 */
|
|
831
903
|
const replayedAgreementResult = (agreement) => ({
|
|
@@ -973,7 +1045,7 @@ export function createMerchantHandler(options) {
|
|
|
973
1045
|
endsAt: value.endsAt,
|
|
974
1046
|
};
|
|
975
1047
|
};
|
|
976
|
-
const recordEntryReceipt = (reply) => {
|
|
1048
|
+
const recordEntryReceipt = (reply, recovering = false) => {
|
|
977
1049
|
// K6:带成交入口的报价正式发出(message_sent 已落账)→ 上报“商家已
|
|
978
1050
|
// 提供入口”(可早于协议,按报价引用关联)。预置公开链接与被拒绝审批
|
|
979
1051
|
// 不触发——本函数只在正式报价发布路径上调用。上报失败不影响报价。
|
|
@@ -993,7 +1065,10 @@ export function createMerchantHandler(options) {
|
|
|
993
1065
|
},
|
|
994
1066
|
occurred_at: reply.created_at,
|
|
995
1067
|
};
|
|
996
|
-
|
|
1068
|
+
if (recovering)
|
|
1069
|
+
options.commerceReceipts.dispatcher.record(entry);
|
|
1070
|
+
else
|
|
1071
|
+
void options.commerceReceipts.dispatcher.enqueueAndDeliver(entry).catch(() => { });
|
|
997
1072
|
}
|
|
998
1073
|
}
|
|
999
1074
|
};
|
|
@@ -1125,6 +1200,40 @@ export function createMerchantHandler(options) {
|
|
|
1125
1200
|
}
|
|
1126
1201
|
return {
|
|
1127
1202
|
name: "kiwi-agent-serve-merchant",
|
|
1203
|
+
resolveAlipayCollection(input) {
|
|
1204
|
+
if (options.alipayCollection === undefined || !alipayBuyerIdentity(input.buyerPrincipalId) || input.agreementId === "")
|
|
1205
|
+
return undefined;
|
|
1206
|
+
try {
|
|
1207
|
+
// Disk is authoritative: the in-memory phase may already be advanced if
|
|
1208
|
+
// the transition append failed after agreement_issued was persisted.
|
|
1209
|
+
const events = ledger.events(input.negotiationId);
|
|
1210
|
+
if (!ledger.verifyChain(input.negotiationId).valid)
|
|
1211
|
+
return undefined;
|
|
1212
|
+
const phaseEvent = [...events].reverse().find((event) => event.state_transition !== undefined);
|
|
1213
|
+
const samePrincipals = (event) => event.identity.actor === "merchant" &&
|
|
1214
|
+
event.identity.sender_identity === sender && event.identity.counterparty_identity === input.buyerPrincipalId;
|
|
1215
|
+
if (phaseEvent?.state_transition?.to_phase !== "AGREEMENT_REACHED" ||
|
|
1216
|
+
phaseEvent.state_transition.from_phase !== "OFFER_OPEN" || !samePrincipals(phaseEvent))
|
|
1217
|
+
return undefined;
|
|
1218
|
+
const evidence = findAgreementEvidence(input.negotiationId, input.offerId, input.termsDigest, events.slice(0, events.indexOf(phaseEvent)).filter((event) => samePrincipals(event) &&
|
|
1219
|
+
event.agreement_id === input.agreementId && event.terms_digest === input.termsDigest));
|
|
1220
|
+
const artifact = evidence?.agreement;
|
|
1221
|
+
if (evidence?.buyer_binding !== input.buyerPrincipalId || artifact === undefined ||
|
|
1222
|
+
artifact.agreement_id !== input.agreementId || artifact.negotiation_id !== input.negotiationId ||
|
|
1223
|
+
artifact.accepted_offer_id !== input.offerId)
|
|
1224
|
+
return undefined;
|
|
1225
|
+
const terms = artifact.agreed_terms;
|
|
1226
|
+
if (terms === null || typeof terms !== "object" || Array.isArray(terms) ||
|
|
1227
|
+
contentDigest(terms) !== input.termsDigest ||
|
|
1228
|
+
artifact.handoff_destination !== terms.handoff_destination)
|
|
1229
|
+
return undefined;
|
|
1230
|
+
const collection = collectionForTerms(terms, input.negotiationId, input.offerId);
|
|
1231
|
+
return collection?.version === input.version ? collection : undefined;
|
|
1232
|
+
}
|
|
1233
|
+
catch {
|
|
1234
|
+
return undefined;
|
|
1235
|
+
}
|
|
1236
|
+
},
|
|
1128
1237
|
async recoverAccept(ctx) {
|
|
1129
1238
|
if (ctx.envelope.action !== "accept_nonbinding" || ctx.identityVerified !== true)
|
|
1130
1239
|
return undefined;
|
|
@@ -1137,6 +1246,11 @@ export function createMerchantHandler(options) {
|
|
|
1137
1246
|
syncPhaseFromLedger(negotiation_id);
|
|
1138
1247
|
const phase = phaseStateByNegotiation.get(negotiation_id)?.phase;
|
|
1139
1248
|
if (phase === "OFFER_OPEN") {
|
|
1249
|
+
const terms = evidence.agreement.agreed_terms;
|
|
1250
|
+
if (terms?.kiwi_alipay_collection !== undefined &&
|
|
1251
|
+
(!alipayBuyerIdentity(ctx.senderIdentity) || collectionForTerms(terms, negotiation_id, payload.offer_id) === undefined)) {
|
|
1252
|
+
return undefined;
|
|
1253
|
+
}
|
|
1140
1254
|
boundPeers.set(negotiation_id, ctx.senderIdentity);
|
|
1141
1255
|
await appendPhaseTransition(negotiation_id, "AGREEMENT_REACHED", phase);
|
|
1142
1256
|
}
|
|
@@ -1552,9 +1666,15 @@ export function createMerchantHandler(options) {
|
|
|
1552
1666
|
})
|
|
1553
1667
|
: undefined;
|
|
1554
1668
|
const quoteNote = quoteMessage(product);
|
|
1669
|
+
const projection = readProcurementProjection(payload.requested_terms);
|
|
1670
|
+
const companyFacts = projection ? options.procurementFacts?.(sku) : undefined;
|
|
1671
|
+
const arrival = deliveryBefore();
|
|
1672
|
+
const companyTerms = merchantProcurementTerms(companyFacts, projection, { price: effectivePriceMinor, currency,
|
|
1673
|
+
quantity, unit: requestedUnit, arrival, stock: sourceConfirmed ? product.stock : undefined });
|
|
1674
|
+
const procurement = projection ? { projection, facts_digest: contentDigest(companyFacts ?? null), arrival } : undefined;
|
|
1555
1675
|
// 买家在 RFQ 里声明了到货时限时必须逐条回应,不能只回价格。
|
|
1556
1676
|
const language = replyLanguageOf(envelope.public_message, buyerMessage);
|
|
1557
|
-
const deliveryNote = deliveryCommitmentNote(requestedDeliveryBeforeOf(envelope.payload),
|
|
1677
|
+
const deliveryNote = deliveryCommitmentNote(requestedDeliveryBeforeOf(envelope.payload), arrival, language);
|
|
1558
1678
|
const publicMessage = [advisedMessage, quoteNote.public_message, deliveryNote]
|
|
1559
1679
|
.filter((part) => part !== undefined && part.trim() !== "")
|
|
1560
1680
|
.join(" ") || undefined;
|
|
@@ -1567,16 +1687,16 @@ export function createMerchantHandler(options) {
|
|
|
1567
1687
|
payload: {
|
|
1568
1688
|
type: "offer",
|
|
1569
1689
|
offer_id: newOfferId(),
|
|
1570
|
-
terms: offerTerms({
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1690
|
+
terms: { ...offerTerms({
|
|
1691
|
+
sku,
|
|
1692
|
+
unit: requestedUnit,
|
|
1693
|
+
priceMinor: effectivePriceMinor,
|
|
1694
|
+
quantity,
|
|
1695
|
+
currency,
|
|
1696
|
+
handoff_destination,
|
|
1697
|
+
deliveryBefore: arrival,
|
|
1698
|
+
...quoteValidity(product, promotion.kind === "applied" ? promotion.endsAt : undefined),
|
|
1699
|
+
}, now()), ...companyTerms },
|
|
1580
1700
|
},
|
|
1581
1701
|
...(publicMessage !== undefined ? { public_message: publicMessage } : {}),
|
|
1582
1702
|
});
|
|
@@ -1597,7 +1717,7 @@ export function createMerchantHandler(options) {
|
|
|
1597
1717
|
// stored.conditional.offer_id 与 accept 的 offer_id 强匹配(A314
|
|
1598
1718
|
// 源码核:原返修缺 offer_id,直报价接受仍 offer_unknown)。
|
|
1599
1719
|
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.terms ?? {}, quantity));
|
|
1600
|
-
await appendSent(reply, snapshotFor(sku, quantity, product, promotion));
|
|
1720
|
+
await appendSent(reply, { ...snapshotFor(sku, quantity, product, promotion), ...(procurement ? { procurement } : {}) });
|
|
1601
1721
|
const registeredSubject = await registerVerifiedCustomerIdentity(ctx, "rfq");
|
|
1602
1722
|
await applyDelegationFromMetadata(ctx, registeredSubject?.subject_id);
|
|
1603
1723
|
await indexHistoryOffer(ctx, reply, product);
|
|
@@ -1693,6 +1813,11 @@ export function createMerchantHandler(options) {
|
|
|
1693
1813
|
return declineReply("schema_invalid");
|
|
1694
1814
|
const quantity = quantityRaw;
|
|
1695
1815
|
const buyerCounterMinor = counter.proposed_terms?.items?.[0]?.unit_price?.amount_minor;
|
|
1816
|
+
const companyPriceBasis = counter.proposed_terms?.items?.[0]?.unit_price?.basis;
|
|
1817
|
+
if (companyPriceBasis !== undefined && (typeof sku !== "string" ||
|
|
1818
|
+
!quoteSources.get(negotiationId)?.snapshot.procurement ||
|
|
1819
|
+
options.procurementFacts?.(sku)?.native_basis !== companyPriceBasis))
|
|
1820
|
+
return declineReply("schema_invalid");
|
|
1696
1821
|
const floorMinor = floorMinorForSku(sku);
|
|
1697
1822
|
if (floorMinor === null)
|
|
1698
1823
|
return declineReply("temporarily_unavailable");
|
|
@@ -1734,17 +1859,38 @@ export function createMerchantHandler(options) {
|
|
|
1734
1859
|
typeof buyerCounterMinor === "number" &&
|
|
1735
1860
|
Number.isFinite(buyerCounterMinor) &&
|
|
1736
1861
|
buyerCounterMinor < publicBoundMinor;
|
|
1737
|
-
// B2
|
|
1738
|
-
//
|
|
1739
|
-
|
|
1740
|
-
|
|
1862
|
+
// B2:只在需要人工审核的区间尝试消费授权。已签名买家重试同一还价时,
|
|
1863
|
+
// 在队列锁内处理等待/终态/消费,避免重排队或覆盖刚批准的授权。
|
|
1864
|
+
let approved;
|
|
1865
|
+
if (belowPublicBound || policyResponsiveMinorFinal < floorMinor) {
|
|
1866
|
+
const approvals = options.negotiationApprovals;
|
|
1867
|
+
const input = {
|
|
1741
1868
|
negotiation_id: negotiationId,
|
|
1742
|
-
sku,
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1869
|
+
sku, currency, quantity, unit: requestedUnit,
|
|
1870
|
+
buyer_offer_minor: buyerCounterMinor ?? priceMinor,
|
|
1871
|
+
public_bound_minor: publicBoundMinor,
|
|
1872
|
+
floor_minor: floorMinor,
|
|
1873
|
+
buyer_message_id: envelope.message_id,
|
|
1874
|
+
};
|
|
1875
|
+
const pendingMessage = "offer is below the merchant's automatic discount boundary; a merchant approval request has been recorded — retry after approval";
|
|
1876
|
+
if (approvals !== undefined && ctx.identityVerified === true && alipayBuyerIdentity(ctx.senderIdentity)) {
|
|
1877
|
+
const review = approvals.reviewSignedCounter(input);
|
|
1878
|
+
if (review.status !== "approved") {
|
|
1879
|
+
return {
|
|
1880
|
+
kind: "declined", taskState: "completed", reasonCode: "approval_required", approvalStatus: review.status,
|
|
1881
|
+
message: review.status === "pending" ? pendingMessage : `merchant approval is ${review.status}; start a new negotiation if needed`,
|
|
1882
|
+
};
|
|
1883
|
+
}
|
|
1884
|
+
approved = review.record;
|
|
1885
|
+
}
|
|
1886
|
+
else {
|
|
1887
|
+
approved = approvals?.consume(input);
|
|
1888
|
+
if (approved?.approved_minor === undefined) {
|
|
1889
|
+
approvals?.recordPending(input);
|
|
1890
|
+
return declineReply("approval_required", approvals === undefined ? undefined : pendingMessage);
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1748
1894
|
const authorizedMinor = approved?.approved_minor;
|
|
1749
1895
|
// 批准价是商家可接受的下限,但不能低于买家本轮已经愿意出的价。
|
|
1750
1896
|
// 注意 buyerCounter 已被 clamp 到公开边界,不能拿它做 max——否则
|
|
@@ -1757,27 +1903,6 @@ export function createMerchantHandler(options) {
|
|
|
1757
1903
|
? Math.min(priceMinor, Math.max(authorizedMinor, buyerOfferMinor))
|
|
1758
1904
|
: authorizedMinor
|
|
1759
1905
|
: policyResponsiveMinorFinal;
|
|
1760
|
-
if (authorizedMinor === undefined &&
|
|
1761
|
-
(belowPublicBound || responsiveMinor < floorMinor)) {
|
|
1762
|
-
// 公开边界低于私有底价:该区间不能自动报价。登记到商家审批队列,
|
|
1763
|
-
// 人工批准后买家下一轮还价可命中一次性授权价;本轮不返回价格。
|
|
1764
|
-
options.negotiationApprovals?.recordPending({
|
|
1765
|
-
negotiation_id: negotiationId,
|
|
1766
|
-
sku,
|
|
1767
|
-
currency,
|
|
1768
|
-
quantity,
|
|
1769
|
-
unit: requestedUnit,
|
|
1770
|
-
buyer_offer_minor: typeof buyerCounterMinor === "number" && Number.isFinite(buyerCounterMinor)
|
|
1771
|
-
? buyerCounterMinor
|
|
1772
|
-
: priceMinor,
|
|
1773
|
-
public_bound_minor: publicBoundMinor,
|
|
1774
|
-
floor_minor: floorMinor,
|
|
1775
|
-
...(typeof envelope.message_id === "string"
|
|
1776
|
-
? { buyer_message_id: envelope.message_id }
|
|
1777
|
-
: {}),
|
|
1778
|
-
});
|
|
1779
|
-
return declineReply("approval_required", "offer is below the merchant's automatic discount boundary; a merchant approval request has been recorded — retry after approval");
|
|
1780
|
-
}
|
|
1781
1906
|
// 可配置促销(merchant_policy.promos[sku]):买满 bulk_threshold 台,
|
|
1782
1907
|
// 批量价 = min(还价响应, list×(1-d%/100)),同样 clamp 到公开边界;
|
|
1783
1908
|
// 若该价低于私有底价则**不挂这条条件**(不广告无法兑现的价)。
|
|
@@ -1832,30 +1957,65 @@ export function createMerchantHandler(options) {
|
|
|
1832
1957
|
]
|
|
1833
1958
|
.filter((part) => part !== undefined && part.trim() !== "")
|
|
1834
1959
|
.join(" ") || undefined;
|
|
1960
|
+
const procurementProjection = quoteSources.get(negotiationId)?.snapshot.procurement?.projection;
|
|
1961
|
+
const companyFacts = procurementProjection ? options.procurementFacts?.(sku) : undefined;
|
|
1962
|
+
const arrival = deliveryBefore();
|
|
1963
|
+
const procurement = procurementProjection ? { projection: procurementProjection, facts_digest: contentDigest(companyFacts ?? null), arrival } : undefined;
|
|
1964
|
+
const companyTerms = (quotePrice) => merchantProcurementTerms(companyFacts, procurementProjection, {
|
|
1965
|
+
price: quotePrice, currency, quantity, unit: requestedUnit, arrival,
|
|
1966
|
+
stock: product.source_evidence !== undefined ? product.stock : undefined,
|
|
1967
|
+
});
|
|
1968
|
+
const offerId = newOfferId();
|
|
1969
|
+
const quoteTime = now();
|
|
1970
|
+
const baseTerms = { ...offerTerms({
|
|
1971
|
+
sku,
|
|
1972
|
+
unit: requestedUnit,
|
|
1973
|
+
priceMinor: responsiveMinor,
|
|
1974
|
+
quantity,
|
|
1975
|
+
currency,
|
|
1976
|
+
handoff_destination,
|
|
1977
|
+
deliveryBefore: arrival,
|
|
1978
|
+
...quoteValidity(product, authorityPromotion.kind === "applied" ? authorityPromotion.endsAt : undefined),
|
|
1979
|
+
}, quoteTime), ...companyTerms(responsiveMinor) };
|
|
1980
|
+
const explicitTotal = approved?.alipay_total_minor;
|
|
1981
|
+
const alipay = ctx.identityVerified === true && alipayBuyerIdentity(ctx.senderIdentity) &&
|
|
1982
|
+
product.handoff_destination === undefined && product.stock !== undefined && product.source_evidence !== undefined &&
|
|
1983
|
+
currency === "CNY" && approved?.buyer_offer_minor === buyerOfferMinor &&
|
|
1984
|
+
authorizedMinor === responsiveMinor && positiveMinorAmount(explicitTotal) &&
|
|
1985
|
+
// This is a consistency check, not a source of the payable total.
|
|
1986
|
+
BigInt(explicitTotal) >= BigInt(responsiveMinor) * BigInt(quantity)
|
|
1987
|
+
? currentAlipay()
|
|
1988
|
+
: undefined;
|
|
1989
|
+
const destination = alipay === undefined ? undefined : alipayRoute(alipay.origin, negotiationId, offerId, alipay.version);
|
|
1990
|
+
const collectionExpiry = Math.min(Date.parse(baseTerms.valid_until), Date.parse(approved?.expires_at ?? ""));
|
|
1991
|
+
const bindCollection = alipay !== undefined && destination !== undefined && explicitTotal !== undefined &&
|
|
1992
|
+
collectionExpiry > Date.parse(quoteTime);
|
|
1993
|
+
const finalTerms = bindCollection ? {
|
|
1994
|
+
...baseTerms,
|
|
1995
|
+
handoff_destination: destination,
|
|
1996
|
+
valid_until: new Date(collectionExpiry).toISOString(),
|
|
1997
|
+
kiwi_alipay_collection: {
|
|
1998
|
+
version: alipay.version,
|
|
1999
|
+
recipient_summary: alipay.summary,
|
|
2000
|
+
total_minor: explicitTotal,
|
|
2001
|
+
currency: "CNY",
|
|
2002
|
+
expires_at: new Date(collectionExpiry).toISOString(),
|
|
2003
|
+
},
|
|
2004
|
+
} : baseTerms;
|
|
1835
2005
|
const reply = seedEnvelope({
|
|
1836
2006
|
negotiation_id: negotiationId,
|
|
1837
2007
|
in_reply_to: inReplyTo,
|
|
1838
2008
|
actor: "merchant",
|
|
1839
2009
|
action: "conditional_offer",
|
|
1840
|
-
created_at:
|
|
2010
|
+
created_at: quoteTime,
|
|
1841
2011
|
payload: {
|
|
1842
2012
|
type: "conditional_offer",
|
|
1843
|
-
offer_id:
|
|
2013
|
+
offer_id: offerId,
|
|
1844
2014
|
responding_to_offer_id: counter.offer_id,
|
|
1845
2015
|
// 许可绑定(T047):本次报价依据的规则摘要与商品事实指纹;accept 时重验。
|
|
1846
2016
|
policy_digest: policyDigest(),
|
|
1847
2017
|
product_fingerprint: productFingerprint({ sku, priceMinor, currency }),
|
|
1848
|
-
|
|
1849
|
-
base_terms: offerTerms({
|
|
1850
|
-
sku,
|
|
1851
|
-
unit: requestedUnit,
|
|
1852
|
-
priceMinor: responsiveMinor,
|
|
1853
|
-
quantity,
|
|
1854
|
-
currency,
|
|
1855
|
-
handoff_destination,
|
|
1856
|
-
deliveryBefore: deliveryBefore(),
|
|
1857
|
-
...quoteValidity(product, authorityPromotion.kind === "applied" ? authorityPromotion.endsAt : undefined),
|
|
1858
|
-
}, now()),
|
|
2018
|
+
base_terms: finalTerms,
|
|
1859
2019
|
// conditions 可空(§13:空 conditions 等价 base_terms)。促销价低于
|
|
1860
2020
|
// 私有底价时不挂条件——不广告无法兑现的价,也不借 then_terms 泄露底价。
|
|
1861
2021
|
conditions: promotionalMinor !== undefined
|
|
@@ -1866,16 +2026,16 @@ export function createMerchantHandler(options) {
|
|
|
1866
2026
|
{ field: "aggregate.total_quantity", op: "gte", value: bulkThreshold },
|
|
1867
2027
|
],
|
|
1868
2028
|
},
|
|
1869
|
-
then_terms: offerTerms({
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
2029
|
+
then_terms: { ...offerTerms({
|
|
2030
|
+
sku,
|
|
2031
|
+
unit: requestedUnit,
|
|
2032
|
+
priceMinor: promotionalMinor,
|
|
2033
|
+
quantity,
|
|
2034
|
+
currency,
|
|
2035
|
+
handoff_destination,
|
|
2036
|
+
deliveryBefore: arrival,
|
|
2037
|
+
...quoteValidity(product, authorityPromotion.kind === "applied" ? authorityPromotion.endsAt : undefined),
|
|
2038
|
+
}, now()), ...companyTerms(promotionalMinor) },
|
|
1879
2039
|
},
|
|
1880
2040
|
]
|
|
1881
2041
|
: [],
|
|
@@ -1895,7 +2055,7 @@ export function createMerchantHandler(options) {
|
|
|
1895
2055
|
});
|
|
1896
2056
|
if (!advanced)
|
|
1897
2057
|
return declineReply("state_conflict");
|
|
1898
|
-
await appendSent(reply, snapshotFor(sku, quantity, product, authorityPromotion));
|
|
2058
|
+
await appendSent(reply, { ...snapshotFor(sku, quantity, product, authorityPromotion), ...(procurement ? { procurement } : {}) });
|
|
1899
2059
|
await indexHistoryOffer(ctx, reply, product);
|
|
1900
2060
|
return envelopeReply(reply);
|
|
1901
2061
|
}
|
|
@@ -2074,6 +2234,11 @@ export function createMerchantHandler(options) {
|
|
|
2074
2234
|
return declineReply("approval_required");
|
|
2075
2235
|
if (!quoteFactsStillValid(saved.snapshot, refreshedFacts.product, Date.parse(now())))
|
|
2076
2236
|
return declineReply("approval_required");
|
|
2237
|
+
if (saved.snapshot.procurement) {
|
|
2238
|
+
const currentFacts = options.procurementFacts?.(saved.snapshot.sku);
|
|
2239
|
+
if (saved.snapshot.procurement.facts_digest !== contentDigest(currentFacts ?? null))
|
|
2240
|
+
return declineReply("approval_required");
|
|
2241
|
+
}
|
|
2077
2242
|
if (saved.snapshot.policy_digest !== policyDigest())
|
|
2078
2243
|
return declineReply("approval_required");
|
|
2079
2244
|
const currentFloor = floorMinorForSku(saved.snapshot.sku);
|
|
@@ -2111,6 +2276,11 @@ export function createMerchantHandler(options) {
|
|
|
2111
2276
|
}
|
|
2112
2277
|
// K4:预写证据补齐——先复用/生成协议,落账证据,再推进相位。
|
|
2113
2278
|
// 任一中断点重试都复用同一商家协议号与摘要(B6)。
|
|
2279
|
+
if (agreedTerms.kiwi_alipay_collection !== undefined &&
|
|
2280
|
+
(ctx.identityVerified !== true || !alipayBuyerIdentity(ctx.senderIdentity) ||
|
|
2281
|
+
collectionForTerms(agreedTerms, negotiationId, acceptedOfferId) === undefined)) {
|
|
2282
|
+
return declineReply("approval_required");
|
|
2283
|
+
}
|
|
2114
2284
|
const priorEvidence = findAgreementEvidence(negotiationId, acceptedOfferId, presentedDigest);
|
|
2115
2285
|
const agreement = priorEvidence?.agreement ??
|
|
2116
2286
|
buildAgreement({
|