Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logLevel only on StripeTerminalProvider component level #223

Merged
merged 5 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/set-up-your-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,7 @@ function App() {
const { initialize } = useStripeTerminal();

useEffect(() => {
initialize({
logLevel: 'verbose',
});
initialize();
}, [initialize]);

return <View />;
Expand Down
4 changes: 1 addition & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ export default function App() {
const { initialize: initStripe } = useStripeTerminal();

const handlePermissionsSuccess = useCallback(async () => {
const { error } = await initStripe({
logLevel: 'verbose',
});
const { error } = await initStripe();
if (error) {
Alert.alert('StripeTerminal init failed', error.message);
} else {
Expand Down
9 changes: 2 additions & 7 deletions src/components/StripeTerminalContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { createContext } from 'react';
import type { EventEmitter } from 'react-native';
import type {
Reader,
InitParams,
InitializeResultType,
UserCallbacks,
} from '../types';
import type { Reader, InitializeResultType, UserCallbacks } from '../types';

type ContextType = {
loading: boolean;
Expand All @@ -17,7 +12,7 @@ type ContextType = {
setIsInitialized(value: boolean): void;
setConnectedReader(value: Reader.Type | null): void;
setDiscoveredReaders(value: Reader.Type[]): void;
initialize?(params: InitParams): Promise<InitializeResultType>;
initialize?(): Promise<InitializeResultType>;
log(code: string, message?: any): void;
setUserCallbacks(callbacks: UserCallbacks): void;
};
Expand Down
64 changes: 30 additions & 34 deletions src/components/StripeTerminalProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useCallback, useRef, useState, useMemo } from 'react';
import {
Reader,
InitParams,
LogLevel,
StripeError,
PaymentStatus,
Expand Down Expand Up @@ -259,44 +258,41 @@ export function StripeTerminalProvider({
userCallbacks.current = callbacks;
}, []);

const _initialize = useCallback(
async (params: InitParams) => {
setLoading(true);

// test tokenProvider method since native SDK's doesn't fetch it on init
try {
await tokenProvider();
} catch (error) {
console.error(TOKEN_PROVIDER_ERROR_MESSAGE);
console.error(error);

return {
error: {
code: CommonError.Failed,
message: TOKEN_PROVIDER_ERROR_MESSAGE,
},
};
}
const _initialize = useCallback(async () => {
setLoading(true);

const response = await initialize(params);
// test tokenProvider method since native SDK's doesn't fetch it on init
try {
await tokenProvider();
} catch (error) {
console.error(TOKEN_PROVIDER_ERROR_MESSAGE);
console.error(error);

if (response.error) {
log(response.error.code, response.error.message);
} else if (response.reader) {
log('Connected to the reader: ', response.reader);
setConnectedReader(response.reader);
}
return {
error: {
code: CommonError.Failed,
message: TOKEN_PROVIDER_ERROR_MESSAGE,
},
};
}

if (!response.error) {
setIsInitialized(true);
}
const response = await initialize({ logLevel });

setLoading(false);
if (response.error) {
log(response.error.code, response.error.message);
} else if (response.reader) {
log('Connected to the reader: ', response.reader);
setConnectedReader(response.reader);
}

return response;
},
[tokenProvider, setLoading, setConnectedReader, setIsInitialized, log]
);
if (!response.error) {
setIsInitialized(true);
}

setLoading(false);

return response;
}, [logLevel, tokenProvider, log]);

const value = useMemo(
() => ({
Expand Down
34 changes: 15 additions & 19 deletions src/hooks/useStripeTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
CollectSetupIntentPaymentMethodParams,
RefundParams,
ReadReusableCardParamsType,
InitParams,
ConnectEmbeddedParams,
ConnectLocalMobileParams,
UserCallbacks,
Expand Down Expand Up @@ -136,25 +135,22 @@ export function useStripeTerminal(props?: Props) {
// TODO: check why NativeEventListeners are not registering properly if there is no below fix
useListener(FETCH_TOKEN_PROVIDER, () => null);

const _initialize = useCallback(
async (params: InitParams) => {
if (!initialize || typeof initialize !== 'function') {
const errorMessage =
'StripeTerminalProvider component is not found or has not been mounted properly';
log('Failed', errorMessage);
return {
error: {
code: 'Failed',
message: errorMessage,
},
};
}
const _initialize = useCallback(async () => {
if (!initialize || typeof initialize !== 'function') {
const errorMessage =
arekkubaczkowski marked this conversation as resolved.
Show resolved Hide resolved
'StripeTerminalProvider component is not found, has not been mounted properly or SDK has not been initialized properly';
log('Failed', errorMessage);
return {
error: {
code: 'Failed',
message: errorMessage,
},
};
}

const res = initialize(params);
return res;
},
[initialize, log]
);
const res = initialize();
return res;
}, [initialize, log]);

const _cancelDiscovering = useCallback(async () => {
setLoading(true);
Expand Down