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.cjs CHANGED
@@ -34,7 +34,9 @@ __export(react_exports, {
34
34
  PriceOSProvider: () => PriceOSProvider,
35
35
  PricingTable: () => PricingTable,
36
36
  PricingTableView: () => PricingTableView,
37
+ useCheckout: () => useCheckout,
37
38
  useCustomer: () => useCustomer,
39
+ useCustomerPortal: () => useCustomerPortal,
38
40
  useFeatureAccess: () => useFeatureAccess,
39
41
  usePricingTable: () => usePricingTable,
40
42
  useTrackUsage: () => useTrackUsage
@@ -61,12 +63,9 @@ var buildFeatureAccessUrl = (backendUrl) => {
61
63
  const normalizedBase = normalizeBaseUrl(backendUrl);
62
64
  return `${normalizedBase}/v1/feature-access`;
63
65
  };
64
- var buildPricingTableUrl = (backendUrl, customerId, pricingTableKey) => {
66
+ var buildPricingTableUrl = (backendUrl, pricingTableKey) => {
65
67
  const normalizedBase = normalizeBaseUrl(backendUrl);
66
68
  const params = new URLSearchParams();
67
- if (customerId && customerId.trim().length > 0) {
68
- params.set("customerId", customerId.trim());
69
- }
70
69
  if (pricingTableKey && pricingTableKey.trim().length > 0) {
71
70
  params.set("pricingTableKey", pricingTableKey.trim());
72
71
  }
@@ -77,18 +76,17 @@ var buildCheckoutUrl = (backendUrl) => {
77
76
  const normalizedBase = normalizeBaseUrl(backendUrl);
78
77
  return `${normalizedBase}/v1/checkout`;
79
78
  };
80
- var buildBillingPortalUrl = (backendUrl) => {
79
+ var buildCustomerPortalUrl = (backendUrl) => {
81
80
  const normalizedBase = normalizeBaseUrl(backendUrl);
82
- return `${normalizedBase}/v1/portal`;
81
+ return `${normalizedBase}/v1/customer-portal`;
83
82
  };
84
83
  var buildTrackUsageUrl = (backendUrl) => {
85
84
  const normalizedBase = normalizeBaseUrl(backendUrl);
86
85
  return `${normalizedBase}/v1/usage`;
87
86
  };
88
87
  var resolveIdempotencyKey = (body) => {
89
- const providedEventKey = typeof body.eventKey === "string" ? body.eventKey.trim() : "";
90
88
  const providedIdempotencyKey = typeof body.idempotencyKey === "string" ? String(body.idempotencyKey).trim() : "";
91
- return providedIdempotencyKey || providedEventKey;
89
+ return providedIdempotencyKey;
92
90
  };
93
91
  var normalizeTrackUsageInput = (input) => {
94
92
  if (typeof input === "string") {
@@ -160,7 +158,6 @@ var requestTrackUsage = async (fetchFn, url, body, logLevel, bearerToken) => {
160
158
  ...idempotencyKey ? { idempotencyKey } : {},
161
159
  amount: body.amount ?? 1
162
160
  };
163
- delete payload.eventKey;
164
161
  const response = await fetchFn(url, {
165
162
  method: "POST",
166
163
  headers,
@@ -272,7 +269,7 @@ var requestPricingTable = async (fetchFn, url, logLevel, bearerToken) => {
272
269
  var requestCheckoutSession = async (fetchFn, url, body, logLevel, bearerToken) => {
273
270
  logMessage(logLevel, "debug", "Creating checkout session", {
274
271
  url,
275
- stripePriceId: body.stripePriceId,
272
+ stripeProductKey: body.stripeProductKey,
276
273
  hasCustomerId: Boolean(body.customerId)
277
274
  });
278
275
  const headers = {
@@ -314,8 +311,8 @@ var requestCheckoutSession = async (fetchFn, url, body, logLevel, bearerToken) =
314
311
  }
315
312
  return data;
316
313
  };
317
- var requestBillingPortalSession = async (fetchFn, url, body, logLevel, bearerToken) => {
318
- logMessage(logLevel, "debug", "Creating billing portal session", {
314
+ var requestCustomerPortalSession = async (fetchFn, url, body, logLevel, bearerToken) => {
315
+ logMessage(logLevel, "debug", "Creating customer portal session", {
319
316
  url,
320
317
  hasCustomerId: Boolean(body.customerId)
321
318
  });
@@ -328,7 +325,7 @@ var requestBillingPortalSession = async (fetchFn, url, body, logLevel, bearerTok
328
325
  headers,
329
326
  body: JSON.stringify(body)
330
327
  });
331
- logMessage(logLevel, "info", "Billing portal request completed", { url, status: response.status });
328
+ logMessage(logLevel, "info", "Customer portal request completed", { url, status: response.status });
332
329
  const text = await response.text();
333
330
  let data = null;
334
331
  let parseFailed = false;
@@ -337,7 +334,7 @@ var requestBillingPortalSession = async (fetchFn, url, body, logLevel, bearerTok
337
334
  data = JSON.parse(text);
338
335
  } catch {
339
336
  parseFailed = true;
340
- logMessage(logLevel, "warn", "Billing portal response is not JSON", {
337
+ logMessage(logLevel, "warn", "Customer portal response is not JSON", {
341
338
  url,
342
339
  status: response.status,
343
340
  bodyPreview: text.slice(0, 300)
@@ -346,7 +343,7 @@ var requestBillingPortalSession = async (fetchFn, url, body, logLevel, bearerTok
346
343
  }
347
344
  if (!response.ok) {
348
345
  const message = !parseFailed && data && typeof data === "object" && "error" in data ? String(data.error) : text || response.statusText;
349
- logMessage(logLevel, "error", "Billing portal request failed", {
346
+ logMessage(logLevel, "error", "Customer portal request failed", {
350
347
  url,
351
348
  status: response.status,
352
349
  message
@@ -490,52 +487,111 @@ function useTrackUsage() {
490
487
  }
491
488
  return { trackUsage };
492
489
  }
493
- function usePricingTable(options = {}) {
494
- const { enabled = true, customerId, pricingTableKey, swr } = options;
490
+ function useCheckout(options = {}) {
491
+ const { customerId } = options;
495
492
  const { getBearerToken, backendUrl, logLevel = DEFAULT_LOG_LEVEL } = (0, import_react.useContext)(PriceOSContext);
496
- const fetcher = async (url) => {
497
- let bearerToken;
498
- try {
499
- bearerToken = getBearerToken ? await getBearerToken() : void 0;
500
- } catch (error2) {
501
- logMessage(logLevel, "error", "Failed to resolve bearer token", {
502
- url,
503
- error: error2 instanceof Error ? error2.message : String(error2)
504
- });
505
- throw error2;
506
- }
507
- return requestPricingTable(fetch, url, logLevel, bearerToken);
508
- };
509
- const key = enabled ? buildPricingTableUrl(backendUrl ?? DEFAULT_BACKEND_URL, customerId, pricingTableKey) : null;
510
- const { data, error, isLoading, mutate } = (0, import_swr.default)(
511
- key,
512
- fetcher,
513
- swr
514
- );
515
493
  const createCheckoutSession = async (body) => {
516
494
  const url = buildCheckoutUrl(backendUrl ?? DEFAULT_BACKEND_URL);
517
495
  let bearerToken;
518
496
  try {
519
497
  bearerToken = getBearerToken ? await getBearerToken() : void 0;
520
- } catch (error2) {
498
+ } catch (error) {
521
499
  logMessage(logLevel, "error", "Failed to resolve bearer token", {
522
500
  url,
523
- error: error2 instanceof Error ? error2.message : String(error2)
501
+ error: error instanceof Error ? error.message : String(error)
524
502
  });
525
- throw error2;
503
+ throw error;
526
504
  }
527
505
  const resolvedCustomerId = body.customerId ?? customerId;
506
+ const baseCheckoutParams = body.checkoutParams && typeof body.checkoutParams === "object" ? body.checkoutParams : {};
507
+ const checkoutParams = {
508
+ ...baseCheckoutParams,
509
+ ...body.cancelUrl ? { cancel_url: body.cancelUrl } : {},
510
+ ...body.metadata ? { metadata: body.metadata } : {}
511
+ };
528
512
  return requestCheckoutSession(
529
513
  fetch,
530
514
  url,
531
515
  {
532
516
  ...body,
517
+ checkoutParams,
533
518
  ...resolvedCustomerId ? { customerId: resolvedCustomerId } : {}
534
519
  },
535
520
  logLevel,
536
521
  bearerToken
537
522
  );
538
523
  };
524
+ const openCheckout = async (body) => {
525
+ const response = await createCheckoutSession(body);
526
+ if (typeof window !== "undefined") {
527
+ window.location.assign(response.url);
528
+ }
529
+ return response;
530
+ };
531
+ return {
532
+ createCheckoutSession,
533
+ openCheckout
534
+ };
535
+ }
536
+ function useCustomerPortal(options = {}) {
537
+ const { customerId } = options;
538
+ const { getBearerToken, backendUrl, logLevel = DEFAULT_LOG_LEVEL } = (0, import_react.useContext)(PriceOSContext);
539
+ const createCustomerPortalSession = async (body) => {
540
+ const url = buildCustomerPortalUrl(backendUrl ?? DEFAULT_BACKEND_URL);
541
+ let bearerToken;
542
+ try {
543
+ bearerToken = getBearerToken ? await getBearerToken() : void 0;
544
+ } catch (error) {
545
+ logMessage(logLevel, "error", "Failed to resolve bearer token", {
546
+ url,
547
+ error: error instanceof Error ? error.message : String(error)
548
+ });
549
+ throw error;
550
+ }
551
+ const resolvedCustomerId = body?.customerId ?? customerId;
552
+ return requestCustomerPortalSession(
553
+ fetch,
554
+ url,
555
+ resolvedCustomerId ? { customerId: resolvedCustomerId } : {},
556
+ logLevel,
557
+ bearerToken
558
+ );
559
+ };
560
+ const openCustomerPortal = async (body) => {
561
+ const response = await createCustomerPortalSession(body);
562
+ if (typeof window !== "undefined") {
563
+ window.location.assign(response.url);
564
+ }
565
+ return response;
566
+ };
567
+ return {
568
+ createCustomerPortalSession,
569
+ openCustomerPortal
570
+ };
571
+ }
572
+ function usePricingTable(options = {}) {
573
+ const { enabled = true, pricingTableKey, swr } = options;
574
+ const { getBearerToken, backendUrl, logLevel = DEFAULT_LOG_LEVEL } = (0, import_react.useContext)(PriceOSContext);
575
+ const { createCheckoutSession } = useCheckout();
576
+ const fetcher = async (url) => {
577
+ let bearerToken;
578
+ try {
579
+ bearerToken = getBearerToken ? await getBearerToken() : void 0;
580
+ } catch (error2) {
581
+ logMessage(logLevel, "error", "Failed to resolve bearer token", {
582
+ url,
583
+ error: error2 instanceof Error ? error2.message : String(error2)
584
+ });
585
+ throw error2;
586
+ }
587
+ return requestPricingTable(fetch, url, logLevel, bearerToken);
588
+ };
589
+ const key = enabled ? buildPricingTableUrl(backendUrl ?? DEFAULT_BACKEND_URL, pricingTableKey) : null;
590
+ const { data, error, isLoading, mutate } = (0, import_swr.default)(
591
+ key,
592
+ fetcher,
593
+ swr
594
+ );
539
595
  return {
540
596
  pricingTable: data ?? null,
541
597
  error,
@@ -1396,7 +1452,6 @@ function PricingTableView({
1396
1452
  );
1397
1453
  }
1398
1454
  function PriceOSPricingTable({
1399
- customerId,
1400
1455
  pricingTableKey,
1401
1456
  className,
1402
1457
  yearlyLabel = "Save with yearly billing",
@@ -1407,48 +1462,22 @@ function PriceOSPricingTable({
1407
1462
  emptyFallback,
1408
1463
  onCustomCtaClick
1409
1464
  }) {
1410
- const { pricingTable, isLoading, error, createCheckoutSession } = usePricingTable({
1411
- customerId,
1465
+ const { pricingTable, isLoading, error } = usePricingTable({
1412
1466
  pricingTableKey
1413
1467
  });
1414
- const { getBearerToken, backendUrl, logLevel = DEFAULT_LOG_LEVEL } = (0, import_react.useContext)(PriceOSContext);
1415
- const onCheckout = async ({ price }) => {
1468
+ const { openCheckout } = useCheckout();
1469
+ const { openCustomerPortal } = useCustomerPortal();
1470
+ const onCheckout = async ({ plan, price }) => {
1416
1471
  const urls = getCheckoutUrls(successUrl, cancelUrl);
1417
- const response = await createCheckoutSession({
1472
+ await openCheckout({
1473
+ stripeProductKey: plan.productKey,
1418
1474
  stripePriceId: price.stripePriceId,
1419
1475
  successUrl: urls.successUrl,
1420
1476
  cancelUrl: urls.cancelUrl
1421
1477
  });
1422
- if (typeof window !== "undefined") {
1423
- window.location.assign(response.url);
1424
- }
1425
1478
  };
1426
1479
  const handleCustomCtaClick = onCustomCtaClick ? onCustomCtaClick : async () => {
1427
- const url = buildBillingPortalUrl(backendUrl ?? DEFAULT_BACKEND_URL);
1428
- let bearerToken;
1429
- try {
1430
- bearerToken = getBearerToken ? await getBearerToken() : void 0;
1431
- } catch (error2) {
1432
- logMessage(logLevel, "error", "Failed to resolve bearer token", {
1433
- url,
1434
- error: error2 instanceof Error ? error2.message : String(error2)
1435
- });
1436
- throw error2;
1437
- }
1438
- const returnUrl = typeof window !== "undefined" ? window.location.href : void 0;
1439
- const response = await requestBillingPortalSession(
1440
- fetch,
1441
- url,
1442
- {
1443
- ...customerId ? { customerId } : {},
1444
- ...returnUrl ? { returnUrl } : {}
1445
- },
1446
- logLevel,
1447
- bearerToken
1448
- );
1449
- if (typeof window !== "undefined") {
1450
- window.location.assign(response.url);
1451
- }
1480
+ await openCustomerPortal();
1452
1481
  };
1453
1482
  return (0, import_react.createElement)(PricingTableView, {
1454
1483
  pricingTable,
@@ -1470,7 +1499,9 @@ var PricingTable = PriceOSPricingTable;
1470
1499
  PriceOSProvider,
1471
1500
  PricingTable,
1472
1501
  PricingTableView,
1502
+ useCheckout,
1473
1503
  useCustomer,
1504
+ useCustomerPortal,
1474
1505
  useFeatureAccess,
1475
1506
  usePricingTable,
1476
1507
  useTrackUsage