Skip to content

Commit

Permalink
chore: Format Swift code
Browse files Browse the repository at this point in the history
  • Loading branch information
nprail committed Jun 28, 2020
1 parent 3707c61 commit 3eb49a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 61 deletions.
110 changes: 55 additions & 55 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -55,46 +55,46 @@ 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 {
call.resolve()
}
})
}

@objc func abortDiscoverReaders(_ call: CAPPluginCall? = nil) {
if pendingDiscoverReaders != nil {
pendingDiscoverReaders?.cancel { error in
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -151,7 +151,7 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega
}
}
}

@objc func checkForUpdate(_ call: CAPPluginCall) {
Terminal.shared.checkForUpdate { _update, error in
self.currentUpdate = _update
Expand All @@ -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
Expand All @@ -178,7 +178,7 @@ public class StripeTerminal: CAPPlugin, ConnectionTokenProvider, DiscoveryDelega
})
}
}

@objc func abortInstallUpdate(_ call: CAPPluginCall? = nil) {
if pendingInstallUpdate != nil {
pendingInstallUpdate?.cancel { error in
Expand All @@ -189,43 +189,43 @@ 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 {
call.resolve(["intent": StripeTerminalUtils.serializePaymentIntent(intent: paymentIntent)])
}
}
}

@objc func abortCollectPaymentMethod(_ call: CAPPluginCall? = nil) {
if pendingCollectPaymentMethod != nil {
pendingCollectPaymentMethod?.cancel { error in
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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])
}
Expand Down
11 changes: 5 additions & 6 deletions ios/Plugin/TerminalUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Foundation
import StripeTerminal


public class StripeTerminalUtils {
static func serializeReader(reader: Reader) -> [String: Any] {
let jsonObject: [String: Any] = [
Expand All @@ -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,
Expand All @@ -46,7 +45,7 @@ public class StripeTerminalUtils {
"currency": intent.currency,
// "metadata": intent.metadata as [String: Any],
]

return jsonObject
}
}

0 comments on commit 3eb49a2

Please sign in to comment.