Skip to content

Commit c554def

Browse files
committed
Undo previous commit, restoring pass mgmt to orchestrator
1 parent b927b20 commit c554def

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

WooCommerce/Classes/ViewModels/CardPresentPayments/PaymentCaptureOrchestrator.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Yosemite
2+
import PassKit
23

34
/// Orchestrates the sequence of actions required to capture a payment:
45
/// 1. Check if there is a card reader connected
@@ -11,6 +12,8 @@ final class PaymentCaptureOrchestrator {
1112

1213
private let celebration = PaymentCaptureCelebration()
1314

15+
private var walletSuppressionRequestToken: PKSuppressionRequestToken?
16+
1417
func collectPayment(for order: Order,
1518
paymentsAccount: PaymentGatewayAccount?,
1619
onPresentMessage: @escaping (String) -> Void,
@@ -35,6 +38,11 @@ final class PaymentCaptureOrchestrator {
3538
return
3639
}
3740

41+
/// Briefly suppress pass (wallet) presentation so that the merchant doesn't attempt to pay for the buyer's order when the
42+
/// reader begins to collect payment.
43+
///
44+
suppressPassPresentation()
45+
3846
let paymentAction = CardPresentPaymentAction.collectPayment(
3947
siteID: order.siteID,
4048
orderID: order.orderID,
@@ -53,6 +61,7 @@ final class PaymentCaptureOrchestrator {
5361
},
5462
onCompletion: { [weak self] result in
5563
onProcessingMessage()
64+
self?.allowPassPresentation()
5665
self?.completePaymentIntentCapture(
5766
order: order,
5867
captureResult: result,
@@ -102,6 +111,54 @@ final class PaymentCaptureOrchestrator {
102111
}
103112
}
104113

114+
private extension PaymentCaptureOrchestrator {
115+
/// Supress wallet presentation. This requires a special entitlement from Apple:
116+
/// `com.apple.developer.passkit.pass-presentation-suppression`
117+
/// See Woo-*.entitlements in WooCommerce/Resources
118+
///
119+
func suppressPassPresentation() {
120+
/// iPads don't support NFC passes. Attempting to call `requestAutomaticPassPresentationSuppression` on them will
121+
/// return 0 `notSupported`
122+
///
123+
guard !UIDevice.isPad() else {
124+
return
125+
}
126+
127+
guard !PKPassLibrary.isSuppressingAutomaticPassPresentation() else {
128+
return
129+
}
130+
131+
walletSuppressionRequestToken = PKPassLibrary.requestAutomaticPassPresentationSuppression() { result in
132+
guard result == .success else {
133+
DDLogWarn("Automatic pass presentation suppression request failed. Reason: \(result.rawValue)")
134+
135+
let logProperties: [String: Any] = ["PKAutomaticPassPresentationSuppressionResult": result.rawValue]
136+
ServiceLocator.crashLogging.logMessage(
137+
"Automatic pass presentation suppression request failed",
138+
properties: logProperties,
139+
level: .warning
140+
)
141+
return
142+
}
143+
}
144+
}
145+
146+
/// Restore wallet presentation.
147+
func allowPassPresentation() {
148+
/// iPads don't have passes (wallets) to present
149+
///
150+
guard !UIDevice.isPad() else {
151+
return
152+
}
153+
154+
guard let walletSuppressionRequestToken = walletSuppressionRequestToken, walletSuppressionRequestToken != 0 else {
155+
return
156+
}
157+
158+
PKPassLibrary.endAutomaticPassPresentationSuppression(withRequestToken: walletSuppressionRequestToken)
159+
}
160+
}
161+
105162
private extension PaymentCaptureOrchestrator {
106163
func completePaymentIntentCapture(order: Order,
107164
captureResult: Result<PaymentIntent, Error>,

Yosemite/Yosemite/Stores/CardPresentPaymentStore.swift

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Storage
22
import Hardware
33
import Networking
44
import Combine
5-
import PassKit
65

76
/// MARK: CardPresentPaymentStore
87
///
@@ -19,8 +18,6 @@ public final class CardPresentPaymentStore: Store {
1918
/// We need to be able to cancel the process of collecting a payment.
2019
private var paymentCancellable: AnyCancellable? = nil
2120

22-
private var walletSuppressionRequestToken: PKSuppressionRequestToken?
23-
2421
public init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network, cardReaderService: CardReaderService) {
2522
self.cardReaderService = cardReaderService
2623
self.remote = WCPayRemote(network: network)
@@ -164,15 +161,12 @@ private extension CardPresentPaymentStore {
164161
parameters: PaymentParameters,
165162
onCardReaderMessage: @escaping (CardReaderEvent) -> Void,
166163
onCompletion: @escaping (Result<PaymentIntent, Error>) -> Void) {
167-
suppressPassPresentation()
168-
169164
// Observe status events fired by the card reader
170165
let readerEventsSubscription = cardReaderService.readerEvents.sink { event in
171166
onCardReaderMessage(event)
172167
}
173168

174169
paymentCancellable = cardReaderService.capturePayment(parameters).sink { error in
175-
self.allowPassPresentation()
176170
readerEventsSubscription.cancel()
177171
switch error {
178172
case .failure(let error):
@@ -191,8 +185,6 @@ private extension CardPresentPaymentStore {
191185

192186
cardReaderService.cancelPaymentIntent()
193187
.subscribe(Subscribers.Sink(receiveCompletion: { value in
194-
self.allowPassPresentation()
195-
196188
switch value {
197189
case .failure(let error):
198190
onCompletion?(.failure(error))
@@ -267,43 +259,6 @@ private extension CardPresentPaymentStore {
267259
}
268260
}
269261

270-
private extension CardPresentPaymentStore {
271-
/// Supress wallet presentation. This requires a special entitlement from Apple:
272-
/// `com.apple.developer.passkit.pass-presentation-suppression`
273-
/// See Woo-*.entitlements in WooCommerce/Resources
274-
///
275-
/// Note: iPads don't support NFC passes.
276-
///
277-
func suppressPassPresentation() {
278-
guard UIDevice.current.userInterfaceIdiom != .pad else {
279-
return
280-
}
281-
282-
guard !PKPassLibrary.isSuppressingAutomaticPassPresentation() else {
283-
return
284-
}
285-
286-
walletSuppressionRequestToken = PKPassLibrary.requestAutomaticPassPresentationSuppression() { result in
287-
guard result == .success else {
288-
DDLogWarn("Automatic pass presentation suppression request failed. Reason: \(result.rawValue)")
289-
return
290-
}
291-
}
292-
}
293-
294-
func allowPassPresentation() {
295-
guard UIDevice.current.userInterfaceIdiom != .pad else {
296-
return
297-
}
298-
299-
guard let walletSuppressionRequestToken = walletSuppressionRequestToken, walletSuppressionRequestToken != 0 else {
300-
return
301-
}
302-
303-
PKPassLibrary.endAutomaticPassPresentationSuppression(withRequestToken: walletSuppressionRequestToken)
304-
}
305-
}
306-
307262
/// Implementation of the CardReaderNetworkingAdapter
308263
/// that fetches a token using WCPayRemote
309264
private final class WCPayTokenProvider: CardReaderConfigProvider {

0 commit comments

Comments
 (0)