Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit e04c504

Browse files
committed
Add support for redeeming a cart and for changing a primary domain on a site.
1 parent b7b4ad2 commit e04c504

File tree

2 files changed

+94
-11
lines changed

2 files changed

+94
-11
lines changed

WordPressKit/DomainsServiceRemote.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
5959
})
6060
}
6161

62+
public func setPrimaryDomainForSite(siteID: Int, domain: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
63+
let endpoint = "sites/\(siteID)/domains/primary"
64+
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
65+
66+
let parameters: [String: AnyObject] = ["domain": domain as AnyObject]
67+
68+
wordPressComRestApi.POST(path, parameters: parameters,
69+
success: { response, _ in
70+
71+
success()
72+
}, failure: { error, _ in
73+
74+
failure(error)
75+
})
76+
}
77+
6278
@objc public func getStates(for countryCode: String,
6379
success: @escaping ([State]) -> Void,
6480
failure: @escaping (Error) -> Void) {

WordPressKit/TransactionsServiceRemote.swift

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ import WordPressShared
33
import CocoaLumberjack
44

55
@objc public class TransactionsServiceRemote: ServiceRemoteWordPressComREST {
6-
6+
77
public enum ResponseError: Error {
88
case decodingFailure
99
}
10-
10+
11+
private enum Constants {
12+
static let privateRegistrationProductID = 16
13+
static let freeDomainPaymentMethod = "WPCOM_Billing_WPCOM"
14+
}
15+
1116
@objc public func getSupportedCountries(success: @escaping ([Country]) -> Void,
1217
failure: @escaping (Error) -> Void) {
1318
let endPoint = "me/transactions/supported-countries/"
@@ -35,44 +40,64 @@ import CocoaLumberjack
3540

3641
public func createShoppingCart(siteID: Int,
3742
domainSuggestion: DomainSuggestion,
38-
success: @escaping (String) -> Void,
43+
privacyProtectionEnabled: Bool,
44+
success: @escaping (CartResponse) -> Void,
3945
failure: @escaping (Error) -> Void) {
4046

4147
let endPoint = "me/shopping-cart/\(siteID)"
4248
let urlPath = path(forEndpoint: endPoint, withVersion: ._1_1)
4349

44-
let productsArray = [["product_id": domainSuggestion.productID,
45-
"meta": domainSuggestion.domainName]]
50+
let productsArray: [[String: AnyObject]]
4651

47-
let parameters: [String: AnyObject] = ["temporary": "false" as AnyObject,
52+
let productDictionary: [String: AnyObject] = ["product_id": domainSuggestion.productID as AnyObject,
53+
"meta": domainSuggestion.domainName as AnyObject]
54+
55+
if privacyProtectionEnabled {
56+
let privacyProduct: [String: AnyObject] = ["product_id": Constants.privateRegistrationProductID as AnyObject,
57+
"meta": domainSuggestion.domainName as AnyObject]
58+
59+
productsArray = [productDictionary, privacyProduct]
60+
} else {
61+
productsArray = [productDictionary]
62+
}
63+
64+
let parameters: [String: AnyObject] = ["temporary": "true" as AnyObject,
4865
"products": productsArray as AnyObject]
4966

5067
wordPressComRestApi.POST(urlPath,
5168
parameters: parameters,
5269
success: { (response, _) in
53-
guard let cartKey = response["cart_key"] as? String else {
70+
71+
72+
guard let jsonResponse = response as? [String: AnyObject],
73+
let cart = CartResponse(jsonDictionary: jsonResponse),
74+
!cart.products.isEmpty else {
75+
5476
failure(TransactionsServiceRemote.ResponseError.decodingFailure)
5577
return
5678
}
5779

58-
success(cartKey)
80+
success(cart)
5981
}) { (error, response) in
6082
failure(error)
6183
}
6284

6385

6486
}
6587

66-
public func redeemCartUsingCredits(cartID: String,
88+
public func redeemCartUsingCredits(cart: CartResponse,
89+
domainContactInformation: [String: String],
6790
success: @escaping () -> Void,
6891
failure: @escaping (Error) -> Void) {
6992

7093
let endPoint = "me/transactions"
7194

7295
let urlPath = path(forEndpoint: endPoint, withVersion: ._1_1)
7396

74-
let paymentDict = ["payment_method": "WPCOM_Billing_WPCOM"]
75-
let parameters: [String: AnyObject] = ["cart": cartID as AnyObject,
97+
let paymentDict = ["payment_method": Constants.freeDomainPaymentMethod]
98+
99+
let parameters: [String: AnyObject] = ["domain_details": domainContactInformation as AnyObject,
100+
"cart": cart.jsonRepresentation() as AnyObject,
76101
"payment": paymentDict as AnyObject]
77102

78103
wordPressComRestApi.POST(urlPath, parameters: parameters, success: { (response, _) in
@@ -82,3 +107,45 @@ import CocoaLumberjack
82107
}
83108
}
84109
}
110+
111+
public struct CartResponse: Codable {
112+
let blogID: Int
113+
let cartKey: String
114+
let products: [Product]
115+
116+
init?(jsonDictionary: [String: AnyObject]) {
117+
guard let cartKey = jsonDictionary["cart_key"] as? String,
118+
let blogID = jsonDictionary["blog_id"] as? Int,
119+
let products = jsonDictionary["products"] as? [[String: AnyObject]] else {
120+
return nil
121+
}
122+
123+
let mappedProducts = products.compactMap { (product) -> Product? in
124+
guard let productID = product["product_id"] as? Int,
125+
let meta = product["meta"] as? String else {
126+
return nil
127+
}
128+
129+
return Product(productID: productID, meta: meta)
130+
}
131+
132+
guard mappedProducts.count == products.count else {
133+
return nil
134+
}
135+
136+
self.blogID = blogID
137+
self.cartKey = cartKey
138+
self.products = mappedProducts
139+
}
140+
141+
fileprivate func jsonRepresentation() -> [String: AnyObject] {
142+
return ["blog_id": blogID as AnyObject,
143+
"cart_key": cartKey as AnyObject,
144+
"products": products.map { return ["product_id": $0.productID, "meta": $0.meta] } as AnyObject ]
145+
}
146+
}
147+
148+
public struct Product: Codable {
149+
let productID: Int
150+
let meta: String
151+
}

0 commit comments

Comments
 (0)