Skip to content

Commit 60f107a

Browse files
committed
7478 Enable Cash on Delivery when CTA tapped
1 parent 43ab0aa commit 60f107a

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Settings/In-Person Payments/Onboarding Errors/InPersonPaymentsCodPaymentGatewayNotSetUp.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SwiftUI
22

33
struct InPersonPaymentsCodPaymentGatewayNotSetUp: View {
4-
let viewModel: InPersonPaymentsCodPaymentGatewayNotSetUpViewModel
4+
@ObservedObject var viewModel: InPersonPaymentsCodPaymentGatewayNotSetUpViewModel
55

66
var body: some View {
77
ScrollableVStack {
@@ -22,8 +22,8 @@ struct InPersonPaymentsCodPaymentGatewayNotSetUp: View {
2222
Button(Localization.skipButton, action: viewModel.skipTapped)
2323
.buttonStyle(SecondaryButtonStyle())
2424

25-
Button(Localization.enableButton, action: {})
26-
.buttonStyle(PrimaryLoadingButtonStyle(isLoading: false))
25+
Button(Localization.enableButton, action: viewModel.enableTapped)
26+
.buttonStyle(PrimaryLoadingButtonStyle(isLoading: viewModel.awaitingResponse))
2727

2828
Spacer()
2929

WooCommerce/Classes/ViewRelated/Dashboard/Settings/In-Person Payments/Onboarding Errors/InPersonPaymentsCodPaymentGatewayNotSetUpViewModel.swift

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import Foundation
22
import 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

Comments
 (0)