@alytegen/sdk 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/QUICKSTART.md +70 -0
- package/README.md +92 -2
- package/dist/agent.d.ts +68 -0
- package/dist/agent.js +59 -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 +154 -0
- package/dist/merchant.js +106 -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 +51 -4
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,70 @@
|
|
|
1
|
+
# @alytegen/sdk — quickstart
|
|
2
|
+
|
|
3
|
+
Two clients, two credentials:
|
|
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
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @alytegen/sdk # zero dependencies, Node ≥18 (built-in fetch)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Merchant: onboard in five calls
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { AlyteMerchant } from '@alytegen/sdk';
|
|
18
|
+
|
|
19
|
+
const alyte = new AlyteMerchant({ baseUrl: ALYTE_URL, token: API_TOKEN });
|
|
20
|
+
|
|
21
|
+
const { merchant } = await alyte.createShop({ name: 'Nova Live', geo: 'DE', currency: 'EUR' });
|
|
22
|
+
await alyte.linkPsp(merchant.id, { // YOUR Stripe — money settles to YOU
|
|
23
|
+
psp: { id: 'stripe', name: 'Stripe' }, isDefault: true,
|
|
24
|
+
secret: MY_STRIPE_SK, // → vaulted; only an opaque ref persists
|
|
25
|
+
publishableKey: MY_STRIPE_PK, // powers the buyer card-capture iframe
|
|
26
|
+
});
|
|
27
|
+
const { event } = await alyte.createEvent({ merchantId: merchant.id, title: 'Warsaw', startsAt: '2026-11-04T19:00:00Z' });
|
|
28
|
+
const { tier } = await alyte.createTier({ eventId: event.id, title: 'GA', priceMinor: 4900, currency: 'EUR', kind: 'general', availableCount: 500 });
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Money is **integer minor units** everywhere (`4900` = €49.00). Floats are refused.
|
|
32
|
+
|
|
33
|
+
## Plugging in YOUR auth + user database (required for buyer flows)
|
|
34
|
+
|
|
35
|
+
Your customers already have accounts with you — Alyte does not replace that.
|
|
36
|
+
When a signed-in user wants an AI agent shopping for them, **your server attests
|
|
37
|
+
them** and you redirect:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const s = await alyte.buyerSessions.mint({ merchantId: merchant.id, email: user.email });
|
|
41
|
+
res.redirect(ALYTE_URL + s.redeemPath); // single-use, 15-min TTL
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
They land authenticated on the shop's authorize page: pick or bring an agent,
|
|
45
|
+
set a spending cap and scope — all enforced server-side by Alyte on every
|
|
46
|
+
purchase. `buyerId` is deterministic per email, so the same user arriving later
|
|
47
|
+
via the email magic-link fallback is the same buyer.
|
|
48
|
+
|
|
49
|
+
## Agent: the buy path
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { AlyteAgent } from '@alytegen/sdk';
|
|
53
|
+
const agent = new AlyteAgent({ baseUrl: ALYTE_URL, token: AGENT_JWT });
|
|
54
|
+
|
|
55
|
+
const { events } = await agent.discoverInventory(merchantId); // live availability + on-sale windows
|
|
56
|
+
const quote = await agent.quote({ tierId, quantity: 2 }); // price LOCKED until expiresAt
|
|
57
|
+
const hold = await agent.reserve({ tierId, quantity: 2 }); // oversell-proof, auto-releasing
|
|
58
|
+
const out = await agent.confirm(hold.id,
|
|
59
|
+
{ quoteId: quote.id, instrumentToken, scheme: 'visa', geo: 'DE' },
|
|
60
|
+
{ idempotencyKey: crypto.randomUUID() }); // retries can never double-charge
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
No amount is ever passed at confirm — the charge is the locked quote. Refusals
|
|
64
|
+
throw `AlyteApiError` with a **stable `code`**: a spend-cap refusal means the
|
|
65
|
+
buyer's mandate said no; surface it, don't retry.
|
|
66
|
+
|
|
67
|
+
Full runnable examples: [`examples/merchant-onboard.ts`](https://github.com/Alytegen/alyte-sdk/blob/main/examples/merchant-onboard.ts),
|
|
68
|
+
[`examples/agent-buy.ts`](https://github.com/Alytegen/alyte-sdk/blob/main/examples/agent-buy.ts), and the complete
|
|
69
|
+
reference integration — buyer hand-off, signed webhook receipt, exactly-once fulfilment, self-diagnosis — in
|
|
70
|
+
[`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,93 @@
|
|
|
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
|
+
## Three 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
|
+
|
|
25
|
+
## Merchant: onboard in four calls
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { AlyteMerchant } from '@alytegen/sdk';
|
|
29
|
+
|
|
30
|
+
const alyte = new AlyteMerchant({ baseUrl: ALYTE_URL, token: API_TOKEN });
|
|
31
|
+
|
|
32
|
+
const { merchant } = await alyte.createShop({ name: 'Nova Live', geo: 'DE', currency: 'EUR' });
|
|
33
|
+
await alyte.linkPsp(merchant.id, { // YOUR Stripe — money settles to YOU
|
|
34
|
+
psp: { id: 'stripe', name: 'Stripe' }, isDefault: true,
|
|
35
|
+
secret: MY_STRIPE_SK, // vaulted; only an opaque reference persists
|
|
36
|
+
publishableKey: MY_STRIPE_PK, // powers the buyer's card-capture frame
|
|
37
|
+
});
|
|
38
|
+
const { event } = await alyte.createEvent({ merchantId: merchant.id, title: 'Warsaw', startsAt: '2026-11-04T19:00:00Z' });
|
|
39
|
+
const { tier } = await alyte.createTier({ eventId: event.id, title: 'GA', priceMinor: 4900, currency: 'EUR', kind: 'general', availableCount: 500 });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Hand a signed-in customer to their agent
|
|
43
|
+
|
|
44
|
+
Your users already have accounts with you. Your server attests one of them and redirects;
|
|
45
|
+
they land authenticated on the shop's authorize page and grant their agent a mandate that
|
|
46
|
+
Alyte enforces on every purchase.
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const s = await alyte.buyerSessions.mint({ merchantId: merchant.id, email: user.email });
|
|
50
|
+
res.redirect(ALYTE_URL + s.redeemPath); // single-use, 15-minute link
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Receive purchases
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { createWebhookHandler } from '@alytegen/sdk';
|
|
57
|
+
|
|
58
|
+
const handler = createWebhookHandler({
|
|
59
|
+
secret: ALYTE_WEBHOOK_SECRET,
|
|
60
|
+
onPaymentSettled: async (evt) => issueTickets(evt), // runs at least once — make it idempotent
|
|
61
|
+
onWatchFailed: async (evt) => notifyBuyer(evt),
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Agent: the buy path
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { AlyteAgent } from '@alytegen/sdk';
|
|
69
|
+
|
|
70
|
+
const agent = new AlyteAgent({ baseUrl: ALYTE_URL, token: AGENT_JWT });
|
|
71
|
+
const quote = await agent.quote({ tierId, quantity: 2 }); // price locked until expiresAt
|
|
72
|
+
const hold = await agent.reserve({ tierId, quantity: 2 }); // oversell-proof, auto-releasing
|
|
73
|
+
const out = await agent.confirm(hold.id,
|
|
74
|
+
{ quoteId: quote.id, instrumentToken, scheme: 'visa', geo: 'DE' },
|
|
75
|
+
{ idempotencyKey: crypto.randomUUID() }); // retries never double-charge
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
No amount is passed at confirm — the charge is the locked quote. A spend-cap refusal means
|
|
79
|
+
the buyer's mandate said no: surface it, don't retry.
|
|
80
|
+
|
|
81
|
+
## Learn more
|
|
82
|
+
|
|
83
|
+
- [QUICKSTART.md](./QUICKSTART.md) — the walkthrough with every call explained.
|
|
84
|
+
- [`examples/reference-merchant/server.ts`](./examples/reference-merchant/server.ts) — a
|
|
85
|
+
complete, one-file merchant server: hand-off, webhook receipt, exactly-once fulfilment,
|
|
86
|
+
and a diagnostic endpoint for when something goes wrong.
|
|
87
|
+
- [`examples/merchant-onboard.ts`](./examples/merchant-onboard.ts),
|
|
88
|
+
[`examples/agent-buy.ts`](./examples/agent-buy.ts) — the two paths above, runnable.
|
|
89
|
+
- Issues and questions: <https://github.com/Alytegen/alyte-sdk/issues>.
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
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
|
+
}
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Minimal fetch transport shared by both clients. Zero dependencies. */
|
|
2
|
+
export interface ClientOptions {
|
|
3
|
+
/** Your Alyte deployment origin, e.g. https://alyte-sandbox-….run.app */
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
/** The bearer credential (merchant API token or agent JWT). Keep it server-side. */
|
|
6
|
+
token: string;
|
|
7
|
+
/** Override fetch (tests, polyfills). Defaults to globalThis.fetch. */
|
|
8
|
+
fetch?: typeof globalThis.fetch;
|
|
9
|
+
}
|
|
10
|
+
/** A typed Alyte API failure: HTTP status + the stable error code from the taxonomy. */
|
|
11
|
+
export declare class AlyteApiError extends Error {
|
|
12
|
+
readonly status: number;
|
|
13
|
+
readonly code: string;
|
|
14
|
+
readonly details?: unknown | undefined;
|
|
15
|
+
constructor(status: number, code: string, message: string, details?: unknown | undefined);
|
|
16
|
+
}
|
|
17
|
+
export declare class Transport {
|
|
18
|
+
private readonly base;
|
|
19
|
+
private readonly token;
|
|
20
|
+
private readonly fetchImpl;
|
|
21
|
+
constructor(opts: ClientOptions);
|
|
22
|
+
request<T>(method: 'GET' | 'POST' | 'PATCH' | 'DELETE', path: string, body?: unknown, headers?: Record<string, string>): Promise<T>;
|
|
23
|
+
}
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Minimal fetch transport shared by both clients. Zero dependencies. */
|
|
2
|
+
/** A typed Alyte API failure: HTTP status + the stable error code from the taxonomy. */
|
|
3
|
+
export class AlyteApiError extends Error {
|
|
4
|
+
status;
|
|
5
|
+
code;
|
|
6
|
+
details;
|
|
7
|
+
constructor(status, code, message, details) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.details = details;
|
|
12
|
+
this.name = 'AlyteApiError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class Transport {
|
|
16
|
+
base;
|
|
17
|
+
token;
|
|
18
|
+
fetchImpl;
|
|
19
|
+
constructor(opts) {
|
|
20
|
+
if (!opts.baseUrl)
|
|
21
|
+
throw new Error('baseUrl is required');
|
|
22
|
+
if (!opts.token)
|
|
23
|
+
throw new Error('token is required');
|
|
24
|
+
this.base = opts.baseUrl.replace(/\/+$/, '');
|
|
25
|
+
this.token = opts.token;
|
|
26
|
+
this.fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
27
|
+
}
|
|
28
|
+
async request(method, path, body, headers) {
|
|
29
|
+
const res = await this.fetchImpl(`${this.base}${path}`, {
|
|
30
|
+
method,
|
|
31
|
+
headers: {
|
|
32
|
+
authorization: `Bearer ${this.token}`,
|
|
33
|
+
...(body !== undefined ? { 'content-type': 'application/json' } : {}),
|
|
34
|
+
...headers,
|
|
35
|
+
},
|
|
36
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
37
|
+
});
|
|
38
|
+
const text = await res.text();
|
|
39
|
+
let json;
|
|
40
|
+
try {
|
|
41
|
+
json = text ? JSON.parse(text) : undefined;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
json = undefined;
|
|
45
|
+
}
|
|
46
|
+
// 2xx AND 202 (sca_required rides a 202 with a normal body) succeed; everything
|
|
47
|
+
// else surfaces the taxonomy code so callers can branch on retryable-vs-terminal.
|
|
48
|
+
if (res.ok || res.status === 202)
|
|
49
|
+
return json;
|
|
50
|
+
const err = json?.error;
|
|
51
|
+
throw new AlyteApiError(res.status, err?.code ?? `http_${res.status}`, err?.message ?? `Alyte API error (HTTP ${res.status})`, err?.details ?? json);
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { AlyteMerchant } from './merchant.js';
|
|
2
|
+
export { AlyteAgent } from './agent.js';
|
|
3
|
+
export { AlyteApiError, type ClientOptions } from './http.js';
|
|
4
|
+
export { verifyWebhookSignature } from './webhookVerify.js';
|
|
5
|
+
export { handleWebhook, createWebhookHandler, type AlyteEventEnvelope, type PaymentSettled, type WatchFailed, type WebhookTest, type PurchaseHandlers, type OnPurchaseOptions, type WebhookResult, } from './onPurchase.js';
|
|
6
|
+
export type * from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { type ClientOptions } from './http.js';
|
|
2
|
+
import type { BuyerSessionMint, CatalogMerchant, Merchant, EventItem, MerchantPspLink, Tier, TierKind, FeeBearer, Scheme, PaymentRow, WebhookEndpoint, MerchantInsights } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Merchant integration client — drives the `/v1/integration/*` surface with a
|
|
5
|
+
* console-minted API token (`merchant:read` / `merchant:write` scopes).
|
|
6
|
+
*
|
|
7
|
+
* SERVER-SIDE ONLY: the API token is a secret. Never ship it to a browser.
|
|
8
|
+
*
|
|
9
|
+
* Auth integration (the primary path): your customer is already signed in on
|
|
10
|
+
* YOUR site with YOUR auth — attest them via `buyerSessions.mint()` and redirect
|
|
11
|
+
* to the returned redeem link. No email round-trip; same buyer identity as the
|
|
12
|
+
* magic-link fallback.
|
|
13
|
+
*/
|
|
14
|
+
export declare class AlyteMerchant {
|
|
15
|
+
private readonly t;
|
|
16
|
+
constructor(opts: ClientOptions);
|
|
17
|
+
/** Your tenant's full catalog: shops → events → tiers (with restriction fields). */
|
|
18
|
+
catalog(): Promise<{
|
|
19
|
+
catalog: CatalogMerchant[];
|
|
20
|
+
}>;
|
|
21
|
+
createShop(input: {
|
|
22
|
+
name: string;
|
|
23
|
+
geo: string;
|
|
24
|
+
currency: string;
|
|
25
|
+
checkout?: 'api' | 'hosted';
|
|
26
|
+
checkoutUrl?: string;
|
|
27
|
+
allowedSchemes?: Scheme[];
|
|
28
|
+
feeBearer?: FeeBearer;
|
|
29
|
+
taxRateBps?: number;
|
|
30
|
+
}): Promise<{
|
|
31
|
+
merchant: Merchant;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Public discovery is OPT-IN and off by default. Flipping `discoverable: true`
|
|
35
|
+
* publishes the shop window (events, tiers, prices, availability states — never
|
|
36
|
+
* counts or PSP data) at `/v1/discovery/merchants/:id` and the ACP product feed
|
|
37
|
+
* at `…/feed.jsonl`. `imageUrl` is the shop image the feed requires.
|
|
38
|
+
*/
|
|
39
|
+
updateShopDiscovery(merchantId: string, input: {
|
|
40
|
+
discoverable?: boolean;
|
|
41
|
+
imageUrl?: string | null;
|
|
42
|
+
embedOrigin?: string | null;
|
|
43
|
+
}): Promise<{
|
|
44
|
+
merchant: Merchant;
|
|
45
|
+
}>;
|
|
46
|
+
createEvent(input: {
|
|
47
|
+
merchantId: string;
|
|
48
|
+
title: string;
|
|
49
|
+
startsAt: string;
|
|
50
|
+
}): Promise<{
|
|
51
|
+
event: EventItem;
|
|
52
|
+
}>;
|
|
53
|
+
/** `priceMinor` is integer minor units (cents) — the server refuses floats. */
|
|
54
|
+
createTier(input: {
|
|
55
|
+
eventId: string;
|
|
56
|
+
title: string;
|
|
57
|
+
priceMinor: number;
|
|
58
|
+
currency: string;
|
|
59
|
+
kind: TierKind;
|
|
60
|
+
availableCount?: number;
|
|
61
|
+
}): Promise<{
|
|
62
|
+
tier: Tier;
|
|
63
|
+
}>;
|
|
64
|
+
updateTier(tierId: string, patch: {
|
|
65
|
+
priceMinor?: number;
|
|
66
|
+
availableCount?: number;
|
|
67
|
+
maxQtyPerPurchase?: number | null;
|
|
68
|
+
sellable?: boolean;
|
|
69
|
+
onSaleStart?: string | null;
|
|
70
|
+
onSaleEnd?: string | null;
|
|
71
|
+
allowedGeos?: string[] | null;
|
|
72
|
+
allowedSchemes?: Scheme[] | null;
|
|
73
|
+
}): Promise<{
|
|
74
|
+
tier: Tier;
|
|
75
|
+
}>;
|
|
76
|
+
deleteTier(tierId: string): Promise<{
|
|
77
|
+
ok: boolean;
|
|
78
|
+
}>;
|
|
79
|
+
deleteEvent(eventId: string): Promise<{
|
|
80
|
+
ok: boolean;
|
|
81
|
+
}>;
|
|
82
|
+
listPsps(merchantId: string): Promise<{
|
|
83
|
+
psps: MerchantPspLink[];
|
|
84
|
+
}>;
|
|
85
|
+
/**
|
|
86
|
+
* Link a PSP. `secret` (your PSP secret key) is captured straight into Alyte's
|
|
87
|
+
* secret store — only an opaque `credentialRef` is ever persisted or returned.
|
|
88
|
+
* `publishableKey` (pk_…) is public material for the card-capture iframe.
|
|
89
|
+
*/
|
|
90
|
+
linkPsp(merchantId: string, input: {
|
|
91
|
+
psp: {
|
|
92
|
+
id: string;
|
|
93
|
+
name: string;
|
|
94
|
+
};
|
|
95
|
+
rateBps?: number;
|
|
96
|
+
isDefault?: boolean;
|
|
97
|
+
secret?: string;
|
|
98
|
+
publishableKey?: string;
|
|
99
|
+
}): Promise<{
|
|
100
|
+
ok: boolean;
|
|
101
|
+
pspId: string;
|
|
102
|
+
credentialRef?: string;
|
|
103
|
+
}>;
|
|
104
|
+
/**
|
|
105
|
+
* The payments feed, newest first. Poll with `since` = the newest `createdAt`
|
|
106
|
+
* you have seen and dedupe by `intentId`. FULFIL on `status === 'authorized'`
|
|
107
|
+
* using `tierId` + `quantity` — never by price-matching. Do not advance your
|
|
108
|
+
* cursor past a row that is still `pending` (it becomes `authorized` seconds
|
|
109
|
+
* later and `since` would skip it) — or use webhooks, which only fire on
|
|
110
|
+
* authorized and have no such race.
|
|
111
|
+
*/
|
|
112
|
+
listPayments(opts?: {
|
|
113
|
+
limit?: number;
|
|
114
|
+
since?: string;
|
|
115
|
+
} | number): Promise<{
|
|
116
|
+
payments: PaymentRow[];
|
|
117
|
+
}>;
|
|
118
|
+
getPayment(intentId: string): Promise<unknown>;
|
|
119
|
+
/** Agent adoption + per-tier watch demand for a shop (counts only — the
|
|
120
|
+
* "N in line" number; never buyer identities). */
|
|
121
|
+
insights(merchantId: string): Promise<MerchantInsights>;
|
|
122
|
+
readonly webhooks: {
|
|
123
|
+
/**
|
|
124
|
+
* Register a payment.settled push endpoint (https; ≤3 active per account).
|
|
125
|
+
* The returned `secret` (whsec_…) is shown ONLY here — store it like a
|
|
126
|
+
* password and verify every delivery with `verifyWebhookSignature`.
|
|
127
|
+
*/
|
|
128
|
+
create: (input: {
|
|
129
|
+
url: string;
|
|
130
|
+
description?: string;
|
|
131
|
+
}) => Promise<{
|
|
132
|
+
webhook: WebhookEndpoint;
|
|
133
|
+
secret: string;
|
|
134
|
+
}>;
|
|
135
|
+
list: () => Promise<{
|
|
136
|
+
webhooks: WebhookEndpoint[];
|
|
137
|
+
}>;
|
|
138
|
+
/** Disable an endpoint — delivery (including queued retries) stops immediately. */
|
|
139
|
+
delete: (id: string) => Promise<{
|
|
140
|
+
ok: boolean;
|
|
141
|
+
}>;
|
|
142
|
+
};
|
|
143
|
+
readonly buyerSessions: {
|
|
144
|
+
/**
|
|
145
|
+
* Attest YOUR authenticated user → a single-use redeem link (15-min TTL).
|
|
146
|
+
* Redirect the user to `baseUrl + redeemPath`; the buyer-session cookie and
|
|
147
|
+
* the shop's authorize page take over from there.
|
|
148
|
+
*/
|
|
149
|
+
mint: (input: {
|
|
150
|
+
merchantId: string;
|
|
151
|
+
email: string;
|
|
152
|
+
}) => Promise<BuyerSessionMint>;
|
|
153
|
+
};
|
|
154
|
+
}
|
package/dist/merchant.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Transport } from './http.js';
|
|
2
|
+
/**
|
|
3
|
+
* Merchant integration client — drives the `/v1/integration/*` surface with a
|
|
4
|
+
* console-minted API token (`merchant:read` / `merchant:write` scopes).
|
|
5
|
+
*
|
|
6
|
+
* SERVER-SIDE ONLY: the API token is a secret. Never ship it to a browser.
|
|
7
|
+
*
|
|
8
|
+
* Auth integration (the primary path): your customer is already signed in on
|
|
9
|
+
* YOUR site with YOUR auth — attest them via `buyerSessions.mint()` and redirect
|
|
10
|
+
* to the returned redeem link. No email round-trip; same buyer identity as the
|
|
11
|
+
* magic-link fallback.
|
|
12
|
+
*/
|
|
13
|
+
export class AlyteMerchant {
|
|
14
|
+
t;
|
|
15
|
+
constructor(opts) {
|
|
16
|
+
this.t = new Transport(opts);
|
|
17
|
+
}
|
|
18
|
+
/** Your tenant's full catalog: shops → events → tiers (with restriction fields). */
|
|
19
|
+
catalog() {
|
|
20
|
+
return this.t.request('GET', '/v1/integration/catalog');
|
|
21
|
+
}
|
|
22
|
+
createShop(input) {
|
|
23
|
+
return this.t.request('POST', '/v1/integration/merchants', input);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Public discovery is OPT-IN and off by default. Flipping `discoverable: true`
|
|
27
|
+
* publishes the shop window (events, tiers, prices, availability states — never
|
|
28
|
+
* counts or PSP data) at `/v1/discovery/merchants/:id` and the ACP product feed
|
|
29
|
+
* at `…/feed.jsonl`. `imageUrl` is the shop image the feed requires.
|
|
30
|
+
*/
|
|
31
|
+
updateShopDiscovery(merchantId, input) {
|
|
32
|
+
return this.t.request('PATCH', `/v1/integration/merchants/${encodeURIComponent(merchantId)}`, input);
|
|
33
|
+
}
|
|
34
|
+
createEvent(input) {
|
|
35
|
+
return this.t.request('POST', '/v1/integration/events', input);
|
|
36
|
+
}
|
|
37
|
+
/** `priceMinor` is integer minor units (cents) — the server refuses floats. */
|
|
38
|
+
createTier(input) {
|
|
39
|
+
return this.t.request('POST', '/v1/integration/tiers', input);
|
|
40
|
+
}
|
|
41
|
+
updateTier(tierId, patch) {
|
|
42
|
+
return this.t.request('PATCH', `/v1/integration/tiers/${encodeURIComponent(tierId)}`, patch);
|
|
43
|
+
}
|
|
44
|
+
deleteTier(tierId) {
|
|
45
|
+
return this.t.request('DELETE', `/v1/integration/tiers/${encodeURIComponent(tierId)}`);
|
|
46
|
+
}
|
|
47
|
+
deleteEvent(eventId) {
|
|
48
|
+
return this.t.request('DELETE', `/v1/integration/events/${encodeURIComponent(eventId)}`);
|
|
49
|
+
}
|
|
50
|
+
listPsps(merchantId) {
|
|
51
|
+
return this.t.request('GET', `/v1/integration/merchants/${encodeURIComponent(merchantId)}/psps`);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Link a PSP. `secret` (your PSP secret key) is captured straight into Alyte's
|
|
55
|
+
* secret store — only an opaque `credentialRef` is ever persisted or returned.
|
|
56
|
+
* `publishableKey` (pk_…) is public material for the card-capture iframe.
|
|
57
|
+
*/
|
|
58
|
+
linkPsp(merchantId, input) {
|
|
59
|
+
return this.t.request('POST', `/v1/integration/merchants/${encodeURIComponent(merchantId)}/psps`, input);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The payments feed, newest first. Poll with `since` = the newest `createdAt`
|
|
63
|
+
* you have seen and dedupe by `intentId`. FULFIL on `status === 'authorized'`
|
|
64
|
+
* using `tierId` + `quantity` — never by price-matching. Do not advance your
|
|
65
|
+
* cursor past a row that is still `pending` (it becomes `authorized` seconds
|
|
66
|
+
* later and `since` would skip it) — or use webhooks, which only fire on
|
|
67
|
+
* authorized and have no such race.
|
|
68
|
+
*/
|
|
69
|
+
listPayments(opts) {
|
|
70
|
+
const o = typeof opts === 'number' ? { limit: opts } : (opts ?? {});
|
|
71
|
+
const q = new URLSearchParams();
|
|
72
|
+
if (o.limit)
|
|
73
|
+
q.set('limit', String(o.limit));
|
|
74
|
+
if (o.since)
|
|
75
|
+
q.set('since', o.since);
|
|
76
|
+
const qs = q.toString();
|
|
77
|
+
return this.t.request('GET', `/v1/integration/payments${qs ? `?${qs}` : ''}`);
|
|
78
|
+
}
|
|
79
|
+
getPayment(intentId) {
|
|
80
|
+
return this.t.request('GET', `/v1/integration/payments/${encodeURIComponent(intentId)}`);
|
|
81
|
+
}
|
|
82
|
+
/** Agent adoption + per-tier watch demand for a shop (counts only — the
|
|
83
|
+
* "N in line" number; never buyer identities). */
|
|
84
|
+
insights(merchantId) {
|
|
85
|
+
return this.t.request('GET', `/v1/integration/merchants/${encodeURIComponent(merchantId)}/insights`);
|
|
86
|
+
}
|
|
87
|
+
webhooks = {
|
|
88
|
+
/**
|
|
89
|
+
* Register a payment.settled push endpoint (https; ≤3 active per account).
|
|
90
|
+
* The returned `secret` (whsec_…) is shown ONLY here — store it like a
|
|
91
|
+
* password and verify every delivery with `verifyWebhookSignature`.
|
|
92
|
+
*/
|
|
93
|
+
create: (input) => this.t.request('POST', '/v1/integration/webhooks', input),
|
|
94
|
+
list: () => this.t.request('GET', '/v1/integration/webhooks'),
|
|
95
|
+
/** Disable an endpoint — delivery (including queued retries) stops immediately. */
|
|
96
|
+
delete: (id) => this.t.request('DELETE', `/v1/integration/webhooks/${encodeURIComponent(id)}`),
|
|
97
|
+
};
|
|
98
|
+
buyerSessions = {
|
|
99
|
+
/**
|
|
100
|
+
* Attest YOUR authenticated user → a single-use redeem link (15-min TTL).
|
|
101
|
+
* Redirect the user to `baseUrl + redeemPath`; the buyer-session cookie and
|
|
102
|
+
* the shop's authorize page take over from there.
|
|
103
|
+
*/
|
|
104
|
+
mint: (input) => this.t.request('POST', '/v1/integration/buyer-sessions', input),
|
|
105
|
+
};
|
|
106
|
+
}
|