@cimplify/sdk 0.8.1 → 0.8.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/dist/index.mjs CHANGED
@@ -715,6 +715,105 @@ var CartOperations = class {
715
715
  }
716
716
  };
717
717
 
718
+ // src/constants.ts
719
+ var CHECKOUT_MODE = {
720
+ LINK: "link",
721
+ GUEST: "guest"
722
+ };
723
+ var ORDER_TYPE = {
724
+ DELIVERY: "delivery",
725
+ PICKUP: "pickup",
726
+ DINE_IN: "dine-in",
727
+ WALK_IN: "walk-in"
728
+ };
729
+ var PAYMENT_METHOD = {
730
+ MOBILE_MONEY: "mobile_money",
731
+ CARD: "card"
732
+ };
733
+ var CHECKOUT_STEP = {
734
+ AUTHENTICATION: "authentication",
735
+ ORDER_DETAILS: "order_details",
736
+ PAYMENT_METHOD: "payment_method",
737
+ PAYMENT: "payment",
738
+ CONFIRMATION: "confirmation"
739
+ };
740
+ var PAYMENT_STATE = {
741
+ INITIAL: "initial",
742
+ PREPARING: "preparing",
743
+ PROCESSING: "processing",
744
+ VERIFYING: "verifying",
745
+ AWAITING_AUTHORIZATION: "awaiting_authorization",
746
+ SUCCESS: "success",
747
+ ERROR: "error",
748
+ TIMEOUT: "timeout"
749
+ };
750
+ var PICKUP_TIME_TYPE = {
751
+ ASAP: "asap",
752
+ SCHEDULED: "scheduled"
753
+ };
754
+ var MOBILE_MONEY_PROVIDER = {
755
+ MTN: "mtn",
756
+ VODAFONE: "vodafone",
757
+ AIRTEL: "airtel"
758
+ };
759
+ var AUTHORIZATION_TYPE = {
760
+ OTP: "otp",
761
+ PIN: "pin",
762
+ PHONE: "phone",
763
+ BIRTHDAY: "birthday",
764
+ ADDRESS: "address"
765
+ };
766
+ var DEVICE_TYPE = {
767
+ MOBILE: "mobile",
768
+ DESKTOP: "desktop",
769
+ TABLET: "tablet"
770
+ };
771
+ var CONTACT_TYPE = {
772
+ PHONE: "phone",
773
+ EMAIL: "email"
774
+ };
775
+ var LINK_QUERY = {
776
+ DATA: "link.data",
777
+ ADDRESSES: "link.addresses",
778
+ MOBILE_MONEY: "link.mobile_money",
779
+ PREFERENCES: "link.preferences",
780
+ SESSIONS: "link.sessions"
781
+ };
782
+ var LINK_MUTATION = {
783
+ CHECK_STATUS: "link.check_status",
784
+ ENROLL: "link.enroll",
785
+ ENROLL_AND_LINK_ORDER: "link.enroll_and_link_order",
786
+ UPDATE_PREFERENCES: "link.update_preferences",
787
+ CREATE_ADDRESS: "link.create_address",
788
+ UPDATE_ADDRESS: "link.update_address",
789
+ DELETE_ADDRESS: "link.delete_address",
790
+ SET_DEFAULT_ADDRESS: "link.set_default_address",
791
+ TRACK_ADDRESS_USAGE: "link.track_address_usage",
792
+ CREATE_MOBILE_MONEY: "link.create_mobile_money",
793
+ DELETE_MOBILE_MONEY: "link.delete_mobile_money",
794
+ SET_DEFAULT_MOBILE_MONEY: "link.set_default_mobile_money",
795
+ TRACK_MOBILE_MONEY_USAGE: "link.track_mobile_money_usage",
796
+ VERIFY_MOBILE_MONEY: "link.verify_mobile_money",
797
+ REVOKE_SESSION: "link.revoke_session",
798
+ REVOKE_ALL_SESSIONS: "link.revoke_all_sessions"
799
+ };
800
+ var AUTH_MUTATION = {
801
+ REQUEST_OTP: "auth.request_otp",
802
+ VERIFY_OTP: "auth.verify_otp"
803
+ };
804
+ var CHECKOUT_MUTATION = {
805
+ PROCESS: "checkout.process"
806
+ };
807
+ var PAYMENT_MUTATION = {
808
+ SUBMIT_AUTHORIZATION: "payment.submit_authorization",
809
+ CHECK_STATUS: "order.poll_payment_status"
810
+ };
811
+ var ORDER_MUTATION = {
812
+ UPDATE_CUSTOMER: "order.update_order_customer"
813
+ };
814
+ var DEFAULT_CURRENCY = "GHS";
815
+ var DEFAULT_COUNTRY = "GHA";
816
+
718
817
  // src/utils/price.ts
719
818
  var CURRENCY_SYMBOLS = {
720
819
  // Major world currencies
@@ -1272,6 +1371,10 @@ async function openCardPopup(provider, checkoutResult, email, currency, signal)
1272
1371
  }
1273
1372
  return { success: false, error: "PROVIDER_UNAVAILABLE" };
1274
1373
  }
1374
+ var VALID_MOBILE_MONEY_PROVIDERS = new Set(Object.values(MOBILE_MONEY_PROVIDER));
1375
+ function isValidMobileMoneyProvider(value) {
1376
+ return VALID_MOBILE_MONEY_PROVIDERS.has(value);
1377
+ }
1275
1378
  function normalizeAuthorizationType(value) {
1276
1379
  return value === "otp" || value === "pin" ? value : void 0;
1277
1380
  }
@@ -1504,6 +1607,16 @@ var CheckoutResolver = class {
1504
1607
  }
1505
1608
  await this.wait(this.pollIntervalMs);
1506
1609
  }
1610
+ try {
1611
+ const finalResult = await this.client.checkout.pollPaymentStatus(input.orderId);
1612
+ if (finalResult.ok) {
1613
+ const normalized = normalizeStatusResponse(finalResult.value);
1614
+ if (normalized.paid || isPaymentStatusSuccess(normalized.status)) {
1615
+ return this.finalizeSuccess(latestCheckoutResult);
1616
+ }
1617
+ }
1618
+ } catch {
1619
+ }
1507
1620
  return this.fail(
1508
1621
  "PAYMENT_TIMEOUT",
1509
1622
  "Payment confirmation timed out. Please retry checkout.",
@@ -1634,7 +1747,7 @@ var CheckoutResolver = class {
1634
1747
  };
1635
1748
  }
1636
1749
  getEnrollmentMobileMoney() {
1637
- if (this.paymentData?.type === "mobile_money" && this.paymentData.phone_number && this.paymentData.provider) {
1750
+ if (this.paymentData?.type === "mobile_money" && this.paymentData.phone_number && this.paymentData.provider && isValidMobileMoneyProvider(this.paymentData.provider)) {
1638
1751
  return {
1639
1752
  phone_number: this.paymentData.phone_number,
1640
1753
  provider: this.paymentData.provider,
@@ -2589,6 +2702,7 @@ var MESSAGE_TYPES = {
2589
2702
  // Parent → Iframe
2590
2703
  INIT: "init",
2591
2704
  SET_TOKEN: "set_token",
2705
+ SET_CART: "set_cart",
2592
2706
  GET_DATA: "get_data",
2593
2707
  REFRESH_TOKEN: "refresh_token",
2594
2708
  LOGOUT: "logout",
@@ -2753,8 +2867,10 @@ var CimplifyElements = class {
2753
2867
  false
2754
2868
  );
2755
2869
  }
2870
+ this.checkoutInProgress = true;
2756
2871
  if (!options.cart_id) {
2757
2872
  console.debug("[cimplify:checkout] BLOCKED: no cart_id");
2873
+ this.checkoutInProgress = false;
2758
2874
  return toCheckoutError(
2759
2875
  "INVALID_CART",
2760
2876
  "A valid cart is required before checkout can start.",
@@ -2763,6 +2879,7 @@ var CimplifyElements = class {
2763
2879
  }
2764
2880
  if (!options.order_type) {
2765
2881
  console.debug("[cimplify:checkout] BLOCKED: no order_type");
2882
+ this.checkoutInProgress = false;
2766
2883
  return toCheckoutError(
2767
2884
  "ORDER_TYPE_REQUIRED",
2768
2885
  "Order type is required before checkout can start.",
@@ -2772,6 +2889,7 @@ var CimplifyElements = class {
2772
2889
  const checkoutElement = this.elements.get(ELEMENT_TYPES.CHECKOUT) || this.elements.get(ELEMENT_TYPES.PAYMENT);
2773
2890
  if (!checkoutElement) {
2774
2891
  console.debug("[cimplify:checkout] BLOCKED: no checkout element");
2892
+ this.checkoutInProgress = false;
2775
2893
  return toCheckoutError(
2776
2894
  "NO_PAYMENT_ELEMENT",
2777
2895
  "Checkout element must be mounted before checkout.",
@@ -2780,6 +2898,7 @@ var CimplifyElements = class {
2780
2898
  }
2781
2899
  if (!checkoutElement.isMounted()) {
2782
2900
  console.debug("[cimplify:checkout] BLOCKED: checkout element not mounted");
2901
+ this.checkoutInProgress = false;
2783
2902
  return toCheckoutError(
2784
2903
  "PAYMENT_NOT_MOUNTED",
2785
2904
  "Checkout element must be mounted before checkout.",
@@ -2791,6 +2910,7 @@ var CimplifyElements = class {
2791
2910
  const authElement = this.elements.get(ELEMENT_TYPES.AUTH);
2792
2911
  if (authElement && !this.accessToken) {
2793
2912
  console.debug("[cimplify:checkout] BLOCKED: auth incomplete");
2913
+ this.checkoutInProgress = false;
2794
2914
  return toCheckoutError(
2795
2915
  "AUTH_INCOMPLETE",
2796
2916
  "Authentication must complete before checkout can start.",
@@ -2828,7 +2948,6 @@ var CimplifyElements = class {
2828
2948
  };
2829
2949
  const timeoutMs = options.timeout_ms ?? 18e4;
2830
2950
  const paymentWindow = checkoutElement.getContentWindow();
2831
- this.checkoutInProgress = true;
2832
2951
  return new Promise((resolve) => {
2833
2952
  let settled = false;
2834
2953
  const cleanup = () => {
@@ -3123,6 +3242,9 @@ var CimplifyElement = class {
3123
3242
  this.sendMessage({ type: MESSAGE_TYPES.GET_DATA });
3124
3243
  });
3125
3244
  }
3245
+ setCart(cart) {
3246
+ this.sendMessage({ type: MESSAGE_TYPES.SET_CART, cart });
3247
+ }
3126
3248
  sendMessage(message) {
3127
3249
  if (this.iframe?.contentWindow) {
3128
3250
  this.iframe.contentWindow.postMessage(message, this.linkUrl);
@@ -3271,6 +3393,7 @@ var ACCESS_TOKEN_STORAGE_KEY = "cimplify_access_token";
3271
3393
  var SESSION_TOKEN_STORAGE_KEY = "cimplify_session_token";
3272
3394
  var ORDER_TOKEN_PREFIX = "cimplify_ot_";
3273
3395
  var SESSION_TOKEN_HEADER = "x-session-token";
3396
+ var ORDER_TOKEN_TTL_MS = 24 * 60 * 60 * 1e3;
3274
3397
  var DEFAULT_TIMEOUT_MS = 3e4;
3275
3398
  var DEFAULT_MAX_RETRIES = 3;
3276
3399
  var DEFAULT_RETRY_DELAY_MS = 1e3;
@@ -3420,12 +3543,27 @@ var CimplifyClient = class {
3420
3543
  }
3421
3544
  setOrderToken(orderId, token) {
3422
3545
  if (typeof window !== "undefined" && window.localStorage) {
3423
- localStorage.setItem(`${ORDER_TOKEN_PREFIX}${orderId}`, token);
3546
+ try {
3547
+ const entry = JSON.stringify({ token, storedAt: Date.now() });
3548
+ localStorage.setItem(`${ORDER_TOKEN_PREFIX}${orderId}`, entry);
3549
+ } catch {
3550
+ }
3424
3551
  }
3425
3552
  }
3426
3553
  getOrderToken(orderId) {
3427
3554
  if (typeof window !== "undefined" && window.localStorage) {
3428
- return localStorage.getItem(`${ORDER_TOKEN_PREFIX}${orderId}`);
3555
+ try {
3556
+ const raw = localStorage.getItem(`${ORDER_TOKEN_PREFIX}${orderId}`);
3557
+ if (!raw) return null;
3558
+ const entry = JSON.parse(raw);
3559
+ if (Date.now() - entry.storedAt > ORDER_TOKEN_TTL_MS) {
3560
+ localStorage.removeItem(`${ORDER_TOKEN_PREFIX}${orderId}`);
3561
+ return null;
3562
+ }
3563
+ return entry.token;
3564
+ } catch {
3565
+ return null;
3566
+ }
3429
3567
  }
3430
3568
  return null;
3431
3569
  }
@@ -3501,10 +3639,13 @@ var CimplifyClient = class {
3501
3639
  }
3502
3640
  saveAccessToken(token) {
3503
3641
  if (typeof window !== "undefined" && window.localStorage) {
3504
- if (token) {
3505
- localStorage.setItem(ACCESS_TOKEN_STORAGE_KEY, token);
3506
- } else {
3507
- localStorage.removeItem(ACCESS_TOKEN_STORAGE_KEY);
3642
+ try {
3643
+ if (token) {
3644
+ localStorage.setItem(ACCESS_TOKEN_STORAGE_KEY, token);
3645
+ } else {
3646
+ localStorage.removeItem(ACCESS_TOKEN_STORAGE_KEY);
3647
+ }
3648
+ } catch {
3508
3649
  }
3509
3650
  }
3510
3651
  }
@@ -3577,6 +3718,19 @@ var CimplifyClient = class {
3577
3718
  });
3578
3719
  return response;
3579
3720
  }
3721
+ if (response.status === 429 && attempt < this.maxRetries) {
3722
+ retryCount++;
3723
+ const retryAfter = response.headers.get("Retry-After");
3724
+ const delay = retryAfter ? Math.min(parseInt(retryAfter, 10) * 1e3, 3e4) || this.retryDelay * Math.pow(2, attempt) : this.retryDelay * Math.pow(2, attempt);
3725
+ this.hooks.onRetry?.({
3726
+ ...context,
3727
+ attempt: retryCount,
3728
+ delayMs: delay,
3729
+ error: new Error(`Rate limited: ${response.status}`)
3730
+ });
3731
+ await sleep(delay);
3732
+ continue;
3733
+ }
3580
3734
  if (response.status >= 400 && response.status < 500) {
3581
3735
  this.hooks.onRequestError?.({
3582
3736
  ...context,
@@ -3927,103 +4081,4 @@ function query(entity) {
3927
4081
  return new QueryBuilder(entity);
3928
4082
  }
3929
4083
 
3930
- // src/constants.ts
3931
- var CHECKOUT_MODE = {
3932
- LINK: "link",
3933
- GUEST: "guest"
3934
- };
3935
- var ORDER_TYPE = {
3936
- DELIVERY: "delivery",
3937
- PICKUP: "pickup",
3938
- DINE_IN: "dine-in",
3939
- WALK_IN: "walk-in"
3940
- };
3941
- var PAYMENT_METHOD = {
3942
- MOBILE_MONEY: "mobile_money",
3943
- CARD: "card"
3944
- };
3945
- var CHECKOUT_STEP = {
3946
- AUTHENTICATION: "authentication",
3947
- ORDER_DETAILS: "order_details",
3948
- PAYMENT_METHOD: "payment_method",
3949
- PAYMENT: "payment",
3950
- CONFIRMATION: "confirmation"
3951
- };
3952
- var PAYMENT_STATE = {
3953
- INITIAL: "initial",
3954
- PREPARING: "preparing",
3955
- PROCESSING: "processing",
3956
- VERIFYING: "verifying",
3957
- AWAITING_AUTHORIZATION: "awaiting_authorization",
3958
- SUCCESS: "success",
3959
- ERROR: "error",
3960
- TIMEOUT: "timeout"
3961
- };
3962
- var PICKUP_TIME_TYPE = {
3963
- ASAP: "asap",
3964
- SCHEDULED: "scheduled"
3965
- };
3966
- var MOBILE_MONEY_PROVIDER = {
3967
- MTN: "mtn",
3968
- VODAFONE: "vodafone",
3969
- AIRTEL: "airtel"
3970
- };
3971
- var AUTHORIZATION_TYPE = {
3972
- OTP: "otp",
3973
- PIN: "pin",
3974
- PHONE: "phone",
3975
- BIRTHDAY: "birthday",
3976
- ADDRESS: "address"
3977
- };
3978
- var DEVICE_TYPE = {
3979
- MOBILE: "mobile",
3980
- DESKTOP: "desktop",
3981
- TABLET: "tablet"
3982
- };
3983
- var CONTACT_TYPE = {
3984
- PHONE: "phone",
3985
- EMAIL: "email"
3986
- };
3987
- var LINK_QUERY = {
3988
- DATA: "link.data",
3989
- ADDRESSES: "link.addresses",
3990
- MOBILE_MONEY: "link.mobile_money",
3991
- PREFERENCES: "link.preferences",
3992
- SESSIONS: "link.sessions"
3993
- };
3994
- var LINK_MUTATION = {
3995
- CHECK_STATUS: "link.check_status",
3996
- ENROLL: "link.enroll",
3997
- ENROLL_AND_LINK_ORDER: "link.enroll_and_link_order",
3998
- UPDATE_PREFERENCES: "link.update_preferences",
3999
- CREATE_ADDRESS: "link.create_address",
4000
- UPDATE_ADDRESS: "link.update_address",
4001
- DELETE_ADDRESS: "link.delete_address",
4002
- SET_DEFAULT_ADDRESS: "link.set_default_address",
4003
- TRACK_ADDRESS_USAGE: "link.track_address_usage",
4004
- CREATE_MOBILE_MONEY: "link.create_mobile_money",
4005
- DELETE_MOBILE_MONEY: "link.delete_mobile_money",
4006
- SET_DEFAULT_MOBILE_MONEY: "link.set_default_mobile_money",
4007
- TRACK_MOBILE_MONEY_USAGE: "link.track_mobile_money_usage",
4008
- VERIFY_MOBILE_MONEY: "link.verify_mobile_money",
4009
- REVOKE_SESSION: "link.revoke_session",
4010
- REVOKE_ALL_SESSIONS: "link.revoke_all_sessions"
4011
- };
4012
- var AUTH_MUTATION = {
4013
- REQUEST_OTP: "auth.request_otp",
4014
- VERIFY_OTP: "auth.verify_otp"
4015
- };
4016
- var CHECKOUT_MUTATION = {
4017
- PROCESS: "checkout.process"
4018
- };
4019
- var PAYMENT_MUTATION = {
4020
- SUBMIT_AUTHORIZATION: "payment.submit_authorization",
4021
- CHECK_STATUS: "order.poll_payment_status"
4022
- };
4023
- var ORDER_MUTATION = {
4024
- UPDATE_CUSTOMER: "order.update_order_customer"
4025
- };
4026
- var DEFAULT_CURRENCY = "GHS";
4027
- var DEFAULT_COUNTRY = "GHA";
4028
-
4029
4084
  export { AUTHORIZATION_TYPE, AUTH_MUTATION, AuthService, BusinessService, CHECKOUT_MODE, CHECKOUT_MUTATION, CHECKOUT_STEP, CONTACT_TYPE, CURRENCY_SYMBOLS, CartOperations, CatalogueQueries, CheckoutService as CheckoutOperations, CheckoutService, CimplifyClient, CimplifyElement, CimplifyElements, CimplifyError, DEFAULT_COUNTRY, DEFAULT_CURRENCY, DEVICE_TYPE, ELEMENT_TYPES, ERROR_HINTS, EVENT_TYPES, ErrorCode, FxService, InventoryService, LINK_MUTATION, LINK_QUERY, LinkService, LiteService, MESSAGE_TYPES, MOBILE_MONEY_PROVIDER, MOBILE_MONEY_PROVIDERS, ORDER_MUTATION, ORDER_TYPE, OrderQueries, PAYMENT_METHOD, PAYMENT_MUTATION, PAYMENT_STATE, PICKUP_TIME_TYPE, QueryBuilder, SchedulingService, ZERO, categorizePaymentError, combine, combineObject, createCimplifyClient, createElements, currencyCode, detectMobileMoneyProvider, enrichError, err, flatMap, formatMoney, formatNumberCompact, formatPrice, formatPriceAdjustment, formatPriceCompact, formatPriceWithTax, formatProductPrice, fromPromise, generateIdempotencyKey, getBasePrice, getCurrencySymbol, getDiscountPercentage, getDisplayPrice, getErrorHint, getMarkupPercentage, getOrElse, getProductCurrency, getTaxAmount, hasTaxInfo, isCimplifyError, isErr, isOk, isOnSale, isPaymentStatusFailure, isPaymentStatusRequiresAction, isPaymentStatusSuccess, isRetryableError, isTaxInclusive, mapError, mapResult, money, moneyFromNumber, normalizePaymentResponse, normalizeStatusResponse, ok, parsePrice, query, toNullable, tryCatch, unwrap };
package/dist/react.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CimplifyClient, X as ProcessCheckoutResult, Z as CheckoutStatus, _ as CheckoutStatusContext, bp as Location, bm as Business, aR as Order, d as QuoteBundleSelectionInput, Q as QuoteCompositeSelectionInput, P as PriceQuote, g as QuoteUiMessage, s as CimplifyElements, w as ElementsOptions, cJ as AuthenticatedData, cF as AddressInfo, cG as PaymentMethodInfo, cL as ElementsCheckoutResult, W as ProcessCheckoutOptions } from './client-bq-xxzM7.mjs';
1
+ import { C as CimplifyClient, X as ProcessCheckoutResult, Z as CheckoutStatus, _ as CheckoutStatusContext, bp as Location, bm as Business, aR as Order, d as QuoteBundleSelectionInput, Q as QuoteCompositeSelectionInput, P as PriceQuote, g as QuoteUiMessage, s as CimplifyElements, w as ElementsOptions, cJ as AuthenticatedData, cF as AddressInfo, cG as PaymentMethodInfo, cL as ElementsCheckoutResult, W as ProcessCheckoutOptions } from './client-Bm2VkZJq.mjs';
2
2
  import React, { ReactNode } from 'react';
3
3
  import { q as Product, d as Pagination, h as CimplifyError, r as ProductWithDetails, K as Category, aw as BundleSelectionInput, a7 as ComponentSelectionInput, N as Collection, X as BundleWithDetails, a3 as CompositeWithDetails, a8 as CompositePriceResult, C as CurrencyCode } from './payment-CLIWNMaP.mjs';
4
4
  import { A as AdSlot, a as AdPosition, e as AdContextValue } from './ads-t3FBTU8p.mjs';
package/dist/react.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CimplifyClient, X as ProcessCheckoutResult, Z as CheckoutStatus, _ as CheckoutStatusContext, bp as Location, bm as Business, aR as Order, d as QuoteBundleSelectionInput, Q as QuoteCompositeSelectionInput, P as PriceQuote, g as QuoteUiMessage, s as CimplifyElements, w as ElementsOptions, cJ as AuthenticatedData, cF as AddressInfo, cG as PaymentMethodInfo, cL as ElementsCheckoutResult, W as ProcessCheckoutOptions } from './client-BJFeYCB2.js';
1
+ import { C as CimplifyClient, X as ProcessCheckoutResult, Z as CheckoutStatus, _ as CheckoutStatusContext, bp as Location, bm as Business, aR as Order, d as QuoteBundleSelectionInput, Q as QuoteCompositeSelectionInput, P as PriceQuote, g as QuoteUiMessage, s as CimplifyElements, w as ElementsOptions, cJ as AuthenticatedData, cF as AddressInfo, cG as PaymentMethodInfo, cL as ElementsCheckoutResult, W as ProcessCheckoutOptions } from './client-1fzEx2NA.js';
2
2
  import React, { ReactNode } from 'react';
3
3
  import { q as Product, d as Pagination, h as CimplifyError, r as ProductWithDetails, K as Category, aw as BundleSelectionInput, a7 as ComponentSelectionInput, N as Collection, X as BundleWithDetails, a3 as CompositeWithDetails, a8 as CompositePriceResult, C as CurrencyCode } from './payment-CLIWNMaP.js';
4
4
  import { A as AdSlot, a as AdPosition, e as AdContextValue } from './ads-t3FBTU8p.js';