Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Fakes/Fakes/Networking.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,8 @@ extension Networking.ShippingLabelAccountSettings {
selectedPaymentMethodID: .fake(),
isEmailReceiptsEnabled: .fake(),
paperSize: .fake(),
lastSelectedPackageID: .fake()
lastSelectedPackageID: .fake(),
addPaymentMethodURL: .fake()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ShippingLabelAccountSettingsMapperTests: XCTestCase {
XCTAssertEqual(settings.storeOwnerUsername, "admin")
XCTAssertEqual(settings.storeOwnerWpcomEmail, "[email protected]")
XCTAssertEqual(settings.storeOwnerWpcomUsername, "apiexamples")
XCTAssertEqual(settings.addPaymentMethodURL, URL(string: "https://wordpress.com/me/purchases/add-credit-card"))
}

/// Verifies that the Shipping Label Account Settings are parsed correctly.
Expand All @@ -51,6 +52,7 @@ class ShippingLabelAccountSettingsMapperTests: XCTestCase {
XCTAssertEqual(settings.storeOwnerUsername, "admin")
XCTAssertEqual(settings.storeOwnerWpcomEmail, "[email protected]")
XCTAssertEqual(settings.storeOwnerWpcomUsername, "apiexamples")
XCTAssertEqual(settings.addPaymentMethodURL, URL(string: "https://wordpress.com/me/purchases/add-credit-card"))
}

/// Verifies that the Shipping Label Account Settings without any payment methods are parsed correctly.
Expand All @@ -73,6 +75,7 @@ class ShippingLabelAccountSettingsMapperTests: XCTestCase {
XCTAssertEqual(settings.storeOwnerUsername, "admin")
XCTAssertEqual(settings.storeOwnerWpcomEmail, "[email protected]")
XCTAssertEqual(settings.storeOwnerWpcomUsername, "apiexamples")
XCTAssertEqual(settings.addPaymentMethodURL, URL(string: "https://wordpress.com/me/purchases/add-credit-card"))
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"master_user_wpcom_login": "apiexamples",
"master_user_email": "[email protected]",
"payment_methods": [],
"add_payment_method_url": "https://wordpress.com/me/purchases/add-credit-card",
"warnings": {
"payment_methods": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"expiry": "2030-12-31"
}
],
"add_payment_method_url": "https://wordpress.com/me/purchases/add-credit-card",
"warnings": {
"payment_methods": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"expiry": "2030-12-31"
}
],
"add_payment_method_url": "https://wordpress.com/me/purchases/add-credit-card",
"warnings": {
"payment_methods": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3112,7 +3112,8 @@ extension Networking.ShippingLabelAccountSettings {
selectedPaymentMethodID: CopiableProp<Int64> = .copy,
isEmailReceiptsEnabled: CopiableProp<Bool> = .copy,
paperSize: CopiableProp<ShippingLabelPaperSize> = .copy,
lastSelectedPackageID: CopiableProp<String> = .copy
lastSelectedPackageID: CopiableProp<String> = .copy,
addPaymentMethodURL: CopiableProp<URL> = .copy
) -> Networking.ShippingLabelAccountSettings {
let siteID = siteID ?? self.siteID
let canManagePayments = canManagePayments ?? self.canManagePayments
Expand All @@ -3126,6 +3127,7 @@ extension Networking.ShippingLabelAccountSettings {
let isEmailReceiptsEnabled = isEmailReceiptsEnabled ?? self.isEmailReceiptsEnabled
let paperSize = paperSize ?? self.paperSize
let lastSelectedPackageID = lastSelectedPackageID ?? self.lastSelectedPackageID
let addPaymentMethodURL = addPaymentMethodURL ?? self.addPaymentMethodURL

return Networking.ShippingLabelAccountSettings(
siteID: siteID,
Expand All @@ -3139,7 +3141,8 @@ extension Networking.ShippingLabelAccountSettings {
selectedPaymentMethodID: selectedPaymentMethodID,
isEmailReceiptsEnabled: isEmailReceiptsEnabled,
paperSize: paperSize,
lastSelectedPackageID: lastSelectedPackageID
lastSelectedPackageID: lastSelectedPackageID,
addPaymentMethodURL: addPaymentMethodURL
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
/// Uses the `id` for predefined packages or `name` for custom packages.
public let lastSelectedPackageID: String

/// URL to open the web view for adding new payment methods
public let addPaymentMethodURL: URL?

public init(siteID: Int64,
canManagePayments: Bool,
canEditSettings: Bool,
Expand All @@ -54,7 +57,8 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
selectedPaymentMethodID: Int64,
isEmailReceiptsEnabled: Bool,
paperSize: ShippingLabelPaperSize,
lastSelectedPackageID: String) {
lastSelectedPackageID: String,
addPaymentMethodURL: URL?) {
self.siteID = siteID
self.canManagePayments = canManagePayments
self.canEditSettings = canEditSettings
Expand All @@ -67,6 +71,7 @@ public struct ShippingLabelAccountSettings: Equatable, GeneratedFakeable, Genera
self.isEmailReceiptsEnabled = isEmailReceiptsEnabled
self.paperSize = paperSize
self.lastSelectedPackageID = lastSelectedPackageID
self.addPaymentMethodURL = addPaymentMethodURL
}
}

Expand All @@ -87,6 +92,11 @@ extension ShippingLabelAccountSettings: Decodable {
let storeOwnerWpcomEmail = try formMetaContainer.decode(String.self, forKey: .storeOwnerWpcomEmail)
let paymentMethods = try formMetaContainer.decodeIfPresent([ShippingLabelPaymentMethod].self, forKey: .paymentMethods) ?? []

var addPaymentMethodURL: URL?
if let addPaymentMethodURLString = try formMetaContainer.decodeIfPresent(String.self, forKey: .addPaymentMethodURL) {
addPaymentMethodURL = URL(string: addPaymentMethodURLString)
}

let formDataContainer = try container.nestedContainer(keyedBy: FormDataKeys.self, forKey: .formData)
let selectedPaymentMethodID = try formDataContainer.decode(Int64.self, forKey: .selectedPaymentMethodID)
let isEmailReceiptsEnabled = try formDataContainer.decode(Bool.self, forKey: .isEmailReceiptsEnabled)
Expand All @@ -107,7 +117,8 @@ extension ShippingLabelAccountSettings: Decodable {
selectedPaymentMethodID: selectedPaymentMethodID,
isEmailReceiptsEnabled: isEmailReceiptsEnabled,
paperSize: paperSize,
lastSelectedPackageID: lastSelectedPackageID)
lastSelectedPackageID: lastSelectedPackageID,
addPaymentMethodURL: addPaymentMethodURL)
}
}

Expand All @@ -134,6 +145,7 @@ private extension ShippingLabelAccountSettings {
case storeOwnerWpcomUsername = "master_user_wpcom_login"
case storeOwnerWpcomEmail = "master_user_email"
case paymentMethods = "payment_methods"
case addPaymentMethodURL = "add_payment_method_url"
}

private enum UserMetaKeys: String, CodingKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ extension WooAnalyticsEvent {

enum PaymentStep: String {
case started
case paymentMethodButtonTapped = "payment_method_button_tapped"
case paymentMethodTapped = "payment_method_tapped"
case addPaymentMethodButtonTapped = "add_payment_method_button_tapped"
case paymentMethodAdded = "payment_method_added"
case paymentMethodSelected = "payment_method_selected"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ private extension ApplicationPasswordAuthorizationWebViewController {
])

extendContentUnderSafeAreas()
webView.configureForSandboxEnvironment()

webView.publisher(for: \.estimatedProgress)
.sink { [weak self] progress in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ private extension AuthenticatedWebViewController {
])

extendContentUnderSafeAreas()
webView.configureForSandboxEnvironment()
}

func configureActivityIndicator() {
Expand Down
18 changes: 0 additions & 18 deletions WooCommerce/Classes/Extensions/WKWebView+Authenticated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,4 @@ extension WKWebView {

return try URLEncoding.default.encode(request, with: parameters)
}

/// For all test cases, to test against the staging server
/// please apply the following patch after replacing [secret] with a sandbox secret from the secret store.
///
func configureForSandboxEnvironment() {
#if DEBUG
if let cookie = HTTPCookie(properties: [
.domain: ".wordpress.com",
.path: "/",
.name: "store_sandbox",
.value: "[secret]",
.secure: "TRUE"
]) {
configuration.websiteDataStore.httpCookieStore.setCookie(cookie) {
}
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import protocol Storage.StorageManagerType
///
final class ShippingLabelPaymentMethodsViewModel: ObservableObject {

/// Indicates if the view model is updating the remote account settings or fetching remote account settings
/// Indicates if the view model is updating the remote account settings
///
@Published var isUpdating: Bool = false
@Published private(set) var isUpdating = false

/// Indicates if the view model is fetching remote account settings
///
@Published private(set) var isReloading: Bool = false

/// Shipping Label account settings from the remote API
///
Expand Down Expand Up @@ -59,6 +63,12 @@ final class ShippingLabelPaymentMethodsViewModel: ObservableObject {
accountSettings.canEditSettings
}

/// Retrieves URL to add payment method from account settings.
/// If none exists, returns the default URL.
var addPaymentMethodURL: URL {
accountSettings.addPaymentMethodURL ?? WooConstants.URLs.addPaymentMethodWCShip.asURL()
}

/// The URL path that will trigger the exit from the webview for adding a new payment method
///
let fetchPaymentMethodURLPath = "me/purchases/payment-methods"
Expand All @@ -78,6 +88,11 @@ final class ShippingLabelPaymentMethodsViewModel: ObservableObject {
isEmailReceiptsEnabled = accountSettings.isEmailReceiptsEnabled
}

func updateSettings(_ settings: ShippingLabelAccountSettings) {
accountSettings = settings
selectedPaymentMethodID = settings.selectedPaymentMethodID
}

func didSelectPaymentMethod(withID paymentMethodID: Int64) {
selectedPaymentMethodID = paymentMethodID
}
Expand Down Expand Up @@ -152,6 +167,20 @@ extension ShippingLabelPaymentMethodsViewModel {
}
return success ? newSettings : accountSettings
}

@MainActor
func syncWooShippingAccountSettings() async throws -> ShippingLabelAccountSettings {
isReloading = true
defer {
isReloading = false
}
let settings = try await withCheckedThrowingContinuation { continuation in
stores.dispatch(WooShippingAction.loadAccountSettings(siteID: accountSettings.siteID, completion: { result in
continuation.resume(with: result)
}))
}
return settings.accountSettings
}
}

// MARK: - Localization
Expand Down Expand Up @@ -183,7 +212,8 @@ extension ShippingLabelPaymentMethodsViewModel {
selectedPaymentMethodID: 11743265,
isEmailReceiptsEnabled: true,
paperSize: .label,
lastSelectedPackageID: "small_flat_box")
lastSelectedPackageID: "small_flat_box",
addPaymentMethodURL: nil)
}

static func samplePaymentMethods() -> [ShippingLabelPaymentMethod] {
Expand Down
Loading