@alytegen/sdk 0.0.0-stage → 0.4.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/BROWSER.md +83 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +78 -0
- package/README.md +96 -2
- package/dist/agent.d.ts +68 -0
- package/dist/agent.js +59 -0
- package/dist/browser.d.ts +183 -0
- package/dist/browser.js +159 -0
- package/dist/http.d.ts +23 -0
- package/dist/http.js +53 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/merchant.d.ts +171 -0
- package/dist/merchant.js +107 -0
- package/dist/onPurchase.d.ts +133 -0
- package/dist/onPurchase.js +92 -0
- package/dist/types.d.ts +188 -0
- package/dist/types.js +5 -0
- package/dist/webhookVerify.d.ts +11 -0
- package/dist/webhookVerify.js +23 -0
- package/examples/agent-buy.ts +47 -0
- package/examples/merchant-onboard.ts +48 -0
- package/examples/reference-merchant/server.ts +190 -0
- package/package.json +56 -4
package/BROWSER.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Buyer purchases inside your chat
|
|
2
|
+
|
|
3
|
+
Available in SDK 0.4.0 with Alyte's Model 1 browser endpoints. The merchant token
|
|
4
|
+
stays on your backend. Your chat receives a short-lived bounded buyer bearer;
|
|
5
|
+
creating or widening permission requires email verification inside Alyte's iframe.
|
|
6
|
+
The full verification bearer stays there. No raw agent credential reaches your chat.
|
|
7
|
+
|
|
8
|
+
## Register and attest
|
|
9
|
+
|
|
10
|
+
Register your exact HTTPS chat origin in the shop's `embedOrigin` (multiple origins
|
|
11
|
+
are comma-separated). This controls both framing and buyer CORS. New registrations
|
|
12
|
+
can take up to 30 seconds to reach preflight caches. Local HTTP development is supported.
|
|
13
|
+
On your backend, authenticate your own user, derive their email from that session,
|
|
14
|
+
then call `merchant.buyerSessions.mint({ merchantId, email })`. Return only the `token`
|
|
15
|
+
query value extracted from `new URL(session.redeemPath, ALYTE_URL)` to that user's
|
|
16
|
+
browser. Do not navigate the redeem link too: either consumer uses the token once.
|
|
17
|
+
The mint endpoint must not accept an arbitrary browser-supplied email as identity.
|
|
18
|
+
|
|
19
|
+
## Connect and open consent
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { exchangeBuyerSession, createBuyerClient, openConsent } from '@alytegen/sdk/browser';
|
|
23
|
+
|
|
24
|
+
const connection = await exchangeBuyerSession({ baseUrl: ALYTE_URL, token });
|
|
25
|
+
const buyer = createBuyerClient({ baseUrl: ALYTE_URL, bearer: connection.bearer });
|
|
26
|
+
const controller = new AbortController(); // abort when your chat/modal closes
|
|
27
|
+
const { agentId, mandateId } = await openConsent({
|
|
28
|
+
baseUrl: ALYTE_URL, merchantId, bearer: connection.bearer,
|
|
29
|
+
container: document.querySelector<HTMLElement>('#alyte-consent')!,
|
|
30
|
+
signal: controller.signal, lang: 'en',
|
|
31
|
+
theme: { accent: '#006466', font: 'sans', radius: 8, scheme: 'light' },
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Keep the connection in memory. It expires after 30 minutes (`expiresAt`). On expiry
|
|
36
|
+
or a `401 unauthorized`, ask your backend for a fresh mint and exchange it; a replayed
|
|
37
|
+
or expired mint returns `401 link_invalid`. Create a new client with the new bearer.
|
|
38
|
+
Cookie login endpoints are not available to the parent chat.
|
|
39
|
+
|
|
40
|
+
The iframe lets the buyer choose an existing usable Alyte agent/permission or
|
|
41
|
+
create one. Selecting an existing permission needs no new email verification.
|
|
42
|
+
Creating authority does. Completion returns IDs only and removes the iframe.
|
|
43
|
+
Cancellation/abort rejects with `AlyteConsentError.code = 'consent_cancelled'`;
|
|
44
|
+
an initial handshake timeout uses `consent_unavailable`. Server errors retain their
|
|
45
|
+
code. `readyTimeoutMs` defaults to 15 seconds and never limits code-entry time.
|
|
46
|
+
|
|
47
|
+
Optional `returnUrl` must belong to the chat origin. `onNavigate(url)` decides how
|
|
48
|
+
your chat handles the Back link. The SDK does not navigate the host automatically.
|
|
49
|
+
Themes accept only `accent`, `background`, `surface`, `text`, `font`, `radius`, `scheme`.
|
|
50
|
+
Colours are opaque hex; font is system/sans/serif, radius is 0–16, scheme light/dark.
|
|
51
|
+
If contrast fails, all four colours fall back to scheme defaults; they are not
|
|
52
|
+
adjusted. Permission terms, errors and disabled controls keep fixed readable colours.
|
|
53
|
+
Provider card inputs keep their provider styling.
|
|
54
|
+
|
|
55
|
+
## Buy, watch and revoke
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const attempt = { agentId, tierId, quantity: 1, attemptId: crypto.randomUUID() };
|
|
59
|
+
// Persist BEFORE sending. Use your app's durable purchase-attempt store; retain it
|
|
60
|
+
// across reloads and reconnects. Namespace it to this buyer/shop/cart.
|
|
61
|
+
await attempts.save(attempt);
|
|
62
|
+
const purchase = await buyer.buy(attempt);
|
|
63
|
+
if (purchase.status === 'pending') {
|
|
64
|
+
const receipt = await buyer.payment(purchase.intentId); // refresh later if pending
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The UUID is required; the SDK never invents one or retries requests automatically.
|
|
69
|
+
After a network failure, resume with the **same saved attempt**, including agent,
|
|
70
|
+
tier, quantity and ID. Never create a fresh attempt to retry an uncertain charge.
|
|
71
|
+
`settled` is success; `pending` remains unresolved. `sca_required` is an authentication
|
|
72
|
+
continuation requirement; browser SCA resume is not yet available. Do not treat it
|
|
73
|
+
as success or dispatch a new purchase. Non-2xx responses throw `AlyteApiError` with
|
|
74
|
+
`status`, `code` and `details`; purchase `402` outcome bodies are in `details`.
|
|
75
|
+
|
|
76
|
+
Other methods: `me()`, `catalog()`, `agents()`, `instruments()`, `instrumentCaps()`,
|
|
77
|
+
`watches()`, `watch({ agentId, tierId, quantity, expiresInDays? })`,
|
|
78
|
+
`cancelWatch(id)`, `revokeMandate(id)`. Revocation is immediate and server-enforced.
|
|
79
|
+
Every buy/watch still depends on the buyer's current mandate, card evidence and
|
|
80
|
+
live availability. A bounded bearer cannot create or widen spending authority.
|
|
81
|
+
|
|
82
|
+
Your backend still fulfills from verified settlement webhooks, idempotently.
|
|
83
|
+
Browser completion messages are presentation state, not fulfillment evidence.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alyte Agentic Pay
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/QUICKSTART.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @alytegen/sdk — quickstart
|
|
2
|
+
|
|
3
|
+
Choose the credential for the runtime:
|
|
4
|
+
|
|
5
|
+
| Client | Credential | Runs | Does |
|
|
6
|
+
|---|---|---|---|
|
|
7
|
+
| `AlyteMerchant` | API token (console → API keys, `merchant:read`/`merchant:write`) | **your server only** | catalog CRUD, PSP links, payments visibility, buyer-session mint |
|
|
8
|
+
| `AlyteAgent` | agent JWT | your agent runtime | discover → quote → hold → confirm |
|
|
9
|
+
| `@alytegen/sdk/browser` | bounded buyer bearer | your chat page | embedded consent and purchases using the buyer's Alyte agent |
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @alytegen/sdk # zero dependencies, Node ≥18 (built-in fetch)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Merchant: onboard in five calls
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { AlyteMerchant } from '@alytegen/sdk';
|
|
19
|
+
|
|
20
|
+
const alyte = new AlyteMerchant({ baseUrl: ALYTE_URL, token: API_TOKEN });
|
|
21
|
+
|
|
22
|
+
const { merchant } = await alyte.createShop({ name: 'Nova Live', geo: 'DE', currency: 'EUR' });
|
|
23
|
+
await alyte.linkPsp(merchant.id, { // YOUR Stripe — money settles to YOU
|
|
24
|
+
psp: { id: 'stripe', name: 'Stripe' }, isDefault: true,
|
|
25
|
+
secret: MY_STRIPE_SK, // → vaulted; only an opaque ref persists
|
|
26
|
+
publishableKey: MY_STRIPE_PK, // powers the buyer card-capture iframe
|
|
27
|
+
});
|
|
28
|
+
const { event } = await alyte.createEvent({ merchantId: merchant.id, title: 'Warsaw', startsAt: '2026-11-04T19:00:00Z' });
|
|
29
|
+
const { tier } = await alyte.createTier({ eventId: event.id, title: 'GA', priceMinor: 4900, currency: 'EUR', kind: 'general', availableCount: 500 });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Money is **integer minor units** everywhere (`4900` = €49.00). Floats are refused.
|
|
33
|
+
|
|
34
|
+
## Plugging in YOUR auth + user database (required for buyer flows)
|
|
35
|
+
|
|
36
|
+
Your customers already have accounts with you — Alyte does not replace that.
|
|
37
|
+
When a signed-in user wants an AI agent shopping for them, **your server attests
|
|
38
|
+
them** and you redirect:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
const s = await alyte.buyerSessions.mint({
|
|
42
|
+
merchantId: merchant.id, email: user.email,
|
|
43
|
+
lang: user.locale, // optional: Alyte's pages open in the language of YOUR site
|
|
44
|
+
});
|
|
45
|
+
res.redirect(ALYTE_URL + s.redeemPath); // single-use, 15-min TTL
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Embedding in a chat? Follow [BROWSER.md](./BROWSER.md) to exchange the single-use
|
|
49
|
+
token and launch consent without third-party cookies. Directly framing the full
|
|
50
|
+
authorize page is also supported, but its cookie session depends on browser policy.
|
|
51
|
+
|
|
52
|
+
They land authenticated on the shop's authorize page: pick or bring an agent,
|
|
53
|
+
set a spending cap and scope — all enforced server-side by Alyte on every
|
|
54
|
+
purchase. `buyerId` is deterministic per email, so the same user arriving later
|
|
55
|
+
via the email magic-link fallback is the same buyer.
|
|
56
|
+
|
|
57
|
+
## Agent: the buy path
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { AlyteAgent } from '@alytegen/sdk';
|
|
61
|
+
const agent = new AlyteAgent({ baseUrl: ALYTE_URL, token: AGENT_JWT });
|
|
62
|
+
|
|
63
|
+
const { events } = await agent.discoverInventory(merchantId); // live availability + on-sale windows
|
|
64
|
+
const quote = await agent.quote({ tierId, quantity: 2 }); // price LOCKED until expiresAt
|
|
65
|
+
const hold = await agent.reserve({ tierId, quantity: 2 }); // oversell-proof, auto-releasing
|
|
66
|
+
const out = await agent.confirm(hold.id,
|
|
67
|
+
{ quoteId: quote.id, instrumentToken, scheme: 'visa', geo: 'DE' },
|
|
68
|
+
{ idempotencyKey: crypto.randomUUID() }); // retries can never double-charge
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
No amount is ever passed at confirm — the charge is the locked quote. Refusals
|
|
72
|
+
throw `AlyteApiError` with a **stable `code`**: a spend-cap refusal means the
|
|
73
|
+
buyer's mandate said no; surface it, don't retry.
|
|
74
|
+
|
|
75
|
+
Full runnable examples: [`examples/merchant-onboard.ts`](https://github.com/Alytegen/alyte-sdk/blob/main/examples/merchant-onboard.ts),
|
|
76
|
+
[`examples/agent-buy.ts`](https://github.com/Alytegen/alyte-sdk/blob/main/examples/agent-buy.ts), and the complete
|
|
77
|
+
reference integration — buyer hand-off, signed webhook receipt, exactly-once fulfilment, self-diagnosis — in
|
|
78
|
+
[`examples/reference-merchant/server.ts`](https://github.com/Alytegen/alyte-sdk/blob/main/examples/reference-merchant/server.ts).
|
package/README.md
CHANGED
|
@@ -1,3 +1,97 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @alytegen/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript client for [Alyte](https://alytegen.com) — the authority layer that lets a buyer
|
|
4
|
+
hand an AI agent a bounded mandate (spend cap, shop, expiry) and lets a merchant sell to that
|
|
5
|
+
agent on the merchant's **own** payment provider. Alyte never holds funds: money settles
|
|
6
|
+
between the buyer and the merchant's Stripe or Adyen account.
|
|
7
|
+
|
|
8
|
+
- Zero runtime dependencies. Node ≥ 18 (built-in `fetch`). ESM.
|
|
9
|
+
- Hand-typed against the server's own schemas; every refusal is an `AlyteApiError` with a
|
|
10
|
+
stable `code`.
|
|
11
|
+
- Money is **integer minor units** everywhere (`4900` = €49.00). Floats are refused.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @alytegen/sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Entry points
|
|
18
|
+
|
|
19
|
+
| Export | Credential | Runs where | Does |
|
|
20
|
+
|---|---|---|---|
|
|
21
|
+
| `AlyteMerchant` | API token (console → API keys) | your server only | shops, events, tiers, PSP links, buyer-session mint, payments visibility |
|
|
22
|
+
| `AlyteAgent` | agent JWT | your agent runtime | discover → quote → hold → confirm |
|
|
23
|
+
| `createWebhookHandler` / `verifyWebhookSignature` | webhook secret | your server | signed, at-least-once purchase events with typed dispatch |
|
|
24
|
+
| `exchangeBuyerSession` / `createBuyerClient` / `openConsent` from `@alytegen/sdk/browser` | bounded buyer bearer | partner browser/chat | isolated consent, then buy/watch/list/revoke within existing authority |
|
|
25
|
+
|
|
26
|
+
The browser entry is new in 0.4.0. See [the chat integration](./BROWSER.md) for setup,
|
|
27
|
+
cookie-independent consent, reconnecting and handling uncertain purchases.
|
|
28
|
+
|
|
29
|
+
## Merchant: onboard in four calls
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { AlyteMerchant } from '@alytegen/sdk';
|
|
33
|
+
|
|
34
|
+
const alyte = new AlyteMerchant({ baseUrl: ALYTE_URL, token: API_TOKEN });
|
|
35
|
+
|
|
36
|
+
const { merchant } = await alyte.createShop({ name: 'Nova Live', geo: 'DE', currency: 'EUR' });
|
|
37
|
+
await alyte.linkPsp(merchant.id, { // YOUR Stripe — money settles to YOU
|
|
38
|
+
psp: { id: 'stripe', name: 'Stripe' }, isDefault: true,
|
|
39
|
+
secret: MY_STRIPE_SK, // vaulted; only an opaque reference persists
|
|
40
|
+
publishableKey: MY_STRIPE_PK, // powers the buyer's card-capture frame
|
|
41
|
+
});
|
|
42
|
+
const { event } = await alyte.createEvent({ merchantId: merchant.id, title: 'Warsaw', startsAt: '2026-11-04T19:00:00Z' });
|
|
43
|
+
const { tier } = await alyte.createTier({ eventId: event.id, title: 'GA', priceMinor: 4900, currency: 'EUR', kind: 'general', availableCount: 500 });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Hand a signed-in customer to their agent
|
|
47
|
+
|
|
48
|
+
Your users already have accounts with you. Your server attests one of them and redirects;
|
|
49
|
+
they land authenticated on the shop's authorize page and grant their agent a mandate that
|
|
50
|
+
Alyte enforces on every purchase.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
const s = await alyte.buyerSessions.mint({ merchantId: merchant.id, email: user.email });
|
|
54
|
+
res.redirect(ALYTE_URL + s.redeemPath); // single-use, 15-minute link
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Receive purchases
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { createWebhookHandler } from '@alytegen/sdk';
|
|
61
|
+
|
|
62
|
+
const handler = createWebhookHandler({
|
|
63
|
+
secret: ALYTE_WEBHOOK_SECRET,
|
|
64
|
+
onPaymentSettled: async (evt) => issueTickets(evt), // runs at least once — make it idempotent
|
|
65
|
+
onWatchFailed: async (evt) => notifyBuyer(evt),
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Agent: the buy path
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { AlyteAgent } from '@alytegen/sdk';
|
|
73
|
+
|
|
74
|
+
const agent = new AlyteAgent({ baseUrl: ALYTE_URL, token: AGENT_JWT });
|
|
75
|
+
const quote = await agent.quote({ tierId, quantity: 2 }); // price locked until expiresAt
|
|
76
|
+
const hold = await agent.reserve({ tierId, quantity: 2 }); // oversell-proof, auto-releasing
|
|
77
|
+
const out = await agent.confirm(hold.id,
|
|
78
|
+
{ quoteId: quote.id, instrumentToken, scheme: 'visa', geo: 'DE' },
|
|
79
|
+
{ idempotencyKey: crypto.randomUUID() }); // retries never double-charge
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
No amount is passed at confirm — the charge is the locked quote. A spend-cap refusal means
|
|
83
|
+
the buyer's mandate said no: surface it, don't retry.
|
|
84
|
+
|
|
85
|
+
## Learn more
|
|
86
|
+
|
|
87
|
+
- [QUICKSTART.md](./QUICKSTART.md) — the walkthrough with every call explained.
|
|
88
|
+
- [`examples/reference-merchant/server.ts`](./examples/reference-merchant/server.ts) — a
|
|
89
|
+
complete, one-file merchant server: hand-off, webhook receipt, exactly-once fulfilment,
|
|
90
|
+
and a diagnostic endpoint for when something goes wrong.
|
|
91
|
+
- [`examples/merchant-onboard.ts`](./examples/merchant-onboard.ts),
|
|
92
|
+
[`examples/agent-buy.ts`](./examples/agent-buy.ts) — the two paths above, runnable.
|
|
93
|
+
- Issues and questions: <https://github.com/Alytegen/alyte-sdk/issues>.
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
MIT — see [LICENSE](./LICENSE).
|
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type ClientOptions } from './http.js';
|
|
2
|
+
import type { ConfirmOutcome, DiscoveredEvent, Hold, Merchant, Quote, Scheme } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Agent buy-path client — the discover → quote → hold → confirm flow, with an
|
|
5
|
+
* agent JWT (`catalog:read` / `payments:write` scopes).
|
|
6
|
+
*
|
|
7
|
+
* Every call is checked SERVER-SIDE against the agent's mandate (spend cap,
|
|
8
|
+
* scope, ownership) — the SDK adds no authority, it only speaks the protocol.
|
|
9
|
+
* Amounts are never passed at confirm: the charge is the LOCKED quote total.
|
|
10
|
+
*/
|
|
11
|
+
export declare class AlyteAgent {
|
|
12
|
+
private readonly t;
|
|
13
|
+
constructor(opts: ClientOptions);
|
|
14
|
+
/** The merchants this agent may transact with (its tenant's catalog). */
|
|
15
|
+
listMerchants(): Promise<{
|
|
16
|
+
merchants: Merchant[];
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* A merchant's events + tiers with LIVE availability: `state` tells you whether
|
|
20
|
+
* a tier is on sale, and `retryAfter` when a drop hasn't opened yet — schedule
|
|
21
|
+
* a retry instead of polling.
|
|
22
|
+
*/
|
|
23
|
+
discoverInventory(merchantId: string): Promise<{
|
|
24
|
+
events: DiscoveredEvent[];
|
|
25
|
+
}>;
|
|
26
|
+
/** Lock a price for tier × quantity. The returned total is frozen until `expiresAt`. */
|
|
27
|
+
quote(input: {
|
|
28
|
+
tierId: string;
|
|
29
|
+
quantity: number;
|
|
30
|
+
}): Promise<Quote>;
|
|
31
|
+
/**
|
|
32
|
+
* Place an oversell-proof hold. Set `allowPartial: true` ONLY when your buyer
|
|
33
|
+
* accepts fewer tickets than requested (per-purchase caps); a partial hold
|
|
34
|
+
* reports `requestedQuantity` + `partialReason`.
|
|
35
|
+
*/
|
|
36
|
+
reserve(input: {
|
|
37
|
+
tierId: string;
|
|
38
|
+
quantity: number;
|
|
39
|
+
scheme?: Scheme;
|
|
40
|
+
geo?: string;
|
|
41
|
+
allowPartial?: boolean;
|
|
42
|
+
}): Promise<Hold>;
|
|
43
|
+
/**
|
|
44
|
+
* Pay the locked quote and confirm the hold. Pass a stable `idempotencyKey`
|
|
45
|
+
* per purchase intent so retries can NEVER double-charge. Outcomes:
|
|
46
|
+
* `settled` · `sca_required` (resume via `resolveSca`) · a thrown
|
|
47
|
+
* AlyteApiError on refusal (stable `code`, e.g. spend-cap or scheme refusals).
|
|
48
|
+
*/
|
|
49
|
+
confirm(holdId: string, input: {
|
|
50
|
+
quoteId: string;
|
|
51
|
+
instrumentToken: string;
|
|
52
|
+
scheme: Scheme;
|
|
53
|
+
geo: string;
|
|
54
|
+
}, opts?: {
|
|
55
|
+
idempotencyKey?: string;
|
|
56
|
+
}): Promise<ConfirmOutcome>;
|
|
57
|
+
/** Resolve a pending SCA challenge (bound to the creating agent) and resume. */
|
|
58
|
+
resolveSca(continueToken: string, outcome: 'approved' | 'failed'): Promise<ConfirmOutcome>;
|
|
59
|
+
/** Vaulted payment instruments this agent may spend from (opaque tokens only). */
|
|
60
|
+
listInstruments(): Promise<{
|
|
61
|
+
instruments: Array<{
|
|
62
|
+
token: string;
|
|
63
|
+
scheme: Scheme;
|
|
64
|
+
last4: string;
|
|
65
|
+
}>;
|
|
66
|
+
}>;
|
|
67
|
+
getPayment(intentId: string): Promise<unknown>;
|
|
68
|
+
}
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Transport } from './http.js';
|
|
2
|
+
/**
|
|
3
|
+
* Agent buy-path client — the discover → quote → hold → confirm flow, with an
|
|
4
|
+
* agent JWT (`catalog:read` / `payments:write` scopes).
|
|
5
|
+
*
|
|
6
|
+
* Every call is checked SERVER-SIDE against the agent's mandate (spend cap,
|
|
7
|
+
* scope, ownership) — the SDK adds no authority, it only speaks the protocol.
|
|
8
|
+
* Amounts are never passed at confirm: the charge is the LOCKED quote total.
|
|
9
|
+
*/
|
|
10
|
+
export class AlyteAgent {
|
|
11
|
+
t;
|
|
12
|
+
constructor(opts) {
|
|
13
|
+
this.t = new Transport(opts);
|
|
14
|
+
}
|
|
15
|
+
/** The merchants this agent may transact with (its tenant's catalog). */
|
|
16
|
+
listMerchants() {
|
|
17
|
+
return this.t.request('GET', '/v1/merchants');
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A merchant's events + tiers with LIVE availability: `state` tells you whether
|
|
21
|
+
* a tier is on sale, and `retryAfter` when a drop hasn't opened yet — schedule
|
|
22
|
+
* a retry instead of polling.
|
|
23
|
+
*/
|
|
24
|
+
discoverInventory(merchantId) {
|
|
25
|
+
return this.t.request('GET', `/v1/merchants/${encodeURIComponent(merchantId)}/events`);
|
|
26
|
+
}
|
|
27
|
+
/** Lock a price for tier × quantity. The returned total is frozen until `expiresAt`. */
|
|
28
|
+
quote(input) {
|
|
29
|
+
return this.t.request('POST', '/v1/quotes', input);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Place an oversell-proof hold. Set `allowPartial: true` ONLY when your buyer
|
|
33
|
+
* accepts fewer tickets than requested (per-purchase caps); a partial hold
|
|
34
|
+
* reports `requestedQuantity` + `partialReason`.
|
|
35
|
+
*/
|
|
36
|
+
reserve(input) {
|
|
37
|
+
return this.t.request('POST', '/v1/holds', input);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Pay the locked quote and confirm the hold. Pass a stable `idempotencyKey`
|
|
41
|
+
* per purchase intent so retries can NEVER double-charge. Outcomes:
|
|
42
|
+
* `settled` · `sca_required` (resume via `resolveSca`) · a thrown
|
|
43
|
+
* AlyteApiError on refusal (stable `code`, e.g. spend-cap or scheme refusals).
|
|
44
|
+
*/
|
|
45
|
+
confirm(holdId, input, opts) {
|
|
46
|
+
return this.t.request('POST', `/v1/holds/${encodeURIComponent(holdId)}/confirm`, input, opts?.idempotencyKey ? { 'idempotency-key': opts.idempotencyKey } : undefined);
|
|
47
|
+
}
|
|
48
|
+
/** Resolve a pending SCA challenge (bound to the creating agent) and resume. */
|
|
49
|
+
resolveSca(continueToken, outcome) {
|
|
50
|
+
return this.t.request('POST', `/v1/sca/${encodeURIComponent(continueToken)}/resolve`, { outcome });
|
|
51
|
+
}
|
|
52
|
+
/** Vaulted payment instruments this agent may spend from (opaque tokens only). */
|
|
53
|
+
listInstruments() {
|
|
54
|
+
return this.t.request('GET', '/v1/instruments');
|
|
55
|
+
}
|
|
56
|
+
getPayment(intentId) {
|
|
57
|
+
return this.t.request('GET', `/v1/payments/${encodeURIComponent(intentId)}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { DiscoveredEvent, Scheme } from './types.js';
|
|
2
|
+
export { AlyteApiError } from './http.js';
|
|
3
|
+
export interface BuyerConnection {
|
|
4
|
+
bearer: string;
|
|
5
|
+
buyerId: string;
|
|
6
|
+
expiresAt: string;
|
|
7
|
+
}
|
|
8
|
+
export interface BuyerIdentity {
|
|
9
|
+
tenantId: string;
|
|
10
|
+
buyerId: string;
|
|
11
|
+
email: string;
|
|
12
|
+
merchantId: string;
|
|
13
|
+
merchantName?: string;
|
|
14
|
+
currency?: string;
|
|
15
|
+
geo?: string;
|
|
16
|
+
tier: 'bounded' | 'full';
|
|
17
|
+
}
|
|
18
|
+
export interface BuyerMandate {
|
|
19
|
+
id: string;
|
|
20
|
+
agentId: string;
|
|
21
|
+
capMinor: number;
|
|
22
|
+
currency: string;
|
|
23
|
+
spentMinor?: number;
|
|
24
|
+
expiresAt?: string;
|
|
25
|
+
status: 'active' | 'amended' | 'revoked';
|
|
26
|
+
recurring: boolean;
|
|
27
|
+
maxTransactions?: number;
|
|
28
|
+
cadence?: string;
|
|
29
|
+
scope: {
|
|
30
|
+
merchants?: string[];
|
|
31
|
+
events?: string[];
|
|
32
|
+
tierKinds?: ('general' | 'seated')[];
|
|
33
|
+
quantity?: {
|
|
34
|
+
min?: number;
|
|
35
|
+
max?: number;
|
|
36
|
+
step?: number;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export interface BuyerAgent {
|
|
41
|
+
id: string;
|
|
42
|
+
name: string;
|
|
43
|
+
kind: 'alyte' | 'byoa';
|
|
44
|
+
spendCapMinor: number;
|
|
45
|
+
scheme: Scheme;
|
|
46
|
+
quantity: number;
|
|
47
|
+
instrumentToken?: string;
|
|
48
|
+
instrumentStatus?: 'ok' | 'missing';
|
|
49
|
+
mandates: BuyerMandate[];
|
|
50
|
+
}
|
|
51
|
+
export interface BuyerWatch {
|
|
52
|
+
id: string;
|
|
53
|
+
agentId: string;
|
|
54
|
+
merchantId: string;
|
|
55
|
+
tierId: string;
|
|
56
|
+
quantity: number;
|
|
57
|
+
status: 'queued' | 'firing' | 'fired' | 'failed' | 'cancelled' | 'expired';
|
|
58
|
+
result?: Record<string, unknown>;
|
|
59
|
+
expiresAt?: string;
|
|
60
|
+
createdAt: string;
|
|
61
|
+
firedAt?: string;
|
|
62
|
+
}
|
|
63
|
+
export type BuyerPurchase = {
|
|
64
|
+
status: 'settled' | 'pending';
|
|
65
|
+
intentId: string;
|
|
66
|
+
chargeMinor: number;
|
|
67
|
+
currency: string;
|
|
68
|
+
paymentState?: string;
|
|
69
|
+
agentName?: string;
|
|
70
|
+
} | {
|
|
71
|
+
status: 'sca_required';
|
|
72
|
+
} | {
|
|
73
|
+
status: 'refused' | 'error';
|
|
74
|
+
outcome: unknown;
|
|
75
|
+
};
|
|
76
|
+
export interface BuyerReceipt {
|
|
77
|
+
intentId: string;
|
|
78
|
+
status: 'settled' | 'pending' | 'refused';
|
|
79
|
+
chargeMinor: number;
|
|
80
|
+
currency: string;
|
|
81
|
+
paymentState?: string;
|
|
82
|
+
}
|
|
83
|
+
export interface BuyerClientOptions {
|
|
84
|
+
baseUrl: string;
|
|
85
|
+
bearer: string;
|
|
86
|
+
fetch?: typeof globalThis.fetch;
|
|
87
|
+
}
|
|
88
|
+
/** Pass only the token from the merchant-minted redeemPath. Keep the result in memory. */
|
|
89
|
+
export declare function exchangeBuyerSession(opts: {
|
|
90
|
+
baseUrl: string;
|
|
91
|
+
token: string;
|
|
92
|
+
fetch?: typeof globalThis.fetch;
|
|
93
|
+
}): Promise<BuyerConnection>;
|
|
94
|
+
/** Existing-authority actions only. Setup and permission changes belong in openConsent. */
|
|
95
|
+
export declare function createBuyerClient(opts: BuyerClientOptions): {
|
|
96
|
+
me: () => Promise<BuyerIdentity>;
|
|
97
|
+
catalog: () => Promise<{
|
|
98
|
+
events: DiscoveredEvent[];
|
|
99
|
+
}>;
|
|
100
|
+
agents: () => Promise<{
|
|
101
|
+
agents: BuyerAgent[];
|
|
102
|
+
}>;
|
|
103
|
+
instruments: () => Promise<{
|
|
104
|
+
instruments: Array<{
|
|
105
|
+
token: string;
|
|
106
|
+
scheme: Scheme;
|
|
107
|
+
last4: string;
|
|
108
|
+
mine: boolean;
|
|
109
|
+
}>;
|
|
110
|
+
}>;
|
|
111
|
+
instrumentCaps: () => Promise<{
|
|
112
|
+
caps: Array<{
|
|
113
|
+
instrumentToken: string;
|
|
114
|
+
capMinor: number;
|
|
115
|
+
currency: string;
|
|
116
|
+
grantedMinor: number;
|
|
117
|
+
}>;
|
|
118
|
+
}>;
|
|
119
|
+
watches: () => Promise<{
|
|
120
|
+
watches: BuyerWatch[];
|
|
121
|
+
}>;
|
|
122
|
+
/** Save attemptId BEFORE dispatch and reuse it after an uncertain response. Never retry with a new ID. */
|
|
123
|
+
buy: (input: {
|
|
124
|
+
agentId: string;
|
|
125
|
+
tierId: string;
|
|
126
|
+
quantity: number;
|
|
127
|
+
attemptId: string;
|
|
128
|
+
}) => Promise<BuyerPurchase>;
|
|
129
|
+
payment: (intentId: string) => Promise<BuyerReceipt>;
|
|
130
|
+
watch: (input: {
|
|
131
|
+
agentId: string;
|
|
132
|
+
tierId: string;
|
|
133
|
+
quantity: number;
|
|
134
|
+
expiresInDays?: number;
|
|
135
|
+
}) => Promise<{
|
|
136
|
+
watch: BuyerWatch;
|
|
137
|
+
}>;
|
|
138
|
+
cancelWatch: (watchId: string) => Promise<{
|
|
139
|
+
watch: BuyerWatch;
|
|
140
|
+
}>;
|
|
141
|
+
revokeMandate: (mandateId: string) => Promise<{
|
|
142
|
+
ok: true;
|
|
143
|
+
id: string;
|
|
144
|
+
status: "revoked";
|
|
145
|
+
}>;
|
|
146
|
+
};
|
|
147
|
+
export type BuyerClient = ReturnType<typeof createBuyerClient>;
|
|
148
|
+
export interface ConsentTheme {
|
|
149
|
+
accent?: string;
|
|
150
|
+
background?: string;
|
|
151
|
+
surface?: string;
|
|
152
|
+
text?: string;
|
|
153
|
+
font?: 'system' | 'sans' | 'serif';
|
|
154
|
+
radius?: number;
|
|
155
|
+
scheme?: 'light' | 'dark';
|
|
156
|
+
}
|
|
157
|
+
export interface ConsentResult {
|
|
158
|
+
agentId: string;
|
|
159
|
+
mandateId: string;
|
|
160
|
+
}
|
|
161
|
+
export interface ConsentOptions {
|
|
162
|
+
baseUrl: string;
|
|
163
|
+
merchantId: string;
|
|
164
|
+
bearer: string;
|
|
165
|
+
container: HTMLElement;
|
|
166
|
+
theme?: ConsentTheme;
|
|
167
|
+
tierId?: string;
|
|
168
|
+
quantity?: number;
|
|
169
|
+
lang?: string;
|
|
170
|
+
/** Optional back link. Restricted to this host origin; the callback decides navigation. */
|
|
171
|
+
returnUrl?: string;
|
|
172
|
+
onNavigate?: (url: string) => void;
|
|
173
|
+
/** Abort when your modal closes; removes the iframe and rejects with consent_cancelled. */
|
|
174
|
+
signal?: AbortSignal;
|
|
175
|
+
/** Bounds the initial ready handshake, not the buyer's time entering a code. Default 15s. */
|
|
176
|
+
readyTimeoutMs?: number;
|
|
177
|
+
}
|
|
178
|
+
export declare class AlyteConsentError extends Error {
|
|
179
|
+
readonly code: string;
|
|
180
|
+
constructor(code: string);
|
|
181
|
+
}
|
|
182
|
+
/** Mount Alyte's isolated consent surface; the full verification credential never leaves it. */
|
|
183
|
+
export declare function openConsent(opts: ConsentOptions): Promise<ConsentResult>;
|