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

Bluetooth pairing cancellation error handling #122

Merged
merged 8 commits into from
Mar 3, 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
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason not to just OR it with null? Also the syntax

let error?: StripeError | null;

will let it be undefined or null

Copy link
Contributor Author

@dhenry-stripe dhenry-stripe Mar 3, 2022

Choose a reason for hiding this comment

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

I swapped it out but StripeError | undefined is required by handleConnectBluetoothReader and handleConnectInternetReader so leaving as 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