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