diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index c6189f8..c50478d 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -14,38 +14,38 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega private var pendingCollectPaymentMethod: Cancelable? private var currentUpdate: ReaderSoftwareUpdate? private var currentPaymentIntent: PaymentIntent? - + private var readers: [Reader]? - + func logMsg(items: Any...) { print("SWIFT \(items)") } - + func onLogEntry(logline _: String) { // self.notifyListeners("log", data: ["logline": logline]) } - + @objc func initialize(_ call: CAPPluginCall) { DispatchQueue.main.async { Terminal.setTokenProvider(self) Terminal.shared.delegate = self - + Terminal.setLogListener { logline in self.onLogEntry(logline: logline) } // Terminal.shared.logLevel = LogLevel.verbose; - + self.abortDiscoverReaders() self.abortInstallUpdate() - + call.resolve() } } - + @objc func setConnectionToken(_ call: CAPPluginCall) { let token = call.getString("token") ?? "" let errorMessage = call.getString("errorMessage") ?? "" - + if let completion = pendingConnectionTokenCompletionBlock { if !errorMessage.isEmpty { let error = NSError(domain: "io.event1.capacitor-stripe-terminal", @@ -55,38 +55,38 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega } else { completion(token, nil) } - + pendingConnectionTokenCompletionBlock = nil call.resolve() } } - + public func fetchConnectionToken(_ completion: @escaping ConnectionTokenCompletionBlock) { pendingConnectionTokenCompletionBlock = completion notifyListeners("requestConnectionToken", data: [:]) } - + @objc func discoverReaders(_ call: CAPPluginCall) { // Attempt to abort any pending discoverReader calls first. abortDiscoverReaders() - + let simulated = call.getBool("simulated") ?? true let method = UInt(call.getInt("discoveryMethod") ?? 0) let device = UInt(call.getInt("deviceType") ?? 0) let locationId = call.getString("locationId") ?? nil - + let deviceType = DeviceType(rawValue: device) ?? DeviceType.chipper2X let discoveryMethod = DiscoveryMethod(rawValue: method) ?? DiscoveryMethod.bluetoothProximity - + pendingDiscoverReaders = nil - + let config = DiscoveryConfiguration(deviceType: deviceType, discoveryMethod: discoveryMethod, locationId: locationId, simulated: simulated) pendingDiscoverReaders = Terminal.shared.discoverReaders(config, delegate: self, completion: { error in self.pendingDiscoverReaders = nil - + if let error = error { call.error(error.localizedDescription, error) } else { @@ -94,7 +94,7 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega } }) } - + @objc func abortDiscoverReaders(_ call: CAPPluginCall? = nil) { if pendingDiscoverReaders != nil { pendingDiscoverReaders?.cancel { error in @@ -105,21 +105,21 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega } } } - + call?.resolve() } - + @objc func connectReader(_ call: CAPPluginCall) { guard let serialNumber = call.getString("serialNumber") else { call.reject("Must provide a serial number") return } - - guard let selectedReader = self.readers?.first(where: { $0.serialNumber == serialNumber }) else { + + guard let selectedReader = readers?.first(where: { $0.serialNumber == serialNumber }) else { call.reject("No reader found") return } - + // this must be run on the main thread // https://stackoverflow.com/questions/44767778/main-thread-checker-ui-api-called-on-a-background-thread-uiapplication-appli DispatchQueue.main.async { @@ -134,13 +134,13 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega }) } } - + @objc func disconnectReader(_ call: CAPPluginCall) { if Terminal.shared.connectedReader == nil { call.resolve() return } - + DispatchQueue.main.async { Terminal.shared.disconnectReader { error in if let error = error { @@ -151,7 +151,7 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega } } } - + @objc func checkForUpdate(_ call: CAPPluginCall) { Terminal.shared.checkForUpdate { _update, error in self.currentUpdate = _update @@ -164,7 +164,7 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega } } } - + @objc func installUpdate(_ call: CAPPluginCall) { if let update = currentUpdate { pendingInstallUpdate = Terminal.shared.installUpdate(update, delegate: self, completion: { error in @@ -178,7 +178,7 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega }) } } - + @objc func abortInstallUpdate(_ call: CAPPluginCall? = nil) { if pendingInstallUpdate != nil { pendingInstallUpdate?.cancel { error in @@ -189,35 +189,35 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega call?.resolve() } } - + return } - + call?.resolve() } - + @objc func getConnectionStatus(_ call: CAPPluginCall) { call.resolve(["status": Terminal.shared.connectionStatus.rawValue]) } - + @objc func getConnectedReader(_ call: CAPPluginCall) { var reader: Any = [:] if Terminal.shared.connectedReader != nil { reader = StripeTerminalUtils.serializeReader(reader: Terminal.shared.connectedReader!) } - + call.resolve(["reader": reader]) } - + @objc func retrievePaymentIntent(_ call: CAPPluginCall) { guard let clientSecret = call.getString("clientSecret") else { call.reject("Must provide a clientSecret") return } - + Terminal.shared.retrievePaymentIntent(clientSecret: clientSecret) { retrieveResult, retrieveError in self.currentPaymentIntent = retrieveResult - + if let error = retrieveError { call.error(error.localizedDescription, error) } else if let paymentIntent = retrieveResult { @@ -225,7 +225,7 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega } } } - + @objc func abortCollectPaymentMethod(_ call: CAPPluginCall? = nil) { if pendingCollectPaymentMethod != nil { pendingCollectPaymentMethod?.cancel { error in @@ -236,18 +236,18 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega call?.resolve() } } - + return } - + call?.resolve() } - + @objc func collectPaymentMethod(_ call: CAPPluginCall) { if let intent = currentPaymentIntent { pendingCollectPaymentMethod = Terminal.shared.collectPaymentMethod(intent, delegate: self) { collectResult, collectError in self.pendingCollectPaymentMethod = nil - + if let error = collectError { call.error(error.localizedDescription, error) } else if let paymentIntent = collectResult { @@ -259,7 +259,7 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega call.reject("There is no active payment intent. Make sure you called retrievePaymentIntent first") } } - + @objc func processPayment(_ call: CAPPluginCall) { if let intent = currentPaymentIntent { Terminal.shared.processPayment(intent) { paymentIntent, error in @@ -274,48 +274,48 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega call.reject("There is no active payment intent. Make sure you called retrievePaymentIntent first") } } - + @objc func clearCachedCredentials(_ call: CAPPluginCall) { Terminal.shared.clearCachedCredentials() call.resolve() } - + // MARK: DiscoveryDelegate - + public func terminal(_: Terminal, didUpdateDiscoveredReaders readers: [Reader]) { self.readers = readers - + let readersJSON = readers.map { (reader: Reader) -> [String: Any] in StripeTerminalUtils.serializeReader(reader: reader) } - + notifyListeners("readersDiscovered", data: ["readers": readersJSON]) } - + // MARK: TerminalDelegate - + public func terminal(_: Terminal, didReportUnexpectedReaderDisconnect reader: Reader) { logMsg(items: "didReportUnexpectedReaderDisconnect \(reader)") notifyListeners("didReportUnexpectedReaderDisconnect", data: ["reader": StripeTerminalUtils.serializeReader(reader: reader)]) } - + public func terminal(_: Terminal, didChangeConnectionStatus status: ConnectionStatus) { notifyListeners("didChangeConnectionStatus", data: ["status": status.rawValue]) } - + // MARK: ReaderSoftwareUpdateDelegate - + public func terminal(_: Terminal, didReportReaderSoftwareUpdateProgress progress: Float) { notifyListeners("didReportReaderSoftwareUpdateProgress", data: ["progress": progress]) } - + // MARK: ReaderDisplayDelegate - + public func terminal(_: Terminal, didRequestReaderInput inputOptions: ReaderInputOptions = []) { notifyListeners("didRequestReaderInput", data: ["value": inputOptions.rawValue]) } - + public func terminal(_: Terminal, didRequestReaderDisplayMessage displayMessage: ReaderDisplayMessage) { notifyListeners("didRequestReaderDisplayMessage", data: ["value": displayMessage.rawValue]) } diff --git a/ios/Plugin/TerminalUtils.swift b/ios/Plugin/TerminalUtils.swift index 52499d1..1aa4ac4 100644 --- a/ios/Plugin/TerminalUtils.swift +++ b/ios/Plugin/TerminalUtils.swift @@ -9,7 +9,6 @@ import Foundation import StripeTerminal - public class StripeTerminalUtils { static func serializeReader(reader: Reader) -> [String: Any] { let jsonObject: [String: Any] = [ @@ -24,19 +23,19 @@ public class StripeTerminalUtils { "label": reader.label, "simulated": reader.simulated, ] - + return jsonObject } - + static func serializeUpdate(update: ReaderSoftwareUpdate) -> [String: Any] { let jsonObject: [String: Any] = [ "estimatedUpdateTime": ReaderSoftwareUpdate.string(from: update.estimatedUpdateTime), "deviceSoftwareVersion": update.deviceSoftwareVersion, ] - + return jsonObject } - + static func serializePaymentIntent(intent: PaymentIntent) -> [String: Any] { let jsonObject: [String: Any] = [ "stripeId": intent.stripeId, @@ -46,7 +45,7 @@ public class StripeTerminalUtils { "currency": intent.currency, // "metadata": intent.metadata as [String: Any], ] - + return jsonObject } }