@cimplify/sdk 0.7.14 → 0.8.1

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.mjs CHANGED
@@ -7,7 +7,8 @@ import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
7
7
  var ELEMENT_TYPES = {
8
8
  AUTH: "auth",
9
9
  ADDRESS: "address",
10
- PAYMENT: "payment"
10
+ PAYMENT: "payment",
11
+ CHECKOUT: "checkout"
11
12
  };
12
13
  var MESSAGE_TYPES = {
13
14
  // Parent → Iframe
@@ -29,14 +30,19 @@ var MESSAGE_TYPES = {
29
30
  LOGOUT_COMPLETE: "logout_complete",
30
31
  CONTACT_PROVIDED: "contact_provided",
31
32
  CHECKOUT_STATUS: "checkout_status",
32
- CHECKOUT_COMPLETE: "checkout_complete"
33
+ CHECKOUT_COMPLETE: "checkout_complete",
34
+ ORDER_TYPE_CHANGED: "order_type_changed",
35
+ REQUEST_SUBMIT: "request_submit"
33
36
  };
34
37
  var EVENT_TYPES = {
35
38
  READY: "ready",
36
39
  AUTHENTICATED: "authenticated",
37
40
  REQUIRES_OTP: "requires_otp",
38
41
  ERROR: "error",
39
- CHANGE: "change"};
42
+ CHANGE: "change",
43
+ ORDER_TYPE_CHANGED: "order_type_changed",
44
+ REQUEST_SUBMIT: "request_submit"
45
+ };
40
46
 
41
47
  // src/ads/identity.ts
42
48
  var COOKIE_NAME = "_cimplify_uid";
@@ -337,7 +343,7 @@ function Ad({
337
343
  }
338
344
  );
339
345
  }
340
- var SPACE = { sm: 8, md: 12, lg: 16, xl: 24 };
346
+ var SPACE = { sm: 8};
341
347
  function shellColors(isDark, primaryColor) {
342
348
  return {
343
349
  text: isDark ? "#f4f4f5" : "#1a1a1a",
@@ -349,15 +355,6 @@ function shellColors(isDark, primaryColor) {
349
355
  primary: primaryColor
350
356
  };
351
357
  }
352
- function labelForOrderType(orderType) {
353
- if (orderType === "dine_in") {
354
- return "Dine In";
355
- }
356
- if (orderType === "pickup") {
357
- return "Pickup";
358
- }
359
- return "Delivery";
360
- }
361
358
  function statusToLabel(status) {
362
359
  if (!status) {
363
360
  return "";
@@ -412,8 +409,7 @@ function CimplifyCheckout({
412
409
  const [errorMessage, setErrorMessage] = useState(null);
413
410
  const [resolvedBusinessId, setResolvedBusinessId] = useState(businessId ?? null);
414
411
  const [resolvedCartId, setResolvedCartId] = useState(cartId ?? null);
415
- const addressMountRef = useRef(null);
416
- const paymentMountRef = useRef(null);
412
+ const checkoutMountRef = useRef(null);
417
413
  const elementsRef = useRef(null);
418
414
  const activeCheckoutRef = useRef(null);
419
415
  const initialAppearanceRef = useRef(appearance);
@@ -423,6 +419,8 @@ function CimplifyCheckout({
423
419
  const onCompleteRef = useRef(onComplete);
424
420
  const onErrorRef = useRef(onError);
425
421
  const onStatusChangeRef = useRef(onStatusChange);
422
+ const handleSubmitRef = useRef(async () => {
423
+ });
426
424
  onCompleteRef.current = onComplete;
427
425
  onErrorRef.current = onError;
428
426
  onStatusChangeRef.current = onStatusChange;
@@ -540,14 +538,22 @@ function CimplifyCheckout({
540
538
  linkUrl
541
539
  });
542
540
  elementsRef.current = elements;
543
- const address = elements.create("address", { mode: "shipping" });
544
- const payment = elements.create("payment");
545
- if (addressMountRef.current) {
546
- address.mount(addressMountRef.current);
547
- }
548
- if (paymentMountRef.current) {
549
- payment.mount(paymentMountRef.current);
541
+ const checkout = elements.create("checkout", {
542
+ orderTypes: resolvedOrderTypes,
543
+ defaultOrderType: resolvedOrderTypes[0]
544
+ });
545
+ if (checkoutMountRef.current) {
546
+ checkout.mount(checkoutMountRef.current);
550
547
  }
548
+ checkout.on("order_type_changed", (data) => {
549
+ const typed = data;
550
+ if (typed.orderType) {
551
+ setOrderType(typed.orderType);
552
+ }
553
+ });
554
+ checkout.on("request_submit", () => {
555
+ void handleSubmitRef.current();
556
+ });
551
557
  return () => {
552
558
  activeCheckoutRef.current?.abort();
553
559
  activeCheckoutRef.current = null;
@@ -645,6 +651,7 @@ function CimplifyCheckout({
645
651
  locationId,
646
652
  orderType
647
653
  ]);
654
+ handleSubmitRef.current = handleSubmit;
648
655
  const colors = shellColors(isDark ?? false, primaryColor);
649
656
  if (isInitializing) {
650
657
  return /* @__PURE__ */ jsx("div", { className, "data-cimplify-checkout": "", children: /* @__PURE__ */ jsx("p", { "data-cimplify-status": "", style: { fontSize: 13, color: colors.textSecondary }, children: "Preparing checkout..." }) });
@@ -666,81 +673,7 @@ function CimplifyCheckout({
666
673
  children: "Test mode - no real charges"
667
674
  }
668
675
  ),
669
- /* @__PURE__ */ jsx("div", { "data-cimplify-section": "payment", children: /* @__PURE__ */ jsx("div", { ref: isDemoCheckout ? void 0 : paymentMountRef }) }),
670
- /* @__PURE__ */ jsxs("div", { "data-cimplify-section": "order-type", style: { marginTop: SPACE.xl }, children: [
671
- /* @__PURE__ */ jsx("p", { style: { fontSize: 13, fontWeight: 500, color: colors.textMuted, marginBottom: SPACE.sm }, children: "Order type" }),
672
- /* @__PURE__ */ jsx(
673
- "div",
674
- {
675
- role: "group",
676
- "aria-label": "Order type",
677
- style: {
678
- display: "inline-flex",
679
- border: `1px solid ${colors.border}`,
680
- borderRadius: 8,
681
- padding: 3,
682
- background: colors.surface
683
- },
684
- children: resolvedOrderTypes.map((type) => {
685
- const active = orderType === type;
686
- return /* @__PURE__ */ jsx(
687
- "button",
688
- {
689
- type: "button",
690
- onClick: () => setOrderType(type),
691
- disabled: isSubmitting,
692
- "data-selected": active ? "true" : "false",
693
- style: {
694
- padding: `${SPACE.sm}px ${SPACE.lg}px`,
695
- borderRadius: 6,
696
- border: "none",
697
- background: active ? primaryColor : "transparent",
698
- color: active ? "#ffffff" : colors.textSecondary,
699
- cursor: isSubmitting ? "not-allowed" : "pointer",
700
- opacity: isSubmitting ? 0.6 : 1,
701
- fontSize: 14,
702
- fontWeight: 500,
703
- transition: "all 150ms ease",
704
- WebkitTapHighlightColor: "transparent"
705
- },
706
- children: labelForOrderType(type)
707
- },
708
- type
709
- );
710
- })
711
- }
712
- )
713
- ] }),
714
- /* @__PURE__ */ jsx(
715
- "div",
716
- {
717
- "data-cimplify-section": "address",
718
- style: { marginTop: SPACE.lg, display: orderType === "delivery" ? "block" : "none" },
719
- children: /* @__PURE__ */ jsx("div", { ref: isDemoCheckout ? void 0 : addressMountRef })
720
- }
721
- ),
722
- /* @__PURE__ */ jsx("div", { style: { marginTop: SPACE.xl }, children: /* @__PURE__ */ jsx(
723
- "button",
724
- {
725
- type: "button",
726
- onClick: handleSubmit,
727
- disabled: isSubmitting,
728
- style: {
729
- width: "100%",
730
- padding: `${SPACE.md}px ${SPACE.lg}px`,
731
- borderRadius: 8,
732
- border: "none",
733
- background: isSubmitting ? colors.textMuted : primaryColor,
734
- color: "#ffffff",
735
- cursor: isSubmitting ? "not-allowed" : "pointer",
736
- fontWeight: 600,
737
- fontSize: 16,
738
- transition: "all 150ms ease",
739
- WebkitTapHighlightColor: "transparent"
740
- },
741
- children: isSubmitting ? "Processing..." : "Complete Order"
742
- }
743
- ) }),
676
+ /* @__PURE__ */ jsx("div", { "data-cimplify-section": "checkout", children: /* @__PURE__ */ jsx("div", { ref: isDemoCheckout ? void 0 : checkoutMountRef }) }),
744
677
  status && /* @__PURE__ */ jsx("p", { "data-cimplify-status": "", style: { marginTop: SPACE.sm, fontSize: 13, color: colors.textSecondary }, children: statusText || statusToLabel(status) }),
745
678
  errorMessage && /* @__PURE__ */ jsx("p", { "data-cimplify-error": "", style: { marginTop: SPACE.sm, fontSize: 13, color: colors.error }, children: errorMessage })
746
679
  ] });
@@ -3040,40 +2973,43 @@ var CimplifyElements = class {
3040
2973
  false
3041
2974
  );
3042
2975
  }
3043
- const paymentElement = this.elements.get(ELEMENT_TYPES.PAYMENT);
3044
- if (!paymentElement) {
3045
- console.debug("[cimplify:checkout] BLOCKED: no payment element");
2976
+ const checkoutElement = this.elements.get(ELEMENT_TYPES.CHECKOUT) || this.elements.get(ELEMENT_TYPES.PAYMENT);
2977
+ if (!checkoutElement) {
2978
+ console.debug("[cimplify:checkout] BLOCKED: no checkout element");
3046
2979
  return toCheckoutError(
3047
2980
  "NO_PAYMENT_ELEMENT",
3048
- "Payment element must be mounted before checkout.",
2981
+ "Checkout element must be mounted before checkout.",
3049
2982
  false
3050
2983
  );
3051
2984
  }
3052
- if (!paymentElement.isMounted()) {
3053
- console.debug("[cimplify:checkout] BLOCKED: payment element not mounted");
2985
+ if (!checkoutElement.isMounted()) {
2986
+ console.debug("[cimplify:checkout] BLOCKED: checkout element not mounted");
3054
2987
  return toCheckoutError(
3055
2988
  "PAYMENT_NOT_MOUNTED",
3056
- "Payment element must be mounted before checkout.",
2989
+ "Checkout element must be mounted before checkout.",
3057
2990
  false
3058
2991
  );
3059
2992
  }
3060
- const authElement = this.elements.get(ELEMENT_TYPES.AUTH);
3061
- if (authElement && !this.accessToken) {
3062
- console.debug("[cimplify:checkout] BLOCKED: auth incomplete");
3063
- return toCheckoutError(
3064
- "AUTH_INCOMPLETE",
3065
- "Authentication must complete before checkout can start.",
3066
- true
3067
- );
3068
- }
3069
- const addressElement = this.elements.get(ELEMENT_TYPES.ADDRESS);
3070
- if (addressElement) {
3071
- console.debug("[cimplify:checkout] getting address data...");
3072
- await addressElement.getData();
3073
- console.debug("[cimplify:checkout] address data resolved", this.addressData);
2993
+ const isUnifiedCheckout = this.elements.has(ELEMENT_TYPES.CHECKOUT);
2994
+ if (!isUnifiedCheckout) {
2995
+ const authElement = this.elements.get(ELEMENT_TYPES.AUTH);
2996
+ if (authElement && !this.accessToken) {
2997
+ console.debug("[cimplify:checkout] BLOCKED: auth incomplete");
2998
+ return toCheckoutError(
2999
+ "AUTH_INCOMPLETE",
3000
+ "Authentication must complete before checkout can start.",
3001
+ true
3002
+ );
3003
+ }
3004
+ const addressElement = this.elements.get(ELEMENT_TYPES.ADDRESS);
3005
+ if (addressElement) {
3006
+ console.debug("[cimplify:checkout] getting address data...");
3007
+ await addressElement.getData();
3008
+ console.debug("[cimplify:checkout] address data resolved", this.addressData);
3009
+ }
3010
+ await this.hydrateCustomerData();
3074
3011
  }
3075
- await this.hydrateCustomerData();
3076
- console.debug("[cimplify:checkout] customer data hydrated", { customerData: this.customerData, accountId: this.accountId });
3012
+ console.debug("[cimplify:checkout] customer data hydrated", { customerData: this.customerData, accountId: this.accountId, isUnifiedCheckout });
3077
3013
  const processMessage = {
3078
3014
  type: MESSAGE_TYPES.PROCESS_CHECKOUT,
3079
3015
  cart_id: options.cart_id,
@@ -3084,8 +3020,9 @@ var CimplifyElements = class {
3084
3020
  tip_amount: options.tip_amount,
3085
3021
  pay_currency: options.pay_currency,
3086
3022
  enroll_in_link: options.enroll_in_link ?? true,
3087
- address: this.addressData ?? void 0,
3088
- customer: this.customerData ? {
3023
+ // Unified checkout has its own address/customer state; only pass if available externally
3024
+ address: isUnifiedCheckout ? void 0 : this.addressData ?? void 0,
3025
+ customer: isUnifiedCheckout ? void 0 : this.customerData ? {
3089
3026
  name: this.customerData.name,
3090
3027
  email: this.customerData.email,
3091
3028
  phone: this.customerData.phone
@@ -3094,7 +3031,7 @@ var CimplifyElements = class {
3094
3031
  customer_id: this.customerId ?? void 0
3095
3032
  };
3096
3033
  const timeoutMs = options.timeout_ms ?? 18e4;
3097
- const paymentWindow = paymentElement.getContentWindow();
3034
+ const paymentWindow = checkoutElement.getContentWindow();
3098
3035
  this.checkoutInProgress = true;
3099
3036
  return new Promise((resolve) => {
3100
3037
  let settled = false;
@@ -3132,7 +3069,7 @@ var CimplifyElements = class {
3132
3069
  return;
3133
3070
  }
3134
3071
  if (message.type === MESSAGE_TYPES.LOGOUT_COMPLETE) {
3135
- paymentElement.sendMessage({ type: MESSAGE_TYPES.ABORT_CHECKOUT });
3072
+ checkoutElement.sendMessage({ type: MESSAGE_TYPES.ABORT_CHECKOUT });
3136
3073
  settle(
3137
3074
  toCheckoutError(
3138
3075
  "AUTH_LOST",
@@ -3163,7 +3100,7 @@ var CimplifyElements = class {
3163
3100
  window.addEventListener("message", handleCheckoutMessage);
3164
3101
  }
3165
3102
  abortFn = () => {
3166
- paymentElement.sendMessage({ type: MESSAGE_TYPES.ABORT_CHECKOUT });
3103
+ checkoutElement.sendMessage({ type: MESSAGE_TYPES.ABORT_CHECKOUT });
3167
3104
  settle(
3168
3105
  toCheckoutError(
3169
3106
  "CANCELLED",
@@ -3173,8 +3110,8 @@ var CimplifyElements = class {
3173
3110
  );
3174
3111
  };
3175
3112
  this.activeCheckoutAbort = abortFn;
3176
- console.debug("[cimplify:checkout] sending process_checkout to payment iframe", { cart_id: processMessage.cart_id, order_type: processMessage.order_type, hasCustomer: !!processMessage.customer, hasAddress: !!processMessage.address });
3177
- paymentElement.sendMessage(processMessage);
3113
+ console.debug("[cimplify:checkout] sending process_checkout to checkout iframe", { cart_id: processMessage.cart_id, order_type: processMessage.order_type, hasCustomer: !!processMessage.customer, hasAddress: !!processMessage.address, isUnifiedCheckout });
3114
+ checkoutElement.sendMessage(processMessage);
3178
3115
  console.debug("[cimplify:checkout] postMessage sent, waiting for checkout_complete...");
3179
3116
  });
3180
3117
  })();
@@ -3253,7 +3190,7 @@ var CimplifyElements = class {
3253
3190
  this.customerData = customer;
3254
3191
  this.client.setAccessToken(message.token);
3255
3192
  this.elements.forEach((element, type) => {
3256
- if (type !== ELEMENT_TYPES.AUTH) {
3193
+ if (type !== ELEMENT_TYPES.AUTH && type !== ELEMENT_TYPES.CHECKOUT) {
3257
3194
  element.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token: message.token });
3258
3195
  }
3259
3196
  });
@@ -3443,7 +3380,10 @@ var CimplifyElement = class {
3443
3380
  publicKey,
3444
3381
  demoMode: publicKey.length === 0,
3445
3382
  prefillEmail: this.options.prefillEmail,
3446
- appearance: this.parent.getAppearance()
3383
+ appearance: this.parent.getAppearance(),
3384
+ orderTypes: this.options.orderTypes,
3385
+ defaultOrderType: this.options.defaultOrderType,
3386
+ renderSubmitButton: true
3447
3387
  });
3448
3388
  const token = this.parent.getAccessToken();
3449
3389
  if (token && this.type !== ELEMENT_TYPES.AUTH) {
@@ -3507,6 +3447,12 @@ var CimplifyElement = class {
3507
3447
  case MESSAGE_TYPES.CONTACT_PROVIDED:
3508
3448
  this.parent._setGuestContact(message.contact, message.contactType);
3509
3449
  break;
3450
+ case MESSAGE_TYPES.ORDER_TYPE_CHANGED:
3451
+ this.emit(EVENT_TYPES.ORDER_TYPE_CHANGED, { orderType: message.orderType });
3452
+ break;
3453
+ case MESSAGE_TYPES.REQUEST_SUBMIT:
3454
+ this.emit(EVENT_TYPES.REQUEST_SUBMIT, {});
3455
+ break;
3510
3456
  }
3511
3457
  }
3512
3458
  emit(event, data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cimplify/sdk",
3
- "version": "0.7.14",
3
+ "version": "0.8.1",
4
4
  "description": "Cimplify Commerce SDK for storefronts",
5
5
  "keywords": [
6
6
  "cimplify",