# Accept network orders

Verify an agent's network order offline, accept it online so the person's budget holds across stores, and cancel it to refund the budget.

Use this when an agent pays at your checkout under a person's [network grant](https://provenanceauth.com/docs/mandate/concepts/network-grants.md). Its `Provenance-Intent` token binds your exact cart, and you accept it online before you ship, so the person's budget stays correct across every store.

**Before you begin:** verify agent checkouts with [`verifyAgentCheckout`](https://provenanceauth.com/docs/authorize/guides/verify-agent-checkout.md). Accepting needs an account API key with the `intent` scope.

1. **Verify offline**: the token and your exact cart
2. **Accept online**: the reservation becomes spend
3. **Ship**: or cancel to refund the budget

## 1. Recognize a network order

A network token carries `network: true` and the grant in `mandate_id`. `verifyAgentCheckout` checks the signature and your cart as usual, and then returns `accepted: false` with `reason: "requires_online_accept"`. Never ship on that result alone.

## 2. Accept it

`acceptNetworkOrder` does both steps: it verifies offline, then calls [`POST /v1/authorize/network_orders/confirm`](https://provenanceauth.com/docs/api/network.md#post-v1-authorize-network-orders-confirm).

```ts
import { acceptNetworkOrder, loadJwks } from "@provenanceauth/rails/checkout";

const jwks = loadJwks(process.env.PROVENANCE_API!);
const result = await acceptNetworkOrder(req.headers["provenance-intent"], {
  jwks: await jwks.get(),
  expected: myOrderRecord, // your own record of this cart
  apiBaseUrl: process.env.PROVENANCE_API!,
  apiKey: process.env.PROVENANCE_KEY!,
});
if (!result.accepted) return reject(result.reason);
// result.acceptance: { object: "network_acceptance", status: "accepted", amount, currency, order_id, … }
```

A repeat for the same token returns the first acceptance with `replayed: true`, so a retry is safe. Another store can't accept a token that was accepted already.

## 3. Cancel or refund

When the order is cancelled or refunded, [`POST /v1/authorize/network_orders/cancel`](https://provenanceauth.com/docs/api/network.md#post-v1-authorize-network-orders-cancel) with the same token gives its amount back to the person's budget. Only the store that accepted it can cancel it.

**Pricing:** each accepted order counts as one decision on your account ([plans and usage](https://provenanceauth.com/docs/account/plans-and-usage.md#billing-units)). Verifying, cancelling and failed accepts are free.

## Errors you might see

| Code | What to do |
| --- | --- |
| `network_token_expired` | The token is older than 10 minutes and its reservation was released. Ask the agent to authorize again. |
| `network_grant_inactive` | The person revoked or paused the grant. Don't ship. |
| `network_order_closed` | Already accepted by another store, cancelled or released. Don't ship. |
| `trial_ended` | A Free account used its trial API calls, or the trial ended. |

## See also

- [Network grants](https://provenanceauth.com/docs/mandate/concepts/network-grants.md)
- [Verify agent checkouts](https://provenanceauth.com/docs/authorize/guides/verify-agent-checkout.md)
- [Connect an agent](https://provenanceauth.com/docs/mandate/guides/connect-an-agent.md)
