Skip to content

Commit 32dc61f

Browse files
committed
Add CardPresentConfigurationLoader
1 parent 4050960 commit 32dc61f

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Foundation
2+
import Yosemite
3+
4+
final class CardPresentConfigurationLoader {
5+
let stores: StoresManager
6+
7+
private var stripeGatewayIPPEnabled: Bool = false
8+
private var canadaIPPEnabled: Bool = false
9+
10+
init(stores: StoresManager = ServiceLocator.stores) {
11+
self.stores = stores
12+
let stripeAction = AppSettingsAction.loadStripeInPersonPaymentsSwitchState(onCompletion: { [weak self] result in
13+
switch result {
14+
case .success(let stripeGatewayIPPEnabled):
15+
self?.stripeGatewayIPPEnabled = stripeGatewayIPPEnabled
16+
default:
17+
break
18+
}
19+
})
20+
stores.dispatch(stripeAction)
21+
22+
let canadaAction = AppSettingsAction.loadCanadaInPersonPaymentsSwitchState(onCompletion: { [weak self] result in
23+
switch result {
24+
case .success(let canadaIPPEnabled):
25+
self?.canadaIPPEnabled = canadaIPPEnabled
26+
default:
27+
break
28+
}
29+
})
30+
stores.dispatch(canadaAction)
31+
}
32+
33+
var configuration: CardPresentPaymentsConfiguration {
34+
.init(
35+
country: SiteAddress().countryCode,
36+
stripeEnabled: stripeGatewayIPPEnabled,
37+
canadaEnabled: canadaIPPEnabled
38+
)
39+
}
40+
}

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

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ protocol CardPresentPaymentsOnboardingUseCaseProtocol {
3030
final class CardPresentPaymentsOnboardingUseCase: CardPresentPaymentsOnboardingUseCaseProtocol, ObservableObject {
3131
let storageManager: StorageManagerType
3232
let stores: StoresManager
33-
private var stripeGatewayIPPEnabled: Bool = false
34-
private var canadaIPPEnabled: Bool = false
33+
let configurationLoader: CardPresentConfigurationLoader
3534

3635
@Published var state: CardPresentPaymentOnboardingState = .loading
3736

@@ -45,26 +44,7 @@ final class CardPresentPaymentsOnboardingUseCase: CardPresentPaymentsOnboardingU
4544
) {
4645
self.storageManager = storageManager
4746
self.stores = stores
48-
49-
let stripeAction = AppSettingsAction.loadStripeInPersonPaymentsSwitchState(onCompletion: { [weak self] result in
50-
switch result {
51-
case .success(let stripeGatewayIPPEnabled):
52-
self?.stripeGatewayIPPEnabled = stripeGatewayIPPEnabled
53-
default:
54-
break
55-
}
56-
})
57-
stores.dispatch(stripeAction)
58-
59-
let canadaAction = AppSettingsAction.loadCanadaInPersonPaymentsSwitchState(onCompletion: { [weak self] result in
60-
switch result {
61-
case .success(let canadaIPPEnabled):
62-
self?.canadaIPPEnabled = canadaIPPEnabled
63-
default:
64-
break
65-
}
66-
})
67-
stores.dispatch(canadaAction)
47+
self.configurationLoader = .init(stores: stores)
6848

6949
// At the time of writing, actions are dispatched and processed synchronously, so the completion blocks for
7050
// loadStripeInPersonPaymentsSwitchState and loadCanadaInPersonPaymentsSwitchState should have been called already.
@@ -161,18 +141,9 @@ private extension CardPresentPaymentsOnboardingUseCase {
161141
return .genericError
162142
}
163143

164-
let configuration: CardPresentPaymentsConfiguration
165-
do {
166-
configuration = try CardPresentPaymentsConfiguration(
167-
country: countryCode,
168-
stripeEnabled: stripeGatewayIPPEnabled,
169-
canadaEnabled: canadaIPPEnabled
170-
)
171-
} catch is CardPresentPaymentsConfigurationMissingError {
144+
let configuration = configurationLoader.configuration
145+
guard configuration.isSupportedCountry else {
172146
return .countryNotSupported(countryCode: countryCode)
173-
} catch {
174-
DDLogError("[CardPresentPaymentsOnboarding] Unexpected error loading configuration: \(error)")
175-
return .genericError
176147
}
177148

178149
let wcPay = getWCPayPlugin()

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,7 @@
15201520
DEF3300C270444070073AE29 /* ShippingLabelSelectedRate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF3300B270444060073AE29 /* ShippingLabelSelectedRate.swift */; };
15211521
DEFD6E61264990FB00E51E0D /* SitePluginListViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFD6E60264990FB00E51E0D /* SitePluginListViewModelTests.swift */; };
15221522
DEFD6E86264ABFB700E51E0D /* PluginListViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = DEFD6E85264ABFB700E51E0D /* PluginListViewController.xib */; };
1523+
E10459C627BBC22E00C1DCBA /* CardPresentConfigurationLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = E10459C527BBC22E00C1DCBA /* CardPresentConfigurationLoader.swift */; };
15231524
E107FCE126C12B2700BAF51B /* InPersonPaymentsCountryNotSupported.swift in Sources */ = {isa = PBXBuildFile; fileRef = E107FCE026C12B2700BAF51B /* InPersonPaymentsCountryNotSupported.swift */; };
15241525
E107FCE326C13A0D00BAF51B /* InPersonPaymentsSupportLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = E107FCE226C13A0D00BAF51B /* InPersonPaymentsSupportLink.swift */; };
15251526
E10BC15E26CC06970064F5E2 /* ScrollableVStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = E10BC15D26CC06970064F5E2 /* ScrollableVStack.swift */; };
@@ -3153,6 +3154,7 @@
31533154
DEF3300B270444060073AE29 /* ShippingLabelSelectedRate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShippingLabelSelectedRate.swift; sourceTree = "<group>"; };
31543155
DEFD6E60264990FB00E51E0D /* SitePluginListViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SitePluginListViewModelTests.swift; sourceTree = "<group>"; };
31553156
DEFD6E85264ABFB700E51E0D /* PluginListViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PluginListViewController.xib; sourceTree = "<group>"; };
3157+
E10459C527BBC22E00C1DCBA /* CardPresentConfigurationLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardPresentConfigurationLoader.swift; sourceTree = "<group>"; };
31563158
E107FCE026C12B2700BAF51B /* InPersonPaymentsCountryNotSupported.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InPersonPaymentsCountryNotSupported.swift; sourceTree = "<group>"; };
31573159
E107FCE226C13A0D00BAF51B /* InPersonPaymentsSupportLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InPersonPaymentsSupportLink.swift; sourceTree = "<group>"; };
31583160
E10BC15D26CC06970064F5E2 /* ScrollableVStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollableVStack.swift; sourceTree = "<group>"; };
@@ -5789,6 +5791,7 @@
57895791
024A543222BA6DD500F4F38E /* Developer */,
57905792
02C0CD2823B5BAFB00F880B1 /* ImageService */,
57915793
029700EA24FE389C00D242F8 /* InfiniteScroll */,
5794+
E10459C427BBC20C00C1DCBA /* In-Person Payments */,
57925795
B5A03699214C0E7000774E2C /* Logging */,
57935796
B58B4ABC2108F7F800076FDD /* Notices */,
57945797
45C11B7D2508E99A006C2089 /* Shared Site Settings */,
@@ -7393,6 +7396,14 @@
73937396
path = Plugins;
73947397
sourceTree = "<group>";
73957398
};
7399+
E10459C427BBC20C00C1DCBA /* In-Person Payments */ = {
7400+
isa = PBXGroup;
7401+
children = (
7402+
E10459C527BBC22E00C1DCBA /* CardPresentConfigurationLoader.swift */,
7403+
);
7404+
path = "In-Person Payments";
7405+
sourceTree = "<group>";
7406+
};
73967407
E107FCDD26C1295600BAF51B /* Onboarding Errors */ = {
73977408
isa = PBXGroup;
73987409
children = (
@@ -8567,6 +8578,7 @@
85678578
B541B2172189EED4008FE7C1 /* NSMutableAttributedString+Helpers.swift in Sources */,
85688579
26F94E2E267A96A000DB6CCF /* ProductAddOnViewModel.swift in Sources */,
85698580
02A9BCD42737DE0D00159C79 /* JetpackBenefitsView.swift in Sources */,
8581+
E10459C627BBC22E00C1DCBA /* CardPresentConfigurationLoader.swift in Sources */,
85708582
B586906621A5F4B1001F1EFC /* UINavigationController+Woo.swift in Sources */,
85718583
45FBDF3A238D3F8B00127F77 /* ExtendedAddProductImageCollectionViewCell.swift in Sources */,
85728584
02A410F52583A84C005E2925 /* SpacerTableViewCell.swift in Sources */,

Yosemite/Yosemite/Model/Payments/CardPresentPaymentsConfiguration.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct CardPresentPaymentsConfiguration {
1111
self.paymentGateways = paymentGateways
1212
}
1313

14-
public init(country: String, stripeEnabled: Bool, canadaEnabled: Bool) throws {
14+
public init(country: String, stripeEnabled: Bool, canadaEnabled: Bool) {
1515
switch country {
1616
case "US" where stripeEnabled == true:
1717
self.init(
@@ -32,9 +32,11 @@ public struct CardPresentPaymentsConfiguration {
3232
paymentGateways: [WCPayAccount.gatewayID]
3333
)
3434
default:
35-
throw CardPresentPaymentsConfigurationMissingError()
35+
self.init(paymentMethods: [], currencies: [], paymentGateways: [])
3636
}
3737
}
38-
}
3938

40-
public struct CardPresentPaymentsConfigurationMissingError: Error {}
39+
public var isSupportedCountry: Bool {
40+
paymentMethods.isEmpty == false && currencies.isEmpty == false && paymentGateways.isEmpty == false
41+
}
42+
}

0 commit comments

Comments
 (0)