Skip to content

Commit 721ec11

Browse files
committed
Refactor CardPresentPaymentsOnboardingUseCase to handle both plugins installed better
1 parent 4b1db96 commit 721ec11

File tree

3 files changed

+81
-43
lines changed

3 files changed

+81
-43
lines changed

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

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,6 @@ final class CardPresentPaymentsOnboardingUseCase: CardPresentPaymentsOnboardingU
7777
return
7878
}
7979

80-
let wcPayPlugin = cardPresentPluginsDataProvider.getWCPayPlugin()
81-
let stripePlugin = cardPresentPluginsDataProvider.getStripePlugin()
82-
let paymentPluginsInstalledAndActiveStatus = cardPresentPluginsDataProvider.paymentPluginsInstalledAndActiveStatus(wcPay: wcPayPlugin,
83-
stripe: stripePlugin)
84-
85-
/// If both plugins are active, don't bother initializing the backend nor fetching
86-
/// accounts. Fall through to updateState so the end user can fix the problem.
87-
guard paymentPluginsInstalledAndActiveStatus != .bothAreInstalledAndActive else {
88-
self.updateState()
89-
return
90-
}
91-
9280
let paymentGatewayAccountsAction = CardPresentPaymentAction.loadAccounts(siteID: siteID) { [weak self] result in
9381
self?.updateState()
9482
}
@@ -155,52 +143,48 @@ private extension CardPresentPaymentsOnboardingUseCase {
155143

156144
let wcPay = cardPresentPluginsDataProvider.getWCPayPlugin()
157145
let stripe = cardPresentPluginsDataProvider.getStripePlugin()
158-
let isStripeSupported = configuration.paymentGateways.contains(StripeAccount.gatewayID)
159146

160147
// If isSupportedCountry is false, IPP is not supported in the country through any
161148
// payment gateway
162149
guard configuration.isSupportedCountry else {
163150
return .countryNotSupported(countryCode: countryCode)
164151
}
165152

166-
let paymentPluginsInstalledAndActiveStatus = cardPresentPluginsDataProvider.paymentPluginsInstalledAndActiveStatus(wcPay: wcPay, stripe: stripe)
167-
168-
// If it is supported in the country, we might or might not support Stripe yet, only WCPay
169-
guard isStripeSupported else {
170-
if paymentPluginsInstalledAndActiveStatus == .bothAreInstalledAndActive {
171-
// They have WCPay and Stripe installed and active at the same time.
172-
// Deactivating Stripe is the advised way to proceed.
173-
return .pluginShouldBeDeactivated(plugin: .stripe)
174-
} else if paymentPluginsInstalledAndActiveStatus == .onlyStripeIsInstalledAndActive {
175-
// If we only support WCPay, we don't want to ask users to set up WCPay if they already
176-
// have Stripe. In that case, we can tell them that IPP is not supported for Stripe in
177-
// their country yet.
178-
return .countryNotSupportedStripe(plugin: .stripe, countryCode: countryCode)
179-
} else {
180-
return wcPayOnlyOnboardingState(plugin: wcPay)
181-
}
153+
switch (wcPay, stripe) {
154+
case (.some(let wcPay), nil):
155+
return wcPayOnlyOnboardingState(plugin: wcPay)
156+
case (nil, .some(let stripe)):
157+
return stripeGatewayOnlyOnboardingState(plugin: stripe)
158+
case (.some(let wcPay), .some(let stripe)):
159+
return bothPluginsInstalledOnboardingState(wcPay: wcPay, stripe: stripe)
160+
case (nil, nil):
161+
return .pluginNotInstalled
182162
}
163+
}
183164

184-
// If both the Stripe plugin and WCPay are installed and activated, the user needs
185-
// to deactivate one: pdfdoF-fW-p2#comment-683
186-
if paymentPluginsInstalledAndActiveStatus == .bothAreInstalledAndActive {
187-
return .selectPlugin
165+
func bothPluginsInstalledOnboardingState(wcPay: SystemPlugin, stripe: SystemPlugin) -> CardPresentPaymentOnboardingState {
166+
switch (wcPay.active, stripe.active) {
167+
case (true, true):
168+
return bothPluginsInstalledAndActiveOnboardingState(wcPay: wcPay, stripe: stripe)
169+
case (true, false):
170+
return wcPayOnlyOnboardingState(plugin: wcPay)
171+
case (false, true):
172+
return stripeGatewayOnlyOnboardingState(plugin: stripe)
173+
case (false, false):
174+
return .pluginNotActivated(plugin: .wcPay)
188175
}
176+
}
189177

190-
// If only the Stripe extension is installed, skip to checking Stripe activation and version
191-
if let stripe = stripe,
192-
paymentPluginsInstalledAndActiveStatus != .onlyWCPayIsInstalledAndActive {
193-
return stripeGatewayOnlyOnboardingState(plugin: stripe)
194-
} else {
195-
return wcPayOnlyOnboardingState(plugin: wcPay)
178+
func bothPluginsInstalledAndActiveOnboardingState(wcPay: SystemPlugin, stripe: SystemPlugin) -> CardPresentPaymentOnboardingState {
179+
if !isStripeSupportedInCountry {
180+
return .pluginShouldBeDeactivated(plugin: .stripe)
196181
}
182+
183+
return .selectPlugin
197184
}
198185

199-
func wcPayOnlyOnboardingState(plugin: SystemPlugin?) -> CardPresentPaymentOnboardingState {
186+
func wcPayOnlyOnboardingState(plugin: SystemPlugin) -> CardPresentPaymentOnboardingState {
200187
// Plugin checks
201-
guard let plugin = plugin else {
202-
return .pluginNotInstalled
203-
}
204188
guard cardPresentPluginsDataProvider.isWCPayVersionSupported(plugin: plugin)
205189
else {
206190
return .pluginUnsupportedVersion(plugin: .wcPay)
@@ -214,6 +198,14 @@ private extension CardPresentPaymentsOnboardingUseCase {
214198
}
215199

216200
func stripeGatewayOnlyOnboardingState(plugin: SystemPlugin) -> CardPresentPaymentOnboardingState {
201+
guard isStripeSupportedInCountry else {
202+
guard let countryCode = storeCountryCode else {
203+
DDLogError("[CardPresentPaymentsOnboarding] Couldn't determine country for store")
204+
return .genericError
205+
}
206+
return .countryNotSupportedStripe(plugin: .stripe, countryCode: countryCode)
207+
}
208+
217209
guard cardPresentPluginsDataProvider.isStripeVersionSupported(plugin: plugin)
218210
else {
219211
return .pluginUnsupportedVersion(plugin: .stripe)
@@ -274,6 +266,10 @@ private extension CardPresentPaymentsOnboardingUseCase {
274266
return storeCountryCode.nonEmptyString()
275267
}
276268

269+
var isStripeSupportedInCountry: Bool {
270+
configurationLoader.configuration.paymentGateways.contains(StripeAccount.gatewayID)
271+
}
272+
277273
// Note: This counts on synchronizeStoreCountryAndPlugins having been called to get
278274
// the appropriate account for the site, be that Stripe or WCPay
279275
func getPaymentGatewayAccount() -> PaymentGatewayAccount? {

WooCommerce/WooCommerceTests/ViewRelated/CardPresentPayments/CardPresentPaymentsOnboardingUseCaseTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ class CardPresentPaymentsOnboardingUseCaseTests: XCTestCase {
177177
XCTAssertEqual(state, .pluginNotActivated(plugin: .stripe))
178178
}
179179

180+
func test_onboarding_returns_select_wcpay_plugin_not_activated_when_both_stripe_and_wcpay_plugins_are_installed_but_not_active() {
181+
// Given
182+
setupCountry(country: .us)
183+
setupWCPayPlugin(status: .inactive, version: WCPayPluginVersion.minimumSupportedVersion)
184+
setupStripePlugin(status: .inactive, version: StripePluginVersion.minimumSupportedVersion)
185+
186+
// When
187+
let useCase = CardPresentPaymentsOnboardingUseCase(storageManager: storageManager, stores: stores)
188+
let state = useCase.state
189+
190+
// Then
191+
XCTAssertEqual(state, .pluginNotActivated(plugin: .wcPay))
192+
}
193+
180194
func test_onboarding_returns_select_plugin_when_both_stripe_and_wcpay_plugins_are_active() {
181195
// Given
182196
setupCountry(country: .us)
@@ -300,6 +314,21 @@ class CardPresentPaymentsOnboardingUseCaseTests: XCTestCase {
300314
XCTAssertEqual(state, .completed(plugin: .wcPay))
301315
}
302316

317+
func test_onboarding_returns_complete_when_wcpay_active_and_stripe_plugin_installed_but_not_active() {
318+
// Given
319+
setupCountry(country: .us)
320+
setupWCPayPlugin(status: .active, version: WCPayPluginVersion.minimumSupportedVersion)
321+
setupStripePlugin(status: .inactive, version: StripePluginVersion.minimumSupportedVersion)
322+
setupPaymentGatewayAccount(accountType: WCPayAccount.self, status: .complete)
323+
324+
// When
325+
let useCase = CardPresentPaymentsOnboardingUseCase(storageManager: storageManager, stores: stores)
326+
let state = useCase.state
327+
328+
// Then
329+
XCTAssertEqual(state, .completed(plugin: .wcPay))
330+
}
331+
303332
func test_onboarding_returns_complete_when_stripe_active_and_wcpay_plugin_installed_but_not_active() {
304333
// Given
305334
setupCountry(country: .us)

Yosemite/Yosemite/Model/Enums/CardPresentPaymentsPlugin.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ public enum CardPresentPaymentsPlugin: Equatable, CaseIterable {
1212
return "WooCommerce Stripe Gateway"
1313
}
1414
}
15+
16+
public var gatewayID: String {
17+
switch self {
18+
case .wcPay:
19+
return WCPayAccount.gatewayID
20+
case .stripe:
21+
return StripeAccount.gatewayID
22+
}
23+
}
24+
25+
public static func with(gatewayID: String) -> Self? {
26+
allCases.first(where: { $0.gatewayID == gatewayID })
27+
}
1528
}
1629

1730
public struct PaymentPluginVersionSupport {

0 commit comments

Comments
 (0)