priceos 1.0.21 → 1.0.23

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/react.js CHANGED
@@ -20,12 +20,9 @@ var buildFeatureAccessUrl = (backendUrl) => {
20
20
  const normalizedBase = normalizeBaseUrl(backendUrl);
21
21
  return `${normalizedBase}/v1/feature-access`;
22
22
  };
23
- var buildPricingTableUrl = (backendUrl, customerId, pricingTableKey) => {
23
+ var buildPricingTableUrl = (backendUrl, pricingTableKey) => {
24
24
  const normalizedBase = normalizeBaseUrl(backendUrl);
25
25
  const params = new URLSearchParams();
26
- if (customerId && customerId.trim().length > 0) {
27
- params.set("customerId", customerId.trim());
28
- }
29
26
  if (pricingTableKey && pricingTableKey.trim().length > 0) {
30
27
  params.set("pricingTableKey", pricingTableKey.trim());
31
28
  }
@@ -36,18 +33,17 @@ var buildCheckoutUrl = (backendUrl) => {
36
33
  const normalizedBase = normalizeBaseUrl(backendUrl);
37
34
  return `${normalizedBase}/v1/checkout`;
38
35
  };
39
- var buildBillingPortalUrl = (backendUrl) => {
36
+ var buildCustomerPortalUrl = (backendUrl) => {
40
37
  const normalizedBase = normalizeBaseUrl(backendUrl);
41
- return `${normalizedBase}/v1/portal`;
38
+ return `${normalizedBase}/v1/customer-portal`;
42
39
  };
43
40
  var buildTrackUsageUrl = (backendUrl) => {
44
41
  const normalizedBase = normalizeBaseUrl(backendUrl);
45
42
  return `${normalizedBase}/v1/usage`;
46
43
  };
47
44
  var resolveIdempotencyKey = (body) => {
48
- const providedEventKey = typeof body.eventKey === "string" ? body.eventKey.trim() : "";
49
45
  const providedIdempotencyKey = typeof body.idempotencyKey === "string" ? String(body.idempotencyKey).trim() : "";
50
- return providedIdempotencyKey || providedEventKey;
46
+ return providedIdempotencyKey;
51
47
  };
52
48
  var normalizeTrackUsageInput = (input) => {
53
49
  if (typeof input === "string") {
@@ -119,7 +115,6 @@ var requestTrackUsage = async (fetchFn, url, body, logLevel, bearerToken) => {
119
115
  ...idempotencyKey ? { idempotencyKey } : {},
120
116
  amount: body.amount ?? 1
121
117
  };
122
- delete payload.eventKey;
123
118
  const response = await fetchFn(url, {
124
119
  method: "POST",
125
120
  headers,
@@ -231,7 +226,7 @@ var requestPricingTable = async (fetchFn, url, logLevel, bearerToken) => {
231
226
  var requestCheckoutSession = async (fetchFn, url, body, logLevel, bearerToken) => {
232
227
  logMessage(logLevel, "debug", "Creating checkout session", {
233
228
  url,
234
- stripePriceId: body.stripePriceId,
229
+ stripeProductKey: body.stripeProductKey,
235
230
  hasCustomerId: Boolean(body.customerId)
236
231
  });
237
232
  const headers = {
@@ -273,8 +268,8 @@ var requestCheckoutSession = async (fetchFn, url, body, logLevel, bearerToken) =
273
268
  }
274
269
  return data;
275
270
  };
276
- var requestBillingPortalSession = async (fetchFn, url, body, logLevel, bearerToken) => {
277
- logMessage(logLevel, "debug", "Creating billing portal session", {
271
+ var requestCustomerPortalSession = async (fetchFn, url, body, logLevel, bearerToken) => {
272
+ logMessage(logLevel, "debug", "Creating customer portal session", {
278
273
  url,
279
274
  hasCustomerId: Boolean(body.customerId)
280
275
  });
@@ -287,7 +282,7 @@ var requestBillingPortalSession = async (fetchFn, url, body, logLevel, bearerTok
287
282
  headers,
288
283
  body: JSON.stringify(body)
289
284
  });
290
- logMessage(logLevel, "info", "Billing portal request completed", { url, status: response.status });
285
+ logMessage(logLevel, "info", "Customer portal request completed", { url, status: response.status });
291
286
  const text = await response.text();
292
287
  let data = null;
293
288
  let parseFailed = false;
@@ -296,7 +291,7 @@ var requestBillingPortalSession = async (fetchFn, url, body, logLevel, bearerTok
296
291
  data = JSON.parse(text);
297
292
  } catch {
298
293
  parseFailed = true;
299
- logMessage(logLevel, "warn", "Billing portal response is not JSON", {
294
+ logMessage(logLevel, "warn", "Customer portal response is not JSON", {
300
295
  url,
301
296
  status: response.status,
302
297
  bodyPreview: text.slice(0, 300)
@@ -305,7 +300,7 @@ var requestBillingPortalSession = async (fetchFn, url, body, logLevel, bearerTok
305
300
  }
306
301
  if (!response.ok) {
307
302
  const message = !parseFailed && data && typeof data === "object" && "error" in data ? String(data.error) : text || response.statusText;
308
- logMessage(logLevel, "error", "Billing portal request failed", {
303
+ logMessage(logLevel, "error", "Customer portal request failed", {
309
304
  url,
310
305
  status: response.status,
311
306
  message
@@ -449,52 +444,111 @@ function useTrackUsage() {
449
444
  }
450
445
  return { trackUsage };
451
446
  }
452
- function usePricingTable(options = {}) {
453
- const { enabled = true, customerId, pricingTableKey, swr } = options;
447
+ function useCheckout(options = {}) {
448
+ const { customerId } = options;
454
449
  const { getBearerToken, backendUrl, logLevel = DEFAULT_LOG_LEVEL } = useContext(PriceOSContext);
455
- const fetcher = async (url) => {
456
- let bearerToken;
457
- try {
458
- bearerToken = getBearerToken ? await getBearerToken() : void 0;
459
- } catch (error2) {
460
- logMessage(logLevel, "error", "Failed to resolve bearer token", {
461
- url,
462
- error: error2 instanceof Error ? error2.message : String(error2)
463
- });
464
- throw error2;
465
- }
466
- return requestPricingTable(fetch, url, logLevel, bearerToken);
467
- };
468
- const key = enabled ? buildPricingTableUrl(backendUrl ?? DEFAULT_BACKEND_URL, customerId, pricingTableKey) : null;
469
- const { data, error, isLoading, mutate } = useSWR(
470
- key,
471
- fetcher,
472
- swr
473
- );
474
450
  const createCheckoutSession = async (body) => {
475
451
  const url = buildCheckoutUrl(backendUrl ?? DEFAULT_BACKEND_URL);
476
452
  let bearerToken;
477
453
  try {
478
454
  bearerToken = getBearerToken ? await getBearerToken() : void 0;
479
- } catch (error2) {
455
+ } catch (error) {
480
456
  logMessage(logLevel, "error", "Failed to resolve bearer token", {
481
457
  url,
482
- error: error2 instanceof Error ? error2.message : String(error2)
458
+ error: error instanceof Error ? error.message : String(error)
483
459
  });
484
- throw error2;
460
+ throw error;
485
461
  }
486
462
  const resolvedCustomerId = body.customerId ?? customerId;
463
+ const baseCheckoutParams = body.checkoutParams && typeof body.checkoutParams === "object" ? body.checkoutParams : {};
464
+ const checkoutParams = {
465
+ ...baseCheckoutParams,
466
+ ...body.cancelUrl ? { cancel_url: body.cancelUrl } : {},
467
+ ...body.metadata ? { metadata: body.metadata } : {}
468
+ };
487
469
  return requestCheckoutSession(
488
470
  fetch,
489
471
  url,
490
472
  {
491
473
  ...body,
474
+ checkoutParams,
492
475
  ...resolvedCustomerId ? { customerId: resolvedCustomerId } : {}
493
476
  },
494
477
  logLevel,
495
478
  bearerToken
496
479
  );
497
480
  };
481
+ const openCheckout = async (body) => {
482
+ const response = await createCheckoutSession(body);
483
+ if (typeof window !== "undefined") {
484
+ window.location.assign(response.url);
485
+ }
486
+ return response;
487
+ };
488
+ return {
489
+ createCheckoutSession,
490
+ openCheckout
491
+ };
492
+ }
493
+ function useCustomerPortal(options = {}) {
494
+ const { customerId } = options;
495
+ const { getBearerToken, backendUrl, logLevel = DEFAULT_LOG_LEVEL } = useContext(PriceOSContext);
496
+ const createCustomerPortalSession = async (body) => {
497
+ const url = buildCustomerPortalUrl(backendUrl ?? DEFAULT_BACKEND_URL);
498
+ let bearerToken;
499
+ try {
500
+ bearerToken = getBearerToken ? await getBearerToken() : void 0;
501
+ } catch (error) {
502
+ logMessage(logLevel, "error", "Failed to resolve bearer token", {
503
+ url,
504
+ error: error instanceof Error ? error.message : String(error)
505
+ });
506
+ throw error;
507
+ }
508
+ const resolvedCustomerId = body?.customerId ?? customerId;
509
+ return requestCustomerPortalSession(
510
+ fetch,
511
+ url,
512
+ resolvedCustomerId ? { customerId: resolvedCustomerId } : {},
513
+ logLevel,
514
+ bearerToken
515
+ );
516
+ };
517
+ const openCustomerPortal = async (body) => {
518
+ const response = await createCustomerPortalSession(body);
519
+ if (typeof window !== "undefined") {
520
+ window.location.assign(response.url);
521
+ }
522
+ return response;
523
+ };
524
+ return {
525
+ createCustomerPortalSession,
526
+ openCustomerPortal
527
+ };
528
+ }
529
+ function usePricingTable(options = {}) {
530
+ const { enabled = true, pricingTableKey, swr } = options;
531
+ const { getBearerToken, backendUrl, logLevel = DEFAULT_LOG_LEVEL } = useContext(PriceOSContext);
532
+ const { createCheckoutSession } = useCheckout();
533
+ const fetcher = async (url) => {
534
+ let bearerToken;
535
+ try {
536
+ bearerToken = getBearerToken ? await getBearerToken() : void 0;
537
+ } catch (error2) {
538
+ logMessage(logLevel, "error", "Failed to resolve bearer token", {
539
+ url,
540
+ error: error2 instanceof Error ? error2.message : String(error2)
541
+ });
542
+ throw error2;
543
+ }
544
+ return requestPricingTable(fetch, url, logLevel, bearerToken);
545
+ };
546
+ const key = enabled ? buildPricingTableUrl(backendUrl ?? DEFAULT_BACKEND_URL, pricingTableKey) : null;
547
+ const { data, error, isLoading, mutate } = useSWR(
548
+ key,
549
+ fetcher,
550
+ swr
551
+ );
498
552
  return {
499
553
  pricingTable: data ?? null,
500
554
  error,
@@ -1355,7 +1409,6 @@ function PricingTableView({
1355
1409
  );
1356
1410
  }
1357
1411
  function PriceOSPricingTable({
1358
- customerId,
1359
1412
  pricingTableKey,
1360
1413
  className,
1361
1414
  yearlyLabel = "Save with yearly billing",
@@ -1366,48 +1419,22 @@ function PriceOSPricingTable({
1366
1419
  emptyFallback,
1367
1420
  onCustomCtaClick
1368
1421
  }) {
1369
- const { pricingTable, isLoading, error, createCheckoutSession } = usePricingTable({
1370
- customerId,
1422
+ const { pricingTable, isLoading, error } = usePricingTable({
1371
1423
  pricingTableKey
1372
1424
  });
1373
- const { getBearerToken, backendUrl, logLevel = DEFAULT_LOG_LEVEL } = useContext(PriceOSContext);
1374
- const onCheckout = async ({ price }) => {
1425
+ const { openCheckout } = useCheckout();
1426
+ const { openCustomerPortal } = useCustomerPortal();
1427
+ const onCheckout = async ({ plan, price }) => {
1375
1428
  const urls = getCheckoutUrls(successUrl, cancelUrl);
1376
- const response = await createCheckoutSession({
1429
+ await openCheckout({
1430
+ stripeProductKey: plan.productKey,
1377
1431
  stripePriceId: price.stripePriceId,
1378
1432
  successUrl: urls.successUrl,
1379
1433
  cancelUrl: urls.cancelUrl
1380
1434
  });
1381
- if (typeof window !== "undefined") {
1382
- window.location.assign(response.url);
1383
- }
1384
1435
  };
1385
1436
  const handleCustomCtaClick = onCustomCtaClick ? onCustomCtaClick : async () => {
1386
- const url = buildBillingPortalUrl(backendUrl ?? DEFAULT_BACKEND_URL);
1387
- let bearerToken;
1388
- try {
1389
- bearerToken = getBearerToken ? await getBearerToken() : void 0;
1390
- } catch (error2) {
1391
- logMessage(logLevel, "error", "Failed to resolve bearer token", {
1392
- url,
1393
- error: error2 instanceof Error ? error2.message : String(error2)
1394
- });
1395
- throw error2;
1396
- }
1397
- const returnUrl = typeof window !== "undefined" ? window.location.href : void 0;
1398
- const response = await requestBillingPortalSession(
1399
- fetch,
1400
- url,
1401
- {
1402
- ...customerId ? { customerId } : {},
1403
- ...returnUrl ? { returnUrl } : {}
1404
- },
1405
- logLevel,
1406
- bearerToken
1407
- );
1408
- if (typeof window !== "undefined") {
1409
- window.location.assign(response.url);
1410
- }
1437
+ await openCustomerPortal();
1411
1438
  };
1412
1439
  return createElement(PricingTableView, {
1413
1440
  pricingTable,
@@ -1428,7 +1455,9 @@ export {
1428
1455
  PriceOSProvider,
1429
1456
  PricingTable,
1430
1457
  PricingTableView,
1458
+ useCheckout,
1431
1459
  useCustomer,
1460
+ useCustomerPortal,
1432
1461
  useFeatureAccess,
1433
1462
  usePricingTable,
1434
1463
  useTrackUsage