@@ -12,6 +12,15 @@ final class InPersonPaymentsMenuViewController: UIViewController {
1212 private let featureFlagService : FeatureFlagService
1313 private let cardPresentPaymentsOnboardingUseCase : CardPresentPaymentsOnboardingUseCase
1414 private var cancellables : Set < AnyCancellable > = [ ]
15+ private lazy var learnMoreViewModel : LearnMoreViewModel = {
16+ LearnMoreViewModel ( url: WooConstants . URLs. wcPayCashOnDeliveryLearnMore. asURL ( ) ,
17+ linkText: Localization . toggleEnableCashOnDeliveryLearnMoreLink,
18+ formatText: Localization . toggleEnableCashOnDeliveryLearnMoreFormat,
19+ tappedAnalyticEvent: WooAnalyticsEvent . InPersonPayments. cardPresentOnboardingLearnMoreTapped (
20+ reason: " reason " ,
21+ countryCode: configurationLoader. configuration. countryCode) )
22+ } ( )
23+ private let inPersonPaymentsMenuViewModel : InPersonPaymentsMenuViewModel
1524
1625 /// No Manuals to be shown in a country where IPP is not supported
1726 ///
@@ -44,6 +53,7 @@ final class InPersonPaymentsMenuViewController: UIViewController {
4453 self . featureFlagService = featureFlagService
4554 self . cardPresentPaymentsOnboardingUseCase = CardPresentPaymentsOnboardingUseCase ( )
4655 configurationLoader = CardPresentConfigurationLoader ( )
56+ self . inPersonPaymentsMenuViewModel = InPersonPaymentsMenuViewModel ( )
4757
4858 super. init ( nibName: nil , bundle: nil )
4959 }
@@ -58,6 +68,7 @@ final class InPersonPaymentsMenuViewController: UIViewController {
5868 configureSections ( )
5969 configureTableView ( )
6070 registerTableViewCells ( )
71+ configureTableReload ( )
6172 runCardPresentPaymentsOnboarding ( )
6273 }
6374}
@@ -77,7 +88,7 @@ private extension InPersonPaymentsMenuViewController {
7788 }
7889
7990 func refreshAfterNewOnboardingState( _ state: CardPresentPaymentOnboardingState ) {
80- self . pluginState = nil
91+ pluginState = nil
8192
8293 guard state != . loading else {
8394 self . activityIndicator? . startAnimating ( )
@@ -86,22 +97,24 @@ private extension InPersonPaymentsMenuViewController {
8697
8798 switch state {
8899 case let . completed( newPluginState) :
89- self . pluginState = newPluginState
90- self . dismissCardPresentPaymentsOnboardingNoticeIfPresent ( )
91- self . dismissOnboardingIfPresented ( )
100+ pluginState = newPluginState
101+ dismissCardPresentPaymentsOnboardingNoticeIfPresent ( )
102+ dismissOnboardingIfPresented ( )
92103 case let . selectPlugin( pluginSelectionWasCleared) :
93104 // If it was cleared it means that we triggered it manually (e.g by tapping in this view on the plugin selection row)
94105 // No need to show the onboarding notice
95106 if !pluginSelectionWasCleared {
96- self . showCardPresentPaymentsOnboardingNotice ( )
107+ showCardPresentPaymentsOnboardingNotice ( )
97108 }
98109 default :
99- self . showCardPresentPaymentsOnboardingNotice ( )
110+ showCardPresentPaymentsOnboardingNotice ( )
100111 }
101112
102- self . activityIndicator? . stopAnimating ( )
103- self . configureSections ( )
104- self . tableView. reloadData ( )
113+ updateViewModelSelectedPlugin ( state: state)
114+
115+ activityIndicator? . stopAnimating ( )
116+ configureSections ( )
117+ tableView. reloadData ( )
105118 }
106119
107120 func showCardPresentPaymentsOnboardingNotice( ) {
@@ -119,6 +132,17 @@ private extension InPersonPaymentsMenuViewController {
119132 permanentNoticePresenter. dismiss ( )
120133 }
121134
135+ func updateViewModelSelectedPlugin( state: CardPresentPaymentOnboardingState ) {
136+ switch state {
137+ case let . completed( pluginState) :
138+ inPersonPaymentsMenuViewModel. selectedPlugin = pluginState. preferred
139+ case let . codPaymentGatewayNotSetUp( plugin) :
140+ inPersonPaymentsMenuViewModel. selectedPlugin = plugin
141+ default :
142+ inPersonPaymentsMenuViewModel. selectedPlugin = nil
143+ }
144+ }
145+
122146 func showOnboarding( ) {
123147 // Instead of using `CardPresentPaymentsOnboardingPresenter` we create the view directly because we already have the onboarding state in the use case.
124148 // That way we avoid triggering the onboarding check again that comes with the presenter.
@@ -147,7 +171,7 @@ private extension InPersonPaymentsMenuViewController {
147171 }
148172
149173 var actionsSection : Section ? {
150- return Section ( header: Localization . paymentActionsSectionTitle, rows: [ . collectPayment] )
174+ return Section ( header: Localization . paymentActionsSectionTitle, rows: [ . collectPayment, . toggleEnableCashOnDelivery ] )
151175 }
152176
153177 var cardReadersSection : Section ? {
@@ -211,6 +235,8 @@ private extension InPersonPaymentsMenuViewController {
211235 configureCardReaderManuals ( cell: cell)
212236 case let cell as LeftImageTableViewCell where row == . collectPayment:
213237 configureCollectPayment ( cell: cell)
238+ case let cell as LeftImageTitleSubtitleToggleTableViewCell where row == . toggleEnableCashOnDelivery:
239+ configureToggleEnableCashOnDelivery ( cell: cell)
214240 default :
215241 fatalError ( )
216242 }
@@ -259,11 +285,32 @@ private extension InPersonPaymentsMenuViewController {
259285 updateEnabledState ( in: cell)
260286 }
261287
288+ func configureToggleEnableCashOnDelivery( cell: LeftImageTitleSubtitleToggleTableViewCell ) {
289+ cell. leftImageView? . tintColor = . text
290+ cell. accessoryType = . none
291+ cell. selectionStyle = . none
292+ cell. configure ( image: . creditCardIcon,
293+ text: Localization . toggleEnableCashOnDelivery,
294+ subtitle: learnMoreViewModel. learnMoreAttributedString,
295+ switchState: inPersonPaymentsMenuViewModel. cashOnDeliveryEnabledState,
296+ switchAction: inPersonPaymentsMenuViewModel. updateCashOnDeliverySetting ( enabled: ) ,
297+ subtitleTapAction: { [ weak self] in
298+ guard let self = self else { return }
299+ self . inPersonPaymentsMenuViewModel. learnMoreTapped ( from: self )
300+ } )
301+ }
302+
262303 func updateEnabledState( in cell: UITableViewCell , shouldBeEnabled: Bool = true ) {
263304 let alpha = shouldBeEnabled ? 1 : 0.3
264305 cell. imageView? . alpha = alpha
265306 cell. textLabel? . alpha = alpha
266307 }
308+
309+ func configureTableReload( ) {
310+ inPersonPaymentsMenuViewModel. $cashOnDeliveryEnabledState. sink { [ weak self] _ in
311+ self ? . tableView. reloadData ( )
312+ } . store ( in: & cancellables)
313+ }
267314}
268315
269316// MARK: - Convenience methods
@@ -375,6 +422,17 @@ extension InPersonPaymentsMenuViewController: UITableViewDelegate {
375422 managePaymentGatewaysWasPressed ( )
376423 case . collectPayment:
377424 collectPaymentWasPressed ( )
425+ case . toggleEnableCashOnDelivery:
426+ break
427+ }
428+ }
429+
430+ func tableView( _ tableView: UITableView , willSelectRowAt indexPath: IndexPath ) -> IndexPath ? {
431+ switch rowAtIndexPath ( indexPath) {
432+ case . toggleEnableCashOnDelivery:
433+ return nil
434+ default :
435+ return indexPath
378436 }
379437 }
380438}
@@ -410,6 +468,22 @@ private extension InPersonPaymentsMenuViewController {
410468 comment: " Navigates to Payment Gateway management screen "
411469 )
412470
471+ static let toggleEnableCashOnDelivery = NSLocalizedString (
472+ " Enable Pay in Person " ,
473+ comment: " Title for a switch on the In-Person Payments menu to enable Cash on Delivery "
474+ )
475+
476+ static let toggleEnableCashOnDeliveryLearnMoreFormat = NSLocalizedString (
477+ " Pay in Person lets you accept card or cash payments on collection or delivery. %1$@ " ,
478+ comment: " A label prompting users to learn more about adding Pay in Person to their checkout. " +
479+ " %1$@ is a placeholder that always replaced with \" Learn more \" string, " +
480+ " which should be translated separately and considered part of this sentence. " )
481+
482+ static let toggleEnableCashOnDeliveryLearnMoreLink = NSLocalizedString (
483+ " Learn more " ,
484+ comment: " The \" Learn more \" string replaces the placeholder in a label prompting users to learn " +
485+ " more about adding Pay in Person to their checkout. " )
486+
413487 static let cardReaderManuals = NSLocalizedString (
414488 " Card Reader Manuals " ,
415489 comment: " Navigates to Card Reader Manuals screen "
@@ -443,11 +517,14 @@ private enum Row: CaseIterable {
443517 case cardReaderManuals
444518 case managePaymentGateways
445519 case collectPayment
520+ case toggleEnableCashOnDelivery
446521
447522 var type : UITableViewCell . Type {
448523 switch self {
449524 case . managePaymentGateways:
450525 return LeftImageTitleSubtitleTableViewCell . self
526+ case . toggleEnableCashOnDelivery:
527+ return LeftImageTitleSubtitleToggleTableViewCell . self
451528 default :
452529 return LeftImageTableViewCell . self
453530 }
0 commit comments