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

connection token error handling #86

Merged
merged 6 commits into from
Feb 23, 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
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ First, create an endpoint on your backend server that creates a new connection t
Next, create a token provider that will fetch connection token from your server and provide it to StripeTerminalProvider as a parameter.
Stripe Terminal SDK will fetch it when it's needed.


```tsx
// App.ts
// Root.ts
import { StripeTerminalProvider } from '@stripe/stripe-terminal-react-native';

function App() {
function Root() {
const fetchTokenProvider = async () => {
const response = await fetch(`${API_URL}/connection_token`, {
method: 'POST',
Expand All @@ -85,12 +86,32 @@ function App() {
logLevel="verbose"
tokenProvider={fetchTokenProvider}
>
<Screen />
<App />
</StripeTerminalProvider>
);
}
```

As a last step, simply call `initialize` method from `useStripeTerminal` hook.
Please note that `initialize` method must be called from the nested component of `StripeTerminalProvider`.

```tsx
// App.tsx
function App() {
const { initialize } = useStripeTerminal()

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

return (
<View />
);
}
```

## Configure your app

### Android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
}

@ReactMethod
fun setConnectionToken(token: String, promise: Promise) {
TokenProvider.setConnectionToken(token)
fun setConnectionToken(params: ReadableMap, promise: Promise) {
val token = getStringOr(params, "token")
val error = getStringOr(params, "error")
TokenProvider.setConnectionToken(token, error)
promise.resolve(null)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ class TokenProvider() {

private var connectionTokenCallback: ConnectionTokenCallback? = null

fun setConnectionToken(token: String) {
fun setConnectionToken(token: String?, error: String?) {
try {
connectionTokenCallback?.onSuccess(token)
connectionTokenCallback = null
if (!token.isNullOrEmpty()) {
connectionTokenCallback?.onSuccess(token)
connectionTokenCallback = null
} else {
connectionTokenCallback?.onFailure(
ConnectionTokenException(error ?: "", null)
)
}
} catch (e: Exception) {
connectionTokenCallback?.onFailure(
ConnectionTokenException("Failed to fetch connection token", e)
Expand Down
26 changes: 23 additions & 3 deletions docs/set-up-your-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ This function is called whenever the SDK needs to authenticate with Stripe or th
To get started, provide your token provider implemented in [Step 3](#set-up-the-connection-token-endpoint) to `StripeTerminalProvider` as a prop.

```tsx
// App.ts
// Root.ts
import { StripeTerminalProvider } from '@stripe/stripe-terminal-react-native';

function App() {
function Root() {
const fetchTokenProvider = async () => {
const response = await fetch(`${API_URL}/connection_token`, {
method: 'POST',
Expand All @@ -167,8 +167,28 @@ function App() {
logLevel="verbose"
tokenProvider={fetchTokenProvider}
>
<Screen />
<App />
</StripeTerminalProvider>
);
}
```

As a last step, simply call `initialize` method from `useStripeTerminal` hook.
Please note that `initialize` method must be called from the nested component of `StripeTerminalProvider`.

```tsx
// App.tsx
function App() {
const { initialize } = useStripeTerminal()

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

return (
<View />
);
}
```
4 changes: 2 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppRegistry } from 'react-native';
import App from './src/App';
import Root from './src/Root';
import { name as appName } from './app.json';

AppRegistry.registerComponent(appName, () => App);
AppRegistry.registerComponent(appName, () => Root);
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "arm64 ";
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -810,7 +810,7 @@
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "arm64 ";
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down
Loading