11import Foundation
22import Yosemite
33
4- struct InPersonPaymentsCodPaymentGatewayNotSetUpViewModel {
4+ class InPersonPaymentsCodPaymentGatewayNotSetUpViewModel : ObservableObject {
55 let completion : ( ) -> ( )
66 private let stores : StoresManager = ServiceLocator . stores
77
8+ @Published var awaitingResponse = false
9+
810 private var siteID : Int64 ? {
911 stores. sessionManager. defaultStoreID
1012 }
1113
14+ init ( completion: @escaping ( ) -> ( ) ) {
15+ self . completion = completion
16+ }
17+
1218 func skipTapped( ) {
1319 guard let siteID = siteID else {
1420 return completion ( )
@@ -18,4 +24,74 @@ struct InPersonPaymentsCodPaymentGatewayNotSetUpViewModel {
1824 stores. dispatch ( action)
1925 completion ( )
2026 }
27+
28+ func enableTapped( ) {
29+ guard let siteID = siteID else {
30+ return completion ( )
31+ }
32+
33+ awaitingResponse = true
34+
35+ let action = PaymentGatewayAction . updatePaymentGateway ( defaultCashOnDeliveryGateway ( siteID: siteID) ) { [ weak self] result in
36+ guard let self = self else { return }
37+ guard result. isSuccess else {
38+ DDLogError ( " 💰 Could not update Payment Gateway: \( String ( describing: result. failure) ) " )
39+ self . awaitingResponse = false
40+ self . displayEnableCashOnDeliveryFailureNotice ( )
41+ return
42+ }
43+
44+ self . completion ( )
45+ }
46+ stores. dispatch ( action)
47+ }
48+
49+ private func defaultCashOnDeliveryGateway( siteID: Int64 ) -> PaymentGateway {
50+ PaymentGateway ( siteID: siteID,
51+ gatewayID: Constants . cashOnDeliveryGatewayID,
52+ title: Localization . cashOnDeliveryCheckoutTitle,
53+ description: Localization . cashOnDeliveryCheckoutDescription,
54+ enabled: true ,
55+ features: [ . products] ,
56+ instructions: Localization . cashOnDeliveryCheckoutInstructions)
57+ }
58+
59+ private func displayEnableCashOnDeliveryFailureNotice( ) {
60+ let notice = Notice ( title: Localization . cashOnDeliveryFailureNoticeTitle,
61+ message: nil ,
62+ feedbackType: . error,
63+ actionTitle: Localization . cashOnDeliveryFailureNoticeRetryTitle,
64+ actionHandler: enableTapped)
65+
66+ ServiceLocator . noticePresenter. enqueue ( notice: notice)
67+ }
68+ }
69+
70+ private enum Localization {
71+ static let cashOnDeliveryCheckoutTitle = NSLocalizedString (
72+ " Pay in Person " ,
73+ comment: " Customer-facing title for the payment option added to the store checkout when the merchant enables " +
74+ " Pay in Person " )
75+
76+ static let cashOnDeliveryCheckoutDescription = NSLocalizedString (
77+ " Pay by card or another accepted payment method " ,
78+ comment: " Customer-facing description showing more details about the Pay in Person option which is added to " +
79+ " the store checkout when the merchant enables Pay in Person " )
80+
81+ static let cashOnDeliveryCheckoutInstructions = NSLocalizedString (
82+ " Pay by card or another accepted payment method " ,
83+ comment: " Customer-facing instructions shown on Order Thank-you pages and confirmation emails, showing more " +
84+ " details about the Pay in Person option added to the store checkout when the merchant enables Pay in Person " )
85+
86+ static let cashOnDeliveryFailureNoticeTitle = NSLocalizedString (
87+ " Failed to enable Pay in Person. Please try again later. " ,
88+ comment: " Error displayed when the attempt to enable a Pay in Person checkout payment option fails " )
89+
90+ static let cashOnDeliveryFailureNoticeRetryTitle = NSLocalizedString (
91+ " Retry " ,
92+ comment: " Retry Action on error displayed when the attempt to enable a Pay in Person checkout payment option fails " )
93+ }
94+
95+ private enum Constants {
96+ static let cashOnDeliveryGatewayID = " cod "
2197}
0 commit comments