Skip to content

Commit

Permalink
PLA-6911: receive reader update publisher events & bump to 0.14.0 (#37)
Browse files Browse the repository at this point in the history
* PLA-6911: receive reader update publisher events

* PLA-6911: Bump to 0.14.0
  • Loading branch information
sanj-tyro authored Jun 12, 2024
1 parent f8de117 commit 830b096
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 64 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ let package = Package(
]
),
.binaryTarget(name: "TyroTapToPaySDK",
url: "https://tap-to-pay.connect.tyro.com/tyro/0.13.0/TyroTapToPaySDK.xcframework.zip",
checksum: "8c4ff5b192dbc544831f75749423afdd567ecfc169783d5f823c16da7699c571"),
url: "https://tap-to-pay.connect.tyro.com/tyro/0.14.0/TyroTapToPaySDK.xcframework.zip",
checksum: "10de9df9aaf5167d4a9da4127e2b23757da602e8efd46b2d689f91322995450d"),
.binaryTarget(name: "MobileConfiguration",
url: "https://tap-to-pay.connect.tyro.com/ss/1.0.10.0/SSMobileConfiguration.xcframework.zip",
checksum: "60e8d21797cf534875ba31416bb96744b470b69d37c72656e8b432273cffc124"),
Expand Down
107 changes: 45 additions & 62 deletions SampleApp/SampleApp/ViewModels/ContentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,78 @@

import Foundation
import TyroTapToPaySDK
import Combine

class ContentViewModel: ObservableObject {
@Published var state: LoadingState = .loading("Loading...")
@Published var transactionOutcome: TransactionOutcome?
private var cancellables = Set<AnyCancellable>()

var tapToPaySdk: TyroTapToPay
var isConnected: Bool = false

init(tapToPaySdk: TyroTapToPay) {
self.tapToPaySdk = tapToPaySdk

Task(priority: .userInitiated) { [weak self] in
await self?.connect()
}
}

@MainActor
func connect() async {
tapToPaySdk
.readerUpdatePublisher
.sink { event in
switch event {
case .updateStarted:
self.state = .loading("Reader update started")
case .updateInProgress(progress: let progress):
self.state = .loading("Updating reader: \(progress)%")
case .updateCompleted:
self.state = .loading("Reader update complete")
case .updateFailed(error: let error):
self.state = .error("Reader update failed: \(error)")
@unknown default:
self.state = .loading("Unknown reader update event: \(event)")
}
}
.store(in: &cancellables)
do {
await MainActor.run {
self.state = .loading("Connecting to reader...")
}
self.state = .loading("Connecting to reader...")
try await self.tapToPaySdk.connect()
await MainActor.run {
self.state = .ready
self.isConnected = true
}
self.state = .ready
self.isConnected = true
} catch TapToPaySDKError.sessionInitialisationError(let errorMessage) {
await MainActor.run {
self.state = .error("sessionInitialisationError: \(errorMessage)")
}
self.state = .error("sessionInitialisationError: \(errorMessage)")
} catch TapToPaySDKError.retryLimitExhausted(let error) {
await MainActor.run {
self.state = .error("retryLimitExhausted (connectionSecret): \(error.localizedDescription)")
}
self.state = .error("retryLimitExhausted (connectionSecret): \(error.localizedDescription)")
} catch TapToPaySDKError.unableToConnectReader(let errorMessage) {
await MainActor.run {
self.state = .error("unableToConnectReader: \(errorMessage)")
}
self.state = .error("unableToConnectReader: \(errorMessage)")
} catch TapToPaySDKError.discoverReadersError {
await MainActor.run {
self.state = .error("discoverReadersError")
}
self.state = .error("discoverReadersError")
} catch TapToPaySDKError.sdkUpgradeRequiredError(let errorMessage) {
await MainActor.run {
self.state = .error("sdkUpgradeRequiredError: \(errorMessage)")
}
self.state = .error("sdkUpgradeRequiredError: \(errorMessage)")
} catch TapToPaySDKError.fetchSessionCredentialsError(let error) {
await MainActor.run {
self.state = .error("fetchSessionCredentialsError: \(error.localizedDescription)")
}
self.state = .error("fetchSessionCredentialsError: \(error.localizedDescription)")
} catch TapToPaySDKError.fetchSdkDataError(let errorMessage) {
await MainActor.run {
self.state = .error("fetchSdkDataError: \(errorMessage)")
}
self.state = .error("fetchSdkDataError: \(errorMessage)")
} catch TapToPaySDKError.noProximityReaderFound {
await MainActor.run {
self.state = .error("noProximityReaderFound" +
"\n\n" +
"Please ensure you are using an iPhone with Tap to Pay on iPhone hardware capability (iPhone XS or above)"
)
}
self.state = .error("noProximityReaderFound" +
"\n\n" +
"Please ensure you are using an iPhone with Tap to Pay on iPhone hardware capability (iPhone XS or above)")
} catch {
await MainActor.run {
self.state = .error(error.localizedDescription)
}
self.state = .error(error.localizedDescription)
}
}

func reset() {
state = .ready
transactionOutcome = nil
state = .ready
transactionOutcome = nil
}

@MainActor
func startPayment(_ transactionType: TransactionType, _ amount: Decimal) async throws {
await MainActor.run {
self.state = .loading("Processing \(transactionType.rawValue.lowercased())...")
}
self.state = .loading("Processing \(transactionType.rawValue.lowercased())...")
let transactionDetail = TransactionDetail(
amount: formatAmount(amount),
referenceNumber: UUID().uuidString,
Expand All @@ -100,30 +95,18 @@ class ContentViewModel: ObservableObject {
transactionType == .payment
? try await self.tapToPaySdk.startPayment(transactionDetail: transactionDetail)
: try await self.tapToPaySdk.refundPayment(transactionDetail: transactionDetail)
await MainActor.run {
self.state = .success(outcome)
self.transactionOutcome = outcome
}
self.state = .success(outcome)
self.transactionOutcome = outcome
} catch TapToPaySDKError.failedToVerifyConnection {
await MainActor.run {
self.state = .error("failedToVerifyConnection")
}
self.state = .error("failedToVerifyConnection")
} catch TapToPaySDKError.transactionError(let errorMessage) {
await MainActor.run {
self.state = .error("transactionError: \(errorMessage)")
}
self.state = .error("transactionError: \(errorMessage)")
} catch TapToPaySDKError.unableToConnectReader(let errorMessage) {
await MainActor.run {
self.state = .error("unableToConnectReader: \(errorMessage)")
}
self.state = .error("unableToConnectReader: \(errorMessage)")
} catch TapToPaySDKError.invalidParameter(let errorMessage) {
await MainActor.run {
self.state = .error("invalidParameter: \(errorMessage)")
}
self.state = .error("invalidParameter: \(errorMessage)")
} catch {
await MainActor.run {
self.state = .error(error.localizedDescription)
}
self.state = .error(error.localizedDescription)
}
}

Expand Down

0 comments on commit 830b096

Please sign in to comment.