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

Add TTP auto-reconnect support #642

Merged
merged 1 commit into from
Mar 18, 2024
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 @@ -204,7 +204,7 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
val locationId =
params.getString("locationId") ?: selectedReader.location?.id.orEmpty()

val autoReconnectOnUnexpectedDisconnect = if (discoveryMethod == DiscoveryMethod.BLUETOOTH_SCAN || discoveryMethod == DiscoveryMethod.USB) {
val autoReconnectOnUnexpectedDisconnect = if (discoveryMethod == DiscoveryMethod.BLUETOOTH_SCAN || discoveryMethod == DiscoveryMethod.USB || discoveryMethod == DiscoveryMethod.LOCAL_MOBILE) {
getBoolean(params,"autoReconnectOnUnexpectedDisconnect")
} else false

Expand Down Expand Up @@ -709,7 +709,7 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
)
terminal.setReaderSettings(readerSettingsParameters, RNReadSettingsCallback(promise))
}

@ReactMethod
fun addListener(eventName: String?) {
// Set up any upstream listeners or background tasks as necessary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ suspend fun Terminal.connectReader(
connectBluetoothReader(reader, connConfig)
}
}
DiscoveryMethod.LOCAL_MOBILE -> connectLocalMobileReader(reader, LocalMobileConnectionConfiguration(locationId))
DiscoveryMethod.LOCAL_MOBILE -> connectLocalMobileReader(reader, LocalMobileConnectionConfiguration(locationId, autoReconnectOnUnexpectedDisconnect, reconnectionListener))
DiscoveryMethod.INTERNET -> connectInternetReader(reader, InternetConnectionConfiguration())
DiscoveryMethod.HANDOFF -> {
if (listener is HandoffReaderListener)
Expand Down
1 change: 1 addition & 0 deletions dev-app/src/screens/DiscoverReadersScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export default function DiscoverReadersScreen() {
const { reader: connectedReader, error } = await connectLocalMobileReader({
reader,
locationId: selectedLocation?.id,
autoReconnectOnUnexpectedDisconnect: autoReconnectOnUnexpectedDisconnect,
});

if (error) {
Expand Down
11 changes: 7 additions & 4 deletions ios/StripeTerminalReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,16 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
let onBehalfOf: String? = params["onBehalfOf"] as? String
let merchantDisplayName: String? = params["merchantDisplayName"] as? String
let tosAcceptancePermitted: Bool = params["tosAcceptancePermitted"] as? Bool ?? true
let autoReconnectOnUnexpectedDisconnect = params["autoReconnectOnUnexpectedDisconnect"] as? Bool ?? false

let connectionConfig: LocalMobileConnectionConfiguration
do {
connectionConfig = try LocalMobileConnectionConfigurationBuilder(locationId: locationId ?? selectedReader.locationId ?? "")
.setMerchantDisplayName(merchantDisplayName ?? nil)
.setOnBehalfOf(onBehalfOf ?? nil)
.setTosAcceptancePermitted(tosAcceptancePermitted)
.setAutoReconnectOnUnexpectedDisconnect(autoReconnectOnUnexpectedDisconnect)
.setAutoReconnectionDelegate(autoReconnectOnUnexpectedDisconnect ? self : nil)
Comment on lines +315 to +324
Copy link
Collaborator

Choose a reason for hiding this comment

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

not for this PR but we should probably setup the Swift code to use a connectReader extension similar to Android's so they're more closely matched 🤔 just noticed this difference (very low pri though 🤷‍♂️)

.build()
} catch {
resolve(Errors.createError(nsError: error as NSError))
Expand Down Expand Up @@ -965,7 +968,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide \(invalidParams!) parameters."))
return
}

let textToSpeechViaSpeakers = params["textToSpeechViaSpeakers"] as? Bool ?? false
Task {
do {
Expand Down Expand Up @@ -1016,7 +1019,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
let result = Mappers.mapFromReaderDisplayMessage(displayMessage)
sendEvent(withName: ReactNativeConstants.REQUEST_READER_DISPLAY_MESSAGE.rawValue, body: ["result": result])
}

func reader(_ reader: Reader, didDisconnect reason: DisconnectReason) {
let result = Mappers.mapFromReaderDisconnectReason(reason)
sendEvent(withName: ReactNativeConstants.DISCONNECT.rawValue, body: ["reason": result])
Expand Down Expand Up @@ -1065,7 +1068,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
func terminal(_ terminal: Terminal, didForwardPaymentIntent intent: PaymentIntent, error: Error?) {
let result = Mappers.mapFromPaymentIntent(intent, uuid: "")
var body: [String: Any] = ["result": result]

if let nsError = error as NSError? {
let errorAsDictionary = Errors.createError(nsError: nsError)
// createError will return a dictionary of ["error": {the error}]
Expand All @@ -1074,7 +1077,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
error
})
}

sendEvent(withName: ReactNativeConstants.FORWARD_PAYMENT_INTENT.rawValue, body: body)
}

Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type ConnectLocalMobileParams = {
onBehalfOf?: string;
merchantDisplayName?: string;
tosAcceptancePermitted?: boolean;
autoReconnectOnUnexpectedDisconnect?: boolean;
};

export type ConnectHandoffParams = {
Expand Down