@conto_finance/sdk 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -22
- package/dist/index.d.mts +29 -11
- package/dist/index.d.ts +29 -11
- package/dist/index.js +318 -58
- package/dist/index.mjs +318 -58
- package/dist/link-wallet.d.mts +11 -2
- package/dist/link-wallet.d.ts +11 -2
- package/dist/link-wallet.js +41 -4
- package/dist/link-wallet.mjs +41 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -47,7 +47,7 @@ var require_package = __commonJS({
|
|
|
47
47
|
"package.json"(exports2, module2) {
|
|
48
48
|
module2.exports = {
|
|
49
49
|
name: "@conto_finance/sdk",
|
|
50
|
-
version: "0.8.
|
|
50
|
+
version: "0.8.1",
|
|
51
51
|
description: "Conto SDK for AI agent payment authorization",
|
|
52
52
|
main: "./dist/index.js",
|
|
53
53
|
module: "./dist/index.mjs",
|
|
@@ -1984,7 +1984,13 @@ var BrowserActionFinalizationPendingError = class extends ContoError {
|
|
|
1984
1984
|
};
|
|
1985
1985
|
var CheckoutCheckpointClosedError = class extends ContoError {
|
|
1986
1986
|
constructor(checkpoint, message = `The checkpoint is ${checkpoint.stage.toLowerCase().replace(/_/g, " ")}. Stop this checkout.`, code = "CHECKOUT_CHECKPOINT_CLOSED") {
|
|
1987
|
-
super(message, {
|
|
1987
|
+
super(message, {
|
|
1988
|
+
code,
|
|
1989
|
+
status: 0,
|
|
1990
|
+
...checkpoint.paymentReference?.provider === "LINK" ? {
|
|
1991
|
+
retryGuidance: "Closing a checkpoint does not close its Link request, so the card may still work. Cancel it with wallet.cancel(error.checkpoint.paymentReference.id). If that call throws, the card may have been used, so do not begin a new purchase. Fix what stopped it and try again, and if Link still refuses, ask the owner to check the order. Once the cancel succeeds, begin a new purchase with a new idempotency key if the buyer still wants it."
|
|
1992
|
+
} : {}
|
|
1993
|
+
});
|
|
1988
1994
|
this.name = "CheckoutCheckpointClosedError";
|
|
1989
1995
|
this.checkpoint = checkpoint;
|
|
1990
1996
|
this.stage = checkpoint.stage;
|
|
@@ -3196,6 +3202,12 @@ function checkpointCart(amountMinor, lineItems, totals) {
|
|
|
3196
3202
|
if (!Array.isArray(lineItems) || lineItems.length === 0) {
|
|
3197
3203
|
throw checkoutError("Supply at least one line item");
|
|
3198
3204
|
}
|
|
3205
|
+
if (totals !== void 0 && totals !== null) {
|
|
3206
|
+
const finalTotal = totals.filter((total) => total.type === "total");
|
|
3207
|
+
if (finalTotal.length !== 1 || finalTotal[0].amountMinor !== amountMinor) {
|
|
3208
|
+
throw checkoutError("totals must have one row with type total that equals amountMinor");
|
|
3209
|
+
}
|
|
3210
|
+
}
|
|
3199
3211
|
return {
|
|
3200
3212
|
lineItems,
|
|
3201
3213
|
totals: totals ?? [{ type: "total", label: "Total", amountMinor }]
|
|
@@ -3235,6 +3247,13 @@ var LINK_REQUEST_ID = /^lsrq_[A-Za-z0-9]{1,64}$/;
|
|
|
3235
3247
|
var LINK_WAITING_STATUSES = /* @__PURE__ */ new Set(["created", "pending_approval"]);
|
|
3236
3248
|
var LINK_APPROVED_STATUSES = /* @__PURE__ */ new Set(["approved", "submitted", "succeeded"]);
|
|
3237
3249
|
var LINK_CLOSED_STATUSES = /* @__PURE__ */ new Set(["denied", "expired", "canceled", "failed"]);
|
|
3250
|
+
var CLOSED_LINK_RESULTS = /* @__PURE__ */ new WeakSet();
|
|
3251
|
+
var CHECKPOINT_RELEASED_STAGES = [
|
|
3252
|
+
"CANCELLED",
|
|
3253
|
+
"EXPIRED",
|
|
3254
|
+
"DENIED",
|
|
3255
|
+
"NOT_PAID"
|
|
3256
|
+
];
|
|
3238
3257
|
var INSPECT = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
|
|
3239
3258
|
function assertPaymentReference(reference) {
|
|
3240
3259
|
if (reference === void 0) return;
|
|
@@ -3316,6 +3335,14 @@ function errorCode(error) {
|
|
|
3316
3335
|
function errorReason(error) {
|
|
3317
3336
|
return error && typeof error === "object" && typeof error.reason === "string" ? error.reason : void 0;
|
|
3318
3337
|
}
|
|
3338
|
+
function duplicateGuidance(requestId, owner) {
|
|
3339
|
+
const wait2 = `This purchase cannot start until Link request ${requestId} closes, so do not retry yet.`;
|
|
3340
|
+
const then = "Once it closes, begin this purchase with a new idempotency key if the buyer still wants it.";
|
|
3341
|
+
if (typeof owner !== "string" || !/^[A-Za-z0-9_-]{1,64}$/.test(owner)) {
|
|
3342
|
+
return `${wait2} Finish the other purchase, or wait for Link to close the request. Do not cancel it, because a payment may be in flight. ${then}`;
|
|
3343
|
+
}
|
|
3344
|
+
return `${wait2} It belongs to checkpoint ${owner}, not error.checkpoint, which is this purchase. Read that one with checkout.get("${owner}"). If Conto cannot find it, another agent owns it, so leave the request alone and wait for Link to close it. At APPROVED, finish that purchase, or call wallet.cancel("${requestId}") and then checkout.cancel("${owner}"). At OUTCOME_UNKNOWN with linkRequestOpen true, if the card was used, report PAID with checkout.reportOutcome(), or UNKNOWN if you cannot tell. Otherwise call wallet.cancel("${requestId}") and then checkout.cancel("${owner}"). At CANCELLED, EXPIRED, DENIED, or NOT_PAID, call only wallet.cancel("${requestId}"). At any other stage a payment may be in flight or done, so do not cancel it. If wallet.cancel() throws, the card may have been used, so do not call checkout.cancel() or begin a new purchase. ${then}`;
|
|
3345
|
+
}
|
|
3319
3346
|
function linkFailure(error, signal) {
|
|
3320
3347
|
if (error instanceof LinkCardError) {
|
|
3321
3348
|
return {
|
|
@@ -3331,6 +3358,24 @@ function linkFailure(error, signal) {
|
|
|
3331
3358
|
message: errorReason(error) === "not_logged_in" ? 'link-cli is not logged in on this machine. Run "link-cli auth login".' : 'The Link wallet is not available here. Install link-cli with "npm i -g @stripe/link-cli", then run "link-cli auth login".'
|
|
3332
3359
|
};
|
|
3333
3360
|
}
|
|
3361
|
+
const duplicate = error;
|
|
3362
|
+
const duplicateOf = String(duplicate?.duplicateOf);
|
|
3363
|
+
if (errorReason(error) === "invalid_input" && LINK_REQUEST_ID.test(duplicateOf)) {
|
|
3364
|
+
return {
|
|
3365
|
+
kind: LinkCardError,
|
|
3366
|
+
code: "LINK_CARD_DUPLICATE_REQUEST",
|
|
3367
|
+
message: "Link has an open request for another purchase with the same details.",
|
|
3368
|
+
retryGuidance: duplicateGuidance(duplicateOf, duplicate?.duplicateCheckpoint)
|
|
3369
|
+
};
|
|
3370
|
+
}
|
|
3371
|
+
if (errorReason(error) === "cli_failed" && LINK_REQUEST_ID.test(duplicateOf)) {
|
|
3372
|
+
return {
|
|
3373
|
+
kind: LinkCardError,
|
|
3374
|
+
code: "LINK_CARD_FAILED",
|
|
3375
|
+
message: "Link has an open request with the same details and no checkpoint on it, so a retry cannot resume it.",
|
|
3376
|
+
retryGuidance: `Do not retry yet. Link request ${duplicateOf} has the same details and is not this checkout's, so do not cancel it yourself. Ask the person to finish or close it in Link, then retry with the same input. To stop this purchase instead, call checkout.cancel(error.checkpoint.id).`
|
|
3377
|
+
};
|
|
3378
|
+
}
|
|
3334
3379
|
if (signal?.aborted || errorReason(error) === "aborted") {
|
|
3335
3380
|
return {
|
|
3336
3381
|
kind: LinkCardError,
|
|
@@ -3371,7 +3416,7 @@ function closedLinkFailure(request) {
|
|
|
3371
3416
|
return {
|
|
3372
3417
|
kind: LinkCardError,
|
|
3373
3418
|
code: "LINK_CARD_ACTION_REQUIRED",
|
|
3374
|
-
message: "Link
|
|
3419
|
+
message: "Link cannot continue this request. The person has to fix a problem in Link before a new request can issue a card."
|
|
3375
3420
|
};
|
|
3376
3421
|
default:
|
|
3377
3422
|
return {
|
|
@@ -3382,13 +3427,15 @@ function closedLinkFailure(request) {
|
|
|
3382
3427
|
};
|
|
3383
3428
|
}
|
|
3384
3429
|
}
|
|
3385
|
-
async function closeLinkRequest(wallet, id2) {
|
|
3430
|
+
async function closeLinkRequest(wallet, id2, onRefused) {
|
|
3386
3431
|
try {
|
|
3387
3432
|
await wallet.cancel(id2);
|
|
3388
3433
|
return true;
|
|
3389
|
-
} catch {
|
|
3390
|
-
const status = await wallet.waitForApproval(id2, { timeoutSeconds: 0 }).then((request) => request.status).catch(() => void 0);
|
|
3391
|
-
|
|
3434
|
+
} catch (error) {
|
|
3435
|
+
const status = await Promise.resolve().then(() => wallet.waitForApproval(id2, { timeoutSeconds: 0 })).then((request) => request.status).catch(() => void 0);
|
|
3436
|
+
if (status !== void 0 && LINK_CLOSED_STATUSES.has(status)) return true;
|
|
3437
|
+
onRefused?.(error);
|
|
3438
|
+
return false;
|
|
3392
3439
|
}
|
|
3393
3440
|
}
|
|
3394
3441
|
function assertLinkApprovalCallback(callback) {
|
|
@@ -3396,6 +3443,65 @@ function assertLinkApprovalCallback(callback) {
|
|
|
3396
3443
|
throw checkoutError("onLinkApproval must be a function");
|
|
3397
3444
|
}
|
|
3398
3445
|
}
|
|
3446
|
+
function linkCardClosable(checkpoint) {
|
|
3447
|
+
return CHECKPOINT_RELEASED_STAGES.includes(checkpoint.stage) || ["AWAITING_APPROVAL", "APPROVED"].includes(checkpoint.stage) || checkpoint.linkRequestOpen === true;
|
|
3448
|
+
}
|
|
3449
|
+
var CONFIRMING_GUIDANCE = "Wait a few seconds, then read the checkpoint with checkout.get(). Act only after the stage changes, and do not cancel the Link request before then, because the check can still end in CONFIRMED. If it is still CONFIRMING after a few minutes, stop and ask the owner to record the outcome on the checkout page.";
|
|
3450
|
+
var PAID_GUIDANCE = "Do not buy it again.";
|
|
3451
|
+
var CONFIRMED_GUIDANCE = "Report what happened with checkout.reportOutcome(), and do not cancel the Link request while a payment may be in flight. If you know no attempt paid or is paying, report NOT_PAID and cancel the Link request with wallet.cancel() so its card cannot be used. The owner can then release the hold, and you can begin a new purchase with a new idempotency key.";
|
|
3452
|
+
var LINK_CANCEL_STUCK_GUIDANCE = "If wallet.cancel() keeps failing, stop and do not call checkout.cancel(). The owner gets a review alert after the approval lapses and records the outcome on the checkout page.";
|
|
3453
|
+
var LINK_CANCEL_STUCK_NO_RECORD_GUIDANCE = "If wallet.cancel() keeps failing, fix what stopped it, then retry with the same input. Do not call checkout.cancel(). While Conto has no record of this request, the hold ends when the approval lapses, the card may still work, and the owner gets no alert, so tell the owner.";
|
|
3454
|
+
var LINK_REQUEST_OPEN_GUIDANCE = `If the card was used, report PAID with checkout.reportOutcome(), or UNKNOWN if you cannot tell, which keeps the spend held until the owner records what happened. Otherwise cancel the Link request with wallet.cancel(error.linkRequestId), then call checkout.cancel(error.checkpoint.id). ${LINK_CANCEL_STUCK_GUIDANCE}`;
|
|
3455
|
+
var LINK_ACTION_REQUIRED_GUIDANCE = `A retry cannot resume this request, and Link will not cancel it, but Link closes it on its own. wallet.waitForApproval(error.linkRequestId, { timeoutSeconds: 0 }) returns its actionUrl, where the person can fix the problem. To buy this, begin a new purchase with a new idempotency key once they have. Do not call checkout.cancel() yet. This hold stays until Link closes the request, and then a retry releases it. ${LINK_CANCEL_STUCK_GUIDANCE}`;
|
|
3456
|
+
function linkCardNotReplaced(stage) {
|
|
3457
|
+
if (stage === "CONFIRMING") {
|
|
3458
|
+
return {
|
|
3459
|
+
message: "Conto is still checking this checkpoint, so the Link card cannot be replaced yet. Nothing changed. Do not pay this total.",
|
|
3460
|
+
retryGuidance: CONFIRMING_GUIDANCE
|
|
3461
|
+
};
|
|
3462
|
+
}
|
|
3463
|
+
if (stage === "PAID") {
|
|
3464
|
+
return {
|
|
3465
|
+
message: "This purchase is already recorded as paid, so the Link card cannot be replaced. Nothing changed. Do not pay again.",
|
|
3466
|
+
retryGuidance: PAID_GUIDANCE
|
|
3467
|
+
};
|
|
3468
|
+
}
|
|
3469
|
+
return {
|
|
3470
|
+
message: "This checkpoint was already confirmed and a payment may be in flight, so the Link card cannot be replaced. Nothing changed. Do not pay this total.",
|
|
3471
|
+
retryGuidance: CONFIRMED_GUIDANCE
|
|
3472
|
+
};
|
|
3473
|
+
}
|
|
3474
|
+
function linkCardNotIssued(stage) {
|
|
3475
|
+
if (stage === "CONFIRMING") {
|
|
3476
|
+
return {
|
|
3477
|
+
message: "Conto is still checking this checkpoint, so Link was not asked for a card. Do not pay, and do not cancel its Link request.",
|
|
3478
|
+
retryGuidance: CONFIRMING_GUIDANCE
|
|
3479
|
+
};
|
|
3480
|
+
}
|
|
3481
|
+
if (stage === "PAID") {
|
|
3482
|
+
return {
|
|
3483
|
+
message: "This purchase is already recorded as paid, so Link was not asked for a card. Do not pay again.",
|
|
3484
|
+
retryGuidance: PAID_GUIDANCE
|
|
3485
|
+
};
|
|
3486
|
+
}
|
|
3487
|
+
return {
|
|
3488
|
+
message: "This checkpoint was already confirmed and a payment may be in flight, so Link was not asked for a card. Do not pay again, and do not cancel its Link request.",
|
|
3489
|
+
retryGuidance: CONFIRMED_GUIDANCE
|
|
3490
|
+
};
|
|
3491
|
+
}
|
|
3492
|
+
function openLinkRequestGuidance(latest) {
|
|
3493
|
+
if (!latest) {
|
|
3494
|
+
return "Read the checkpoint with checkout.get(error.checkpoint.id) before you do anything else. Do not cancel the Link request until you know its stage, because a payment may be in flight. If the stage is CANCELLED, EXPIRED, DENIED, or NOT_PAID, cancel it with wallet.cancel(error.linkRequestId) and do not pay with it. Otherwise retry with the same input, which resumes the request or tells you what to do at that stage.";
|
|
3495
|
+
}
|
|
3496
|
+
if (CHECKPOINT_RELEASED_STAGES.includes(latest.stage)) {
|
|
3497
|
+
return "The checkpoint has closed, so a retry cannot resume this request. Cancel it with wallet.cancel(error.linkRequestId), and do not pay with it.";
|
|
3498
|
+
}
|
|
3499
|
+
if (latest.linkRequestOpen) return LINK_REQUEST_OPEN_GUIDANCE;
|
|
3500
|
+
if (linkCardClosable(latest)) {
|
|
3501
|
+
return `Retry with the same input to resume this Link request. To stop instead, read the checkpoint with checkout.get(). If it is still APPROVED, cancel the request with wallet.cancel(error.linkRequestId) and then call checkout.cancel(error.checkpoint.id). ${latest.paymentReference ? LINK_CANCEL_STUCK_GUIDANCE : LINK_CANCEL_STUCK_NO_RECORD_GUIDANCE}`;
|
|
3502
|
+
}
|
|
3503
|
+
return linkCardNotIssued(latest.stage).retryGuidance;
|
|
3504
|
+
}
|
|
3399
3505
|
function linkCreatedNothing(error) {
|
|
3400
3506
|
return errorCode(error) === "LINK_WALLET_UNAVAILABLE" || errorReason(error) === "invalid_input";
|
|
3401
3507
|
}
|
|
@@ -3422,7 +3528,7 @@ function notifyLinkApproval(callback, event) {
|
|
|
3422
3528
|
} catch {
|
|
3423
3529
|
}
|
|
3424
3530
|
}
|
|
3425
|
-
var _client4, _CheckoutClient_instances, recordLinkRequest_fn, linkCardFor_fn;
|
|
3531
|
+
var _client4, _CheckoutClient_instances, recordLinkRequest_fn, waitForLinkOwner_fn, linkCardFor_fn;
|
|
3426
3532
|
var CheckoutClient = class {
|
|
3427
3533
|
constructor(client) {
|
|
3428
3534
|
__privateAdd(this, _CheckoutClient_instances);
|
|
@@ -3507,17 +3613,37 @@ var CheckoutClient = class {
|
|
|
3507
3613
|
assertPaymentReference(input.paymentReference);
|
|
3508
3614
|
let current = checkpoint;
|
|
3509
3615
|
if (input.amountMinor !== checkpoint.amountMinor) {
|
|
3510
|
-
if (input.paymentReference ?? checkpoint.paymentReference) {
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
|
|
3515
|
-
|
|
3616
|
+
if (input.paymentReference ?? checkpoint.paymentReference ?? (await this.get(checkpoint.id)).paymentReference) {
|
|
3617
|
+
let notRecorded = false;
|
|
3618
|
+
if (input.paymentReference && !checkpoint.paymentReference) {
|
|
3619
|
+
notRecorded = await __privateGet(this, _client4).request(
|
|
3620
|
+
"POST",
|
|
3621
|
+
`/api/sdk/checkout-checkpoints/${encodeURIComponent(checkpoint.id)}/payment-reference`,
|
|
3622
|
+
{ paymentReference: { provider: "LINK", id: input.paymentReference.id } },
|
|
3623
|
+
{ idempotent: true }
|
|
3624
|
+
).then(
|
|
3625
|
+
() => false,
|
|
3626
|
+
async (error) => {
|
|
3627
|
+
if (!(error instanceof ContoError && error.status === 409)) return true;
|
|
3628
|
+
const latest = await this.get(checkpoint.id).catch(() => void 0);
|
|
3629
|
+
return latest?.paymentReference?.id !== input.paymentReference?.id;
|
|
3630
|
+
}
|
|
3631
|
+
);
|
|
3632
|
+
}
|
|
3633
|
+
const note = notRecorded ? " Conto could not record this Link request, so its hold may end when the approval lapses." : "";
|
|
3634
|
+
throw new ContoError(
|
|
3635
|
+
`The Link card covers the approved total, not this one. Do not pay.${note}`,
|
|
3636
|
+
{
|
|
3637
|
+
code: "LINK_CARD_TOTAL_CHANGED",
|
|
3638
|
+
status: 0,
|
|
3639
|
+
retryGuidance: "Call checkout.replaceLinkCard(result, { wallet, amountMinor }) for a card at the final total, then confirm the checkpoint it returns."
|
|
3640
|
+
}
|
|
3641
|
+
);
|
|
3516
3642
|
}
|
|
3517
3643
|
try {
|
|
3518
3644
|
await this.cancel(checkpoint.id);
|
|
3519
3645
|
} catch (error) {
|
|
3520
|
-
const closed = error instanceof ContoError && error.status === 409 &&
|
|
3646
|
+
const closed = error instanceof ContoError && error.status === 409 && CHECKPOINT_RELEASED_STAGES.includes((await this.get(checkpoint.id)).stage);
|
|
3521
3647
|
if (!closed) throw error;
|
|
3522
3648
|
}
|
|
3523
3649
|
current = await this.begin({
|
|
@@ -3581,7 +3707,9 @@ var CheckoutClient = class {
|
|
|
3581
3707
|
}
|
|
3582
3708
|
/**
|
|
3583
3709
|
* Cancel a checkpoint that was not confirmed and release its spend. A
|
|
3584
|
-
* checkpoint waiting for the owner can be cancelled too.
|
|
3710
|
+
* checkpoint waiting for the owner can be cancelled too. This does not close
|
|
3711
|
+
* a Link card, which can still work with nothing held behind it, so cancel
|
|
3712
|
+
* the Link request first.
|
|
3585
3713
|
*/
|
|
3586
3714
|
async cancel(checkpointId) {
|
|
3587
3715
|
const result = await __privateGet(this, _client4).request(
|
|
@@ -3623,9 +3751,11 @@ var CheckoutClient = class {
|
|
|
3623
3751
|
* held spend and throws a LinkCardError. When Conto cannot cancel an open Link
|
|
3624
3752
|
* request, it keeps the hold and throws LINK_CARD_NOT_CANCELLED with the
|
|
3625
3753
|
* request id. Retrying with the same input resumes the same checkpoint and
|
|
3626
|
-
* Link request while Conto's approval is open.
|
|
3627
|
-
* a retry throws CHECKOUT_CHECKPOINT_NOT_APPROVED
|
|
3628
|
-
* the checkpoint.
|
|
3754
|
+
* Link request while Conto's approval is open. If its approval ended with the
|
|
3755
|
+
* request open, a retry throws CHECKOUT_CHECKPOINT_NOT_APPROVED. Cancel the Link
|
|
3756
|
+
* request, then the checkpoint. Once a confirm has started, a retry throws
|
|
3757
|
+
* LINK_CARD_NOT_ISSUED. Do not cancel the Link request while a payment may be in
|
|
3758
|
+
* flight. Follow error.retryGuidance, which depends on the stage.
|
|
3629
3759
|
*/
|
|
3630
3760
|
async getLinkCard(input) {
|
|
3631
3761
|
const {
|
|
@@ -3642,10 +3772,7 @@ var CheckoutClient = class {
|
|
|
3642
3772
|
assertLinkApprovalCallback(onLinkApproval);
|
|
3643
3773
|
let checkpoint = await this.begin({ ...beginInput, checkoutMethod: "LINK" });
|
|
3644
3774
|
if (checkpoint.stage === "AWAITING_APPROVAL") {
|
|
3645
|
-
checkpoint = await this.
|
|
3646
|
-
...ownerApprovalTimeoutMs !== void 0 ? { timeoutMs: ownerApprovalTimeoutMs } : {},
|
|
3647
|
-
...signal ? { signal } : {}
|
|
3648
|
-
});
|
|
3775
|
+
checkpoint = await __privateMethod(this, _CheckoutClient_instances, waitForLinkOwner_fn).call(this, checkpoint, ownerApprovalTimeoutMs, signal);
|
|
3649
3776
|
}
|
|
3650
3777
|
return __privateMethod(this, _CheckoutClient_instances, linkCardFor_fn).call(this, checkpoint, wallet, {
|
|
3651
3778
|
linkApprovalTimeoutMs,
|
|
@@ -3657,7 +3784,11 @@ var CheckoutClient = class {
|
|
|
3657
3784
|
* The final total differs from the one the Link card covers. This cancels
|
|
3658
3785
|
* the old Link request and checkpoint, begins a checkpoint at the final
|
|
3659
3786
|
* total (waiting for the owner when a rule requires it), and gets a new card
|
|
3660
|
-
* for it. Confirm the returned checkpoint with the same total.
|
|
3787
|
+
* for it. Confirm the returned checkpoint with the same total. It reads the
|
|
3788
|
+
* checkpoint first. At a stage where a payment may be in flight, or with a
|
|
3789
|
+
* different Link request on record, this throws LINK_CARD_NOT_REPLACED and
|
|
3790
|
+
* leaves the card and the hold alone. After the old card is closed, trying
|
|
3791
|
+
* again with the same input resumes the replacement.
|
|
3661
3792
|
*/
|
|
3662
3793
|
async replaceLinkCard(previous, input) {
|
|
3663
3794
|
const {
|
|
@@ -3672,23 +3803,73 @@ var CheckoutClient = class {
|
|
|
3672
3803
|
throw checkoutError("amountMinor must be a positive integer number of cents");
|
|
3673
3804
|
}
|
|
3674
3805
|
assertLinkApprovalCallback(onLinkApproval);
|
|
3675
|
-
if (amountMinor === previous.checkpoint.amountMinor) return previous;
|
|
3676
3806
|
const old = previous.checkpoint;
|
|
3677
|
-
|
|
3807
|
+
checkpointCart(amountMinor, input.lineItems ?? old.cart.lineItems, input.totals);
|
|
3808
|
+
const current = await this.get(old.id);
|
|
3809
|
+
const released = CHECKPOINT_RELEASED_STAGES.includes(current.stage);
|
|
3810
|
+
if (!linkCardClosable(current)) {
|
|
3811
|
+
throw new LinkCardError({
|
|
3812
|
+
...linkCardNotReplaced(current.stage),
|
|
3813
|
+
code: "LINK_CARD_NOT_REPLACED",
|
|
3814
|
+
checkpoint: current,
|
|
3815
|
+
linkRequestId: previous.linkRequestId,
|
|
3816
|
+
holdReleased: false
|
|
3817
|
+
});
|
|
3818
|
+
}
|
|
3819
|
+
const recordedRequestId = current.paymentReference?.id;
|
|
3820
|
+
if (recordedRequestId && recordedRequestId !== previous.linkRequestId) {
|
|
3821
|
+
throw new LinkCardError({
|
|
3822
|
+
message: released ? "A different Link request is on record for this checkpoint. The held spend is already released, but either Link card may still work. Do not pay with them." : "A different Link request is on record for this checkpoint, so the Link card cannot be replaced. Nothing changed. Do not pay.",
|
|
3823
|
+
code: "LINK_CARD_NOT_REPLACED",
|
|
3824
|
+
checkpoint: current,
|
|
3825
|
+
linkRequestId: recordedRequestId,
|
|
3826
|
+
holdReleased: released,
|
|
3827
|
+
retryGuidance: released ? `Cancel both ${recordedRequestId} and ${previous.linkRequestId} with wallet.cancel().` : `Use the result for that request, or cancel both ${recordedRequestId} and ${previous.linkRequestId} with wallet.cancel() and then call checkout.cancel(error.checkpoint.id).`
|
|
3828
|
+
});
|
|
3829
|
+
}
|
|
3830
|
+
if (amountMinor === old.amountMinor && !released && !current.linkRequestOpen && !CLOSED_LINK_RESULTS.has(previous)) {
|
|
3831
|
+
return previous;
|
|
3832
|
+
}
|
|
3833
|
+
if (signal?.aborted) {
|
|
3678
3834
|
throw new LinkCardError({
|
|
3679
|
-
message: "
|
|
3835
|
+
message: released ? "The caller stopped before the Link card was replaced. The held spend is already released, and the earlier Link card may still work. Do not pay with it." : "The caller stopped before the Link card was replaced. Nothing changed. Do not pay.",
|
|
3836
|
+
code: "LINK_CARD_ABORTED",
|
|
3837
|
+
checkpoint: current,
|
|
3838
|
+
linkRequestId: previous.linkRequestId,
|
|
3839
|
+
holdReleased: released,
|
|
3840
|
+
retryGuidance: released ? "Try replaceLinkCard again, or cancel the request with wallet.cancel(error.linkRequestId)." : "Try replaceLinkCard again, or cancel the request with wallet.cancel(error.linkRequestId) and then call checkout.cancel(error.checkpoint.id)."
|
|
3841
|
+
});
|
|
3842
|
+
}
|
|
3843
|
+
let refusal;
|
|
3844
|
+
const oldCardClosed = await closeLinkRequest(wallet, previous.linkRequestId, (error) => {
|
|
3845
|
+
refusal = error;
|
|
3846
|
+
});
|
|
3847
|
+
if (!oldCardClosed) {
|
|
3848
|
+
const walletDown = errorCode(refusal) === "LINK_WALLET_UNAVAILABLE";
|
|
3849
|
+
throw new LinkCardError({
|
|
3850
|
+
message: `${walletDown ? `${linkFailure(refusal).message} ` : ""}${released ? "The held spend is already released, but Conto could not cancel the earlier Link card, so it may still work. Do not pay with it." : "Conto could not cancel the earlier Link card. Nothing changed. Do not pay."}`,
|
|
3680
3851
|
code: "LINK_CARD_NOT_CANCELLED",
|
|
3681
|
-
checkpoint:
|
|
3852
|
+
checkpoint: current,
|
|
3682
3853
|
linkRequestId: previous.linkRequestId,
|
|
3683
|
-
holdReleased:
|
|
3684
|
-
retryGuidance: "Try replaceLinkCard again."
|
|
3854
|
+
holdReleased: released,
|
|
3855
|
+
retryGuidance: `${walletDown ? "Fix the Link wallet first, as the message says. Until then a retry fails the same way. " : ""}${current.linkRequestOpen ? "Try replaceLinkCard again. If the card was already used, report PAID with checkout.reportOutcome(), or UNKNOWN if you cannot tell, which keeps the spend held until the owner records what happened." : released ? "Try replaceLinkCard again, or cancel the request with wallet.cancel(error.linkRequestId) and do not pay with it." : "Try replaceLinkCard again."}`
|
|
3685
3856
|
});
|
|
3686
3857
|
}
|
|
3858
|
+
CLOSED_LINK_RESULTS.add(previous);
|
|
3687
3859
|
try {
|
|
3688
3860
|
await this.cancel(old.id);
|
|
3689
3861
|
} catch (error) {
|
|
3690
|
-
const
|
|
3691
|
-
if (!
|
|
3862
|
+
const latest = error instanceof ContoError && error.status === 409 ? await this.get(old.id).catch(() => void 0) : void 0;
|
|
3863
|
+
if (!released && (!latest || !CHECKPOINT_RELEASED_STAGES.includes(latest.stage))) {
|
|
3864
|
+
throw new LinkCardError({
|
|
3865
|
+
message: "The earlier Link request is cancelled, but Conto did not release the held spend. Do not confirm or pay.",
|
|
3866
|
+
code: "CHECKPOINT_NOT_CANCELLED",
|
|
3867
|
+
checkpoint: latest ?? current,
|
|
3868
|
+
linkRequestId: previous.linkRequestId,
|
|
3869
|
+
holdReleased: false,
|
|
3870
|
+
retryGuidance: "Try replaceLinkCard again. If the checkpoint was confirmed, report its outcome instead."
|
|
3871
|
+
});
|
|
3872
|
+
}
|
|
3692
3873
|
}
|
|
3693
3874
|
let checkpoint = await this.begin({
|
|
3694
3875
|
idempotencyKey: `${old.idempotencyKey.slice(0, 200)}:final-${amountMinor}`,
|
|
@@ -3705,10 +3886,7 @@ var CheckoutClient = class {
|
|
|
3705
3886
|
totals: input.totals
|
|
3706
3887
|
});
|
|
3707
3888
|
if (checkpoint.stage === "AWAITING_APPROVAL") {
|
|
3708
|
-
checkpoint = await this.
|
|
3709
|
-
...ownerApprovalTimeoutMs !== void 0 ? { timeoutMs: ownerApprovalTimeoutMs } : {},
|
|
3710
|
-
...signal ? { signal } : {}
|
|
3711
|
-
});
|
|
3889
|
+
checkpoint = await __privateMethod(this, _CheckoutClient_instances, waitForLinkOwner_fn).call(this, checkpoint, ownerApprovalTimeoutMs, signal);
|
|
3712
3890
|
}
|
|
3713
3891
|
return __privateMethod(this, _CheckoutClient_instances, linkCardFor_fn).call(this, checkpoint, wallet, {
|
|
3714
3892
|
linkApprovalTimeoutMs,
|
|
@@ -3737,19 +3915,56 @@ recordLinkRequest_fn = async function(checkpoint, linkRequestId) {
|
|
|
3737
3915
|
});
|
|
3738
3916
|
}
|
|
3739
3917
|
};
|
|
3918
|
+
waitForLinkOwner_fn = async function(checkpoint, timeoutMs, signal) {
|
|
3919
|
+
try {
|
|
3920
|
+
return await this.waitForApproval(checkpoint, {
|
|
3921
|
+
...timeoutMs !== void 0 ? { timeoutMs } : {},
|
|
3922
|
+
...signal ? { signal } : {}
|
|
3923
|
+
});
|
|
3924
|
+
} catch (error) {
|
|
3925
|
+
if (!signal?.aborted || error instanceof ContoError && error !== signal.reason) throw error;
|
|
3926
|
+
const latest = await this.get(checkpoint.id).catch(() => void 0);
|
|
3927
|
+
const linkRequestId = latest?.paymentReference?.id;
|
|
3928
|
+
const released = Boolean(latest && CHECKPOINT_RELEASED_STAGES.includes(latest.stage));
|
|
3929
|
+
throw new LinkCardError({
|
|
3930
|
+
message: released ? "The caller stopped while waiting for the owner, and the held spend is already released. Do not pay." : "The caller stopped while waiting for the owner, and the held spend stays in place. Do not pay.",
|
|
3931
|
+
code: "LINK_CARD_ABORTED",
|
|
3932
|
+
checkpoint: latest ?? checkpoint,
|
|
3933
|
+
...linkRequestId ? { linkRequestId } : {},
|
|
3934
|
+
holdReleased: released,
|
|
3935
|
+
...released && !linkRequestId ? {} : {
|
|
3936
|
+
retryGuidance: latest && !linkRequestId && linkCardClosable(latest) ? "Retry with the same input, or call checkout.cancel(error.checkpoint.id) to release the held spend." : openLinkRequestGuidance(latest)
|
|
3937
|
+
}
|
|
3938
|
+
});
|
|
3939
|
+
}
|
|
3940
|
+
};
|
|
3740
3941
|
linkCardFor_fn = async function(checkpoint, wallet, options) {
|
|
3741
3942
|
const { linkApprovalTimeoutMs = 10 * 6e4, signal, onLinkApproval } = options;
|
|
3943
|
+
if (checkpoint.linkRequestOpen) {
|
|
3944
|
+
throw new LinkCardError({
|
|
3945
|
+
message: `Conto's approval for this checkpoint ended while its Link request may still work (stage ${checkpoint.stage}). Do not pay.`,
|
|
3946
|
+
code: "CHECKOUT_CHECKPOINT_NOT_APPROVED",
|
|
3947
|
+
checkpoint,
|
|
3948
|
+
...checkpoint.paymentReference ? { linkRequestId: checkpoint.paymentReference.id } : {},
|
|
3949
|
+
holdReleased: false,
|
|
3950
|
+
retryGuidance: LINK_REQUEST_OPEN_GUIDANCE
|
|
3951
|
+
});
|
|
3952
|
+
}
|
|
3742
3953
|
if (checkpoint.stage !== "APPROVED") {
|
|
3743
|
-
throw
|
|
3744
|
-
|
|
3745
|
-
"
|
|
3746
|
-
|
|
3954
|
+
throw new LinkCardError({
|
|
3955
|
+
...linkCardNotIssued(checkpoint.stage),
|
|
3956
|
+
code: "LINK_CARD_NOT_ISSUED",
|
|
3957
|
+
checkpoint,
|
|
3958
|
+
holdReleased: false
|
|
3959
|
+
});
|
|
3747
3960
|
}
|
|
3748
3961
|
const deadline = Date.now() + linkApprovalTimeoutMs;
|
|
3749
3962
|
const call = signal ? { signal } : {};
|
|
3750
|
-
|
|
3963
|
+
const recordedRequestId = checkpoint.paymentReference?.id;
|
|
3964
|
+
let linkRequestId = recordedRequestId;
|
|
3751
3965
|
let approvalUrl;
|
|
3752
3966
|
let createStarted = false;
|
|
3967
|
+
let createAnswered = false;
|
|
3753
3968
|
try {
|
|
3754
3969
|
const cardRequest = linkCardRequest(checkpoint);
|
|
3755
3970
|
createStarted = true;
|
|
@@ -3762,6 +3977,7 @@ linkCardFor_fn = async function(checkpoint, wallet, options) {
|
|
|
3762
3977
|
});
|
|
3763
3978
|
}
|
|
3764
3979
|
linkRequestId = request.id;
|
|
3980
|
+
createAnswered = true;
|
|
3765
3981
|
if (request.amountMinor !== void 0 && request.amountMinor !== checkpoint.amountMinor) {
|
|
3766
3982
|
throw new LinkFailureSignal({
|
|
3767
3983
|
kind: LinkCardError,
|
|
@@ -3770,6 +3986,13 @@ linkCardFor_fn = async function(checkpoint, wallet, options) {
|
|
|
3770
3986
|
});
|
|
3771
3987
|
}
|
|
3772
3988
|
checkpoint = await __privateMethod(this, _CheckoutClient_instances, recordLinkRequest_fn).call(this, checkpoint, request.id);
|
|
3989
|
+
if (checkpoint.stage !== "APPROVED") {
|
|
3990
|
+
throw new LinkFailureSignal({
|
|
3991
|
+
kind: LinkCardError,
|
|
3992
|
+
code: "LINK_CARD_NOT_ISSUED",
|
|
3993
|
+
message: "Conto no longer has this checkpoint approved, so no Link card was returned."
|
|
3994
|
+
});
|
|
3995
|
+
}
|
|
3773
3996
|
if (LINK_WAITING_STATUSES.has(request.status)) {
|
|
3774
3997
|
const details = linkApprovalDetails(request);
|
|
3775
3998
|
approvalUrl = details.approvalUrl;
|
|
@@ -3808,37 +4031,74 @@ linkCardFor_fn = async function(checkpoint, wallet, options) {
|
|
|
3808
4031
|
};
|
|
3809
4032
|
} catch (error) {
|
|
3810
4033
|
const failure = error instanceof LinkFailureSignal ? error.failure : linkFailure(error, signal);
|
|
3811
|
-
|
|
4034
|
+
const latest = createStarted || linkRequestId || !linkCardClosable(checkpoint) ? await this.get(checkpoint.id).catch(() => void 0) : checkpoint;
|
|
4035
|
+
const onRecordId = latest?.paymentReference?.id ?? recordedRequestId;
|
|
4036
|
+
const anotherFlow = !recordedRequestId && onRecordId && onRecordId !== linkRequestId;
|
|
4037
|
+
const alreadyReleased = Boolean(latest && CHECKPOINT_RELEASED_STAGES.includes(latest.stage));
|
|
4038
|
+
const unread = recordedRequestId || linkRequestId && !failure.linkClosed;
|
|
4039
|
+
if (latest ? !linkCardClosable(latest) || anotherFlow : unread) {
|
|
4040
|
+
const extraRequestId = onRecordId && linkRequestId !== onRecordId ? linkRequestId : void 0;
|
|
4041
|
+
const extraOpen = extraRequestId && !failure.linkClosed && !await closeLinkRequest(wallet, extraRequestId);
|
|
4042
|
+
const keptId = onRecordId ?? linkRequestId;
|
|
4043
|
+
throw new LinkCardError({
|
|
4044
|
+
message: alreadyReleased ? `${failure.message} A different Link request is on record, so Conto did not cancel it. The held spend is already released, but its card may still work. Do not pay with it.` : `${failure.message} The checkpoint changed or could not be read, so Conto did not cancel the Link request, and the held spend stays in place${latest || onRecordId || checkpoint.paymentReference ? "" : ", and may be released when the approval lapses"}. Do not pay.`,
|
|
4045
|
+
code: "LINK_CARD_NOT_CANCELLED",
|
|
4046
|
+
checkpoint: latest ?? checkpoint,
|
|
4047
|
+
...keptId ? { linkRequestId: keptId } : {},
|
|
4048
|
+
holdReleased: alreadyReleased,
|
|
4049
|
+
retryGuidance: `${extraOpen ? `Cancel ${extraRequestId} with wallet.cancel() first, because it is not the request on record. ` : ""}${openLinkRequestGuidance(latest)}`
|
|
4050
|
+
});
|
|
4051
|
+
}
|
|
4052
|
+
if (createStarted && !createAnswered && !linkCreatedNothing(error)) {
|
|
4053
|
+
const blockedBy = !recordedRequestId && !alreadyReleased && failure.retryGuidance;
|
|
3812
4054
|
throw new LinkCardError({
|
|
3813
|
-
message: `${failure.message} Conto could not tell whether Link created the card request, so the held spend stays in place. Do not pay.`,
|
|
4055
|
+
message: alreadyReleased ? `${failure.message} Conto could not tell whether Link created the card request. The held spend is already released, but ${recordedRequestId ? "the Link request on record" : "a request Link created"} may still work. Do not pay with it.` : blockedBy ? `${failure.message} The held spend stays in place, and may be released when the approval lapses. Do not pay.` : `${failure.message} Conto could not tell whether Link created the card request, so the held spend stays in place${recordedRequestId ? "" : ", and may be released when the approval lapses"}. Do not pay.`,
|
|
3814
4056
|
code: "LINK_CARD_NOT_CANCELLED",
|
|
3815
|
-
checkpoint,
|
|
3816
|
-
|
|
3817
|
-
|
|
4057
|
+
checkpoint: latest ?? checkpoint,
|
|
4058
|
+
...recordedRequestId ? { linkRequestId: recordedRequestId } : {},
|
|
4059
|
+
holdReleased: alreadyReleased,
|
|
4060
|
+
retryGuidance: recordedRequestId ? openLinkRequestGuidance(latest) : alreadyReleased ? "The checkpoint has closed, so a retry cannot resume a Link request. Begin a new purchase with a new idempotency key if the buyer still wants it. If Link then reports a duplicate, follow that error." : blockedBy || "Retry with the same input to resume the Link request, or call checkout.cancel(error.checkpoint.id) once you know Link created none."
|
|
3818
4061
|
});
|
|
3819
4062
|
}
|
|
3820
|
-
|
|
4063
|
+
const requestsToClose = /* @__PURE__ */ new Set();
|
|
4064
|
+
if (linkRequestId && !failure.linkClosed) requestsToClose.add(linkRequestId);
|
|
4065
|
+
if (recordedRequestId && recordedRequestId !== linkRequestId) {
|
|
4066
|
+
requestsToClose.add(recordedRequestId);
|
|
4067
|
+
}
|
|
4068
|
+
const stillOpen = [];
|
|
4069
|
+
for (const id2 of requestsToClose) {
|
|
4070
|
+
if (!await closeLinkRequest(wallet, id2)) stillOpen.push(id2);
|
|
4071
|
+
}
|
|
4072
|
+
if (stillOpen.length > 0) {
|
|
4073
|
+
const held = onRecordId ? "" : ", and may be released when the approval lapses";
|
|
4074
|
+
const bothOpen = stillOpen.length > 1 ? ` Link requests ${stillOpen.join(" and ")} are both open.` : "";
|
|
3821
4075
|
throw new LinkCardError({
|
|
3822
|
-
message: `${failure.message} Conto could not cancel the Link request, so the held spend stays in place
|
|
4076
|
+
message: alreadyReleased ? `${failure.message} The held spend is already released, but Conto could not cancel the Link request, so its card may still work.${bothOpen} Do not pay with it.` : `${failure.message} Conto could not cancel the Link request, so the held spend stays in place${held}.${bothOpen} Do not pay.`,
|
|
3823
4077
|
code: "LINK_CARD_NOT_CANCELLED",
|
|
3824
|
-
checkpoint,
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
4078
|
+
checkpoint: latest ?? checkpoint,
|
|
4079
|
+
// The one on record first, because a card may already be out on it.
|
|
4080
|
+
linkRequestId: onRecordId && stillOpen.includes(onRecordId) ? onRecordId : stillOpen[0],
|
|
4081
|
+
...approvalUrl && latest?.stage === "APPROVED" ? { approvalUrl } : {},
|
|
4082
|
+
holdReleased: alreadyReleased,
|
|
4083
|
+
retryGuidance: stillOpen.length > 1 ? `Cancel ${stillOpen.join(" and ")} with wallet.cancel()${alreadyReleased ? "" : ", then call checkout.cancel(error.checkpoint.id)"}.` : failure.code === "LINK_CARD_ACTION_REQUIRED" && latest?.stage === "APPROVED" ? LINK_ACTION_REQUIRED_GUIDANCE : openLinkRequestGuidance(latest)
|
|
3829
4084
|
});
|
|
3830
4085
|
}
|
|
3831
|
-
|
|
3832
|
-
|
|
4086
|
+
let cancelled;
|
|
4087
|
+
const holdReleased = alreadyReleased || await this.cancel(checkpoint.id).then(
|
|
4088
|
+
(result) => {
|
|
4089
|
+
cancelled = result;
|
|
4090
|
+
return true;
|
|
4091
|
+
},
|
|
3833
4092
|
() => false
|
|
3834
4093
|
);
|
|
3835
4094
|
throw new failure.kind({
|
|
3836
|
-
message: `${failure.message} ${holdReleased ? "Conto cancelled the checkpoint and released the held spend." : "Conto could not cancel the checkpoint yet."} Do not pay.`,
|
|
4095
|
+
message: `${failure.message} ${alreadyReleased ? "The held spend was already released." : holdReleased ? "Conto cancelled the checkpoint and released the held spend." : "Conto could not cancel the checkpoint yet."} Do not pay.`,
|
|
3837
4096
|
code: failure.code,
|
|
3838
|
-
checkpoint,
|
|
4097
|
+
checkpoint: cancelled ?? latest ?? checkpoint,
|
|
3839
4098
|
...linkRequestId ? { linkRequestId } : {},
|
|
3840
4099
|
...linkRequestId && approvalUrl ? { approvalUrl } : {},
|
|
3841
|
-
holdReleased
|
|
4100
|
+
holdReleased,
|
|
4101
|
+
...holdReleased && failure.retryGuidance ? { retryGuidance: failure.retryGuidance } : {}
|
|
3842
4102
|
});
|
|
3843
4103
|
}
|
|
3844
4104
|
};
|