@moneymq/sdk 0.3.2 → 0.7.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/dist/index.d.mts +718 -60
- package/dist/index.d.ts +718 -60
- package/dist/index.js +492 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +482 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
+
import { Signer } from 'x402-fetch';
|
|
2
|
+
export { Signer } from 'x402-fetch';
|
|
3
|
+
|
|
4
|
+
/** Payment requirements returned when payment is required (402) */
|
|
5
|
+
interface PaymentRequirements {
|
|
6
|
+
scheme: string;
|
|
7
|
+
network: string;
|
|
8
|
+
max_amount_required: string;
|
|
9
|
+
resource: string;
|
|
10
|
+
description: string;
|
|
11
|
+
mime_type: string;
|
|
12
|
+
pay_to: string;
|
|
13
|
+
max_timeout_seconds: number;
|
|
14
|
+
asset: string;
|
|
15
|
+
extra?: {
|
|
16
|
+
feePayer?: string;
|
|
17
|
+
product?: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** Error thrown when payment is required to access a resource */
|
|
21
|
+
declare class PaymentRequiredError extends Error {
|
|
22
|
+
readonly paymentRequirements: PaymentRequirements[];
|
|
23
|
+
readonly raw: unknown;
|
|
24
|
+
constructor(message: string, paymentRequirements: PaymentRequirements[], raw: unknown);
|
|
25
|
+
}
|
|
26
|
+
/** Response from product access endpoint */
|
|
27
|
+
interface ProductAccessResponse {
|
|
28
|
+
object: 'product_access';
|
|
29
|
+
product_id: string;
|
|
30
|
+
access_granted: boolean;
|
|
31
|
+
message: string;
|
|
32
|
+
}
|
|
33
|
+
/** Parameters for accessing a product with x402 payment */
|
|
34
|
+
interface ProductAccessParams {
|
|
35
|
+
/** Base64-encoded X-Payment header value */
|
|
36
|
+
paymentHeader?: string;
|
|
37
|
+
}
|
|
1
38
|
interface Product {
|
|
2
39
|
id: string;
|
|
3
40
|
object: 'product';
|
|
@@ -7,6 +44,8 @@ interface Product {
|
|
|
7
44
|
metadata?: Record<string, string>;
|
|
8
45
|
created: number;
|
|
9
46
|
updated: number;
|
|
47
|
+
/** URL path for accessing this product (x402 gated) */
|
|
48
|
+
accessUrl: string;
|
|
10
49
|
}
|
|
11
50
|
interface ProductCreateParams {
|
|
12
51
|
name: string;
|
|
@@ -74,6 +113,32 @@ declare class ProductsAPI {
|
|
|
74
113
|
delete(id: string): Promise<{
|
|
75
114
|
deleted: boolean;
|
|
76
115
|
}>;
|
|
116
|
+
/**
|
|
117
|
+
* Access a product - gated by x402 payment
|
|
118
|
+
*
|
|
119
|
+
* This endpoint requires payment. If no payment header is provided (or payment is invalid),
|
|
120
|
+
* throws a PaymentRequiredError with the payment requirements.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* try {
|
|
125
|
+
* // First attempt without payment - will throw PaymentRequiredError
|
|
126
|
+
* const access = await moneymq.catalog.products.access('surfnet-max');
|
|
127
|
+
* } catch (error) {
|
|
128
|
+
* if (error instanceof PaymentRequiredError) {
|
|
129
|
+
* // Get payment requirements and create payment
|
|
130
|
+
* const requirements = error.paymentRequirements[0];
|
|
131
|
+
* const paymentHeader = await createPayment(requirements);
|
|
132
|
+
*
|
|
133
|
+
* // Retry with payment
|
|
134
|
+
* const access = await moneymq.catalog.products.access('surfnet-max', {
|
|
135
|
+
* paymentHeader,
|
|
136
|
+
* });
|
|
137
|
+
* }
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
access(id: string, params?: ProductAccessParams): Promise<ProductAccessResponse>;
|
|
77
142
|
}
|
|
78
143
|
declare class PricesAPI {
|
|
79
144
|
private config;
|
|
@@ -167,6 +232,60 @@ interface PaymentListParams {
|
|
|
167
232
|
limit?: number;
|
|
168
233
|
startingAfter?: string;
|
|
169
234
|
}
|
|
235
|
+
interface PayParams {
|
|
236
|
+
/** Amount in smallest currency unit (e.g., cents for USD) */
|
|
237
|
+
amount: number;
|
|
238
|
+
/** Currency code (e.g., 'usd', 'usdc') */
|
|
239
|
+
currency: string;
|
|
240
|
+
/** Product name for display */
|
|
241
|
+
productName: string;
|
|
242
|
+
/** Product ID for tracking */
|
|
243
|
+
productId?: string;
|
|
244
|
+
/** Optional product description */
|
|
245
|
+
description?: string;
|
|
246
|
+
/** Customer wallet address */
|
|
247
|
+
customer?: string;
|
|
248
|
+
/** Additional metadata */
|
|
249
|
+
metadata?: Record<string, string>;
|
|
250
|
+
}
|
|
251
|
+
interface PayResult {
|
|
252
|
+
/** Checkout session ID */
|
|
253
|
+
sessionId: string;
|
|
254
|
+
/** Payment intent ID */
|
|
255
|
+
paymentIntentId: string;
|
|
256
|
+
/** Client secret for confirming payment */
|
|
257
|
+
clientSecret: string;
|
|
258
|
+
/** Total amount in smallest currency unit */
|
|
259
|
+
amount: number;
|
|
260
|
+
/** Currency */
|
|
261
|
+
currency: string;
|
|
262
|
+
/** Status */
|
|
263
|
+
status: 'requires_confirmation' | 'succeeded' | 'failed';
|
|
264
|
+
}
|
|
265
|
+
interface PaymentIntent {
|
|
266
|
+
id: string;
|
|
267
|
+
object: 'payment_intent';
|
|
268
|
+
amount: number;
|
|
269
|
+
currency: string;
|
|
270
|
+
status: 'requires_payment_method' | 'requires_confirmation' | 'processing' | 'succeeded' | 'canceled';
|
|
271
|
+
customer?: string;
|
|
272
|
+
description?: string;
|
|
273
|
+
metadata: Record<string, string>;
|
|
274
|
+
clientSecret?: string;
|
|
275
|
+
created: number;
|
|
276
|
+
}
|
|
277
|
+
interface PaymentIntentCreateParams {
|
|
278
|
+
/** Amount in smallest currency unit */
|
|
279
|
+
amount: number;
|
|
280
|
+
/** Currency code */
|
|
281
|
+
currency: string;
|
|
282
|
+
/** Customer wallet address */
|
|
283
|
+
customer?: string;
|
|
284
|
+
/** Description */
|
|
285
|
+
description?: string;
|
|
286
|
+
/** Metadata including product info */
|
|
287
|
+
metadata?: Record<string, string>;
|
|
288
|
+
}
|
|
170
289
|
interface Customer {
|
|
171
290
|
id: string;
|
|
172
291
|
object: 'customer';
|
|
@@ -319,13 +438,41 @@ declare class WebhooksAPI {
|
|
|
319
438
|
success: boolean;
|
|
320
439
|
}>;
|
|
321
440
|
}
|
|
441
|
+
/**
|
|
442
|
+
* Payment Intents API - for direct payments without full checkout flow
|
|
443
|
+
* Similar to Stripe's Payment Intents API
|
|
444
|
+
*/
|
|
445
|
+
declare class PaymentIntentsAPI {
|
|
446
|
+
private request;
|
|
447
|
+
constructor(config: MoneyMQConfig);
|
|
448
|
+
/**
|
|
449
|
+
* Create a payment intent
|
|
450
|
+
* Use this for simple payments without the full checkout session flow
|
|
451
|
+
*/
|
|
452
|
+
create(params: PaymentIntentCreateParams): Promise<PaymentIntent>;
|
|
453
|
+
/**
|
|
454
|
+
* Retrieve a payment intent
|
|
455
|
+
*/
|
|
456
|
+
retrieve(id: string): Promise<PaymentIntent>;
|
|
457
|
+
/**
|
|
458
|
+
* Confirm a payment intent
|
|
459
|
+
* This triggers the actual payment (and x402 flow if required)
|
|
460
|
+
*/
|
|
461
|
+
confirm(id: string): Promise<PaymentIntent>;
|
|
462
|
+
/**
|
|
463
|
+
* Cancel a payment intent
|
|
464
|
+
*/
|
|
465
|
+
cancel(id: string): Promise<PaymentIntent>;
|
|
466
|
+
}
|
|
322
467
|
/**
|
|
323
468
|
* Payment API for checkout, links, customers, and payouts
|
|
324
469
|
*/
|
|
325
470
|
declare class PaymentAPI {
|
|
326
471
|
private request;
|
|
327
|
-
/** Checkout sessions API */
|
|
472
|
+
/** Checkout sessions API - for full e-commerce flows with line items */
|
|
328
473
|
readonly checkout: CheckoutAPI;
|
|
474
|
+
/** Payment intents API - for simpler direct payments */
|
|
475
|
+
readonly intents: PaymentIntentsAPI;
|
|
329
476
|
/** Payment links API */
|
|
330
477
|
readonly links: LinksAPI;
|
|
331
478
|
/** Customers API */
|
|
@@ -335,6 +482,21 @@ declare class PaymentAPI {
|
|
|
335
482
|
/** Webhooks API */
|
|
336
483
|
readonly webhooks: WebhooksAPI;
|
|
337
484
|
constructor(config: MoneyMQConfig);
|
|
485
|
+
/**
|
|
486
|
+
* Simple one-liner payment - creates a checkout session with inline product data
|
|
487
|
+
*
|
|
488
|
+
* @example
|
|
489
|
+
* ```ts
|
|
490
|
+
* const result = await moneymq.payment.pay({
|
|
491
|
+
* amount: 999,
|
|
492
|
+
* currency: 'usd',
|
|
493
|
+
* productName: 'Pro Plan',
|
|
494
|
+
* productId: 'pro-plan',
|
|
495
|
+
* customer: 'wallet_address',
|
|
496
|
+
* });
|
|
497
|
+
* ```
|
|
498
|
+
*/
|
|
499
|
+
pay(params: PayParams): Promise<PayResult>;
|
|
338
500
|
/**
|
|
339
501
|
* Retrieve a payment by ID
|
|
340
502
|
*/
|
|
@@ -348,6 +510,538 @@ declare class PaymentAPI {
|
|
|
348
510
|
}>;
|
|
349
511
|
}
|
|
350
512
|
|
|
513
|
+
/**
|
|
514
|
+
* MoneyMQ server configuration returned from /config endpoint
|
|
515
|
+
*/
|
|
516
|
+
interface ServerConfig {
|
|
517
|
+
account: {
|
|
518
|
+
name: string;
|
|
519
|
+
description: string;
|
|
520
|
+
};
|
|
521
|
+
x402: {
|
|
522
|
+
payoutAccount: {
|
|
523
|
+
currency: string;
|
|
524
|
+
decimals: number;
|
|
525
|
+
address: string;
|
|
526
|
+
tokenAddress: string;
|
|
527
|
+
};
|
|
528
|
+
facilitator: {
|
|
529
|
+
operatorAccount: {
|
|
530
|
+
out: string;
|
|
531
|
+
in: {
|
|
532
|
+
currency: string;
|
|
533
|
+
decimals: number;
|
|
534
|
+
address: string;
|
|
535
|
+
tokenAddress: string;
|
|
536
|
+
};
|
|
537
|
+
};
|
|
538
|
+
url: string;
|
|
539
|
+
};
|
|
540
|
+
validator: {
|
|
541
|
+
network: string;
|
|
542
|
+
rpcUrl: string;
|
|
543
|
+
bindHost: string;
|
|
544
|
+
rpcPort: number;
|
|
545
|
+
wsPort: number;
|
|
546
|
+
};
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Fetch server configuration from MoneyMQ API
|
|
551
|
+
*
|
|
552
|
+
* @param apiUrl - The MoneyMQ API URL
|
|
553
|
+
* @returns Server configuration including RPC URL
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* ```typescript
|
|
557
|
+
* const config = await fetchConfig('http://localhost:8488');
|
|
558
|
+
* console.log(config.x402.validator.rpcUrl);
|
|
559
|
+
* ```
|
|
560
|
+
*/
|
|
561
|
+
declare function fetchConfig(apiUrl: string): Promise<ServerConfig>;
|
|
562
|
+
/**
|
|
563
|
+
* Get the Solana RPC URL from server config
|
|
564
|
+
*
|
|
565
|
+
* @param apiUrl - The MoneyMQ API URL
|
|
566
|
+
* @param fallback - Fallback RPC URL if fetch fails
|
|
567
|
+
* @returns RPC URL string
|
|
568
|
+
*/
|
|
569
|
+
declare function getRpcUrl(apiUrl: string, fallback?: string): Promise<string>;
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Parameters for getting a signer by tag
|
|
573
|
+
*/
|
|
574
|
+
interface GetSignerParams {
|
|
575
|
+
/**
|
|
576
|
+
* Tag/label identifying the sandbox wallet account
|
|
577
|
+
* @example 'alice', 'bob', 'agent-1'
|
|
578
|
+
*/
|
|
579
|
+
tag: string;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* X402 configuration compatible with x402-fetch wrapFetchWithPayment
|
|
584
|
+
*/
|
|
585
|
+
interface X402ClientConfig {
|
|
586
|
+
svmConfig?: {
|
|
587
|
+
rpcUrl: string;
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* X402 API for agentic payments
|
|
592
|
+
*
|
|
593
|
+
* @example
|
|
594
|
+
* ```typescript
|
|
595
|
+
* import { wrapFetchWithPayment } from 'x402-fetch';
|
|
596
|
+
*
|
|
597
|
+
* const moneymq = new MoneyMQ({
|
|
598
|
+
* endpoint: 'http://localhost:8488',
|
|
599
|
+
* });
|
|
600
|
+
*
|
|
601
|
+
* // Get signer for sandbox account by label
|
|
602
|
+
* const payer = await moneymq.x402.getSigner({ tag: 'alice' });
|
|
603
|
+
*
|
|
604
|
+
* // Get x402 config for the fetch wrapper
|
|
605
|
+
* const config = await moneymq.x402.getConfig();
|
|
606
|
+
*
|
|
607
|
+
* // Create the payment-enabled fetch
|
|
608
|
+
* const fetchWithPayment = wrapFetchWithPayment(
|
|
609
|
+
* fetch,
|
|
610
|
+
* payer,
|
|
611
|
+
* undefined,
|
|
612
|
+
* undefined,
|
|
613
|
+
* config,
|
|
614
|
+
* );
|
|
615
|
+
*
|
|
616
|
+
* // Make requests that automatically handle 402 payments
|
|
617
|
+
* const response = await fetchWithPayment(url, { method: 'GET' });
|
|
618
|
+
* ```
|
|
619
|
+
*/
|
|
620
|
+
declare class X402API {
|
|
621
|
+
private config;
|
|
622
|
+
private serverConfig;
|
|
623
|
+
private sandboxAccounts;
|
|
624
|
+
constructor(config: MoneyMQConfig);
|
|
625
|
+
/**
|
|
626
|
+
* Fetch sandbox accounts from the server
|
|
627
|
+
*/
|
|
628
|
+
private fetchSandboxAccounts;
|
|
629
|
+
/**
|
|
630
|
+
* Get a signer for a sandbox account by tag/label
|
|
631
|
+
*
|
|
632
|
+
* @param params - Parameters containing the wallet tag/label
|
|
633
|
+
* @returns A Signer that can be used directly with wrapFetchWithPayment
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```typescript
|
|
637
|
+
* const payer = await moneymq.x402.getSigner({ tag: 'alice' });
|
|
638
|
+
* ```
|
|
639
|
+
*/
|
|
640
|
+
getSigner(params: GetSignerParams): Promise<Signer>;
|
|
641
|
+
/**
|
|
642
|
+
* Get x402 configuration for use with wrapFetchWithPayment
|
|
643
|
+
*
|
|
644
|
+
* @returns Configuration object compatible with x402-fetch
|
|
645
|
+
*
|
|
646
|
+
* @example
|
|
647
|
+
* ```typescript
|
|
648
|
+
* const config = await moneymq.x402.getConfig();
|
|
649
|
+
* const fetchWithPayment = wrapFetchWithPayment(fetch, payer, undefined, undefined, config);
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
652
|
+
getConfig(): Promise<X402ClientConfig>;
|
|
653
|
+
/**
|
|
654
|
+
* Get the full server configuration
|
|
655
|
+
*
|
|
656
|
+
* @returns The complete server configuration including x402 settings
|
|
657
|
+
*/
|
|
658
|
+
getServerConfig(): Promise<ServerConfig>;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* Server-Sent Events (SSE) helpers for MoneyMQ real-time events
|
|
663
|
+
*
|
|
664
|
+
* Supports two modes:
|
|
665
|
+
* - **Stateless**: Client tracks cursor, replay from memory on reconnect
|
|
666
|
+
* - **Stateful**: Server tracks cursor in DB, guaranteed delivery on reconnect
|
|
667
|
+
*
|
|
668
|
+
* @example Stateless stream (client-side cursor)
|
|
669
|
+
* ```typescript
|
|
670
|
+
* import { MoneyMQ } from '@moneymq/sdk';
|
|
671
|
+
*
|
|
672
|
+
* const moneymq = new MoneyMQ({ endpoint: 'http://localhost:8488' });
|
|
673
|
+
*
|
|
674
|
+
* // Create a stateless event stream with replay
|
|
675
|
+
* const stream = moneymq.events.stream({ last: 10 });
|
|
676
|
+
*
|
|
677
|
+
* stream.on('payment', (event) => {
|
|
678
|
+
* console.log('Payment event:', event.type, event.data);
|
|
679
|
+
* // Store cursor for reconnection
|
|
680
|
+
* localStorage.setItem('cursor', event.id);
|
|
681
|
+
* });
|
|
682
|
+
*
|
|
683
|
+
* stream.connect();
|
|
684
|
+
* ```
|
|
685
|
+
*
|
|
686
|
+
* @example Stateful stream (server-side cursor persistence)
|
|
687
|
+
* ```typescript
|
|
688
|
+
* import { MoneyMQ } from '@moneymq/sdk';
|
|
689
|
+
*
|
|
690
|
+
* const moneymq = new MoneyMQ({ endpoint: 'http://localhost:8488' });
|
|
691
|
+
*
|
|
692
|
+
* // Create a stateful stream - server tracks your position
|
|
693
|
+
* // Use a unique, deterministic ID for your consumer
|
|
694
|
+
* const stream = moneymq.events.stream({
|
|
695
|
+
* streamId: 'checkout-widget-user-123',
|
|
696
|
+
* });
|
|
697
|
+
*
|
|
698
|
+
* stream.on('payment', (event) => {
|
|
699
|
+
* // Server automatically tracks cursor - no need to store locally
|
|
700
|
+
* console.log('Payment event:', event.type, event.data);
|
|
701
|
+
* });
|
|
702
|
+
*
|
|
703
|
+
* stream.connect();
|
|
704
|
+
*
|
|
705
|
+
* // On reconnect, server replays missed events automatically
|
|
706
|
+
* ```
|
|
707
|
+
*/
|
|
708
|
+
/**
|
|
709
|
+
* CloudEvent v1.0 specification envelope
|
|
710
|
+
*/
|
|
711
|
+
interface CloudEventEnvelope<T = unknown> {
|
|
712
|
+
/** CloudEvents specification version */
|
|
713
|
+
specversion: string;
|
|
714
|
+
/** Unique event identifier (UUID) */
|
|
715
|
+
id: string;
|
|
716
|
+
/** Event type (e.g., 'mq.money.payment.verification.succeeded') */
|
|
717
|
+
type: string;
|
|
718
|
+
/** Event source URI */
|
|
719
|
+
source: string;
|
|
720
|
+
/** Timestamp in ISO 8601 format */
|
|
721
|
+
time: string;
|
|
722
|
+
/** Content type of the data field */
|
|
723
|
+
datacontenttype: string;
|
|
724
|
+
/** Event payload */
|
|
725
|
+
data: T;
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Payment flow type - indicates how the payment was initiated
|
|
729
|
+
*/
|
|
730
|
+
type PaymentFlow = {
|
|
731
|
+
type: 'x402';
|
|
732
|
+
} | {
|
|
733
|
+
type: 'checkout';
|
|
734
|
+
intent_id: string;
|
|
735
|
+
};
|
|
736
|
+
/**
|
|
737
|
+
* Payment verification succeeded event data
|
|
738
|
+
*/
|
|
739
|
+
interface PaymentVerificationSucceededData {
|
|
740
|
+
/** Wallet address of the payer */
|
|
741
|
+
payer: string;
|
|
742
|
+
/** Amount in smallest unit (e.g., lamports for SOL, micro-units for USDC) */
|
|
743
|
+
amount: string;
|
|
744
|
+
/** Network the payment was verified on */
|
|
745
|
+
network: string;
|
|
746
|
+
/** Product ID if available */
|
|
747
|
+
product_id: string | null;
|
|
748
|
+
/** Payment flow type (x402 or checkout) */
|
|
749
|
+
payment_flow: PaymentFlow;
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Payment verification failed event data
|
|
753
|
+
*/
|
|
754
|
+
interface PaymentVerificationFailedData {
|
|
755
|
+
/** Wallet address of the payer if known */
|
|
756
|
+
payer: string | null;
|
|
757
|
+
/** Amount that was attempted */
|
|
758
|
+
amount: string;
|
|
759
|
+
/** Network the verification was attempted on */
|
|
760
|
+
network: string;
|
|
761
|
+
/** Reason for the failure */
|
|
762
|
+
reason: string;
|
|
763
|
+
/** Product ID if available */
|
|
764
|
+
product_id: string | null;
|
|
765
|
+
/** Payment flow type (x402 or checkout) */
|
|
766
|
+
payment_flow: PaymentFlow;
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* Payment settlement succeeded event data
|
|
770
|
+
*/
|
|
771
|
+
interface PaymentSettlementSucceededData {
|
|
772
|
+
/** Wallet address of the payer */
|
|
773
|
+
payer: string;
|
|
774
|
+
/** Amount in smallest unit (e.g., lamports for SOL, micro-units for USDC) */
|
|
775
|
+
amount: string;
|
|
776
|
+
/** Network the transaction occurred on */
|
|
777
|
+
network: string;
|
|
778
|
+
/** Transaction signature/hash if available */
|
|
779
|
+
transaction_signature: string | null;
|
|
780
|
+
/** Product ID if available */
|
|
781
|
+
product_id: string | null;
|
|
782
|
+
/** Payment flow type (x402 or checkout) */
|
|
783
|
+
payment_flow: PaymentFlow;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Payment settlement failed event data
|
|
787
|
+
*/
|
|
788
|
+
interface PaymentSettlementFailedData {
|
|
789
|
+
/** Wallet address of the payer if known */
|
|
790
|
+
payer: string | null;
|
|
791
|
+
/** Amount that was attempted */
|
|
792
|
+
amount: string;
|
|
793
|
+
/** Network the settlement was attempted on */
|
|
794
|
+
network: string;
|
|
795
|
+
/** Reason for the failure */
|
|
796
|
+
reason: string;
|
|
797
|
+
/** Product ID if available */
|
|
798
|
+
product_id: string | null;
|
|
799
|
+
/** Payment flow type (x402 or checkout) */
|
|
800
|
+
payment_flow: PaymentFlow;
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* All MoneyMQ event types
|
|
804
|
+
*/
|
|
805
|
+
type MoneyMQEventType = 'mq.money.payment.verification.succeeded' | 'mq.money.payment.verification.failed' | 'mq.money.payment.settlement.succeeded' | 'mq.money.payment.settlement.failed';
|
|
806
|
+
/**
|
|
807
|
+
* Event type to data type mapping
|
|
808
|
+
*/
|
|
809
|
+
interface MoneyMQEventMap {
|
|
810
|
+
'mq.money.payment.verification.succeeded': PaymentVerificationSucceededData;
|
|
811
|
+
'mq.money.payment.verification.failed': PaymentVerificationFailedData;
|
|
812
|
+
'mq.money.payment.settlement.succeeded': PaymentSettlementSucceededData;
|
|
813
|
+
'mq.money.payment.settlement.failed': PaymentSettlementFailedData;
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Payment verification event (succeeded or failed)
|
|
817
|
+
*/
|
|
818
|
+
type PaymentVerificationEvent = CloudEventEnvelope<PaymentVerificationSucceededData> | CloudEventEnvelope<PaymentVerificationFailedData>;
|
|
819
|
+
/**
|
|
820
|
+
* Payment settlement event (succeeded or failed)
|
|
821
|
+
*/
|
|
822
|
+
type PaymentSettlementEvent = CloudEventEnvelope<PaymentSettlementSucceededData> | CloudEventEnvelope<PaymentSettlementFailedData>;
|
|
823
|
+
/**
|
|
824
|
+
* Any payment event
|
|
825
|
+
*/
|
|
826
|
+
type PaymentEvent = PaymentVerificationEvent | PaymentSettlementEvent;
|
|
827
|
+
/**
|
|
828
|
+
* Options for creating an event stream connection
|
|
829
|
+
*/
|
|
830
|
+
interface EventStreamOptions {
|
|
831
|
+
/**
|
|
832
|
+
* Replay the last N events before switching to live
|
|
833
|
+
* @example { last: 10 } - Replay last 10 events
|
|
834
|
+
*/
|
|
835
|
+
last?: number;
|
|
836
|
+
/**
|
|
837
|
+
* Resume from a specific event ID (exclusive)
|
|
838
|
+
* Events after this ID will be replayed, then live events
|
|
839
|
+
* @example { cursor: 'abc-123-def' }
|
|
840
|
+
*/
|
|
841
|
+
cursor?: string;
|
|
842
|
+
/**
|
|
843
|
+
* Stateful stream ID for server-side cursor persistence
|
|
844
|
+
*
|
|
845
|
+
* When provided, the server tracks the last consumed event for this stream.
|
|
846
|
+
* On reconnection, missed events are automatically replayed from where you left off.
|
|
847
|
+
*
|
|
848
|
+
* Use a deterministic ID unique to your consumer (e.g., 'checkout-widget-123').
|
|
849
|
+
*
|
|
850
|
+
* @example { streamId: 'my-checkout-widget' }
|
|
851
|
+
*/
|
|
852
|
+
streamId?: string;
|
|
853
|
+
/**
|
|
854
|
+
* Automatically reconnect on connection loss
|
|
855
|
+
* @default true
|
|
856
|
+
*/
|
|
857
|
+
autoReconnect?: boolean;
|
|
858
|
+
/**
|
|
859
|
+
* Delay between reconnection attempts in milliseconds
|
|
860
|
+
* @default 1000
|
|
861
|
+
*/
|
|
862
|
+
reconnectDelay?: number;
|
|
863
|
+
/**
|
|
864
|
+
* Maximum number of reconnection attempts (0 = infinite)
|
|
865
|
+
* @default 0
|
|
866
|
+
*/
|
|
867
|
+
maxReconnectAttempts?: number;
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Event stream connection state
|
|
871
|
+
*/
|
|
872
|
+
type EventStreamState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting';
|
|
873
|
+
/**
|
|
874
|
+
* Event handler callback type
|
|
875
|
+
*/
|
|
876
|
+
type EventHandler<T = PaymentEvent> = (event: T) => void;
|
|
877
|
+
/**
|
|
878
|
+
* Error handler callback type
|
|
879
|
+
*/
|
|
880
|
+
type ErrorHandler = (error: Error) => void;
|
|
881
|
+
/**
|
|
882
|
+
* State change handler callback type
|
|
883
|
+
*/
|
|
884
|
+
type StateHandler = (state: EventStreamState) => void;
|
|
885
|
+
/**
|
|
886
|
+
* MoneyMQ Event Stream client for receiving real-time purchase events
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* ```typescript
|
|
890
|
+
* const stream = new EventStream('http://localhost:8488', { last: 5 });
|
|
891
|
+
*
|
|
892
|
+
* stream.on('payment', (event) => {
|
|
893
|
+
* console.log('Payment event:', event.type);
|
|
894
|
+
* // Store cursor for reconnection
|
|
895
|
+
* localStorage.setItem('cursor', event.id);
|
|
896
|
+
* });
|
|
897
|
+
*
|
|
898
|
+
* stream.on('error', (error) => {
|
|
899
|
+
* console.error('Stream error:', error);
|
|
900
|
+
* });
|
|
901
|
+
*
|
|
902
|
+
* stream.on('stateChange', (state) => {
|
|
903
|
+
* console.log('Connection state:', state);
|
|
904
|
+
* });
|
|
905
|
+
*
|
|
906
|
+
* stream.connect();
|
|
907
|
+
*
|
|
908
|
+
* // Later: disconnect
|
|
909
|
+
* stream.disconnect();
|
|
910
|
+
* ```
|
|
911
|
+
*/
|
|
912
|
+
declare class EventStream {
|
|
913
|
+
private endpoint;
|
|
914
|
+
private options;
|
|
915
|
+
private eventSource;
|
|
916
|
+
private state;
|
|
917
|
+
private reconnectAttempts;
|
|
918
|
+
private lastEventId;
|
|
919
|
+
private paymentHandlers;
|
|
920
|
+
private errorHandlers;
|
|
921
|
+
private stateHandlers;
|
|
922
|
+
constructor(endpoint: string, options?: EventStreamOptions);
|
|
923
|
+
/**
|
|
924
|
+
* Whether this is a stateful stream (server tracks cursor)
|
|
925
|
+
*/
|
|
926
|
+
get isStateful(): boolean;
|
|
927
|
+
/**
|
|
928
|
+
* The stream ID for stateful streams
|
|
929
|
+
*/
|
|
930
|
+
get streamId(): string | null;
|
|
931
|
+
/**
|
|
932
|
+
* Current connection state
|
|
933
|
+
*/
|
|
934
|
+
get connectionState(): EventStreamState;
|
|
935
|
+
/**
|
|
936
|
+
* Current cursor (last received event ID)
|
|
937
|
+
*/
|
|
938
|
+
get cursor(): string | null;
|
|
939
|
+
/**
|
|
940
|
+
* Whether the stream is currently connected
|
|
941
|
+
*/
|
|
942
|
+
get isConnected(): boolean;
|
|
943
|
+
/**
|
|
944
|
+
* Register an event handler
|
|
945
|
+
*
|
|
946
|
+
* @param event - Event type: 'payment', 'error', or 'stateChange'
|
|
947
|
+
* @param handler - Callback function
|
|
948
|
+
* @returns Unsubscribe function
|
|
949
|
+
*/
|
|
950
|
+
on(event: 'payment', handler: EventHandler): () => void;
|
|
951
|
+
on(event: 'error', handler: ErrorHandler): () => void;
|
|
952
|
+
on(event: 'stateChange', handler: StateHandler): () => void;
|
|
953
|
+
/**
|
|
954
|
+
* Remove an event handler
|
|
955
|
+
*/
|
|
956
|
+
off(event: 'payment', handler: EventHandler): void;
|
|
957
|
+
off(event: 'error', handler: ErrorHandler): void;
|
|
958
|
+
off(event: 'stateChange', handler: StateHandler): void;
|
|
959
|
+
/**
|
|
960
|
+
* Connect to the event stream
|
|
961
|
+
*/
|
|
962
|
+
connect(): void;
|
|
963
|
+
/**
|
|
964
|
+
* Disconnect from the event stream
|
|
965
|
+
*/
|
|
966
|
+
disconnect(): void;
|
|
967
|
+
/**
|
|
968
|
+
* Reconnect with a new cursor
|
|
969
|
+
* Useful for resuming from a stored position
|
|
970
|
+
*/
|
|
971
|
+
reconnectFrom(cursor: string): void;
|
|
972
|
+
private buildUrl;
|
|
973
|
+
private setState;
|
|
974
|
+
private shouldReconnect;
|
|
975
|
+
private scheduleReconnect;
|
|
976
|
+
private emitPayment;
|
|
977
|
+
private emitError;
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Create an event stream connection
|
|
981
|
+
*
|
|
982
|
+
* @param endpoint - MoneyMQ API endpoint
|
|
983
|
+
* @param options - Stream options (cursor, replay count, etc.)
|
|
984
|
+
* @returns EventStream instance
|
|
985
|
+
*
|
|
986
|
+
* @example
|
|
987
|
+
* ```typescript
|
|
988
|
+
* // Live events only
|
|
989
|
+
* const stream = createEventStream('http://localhost:8488');
|
|
990
|
+
*
|
|
991
|
+
* // Replay last 10 events
|
|
992
|
+
* const stream = createEventStream('http://localhost:8488', { last: 10 });
|
|
993
|
+
*
|
|
994
|
+
* // Resume from cursor
|
|
995
|
+
* const cursor = localStorage.getItem('lastEventId');
|
|
996
|
+
* const stream = createEventStream('http://localhost:8488', { cursor });
|
|
997
|
+
* ```
|
|
998
|
+
*/
|
|
999
|
+
declare function createEventStream(endpoint: string, options?: EventStreamOptions): EventStream;
|
|
1000
|
+
/**
|
|
1001
|
+
* Check if an event is a payment verification succeeded event
|
|
1002
|
+
*/
|
|
1003
|
+
declare function isPaymentVerificationSucceeded(event: PaymentEvent): event is CloudEventEnvelope<PaymentVerificationSucceededData>;
|
|
1004
|
+
/**
|
|
1005
|
+
* Check if an event is a payment verification failed event
|
|
1006
|
+
*/
|
|
1007
|
+
declare function isPaymentVerificationFailed(event: PaymentEvent): event is CloudEventEnvelope<PaymentVerificationFailedData>;
|
|
1008
|
+
/**
|
|
1009
|
+
* Check if an event is a payment settlement succeeded event
|
|
1010
|
+
*/
|
|
1011
|
+
declare function isPaymentSettlementSucceeded(event: PaymentEvent): event is CloudEventEnvelope<PaymentSettlementSucceededData>;
|
|
1012
|
+
/**
|
|
1013
|
+
* Check if an event is a payment settlement failed event
|
|
1014
|
+
*/
|
|
1015
|
+
declare function isPaymentSettlementFailed(event: PaymentEvent): event is CloudEventEnvelope<PaymentSettlementFailedData>;
|
|
1016
|
+
/**
|
|
1017
|
+
* Parse a raw SSE data string into a CloudEvent
|
|
1018
|
+
*
|
|
1019
|
+
* @param data - Raw event data string from SSE
|
|
1020
|
+
* @returns Parsed CloudEvent or null if parsing fails
|
|
1021
|
+
*/
|
|
1022
|
+
declare function parseCloudEvent(data: string): PaymentEvent | null;
|
|
1023
|
+
/**
|
|
1024
|
+
* Build an event stream URL with query parameters
|
|
1025
|
+
*
|
|
1026
|
+
* @param endpoint - MoneyMQ API endpoint
|
|
1027
|
+
* @param options - Stream options
|
|
1028
|
+
* @returns Full URL string
|
|
1029
|
+
*
|
|
1030
|
+
* @example
|
|
1031
|
+
* ```typescript
|
|
1032
|
+
* const url = buildEventStreamUrl('http://localhost:8488', { last: 10 });
|
|
1033
|
+
* // Returns: 'http://localhost:8488/events?last=10'
|
|
1034
|
+
*
|
|
1035
|
+
* const url = buildEventStreamUrl('http://localhost:8488', { cursor: 'abc' });
|
|
1036
|
+
* // Returns: 'http://localhost:8488/events?cursor=abc'
|
|
1037
|
+
*
|
|
1038
|
+
* // Stateful stream (server tracks cursor):
|
|
1039
|
+
* const url = buildEventStreamUrl('http://localhost:8488', { streamId: 'my-stream' });
|
|
1040
|
+
* // Returns: 'http://localhost:8488/events?stream_id=my-stream'
|
|
1041
|
+
* ```
|
|
1042
|
+
*/
|
|
1043
|
+
declare function buildEventStreamUrl(endpoint: string, options?: Pick<EventStreamOptions, 'last' | 'cursor' | 'streamId'>): string;
|
|
1044
|
+
|
|
351
1045
|
/**
|
|
352
1046
|
* Configuration options for the MoneyMQ client
|
|
353
1047
|
*
|
|
@@ -408,6 +1102,28 @@ declare class MoneyMQ {
|
|
|
408
1102
|
readonly catalog: CatalogAPI;
|
|
409
1103
|
/** Payment API for checkout, links, customers, and payouts */
|
|
410
1104
|
readonly payment: PaymentAPI;
|
|
1105
|
+
/** X402 API for agentic payments */
|
|
1106
|
+
readonly x402: X402API;
|
|
1107
|
+
/** Events API for real-time SSE streams */
|
|
1108
|
+
readonly events: {
|
|
1109
|
+
/**
|
|
1110
|
+
* Create a new event stream connection
|
|
1111
|
+
*
|
|
1112
|
+
* @example
|
|
1113
|
+
* ```typescript
|
|
1114
|
+
* const stream = moneymq.events.stream({ last: 10 });
|
|
1115
|
+
*
|
|
1116
|
+
* stream.on('payment', (event) => {
|
|
1117
|
+
* console.log('Payment event:', event.type);
|
|
1118
|
+
* });
|
|
1119
|
+
*
|
|
1120
|
+
* stream.connect();
|
|
1121
|
+
* ```
|
|
1122
|
+
*/
|
|
1123
|
+
stream: (options?: EventStreamOptions) => EventStream;
|
|
1124
|
+
};
|
|
1125
|
+
/** MoneyMQ API endpoint */
|
|
1126
|
+
get endpoint(): string;
|
|
411
1127
|
constructor(config: MoneyMQConfig);
|
|
412
1128
|
/**
|
|
413
1129
|
* Make an authenticated request to the MoneyMQ API
|
|
@@ -415,62 +1131,4 @@ declare class MoneyMQ {
|
|
|
415
1131
|
request<T>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, body?: unknown): Promise<T>;
|
|
416
1132
|
}
|
|
417
1133
|
|
|
418
|
-
|
|
419
|
-
* MoneyMQ server configuration returned from /config endpoint
|
|
420
|
-
*/
|
|
421
|
-
interface ServerConfig {
|
|
422
|
-
account: {
|
|
423
|
-
name: string;
|
|
424
|
-
description: string;
|
|
425
|
-
};
|
|
426
|
-
x402: {
|
|
427
|
-
payoutAccount: {
|
|
428
|
-
currency: string;
|
|
429
|
-
decimals: number;
|
|
430
|
-
address: string;
|
|
431
|
-
tokenAddress: string;
|
|
432
|
-
};
|
|
433
|
-
facilitator: {
|
|
434
|
-
operatorAccount: {
|
|
435
|
-
out: string;
|
|
436
|
-
in: {
|
|
437
|
-
currency: string;
|
|
438
|
-
decimals: number;
|
|
439
|
-
address: string;
|
|
440
|
-
tokenAddress: string;
|
|
441
|
-
};
|
|
442
|
-
};
|
|
443
|
-
url: string;
|
|
444
|
-
};
|
|
445
|
-
validator: {
|
|
446
|
-
network: string;
|
|
447
|
-
rpcUrl: string;
|
|
448
|
-
bindHost: string;
|
|
449
|
-
rpcPort: number;
|
|
450
|
-
wsPort: number;
|
|
451
|
-
};
|
|
452
|
-
};
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Fetch server configuration from MoneyMQ API
|
|
456
|
-
*
|
|
457
|
-
* @param apiUrl - The MoneyMQ API URL
|
|
458
|
-
* @returns Server configuration including RPC URL
|
|
459
|
-
*
|
|
460
|
-
* @example
|
|
461
|
-
* ```typescript
|
|
462
|
-
* const config = await fetchConfig('http://localhost:8488');
|
|
463
|
-
* console.log(config.x402.validator.rpcUrl);
|
|
464
|
-
* ```
|
|
465
|
-
*/
|
|
466
|
-
declare function fetchConfig(apiUrl: string): Promise<ServerConfig>;
|
|
467
|
-
/**
|
|
468
|
-
* Get the Solana RPC URL from server config
|
|
469
|
-
*
|
|
470
|
-
* @param apiUrl - The MoneyMQ API URL
|
|
471
|
-
* @param fallback - Fallback RPC URL if fetch fails
|
|
472
|
-
* @returns RPC URL string
|
|
473
|
-
*/
|
|
474
|
-
declare function getRpcUrl(apiUrl: string, fallback?: string): Promise<string>;
|
|
475
|
-
|
|
476
|
-
export { type CheckoutCreateParams, type CheckoutSession, type Customer, type CustomerCreateParams, type CustomerUpdateParams, MoneyMQ, type MoneyMQConfig, type Payment, type PaymentLink, type PaymentLinkCreateParams, type PaymentListParams, type Payout, type PayoutCreateParams, type PayoutListParams, type PayoutSettings, type PayoutSettingsUpdateParams, type Price, type PriceCreateParams, type Product, type ProductCreateParams, type ProductListParams, type ServerConfig, fetchConfig, getRpcUrl };
|
|
1134
|
+
export { type CheckoutCreateParams, type CheckoutSession, type CloudEventEnvelope, type Customer, type CustomerCreateParams, type CustomerUpdateParams, type ErrorHandler, type EventHandler, EventStream, type EventStreamOptions, type EventStreamState, type GetSignerParams, MoneyMQ, type MoneyMQConfig, type MoneyMQEventMap, type MoneyMQEventType, type PayParams, type PayResult, type Payment, type PaymentEvent, type PaymentFlow, type PaymentIntent, type PaymentIntentCreateParams, type PaymentLink, type PaymentLinkCreateParams, type PaymentListParams, PaymentRequiredError, type PaymentRequirements, type PaymentSettlementEvent, type PaymentSettlementFailedData, type PaymentSettlementSucceededData, type PaymentVerificationEvent, type PaymentVerificationFailedData, type PaymentVerificationSucceededData, type Payout, type PayoutCreateParams, type PayoutListParams, type PayoutSettings, type PayoutSettingsUpdateParams, type Price, type PriceCreateParams, type Product, type ProductAccessParams, type ProductAccessResponse, type ProductCreateParams, type ProductListParams, type ServerConfig, type StateHandler, type X402ClientConfig, buildEventStreamUrl, createEventStream, fetchConfig, getRpcUrl, isPaymentSettlementFailed, isPaymentSettlementSucceeded, isPaymentVerificationFailed, isPaymentVerificationSucceeded, parseCloudEvent };
|