From 93ec7d5da4703d51f3f344c503134c2952b1f454 Mon Sep 17 00:00:00 2001 From: Arek Kubaczkowski Date: Thu, 24 Mar 2022 09:55:12 +0100 Subject: [PATCH 1/5] prevent from calling methods before SDK initializing SDK --- src/hooks/useStripeTerminal.tsx | 179 ++++++++++++++++++++++++++------ 1 file changed, 149 insertions(+), 30 deletions(-) diff --git a/src/hooks/useStripeTerminal.tsx b/src/hooks/useStripeTerminal.tsx index 597d439c..bbf3d4e4 100644 --- a/src/hooks/useStripeTerminal.tsx +++ b/src/hooks/useStripeTerminal.tsx @@ -53,6 +53,9 @@ import { NativeModules } from 'react-native'; const { FETCH_TOKEN_PROVIDER } = NativeModules.StripeTerminalReactNative.getConstants(); +const NOT_INITIALIZED_ERROR_MESSAGE = + 'Before any action you must initalize SDK as first'; + /** * useStripeTerminal hook Props */ @@ -119,13 +122,17 @@ export function useStripeTerminal(props?: Props) { const _discoverReaders = useCallback( async (params: DiscoverReadersParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await discoverReaders(params); setLoading(false); return response; }, - [setLoading] + [isInitialized, setLoading] ); // TODO: check why NativeEventListeners are not registering properly if there is no below fix @@ -135,7 +142,7 @@ export function useStripeTerminal(props?: Props) { async (params: InitParams) => { if (!initialize || typeof initialize !== 'function') { const errorMessage = - 'StripeTerminalProvider component is not found or has not been mounted properly'; + 'StripeTerminalProvider component is not found, has not been mounted properly or SDK has not been initialized proerly'; log('Failed', errorMessage); return { error: { @@ -152,6 +159,10 @@ export function useStripeTerminal(props?: Props) { ); const _cancelDiscovering = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await cancelDiscovering(); @@ -161,10 +172,14 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, setDiscoveredReaders]); + }, [setLoading, setDiscoveredReaders, isInitialized]); const _connectBluetoothReader = useCallback( async (params: ConnectBluetoothReaderParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await connectBluetoothReader(params); @@ -176,11 +191,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setConnectedReader, setLoading] + [setConnectedReader, setLoading, isInitialized] ); const _connectInternetReader = useCallback( async (params: ConnectInternetReaderParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await connectInternetReader(params); @@ -192,11 +211,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setConnectedReader, setLoading] + [setConnectedReader, setLoading, isInitialized] ); const _connectUsbReader = useCallback( async (params: ConnectUsbReaderParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await connectUsbReader(params); @@ -208,10 +231,14 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setConnectedReader, setLoading] + [isInitialized, setConnectedReader, setLoading] ); const _disconnectReader = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + return; + } setLoading(true); const response = await disconnectReader(); @@ -224,10 +251,14 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, setConnectedReader, setDiscoveredReaders]); + }, [setLoading, setConnectedReader, setDiscoveredReaders, isInitialized]); const _createPaymentIntent = useCallback( async (params: CreatePaymentIntentParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await createPaymentIntent(params); @@ -236,11 +267,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _collectPaymentMethod = useCallback( async (paymentIntentId: string) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await collectPaymentMethod(paymentIntentId); @@ -249,11 +284,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _retrievePaymentIntent = useCallback( async (clientSecret: string) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await retrievePaymentIntent(clientSecret); @@ -262,11 +301,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _getLocations = useCallback( async (params: GetLocationsParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await getLocations(params); @@ -275,11 +318,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _processPayment = useCallback( async (paymentIntentId: string) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await processPayment(paymentIntentId); @@ -288,11 +335,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _createSetupIntent = useCallback( async (params: CreateSetupIntentParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await createSetupIntent(params); @@ -301,11 +352,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [isInitialized, setLoading] ); const _cancelPaymentIntent = useCallback( async (paymentIntentId: string) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await cancelPaymentIntent(paymentIntentId); @@ -314,10 +369,14 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _installAvailableUpdate = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await installAvailableUpdate(); @@ -325,19 +384,27 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading]); + }, [setLoading, isInitialized]); const _cancelInstallingUpdate = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await cancelInstallingUpdate(); setLoading(false); return response; - }, [setLoading]); + }, [setLoading, isInitialized]); const _setReaderDisplay = useCallback( async (cart: Cart) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await setReaderDisplay(cart); @@ -345,11 +412,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _retrieveSetupIntent = useCallback( async (clientSecret: string) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await retrieveSetupIntent(clientSecret); @@ -358,11 +429,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _collectSetupIntentPaymentMethod = useCallback( async (params: CollectSetupIntentPaymentMethodParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await collectSetupIntentPaymentMethod(params); @@ -370,10 +445,14 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _clearReaderDisplay = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await clearReaderDisplay(); @@ -381,10 +460,14 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading]); + }, [setLoading, isInitialized]); const _cancelSetupIntent = useCallback( async (setupIntentId: string) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await cancelSetupIntent(setupIntentId); @@ -393,11 +476,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _confirmSetupIntent = useCallback( async (setupIntentId: string) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await confirmSetupIntent(setupIntentId); @@ -406,11 +493,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _simulateReaderUpdate = useCallback( async (update: Reader.SimulateUpdateType) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await simulateReaderUpdate(update); @@ -418,11 +509,15 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _collectRefundPaymentMethod = useCallback( async (params: RefundParams) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await collectRefundPaymentMethod(params); @@ -431,10 +526,14 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _processRefund = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await processRefund(); @@ -442,9 +541,13 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading]); + }, [setLoading, isInitialized]); const _clearCachedCredentials = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await clearCachedCredentials(); @@ -452,10 +555,14 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading]); + }, [setLoading, isInitialized]); const _readReusableCard = useCallback( async (params: ReadReusableCardParamsType) => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await readReusableCard(params); @@ -464,10 +571,14 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading] + [setLoading, isInitialized] ); const _cancelCollectPaymentMethod = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await cancelCollectPaymentMethod(); @@ -475,9 +586,13 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading]); + }, [setLoading, isInitialized]); const _cancelCollectSetupIntent = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await cancelCollectSetupIntent(); @@ -485,9 +600,13 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading]); + }, [isInitialized, setLoading]); const _cancelReadReusableCard = useCallback(async () => { + if (!isInitialized) { + console.error(NOT_INITIALIZED_ERROR_MESSAGE); + throw Error(NOT_INITIALIZED_ERROR_MESSAGE); + } setLoading(true); const response = await cancelReadReusableCard(); @@ -495,7 +614,7 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading]); + }, [isInitialized, setLoading]); return { initialize: _initialize, From 415248c5eccfc0975bdb6a5131c6c96fc48807a5 Mon Sep 17 00:00:00 2001 From: Arek Kubaczkowski Date: Mon, 28 Mar 2022 08:56:41 +0200 Subject: [PATCH 2/5] improve error message --- src/hooks/useStripeTerminal.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useStripeTerminal.tsx b/src/hooks/useStripeTerminal.tsx index bbf3d4e4..fd04bcff 100644 --- a/src/hooks/useStripeTerminal.tsx +++ b/src/hooks/useStripeTerminal.tsx @@ -54,7 +54,7 @@ const { FETCH_TOKEN_PROVIDER } = NativeModules.StripeTerminalReactNative.getConstants(); const NOT_INITIALIZED_ERROR_MESSAGE = - 'Before any action you must initalize SDK as first'; + 'First initialize the Stripe Terminal SDK before performing any action'; /** * useStripeTerminal hook Props From 6f4647df5a64b16634a93e40a8fd0bfa31473fd2 Mon Sep 17 00:00:00 2001 From: Arek Kubaczkowski Date: Tue, 29 Mar 2022 08:13:33 +0200 Subject: [PATCH 3/5] isInitialized as a util function --- src/hooks/useStripeTerminal.tsx | 118 ++++++++++++++++---------------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/src/hooks/useStripeTerminal.tsx b/src/hooks/useStripeTerminal.tsx index fd04bcff..40d420dc 100644 --- a/src/hooks/useStripeTerminal.tsx +++ b/src/hooks/useStripeTerminal.tsx @@ -92,6 +92,8 @@ export function useStripeTerminal(props?: Props) { setUserCallbacks, } = useContext(StripeTerminalContext); + const _isInitialized = useCallback(() => isInitialized, [isInitialized]); + const { onUpdateDiscoveredReaders, onFinishDiscoveringReaders, @@ -122,7 +124,7 @@ export function useStripeTerminal(props?: Props) { const _discoverReaders = useCallback( async (params: DiscoverReadersParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -132,7 +134,7 @@ export function useStripeTerminal(props?: Props) { return response; }, - [isInitialized, setLoading] + [_isInitialized, setLoading] ); // TODO: check why NativeEventListeners are not registering properly if there is no below fix @@ -159,7 +161,7 @@ export function useStripeTerminal(props?: Props) { ); const _cancelDiscovering = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -172,11 +174,11 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, setDiscoveredReaders, isInitialized]); + }, [setLoading, setDiscoveredReaders, _isInitialized]); const _connectBluetoothReader = useCallback( async (params: ConnectBluetoothReaderParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -191,12 +193,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setConnectedReader, setLoading, isInitialized] + [setConnectedReader, setLoading, _isInitialized] ); const _connectInternetReader = useCallback( async (params: ConnectInternetReaderParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -211,12 +213,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setConnectedReader, setLoading, isInitialized] + [setConnectedReader, setLoading, _isInitialized] ); const _connectUsbReader = useCallback( async (params: ConnectUsbReaderParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -231,11 +233,11 @@ export function useStripeTerminal(props?: Props) { return response; }, - [isInitialized, setConnectedReader, setLoading] + [_isInitialized, setConnectedReader, setLoading] ); const _disconnectReader = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); return; } @@ -251,11 +253,11 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, setConnectedReader, setDiscoveredReaders, isInitialized]); + }, [setLoading, setConnectedReader, setDiscoveredReaders, _isInitialized]); const _createPaymentIntent = useCallback( async (params: CreatePaymentIntentParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -267,12 +269,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _collectPaymentMethod = useCallback( async (paymentIntentId: string) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -284,12 +286,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _retrievePaymentIntent = useCallback( async (clientSecret: string) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -301,12 +303,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _getLocations = useCallback( async (params: GetLocationsParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -318,12 +320,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _processPayment = useCallback( async (paymentIntentId: string) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -335,12 +337,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _createSetupIntent = useCallback( async (params: CreateSetupIntentParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -352,12 +354,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [isInitialized, setLoading] + [_isInitialized, setLoading] ); const _cancelPaymentIntent = useCallback( async (paymentIntentId: string) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -369,11 +371,11 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _installAvailableUpdate = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -384,10 +386,10 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, isInitialized]); + }, [setLoading, _isInitialized]); const _cancelInstallingUpdate = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -397,11 +399,11 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, isInitialized]); + }, [setLoading, _isInitialized]); const _setReaderDisplay = useCallback( async (cart: Cart) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -412,12 +414,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _retrieveSetupIntent = useCallback( async (clientSecret: string) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -429,12 +431,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _collectSetupIntentPaymentMethod = useCallback( async (params: CollectSetupIntentPaymentMethodParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -445,11 +447,11 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _clearReaderDisplay = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -460,11 +462,11 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, isInitialized]); + }, [setLoading, _isInitialized]); const _cancelSetupIntent = useCallback( async (setupIntentId: string) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -476,12 +478,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _confirmSetupIntent = useCallback( async (setupIntentId: string) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -493,12 +495,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _simulateReaderUpdate = useCallback( async (update: Reader.SimulateUpdateType) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -509,12 +511,12 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _collectRefundPaymentMethod = useCallback( async (params: RefundParams) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -526,11 +528,11 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _processRefund = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -541,10 +543,10 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, isInitialized]); + }, [setLoading, _isInitialized]); const _clearCachedCredentials = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -555,11 +557,11 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, isInitialized]); + }, [setLoading, _isInitialized]); const _readReusableCard = useCallback( async (params: ReadReusableCardParamsType) => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -571,11 +573,11 @@ export function useStripeTerminal(props?: Props) { return response; }, - [setLoading, isInitialized] + [setLoading, _isInitialized] ); const _cancelCollectPaymentMethod = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -586,10 +588,10 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [setLoading, isInitialized]); + }, [setLoading, _isInitialized]); const _cancelCollectSetupIntent = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -600,10 +602,10 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [isInitialized, setLoading]); + }, [_isInitialized, setLoading]); const _cancelReadReusableCard = useCallback(async () => { - if (!isInitialized) { + if (!_isInitialized()) { console.error(NOT_INITIALIZED_ERROR_MESSAGE); throw Error(NOT_INITIALIZED_ERROR_MESSAGE); } @@ -614,7 +616,7 @@ export function useStripeTerminal(props?: Props) { setLoading(false); return response; - }, [isInitialized, setLoading]); + }, [_isInitialized, setLoading]); return { initialize: _initialize, From 87baf866a47d651bdc5d2088808890ea7943ef72 Mon Sep 17 00:00:00 2001 From: Arek Kubaczkowski Date: Thu, 31 Mar 2022 08:39:22 +0200 Subject: [PATCH 4/5] improvements --- example/src/App.tsx | 7 ++++++- src/hooks/useStripeTerminal.tsx | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 029f6518..1c5eb1ae 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -167,11 +167,16 @@ export default function App() { }; const handlePermissionsSuccess = async () => { - const { error } = await initStripe({ + const { error, reader } = await initStripe({ logLevel: 'verbose', }); if (error) { Alert.alert('StripeTerminal init failed', error.message); + } else if (reader) { + console.log( + 'StripeTerminal has been initialized properly and connected to the reader', + reader + ); } else { console.log('StripeTerminal has been initialized properly'); } diff --git a/src/hooks/useStripeTerminal.tsx b/src/hooks/useStripeTerminal.tsx index 40d420dc..e38efc6d 100644 --- a/src/hooks/useStripeTerminal.tsx +++ b/src/hooks/useStripeTerminal.tsx @@ -146,15 +146,17 @@ export function useStripeTerminal(props?: Props) { const errorMessage = 'StripeTerminalProvider component is not found, has not been mounted properly or SDK has not been initialized proerly'; log('Failed', errorMessage); + return { error: { code: 'Failed', message: errorMessage, }, + reader: undefined, }; } - const res = initialize(params); + const res = await initialize(params); return res; }, [initialize, log] From 42255da628baed822d11c9fa02d8fecdfc56b3c9 Mon Sep 17 00:00:00 2001 From: Arek Kubaczkowski Date: Mon, 4 Apr 2022 11:04:56 +0200 Subject: [PATCH 5/5] remove direct functions export --- README.md | 4 ---- src/index.tsx | 1 - 2 files changed, 5 deletions(-) diff --git a/README.md b/README.md index 3b876c3e..a654a807 100644 --- a/README.md +++ b/README.md @@ -364,17 +364,13 @@ function App() { Stripe Terminal SDK provides dedicated hook which exposes bunch of methods and props to be used within your App. Additionally, you have access to the internal state of SDK that contains information about the current connection, discovered readers and loading state. -Alternatively, you can import all of the functions directly from the module but keep in mind that you will lose the access to SDK state. - ```tsx // Screen.ts import { useStripeTerminal } from '@stripe/stripe-terminal-react-native'; -// Alternatively you can import the methods directly. import { useStripeTerminal, - discoverReaders, } from '@stripe/stripe-terminal-react-native'; export default function PaymentScreen() { diff --git a/src/index.tsx b/src/index.tsx index ac06afa4..8cc8a85a 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,4 @@ export * from './types'; -export * from './functions'; // hooks export {