Skip to content

Commit

Permalink
connection token error handling (stripe#86)
Browse files Browse the repository at this point in the history
* chore: connection token error handling

* chore: init module refactor

* handle init error

* chore: log error

* fix: ts errors

* fix: variable name
  • Loading branch information
arekkubaczkowski committed Jun 29, 2022
1 parent a33773a commit bfeb301
Show file tree
Hide file tree
Showing 18 changed files with 272 additions and 178 deletions.
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

0 comments on commit bfeb301

Please sign in to comment.