Skip to content

Commit

Permalink
Bluetooth pairing cancellation error handling (stripe#122)
Browse files Browse the repository at this point in the history
* connectingReader reset on error

* locationId required for connectBluetoothReader

* code style

* onUnexpectedReaderDisconnect sends didFinishDiscoveringReaders event

* DiscoverReaders failure triggers navigation.goBack

* typecheck

* replacing undefined with null

* back to undefined
  • Loading branch information
dhenry-stripe authored and arekkubaczkowski committed Jun 29, 2022
1 parent f8fb9d5 commit cb99c82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
CommonErrorType.Failed.toString(),
"Reader has been disconnected unexpectedly"
)
sendEvent("didUpdateDiscoveredReaders", error)
val result = WritableNativeMap().apply {
putMap("result", error)
}
sendEvent("didFinishDiscoveringReaders", result)
}

override fun onConnectionStatusChange(status: ConnectionStatus) {
Expand Down Expand Up @@ -261,7 +264,15 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
return
}

val locationId = getStringOr(params, "locationId") ?: selectedReader.location?.id.orEmpty()
val locationId = getStringOr(params, "locationId") ?: selectedReader.location?.id ?: run {
promise.resolve(
createError(
CommonErrorType.Failed.toString(),
"You must provide a locationId"
)
)
return
}

val connectionConfig = ConnectionConfiguration.BluetoothConnectionConfiguration(
locationId
Expand Down Expand Up @@ -550,7 +561,7 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
@Suppress("unused")
fun createSetupIntent(params: ReadableMap, promise: Promise) {
val intentParams = getStringOr(params, "customer")?.let { customerId ->
SetupIntentParameters.Builder().setCustomer(customerId).build()
SetupIntentParameters.Builder().setCustomer(customerId).build()
} ?: SetupIntentParameters.NULL

Terminal.getInstance().createSetupIntent(intentParams, object : SetupIntentCallback {
Expand Down Expand Up @@ -849,9 +860,9 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
@ReactMethod
@Suppress("unused")
fun readReusableCard(params: ReadableMap, promise: Promise) {
val reusableCardParams = getStringOr(params, "customer") ?.let { customerId ->
ReadReusableCardParameters.Builder().setCustomer(customerId).build()
} ?: ReadReusableCardParameters.NULL
val reusableCardParams = getStringOr(params, "customer")?.let { customerId ->
ReadReusableCardParameters.Builder().setCustomer(customerId).build()
} ?: ReadReusableCardParameters.NULL

readReusableCardCancelable = Terminal.getInstance()
.readReusableCard(reusableCardParams, object : PaymentMethodCallback {
Expand Down
25 changes: 13 additions & 12 deletions example/src/screens/DiscoverReadersScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Reader,
} from 'stripe-terminal-react-native';
import type { NavigationAction } from '@react-navigation/routers';
import type { StripeError } from 'stripe-terminal-react-native';
import { colors } from '../colors';
import { useNavigation, useRoute } from '@react-navigation/core';
import { Picker } from '@react-native-picker/picker';
Expand Down Expand Up @@ -52,6 +53,7 @@ export default function DiscoverReadersScreen() {
'Discover readers error',
`${finishError.code}, ${finishError.message}`
);
navigation.goBack();
} else {
console.log('onFinishDiscoveringReaders success');
}
Expand Down Expand Up @@ -132,23 +134,22 @@ export default function DiscoverReadersScreen() {
}, []);

const handleConnectReader = async (reader: Reader.Type) => {
let error: StripeError | undefined;
if (discoveryMethod === 'internet') {
const { error } = await handleConnectInternetReader(reader);
if (error) {
Alert.alert(error.code, error.message);
} else if (selectedUpdatePlan !== 'required') {
navigation.goBack();
}
const result = await handleConnectInternetReader(reader);
error = result.error;
} else if (
discoveryMethod === 'bluetoothScan' ||
discoveryMethod === 'bluetoothProximity'
) {
const { error } = await handleConnectBluetoothReader(reader);
if (error) {
Alert.alert(error.code, error.message);
} else if (selectedUpdatePlan !== 'required') {
navigation.goBack();
}
const result = await handleConnectBluetoothReader(reader);
error = result.error;
}
if (error) {
setConnectingReader(undefined);
Alert.alert(error.code, error.message);
} else if (selectedUpdatePlan !== 'required') {
navigation.goBack();
}
};

Expand Down

0 comments on commit cb99c82

Please sign in to comment.