@reevit/react 0.2.7 → 0.2.9

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.d.mts CHANGED
@@ -23,6 +23,8 @@ interface ReevitCheckoutConfig {
23
23
  metadata?: Record<string, unknown>;
24
24
  /** Payment methods to display */
25
25
  paymentMethods?: PaymentMethod[];
26
+ /** Optional existing payment intent to use instead of creating a new one */
27
+ initialPaymentIntent?: PaymentIntent;
26
28
  }
27
29
  interface ReevitCheckoutCallbacks {
28
30
  /** Called when payment is successful */
@@ -39,6 +41,10 @@ interface ReevitCheckoutProps extends ReevitCheckoutConfig, ReevitCheckoutCallba
39
41
  children?: React.ReactNode;
40
42
  /** Whether to open automatically */
41
43
  autoOpen?: boolean;
44
+ /** Controlled open state */
45
+ isOpen?: boolean;
46
+ /** Callback for open state changes */
47
+ onOpenChange?: (isOpen: boolean) => void;
42
48
  /** Custom theme */
43
49
  theme?: ReevitTheme;
44
50
  /** Custom API base URL (for testing or self-hosted deployments) */
@@ -132,7 +138,7 @@ interface ReevitContextValue {
132
138
  currency: string;
133
139
  }
134
140
  declare function useReevitContext(): ReevitContextValue;
135
- declare function ReevitCheckout({ publicKey, amount, currency, email, phone, reference, metadata, paymentMethods, onSuccess, onError, onClose, onStateChange, children, autoOpen, theme, apiBaseUrl, }: ReevitCheckoutProps): react_jsx_runtime.JSX.Element;
141
+ declare function ReevitCheckout({ publicKey, amount, currency, email, phone, reference, metadata, paymentMethods, onSuccess, onError, onClose, onStateChange, children, autoOpen, isOpen: controlledIsOpen, onOpenChange, theme, apiBaseUrl, }: ReevitCheckoutProps): react_jsx_runtime.JSX.Element;
136
142
 
137
143
  interface PaymentMethodSelectorProps {
138
144
  methods: PaymentMethod[];
package/dist/index.d.ts CHANGED
@@ -23,6 +23,8 @@ interface ReevitCheckoutConfig {
23
23
  metadata?: Record<string, unknown>;
24
24
  /** Payment methods to display */
25
25
  paymentMethods?: PaymentMethod[];
26
+ /** Optional existing payment intent to use instead of creating a new one */
27
+ initialPaymentIntent?: PaymentIntent;
26
28
  }
27
29
  interface ReevitCheckoutCallbacks {
28
30
  /** Called when payment is successful */
@@ -39,6 +41,10 @@ interface ReevitCheckoutProps extends ReevitCheckoutConfig, ReevitCheckoutCallba
39
41
  children?: React.ReactNode;
40
42
  /** Whether to open automatically */
41
43
  autoOpen?: boolean;
44
+ /** Controlled open state */
45
+ isOpen?: boolean;
46
+ /** Callback for open state changes */
47
+ onOpenChange?: (isOpen: boolean) => void;
42
48
  /** Custom theme */
43
49
  theme?: ReevitTheme;
44
50
  /** Custom API base URL (for testing or self-hosted deployments) */
@@ -132,7 +138,7 @@ interface ReevitContextValue {
132
138
  currency: string;
133
139
  }
134
140
  declare function useReevitContext(): ReevitContextValue;
135
- declare function ReevitCheckout({ publicKey, amount, currency, email, phone, reference, metadata, paymentMethods, onSuccess, onError, onClose, onStateChange, children, autoOpen, theme, apiBaseUrl, }: ReevitCheckoutProps): react_jsx_runtime.JSX.Element;
141
+ declare function ReevitCheckout({ publicKey, amount, currency, email, phone, reference, metadata, paymentMethods, onSuccess, onError, onClose, onStateChange, children, autoOpen, isOpen: controlledIsOpen, onOpenChange, theme, apiBaseUrl, }: ReevitCheckoutProps): react_jsx_runtime.JSX.Element;
136
142
 
137
143
  interface PaymentMethodSelectorProps {
138
144
  methods: PaymentMethod[];
package/dist/index.js CHANGED
@@ -279,8 +279,19 @@ function mapToPaymentIntent(response, config) {
279
279
  }
280
280
  function useReevit(options) {
281
281
  const { config, onSuccess, onError, onClose, onStateChange, apiBaseUrl } = options;
282
- const [state, dispatch] = react.useReducer(reevitReducer, initialState);
282
+ const [state, dispatch] = react.useReducer(reevitReducer, {
283
+ ...initialState,
284
+ status: config.initialPaymentIntent ? "ready" : "idle",
285
+ paymentIntent: config.initialPaymentIntent || null
286
+ });
283
287
  const apiClientRef = react.useRef(null);
288
+ const initializingRef = react.useRef(!!config.initialPaymentIntent);
289
+ react.useEffect(() => {
290
+ if (config.initialPaymentIntent && (!state.paymentIntent || state.paymentIntent.id !== config.initialPaymentIntent.id)) {
291
+ dispatch({ type: "INIT_SUCCESS", payload: config.initialPaymentIntent });
292
+ initializingRef.current = true;
293
+ }
294
+ }, [config.initialPaymentIntent, state.paymentIntent?.id]);
284
295
  if (!apiClientRef.current) {
285
296
  apiClientRef.current = new ReevitAPIClient({
286
297
  publicKey: config.publicKey,
@@ -292,10 +303,15 @@ function useReevit(options) {
292
303
  }, [state.status, onStateChange]);
293
304
  const initialize = react.useCallback(
294
305
  async (method) => {
306
+ if (initializingRef.current) {
307
+ return;
308
+ }
309
+ initializingRef.current = true;
295
310
  dispatch({ type: "INIT_START" });
296
311
  try {
297
312
  const apiClient = apiClientRef.current;
298
313
  if (!apiClient) {
314
+ initializingRef.current = false;
299
315
  throw new Error("API client not initialized");
300
316
  }
301
317
  const reference = config.reference || generateReference();
@@ -319,6 +335,7 @@ function useReevit(options) {
319
335
  };
320
336
  dispatch({ type: "INIT_ERROR", payload: noDataError });
321
337
  onError?.(noDataError);
338
+ initializingRef.current = false;
322
339
  return;
323
340
  }
324
341
  const paymentIntent = mapToPaymentIntent(data, { ...config, reference });
@@ -332,6 +349,7 @@ function useReevit(options) {
332
349
  };
333
350
  dispatch({ type: "INIT_ERROR", payload: error });
334
351
  onError?.(error);
352
+ initializingRef.current = false;
335
353
  }
336
354
  },
337
355
  [config, onError, apiBaseUrl]
@@ -396,6 +414,7 @@ function useReevit(options) {
396
414
  [onError]
397
415
  );
398
416
  const reset = react.useCallback(() => {
417
+ initializingRef.current = false;
399
418
  dispatch({ type: "RESET" });
400
419
  }, []);
401
420
  const close = react.useCallback(async () => {
@@ -760,10 +779,23 @@ function ReevitCheckout({
760
779
  // UI
761
780
  children,
762
781
  autoOpen = false,
782
+ isOpen: controlledIsOpen,
783
+ onOpenChange,
763
784
  theme,
764
785
  apiBaseUrl
765
786
  }) {
766
- const [isOpen, setIsOpen] = react.useState(autoOpen);
787
+ const [internalIsOpen, setInternalIsOpen] = react.useState(autoOpen);
788
+ const isOpen = controlledIsOpen !== void 0 ? controlledIsOpen : internalIsOpen;
789
+ const setIsOpen = react.useCallback(
790
+ (value) => {
791
+ if (onOpenChange) {
792
+ onOpenChange(value);
793
+ } else {
794
+ setInternalIsOpen(value);
795
+ }
796
+ },
797
+ [onOpenChange]
798
+ );
767
799
  const [showPSPBridge, setShowPSPBridge] = react.useState(false);
768
800
  const [momoData, setMomoData] = react.useState(null);
769
801
  const {
@@ -801,16 +833,17 @@ function ReevitCheckout({
801
833
  }
802
834
  }, [isOpen, status, initialize]);
803
835
  const handleOpen = react.useCallback(() => {
836
+ if (controlledIsOpen !== void 0) return;
804
837
  setIsOpen(true);
805
838
  setShowPSPBridge(false);
806
839
  setMomoData(null);
807
- }, []);
840
+ }, [controlledIsOpen, setIsOpen]);
808
841
  const handleClose = react.useCallback(() => {
809
842
  closeCheckout();
810
843
  setIsOpen(false);
811
844
  setShowPSPBridge(false);
812
845
  setMomoData(null);
813
- }, [closeCheckout]);
846
+ }, [closeCheckout, setIsOpen]);
814
847
  const handleMethodSelect = react.useCallback(
815
848
  (method) => {
816
849
  selectMethod(method);