@devlab.group/agent-commerce 2.4.1 → 2.6.0
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 +168 -16
- package/dist/ap2.d.ts +42 -17
- package/dist/ap2.js +83 -27
- package/dist/ap2.js.map +1 -1
- package/dist/{chunk-CXWC5LWM.js → chunk-DRLBML7Z.js} +661 -75
- package/dist/chunk-DRLBML7Z.js.map +1 -0
- package/dist/{chunk-WZW5ORZH.js → chunk-G3EYYHKF.js} +3 -3
- package/dist/{chunk-WZW5ORZH.js.map → chunk-G3EYYHKF.js.map} +1 -1
- package/dist/chunk-GA3O2A7R.js +92 -0
- package/dist/chunk-GA3O2A7R.js.map +1 -0
- package/dist/{chunk-DTKU3Z2M.js → chunk-M52FBNJX.js} +36 -5
- package/dist/chunk-M52FBNJX.js.map +1 -0
- package/dist/chunk-M6BSXXTF.js +8 -0
- package/dist/chunk-M6BSXXTF.js.map +1 -0
- package/dist/{chunk-QPV3EGJO.js → chunk-M6MQ3FYS.js} +25 -4
- package/dist/chunk-M6MQ3FYS.js.map +1 -0
- package/dist/chunk-M6WW6QIT.js +18 -0
- package/dist/chunk-M6WW6QIT.js.map +1 -0
- package/dist/cli/{ap2-GWSP4LYY.js → ap2-2YMYA2H6.js} +85 -29
- package/dist/cli/ap2-2YMYA2H6.js.map +1 -0
- package/dist/cli/{chunk-OZ4LAWBK.js → chunk-3NOMLMJW.js} +29 -8
- package/dist/cli/chunk-3NOMLMJW.js.map +1 -0
- package/dist/cli/chunk-4KLYFXMW.js +8 -0
- package/dist/cli/{chunk-ITWNSS2B.js.map → chunk-4KLYFXMW.js.map} +1 -1
- package/dist/cli/chunk-663ALDZA.js +92 -0
- package/dist/cli/chunk-663ALDZA.js.map +1 -0
- package/dist/cli/chunk-6R6WJ4FD.js +18 -0
- package/dist/cli/chunk-6R6WJ4FD.js.map +1 -0
- package/dist/cli/{chunk-G2JEY7MB.js → chunk-AZL7RPFU.js} +39 -33
- package/dist/cli/chunk-AZL7RPFU.js.map +1 -0
- package/dist/cli/chunk-DHFMIBPG.js +107 -0
- package/dist/cli/chunk-DHFMIBPG.js.map +1 -0
- package/dist/cli/{chunk-QQ3WY5L3.js → chunk-GET3CZGR.js} +36 -104
- package/dist/cli/chunk-GET3CZGR.js.map +1 -0
- package/dist/cli/chunk-NSYSMDQN.js +1544 -0
- package/dist/cli/chunk-NSYSMDQN.js.map +1 -0
- package/dist/cli/{chunk-PIMFPCNZ.js → chunk-PTL7SNOI.js} +313 -138
- package/dist/cli/chunk-PTL7SNOI.js.map +1 -0
- package/dist/cli/chunk-SYQLJXIU.js +105 -0
- package/dist/cli/chunk-SYQLJXIU.js.map +1 -0
- package/dist/cli/chunk-TFV3K6YS.js +373 -0
- package/dist/cli/chunk-TFV3K6YS.js.map +1 -0
- package/dist/cli/{chunk-3MGAJ5FX.js → chunk-VJ4EO4LE.js} +3 -3
- package/dist/cli/{chunk-3MGAJ5FX.js.map → chunk-VJ4EO4LE.js.map} +1 -1
- package/dist/cli/chunk-VJH6RYRK.js +119 -0
- package/dist/cli/chunk-VJH6RYRK.js.map +1 -0
- package/dist/cli/{chunk-TNMLDPUO.js → chunk-YOF7IK4V.js} +663 -77
- package/dist/cli/chunk-YOF7IK4V.js.map +1 -0
- package/dist/cli/{chunk-WYOGIANL.js → chunk-YWG2ZN3L.js} +4 -4
- package/dist/cli/{chunk-WYOGIANL.js.map → chunk-YWG2ZN3L.js.map} +1 -1
- package/dist/cli/config-3K65QJ22.js +12 -0
- package/dist/cli/{config-UI4EDG4L.js.map → config-3K65QJ22.js.map} +1 -1
- package/dist/cli/index.js +753 -212
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/{mcp-PGBRWZVA.js → mcp-6F64A7KE.js} +5 -4
- package/dist/cli/mcp-6F64A7KE.js.map +1 -0
- package/dist/cli/{near-5AY3HWYF.js → near-U6365VFX.js} +15 -4
- package/dist/cli/near-U6365VFX.js.map +1 -0
- package/dist/cli/payment-providers-2ITS6KNR.js +6 -0
- package/dist/cli/payment-providers-2ITS6KNR.js.map +1 -0
- package/dist/cli/{provider-I2ESL6RS.js → provider-5BGG5YKQ.js} +53 -24
- package/dist/cli/provider-5BGG5YKQ.js.map +1 -0
- package/dist/cli/receipts-I4IIE7U6.js +8 -0
- package/dist/cli/receipts-I4IIE7U6.js.map +1 -0
- package/dist/cli/reconcile-55WNCSFC.js +4 -0
- package/dist/cli/reconcile-55WNCSFC.js.map +1 -0
- package/dist/cli/{run-JK4ERPJV.js → run-2P6ZGFUE.js} +356 -173
- package/dist/cli/run-2P6ZGFUE.js.map +1 -0
- package/dist/cli/svm-KDSPDFJG.js +48 -0
- package/dist/cli/svm-KDSPDFJG.js.map +1 -0
- package/dist/cli/{x402-7WBFZQXV.js → x402-4LPIIELI.js} +10 -9
- package/dist/cli/x402-4LPIIELI.js.map +1 -0
- package/dist/index.d.ts +116 -113
- package/dist/index.js +1928 -351
- package/dist/index.js.map +1 -1
- package/dist/logger-CllGwQ8i.d.ts +19 -0
- package/dist/mcp.d.ts +3 -2
- package/dist/mcp.js +2 -1
- package/dist/mcp.js.map +1 -1
- package/dist/mpp.d.ts +2 -1
- package/dist/mpp.js +48 -19
- package/dist/mpp.js.map +1 -1
- package/dist/{near-D6C42VPE.js → near-FH7HPZV3.js} +15 -4
- package/dist/near-FH7HPZV3.js.map +1 -0
- package/dist/{protocol-adapter-C9odYl6l.d.ts → protocol-adapter-1WbtM3xG.d.ts} +12 -85
- package/dist/{runtime-Cl_BLVZV.d.ts → runtime-B-kARa2f.d.ts} +218 -15
- package/dist/store-kIIMpUA-.d.ts +372 -0
- package/dist/svm-KNC3PHMS.js +48 -0
- package/dist/svm-KNC3PHMS.js.map +1 -0
- package/dist/testing.d.ts +51 -0
- package/dist/testing.js +1896 -0
- package/dist/testing.js.map +1 -0
- package/dist/transaction-Dv0UFGg5.d.ts +129 -0
- package/dist/{types-j7MnUXYN.d.ts → types-DWqjz81-.d.ts} +5 -2
- package/dist/x402.d.ts +2 -1
- package/dist/x402.js +6 -5
- package/dist/x402.js.map +1 -1
- package/package.json +5 -1
- package/dist/authorization-DjAecwuW.d.ts +0 -115
- package/dist/chunk-CXWC5LWM.js.map +0 -1
- package/dist/chunk-DTKU3Z2M.js.map +0 -1
- package/dist/chunk-KA2AX5FH.js +0 -44
- package/dist/chunk-KA2AX5FH.js.map +0 -1
- package/dist/chunk-QPV3EGJO.js.map +0 -1
- package/dist/cli/ap2-GWSP4LYY.js.map +0 -1
- package/dist/cli/chunk-G2JEY7MB.js.map +0 -1
- package/dist/cli/chunk-GJIMDMYT.js +0 -639
- package/dist/cli/chunk-GJIMDMYT.js.map +0 -1
- package/dist/cli/chunk-ITWNSS2B.js +0 -8
- package/dist/cli/chunk-OSTCU6ZK.js +0 -44
- package/dist/cli/chunk-OSTCU6ZK.js.map +0 -1
- package/dist/cli/chunk-OZ4LAWBK.js.map +0 -1
- package/dist/cli/chunk-PIMFPCNZ.js.map +0 -1
- package/dist/cli/chunk-QQ3WY5L3.js.map +0 -1
- package/dist/cli/chunk-TNMLDPUO.js.map +0 -1
- package/dist/cli/config-UI4EDG4L.js +0 -11
- package/dist/cli/mcp-PGBRWZVA.js.map +0 -1
- package/dist/cli/near-5AY3HWYF.js.map +0 -1
- package/dist/cli/provider-I2ESL6RS.js.map +0 -1
- package/dist/cli/run-JK4ERPJV.js.map +0 -1
- package/dist/cli/svm-3MMYFPC7.js +0 -33
- package/dist/cli/svm-3MMYFPC7.js.map +0 -1
- package/dist/cli/x402-7WBFZQXV.js.map +0 -1
- package/dist/near-D6C42VPE.js.map +0 -1
- package/dist/svm-QEUAUNPQ.js +0 -33
- package/dist/svm-QEUAUNPQ.js.map +0 -1
|
@@ -1,11 +1,54 @@
|
|
|
1
|
-
import { SETTLE_AFTER_BACKEND_METADATA_KEY, REUSE_REJECTED_AUTHORIZATION_METADATA_KEY } from './chunk-
|
|
2
|
-
import { RESERVED_INPUT_FIELDS, NOOP_LOGGER, DEFAULT_BACKEND_TIMEOUT_MS,
|
|
1
|
+
import { SETTLE_AFTER_BACKEND_METADATA_KEY, REUSE_REJECTED_AUTHORIZATION_METADATA_KEY, canonicalJson } from './chunk-3NOMLMJW.js';
|
|
2
|
+
import { RESERVED_INPUT_FIELDS, NOOP_LOGGER, DEFAULT_BACKEND_TIMEOUT_MS, RECONCILIATION_METADATA_KEY } from './chunk-GET3CZGR.js';
|
|
3
|
+
import { CommerceError, isCommerceError, describeError, toCommerceError } from './chunk-SYQLJXIU.js';
|
|
3
4
|
import { isRecord } from './chunk-3M6U7ILB.js';
|
|
5
|
+
import { createHash } from 'crypto';
|
|
4
6
|
import { isDeepStrictEqual } from 'util';
|
|
5
7
|
|
|
6
8
|
// src/config/filename.ts
|
|
7
9
|
var DEFAULT_CONFIG_FILENAME = "config.yaml";
|
|
8
10
|
|
|
11
|
+
// src/protocols/acp/constants.ts
|
|
12
|
+
var ACP_SPEC_VERSION = "2026-04-17";
|
|
13
|
+
var ACP_API_VERSION = "2026-04-17";
|
|
14
|
+
var ACP_WELL_KNOWN_PATH = "/.well-known/acp.json";
|
|
15
|
+
var ACP_CHECKOUT_OPERATIONS = [
|
|
16
|
+
"createCheckoutSession",
|
|
17
|
+
"updateCheckoutSession",
|
|
18
|
+
"getCheckoutSession",
|
|
19
|
+
"completeCheckoutSession",
|
|
20
|
+
"cancelCheckoutSession"
|
|
21
|
+
];
|
|
22
|
+
var ACP_OPERATION_INPUT_KEYS = {
|
|
23
|
+
createCheckoutSession: ["body"],
|
|
24
|
+
updateCheckoutSession: ["path", "body"],
|
|
25
|
+
getCheckoutSession: ["path"],
|
|
26
|
+
completeCheckoutSession: ["path", "body"],
|
|
27
|
+
cancelCheckoutSession: ["path"]
|
|
28
|
+
};
|
|
29
|
+
var ACP_OPERATION_OPTIONAL_INPUT_KEYS = {
|
|
30
|
+
cancelCheckoutSession: ["body"]
|
|
31
|
+
};
|
|
32
|
+
var ACP_SESSION_ID_INPUT_KEY = "checkout_session_id";
|
|
33
|
+
var ACP_JSON_MEDIA_TYPE = "application/json";
|
|
34
|
+
var ACP_API_VERSION_HEADER = "api-version";
|
|
35
|
+
var ACP_REQUEST_ID_HEADER = "request-id";
|
|
36
|
+
var ACP_IDEMPOTENCY_KEY_HEADER = "idempotency-key";
|
|
37
|
+
var ACP_IDEMPOTENT_REPLAYED_HEADER = "idempotent-replayed";
|
|
38
|
+
var ACP_IN_FLIGHT_RETRY_AFTER_SECONDS = 1;
|
|
39
|
+
var ACP_MAX_REQUEST_ID_LENGTH = 128;
|
|
40
|
+
var ACP_FORWARDED_HEADERS = [
|
|
41
|
+
"accept-language",
|
|
42
|
+
"user-agent",
|
|
43
|
+
ACP_REQUEST_ID_HEADER,
|
|
44
|
+
ACP_API_VERSION_HEADER,
|
|
45
|
+
"signature",
|
|
46
|
+
"timestamp"
|
|
47
|
+
];
|
|
48
|
+
var ACP_MAX_FORWARDED_HEADER_LENGTH = 2048;
|
|
49
|
+
var ACP_MERCHANT_RETRY_AFTER_DEFAULT_SECONDS = 5;
|
|
50
|
+
var ACP_MERCHANT_RETRY_AFTER_MAX_SECONDS = 300;
|
|
51
|
+
|
|
9
52
|
// src/core/execution/backend-request.ts
|
|
10
53
|
var NEVER_FORWARDED_HEADER = /^(authorization|cookie|set-cookie|host|content-length|content-type|connection|keep-alive|te|trailer|transfer-encoding|upgrade|idempotency-key|proxy-.*)$/;
|
|
11
54
|
var PATH_PARAM_PATTERN = /\{([a-zA-Z0-9_.-]+)\}/g;
|
|
@@ -352,6 +395,56 @@ function safeStringify(value) {
|
|
|
352
395
|
function isAbortError(error) {
|
|
353
396
|
return error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError");
|
|
354
397
|
}
|
|
398
|
+
function createFlow(request, transactionId) {
|
|
399
|
+
return { request, transactionId, persistenceFailures: [], journaled: false };
|
|
400
|
+
}
|
|
401
|
+
function failuresOf(flow) {
|
|
402
|
+
return flow.persistenceFailures.length > 0 ? { persistenceFailures: [...flow.persistenceFailures] } : {};
|
|
403
|
+
}
|
|
404
|
+
function sha256(text) {
|
|
405
|
+
return `sha256:${createHash("sha256").update(text, "utf8").digest("hex")}`;
|
|
406
|
+
}
|
|
407
|
+
function inputDigest(input) {
|
|
408
|
+
const json = canonicalJson(input);
|
|
409
|
+
return json !== void 0 ? sha256(json) : void 0;
|
|
410
|
+
}
|
|
411
|
+
function configDigest(resource, requirement) {
|
|
412
|
+
const terms = {
|
|
413
|
+
resourceId: resource.id,
|
|
414
|
+
pricing: resource.pricing,
|
|
415
|
+
paymentMethods: resource.paymentMethods,
|
|
416
|
+
authorization: resource.authorization?.required ?? [],
|
|
417
|
+
requirement: {
|
|
418
|
+
provider: requirement.provider,
|
|
419
|
+
amount: requirement.amount,
|
|
420
|
+
currency: requirement.currency,
|
|
421
|
+
destination: requirement.destination ?? null,
|
|
422
|
+
network: requirement.network ?? null,
|
|
423
|
+
asset: requirement.asset ?? null,
|
|
424
|
+
settleAfterBackend: requirement.metadata?.[SETTLE_AFTER_BACKEND_METADATA_KEY] === true
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
return sha256(canonicalJson(terms) ?? JSON.stringify(terms));
|
|
428
|
+
}
|
|
429
|
+
function newTransaction(flow, resource, requirement, verification, validInput, authorization) {
|
|
430
|
+
const raw = verification.metadata?.[RECONCILIATION_METADATA_KEY];
|
|
431
|
+
const locator = isRecord(raw) ? raw : void 0;
|
|
432
|
+
const expiresAt = typeof locator?.["expiresAt"] === "string" ? locator["expiresAt"] : void 0;
|
|
433
|
+
const network = verification.network ?? requirement.network;
|
|
434
|
+
const asset = verification.asset ?? requirement.asset;
|
|
435
|
+
const digest = inputDigest(validInput);
|
|
436
|
+
return {
|
|
437
|
+
id: flow.transactionId,
|
|
438
|
+
protocol: flow.request.protocol,
|
|
439
|
+
...network !== void 0 ? { network } : {},
|
|
440
|
+
...asset !== void 0 ? { asset } : {},
|
|
441
|
+
...locator !== void 0 ? { locator } : {},
|
|
442
|
+
...expiresAt !== void 0 ? { expiresAt } : {},
|
|
443
|
+
...authorization !== void 0 ? { authorization } : {},
|
|
444
|
+
...digest !== void 0 ? { inputDigest: digest } : {},
|
|
445
|
+
configDigest: configDigest(resource, requirement)
|
|
446
|
+
};
|
|
447
|
+
}
|
|
355
448
|
var SUPPORTED_TYPES = /* @__PURE__ */ new Set([
|
|
356
449
|
"object",
|
|
357
450
|
"string",
|
|
@@ -494,16 +587,15 @@ function resolveAuthorizationProviders(providers, resource, requestId) {
|
|
|
494
587
|
}
|
|
495
588
|
function createAuthorizeAndReserve(ctx) {
|
|
496
589
|
const { options, safeEmit, buildEvent } = ctx;
|
|
497
|
-
async function authorizeAndReserve(request, resource, providers, requirement, input) {
|
|
590
|
+
async function authorizeAndReserve(flow, request, resource, providers, requirement, input) {
|
|
498
591
|
if (providers.length === 0) return void 0;
|
|
499
592
|
const submission = request.authorization;
|
|
500
593
|
const provider = providers.find((candidate) => candidate.name === submission?.method);
|
|
501
594
|
const unmet = providers.filter((candidate) => candidate !== provider).map((c) => c.name);
|
|
502
595
|
if (submission === void 0 || provider === void 0 || unmet.length > 0) {
|
|
503
596
|
await safeEmit(
|
|
504
|
-
buildEvent({
|
|
597
|
+
buildEvent(flow, {
|
|
505
598
|
type: "authorization.rejected",
|
|
506
|
-
requestId: request.requestId,
|
|
507
599
|
resourceId: resource.id,
|
|
508
600
|
adapter: request.protocol,
|
|
509
601
|
status: "error",
|
|
@@ -540,9 +632,8 @@ function createAuthorizeAndReserve(ctx) {
|
|
|
540
632
|
}
|
|
541
633
|
);
|
|
542
634
|
await safeEmit(
|
|
543
|
-
buildEvent({
|
|
635
|
+
buildEvent(flow, {
|
|
544
636
|
type: "authorization.rejected",
|
|
545
|
-
requestId: request.requestId,
|
|
546
637
|
resourceId: resource.id,
|
|
547
638
|
adapter: request.protocol,
|
|
548
639
|
status: "error",
|
|
@@ -552,9 +643,8 @@ function createAuthorizeAndReserve(ctx) {
|
|
|
552
643
|
throw mapped;
|
|
553
644
|
}
|
|
554
645
|
await safeEmit(
|
|
555
|
-
buildEvent({
|
|
646
|
+
buildEvent(flow, {
|
|
556
647
|
type: "authorization.verified",
|
|
557
|
-
requestId: request.requestId,
|
|
558
648
|
resourceId: resource.id,
|
|
559
649
|
adapter: request.protocol,
|
|
560
650
|
status: "ok",
|
|
@@ -568,14 +658,17 @@ function createAuthorizeAndReserve(ctx) {
|
|
|
568
658
|
reference: verification.reference,
|
|
569
659
|
...verification.metadata !== void 0 ? { metadata: verification.metadata } : {}
|
|
570
660
|
},
|
|
661
|
+
reservationId: verification.reservationId,
|
|
571
662
|
async finalize(action) {
|
|
572
663
|
try {
|
|
573
664
|
await provider[action](verification.reservationId, context);
|
|
665
|
+
return true;
|
|
574
666
|
} catch (error) {
|
|
575
667
|
options.logger.error(
|
|
576
668
|
{ err: describeError(error), requestId: request.requestId, action },
|
|
577
669
|
"Authorization finalization failed; the reservation stays reserved"
|
|
578
670
|
);
|
|
671
|
+
return false;
|
|
579
672
|
}
|
|
580
673
|
}
|
|
581
674
|
};
|
|
@@ -595,10 +688,12 @@ function retryChallenge(requirement, reason) {
|
|
|
595
688
|
};
|
|
596
689
|
}
|
|
597
690
|
var FINISHED_ATTEMPT_STATUSES = /* @__PURE__ */ new Set(["settled", "rejected"]);
|
|
691
|
+
function isFinishedAttempt(status) {
|
|
692
|
+
return typeof status === "string" && FINISHED_ATTEMPT_STATUSES.has(status);
|
|
693
|
+
}
|
|
598
694
|
function replayError(error, requirement) {
|
|
599
695
|
if (error.code !== "PAYMENT_REPLAYED") return error;
|
|
600
|
-
const
|
|
601
|
-
const finished = typeof status === "string" && FINISHED_ATTEMPT_STATUSES.has(status);
|
|
696
|
+
const finished = isFinishedAttempt(error.details?.["attemptStatus"]);
|
|
602
697
|
const network = requirement.network;
|
|
603
698
|
if (!finished && network === void 0) return error;
|
|
604
699
|
return new CommerceError("PAYMENT_REPLAYED", error.message, {
|
|
@@ -616,7 +711,7 @@ function replayError(error, requirement) {
|
|
|
616
711
|
// src/core/execution/pipeline/admission.ts
|
|
617
712
|
function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
618
713
|
const { options, safeEmit, buildEvent } = ctx;
|
|
619
|
-
return async function admitPayment(request, resource, pricing, validInput) {
|
|
714
|
+
return async function admitPayment(flow, request, resource, pricing, validInput) {
|
|
620
715
|
const provider = pickProvider(options.paymentProviders, resource.paymentMethods);
|
|
621
716
|
if (!provider) {
|
|
622
717
|
throw new CommerceError(
|
|
@@ -655,9 +750,8 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
655
750
|
}
|
|
656
751
|
if (!request.payment) {
|
|
657
752
|
await safeEmit(
|
|
658
|
-
buildEvent({
|
|
753
|
+
buildEvent(flow, {
|
|
659
754
|
type: "payment.required",
|
|
660
|
-
requestId: request.requestId,
|
|
661
755
|
resourceId: resource.id,
|
|
662
756
|
adapter: request.protocol,
|
|
663
757
|
paymentProvider: provider.name
|
|
@@ -666,6 +760,7 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
666
760
|
return {
|
|
667
761
|
kind: "payment-required",
|
|
668
762
|
requestId: request.requestId,
|
|
763
|
+
transactionId: flow.transactionId,
|
|
669
764
|
resourceId: resource.id,
|
|
670
765
|
requirement,
|
|
671
766
|
...authProviders.length > 0 ? { authorization: authProviders.map((p) => p.requirement) } : {}
|
|
@@ -696,9 +791,8 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
696
791
|
...retryChallenge(requirement)
|
|
697
792
|
});
|
|
698
793
|
await safeEmit(
|
|
699
|
-
buildEvent({
|
|
794
|
+
buildEvent(flow, {
|
|
700
795
|
type: "payment.rejected",
|
|
701
|
-
requestId: request.requestId,
|
|
702
796
|
resourceId: resource.id,
|
|
703
797
|
adapter: request.protocol,
|
|
704
798
|
paymentProvider: provider.name,
|
|
@@ -709,32 +803,23 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
709
803
|
throw mapped;
|
|
710
804
|
}
|
|
711
805
|
if (verification.status !== "verified") {
|
|
806
|
+
const refusal = await refusalFor(verification, requirement, request, resource);
|
|
712
807
|
await safeEmit(
|
|
713
|
-
buildEvent({
|
|
808
|
+
buildEvent(flow, {
|
|
714
809
|
type: "payment.rejected",
|
|
715
|
-
requestId: request.requestId,
|
|
716
810
|
resourceId: resource.id,
|
|
717
811
|
adapter: request.protocol,
|
|
718
812
|
paymentProvider: provider.name,
|
|
719
813
|
status: "error",
|
|
720
|
-
data: verification.rejectionReason ? { reason: verification.rejectionReason } : {}
|
|
814
|
+
data: refusal.code !== "PAYMENT_INVALID" ? { reason: refusal.code } : verification.rejectionReason ? { reason: verification.rejectionReason } : {}
|
|
721
815
|
})
|
|
722
816
|
);
|
|
723
|
-
throw
|
|
724
|
-
"PAYMENT_INVALID",
|
|
725
|
-
verification.rejectionReason ?? "Payment was rejected",
|
|
726
|
-
{
|
|
727
|
-
requestId: request.requestId,
|
|
728
|
-
resourceId: resource.id,
|
|
729
|
-
...retryChallenge(requirement, verification.rejectionReason)
|
|
730
|
-
}
|
|
731
|
-
);
|
|
817
|
+
throw refusal;
|
|
732
818
|
}
|
|
733
819
|
if (!verification.replayKey) {
|
|
734
820
|
await safeEmit(
|
|
735
|
-
buildEvent({
|
|
821
|
+
buildEvent(flow, {
|
|
736
822
|
type: "payment.rejected",
|
|
737
|
-
requestId: request.requestId,
|
|
738
823
|
resourceId: resource.id,
|
|
739
824
|
adapter: request.protocol,
|
|
740
825
|
paymentProvider: provider.name,
|
|
@@ -749,6 +834,7 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
749
834
|
}
|
|
750
835
|
const replayKey = verification.replayKey;
|
|
751
836
|
const hold = await authorizeAndReserve(
|
|
837
|
+
flow,
|
|
752
838
|
request,
|
|
753
839
|
resource,
|
|
754
840
|
authProviders,
|
|
@@ -756,7 +842,7 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
756
842
|
validInput
|
|
757
843
|
);
|
|
758
844
|
try {
|
|
759
|
-
await options.store.reservePaymentAttempt({
|
|
845
|
+
const attempt = await options.store.reservePaymentAttempt({
|
|
760
846
|
requestId: request.requestId,
|
|
761
847
|
resourceId: resource.id,
|
|
762
848
|
provider: provider.name,
|
|
@@ -765,8 +851,17 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
765
851
|
currency: verification.currency,
|
|
766
852
|
...verification.payer !== void 0 ? { payer: verification.payer } : {},
|
|
767
853
|
...verification.payee !== void 0 ? { payee: verification.payee } : {},
|
|
768
|
-
...requirement.metadata?.[REUSE_REJECTED_AUTHORIZATION_METADATA_KEY] === true ? { reclaimRejected: true } : {}
|
|
854
|
+
...requirement.metadata?.[REUSE_REJECTED_AUTHORIZATION_METADATA_KEY] === true ? { reclaimRejected: true } : {},
|
|
855
|
+
transaction: newTransaction(
|
|
856
|
+
flow,
|
|
857
|
+
resource,
|
|
858
|
+
requirement,
|
|
859
|
+
verification,
|
|
860
|
+
validInput,
|
|
861
|
+
hold !== void 0 ? { ...hold.record, reservationId: hold.reservationId } : void 0
|
|
862
|
+
)
|
|
769
863
|
});
|
|
864
|
+
flow.journaled = attempt.transactionId === flow.transactionId && options.store.recordTransactionStep !== void 0;
|
|
770
865
|
} catch (error) {
|
|
771
866
|
const mapped = toCommerceError(
|
|
772
867
|
error,
|
|
@@ -775,9 +870,8 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
775
870
|
);
|
|
776
871
|
await hold?.finalize("release");
|
|
777
872
|
await safeEmit(
|
|
778
|
-
buildEvent({
|
|
873
|
+
buildEvent(flow, {
|
|
779
874
|
type: "payment.rejected",
|
|
780
|
-
requestId: request.requestId,
|
|
781
875
|
resourceId: resource.id,
|
|
782
876
|
adapter: request.protocol,
|
|
783
877
|
paymentProvider: provider.name,
|
|
@@ -788,9 +882,8 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
788
882
|
throw replayError(mapped, requirement);
|
|
789
883
|
}
|
|
790
884
|
await safeEmit(
|
|
791
|
-
buildEvent({
|
|
885
|
+
buildEvent(flow, {
|
|
792
886
|
type: "payment.verified",
|
|
793
|
-
requestId: request.requestId,
|
|
794
887
|
resourceId: resource.id,
|
|
795
888
|
adapter: request.protocol,
|
|
796
889
|
paymentProvider: provider.name,
|
|
@@ -800,6 +893,7 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
800
893
|
return {
|
|
801
894
|
kind: "admitted",
|
|
802
895
|
settleArgs: {
|
|
896
|
+
flow,
|
|
803
897
|
request,
|
|
804
898
|
resource,
|
|
805
899
|
provider,
|
|
@@ -811,6 +905,37 @@ function createPaymentAdmission(ctx, authorizeAndReserve) {
|
|
|
811
905
|
}
|
|
812
906
|
};
|
|
813
907
|
};
|
|
908
|
+
async function refusalFor(verification, requirement, request, resource) {
|
|
909
|
+
const ids = { requestId: request.requestId, resourceId: resource.id };
|
|
910
|
+
if (verification.replayKey !== void 0) {
|
|
911
|
+
let attempt;
|
|
912
|
+
try {
|
|
913
|
+
attempt = await options.store.findPaymentAttemptByReplayKey?.(verification.replayKey);
|
|
914
|
+
} catch (error) {
|
|
915
|
+
return new CommerceError("STORAGE_ERROR", "Payment attempt could not be read", {
|
|
916
|
+
...ids,
|
|
917
|
+
cause: error
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
if (attempt !== void 0 && !isFinishedAttempt(attempt.status)) {
|
|
921
|
+
return replayError(
|
|
922
|
+
new CommerceError("PAYMENT_REPLAYED", "Payment authorization has already been used", {
|
|
923
|
+
...ids,
|
|
924
|
+
details: { attemptStatus: attempt.status }
|
|
925
|
+
}),
|
|
926
|
+
requirement
|
|
927
|
+
);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
return new CommerceError(
|
|
931
|
+
"PAYMENT_INVALID",
|
|
932
|
+
verification.rejectionReason ?? "Payment was rejected",
|
|
933
|
+
{
|
|
934
|
+
...ids,
|
|
935
|
+
...retryChallenge(requirement, verification.rejectionReason)
|
|
936
|
+
}
|
|
937
|
+
);
|
|
938
|
+
}
|
|
814
939
|
}
|
|
815
940
|
function pickProvider(providers, methods) {
|
|
816
941
|
for (const method of methods) {
|
|
@@ -821,6 +946,11 @@ function pickProvider(providers, methods) {
|
|
|
821
946
|
}
|
|
822
947
|
|
|
823
948
|
// src/core/execution/pipeline/context.ts
|
|
949
|
+
var HOLD_STATES = {
|
|
950
|
+
consume: "consumed",
|
|
951
|
+
release: "released",
|
|
952
|
+
markUncertain: "uncertain"
|
|
953
|
+
};
|
|
824
954
|
function createPipelineContext(options) {
|
|
825
955
|
async function safeEmit(event) {
|
|
826
956
|
try {
|
|
@@ -832,27 +962,73 @@ function createPipelineContext(options) {
|
|
|
832
962
|
);
|
|
833
963
|
}
|
|
834
964
|
}
|
|
835
|
-
async function safePersist(op, label,
|
|
965
|
+
async function safePersist(flow, op, label, failure) {
|
|
836
966
|
try {
|
|
837
967
|
await op();
|
|
968
|
+
return true;
|
|
838
969
|
} catch (error) {
|
|
839
970
|
options.logger.error(
|
|
840
|
-
{
|
|
971
|
+
{
|
|
972
|
+
err: describeError(error),
|
|
973
|
+
requestId: flow.request.requestId,
|
|
974
|
+
transactionId: flow.transactionId
|
|
975
|
+
},
|
|
841
976
|
`Persistence failed: ${label}`
|
|
842
977
|
);
|
|
978
|
+
if (failure !== void 0 && !flow.persistenceFailures.includes(failure)) {
|
|
979
|
+
flow.persistenceFailures.push(failure);
|
|
980
|
+
}
|
|
981
|
+
return false;
|
|
843
982
|
}
|
|
844
983
|
}
|
|
845
|
-
function
|
|
846
|
-
|
|
984
|
+
async function recordStep(flow, step) {
|
|
985
|
+
const record = options.store.recordTransactionStep;
|
|
986
|
+
if (!flow.journaled || record === void 0) return;
|
|
987
|
+
const full = {
|
|
988
|
+
...step,
|
|
989
|
+
transactionId: flow.transactionId,
|
|
990
|
+
at: options.clock.nowIso()
|
|
991
|
+
};
|
|
992
|
+
await safePersist(
|
|
993
|
+
flow,
|
|
994
|
+
() => record.call(options.store, full),
|
|
995
|
+
`recordTransactionStep(${step.kind})`,
|
|
996
|
+
"journal"
|
|
997
|
+
);
|
|
847
998
|
}
|
|
848
|
-
|
|
999
|
+
async function finalizeHold(flow, hold, action) {
|
|
1000
|
+
if (hold !== void 0 && await hold.finalize(action)) {
|
|
1001
|
+
await recordStep(flow, { kind: "hold", hold: HOLD_STATES[action] });
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
function buildEvent(flow, partial) {
|
|
1005
|
+
return {
|
|
1006
|
+
id: options.ids.next("evt"),
|
|
1007
|
+
at: options.clock.nowIso(),
|
|
1008
|
+
requestId: flow.request.requestId,
|
|
1009
|
+
transactionId: flow.transactionId,
|
|
1010
|
+
...partial
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
return { options, safeEmit, safePersist, recordStep, finalizeHold, buildEvent };
|
|
849
1014
|
}
|
|
850
1015
|
|
|
851
1016
|
// src/core/execution/pipeline/settlement.ts
|
|
852
1017
|
function createSettlement(ctx) {
|
|
853
|
-
const { options, safeEmit, safePersist, buildEvent } = ctx;
|
|
1018
|
+
const { options, safeEmit, safePersist, recordStep, finalizeHold, buildEvent } = ctx;
|
|
854
1019
|
async function settle(args) {
|
|
855
|
-
const {
|
|
1020
|
+
const {
|
|
1021
|
+
flow,
|
|
1022
|
+
request,
|
|
1023
|
+
resource,
|
|
1024
|
+
provider,
|
|
1025
|
+
requirement,
|
|
1026
|
+
payment,
|
|
1027
|
+
verification,
|
|
1028
|
+
replayKey,
|
|
1029
|
+
hold
|
|
1030
|
+
} = args;
|
|
1031
|
+
await recordStep(flow, { kind: "settling" });
|
|
856
1032
|
let settlement;
|
|
857
1033
|
try {
|
|
858
1034
|
settlement = await provider.settle({
|
|
@@ -865,25 +1041,29 @@ function createSettlement(ctx) {
|
|
|
865
1041
|
} catch (error) {
|
|
866
1042
|
const notSubmitted = notSubmittedReason(error);
|
|
867
1043
|
if (notSubmitted !== void 0) {
|
|
868
|
-
await hold
|
|
1044
|
+
await finalizeHold(flow, hold, "release");
|
|
869
1045
|
await safePersist(
|
|
1046
|
+
flow,
|
|
870
1047
|
() => options.store.updatePaymentAttempt({
|
|
871
1048
|
replayKey,
|
|
872
1049
|
status: "rejected",
|
|
873
1050
|
rejectionReason: notSubmitted
|
|
874
1051
|
}),
|
|
875
1052
|
"updatePaymentAttempt(not-submitted)",
|
|
876
|
-
|
|
1053
|
+
"payment-attempt"
|
|
877
1054
|
);
|
|
878
1055
|
await safeEmit(
|
|
879
|
-
buildEvent({
|
|
1056
|
+
buildEvent(flow, {
|
|
880
1057
|
type: "payment.rejected",
|
|
881
|
-
requestId: request.requestId,
|
|
882
1058
|
resourceId: resource.id,
|
|
883
1059
|
adapter: request.protocol,
|
|
884
1060
|
paymentProvider: provider.name,
|
|
885
1061
|
status: "error",
|
|
886
|
-
data: {
|
|
1062
|
+
data: {
|
|
1063
|
+
reason: "settlement-not-submitted",
|
|
1064
|
+
providerReason: notSubmitted,
|
|
1065
|
+
...failuresOf(flow)
|
|
1066
|
+
}
|
|
887
1067
|
})
|
|
888
1068
|
);
|
|
889
1069
|
throw new CommerceError(
|
|
@@ -893,8 +1073,9 @@ function createSettlement(ctx) {
|
|
|
893
1073
|
);
|
|
894
1074
|
}
|
|
895
1075
|
const txHash = settlementTxHash(error);
|
|
896
|
-
await hold
|
|
1076
|
+
await finalizeHold(flow, hold, "markUncertain");
|
|
897
1077
|
await safePersist(
|
|
1078
|
+
flow,
|
|
898
1079
|
() => options.store.updatePaymentAttempt({
|
|
899
1080
|
replayKey,
|
|
900
1081
|
status: "settlement-uncertain",
|
|
@@ -902,19 +1083,19 @@ function createSettlement(ctx) {
|
|
|
902
1083
|
...error instanceof Error ? { rejectionReason: error.message } : {}
|
|
903
1084
|
}),
|
|
904
1085
|
"updatePaymentAttempt(settlement-uncertain)",
|
|
905
|
-
|
|
1086
|
+
"payment-attempt"
|
|
906
1087
|
);
|
|
907
1088
|
await safeEmit(
|
|
908
|
-
buildEvent({
|
|
1089
|
+
buildEvent(flow, {
|
|
909
1090
|
type: "payment.rejected",
|
|
910
|
-
requestId: request.requestId,
|
|
911
1091
|
resourceId: resource.id,
|
|
912
1092
|
adapter: request.protocol,
|
|
913
1093
|
paymentProvider: provider.name,
|
|
914
1094
|
status: "error",
|
|
915
1095
|
data: {
|
|
916
1096
|
reason: "settlement-uncertain",
|
|
917
|
-
...txHash !== void 0 ? { transactionHash: txHash } : {}
|
|
1097
|
+
...txHash !== void 0 ? { transactionHash: txHash } : {},
|
|
1098
|
+
...failuresOf(flow)
|
|
918
1099
|
}
|
|
919
1100
|
})
|
|
920
1101
|
);
|
|
@@ -930,8 +1111,9 @@ function createSettlement(ctx) {
|
|
|
930
1111
|
});
|
|
931
1112
|
}
|
|
932
1113
|
if (settlement.status !== "settled") {
|
|
933
|
-
await hold
|
|
1114
|
+
await finalizeHold(flow, hold, "release");
|
|
934
1115
|
await safePersist(
|
|
1116
|
+
flow,
|
|
935
1117
|
() => options.store.updatePaymentAttempt({
|
|
936
1118
|
replayKey,
|
|
937
1119
|
status: "rejected",
|
|
@@ -939,17 +1121,19 @@ function createSettlement(ctx) {
|
|
|
939
1121
|
...settlement.externalReference !== void 0 ? { externalReference: settlement.externalReference } : {}
|
|
940
1122
|
}),
|
|
941
1123
|
"updatePaymentAttempt(rejected)",
|
|
942
|
-
|
|
1124
|
+
"payment-attempt"
|
|
943
1125
|
);
|
|
944
1126
|
await safeEmit(
|
|
945
|
-
buildEvent({
|
|
1127
|
+
buildEvent(flow, {
|
|
946
1128
|
type: "payment.rejected",
|
|
947
|
-
requestId: request.requestId,
|
|
948
1129
|
resourceId: resource.id,
|
|
949
1130
|
adapter: request.protocol,
|
|
950
1131
|
paymentProvider: provider.name,
|
|
951
1132
|
status: "error",
|
|
952
|
-
data:
|
|
1133
|
+
data: {
|
|
1134
|
+
...settlement.rejectionReason ? { reason: settlement.rejectionReason } : {},
|
|
1135
|
+
...failuresOf(flow)
|
|
1136
|
+
}
|
|
953
1137
|
})
|
|
954
1138
|
);
|
|
955
1139
|
const rejectionDetails = retryChallenge(requirement, settlement.rejectionReason).details;
|
|
@@ -967,20 +1151,20 @@ function createSettlement(ctx) {
|
|
|
967
1151
|
}
|
|
968
1152
|
);
|
|
969
1153
|
}
|
|
970
|
-
await hold
|
|
1154
|
+
await finalizeHold(flow, hold, "consume");
|
|
971
1155
|
await safePersist(
|
|
1156
|
+
flow,
|
|
972
1157
|
() => options.store.updatePaymentAttempt({
|
|
973
1158
|
replayKey,
|
|
974
1159
|
status: "settled",
|
|
975
1160
|
...settlement.externalReference !== void 0 ? { externalReference: settlement.externalReference } : {}
|
|
976
1161
|
}),
|
|
977
1162
|
"updatePaymentAttempt(settled)",
|
|
978
|
-
|
|
1163
|
+
"payment-attempt"
|
|
979
1164
|
);
|
|
980
1165
|
await safeEmit(
|
|
981
|
-
buildEvent({
|
|
1166
|
+
buildEvent(flow, {
|
|
982
1167
|
type: "payment.settled",
|
|
983
|
-
requestId: request.requestId,
|
|
984
1168
|
resourceId: resource.id,
|
|
985
1169
|
adapter: request.protocol,
|
|
986
1170
|
paymentProvider: provider.name,
|
|
@@ -990,26 +1174,26 @@ function createSettlement(ctx) {
|
|
|
990
1174
|
return settlement;
|
|
991
1175
|
}
|
|
992
1176
|
async function abandonSettlement(args, backendErrorCode) {
|
|
993
|
-
const { request, resource, provider, replayKey, hold } = args;
|
|
994
|
-
await hold
|
|
1177
|
+
const { flow, request, resource, provider, replayKey, hold } = args;
|
|
1178
|
+
await finalizeHold(flow, hold, "release");
|
|
995
1179
|
await safePersist(
|
|
1180
|
+
flow,
|
|
996
1181
|
() => options.store.updatePaymentAttempt({
|
|
997
1182
|
replayKey,
|
|
998
1183
|
status: "rejected",
|
|
999
1184
|
rejectionReason: "backend_failed"
|
|
1000
1185
|
}),
|
|
1001
1186
|
"updatePaymentAttempt(backend-failed)",
|
|
1002
|
-
|
|
1187
|
+
"payment-attempt"
|
|
1003
1188
|
);
|
|
1004
1189
|
await safeEmit(
|
|
1005
|
-
buildEvent({
|
|
1190
|
+
buildEvent(flow, {
|
|
1006
1191
|
type: "payment.rejected",
|
|
1007
|
-
requestId: request.requestId,
|
|
1008
1192
|
resourceId: resource.id,
|
|
1009
1193
|
adapter: request.protocol,
|
|
1010
1194
|
paymentProvider: provider.name,
|
|
1011
1195
|
status: "error",
|
|
1012
|
-
data: { reason: "backend-failed", backendErrorCode }
|
|
1196
|
+
data: { reason: "backend-failed", backendErrorCode, ...failuresOf(flow) }
|
|
1013
1197
|
})
|
|
1014
1198
|
);
|
|
1015
1199
|
}
|
|
@@ -1046,7 +1230,7 @@ function createExecutionPipeline(options) {
|
|
|
1046
1230
|
return compiled;
|
|
1047
1231
|
}
|
|
1048
1232
|
const ctx = createPipelineContext(options);
|
|
1049
|
-
const { safeEmit, safePersist, buildEvent } = ctx;
|
|
1233
|
+
const { safeEmit, safePersist, recordStep, buildEvent } = ctx;
|
|
1050
1234
|
const { settle, abandonSettlement } = createSettlement(ctx);
|
|
1051
1235
|
const admitPayment = createPaymentAdmission(ctx, createAuthorizeAndReserve(ctx));
|
|
1052
1236
|
async function execute(request) {
|
|
@@ -1062,10 +1246,10 @@ function createExecutionPipeline(options) {
|
|
|
1062
1246
|
}
|
|
1063
1247
|
);
|
|
1064
1248
|
}
|
|
1249
|
+
const flow = createFlow(request, options.ids.next("txn"));
|
|
1065
1250
|
await safeEmit(
|
|
1066
|
-
buildEvent({
|
|
1251
|
+
buildEvent(flow, {
|
|
1067
1252
|
type: "resource.requested",
|
|
1068
|
-
requestId: request.requestId,
|
|
1069
1253
|
resourceId: resource.id,
|
|
1070
1254
|
adapter: request.protocol
|
|
1071
1255
|
})
|
|
@@ -1110,20 +1294,34 @@ function createExecutionPipeline(options) {
|
|
|
1110
1294
|
}
|
|
1111
1295
|
);
|
|
1112
1296
|
}
|
|
1113
|
-
let
|
|
1114
|
-
let authorization;
|
|
1115
|
-
let deferredSettlement;
|
|
1297
|
+
let settleArgs;
|
|
1116
1298
|
if (resource.pricing.type === "fixed") {
|
|
1117
|
-
const admission = await admitPayment(request, resource, resource.pricing, validInput);
|
|
1299
|
+
const admission = await admitPayment(flow, request, resource, resource.pricing, validInput);
|
|
1118
1300
|
if (admission.kind === "payment-required") return admission;
|
|
1119
|
-
|
|
1120
|
-
|
|
1301
|
+
settleArgs = admission.settleArgs;
|
|
1302
|
+
}
|
|
1303
|
+
try {
|
|
1304
|
+
return await settleAndDeliver(flow, resource, validInput, settleArgs, pipelineStart);
|
|
1305
|
+
} finally {
|
|
1306
|
+
await recordStep(flow, { kind: "completed", ...failuresOf(flow) });
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
async function settleAndDeliver(flow, resource, validInput, settleArgs, pipelineStart) {
|
|
1310
|
+
const { request } = flow;
|
|
1311
|
+
const authorization = settleArgs?.hold?.record;
|
|
1312
|
+
let paymentResult;
|
|
1313
|
+
let deferredSettlement;
|
|
1314
|
+
if (settleArgs !== void 0) {
|
|
1121
1315
|
if (settleArgs.requirement.metadata?.[SETTLE_AFTER_BACKEND_METADATA_KEY] === true) {
|
|
1122
1316
|
deferredSettlement = settleArgs;
|
|
1123
1317
|
} else {
|
|
1124
1318
|
paymentResult = await settle(settleArgs);
|
|
1125
1319
|
}
|
|
1126
1320
|
}
|
|
1321
|
+
await recordStep(flow, {
|
|
1322
|
+
kind: "backend-dispatched",
|
|
1323
|
+
deadlineAt: backendDeadline(options.clock.now(), resource)
|
|
1324
|
+
});
|
|
1127
1325
|
const backendStart = options.clock.monotonicMs();
|
|
1128
1326
|
let backendResponse;
|
|
1129
1327
|
try {
|
|
@@ -1136,18 +1334,23 @@ function createExecutionPipeline(options) {
|
|
|
1136
1334
|
});
|
|
1137
1335
|
} catch (error) {
|
|
1138
1336
|
const commerceError = toCommerceError(error, "BACKEND_ERROR", "Backend call failed");
|
|
1139
|
-
await
|
|
1140
|
-
|
|
1337
|
+
await recordStep(flow, {
|
|
1338
|
+
kind: "backend-result",
|
|
1339
|
+
status: backendErrorStatus(commerceError),
|
|
1340
|
+
errorCode: commerceError.code
|
|
1341
|
+
});
|
|
1342
|
+
const backendFailed = (data = {}) => safeEmit(
|
|
1343
|
+
buildEvent(flow, {
|
|
1141
1344
|
type: "backend.failed",
|
|
1142
|
-
requestId: request.requestId,
|
|
1143
1345
|
resourceId: resource.id,
|
|
1144
1346
|
adapter: request.protocol,
|
|
1145
1347
|
status: "error",
|
|
1146
1348
|
durationMs: Math.round(options.clock.monotonicMs() - backendStart),
|
|
1147
|
-
data: { code: commerceError.code }
|
|
1349
|
+
data: { code: commerceError.code, ...data }
|
|
1148
1350
|
})
|
|
1149
1351
|
);
|
|
1150
1352
|
if (deferredSettlement !== void 0) {
|
|
1353
|
+
await backendFailed();
|
|
1151
1354
|
await abandonSettlement(deferredSettlement, commerceError.code);
|
|
1152
1355
|
throw commerceError;
|
|
1153
1356
|
}
|
|
@@ -1161,13 +1364,11 @@ function createExecutionPipeline(options) {
|
|
|
1161
1364
|
protocol: request.protocol,
|
|
1162
1365
|
payment: paymentResult,
|
|
1163
1366
|
...authorization !== void 0 ? { authorization } : {},
|
|
1164
|
-
metadata: { delivered: false, backendErrorCode: commerceError.code }
|
|
1367
|
+
metadata: { delivered: false, backendErrorCode: commerceError.code },
|
|
1368
|
+
transactionId: flow.transactionId
|
|
1165
1369
|
};
|
|
1166
|
-
await
|
|
1167
|
-
|
|
1168
|
-
"saveReceipt(backend-failed-after-settlement)",
|
|
1169
|
-
request.requestId
|
|
1170
|
-
);
|
|
1370
|
+
await saveReceipt(flow, failedReceipt, "saveReceipt(backend-failed-after-settlement)");
|
|
1371
|
+
await backendFailed(failuresOf(flow));
|
|
1171
1372
|
throw new CommerceError(
|
|
1172
1373
|
"BACKEND_ERROR",
|
|
1173
1374
|
"Payment settled but the backend call failed; the payment was not refunded automatically",
|
|
@@ -1191,12 +1392,13 @@ function createExecutionPipeline(options) {
|
|
|
1191
1392
|
}
|
|
1192
1393
|
);
|
|
1193
1394
|
}
|
|
1395
|
+
await backendFailed();
|
|
1194
1396
|
throw commerceError;
|
|
1195
1397
|
}
|
|
1398
|
+
await recordStep(flow, { kind: "backend-result", status: backendResponse.status });
|
|
1196
1399
|
await safeEmit(
|
|
1197
|
-
buildEvent({
|
|
1400
|
+
buildEvent(flow, {
|
|
1198
1401
|
type: "backend.called",
|
|
1199
|
-
requestId: request.requestId,
|
|
1200
1402
|
resourceId: resource.id,
|
|
1201
1403
|
adapter: request.protocol,
|
|
1202
1404
|
status: "ok",
|
|
@@ -1216,34 +1418,48 @@ function createExecutionPipeline(options) {
|
|
|
1216
1418
|
durationMs: backendResponse.durationMs,
|
|
1217
1419
|
protocol: request.protocol,
|
|
1218
1420
|
...paymentResult !== void 0 ? { payment: paymentResult } : {},
|
|
1219
|
-
...authorization !== void 0 ? { authorization } : {}
|
|
1421
|
+
...authorization !== void 0 ? { authorization } : {},
|
|
1422
|
+
transactionId: flow.transactionId
|
|
1220
1423
|
};
|
|
1221
|
-
await
|
|
1424
|
+
await saveReceipt(flow, receipt, "saveReceipt");
|
|
1222
1425
|
const totalDurationMs = Math.round(options.clock.monotonicMs() - pipelineStart);
|
|
1426
|
+
const failures = failuresOf(flow);
|
|
1223
1427
|
await safeEmit(
|
|
1224
|
-
buildEvent({
|
|
1428
|
+
buildEvent(flow, {
|
|
1225
1429
|
type: "resource.delivered",
|
|
1226
|
-
requestId: request.requestId,
|
|
1227
1430
|
resourceId: resource.id,
|
|
1228
1431
|
adapter: request.protocol,
|
|
1229
1432
|
status: "ok",
|
|
1230
|
-
durationMs: totalDurationMs
|
|
1433
|
+
durationMs: totalDurationMs,
|
|
1434
|
+
...failures.persistenceFailures !== void 0 ? { data: failures } : {}
|
|
1231
1435
|
})
|
|
1232
1436
|
);
|
|
1233
1437
|
return {
|
|
1234
1438
|
kind: "delivered",
|
|
1235
1439
|
requestId: request.requestId,
|
|
1440
|
+
transactionId: flow.transactionId,
|
|
1236
1441
|
resourceId: resource.id,
|
|
1237
1442
|
backendStatus: backendResponse.status,
|
|
1238
1443
|
body: backendResponse.body,
|
|
1239
1444
|
headers: backendResponse.headers,
|
|
1240
1445
|
...paymentResult !== void 0 ? { payment: paymentResult } : {},
|
|
1241
1446
|
receipt,
|
|
1242
|
-
durationMs: totalDurationMs
|
|
1447
|
+
durationMs: totalDurationMs,
|
|
1448
|
+
...failures
|
|
1243
1449
|
};
|
|
1244
1450
|
}
|
|
1451
|
+
async function saveReceipt(flow, receipt, label) {
|
|
1452
|
+
if (await safePersist(flow, () => options.store.saveReceipt(receipt), label, "receipt")) {
|
|
1453
|
+
await recordStep(flow, { kind: "receipt", receiptId: receipt.id });
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1245
1456
|
return { execute };
|
|
1246
1457
|
}
|
|
1458
|
+
var BACKEND_DEADLINE_MARGIN_MS = 5e3;
|
|
1459
|
+
function backendDeadline(dispatchedAt, resource) {
|
|
1460
|
+
const timeoutMs = resource.handler.timeoutMs ?? DEFAULT_BACKEND_TIMEOUT_MS;
|
|
1461
|
+
return new Date(dispatchedAt.getTime() + timeoutMs + BACKEND_DEADLINE_MARGIN_MS).toISOString();
|
|
1462
|
+
}
|
|
1247
1463
|
function backendErrorStatus(error) {
|
|
1248
1464
|
const status = error.details?.["status"];
|
|
1249
1465
|
return typeof status === "number" ? status : 0;
|
|
@@ -1307,47 +1523,6 @@ function createStoreEventSink(options) {
|
|
|
1307
1523
|
};
|
|
1308
1524
|
}
|
|
1309
1525
|
|
|
1310
|
-
// src/protocols/acp/constants.ts
|
|
1311
|
-
var ACP_SPEC_VERSION = "2026-04-17";
|
|
1312
|
-
var ACP_API_VERSION = "2026-04-17";
|
|
1313
|
-
var ACP_WELL_KNOWN_PATH = "/.well-known/acp.json";
|
|
1314
|
-
var ACP_CHECKOUT_OPERATIONS = [
|
|
1315
|
-
"createCheckoutSession",
|
|
1316
|
-
"updateCheckoutSession",
|
|
1317
|
-
"getCheckoutSession",
|
|
1318
|
-
"completeCheckoutSession",
|
|
1319
|
-
"cancelCheckoutSession"
|
|
1320
|
-
];
|
|
1321
|
-
var ACP_OPERATION_INPUT_KEYS = {
|
|
1322
|
-
createCheckoutSession: ["body"],
|
|
1323
|
-
updateCheckoutSession: ["path", "body"],
|
|
1324
|
-
getCheckoutSession: ["path"],
|
|
1325
|
-
completeCheckoutSession: ["path", "body"],
|
|
1326
|
-
cancelCheckoutSession: ["path"]
|
|
1327
|
-
};
|
|
1328
|
-
var ACP_OPERATION_OPTIONAL_INPUT_KEYS = {
|
|
1329
|
-
cancelCheckoutSession: ["body"]
|
|
1330
|
-
};
|
|
1331
|
-
var ACP_SESSION_ID_INPUT_KEY = "checkout_session_id";
|
|
1332
|
-
var ACP_JSON_MEDIA_TYPE = "application/json";
|
|
1333
|
-
var ACP_API_VERSION_HEADER = "api-version";
|
|
1334
|
-
var ACP_REQUEST_ID_HEADER = "request-id";
|
|
1335
|
-
var ACP_IDEMPOTENCY_KEY_HEADER = "idempotency-key";
|
|
1336
|
-
var ACP_IDEMPOTENT_REPLAYED_HEADER = "idempotent-replayed";
|
|
1337
|
-
var ACP_IN_FLIGHT_RETRY_AFTER_SECONDS = 1;
|
|
1338
|
-
var ACP_MAX_REQUEST_ID_LENGTH = 128;
|
|
1339
|
-
var ACP_FORWARDED_HEADERS = [
|
|
1340
|
-
"accept-language",
|
|
1341
|
-
"user-agent",
|
|
1342
|
-
ACP_REQUEST_ID_HEADER,
|
|
1343
|
-
ACP_API_VERSION_HEADER,
|
|
1344
|
-
"signature",
|
|
1345
|
-
"timestamp"
|
|
1346
|
-
];
|
|
1347
|
-
var ACP_MAX_FORWARDED_HEADER_LENGTH = 2048;
|
|
1348
|
-
var ACP_MERCHANT_RETRY_AFTER_DEFAULT_SECONDS = 5;
|
|
1349
|
-
var ACP_MERCHANT_RETRY_AFTER_MAX_SECONDS = 300;
|
|
1350
|
-
|
|
1351
1526
|
export { ACP_API_VERSION, ACP_API_VERSION_HEADER, ACP_CHECKOUT_OPERATIONS, ACP_FORWARDED_HEADERS, ACP_IDEMPOTENCY_KEY_HEADER, ACP_IDEMPOTENT_REPLAYED_HEADER, ACP_IN_FLIGHT_RETRY_AFTER_SECONDS, ACP_JSON_MEDIA_TYPE, ACP_MAX_FORWARDED_HEADER_LENGTH, ACP_MAX_REQUEST_ID_LENGTH, ACP_MERCHANT_RETRY_AFTER_DEFAULT_SECONDS, ACP_MERCHANT_RETRY_AFTER_MAX_SECONDS, ACP_OPERATION_INPUT_KEYS, ACP_OPERATION_OPTIONAL_INPUT_KEYS, ACP_REQUEST_ID_HEADER, ACP_SESSION_ID_INPUT_KEY, ACP_SPEC_VERSION, ACP_WELL_KNOWN_PATH, BackendErrorResponse, DEFAULT_CONFIG_FILENAME, HttpBackendExecutor, createExecutionPipeline, createResourceRegistry, createStoreEventSink, extractPathParameterNames, findUnparsedBraceToken, isObjectSchemaNode };
|
|
1352
|
-
//# sourceMappingURL=chunk-
|
|
1353
|
-
//# sourceMappingURL=chunk-
|
|
1527
|
+
//# sourceMappingURL=chunk-PTL7SNOI.js.map
|
|
1528
|
+
//# sourceMappingURL=chunk-PTL7SNOI.js.map
|