@solvapay/server 1.2.0 → 1.2.1-preview-e89fa52f99b6d0c7bf9834fcf2d1403141051fa0

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/edge.js CHANGED
@@ -28,7 +28,9 @@ function createSolvaPayClient(opts) {
28
28
  if (!res.ok) {
29
29
  const error = await res.text();
30
30
  log(`\u274C API Error: ${res.status} - ${error}`);
31
- throw new SolvaPayError(`Check limits failed (${res.status}): ${error}`, { status: res.status });
31
+ throw new SolvaPayError(`Check limits failed (${res.status}): ${error}`, {
32
+ status: res.status
33
+ });
32
34
  }
33
35
  const result = await res.json();
34
36
  return result;
@@ -44,8 +46,28 @@ function createSolvaPayClient(opts) {
44
46
  if (!res.ok) {
45
47
  const error = await res.text();
46
48
  log(`\u274C API Error: ${res.status} - ${error}`);
47
- throw new SolvaPayError(`Track usage failed (${res.status}): ${error}`, { status: res.status });
49
+ throw new SolvaPayError(`Track usage failed (${res.status}): ${error}`, {
50
+ status: res.status
51
+ });
48
52
  }
53
+ return await res.json();
54
+ },
55
+ // POST: /v1/sdk/usages/bulk
56
+ async trackUsageBulk(params) {
57
+ const url = `${base}/v1/sdk/usages/bulk`;
58
+ const res = await fetch(url, {
59
+ method: "POST",
60
+ headers,
61
+ body: JSON.stringify(params)
62
+ });
63
+ if (!res.ok) {
64
+ const error = await res.text();
65
+ log(`\u274C API Error: ${res.status} - ${error}`);
66
+ throw new SolvaPayError(`Track usage bulk failed (${res.status}): ${error}`, {
67
+ status: res.status
68
+ });
69
+ }
70
+ return await res.json();
49
71
  },
50
72
  // POST: /v1/sdk/customers
51
73
  async createCustomer(params) {
@@ -58,7 +80,9 @@ function createSolvaPayClient(opts) {
58
80
  if (!res.ok) {
59
81
  const error = await res.text();
60
82
  log(`\u274C API Error: ${res.status} - ${error}`);
61
- throw new SolvaPayError(`Create customer failed (${res.status}): ${error}`, { status: res.status });
83
+ throw new SolvaPayError(`Create customer failed (${res.status}): ${error}`, {
84
+ status: res.status
85
+ });
62
86
  }
63
87
  const result = await res.json();
64
88
  return {
@@ -76,7 +100,9 @@ function createSolvaPayClient(opts) {
76
100
  if (!res.ok) {
77
101
  const error = await res.text();
78
102
  log(`\u274C API Error: ${res.status} - ${error}`);
79
- throw new SolvaPayError(`Update customer failed (${res.status}): ${error}`, { status: res.status });
103
+ throw new SolvaPayError(`Update customer failed (${res.status}): ${error}`, {
104
+ status: res.status
105
+ });
80
106
  }
81
107
  const result = await res.json();
82
108
  return {
@@ -106,7 +132,9 @@ function createSolvaPayClient(opts) {
106
132
  if (!res.ok) {
107
133
  const error = await res.text();
108
134
  log(`\u274C API Error: ${res.status} - ${error}`);
109
- throw new SolvaPayError(`Get customer failed (${res.status}): ${error}`, { status: res.status });
135
+ throw new SolvaPayError(`Get customer failed (${res.status}): ${error}`, {
136
+ status: res.status
137
+ });
110
138
  }
111
139
  const result = await res.json();
112
140
  let customer = result;
@@ -127,6 +155,27 @@ function createSolvaPayClient(opts) {
127
155
  purchases: customer.purchases || []
128
156
  };
129
157
  },
158
+ // POST: /v1/sdk/customers/{reference}/credits
159
+ async assignCredits(params) {
160
+ const { customerRef, idempotencyKey, ...body } = params;
161
+ const url = `${base}/v1/sdk/customers/${encodeURIComponent(customerRef)}/credits`;
162
+ const res = await fetch(url, {
163
+ method: "POST",
164
+ headers: {
165
+ ...headers,
166
+ ...idempotencyKey ? { "Idempotency-Key": idempotencyKey } : {}
167
+ },
168
+ body: JSON.stringify(body)
169
+ });
170
+ if (!res.ok) {
171
+ const error = await res.text();
172
+ log(`\u274C API Error: ${res.status} - ${error}`);
173
+ throw new SolvaPayError(`Assign credits failed (${res.status}): ${error}`, {
174
+ status: res.status
175
+ });
176
+ }
177
+ return await res.json();
178
+ },
130
179
  // GET: /v1/sdk/merchant
131
180
  async getMerchant() {
132
181
  const url = `${base}/v1/sdk/merchant`;
@@ -137,7 +186,9 @@ function createSolvaPayClient(opts) {
137
186
  if (!res.ok) {
138
187
  const error = await res.text();
139
188
  log(`\u274C API Error: ${res.status} - ${error}`);
140
- throw new SolvaPayError(`Get merchant failed (${res.status}): ${error}`, { status: res.status });
189
+ throw new SolvaPayError(`Get merchant failed (${res.status}): ${error}`, {
190
+ status: res.status
191
+ });
141
192
  }
142
193
  return res.json();
143
194
  },
@@ -151,7 +202,9 @@ function createSolvaPayClient(opts) {
151
202
  if (!res.ok) {
152
203
  const error = await res.text();
153
204
  log(`\u274C API Error: ${res.status} - ${error}`);
154
- throw new SolvaPayError(`Get platform config failed (${res.status}): ${error}`, { status: res.status });
205
+ throw new SolvaPayError(`Get platform config failed (${res.status}): ${error}`, {
206
+ status: res.status
207
+ });
155
208
  }
156
209
  return res.json();
157
210
  },
@@ -165,7 +218,9 @@ function createSolvaPayClient(opts) {
165
218
  if (!res.ok) {
166
219
  const error = await res.text();
167
220
  log(`\u274C API Error: ${res.status} - ${error}`);
168
- throw new SolvaPayError(`Get product failed (${res.status}): ${error}`, { status: res.status });
221
+ throw new SolvaPayError(`Get product failed (${res.status}): ${error}`, {
222
+ status: res.status
223
+ });
169
224
  }
170
225
  const result = await res.json();
171
226
  const data = result.data || {};
@@ -182,7 +237,9 @@ function createSolvaPayClient(opts) {
182
237
  if (!res.ok) {
183
238
  const error = await res.text();
184
239
  log(`\u274C API Error: ${res.status} - ${error}`);
185
- throw new SolvaPayError(`List products failed (${res.status}): ${error}`, { status: res.status });
240
+ throw new SolvaPayError(`List products failed (${res.status}): ${error}`, {
241
+ status: res.status
242
+ });
186
243
  }
187
244
  const result = await res.json();
188
245
  const products = Array.isArray(result) ? result : result.products || [];
@@ -202,7 +259,9 @@ function createSolvaPayClient(opts) {
202
259
  if (!res.ok) {
203
260
  const error = await res.text();
204
261
  log(`\u274C API Error: ${res.status} - ${error}`);
205
- throw new SolvaPayError(`Create product failed (${res.status}): ${error}`, { status: res.status });
262
+ throw new SolvaPayError(`Create product failed (${res.status}): ${error}`, {
263
+ status: res.status
264
+ });
206
265
  }
207
266
  const result = await res.json();
208
267
  return result;
@@ -218,7 +277,9 @@ function createSolvaPayClient(opts) {
218
277
  if (!res.ok) {
219
278
  const error = await res.text();
220
279
  log(`\u274C API Error: ${res.status} - ${error}`);
221
- throw new SolvaPayError(`Bootstrap MCP product failed (${res.status}): ${error}`, { status: res.status });
280
+ throw new SolvaPayError(`Bootstrap MCP product failed (${res.status}): ${error}`, {
281
+ status: res.status
282
+ });
222
283
  }
223
284
  return await res.json();
224
285
  },
@@ -233,7 +294,9 @@ function createSolvaPayClient(opts) {
233
294
  if (!res.ok) {
234
295
  const error = await res.text();
235
296
  log(`\u274C API Error: ${res.status} - ${error}`);
236
- throw new SolvaPayError(`Configure MCP plans failed (${res.status}): ${error}`, { status: res.status });
297
+ throw new SolvaPayError(`Configure MCP plans failed (${res.status}): ${error}`, {
298
+ status: res.status
299
+ });
237
300
  }
238
301
  return await res.json();
239
302
  },
@@ -247,7 +310,9 @@ function createSolvaPayClient(opts) {
247
310
  if (!res.ok && res.status !== 404) {
248
311
  const error = await res.text();
249
312
  log(`\u274C API Error: ${res.status} - ${error}`);
250
- throw new SolvaPayError(`Delete product failed (${res.status}): ${error}`, { status: res.status });
313
+ throw new SolvaPayError(`Delete product failed (${res.status}): ${error}`, {
314
+ status: res.status
315
+ });
251
316
  }
252
317
  },
253
318
  // POST: /v1/sdk/products/{productRef}/clone
@@ -261,7 +326,9 @@ function createSolvaPayClient(opts) {
261
326
  if (!res.ok) {
262
327
  const error = await res.text();
263
328
  log(`\u274C API Error: ${res.status} - ${error}`);
264
- throw new SolvaPayError(`Clone product failed (${res.status}): ${error}`, { status: res.status });
329
+ throw new SolvaPayError(`Clone product failed (${res.status}): ${error}`, {
330
+ status: res.status
331
+ });
265
332
  }
266
333
  return await res.json();
267
334
  },
@@ -275,7 +342,9 @@ function createSolvaPayClient(opts) {
275
342
  if (!res.ok) {
276
343
  const error = await res.text();
277
344
  log(`\u274C API Error: ${res.status} - ${error}`);
278
- throw new SolvaPayError(`List plans failed (${res.status}): ${error}`, { status: res.status });
345
+ throw new SolvaPayError(`List plans failed (${res.status}): ${error}`, {
346
+ status: res.status
347
+ });
279
348
  }
280
349
  const result = await res.json();
281
350
  const plans = Array.isArray(result) ? result : result.plans || [];
@@ -302,7 +371,9 @@ function createSolvaPayClient(opts) {
302
371
  if (!res.ok) {
303
372
  const error = await res.text();
304
373
  log(`\u274C API Error: ${res.status} - ${error}`);
305
- throw new SolvaPayError(`Create plan failed (${res.status}): ${error}`, { status: res.status });
374
+ throw new SolvaPayError(`Create plan failed (${res.status}): ${error}`, {
375
+ status: res.status
376
+ });
306
377
  }
307
378
  const result = await res.json();
308
379
  return result;
@@ -318,7 +389,9 @@ function createSolvaPayClient(opts) {
318
389
  if (!res.ok) {
319
390
  const error = await res.text();
320
391
  log(`\u274C API Error: ${res.status} - ${error}`);
321
- throw new SolvaPayError(`Update plan failed (${res.status}): ${error}`, { status: res.status });
392
+ throw new SolvaPayError(`Update plan failed (${res.status}): ${error}`, {
393
+ status: res.status
394
+ });
322
395
  }
323
396
  return await res.json();
324
397
  },
@@ -332,7 +405,9 @@ function createSolvaPayClient(opts) {
332
405
  if (!res.ok && res.status !== 404) {
333
406
  const error = await res.text();
334
407
  log(`\u274C API Error: ${res.status} - ${error}`);
335
- throw new SolvaPayError(`Delete plan failed (${res.status}): ${error}`, { status: res.status });
408
+ throw new SolvaPayError(`Delete plan failed (${res.status}): ${error}`, {
409
+ status: res.status
410
+ });
336
411
  }
337
412
  },
338
413
  // POST: /payment-intents
@@ -348,13 +423,16 @@ function createSolvaPayClient(opts) {
348
423
  body: JSON.stringify({
349
424
  productRef: params.productRef,
350
425
  planRef: params.planRef,
351
- customerRef: params.customerRef
426
+ customerRef: params.customerRef,
427
+ ...params.currency && { currency: params.currency }
352
428
  })
353
429
  });
354
430
  if (!res.ok) {
355
431
  const error = await res.text();
356
432
  log(`\u274C API Error: ${res.status} - ${error}`);
357
- throw new SolvaPayError(`Create payment intent failed (${res.status}): ${error}`, { status: res.status });
433
+ throw new SolvaPayError(`Create payment intent failed (${res.status}): ${error}`, {
434
+ status: res.status
435
+ });
358
436
  }
359
437
  return await res.json();
360
438
  },
@@ -379,7 +457,9 @@ function createSolvaPayClient(opts) {
379
457
  if (!res.ok) {
380
458
  const error = await res.text();
381
459
  log(`\u274C API Error: ${res.status} - ${error}`);
382
- throw new SolvaPayError(`Create topup payment intent failed (${res.status}): ${error}`, { status: res.status });
460
+ throw new SolvaPayError(`Create topup payment intent failed (${res.status}): ${error}`, {
461
+ status: res.status
462
+ });
383
463
  }
384
464
  return await res.json();
385
465
  },
@@ -401,7 +481,9 @@ function createSolvaPayClient(opts) {
401
481
  if (!res.ok) {
402
482
  const error = await res.text();
403
483
  log(`\u274C API Error: ${res.status} - ${error}`);
404
- throw new SolvaPayError(`Process payment failed (${res.status}): ${error}`, { status: res.status });
484
+ throw new SolvaPayError(`Process payment failed (${res.status}): ${error}`, {
485
+ status: res.status
486
+ });
405
487
  }
406
488
  const result = await res.json();
407
489
  return result;
@@ -429,7 +511,9 @@ function createSolvaPayClient(opts) {
429
511
  { status: 400 }
430
512
  );
431
513
  }
432
- throw new SolvaPayError(`Cancel purchase failed (${res.status}): ${error}`, { status: res.status });
514
+ throw new SolvaPayError(`Cancel purchase failed (${res.status}): ${error}`, {
515
+ status: res.status
516
+ });
433
517
  }
434
518
  const responseText = await res.text();
435
519
  let responseData;
@@ -473,12 +557,11 @@ function createSolvaPayClient(opts) {
473
557
  throw new SolvaPayError(`Purchase not found: ${error}`, { status: 404 });
474
558
  }
475
559
  if (res.status === 400) {
476
- throw new SolvaPayError(
477
- `Purchase cannot be reactivated: ${error}`,
478
- { status: 400 }
479
- );
560
+ throw new SolvaPayError(`Purchase cannot be reactivated: ${error}`, { status: 400 });
480
561
  }
481
- throw new SolvaPayError(`Reactivate purchase failed (${res.status}): ${error}`, { status: res.status });
562
+ throw new SolvaPayError(`Reactivate purchase failed (${res.status}): ${error}`, {
563
+ status: res.status
564
+ });
482
565
  }
483
566
  const responseText = await res.text();
484
567
  let responseData;
@@ -519,7 +602,9 @@ function createSolvaPayClient(opts) {
519
602
  if (!res.ok) {
520
603
  const error = await res.text();
521
604
  log(`\u274C API Error: ${res.status} - ${error}`);
522
- throw new SolvaPayError(`Get user info failed (${res.status}): ${error}`, { status: res.status });
605
+ throw new SolvaPayError(`Get user info failed (${res.status}): ${error}`, {
606
+ status: res.status
607
+ });
523
608
  }
524
609
  return await res.json();
525
610
  },
@@ -533,7 +618,9 @@ function createSolvaPayClient(opts) {
533
618
  if (!res.ok) {
534
619
  const error = await res.text();
535
620
  log(`\u274C API Error: ${res.status} - ${error}`);
536
- throw new SolvaPayError(`Get customer balance failed (${res.status}): ${error}`, { status: res.status });
621
+ throw new SolvaPayError(`Get customer balance failed (${res.status}): ${error}`, {
622
+ status: res.status
623
+ });
537
624
  }
538
625
  return await res.json();
539
626
  },
@@ -548,7 +635,9 @@ function createSolvaPayClient(opts) {
548
635
  if (!res.ok) {
549
636
  const error = await res.text();
550
637
  log(`\u274C API Error: ${res.status} - ${error}`);
551
- throw new SolvaPayError(`Create checkout session failed (${res.status}): ${error}`, { status: res.status });
638
+ throw new SolvaPayError(`Create checkout session failed (${res.status}): ${error}`, {
639
+ status: res.status
640
+ });
552
641
  }
553
642
  const result = await res.json();
554
643
  return result;
@@ -564,7 +653,9 @@ function createSolvaPayClient(opts) {
564
653
  if (!res.ok) {
565
654
  const error = await res.text();
566
655
  log(`\u274C API Error: ${res.status} - ${error}`);
567
- throw new SolvaPayError(`Create customer session failed (${res.status}): ${error}`, { status: res.status });
656
+ throw new SolvaPayError(`Create customer session failed (${res.status}): ${error}`, {
657
+ status: res.status
658
+ });
568
659
  }
569
660
  const result = await res.json();
570
661
  return result;
@@ -580,7 +671,9 @@ function createSolvaPayClient(opts) {
580
671
  if (!res.ok) {
581
672
  const error = await res.text();
582
673
  log(`\u274C API Error: ${res.status} - ${error}`);
583
- throw new SolvaPayError(`Activate plan failed (${res.status}): ${error}`, { status: res.status });
674
+ throw new SolvaPayError(`Activate plan failed (${res.status}): ${error}`, {
675
+ status: res.status
676
+ });
584
677
  }
585
678
  return await res.json();
586
679
  },
@@ -591,7 +684,9 @@ function createSolvaPayClient(opts) {
591
684
  if (!res.ok) {
592
685
  const error = await res.text();
593
686
  log(`\u274C API Error: ${res.status} - ${error}`);
594
- throw new SolvaPayError(`Get payment method failed (${res.status}): ${error}`, { status: res.status });
687
+ throw new SolvaPayError(`Get payment method failed (${res.status}): ${error}`, {
688
+ status: res.status
689
+ });
595
690
  }
596
691
  return await res.json();
597
692
  }
@@ -1880,6 +1975,12 @@ function createSolvaPay(config) {
1880
1975
  trackUsage(params) {
1881
1976
  return apiClient.trackUsage(params);
1882
1977
  },
1978
+ trackUsageBulk(params) {
1979
+ if (!apiClient.trackUsageBulk) {
1980
+ throw new SolvaPayError2("trackUsageBulk is not available on this API client");
1981
+ }
1982
+ return apiClient.trackUsageBulk(params);
1983
+ },
1883
1984
  createCustomer(params) {
1884
1985
  if (!apiClient.createCustomer) {
1885
1986
  throw new SolvaPayError2("createCustomer is not available on this API client");
@@ -1889,6 +1990,12 @@ function createSolvaPay(config) {
1889
1990
  getCustomer(params) {
1890
1991
  return apiClient.getCustomer(params);
1891
1992
  },
1993
+ assignCredits(params) {
1994
+ if (!apiClient.assignCredits) {
1995
+ throw new SolvaPayError2("assignCredits is not available on this API client");
1996
+ }
1997
+ return apiClient.assignCredits(params);
1998
+ },
1892
1999
  getCustomerBalance(params) {
1893
2000
  if (!apiClient.getCustomerBalance) {
1894
2001
  throw new SolvaPayError2("getCustomerBalance is not available on this API client");
@@ -1974,7 +2081,9 @@ function createSolvaPay(config) {
1974
2081
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1975
2082
  async function(businessLogic) {
1976
2083
  const getCustomerRef = (args) => {
1977
- const configuredRef = options.getCustomerRef?.(args);
2084
+ const configuredRef = options.getCustomerRef?.(
2085
+ args
2086
+ );
1978
2087
  if (typeof configuredRef === "string") {
1979
2088
  return configuredRef;
1980
2089
  }
@@ -1993,9 +2102,7 @@ function createSolvaPay(config) {
1993
2102
  );
1994
2103
  if (decision.outcome === "gate") {
1995
2104
  const errorMessage = decision.gate.kind === "activation_required" ? "Activation required" : "Payment required";
1996
- const body = paywallErrorToClientPayload(
1997
- new PaywallError(errorMessage, decision.gate)
1998
- );
2105
+ const body = paywallErrorToClientPayload(new PaywallError(errorMessage, decision.gate));
1999
2106
  const response = new Response(JSON.stringify(body), {
2000
2107
  status: 402,
2001
2108
  headers: { "content-type": "application/json" }
@@ -2094,6 +2201,7 @@ function handleRouteError(error, operationName, defaultMessage) {
2094
2201
  }
2095
2202
 
2096
2203
  // src/helpers/auth.ts
2204
+ import { SOLVAPAY_USER_ID_HEADER } from "@solvapay/auth";
2097
2205
  function base64UrlDecode(input) {
2098
2206
  const padded = input.replace(/-/g, "+").replace(/_/g, "/");
2099
2207
  const padding = padded.length % 4 === 0 ? "" : "=".repeat(4 - padded.length % 4);
@@ -2170,7 +2278,7 @@ async function getAuthenticatedUserCore(request, options = {}) {
2170
2278
  try {
2171
2279
  const includeEmail = options.includeEmail !== false;
2172
2280
  const includeName = options.includeName !== false;
2173
- const headerUserId = request.headers.get("x-user-id");
2281
+ const headerUserId = request.headers.get(SOLVAPAY_USER_ID_HEADER);
2174
2282
  if (headerUserId) {
2175
2283
  let email = null;
2176
2284
  let name = null;
@@ -2284,7 +2392,8 @@ async function createPaymentIntentCore(request, body, options = {}) {
2284
2392
  const paymentIntent = await solvaPay.createPaymentIntent({
2285
2393
  productRef: body.productRef,
2286
2394
  planRef: body.planRef,
2287
- customerRef
2395
+ customerRef,
2396
+ ...body.currency && { currency: body.currency }
2288
2397
  });
2289
2398
  return {
2290
2399
  processorPaymentId: paymentIntent.processorPaymentId,
@@ -2940,15 +3049,16 @@ async function trackUsageCore(request, body, options = {}) {
2940
3049
  email: email || void 0,
2941
3050
  name: name || void 0
2942
3051
  });
2943
- await solvaPay.trackUsage({
3052
+ const result = await solvaPay.trackUsage({
2944
3053
  customerRef,
2945
3054
  actionType: body.actionType,
2946
3055
  units: body.units,
2947
3056
  productRef: body.productRef,
2948
3057
  description: body.description,
2949
- metadata: body.metadata
3058
+ metadata: body.metadata,
3059
+ idempotencyKey: body.idempotencyKey
2950
3060
  });
2951
- return { success: true };
3061
+ return result;
2952
3062
  } catch (error) {
2953
3063
  return handleRouteError(error, "Track usage", "Track usage failed");
2954
3064
  }