Skip to content

Commit 2e592c6

Browse files
authored
Try to address Stripe Terminal SDK crashes when checking local mobile reader support (#14936)
2 parents 9457544 + 726d023 commit 2e592c6

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

WooCommerce/Classes/ViewModels/Order Details/Receipts/ReceiptEligibilityUseCase.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ protocol ReceiptEligibilityUseCaseProtocol {
1010
final class ReceiptEligibilityUseCase: ReceiptEligibilityUseCaseProtocol {
1111
private let stores: StoresManager
1212
private let featureFlagService: FeatureFlagService
13-
private let cardPresentPaymentsOnboarding: CardPresentPaymentsOnboardingUseCaseProtocol
1413

1514
private var siteID: Int64 {
1615
stores.sessionManager.defaultStoreID ?? 0
1716
}
1817

1918
init(stores: StoresManager = ServiceLocator.stores,
20-
cardPresentPaymentsOnboarding: CardPresentPaymentsOnboardingUseCaseProtocol = CardPresentPaymentsOnboardingUseCase(),
2119
featureFlagService: FeatureFlagService = ServiceLocator.featureFlagService) {
2220
self.stores = stores
23-
self.cardPresentPaymentsOnboarding = cardPresentPaymentsOnboarding
2421
self.featureFlagService = featureFlagService
2522
}
2623

WooCommerce/Classes/ViewRelated/Dashboard/Settings/In-Person Payments/CardReaderSupportDeterminer.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
1616
private let configuration: CardPresentPaymentsConfiguration
1717
private let siteID: Int64
1818
private var locationManager: CLLocationManager = CLLocationManager()
19+
private static var deviceSupportsLocalMobileReader: [Int64: ExpiringBool] = [:]
1920

2021
init(siteID: Int64,
2122
configuration: CardPresentPaymentsConfiguration = CardPresentConfigurationLoader().configuration,
@@ -59,7 +60,15 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
5960

6061
@MainActor
6162
func deviceSupportsLocalMobileReader() async -> Bool {
62-
await withCheckedContinuation { continuation in
63+
/// There may be crashes due to multiple consecutive calls checkDeviceSupport
64+
/// Limit the calls to once every 30 seconds and cache the result
65+
///
66+
if let cachedResult = Self.deviceSupportsLocalMobileReader[siteID], !cachedResult.isExpired {
67+
return cachedResult.value
68+
}
69+
70+
71+
let deviceSupportsLocalMobileReader = await withCheckedContinuation { continuation in
6372
let action = CardPresentPaymentAction.checkDeviceSupport(
6473
siteID: siteID,
6574
cardReaderType: .appleBuiltIn,
@@ -69,6 +78,9 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
6978
}
7079
stores.dispatch(action)
7180
}
81+
82+
Self.deviceSupportsLocalMobileReader[siteID] = ExpiringBool(value: deviceSupportsLocalMobileReader, expirationInSeconds: 30)
83+
return deviceSupportsLocalMobileReader
7284
}
7385

7486
@MainActor
@@ -84,3 +96,15 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
8496
}
8597
}
8698
}
99+
100+
// MARK: - Helper for caching local reader device support check
101+
102+
private struct ExpiringBool {
103+
let value: Bool
104+
let expirationInSeconds: Int
105+
private let created = Date()
106+
107+
var isExpired: Bool {
108+
Date().timeIntervalSince(created) > Double(expirationInSeconds)
109+
}
110+
}

0 commit comments

Comments
 (0)