@mixrpay/merchant-sdk 0.1.1 → 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/README.md +186 -88
- package/dist/index.d.mts +210 -212
- package/dist/index.d.ts +210 -212
- package/dist/index.js +2 -782
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -764
- package/dist/index.mjs.map +1 -1
- package/dist/middleware/express.d.mts +5 -2
- package/dist/middleware/express.d.ts +5 -2
- package/dist/middleware/express.js +311 -0
- package/dist/middleware/express.js.map +1 -1
- package/dist/middleware/express.mjs +310 -0
- package/dist/middleware/express.mjs.map +1 -1
- package/dist/middleware/fastify.d.mts +1 -1
- package/dist/middleware/fastify.d.ts +1 -1
- package/dist/middleware/nextjs.d.mts +31 -2
- package/dist/middleware/nextjs.d.ts +31 -2
- package/dist/middleware/nextjs.js +296 -0
- package/dist/middleware/nextjs.js.map +1 -1
- package/dist/middleware/nextjs.mjs +295 -0
- package/dist/middleware/nextjs.mjs.map +1 -1
- package/dist/{types-BJNy6Hhb.d.mts → types-jelXkTp9.d.mts} +77 -61
- package/dist/{types-BJNy6Hhb.d.ts → types-jelXkTp9.d.ts} +77 -61
- package/package.json +4 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,49 +1,5 @@
|
|
|
1
|
-
import { V as
|
|
2
|
-
export {
|
|
3
|
-
export { default as expressX402 } from './middleware/express.mjs';
|
|
4
|
-
export { createPaymentRequired, withX402 } from './middleware/nextjs.mjs';
|
|
5
|
-
export { x402 as fastifyX402, x402Plugin } from './middleware/fastify.mjs';
|
|
6
|
-
import 'express';
|
|
7
|
-
import 'next/server';
|
|
8
|
-
import 'fastify';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* MixrPay Merchant SDK - Payment Verification
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Verify an X-PAYMENT header and optionally settle the payment.
|
|
16
|
-
*
|
|
17
|
-
* @param paymentHeader - The X-PAYMENT header value (base64 encoded JSON)
|
|
18
|
-
* @param options - Verification options
|
|
19
|
-
* @returns Payment verification result
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```typescript
|
|
23
|
-
* const result = await verifyX402Payment(req.headers['x-payment'], {
|
|
24
|
-
* expectedAmount: 50000n, // 0.05 USDC
|
|
25
|
-
* expectedRecipient: '0x...',
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* if (result.valid) {
|
|
29
|
-
* console.log(`Payment from ${result.payer}: $${result.amount}`);
|
|
30
|
-
* }
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
declare function verifyX402Payment(paymentHeader: string, options: VerifyOptions): Promise<X402PaymentResult>;
|
|
34
|
-
/**
|
|
35
|
-
* Parse and validate an X-PAYMENT header without settlement.
|
|
36
|
-
* Useful for checking if a payment is structurally valid before processing.
|
|
37
|
-
*/
|
|
38
|
-
declare function parseX402Payment(paymentHeader: string, chainId?: number): Promise<{
|
|
39
|
-
valid: boolean;
|
|
40
|
-
error?: string;
|
|
41
|
-
payer?: string;
|
|
42
|
-
recipient?: string;
|
|
43
|
-
amount?: number;
|
|
44
|
-
amountMinor?: bigint;
|
|
45
|
-
expiresAt?: Date;
|
|
46
|
-
}>;
|
|
1
|
+
import { V as VerifyReceiptOptions, P as PaymentReceipt } from './types-jelXkTp9.mjs';
|
|
2
|
+
export { M as MixrPayOptions, a as MixrPayPaymentResult, b as PaymentMethod, c as PriceContext } from './types-jelXkTp9.mjs';
|
|
47
3
|
|
|
48
4
|
/**
|
|
49
5
|
* MixrPay Merchant SDK - Payment Receipt Verification
|
|
@@ -88,115 +44,7 @@ declare function parseX402Payment(paymentHeader: string, chainId?: number): Prom
|
|
|
88
44
|
* ```
|
|
89
45
|
*/
|
|
90
46
|
declare function verifyPaymentReceipt(receipt: string, options?: VerifyReceiptOptions): Promise<PaymentReceipt>;
|
|
91
|
-
/**
|
|
92
|
-
* Parse a JWT payment receipt without verification.
|
|
93
|
-
*
|
|
94
|
-
* WARNING: This does NOT verify the signature. Use only for debugging
|
|
95
|
-
* or when you've already verified the receipt elsewhere.
|
|
96
|
-
*
|
|
97
|
-
* @param receipt - The JWT receipt string
|
|
98
|
-
* @returns Decoded payload (unverified)
|
|
99
|
-
*/
|
|
100
|
-
declare function parsePaymentReceipt(receipt: string): PaymentReceipt;
|
|
101
|
-
/**
|
|
102
|
-
* Check if a receipt is expired.
|
|
103
|
-
*
|
|
104
|
-
* @param receipt - The JWT receipt string or parsed PaymentReceipt
|
|
105
|
-
* @returns true if expired, false otherwise
|
|
106
|
-
*/
|
|
107
|
-
declare function isReceiptExpired(receipt: string | PaymentReceipt): boolean;
|
|
108
|
-
/**
|
|
109
|
-
* Clear the JWKS cache.
|
|
110
|
-
* Useful for testing or when keys have rotated.
|
|
111
|
-
*/
|
|
112
|
-
declare function clearJWKSCache(): void;
|
|
113
|
-
/**
|
|
114
|
-
* Get the default JWKS URL.
|
|
115
|
-
*/
|
|
116
|
-
declare function getDefaultJWKSUrl(): string;
|
|
117
47
|
|
|
118
|
-
/**
|
|
119
|
-
* MixrPay Merchant SDK - Utilities
|
|
120
|
-
*/
|
|
121
|
-
|
|
122
|
-
declare const USDC_CONTRACTS: Record<number, `0x${string}`>;
|
|
123
|
-
declare const DEFAULT_FACILITATOR = "https://x402.org/facilitator";
|
|
124
|
-
/**
|
|
125
|
-
* Convert USD dollars to USDC minor units (6 decimals)
|
|
126
|
-
*/
|
|
127
|
-
declare function usdToMinor(usd: number): bigint;
|
|
128
|
-
/**
|
|
129
|
-
* Convert USDC minor units to USD dollars
|
|
130
|
-
*/
|
|
131
|
-
declare function minorToUsd(minor: bigint): number;
|
|
132
|
-
/**
|
|
133
|
-
* Generate a random nonce for x402 payments
|
|
134
|
-
*/
|
|
135
|
-
declare function generateNonce(): `0x${string}`;
|
|
136
|
-
/**
|
|
137
|
-
* Check if an address is valid
|
|
138
|
-
*/
|
|
139
|
-
declare function isValidAddress(address: string): address is `0x${string}`;
|
|
140
|
-
/**
|
|
141
|
-
* Normalize an address to lowercase checksum format
|
|
142
|
-
*/
|
|
143
|
-
declare function normalizeAddress(address: string): `0x${string}`;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* MixrPay Charges Module
|
|
147
|
-
*
|
|
148
|
-
* Create and manage charges using session signers.
|
|
149
|
-
* Session signers allow you to charge user wallets without
|
|
150
|
-
* requiring approval for each transaction.
|
|
151
|
-
*/
|
|
152
|
-
interface CreateChargeParams {
|
|
153
|
-
/** Session key ID granted by the user */
|
|
154
|
-
sessionId: string;
|
|
155
|
-
/** Amount to charge in USD (e.g., 0.05 for 5 cents) */
|
|
156
|
-
amountUsd: number;
|
|
157
|
-
/** Unique reference for idempotency (e.g., "order_123") */
|
|
158
|
-
reference: string;
|
|
159
|
-
/** Optional metadata */
|
|
160
|
-
metadata?: Record<string, unknown>;
|
|
161
|
-
}
|
|
162
|
-
interface Charge {
|
|
163
|
-
/** Unique charge ID */
|
|
164
|
-
id: string;
|
|
165
|
-
/** Current status */
|
|
166
|
-
status: 'pending' | 'submitted' | 'confirmed' | 'failed';
|
|
167
|
-
/** Amount in USD */
|
|
168
|
-
amountUsd: number;
|
|
169
|
-
/** Formatted USDC amount */
|
|
170
|
-
amountUsdc: string;
|
|
171
|
-
/** Transaction hash (if submitted) */
|
|
172
|
-
txHash?: string;
|
|
173
|
-
/** Block explorer URL */
|
|
174
|
-
explorerUrl?: string;
|
|
175
|
-
/** From wallet address */
|
|
176
|
-
fromAddress: string;
|
|
177
|
-
/** To wallet address */
|
|
178
|
-
toAddress: string;
|
|
179
|
-
/** Your reference */
|
|
180
|
-
reference: string;
|
|
181
|
-
/** When the charge was created */
|
|
182
|
-
createdAt: string;
|
|
183
|
-
/** When the charge was confirmed */
|
|
184
|
-
confirmedAt?: string;
|
|
185
|
-
/** Session name */
|
|
186
|
-
sessionName?: string;
|
|
187
|
-
/** User wallet address */
|
|
188
|
-
userWallet?: string;
|
|
189
|
-
/** Error message if failed */
|
|
190
|
-
error?: string;
|
|
191
|
-
}
|
|
192
|
-
interface CreateChargeResult {
|
|
193
|
-
success: boolean;
|
|
194
|
-
charge?: Charge;
|
|
195
|
-
/** True if this is a replay of an existing charge */
|
|
196
|
-
idempotentReplay?: boolean;
|
|
197
|
-
error?: string;
|
|
198
|
-
details?: string;
|
|
199
|
-
}
|
|
200
48
|
interface SessionGrant {
|
|
201
49
|
/** Session key ID */
|
|
202
50
|
sessionId: string;
|
|
@@ -213,50 +61,6 @@ interface SessionGrant {
|
|
|
213
61
|
/** When the session was granted */
|
|
214
62
|
grantedAt: string;
|
|
215
63
|
}
|
|
216
|
-
interface ChargesClientOptions {
|
|
217
|
-
/** Your API key */
|
|
218
|
-
apiKey: string;
|
|
219
|
-
/** MixrPay API base URL */
|
|
220
|
-
baseUrl?: string;
|
|
221
|
-
}
|
|
222
|
-
declare class ChargesClient {
|
|
223
|
-
private apiKey;
|
|
224
|
-
private baseUrl;
|
|
225
|
-
constructor(options: ChargesClientOptions);
|
|
226
|
-
/**
|
|
227
|
-
* Create a new charge.
|
|
228
|
-
*
|
|
229
|
-
* @example
|
|
230
|
-
* ```typescript
|
|
231
|
-
* const result = await charges.create({
|
|
232
|
-
* sessionId: 'sk_xxx',
|
|
233
|
-
* amountUsd: 0.05,
|
|
234
|
-
* reference: 'image_gen_123',
|
|
235
|
-
* metadata: { feature: 'image_generation' }
|
|
236
|
-
* });
|
|
237
|
-
*
|
|
238
|
-
* if (result.success) {
|
|
239
|
-
* console.log(`Charged ${result.charge.amountUsdc}`);
|
|
240
|
-
* console.log(`TX: ${result.charge.explorerUrl}`);
|
|
241
|
-
* }
|
|
242
|
-
* ```
|
|
243
|
-
*/
|
|
244
|
-
create(params: CreateChargeParams): Promise<CreateChargeResult>;
|
|
245
|
-
/**
|
|
246
|
-
* Get a charge by ID.
|
|
247
|
-
*
|
|
248
|
-
* @example
|
|
249
|
-
* ```typescript
|
|
250
|
-
* const charge = await charges.get('chg_xxx');
|
|
251
|
-
* console.log(charge.status); // 'confirmed'
|
|
252
|
-
* ```
|
|
253
|
-
*/
|
|
254
|
-
get(chargeId: string): Promise<Charge | null>;
|
|
255
|
-
/**
|
|
256
|
-
* Get a charge by transaction hash.
|
|
257
|
-
*/
|
|
258
|
-
getByTxHash(txHash: string): Promise<Charge | null>;
|
|
259
|
-
}
|
|
260
64
|
/**
|
|
261
65
|
* Verify a session.granted webhook signature.
|
|
262
66
|
*
|
|
@@ -272,20 +76,214 @@ declare class ChargesClient {
|
|
|
272
76
|
* ```
|
|
273
77
|
*/
|
|
274
78
|
declare function verifySessionWebhook(payload: string, signature: string, secret: string): boolean;
|
|
79
|
+
|
|
275
80
|
/**
|
|
276
|
-
*
|
|
81
|
+
* MixrPay Widget Custom Element Type Declarations
|
|
82
|
+
*
|
|
83
|
+
* These types enable TypeScript support for MixrPay widget custom elements
|
|
84
|
+
* in JSX/TSX files.
|
|
85
|
+
*
|
|
86
|
+
* Usage in your app:
|
|
87
|
+
* ```typescript
|
|
88
|
+
* // Option 1: Import for side effects (recommended)
|
|
89
|
+
* import '@mixrpay/merchant-sdk/widget-elements';
|
|
90
|
+
*
|
|
91
|
+
* // Option 2: Reference directly
|
|
92
|
+
* /// <reference types="@mixrpay/merchant-sdk/widget-elements" />
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* Now these work without TypeScript errors:
|
|
96
|
+
* ```tsx
|
|
97
|
+
* <mixr-balance data-theme="dark" />
|
|
98
|
+
* <mixr-wallet />
|
|
99
|
+
* ```
|
|
277
100
|
*/
|
|
278
|
-
declare function parseSessionGrant(payload: {
|
|
279
|
-
event: string;
|
|
280
|
-
session_id: string;
|
|
281
|
-
user_wallet: string;
|
|
282
|
-
merchant_wallet: string;
|
|
283
|
-
limits: {
|
|
284
|
-
max_total_usd?: number;
|
|
285
|
-
max_per_tx_usd?: number;
|
|
286
|
-
expires_at: string;
|
|
287
|
-
};
|
|
288
|
-
granted_at: string;
|
|
289
|
-
}): SessionGrant;
|
|
290
101
|
|
|
291
|
-
|
|
102
|
+
// =============================================================================
|
|
103
|
+
// Widget Attribute Types
|
|
104
|
+
// =============================================================================
|
|
105
|
+
|
|
106
|
+
interface MixrWidgetAttributes {
|
|
107
|
+
/** Theme for the widget - 'dark' or 'light' */
|
|
108
|
+
'data-theme'?: 'dark' | 'light';
|
|
109
|
+
/** Additional class names */
|
|
110
|
+
class?: string;
|
|
111
|
+
className?: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface MixrBalanceAttributes extends MixrWidgetAttributes {
|
|
115
|
+
// Balance-specific attributes (currently none beyond base)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
interface MixrWalletAttributes extends MixrWidgetAttributes {
|
|
119
|
+
// Wallet-specific attributes (currently none beyond base)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// =============================================================================
|
|
123
|
+
// Global State Types
|
|
124
|
+
// =============================================================================
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* State object returned by window.Mixr.getState()
|
|
128
|
+
*/
|
|
129
|
+
interface MixrState {
|
|
130
|
+
/** User's balance in minor units (bigint) */
|
|
131
|
+
balance: bigint;
|
|
132
|
+
/** User's balance in USD */
|
|
133
|
+
balanceUsd: number;
|
|
134
|
+
/** Currency code (e.g., 'USD') */
|
|
135
|
+
currency: string;
|
|
136
|
+
/** Whether the user is authenticated */
|
|
137
|
+
isAuthenticated: boolean;
|
|
138
|
+
/** Whether the user has completed onboarding */
|
|
139
|
+
isOnboarded: boolean;
|
|
140
|
+
/** User's wallet address (null if not authenticated) */
|
|
141
|
+
walletAddress: string | null;
|
|
142
|
+
/** Whether the widget is loading */
|
|
143
|
+
loading: boolean;
|
|
144
|
+
/** Error message if any */
|
|
145
|
+
error: string | null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// =============================================================================
|
|
149
|
+
// Global API Types
|
|
150
|
+
// =============================================================================
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* MixrPay global API available on window.Mixr
|
|
154
|
+
*/
|
|
155
|
+
interface MixrAPI {
|
|
156
|
+
/** Initialize the widget (called automatically on load) */
|
|
157
|
+
init(): void;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Open the wallet popup
|
|
161
|
+
* @param tab - Optional tab to open: 'add_funds', 'send', 'receive', 'sessions'
|
|
162
|
+
*/
|
|
163
|
+
openWallet(tab?: 'add_funds' | 'send' | 'receive' | 'sessions'): void;
|
|
164
|
+
|
|
165
|
+
/** Get current widget state */
|
|
166
|
+
getState(): MixrState;
|
|
167
|
+
|
|
168
|
+
/** Refresh user balance and state */
|
|
169
|
+
refresh(): Promise<void>;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Programmatically charge a user
|
|
173
|
+
* @param feature - Feature slug to charge for
|
|
174
|
+
* @param priceUsd - Price in USD as string (e.g., "0.25")
|
|
175
|
+
* @param element - The element that triggered the charge (for UI feedback)
|
|
176
|
+
*/
|
|
177
|
+
charge(feature: string, priceUsd: string, element: HTMLElement): Promise<void>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// =============================================================================
|
|
181
|
+
// Event Detail Types
|
|
182
|
+
// =============================================================================
|
|
183
|
+
|
|
184
|
+
interface MixrChargeSuccessDetail {
|
|
185
|
+
/** The feature that was charged */
|
|
186
|
+
feature: string;
|
|
187
|
+
/** The price that was charged */
|
|
188
|
+
price: string;
|
|
189
|
+
/** Unique charge ID */
|
|
190
|
+
chargeId?: string;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
interface MixrChargeErrorDetail {
|
|
194
|
+
/** Error message */
|
|
195
|
+
error: string;
|
|
196
|
+
/** The feature that failed to charge */
|
|
197
|
+
feature?: string;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface MixrLowBalanceDetail {
|
|
201
|
+
/** Current balance */
|
|
202
|
+
balance: number;
|
|
203
|
+
/** Required amount */
|
|
204
|
+
required: number;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
interface MixrStateUpdatedDetail extends MixrState {}
|
|
208
|
+
|
|
209
|
+
interface MixrFundingDetail {
|
|
210
|
+
/** Amount funded */
|
|
211
|
+
amount: number;
|
|
212
|
+
/** Transaction hash if available */
|
|
213
|
+
txHash?: string;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
interface MixrTransactionDetail {
|
|
217
|
+
/** Transaction hash */
|
|
218
|
+
txHash: string;
|
|
219
|
+
/** Amount transferred */
|
|
220
|
+
amount: number;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// =============================================================================
|
|
224
|
+
// Custom Events Map
|
|
225
|
+
// =============================================================================
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* MixrPay custom events that can be listened to via addEventListener
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* window.addEventListener('mixr:charge-success', (e) => {
|
|
233
|
+
* console.log('Charged:', e.detail.feature, e.detail.price);
|
|
234
|
+
* });
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
interface MixrEventMap {
|
|
238
|
+
'mixr:charge-success': CustomEvent<MixrChargeSuccessDetail>;
|
|
239
|
+
'mixr:charge-error': CustomEvent<MixrChargeErrorDetail>;
|
|
240
|
+
'mixr:low-balance': CustomEvent<MixrLowBalanceDetail>;
|
|
241
|
+
'mixr:state-updated': CustomEvent<MixrStateUpdatedDetail>;
|
|
242
|
+
'mixr:funding': CustomEvent<MixrFundingDetail>;
|
|
243
|
+
'mixr:transaction': CustomEvent<MixrTransactionDetail>;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// =============================================================================
|
|
247
|
+
// Global Augmentations
|
|
248
|
+
// =============================================================================
|
|
249
|
+
|
|
250
|
+
declare global {
|
|
251
|
+
interface Window {
|
|
252
|
+
/** MixrPay global API */
|
|
253
|
+
Mixr: MixrAPI;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
interface WindowEventMap extends MixrEventMap {}
|
|
257
|
+
|
|
258
|
+
namespace JSX {
|
|
259
|
+
interface IntrinsicElements {
|
|
260
|
+
/**
|
|
261
|
+
* MixrPay balance display component
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```tsx
|
|
265
|
+
* <mixr-balance data-theme="dark" />
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
'mixr-balance': React.DetailedHTMLProps<
|
|
269
|
+
React.HTMLAttributes<HTMLElement> & MixrBalanceAttributes,
|
|
270
|
+
HTMLElement
|
|
271
|
+
>;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* MixrPay wallet widget component
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```tsx
|
|
278
|
+
* <mixr-wallet data-theme="dark" />
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
'mixr-wallet': React.DetailedHTMLProps<
|
|
282
|
+
React.HTMLAttributes<HTMLElement> & MixrWalletAttributes,
|
|
283
|
+
HTMLElement
|
|
284
|
+
>;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export { PaymentReceipt, type SessionGrant, verifyPaymentReceipt, verifySessionWebhook };
|