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

test tokenProvider on init #211

Merged
merged 4 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 2 additions & 23 deletions example/src/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, Alert, StyleSheet } from 'react-native';
import React from 'react';

import { StripeTerminalProvider } from 'stripe-terminal-react-native';
import App from './App';
import { API_URL } from './Config';

export default function Root() {
const [initialized, setInitialized] = useState(false);

const fetchTokenProvider = async () => {
const response = await fetch(`${API_URL}/connection_token`, {
method: 'POST',
Expand All @@ -20,30 +17,12 @@ export default function Root() {
return secret;
};

useEffect(() => {
// test connection_token endpoint since native SDK's doesn't fetch it on init
async function init() {
try {
await fetchTokenProvider();
setInitialized(true);
} catch (error) {
console.error(error);
Alert.alert("Couldn't fetch connection token!");
}
}
init();
}, []);

return (
<StripeTerminalProvider
logLevel="verbose"
tokenProvider={fetchTokenProvider}
>
{initialized ? (
<App />
) : (
<ActivityIndicator style={StyleSheet.absoluteFillObject} />
)}
<App />
</StripeTerminalProvider>
);
}
26 changes: 20 additions & 6 deletions src/components/StripeTerminalProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useRef, useState } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import type {
Reader,
InitParams,
Expand Down Expand Up @@ -30,6 +30,9 @@ const {

const emitter = new EventEmitter();

const TOKEN_PROVIDER_ERROR_MESSAGE =
"Couldn't fetch connection token. Please check your tokenProvider method";

/**
* @ignore
*/
Expand Down Expand Up @@ -91,19 +94,30 @@ export function StripeTerminalProvider({
[logLevel]
);

useEffect(() => {
// test tokenProvider method since native SDK's doesn't fetch it on init
async function init() {
try {
await tokenProvider();
} catch (error) {
console.error(TOKEN_PROVIDER_ERROR_MESSAGE);
console.error(error);
}
}
init();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const tokenProviderHandler = async () => {
try {
const connectionToken = await tokenProvider();

setConnectionToken(connectionToken);
} catch (error) {
const errorMessage =
"Couldn't fetch connection token. Please check your tokenProvider method";

setConnectionToken(undefined, errorMessage);
setConnectionToken(undefined, TOKEN_PROVIDER_ERROR_MESSAGE);

console.error(error);
console.error(errorMessage);
console.error(TOKEN_PROVIDER_ERROR_MESSAGE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a way for the client code to know that init failed due to a client token not being able to be retrieved, can we throw or somehow return that error to the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdivock-stripe hmm, are you sure? I think this information might be helpful during the implementation, also the original error only says that network failed so it might be misleading, wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, the console.error is helpful, but there's still no error payload returned to init() or propagated to the client code to catch and handle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I misunderstood you at the beginning. I've moved the test logic to initialize method so user has an access to the error details

}
};

Expand Down