@beep-it/sdk-core 0.1.1 → 0.1.2-beta.2
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 +228 -210
- package/dist/index.d.ts +111 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +348 -10
- package/dist/index.mjs +347 -10
- package/dist/modules/payments.d.ts +197 -11
- package/dist/modules/payments.d.ts.map +1 -1
- package/dist/modules/widget.d.ts +31 -0
- package/dist/modules/widget.d.ts.map +1 -0
- package/dist/types/payment.d.ts +211 -0
- package/dist/types/payment.d.ts.map +1 -1
- package/dist/types/public.d.ts +34 -0
- package/dist/types/public.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { InvoicesModule } from './modules/invoices';
|
|
6
6
|
import { PaymentsModule } from './modules/payments';
|
|
7
7
|
import { ProductsModule } from './modules/products';
|
|
8
|
+
import { WidgetModule } from './modules/widget';
|
|
8
9
|
/**
|
|
9
10
|
* Configuration options for initializing the BeepClient
|
|
10
11
|
*/
|
|
@@ -18,14 +19,24 @@ export interface BeepClientOptions {
|
|
|
18
19
|
serverUrl?: string;
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
|
-
* The main BEEP SDK client for
|
|
22
|
+
* The main BEEP SDK client for server-side applications using secret API keys
|
|
23
|
+
*
|
|
24
|
+
* **Use this client for:**
|
|
25
|
+
* - Server-side applications (Node.js, Express, Next.js API routes)
|
|
26
|
+
* - Backend services that can safely store secret API keys
|
|
27
|
+
* - Full payment processing capabilities including streaming payments
|
|
28
|
+
* - Administrative operations like product and invoice management
|
|
29
|
+
*
|
|
30
|
+
* **Security Note:** Never use BeepClient in client-side code (browsers) as it
|
|
31
|
+
* requires secret API keys that should not be exposed publicly.
|
|
22
32
|
*
|
|
23
33
|
* @example
|
|
24
34
|
* ```typescript
|
|
25
35
|
* import { BeepClient, SupportedToken } from '@beep-it/sdk-core';
|
|
26
36
|
*
|
|
37
|
+
* // Only use this server-side with secret API keys
|
|
27
38
|
* const beep = new BeepClient({
|
|
28
|
-
* apiKey: '
|
|
39
|
+
* apiKey: 'your_secret_api_key_here' // Keep this secure!
|
|
29
40
|
* });
|
|
30
41
|
*
|
|
31
42
|
* // Create a payment request
|
|
@@ -34,15 +45,27 @@ export interface BeepClientOptions {
|
|
|
34
45
|
* token: SupportedToken.USDT,
|
|
35
46
|
* description: 'Premium subscription'
|
|
36
47
|
* });
|
|
48
|
+
*
|
|
49
|
+
* // Issue streaming payments (BeepClient only)
|
|
50
|
+
* const streamingSession = await beep.payments.issuePayment({
|
|
51
|
+
* apiKey: 'your_secret_api_key',
|
|
52
|
+
* assetChunks: [{ assetId: 'video-content', quantity: 1 }],
|
|
53
|
+
* payingMerchantId: 'merchant_id'
|
|
54
|
+
* });
|
|
37
55
|
* ```
|
|
38
56
|
*/
|
|
39
57
|
export declare class BeepClient {
|
|
40
58
|
private client;
|
|
41
|
-
/** Access to product management functionality */
|
|
59
|
+
/** Access to product management functionality (server-side only) */
|
|
42
60
|
readonly products: ProductsModule;
|
|
43
|
-
/** Access to invoice management functionality */
|
|
61
|
+
/** Access to invoice management functionality (server-side only) */
|
|
44
62
|
readonly invoices: InvoicesModule;
|
|
45
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* Access to payment processing functionality including streaming payments
|
|
65
|
+
*
|
|
66
|
+
* **Note:** Streaming payment methods (issuePayment, startStreaming, pauseStreaming,
|
|
67
|
+
* stopStreaming) are only available with BeepClient and secret API keys.
|
|
68
|
+
*/
|
|
46
69
|
readonly payments: PaymentsModule;
|
|
47
70
|
/**
|
|
48
71
|
* Creates a new BEEP client instance
|
|
@@ -51,6 +74,23 @@ export declare class BeepClient {
|
|
|
51
74
|
* @throws {Error} When API key is missing or invalid
|
|
52
75
|
*/
|
|
53
76
|
constructor(options: BeepClientOptions);
|
|
77
|
+
/**
|
|
78
|
+
* Initiate a payout from your treasury wallet to an external address.
|
|
79
|
+
* Requires a secret API key (server-side only).
|
|
80
|
+
*
|
|
81
|
+
* Notes:
|
|
82
|
+
* - Do not pass walletId. The server derives the wallet based on your API key's merchant and requested chain.
|
|
83
|
+
* - amount must be in smallest units for the token (e.g., 6‑decimals USDC amount as an integer string).
|
|
84
|
+
* - This endpoint responds immediately with acceptance/rejection. Actual transfer executes asynchronously after funds are reserved.
|
|
85
|
+
*
|
|
86
|
+
* Example:
|
|
87
|
+
* const res = await beep.payments.createPayout({
|
|
88
|
+
* amount: '1000000', // 1.0 USDC with 6 decimals
|
|
89
|
+
* destinationWalletAddress: 'DEST_ADDRESS',
|
|
90
|
+
* chain: 'SOLANA',
|
|
91
|
+
* token: 'USDC',
|
|
92
|
+
* });
|
|
93
|
+
*/
|
|
54
94
|
/**
|
|
55
95
|
* Checks the health status of the BEEP API server
|
|
56
96
|
*
|
|
@@ -70,4 +110,70 @@ export type { CreateCustomInvoicePayload, CreateInvoiceFromProductPayload, Creat
|
|
|
70
110
|
export type { CreateProductPayload, Product, UpdateProductPayload } from './types/product';
|
|
71
111
|
export { SupportedToken, TokenUtils } from './types/token';
|
|
72
112
|
export type { BeepResponse } from './types';
|
|
113
|
+
export interface BeepPublicClientOptions {
|
|
114
|
+
/** Publishable key for browser access; safe to embed in clients */
|
|
115
|
+
publishableKey: string;
|
|
116
|
+
/** Optional server URL override */
|
|
117
|
+
serverUrl?: string;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Browser-safe BEEP client for frontend applications using publishable keys
|
|
121
|
+
*
|
|
122
|
+
* **Use this client for:**
|
|
123
|
+
* - Frontend applications (React, Vue, vanilla JavaScript)
|
|
124
|
+
* - Browser-based code where secret keys cannot be safely stored
|
|
125
|
+
* - Widget-based payment sessions with ephemeral items
|
|
126
|
+
* - Client-side payment status polling
|
|
127
|
+
*
|
|
128
|
+
* **Limitations compared to BeepClient:**
|
|
129
|
+
* - Cannot access streaming payment methods (issuePayment, startStreaming, etc.)
|
|
130
|
+
* - Cannot manage products or invoices directly
|
|
131
|
+
* - Limited to public widget endpoints only
|
|
132
|
+
* - No access to administrative functions
|
|
133
|
+
*
|
|
134
|
+
* **Security:** Uses publishable keys which are safe to expose in client-side code.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* import { BeepPublicClient } from '@beep-it/sdk-core';
|
|
139
|
+
*
|
|
140
|
+
* // Safe to use in browsers with publishable keys
|
|
141
|
+
* const publicBeep = new BeepPublicClient({
|
|
142
|
+
* publishableKey: 'beep_pk_your_publishable_key_here' // Safe to expose
|
|
143
|
+
* });
|
|
144
|
+
*
|
|
145
|
+
* // Create payment sessions with mixed assets
|
|
146
|
+
* const session = await publicBeep.widget.createPaymentSession({
|
|
147
|
+
* assets: [
|
|
148
|
+
* { assetId: 'existing-product-uuid', quantity: 1 },
|
|
149
|
+
* { name: 'Custom Item', price: '12.50', quantity: 1 }
|
|
150
|
+
* ],
|
|
151
|
+
* paymentLabel: 'My Store'
|
|
152
|
+
* });
|
|
153
|
+
*
|
|
154
|
+
* // Poll for payment completion
|
|
155
|
+
* const { paid } = await publicBeep.widget.waitForPaid({
|
|
156
|
+
* referenceKey: session.referenceKey
|
|
157
|
+
* });
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export declare class BeepPublicClient {
|
|
161
|
+
private client;
|
|
162
|
+
/**
|
|
163
|
+
* Access to public widget endpoints for payment sessions
|
|
164
|
+
*
|
|
165
|
+
* **Note:** This is the only module available in BeepPublicClient.
|
|
166
|
+
* Streaming payments, product management, and administrative functions
|
|
167
|
+
* are only available in BeepClient with secret API keys.
|
|
168
|
+
*/
|
|
169
|
+
readonly widget: WidgetModule;
|
|
170
|
+
/**
|
|
171
|
+
* Creates a new BEEP public client instance for browser use
|
|
172
|
+
*
|
|
173
|
+
* @param options - Configuration options including publishable key
|
|
174
|
+
* @throws {Error} When publishable key is missing or invalid
|
|
175
|
+
*/
|
|
176
|
+
constructor(options: BeepPublicClientOptions);
|
|
177
|
+
}
|
|
178
|
+
export type { PublicPaymentSessionRequest, PublicPaymentSessionResponse, PublicPaymentStatusResponse, EphemeralItem, } from './types/public';
|
|
73
179
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,mFAAmF;IACnF,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAgB;IAE9B,oEAAoE;IACpE,SAAgB,QAAQ,EAAE,cAAc,CAAC;IAEzC,oEAAoE;IACpE,SAAgB,QAAQ,EAAE,cAAc,CAAC;IAEzC;;;;;OAKG;IACH,SAAgB,QAAQ,EAAE,cAAc,CAAC;IAEzC;;;;;OAKG;gBACS,OAAO,EAAE,iBAAiB;IAmBtC;;;;;;;;;;;;;;;;OAgBG;IAGH;;;;;;;;;;;OAWG;IACU,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;CAW5C;AAOD,YAAY,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAGpG,YAAY,EACV,0BAA0B,EAC1B,+BAA+B,EAC/B,oBAAoB,EACpB,OAAO,EACP,aAAa,EACb,SAAS,GACV,MAAM,iBAAiB,CAAC;AAGzB,YAAY,EAAE,oBAAoB,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAG3F,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAG3D,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAM5C,MAAM,WAAW,uBAAuB;IACtC,mEAAmE;IACnE,cAAc,EAAE,MAAM,CAAC;IACvB,mCAAmC;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAgB;IAE9B;;;;;;OAMG;IACH,SAAgB,MAAM,EAAE,YAAY,CAAC;IAErC;;;;;OAKG;gBACS,OAAO,EAAE,uBAAuB;CAe7C;AAED,YAAY,EACV,2BAA2B,EAC3B,4BAA4B,EAC5B,2BAA2B,EAC3B,aAAa,GACd,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
BeepClient: () => BeepClient,
|
|
34
|
+
BeepPublicClient: () => BeepPublicClient,
|
|
34
35
|
SupportedToken: () => SupportedToken,
|
|
35
36
|
TokenUtils: () => TokenUtils
|
|
36
37
|
});
|
|
@@ -236,22 +237,116 @@ var PaymentsModule = class {
|
|
|
236
237
|
this.client = client;
|
|
237
238
|
}
|
|
238
239
|
/**
|
|
239
|
-
*
|
|
240
|
+
* Initiate a payout from your treasury wallet to an external address.
|
|
241
|
+
* Requires a secret API key (server-side only).
|
|
242
|
+
*
|
|
243
|
+
* Notes:
|
|
244
|
+
* - Do not pass walletId. The server derives the wallet based on your API key's merchant and requested chain.
|
|
245
|
+
* - amount must be in smallest units for the token (e.g., 6‑decimals USDC amount as an integer string).
|
|
246
|
+
* - This endpoint responds immediately with acceptance/rejection. Actual transfer executes asynchronously after funds are reserved.
|
|
247
|
+
*
|
|
248
|
+
* Example:
|
|
249
|
+
* const res = await beep.payments.createPayout({
|
|
250
|
+
* amount: '1000000', // 1.0 USDC with 6 decimals
|
|
251
|
+
* destinationWalletAddress: 'DEST_ADDRESS',
|
|
252
|
+
* chain: 'SOLANA',
|
|
253
|
+
* token: 'USDC',
|
|
254
|
+
* });
|
|
255
|
+
*/
|
|
256
|
+
async createPayout(params) {
|
|
257
|
+
const { data } = await this.client.post("/v1/payouts", params);
|
|
258
|
+
return data;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Waits for a payment to complete by polling the 402 endpoint using a reference key.
|
|
262
|
+
* The request is considered complete when the response no longer includes `referenceKey`.
|
|
263
|
+
*/
|
|
264
|
+
async waitForPaymentCompletion(options) {
|
|
265
|
+
const baseIntervalMs = options.intervalMs ?? 15e3;
|
|
266
|
+
let currentIntervalMs = baseIntervalMs;
|
|
267
|
+
const timeoutMs = options.timeoutMs ?? 5 * 6e4;
|
|
268
|
+
const deadline = Date.now() + timeoutMs;
|
|
269
|
+
let last = null;
|
|
270
|
+
while (true) {
|
|
271
|
+
if (options.signal?.aborted) {
|
|
272
|
+
return { paid: false, last };
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
try {
|
|
276
|
+
const resp = await this.client.post(
|
|
277
|
+
`/v1/payment/request-payment`,
|
|
278
|
+
{
|
|
279
|
+
assets: options.assets,
|
|
280
|
+
paymentReference: options.paymentReference,
|
|
281
|
+
paymentLabel: options.paymentLabel,
|
|
282
|
+
generateQrCode: false
|
|
283
|
+
}
|
|
284
|
+
);
|
|
285
|
+
last = resp.data.data;
|
|
286
|
+
} catch (err) {
|
|
287
|
+
const ax = err;
|
|
288
|
+
const status = ax.response?.status;
|
|
289
|
+
if (status === 402) {
|
|
290
|
+
last = ax.response?.data?.data ?? null;
|
|
291
|
+
} else {
|
|
292
|
+
if (status && [400, 401, 403, 404, 422].includes(status)) {
|
|
293
|
+
options.onError?.(err);
|
|
294
|
+
return { paid: false, last };
|
|
295
|
+
}
|
|
296
|
+
options.onError?.(err);
|
|
297
|
+
currentIntervalMs = Math.min(Math.ceil(currentIntervalMs * 1.5), 6e4);
|
|
298
|
+
last = last ?? null;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
options.onUpdate?.(last);
|
|
302
|
+
if (last?.status === "expired" /* EXPIRED */ || last?.status === "failed" /* FAILED */) {
|
|
303
|
+
return { paid: false, last };
|
|
304
|
+
}
|
|
305
|
+
const paid = !last?.referenceKey;
|
|
306
|
+
if (paid) return { paid: true, last };
|
|
307
|
+
currentIntervalMs = baseIntervalMs;
|
|
308
|
+
} catch (e) {
|
|
309
|
+
options.onError?.(e);
|
|
310
|
+
currentIntervalMs = Math.min(Math.ceil(currentIntervalMs * 1.5), 6e4);
|
|
311
|
+
}
|
|
312
|
+
if (Date.now() >= deadline) return { paid: false, last };
|
|
313
|
+
await new Promise((r) => setTimeout(r, currentIntervalMs));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Creates a payment request for purchasing assets using a two‑phase 402 flow.
|
|
318
|
+
*
|
|
319
|
+
* Phase 1 – Request payment (no paymentReference):
|
|
320
|
+
* - Server responds with HTTP 402 Payment Required and a payload containing:
|
|
321
|
+
* referenceKey, paymentUrl (Solana Pay), optional qrCode, amount, expiresAt, status.
|
|
322
|
+
* - The SDK normalizes this by returning the payload (even when status code is 402).
|
|
323
|
+
* - The caller must instruct the user to pay via their wallet using paymentUrl/qrCode.
|
|
324
|
+
*
|
|
325
|
+
* Phase 2 – Check/complete payment (with paymentReference):
|
|
326
|
+
* - Call again with the same assets and paymentReference returned in Phase 1.
|
|
327
|
+
* - If payment is still pending, server may again provide the referenceKey (or return 402).
|
|
328
|
+
* - When payment is complete, server will return success (no referenceKey required). In this SDK
|
|
329
|
+
* we consider the payment complete when the response does NOT include referenceKey.
|
|
240
330
|
*
|
|
241
331
|
* @param input - Parameters for the asset purchase request
|
|
242
|
-
* @returns
|
|
332
|
+
* @returns Payment request data or null when the input is invalid
|
|
243
333
|
*
|
|
244
334
|
* @example
|
|
245
|
-
*
|
|
246
|
-
* const
|
|
247
|
-
*
|
|
248
|
-
*
|
|
335
|
+
* // Phase 1: request
|
|
336
|
+
* const req = await beep.payments.requestAndPurchaseAsset({
|
|
337
|
+
* assets: [{ assetId: 'uuid', quantity: 1 }],
|
|
338
|
+
* generateQrCode: true,
|
|
339
|
+
* paymentLabel: 'My Store'
|
|
249
340
|
* });
|
|
341
|
+
* // Show req.paymentUrl/req.qrCode to user; req.referenceKey identifies this payment.
|
|
250
342
|
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* }
|
|
254
|
-
*
|
|
343
|
+
* // Phase 2: poll status using the same API with the referenceKey
|
|
344
|
+
* const check = await beep.payments.requestAndPurchaseAsset({
|
|
345
|
+
* assets: [{ assetId: 'uuid', quantity: 1 }],
|
|
346
|
+
* paymentReference: req?.referenceKey,
|
|
347
|
+
* generateQrCode: false
|
|
348
|
+
* });
|
|
349
|
+
* const isPaid = !check?.referenceKey; // When no referenceKey is returned, payment is complete
|
|
255
350
|
*/
|
|
256
351
|
async requestAndPurchaseAsset(input) {
|
|
257
352
|
if (!input.paymentReference && !input.assets?.length) {
|
|
@@ -317,6 +412,143 @@ var PaymentsModule = class {
|
|
|
317
412
|
throw new Error(`Failed to sign solana transaction: ${errorMessage}`);
|
|
318
413
|
}
|
|
319
414
|
}
|
|
415
|
+
// --- Streaming Payment Methods ---
|
|
416
|
+
// Note: These methods are ONLY available with BeepClient (secret API keys)
|
|
417
|
+
// They do NOT work with BeepPublicClient (publishable keys)
|
|
418
|
+
/**
|
|
419
|
+
* Issues a payment request for streaming charges with specified asset chunks
|
|
420
|
+
*
|
|
421
|
+
* Creates a new streaming payment session that can be started, paused, and stopped.
|
|
422
|
+
* This is the first step in setting up automatic billing for ongoing services or
|
|
423
|
+
* consumption-based pricing.
|
|
424
|
+
*
|
|
425
|
+
* **Important Security Note**: This method requires a secret API key and is only
|
|
426
|
+
* available when using `BeepClient`. It will NOT work with `BeepPublicClient` or
|
|
427
|
+
* publishable keys for security reasons.
|
|
428
|
+
*
|
|
429
|
+
* @param payload - Payment request details including assets and merchant information
|
|
430
|
+
* @returns Promise resolving to payment session identifiers
|
|
431
|
+
* @throws {Error} When the request fails or authentication is invalid
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```typescript
|
|
435
|
+
* // Only works with BeepClient (server-side)
|
|
436
|
+
* const beep = new BeepClient({ apiKey: 'your_secret_api_key' });
|
|
437
|
+
*
|
|
438
|
+
* const paymentSession = await beep.payments.issuePayment({
|
|
439
|
+
* apiKey: 'your_secret_api_key',
|
|
440
|
+
* assetChunks: [
|
|
441
|
+
* { assetId: 'video-streaming-uuid', quantity: 1 },
|
|
442
|
+
* { assetId: 'api-calls-uuid', quantity: 100 }
|
|
443
|
+
* ],
|
|
444
|
+
* payingMerchantId: 'merchant_who_will_be_charged',
|
|
445
|
+
* invoiceId: 'optional_existing_invoice_uuid'
|
|
446
|
+
* });
|
|
447
|
+
*
|
|
448
|
+
* console.log('Payment session created:', paymentSession.referenceKey);
|
|
449
|
+
* console.log('Invoice ID for management:', paymentSession.invoiceId);
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
async issuePayment(payload) {
|
|
453
|
+
const response = await this.client.post(
|
|
454
|
+
"/v1/invoices/issue-payment",
|
|
455
|
+
payload
|
|
456
|
+
);
|
|
457
|
+
return response.data;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Starts an active streaming session and begins charging for asset usage
|
|
461
|
+
*
|
|
462
|
+
* Activates a previously issued payment request and begins billing the specified
|
|
463
|
+
* merchant according to the streaming payment configuration. Once started, charges
|
|
464
|
+
* will accumulate based on actual usage.
|
|
465
|
+
*
|
|
466
|
+
* **Important Security Note**: This method requires a secret API key and is only
|
|
467
|
+
* available when using `BeepClient`. It will NOT work with `BeepPublicClient`.
|
|
468
|
+
*
|
|
469
|
+
* @param payload - Streaming session start details
|
|
470
|
+
* @returns Promise resolving to confirmation of the started session
|
|
471
|
+
* @throws {Error} When the invoice is invalid or already active
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```typescript
|
|
475
|
+
* // Start billing for the streaming session
|
|
476
|
+
* const result = await beep.payments.startStreaming({
|
|
477
|
+
* apiKey: 'your_secret_api_key',
|
|
478
|
+
* invoiceId: 'invoice_uuid_from_issuePayment'
|
|
479
|
+
* });
|
|
480
|
+
*
|
|
481
|
+
* console.log('Streaming started for invoice:', result.invoiceId);
|
|
482
|
+
* // Charges will now accumulate based on usage
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
async startStreaming(payload) {
|
|
486
|
+
const response = await this.client.post("/v1/invoices/start", payload);
|
|
487
|
+
return response.data;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Temporarily pauses an active streaming session without terminating it
|
|
491
|
+
*
|
|
492
|
+
* Halts billing for a streaming session while keeping the session alive for later resumption.
|
|
493
|
+
* This is useful for temporary service interruptions or when you want to control billing
|
|
494
|
+
* periods precisely.
|
|
495
|
+
*
|
|
496
|
+
* **Important Security Note**: This method requires a secret API key and is only
|
|
497
|
+
* available when using `BeepClient`. It will NOT work with `BeepPublicClient`.
|
|
498
|
+
*
|
|
499
|
+
* @param payload - Streaming session pause details
|
|
500
|
+
* @returns Promise resolving to pause operation result
|
|
501
|
+
* @throws {Error} When the invoice is not in a valid state for pausing
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* ```typescript
|
|
505
|
+
* // Temporarily pause billing (can be resumed later)
|
|
506
|
+
* const result = await beep.payments.pauseStreaming({
|
|
507
|
+
* apiKey: 'your_secret_api_key',
|
|
508
|
+
* invoiceId: 'active_streaming_invoice_uuid'
|
|
509
|
+
* });
|
|
510
|
+
*
|
|
511
|
+
* if (result.success) {
|
|
512
|
+
* console.log('Streaming paused - no new charges will accumulate');
|
|
513
|
+
* // Use startStreaming() again to resume
|
|
514
|
+
* }
|
|
515
|
+
* ```
|
|
516
|
+
*/
|
|
517
|
+
async pauseStreaming(payload) {
|
|
518
|
+
const response = await this.client.post("/v1/invoices/pause", payload);
|
|
519
|
+
return response.data;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Permanently stops a streaming session and finalizes all charges
|
|
523
|
+
*
|
|
524
|
+
* Terminates a streaming session completely, finalizing all accumulated charges.
|
|
525
|
+
* This action cannot be undone - the session cannot be restarted after stopping.
|
|
526
|
+
* Use this when you're completely finished with a service or want to close out billing.
|
|
527
|
+
*
|
|
528
|
+
* **Important Security Note**: This method requires a secret API key and is only
|
|
529
|
+
* available when using `BeepClient`. It will NOT work with `BeepPublicClient`.
|
|
530
|
+
*
|
|
531
|
+
* @param payload - Streaming session stop details
|
|
532
|
+
* @returns Promise resolving to final session details and reference keys
|
|
533
|
+
* @throws {Error} When the invoice cannot be stopped or doesn't exist
|
|
534
|
+
*
|
|
535
|
+
* @example
|
|
536
|
+
* ```typescript
|
|
537
|
+
* // Permanently stop and finalize the streaming session
|
|
538
|
+
* const result = await beep.payments.stopStreaming({
|
|
539
|
+
* apiKey: 'your_secret_api_key',
|
|
540
|
+
* invoiceId: 'active_streaming_invoice_uuid'
|
|
541
|
+
* });
|
|
542
|
+
*
|
|
543
|
+
* console.log('Session permanently stopped:', result.invoiceId);
|
|
544
|
+
* console.log('All reference keys for records:', result.referenceKeys);
|
|
545
|
+
* // Session cannot be restarted after this point
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
548
|
+
async stopStreaming(payload) {
|
|
549
|
+
const response = await this.client.post("/v1/invoices/stop", payload);
|
|
550
|
+
return response.data;
|
|
551
|
+
}
|
|
320
552
|
};
|
|
321
553
|
|
|
322
554
|
// src/modules/products.ts
|
|
@@ -454,6 +686,72 @@ var ProductsModule = class {
|
|
|
454
686
|
}
|
|
455
687
|
};
|
|
456
688
|
|
|
689
|
+
// src/modules/widget.ts
|
|
690
|
+
var WidgetModule = class {
|
|
691
|
+
constructor(client, publishableKey) {
|
|
692
|
+
this.client = client;
|
|
693
|
+
this.publishableKey = publishableKey;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Creates a payment session (public, CORS-open) for Checkout Widget
|
|
697
|
+
*/
|
|
698
|
+
async createPaymentSession(input) {
|
|
699
|
+
const body = {
|
|
700
|
+
publishableKey: this.publishableKey,
|
|
701
|
+
assets: input.assets,
|
|
702
|
+
paymentLabel: input.paymentLabel,
|
|
703
|
+
generateQrCode: input.generateQrCode ?? true
|
|
704
|
+
};
|
|
705
|
+
const res = await this.client.post(
|
|
706
|
+
"/v1/widget/payment-session",
|
|
707
|
+
body
|
|
708
|
+
);
|
|
709
|
+
return res.data;
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Retrieves payment status for a reference key
|
|
713
|
+
*/
|
|
714
|
+
async getPaymentStatus(referenceKey) {
|
|
715
|
+
const res = await this.client.get(
|
|
716
|
+
`/v1/widget/payment-status/${encodeURIComponent(referenceKey)}`
|
|
717
|
+
);
|
|
718
|
+
return res.data;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Waits for payment completion by polling the public status endpoint until paid or timeout.
|
|
722
|
+
* Designed for browser/public usage (no secret keys).
|
|
723
|
+
*/
|
|
724
|
+
async waitForPaid(options) {
|
|
725
|
+
const baseIntervalMs = options.intervalMs ?? 15e3;
|
|
726
|
+
let currentIntervalMs = baseIntervalMs;
|
|
727
|
+
const timeoutMs = options.timeoutMs ?? 5 * 6e4;
|
|
728
|
+
const deadline = Date.now() + timeoutMs;
|
|
729
|
+
let last;
|
|
730
|
+
while (true) {
|
|
731
|
+
if (options.signal?.aborted) return { paid: false, last };
|
|
732
|
+
try {
|
|
733
|
+
const res = await this.client.get(
|
|
734
|
+
`/v1/widget/payment-status/${encodeURIComponent(options.referenceKey)}`
|
|
735
|
+
);
|
|
736
|
+
last = res.data;
|
|
737
|
+
options.onUpdate?.(last);
|
|
738
|
+
if (last?.paid) return { paid: true, last };
|
|
739
|
+
currentIntervalMs = baseIntervalMs;
|
|
740
|
+
} catch (err) {
|
|
741
|
+
options.onError?.(err);
|
|
742
|
+
const ax = err;
|
|
743
|
+
const status = ax.response?.status;
|
|
744
|
+
if (status && [400, 401, 403, 404, 422].includes(status)) {
|
|
745
|
+
return { paid: false, last };
|
|
746
|
+
}
|
|
747
|
+
currentIntervalMs = Math.min(Math.ceil(currentIntervalMs * 1.5), 6e4);
|
|
748
|
+
}
|
|
749
|
+
if (Date.now() >= deadline) return { paid: false, last };
|
|
750
|
+
await new Promise((r) => setTimeout(r, currentIntervalMs));
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
|
|
457
755
|
// src/index.ts
|
|
458
756
|
var BeepClient = class {
|
|
459
757
|
/**
|
|
@@ -478,6 +776,24 @@ var BeepClient = class {
|
|
|
478
776
|
this.invoices = new InvoicesModule(this.client);
|
|
479
777
|
this.payments = new PaymentsModule(this.client);
|
|
480
778
|
}
|
|
779
|
+
/**
|
|
780
|
+
* Initiate a payout from your treasury wallet to an external address.
|
|
781
|
+
* Requires a secret API key (server-side only).
|
|
782
|
+
*
|
|
783
|
+
* Notes:
|
|
784
|
+
* - Do not pass walletId. The server derives the wallet based on your API key's merchant and requested chain.
|
|
785
|
+
* - amount must be in smallest units for the token (e.g., 6‑decimals USDC amount as an integer string).
|
|
786
|
+
* - This endpoint responds immediately with acceptance/rejection. Actual transfer executes asynchronously after funds are reserved.
|
|
787
|
+
*
|
|
788
|
+
* Example:
|
|
789
|
+
* const res = await beep.payments.createPayout({
|
|
790
|
+
* amount: '1000000', // 1.0 USDC with 6 decimals
|
|
791
|
+
* destinationWalletAddress: 'DEST_ADDRESS',
|
|
792
|
+
* chain: 'SOLANA',
|
|
793
|
+
* token: 'USDC',
|
|
794
|
+
* });
|
|
795
|
+
*/
|
|
796
|
+
// Deprecated: use beep.payments.createPayout()
|
|
481
797
|
/**
|
|
482
798
|
* Checks the health status of the BEEP API server
|
|
483
799
|
*
|
|
@@ -502,9 +818,31 @@ var BeepClient = class {
|
|
|
502
818
|
}
|
|
503
819
|
}
|
|
504
820
|
};
|
|
821
|
+
var BeepPublicClient = class {
|
|
822
|
+
/**
|
|
823
|
+
* Creates a new BEEP public client instance for browser use
|
|
824
|
+
*
|
|
825
|
+
* @param options - Configuration options including publishable key
|
|
826
|
+
* @throws {Error} When publishable key is missing or invalid
|
|
827
|
+
*/
|
|
828
|
+
constructor(options) {
|
|
829
|
+
if (!options.publishableKey) {
|
|
830
|
+
throw new Error("publishableKey is required to initialize BeepPublicClient");
|
|
831
|
+
}
|
|
832
|
+
this.client = import_axios.default.create({
|
|
833
|
+
baseURL: options.serverUrl || "https://api.justbeep.it",
|
|
834
|
+
headers: {
|
|
835
|
+
"Content-Type": "application/json",
|
|
836
|
+
"X-Beep-Client": "beep-sdk"
|
|
837
|
+
}
|
|
838
|
+
});
|
|
839
|
+
this.widget = new WidgetModule(this.client, options.publishableKey);
|
|
840
|
+
}
|
|
841
|
+
};
|
|
505
842
|
// Annotate the CommonJS export names for ESM import in node:
|
|
506
843
|
0 && (module.exports = {
|
|
507
844
|
BeepClient,
|
|
845
|
+
BeepPublicClient,
|
|
508
846
|
SupportedToken,
|
|
509
847
|
TokenUtils
|
|
510
848
|
});
|