-
Notifications
You must be signed in to change notification settings - Fork 121
Stripe Terminal SDK update 4.2.0 #15377
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
Changes from all commits
b18bec9
60e4548
f45e25c
9db12c2
6fe7da7
3fd6a61
88bb3e0
8cb9bae
e19a4a5
d83cbbc
ac16053
0198905
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /// PaymentMethodType defines supported hardware payment types. | ||
| /// Mirrors supported types from StripeTerminal.PaymentMethodType https://stripe.dev/stripe-terminal-ios/docs/Enums/SCPPaymentMethodType.html | ||
| /// | ||
| public enum PaymentMethodType { | ||
| case cardPresent | ||
| case interacPresent | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,11 +142,11 @@ extension StripeCardReaderService: CardReaderService { | |
| throw error | ||
| } | ||
| case .localMobile: | ||
| let localMobileConfig = LocalMobileDiscoveryConfigurationBuilder() | ||
| let tapToPayConfig = TapToPayDiscoveryConfigurationBuilder() | ||
| do { | ||
| config = try localMobileConfig.setSimulated(shouldUseSimulatedCardReader).build() | ||
| config = try tapToPayConfig.setSimulated(shouldUseSimulatedCardReader).build() | ||
| } catch let error as UnderlyingError { | ||
| DDLogError("Failed to start LocalMobileDiscovery. Error:\(String(describing: error.failureReason))") | ||
| DDLogError("Failed to start TapToPayDiscovery. Error:\(String(describing: error.failureReason))") | ||
| throw error | ||
| } catch { | ||
| DDLogError("\(error)") | ||
|
|
@@ -159,8 +159,6 @@ extension StripeCardReaderService: CardReaderService { | |
| throw CardReaderServiceError.bluetoothDenied | ||
| } | ||
|
|
||
| Terminal.shared.delegate = self | ||
|
|
||
| // We're now ready to start discovery, but first we'll check that we're not starting or canceling | ||
| // another discovery process. | ||
| // If we can't grab a lock quickly, let's fail rather than wait indefinitely | ||
|
|
@@ -198,7 +196,7 @@ extension StripeCardReaderService: CardReaderService { | |
| // as the simulator won't have Bluetooth available. | ||
| // If we're using Tap to Pay on iPhone, bluetooth is not required. | ||
| private func shouldSkipBluetoothCheck(discoveryConfiguration: DiscoveryConfiguration) -> Bool { | ||
| shouldUseSimulatedCardReader || discoveryConfiguration.discoveryMethod == .localMobile | ||
| shouldUseSimulatedCardReader || discoveryConfiguration.discoveryMethod == .tapToPay | ||
| } | ||
|
|
||
| public func cancelDiscovery() -> Future <Void, Error> { | ||
|
|
@@ -273,7 +271,7 @@ extension StripeCardReaderService: CardReaderService { | |
| /// https://stripe.dev/stripe-terminal-ios/docs/Classes/SCPTerminal.html#/c:objc(cs)SCPTerminal(im)disconnectReader: | ||
| /// The completion block for disconnect, apparently, is called when the SDK has not really transitioned to an idle state. | ||
| /// Clients might need to dispatch operations that rely on this completion block to start a second operation on the card reader. | ||
| /// (for example, starting a `localMobile` connection after a BlueTooth reader has been disconnected) | ||
| /// (for example, starting a `tapToPay` connection after a BlueTooth reader has been disconnected) | ||
| Terminal.shared.disconnectReader { error in | ||
| DispatchQueue.main.asyncAfter(deadline: .now() + 1) { | ||
| if let error = error { | ||
|
|
@@ -453,7 +451,7 @@ extension StripeCardReaderService: CardReaderService { | |
|
|
||
| connectionAttemptInvalidated = false | ||
| switch stripeReader.deviceType { | ||
| case .appleBuiltIn: | ||
| case .tapToPay: | ||
| return getLocalMobileConfiguration(stripeReader, options: options).flatMap { configuration in | ||
| self.connect(stripeReader, configuration: configuration) | ||
| } | ||
|
|
@@ -480,7 +478,7 @@ extension StripeCardReaderService: CardReaderService { | |
| self.readerLocationProvider?.fetchDefaultLocationID { result in | ||
| switch result { | ||
| case .success(let locationId): | ||
| let buildConfig = BluetoothConnectionConfigurationBuilder(locationId: locationId) | ||
| let buildConfig = BluetoothConnectionConfigurationBuilder(delegate: self, locationId: locationId) | ||
| do { | ||
| let config = try buildConfig.build() | ||
| return promise(.success(config)) | ||
|
|
@@ -497,7 +495,7 @@ extension StripeCardReaderService: CardReaderService { | |
| } | ||
|
|
||
| private func getLocalMobileConfiguration(_ reader: StripeTerminal.Reader, | ||
| options: CardReaderConnectionOptions?) -> Future<LocalMobileConnectionConfiguration, Error> { | ||
| options: CardReaderConnectionOptions?) -> Future<TapToPayConnectionConfiguration, Error> { | ||
| return Future() { [weak self] promise in | ||
| guard let self = self else { | ||
| promise(.failure(CardReaderServiceError.connection())) | ||
|
|
@@ -509,7 +507,7 @@ extension StripeCardReaderService: CardReaderService { | |
| self.readerLocationProvider?.fetchDefaultLocationID { result in | ||
| switch result { | ||
| case .success(let locationId): | ||
| let localMobileConfig = LocalMobileConnectionConfigurationBuilder(locationId: locationId) | ||
| let localMobileConfig = TapToPayConnectionConfigurationBuilder(delegate: self, locationId: locationId) | ||
| localMobileConfig.setMerchantDisplayName(nil) | ||
| localMobileConfig.setOnBehalfOf(nil) | ||
| localMobileConfig.setTosAcceptancePermitted(options?.builtInOptions?.termsOfServiceAcceptancePermitted ?? true) | ||
|
|
@@ -540,7 +538,7 @@ extension StripeCardReaderService: CardReaderService { | |
| return | ||
| } | ||
|
|
||
| Terminal.shared.connectBluetoothReader(reader, delegate: self, connectionConfig: configuration) { [weak self] (reader, error) in | ||
| Terminal.shared.connectReader(reader, connectionConfig: configuration) { [weak self] (reader, error) in | ||
| guard let self = self else { | ||
| promise(.failure(CardReaderServiceError.connection())) | ||
| return | ||
|
|
@@ -573,14 +571,14 @@ extension StripeCardReaderService: CardReaderService { | |
| } | ||
| } | ||
|
|
||
| public func connect(_ reader: StripeTerminal.Reader, configuration: LocalMobileConnectionConfiguration) -> Future <CardReader, Error> { | ||
| public func connect(_ reader: StripeTerminal.Reader, configuration: TapToPayConnectionConfiguration) -> Future <CardReader, Error> { | ||
| return Future { [weak self] promise in | ||
| guard let self = self else { | ||
| promise(.failure(CardReaderServiceError.connection())) | ||
| return | ||
| } | ||
|
|
||
| Terminal.shared.connectLocalMobileReader(reader, delegate: self, connectionConfig: configuration) { [weak self] (reader, error) in | ||
| Terminal.shared.connectReader(reader, connectionConfig: configuration) { [weak self] (reader, error) in | ||
| guard let self = self else { | ||
| promise(.failure(CardReaderServiceError.connection())) | ||
| return | ||
|
|
@@ -873,7 +871,7 @@ extension StripeCardReaderService: DiscoveryDelegate { | |
|
|
||
|
|
||
| // MARK: - ReaderDisplayDelegate. | ||
| extension StripeCardReaderService: BluetoothReaderDelegate { | ||
| extension StripeCardReaderService: MobileReaderDelegate { | ||
| public func reader(_ reader: Reader, didReportAvailableUpdate update: ReaderSoftwareUpdate) { | ||
| softwareUpdateSubject.send(.available) | ||
| } | ||
|
|
@@ -960,29 +958,33 @@ extension StripeCardReaderService: BluetoothReaderDelegate { | |
|
|
||
| connectedReadersSubject.send([connectedReaderWithUpdatedBatteryLevel]) | ||
| } | ||
|
|
||
| public func reader(_ reader: Reader, didDisconnect reason: DisconnectReason) { | ||
| connectedReadersSubject.send([]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering, does TTP delegate have a similar method to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Both It means that when we set delegate to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation, makes sense now. I wasn't aware both delegates extend the same base delegate. |
||
| } | ||
| } | ||
|
|
||
| extension StripeCardReaderService: LocalMobileReaderDelegate { | ||
| public func localMobileReader(_ reader: Reader, didRequestReaderInput inputOptions: ReaderInputOptions = []) { | ||
| extension StripeCardReaderService: TapToPayReaderDelegate { | ||
| public func tapToPayReader(_ reader: Reader, didRequestReaderInput inputOptions: ReaderInputOptions = []) { | ||
| sendReaderEvent(CardReaderEvent.make(stripeReaderInputOptions: inputOptions)) | ||
| } | ||
|
|
||
| public func localMobileReader(_ reader: Reader, didRequestReaderDisplayMessage displayMessage: ReaderDisplayMessage) { | ||
| public func tapToPayReader(_ reader: Reader, didRequestReaderDisplayMessage displayMessage: ReaderDisplayMessage) { | ||
| sendReaderEvent(CardReaderEvent.make(displayMessage: displayMessage)) | ||
| } | ||
|
|
||
|
|
||
| // TODO: use a specific `deviceSetup` in these three functions instead of reusing the softwareUpdateSubject | ||
| // https://github.com/woocommerce/woocommerce-ios/issues/8088 | ||
| public func localMobileReader(_ reader: Reader, didStartInstallingUpdate update: ReaderSoftwareUpdate, cancelable: Cancelable?) { | ||
| public func tapToPayReader(_ reader: Reader, didStartInstallingUpdate update: ReaderSoftwareUpdate, cancelable: Cancelable?) { | ||
| softwareUpdateSubject.send(.started(cancelable: cancelable.map(StripeCancelable.init(cancelable:)))) | ||
| } | ||
|
|
||
| public func localMobileReader(_ reader: Reader, didReportReaderSoftwareUpdateProgress progress: Float) { | ||
| public func tapToPayReader(_ reader: Reader, didReportReaderSoftwareUpdateProgress progress: Float) { | ||
| softwareUpdateSubject.send(.installing(progress: progress)) | ||
| } | ||
|
|
||
| public func localMobileReader(_ reader: Reader, didFinishInstallingUpdate update: ReaderSoftwareUpdate?, error: Error?) { | ||
| public func tapToPayReader(_ reader: Reader, didFinishInstallingUpdate update: ReaderSoftwareUpdate?, error: Error?) { | ||
| if let error = error { | ||
| let underlyingError = Self.logAndDecodeError(error) | ||
| softwareUpdateSubject.send(.failed( | ||
|
|
@@ -1002,18 +1004,11 @@ extension StripeCardReaderService: LocalMobileReaderDelegate { | |
| } | ||
| } | ||
|
|
||
| public func localMobileReaderDidAcceptTermsOfService(_ reader: Reader) { | ||
| public func tapToPayReaderDidAcceptTermsOfService(_ reader: Reader) { | ||
| builtInCardReaderAcceptToSSubject.send(()) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Terminal delegate | ||
| extension StripeCardReaderService: TerminalDelegate { | ||
| public func terminal(_ terminal: Terminal, didReportUnexpectedReaderDisconnect reader: Reader) { | ||
| connectedReadersSubject.send([]) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Reader events | ||
| private extension StripeCardReaderService { | ||
| func sendReaderEvent(_ event: CardReaderEvent) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe we can add a comment potentially with a link to the Stripe doc, as it seems to mirror a subset of
StripeTerminal.PaymentMethodType?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. I will.
I borrowed the approach of
CardReaderTypeandCardReaderType+Stripe, it's a similar type used in a similar way.