Skip to content
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 @@ -344,6 +344,17 @@ class MobilePaymentsSdkReactNativeModule(private val reactContext: ReactApplicat
}
// ---

@ReactMethod
fun retryConnection(id: String, promise: Promise) {
val readerManager = MobilePaymentsSdk.readerManager()
val reader = readerManager.getReader(id)
if (reader == null) {
promise.reject("READER_NOT_FOUND", "No reader found with id '$id'")
return
}
promise.resolve(readerManager.retryConnection(reader).toRetryConnectionResultString())
}

// pairReader
@ReactMethod
fun pairReader(promise: Promise) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.sdk.mobilepayments.cardreader.CardEntryMethod
import com.squareup.sdk.mobilepayments.cardreader.ReaderChangedEvent
import com.squareup.sdk.mobilepayments.cardreader.ReaderInfo
import com.squareup.sdk.mobilepayments.cardreader.ReaderSettings
import com.squareup.sdk.mobilepayments.cardreader.RetryConnectionResult
import com.squareup.sdk.mobilepayments.core.TimeOfDay
import com.squareup.sdk.mobilepayments.core.Result.Failure
import com.squareup.sdk.mobilepayments.payment.AdditionalPaymentMethod
Expand Down Expand Up @@ -484,3 +485,10 @@ fun TimeOfDay.toTimeOfDayMap(): WritableMap {
putInt("minute", minute)
}
}

fun RetryConnectionResult.toRetryConnectionResultString(): String = when (this) {
RetryConnectionResult.STARTING_RECONNECTION -> "STARTING_RECONNECTION"
RetryConnectionResult.READER_ALREADY_CONNECTING_TO_SQUARE -> "READER_ALREADY_CONNECTING_TO_SQUARE"
RetryConnectionResult.UNABLE_TO_RETRY -> "UNABLE_TO_RETRY"
RetryConnectionResult.READER_NOT_FOUND -> "READER_NOT_FOUND"
}
17 changes: 17 additions & 0 deletions ios/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ extension ReaderChange {
}
}

extension RetryConnectionResult {
func mapToString() -> String {
switch self {
case .startingReconnection:
return "STARTING_RECONNECTION"
case .readerAlreadyConnectingToSquare:
return "READER_ALREADY_CONNECTING_TO_SQUARE"
case .readerNotFound:
return "READER_NOT_FOUND"
case .unableToRetry:
fallthrough
@unknown default:
return "UNABLE_TO_RETRY"
}
}
}

extension ReaderSettings {
func toMap() -> NSDictionary {
return [
Expand Down
8 changes: 8 additions & 0 deletions ios/MobilePaymentsSdkReactNative.mm
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ @interface RCT_EXTERN_MODULE(MobilePaymentsSdkReactNative, NSObject)
withResolve:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(retryConnection:(NSString *)id
withResolve:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(rebootReader:(NSString *)id
withResolve:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(isPairingInProgress:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

Expand Down
29 changes: 29 additions & 0 deletions ios/MobilePaymentsSdkReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,35 @@ class MobilePaymentsSdkReactNative: RCTEventEmitter {
resolve(NSNull())
}

@objc(retryConnection:withResolve:withRejecter:)
func retryConnection(id: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let readerId = parseId(readerId: id, reject: reject) else {
return
}
guard let reader = findReader(readerId: readerId) else {
reject("READER_NOT_FOUND", "No reader found with id '\(id)'", nil)
return
}
resolve(mobilePaymentsSDK.readerManager.retryConnection(reader).mapToString())
}

@objc(rebootReader:withResolve:withRejecter:)
func rebootReader(id: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let readerId = parseId(readerId: id, reject: reject) else {
return
}
guard let reader = findReader(readerId: readerId) else {
reject("READER_NOT_FOUND", "No reader found with id '\(id)'", nil)
return
}
guard reader.isRebootable else {
reject("READER_NOT_REBOOTABLE", "Reader '\(id)' does not support rebooting.", nil)
return
}
mobilePaymentsSDK.readerManager.rebootReader(reader)
resolve(NSNull())
}

@objc(isPairingInProgress:withRejecter:)
func isPairingInProgress(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
resolve(mobilePaymentsSDK.readerManager.isPairingInProgress)
Expand Down
13 changes: 13 additions & 0 deletions src/managers/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ReaderInfo,
ReaderSettings,
} from '../models/objects';
import type { RetryConnectionResult } from '../models/enums';

export const readerEventEmitter = new NativeEventEmitter(
MobilePaymentsSdkReactNative
Expand All @@ -31,6 +32,18 @@ export const blink = (id: string): Promise<void> => {
return MobilePaymentsSdkReactNative.blink(id);
};

export const retryConnection = (id: string): Promise<RetryConnectionResult> => {
return MobilePaymentsSdkReactNative.retryConnection(id);
};

export const rebootReader = (id: string): Promise<void> => {
return Platform.select({
ios: () => MobilePaymentsSdkReactNative.rebootReader(id),
android: () =>
Promise.reject(new Error('This feature is only available on iOS.')),
})!();
};

export const isPairingInProgress = (): Promise<boolean> => {
return MobilePaymentsSdkReactNative.isPairingInProgress();
};
Expand Down
7 changes: 7 additions & 0 deletions src/models/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,10 @@ export enum ProcessingMode {
OFFLINE_ONLY = 1,
AUTO_DETECT = 2,
}

export enum RetryConnectionResult {
STARTING_RECONNECTION = 'STARTING_RECONNECTION',
READER_ALREADY_CONNECTING_TO_SQUARE = 'READER_ALREADY_CONNECTING_TO_SQUARE',
UNABLE_TO_RETRY = 'UNABLE_TO_RETRY',
READER_NOT_FOUND = 'READER_NOT_FOUND',
}
Loading