---
title: "Take a payment after a hold expired"
description: "Read what is still available after an expired checkout hold, then buy the same stock if your payment handler decides to go ahead."
url: "https://agentpost.tools/docs/resourceshark/guides/late-payment"
kind: guide
needs:
  - what: "The `api` helper"
    from: "https://agentpost.tools/docs/platform/quickstart.md"
  - what: "A `dev` secret key, `key`, for a project that adopted ResourceShark"
    from: "https://agentpost.tools/docs/platform/guides/keys.md"
---

# Take a payment after a hold expired

When a payment arrives after a checkout hold expired, check what is free and decide whether to fill the order with a new consume. [Holds](/docs/resourceshark/concepts/holds.md) explains how a hold expires, and [Claims](/docs/resourceshark/concepts/claims.md) explains what a consume takes.

## Before you start

-   The `api` helper, from [Platform quickstart](/docs/platform/quickstart.md).
-   A `dev` secret key, `key`, for a project that adopted ResourceShark, from [Create a project and issue a key](/docs/platform/guides/keys.md).

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

## 1\. Set aside stock for a checkout

Use `api` and the `dev` secret key `key` for a project that adopted ResourceShark. Create a resource with five items, then hold two for 3 seconds. Run the blocks in order on your server and choose one tab in step 3.

```ts
const latePaymentExample = crypto.randomUUID();
const { resource: latePaymentStock } = await api(
  'POST',
  '/v1/resources/resources',
  key,
  {
    key: `late-payment-stock-${latePaymentExample}`,
    name: 'Checkout stock',
    unit: { code: 'item', singular: 'item', plural: 'items', precision: 0 },
    settings: { time: 'none', identity: 'count', supplyExpiry: 'none' },
    scope: { places: false, holders: false },
    tracking: 'none',
    shortage: 'deny',
  },
);
await api(
  'POST',
  '/v1/resources/supply',
  key,
  {
    resourceId: latePaymentStock.id,
    kind: 'added',
    quantity: 5,
    evidence: { kind: 'receipt', ref: latePaymentExample },
    sourceKey: `late-payment-receipt:${latePaymentExample}`,
  },
  { 'Idempotency-Key': `late-payment-supply-${latePaymentExample}` },
);

const latePaymentLines = [{ resourceId: latePaymentStock.id, quantity: 2 }];
const { hold: latePaymentHold } = await api(
  'POST',
  '/v1/resources/holds',
  key,
  {
    lines: latePaymentLines,
    ttl: '3s',
    reference: { kind: 'checkout', ref: latePaymentExample },
    sourceKey: `late-payment-checkout:${latePaymentExample}`,
  },
  { 'Idempotency-Key': `late-payment-hold-${latePaymentExample}` },
);
```

## 2\. Let the hold expire

Wait 4 seconds to simulate the payment arriving late. Use the order as evidence for the commit and the same order’s `sourceKey` if you go on to buy the stock.

```ts
await new Promise((resolve) => setTimeout(resolve, 4_000));
const latePaymentOrder = {
  evidence: { kind: 'order', ref: latePaymentExample },
  sourceKey: `late-payment-order:${latePaymentExample}`,
};
const latePaymentCommitKey = `late-payment-commit-${latePaymentExample}`;
const latePaymentBuyKey = `late-payment-buy-${latePaymentExample}`;
```

## 3\. Read the expired commit and choose whether to buy

Commit with `lines` omitted to ask for every held line. An expired commit answers [`hold_expired`](/docs/resourceshark/errors.md#hold_expired). While ResourceShark still keeps the hold’s details, the refusal gives what each line has available. The commit takes no stock.

If every line fits and your app chooses to fill the order, send a consume with the original lines and its own `Idempotency-Key`. The HTTP example reads `details` from the error `api` throws. The SDK’s `commitHold` exposes the same choice through the expired outcome’s `allFit` and `buy`.

```ts tab="HTTP"
let latePaymentError;
try {
  await api(
    'POST',
    `/v1/resources/holds/${latePaymentHold.id}/commit`,
    key,
    latePaymentOrder,
    { 'Idempotency-Key': latePaymentCommitKey },
  );
} catch (thrown) {
  latePaymentError = (thrown as { error?: any }).error;
}
if (
  latePaymentError?.code !== 'hold_expired' ||
  !latePaymentError.details?.lines
) {
  throw new Error('Expected an expired hold with line availability');
}
const latePaymentAvailability: { quantity: number; available: number }[] =
  latePaymentError.details.lines;
const latePaymentFits = latePaymentAvailability.every(
  ({ quantity, available }) => available >= quantity,
);
console.log(
  latePaymentAvailability[0].quantity,
  latePaymentAvailability[0].available,
  latePaymentFits,
); // 2 5 true

if (latePaymentFits) {
  await api(
    'POST',
    '/v1/resources/consume',
    key,
    {
      ...latePaymentOrder,
      lines: latePaymentLines,
    },
    { 'Idempotency-Key': latePaymentBuyKey },
  );
}
```

```ts tab="SDK"
import { createClient } from '@agentpost-tools/resourceshark-sdk';
import { commitHold } from '@agentpost-tools/resourceshark-sdk/server';

const latePaymentClient = createClient({ token: key });
const latePaymentOutcome = await commitHold(
  latePaymentClient,
  latePaymentHold.id,
  latePaymentOrder,
  { idempotencyKey: latePaymentCommitKey },
);
if (latePaymentOutcome.status !== 'expired' || !latePaymentOutcome.lines) {
  throw new Error('Expected an expired hold with line availability');
}
const {
  lines: latePaymentAvailability,
  allFit: latePaymentFits,
  buy: buyLatePaymentStock,
} = latePaymentOutcome;
console.log(
  latePaymentAvailability[0].quantity,
  latePaymentAvailability[0].available,
  latePaymentFits,
); // 2 5 true

if (latePaymentFits && buyLatePaymentStock) {
  const latePaymentPurchase = await buyLatePaymentStock({
    ...latePaymentOrder,
    idempotencyKey: latePaymentBuyKey,
  });
  if (latePaymentPurchase.status !== 'bought') {
    throw new Error(`Purchase outcome: ${latePaymentPurchase.status}`);
  }
}
```

Availability can change before the consume. If another checkout takes the stock first, the consume can answer [`insufficient_supply`](/docs/resourceshark/errors.md#insufficient_supply) and takes nothing. Handle that refusal, a line that already falls short, or an expired hold whose details are gone in your payment handler, for example by refunding through your payment provider.

## Check it worked

Read the balance and the hold. Three items are on hand, none are held, and the original hold is still expired.

```ts
const { balance: latePaymentBalance } = await api(
  'GET',
  `/v1/resources/resources/${latePaymentStock.id}/balance`,
  key,
);
const { hold: expiredPaymentHold } = await api(
  'GET',
  `/v1/resources/holds/${latePaymentHold.id}`,
  key,
);
console.log(
  latePaymentBalance.onHand,
  latePaymentBalance.held,
  expiredPaymentHold.status === 'expired',
); // 3 0 true
```

The [holds reference](/docs/resourceshark/reference/holds.md) has the commit, and the [claims reference](/docs/resourceshark/reference/claims.md) has the consume. The [SDK server helper](/docs/resourceshark/sdk/server.md) describes each `commitHold` outcome.
