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

Commit e40f9c3

Browse files
authored
Merge pull request #628 from wordpress-mobile/task/21636-create-functionality-to-add-domain-with-a-plan-to-a-cart
Created functionality to add domain with a plan to a cart
2 parents a2321ae + b31e48f commit e40f9c3

File tree

3 files changed

+73
-49
lines changed

3 files changed

+73
-49
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ _None._
4848

4949
_None._
5050

51+
## 8.6.0
52+
53+
### New Features
54+
55+
- Add `createShoppingCart` method to add domains and plans when creating a new cart [#628]
56+
5157
## 8.5.2
5258

5359
### Bug Fixes

WordPressKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Pod::Spec.new do |s|
44
s.name = 'WordPressKit'
5-
s.version = '8.5.2'
5+
s.version = '8.6.0-beta.1'
66

77
s.summary = 'WordPressKit offers a clean and simple WordPress.com and WordPress.org API.'
88
s.description = <<-DESC

WordPressKit/TransactionsServiceRemote.swift

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,77 +36,58 @@ import WordPressShared
3636
})
3737
}
3838

39-
/// Creates a shopping cart for a domain purchase
39+
/// Creates a shopping cart with products
4040
/// - Parameters:
4141
/// - siteID: id of the current site
42-
/// - domainSuggestion: suggested new domain to purchase
42+
/// - products: an array of products to be added to the newly created cart
4343
/// - temporary: true if the card is temporary, false otherwise
44-
/// - privacyProtectionEnabled: true if privacy protection on the given domain is enabled
45-
private func createDomainShoppingCart(siteID: Int,
46-
domainSuggestion: DomainSuggestion,
47-
privacyProtectionEnabled: Bool,
48-
temporary: Bool,
49-
success: @escaping (CartResponse) -> Void,
50-
failure: @escaping (Error) -> Void) {
44+
public func createShoppingCart(siteID: Int,
45+
products: [TransactionsServiceProduct],
46+
temporary: Bool,
47+
success: @escaping (CartResponse) -> Void,
48+
failure: @escaping (Error) -> Void) {
5149

5250
let endPoint = "me/shopping-cart/\(siteID)"
5351
let urlPath = path(forEndpoint: endPoint, withVersion: ._1_1)
5452

55-
var productDictionary: [String: AnyObject] = ["product_id": domainSuggestion.productID as AnyObject,
56-
"meta": domainSuggestion.domainName as AnyObject]
53+
var productsDictionary: [[String: AnyObject]] = []
5754

58-
if privacyProtectionEnabled {
59-
productDictionary["extra"] = ["privacy": true] as AnyObject
55+
for product in products {
56+
switch product {
57+
case .domain(let domainSuggestion, let privacyProtectionEnabled):
58+
productsDictionary.append(["product_id": domainSuggestion.productID as AnyObject,
59+
"meta": domainSuggestion.domainName as AnyObject,
60+
"extra": ["privacy": privacyProtectionEnabled] as AnyObject])
61+
62+
case .plan(let productId):
63+
productsDictionary.append(["product_id": productId as AnyObject])
64+
case .other(let productDict):
65+
productsDictionary.append(productDict)
66+
}
6067
}
6168

6269
let parameters: [String: AnyObject] = ["temporary": (temporary ? "true" : "false") as AnyObject,
63-
"products": [productDictionary] as AnyObject]
70+
"products": productsDictionary as AnyObject]
6471

6572
wordPressComRestApi.POST(urlPath,
6673
parameters: parameters,
6774
success: { (response, _) in
6875

69-
guard let jsonResponse = response as? [String: AnyObject],
70-
let cart = CartResponse(jsonDictionary: jsonResponse),
71-
!cart.products.isEmpty else {
76+
guard let jsonResponse = response as? [String: AnyObject],
77+
let cart = CartResponse(jsonDictionary: jsonResponse),
78+
!cart.products.isEmpty else {
7279

73-
failure(TransactionsServiceRemote.ResponseError.decodingFailure)
74-
return
75-
}
80+
failure(TransactionsServiceRemote.ResponseError.decodingFailure)
81+
return
82+
}
7683

77-
success(cart)
84+
success(cart)
7885
}) { (error, _) in
7986
failure(error)
8087
}
8188
}
8289

83-
/// Creates a temporary shopping cart for a domain purchase
84-
public func createTemporaryDomainShoppingCart(siteID: Int,
85-
domainSuggestion: DomainSuggestion,
86-
privacyProtectionEnabled: Bool,
87-
success: @escaping (CartResponse) -> Void,
88-
failure: @escaping (Error) -> Void) {
89-
createDomainShoppingCart(siteID: siteID,
90-
domainSuggestion: domainSuggestion,
91-
privacyProtectionEnabled: privacyProtectionEnabled,
92-
temporary: true,
93-
success: success,
94-
failure: failure)
95-
}
96-
97-
/// Creates a persistent shopping cart for a domain purchase
98-
public func createPersistentDomainShoppingCart(siteID: Int,
99-
domainSuggestion: DomainSuggestion,
100-
privacyProtectionEnabled: Bool,
101-
success: @escaping (CartResponse) -> Void,
102-
failure: @escaping (Error) -> Void) {
103-
createDomainShoppingCart(siteID: siteID,
104-
domainSuggestion: domainSuggestion,
105-
privacyProtectionEnabled: privacyProtectionEnabled,
106-
temporary: false,
107-
success: success,
108-
failure: failure)
109-
}
90+
// MARK: - Domains
11091

11192
public func redeemCartUsingCredits(cart: CartResponse,
11293
domainContactInformation: [String: String],
@@ -129,6 +110,43 @@ import WordPressShared
129110
failure(error)
130111
}
131112
}
113+
114+
/// Creates a temporary shopping cart for a domain purchase
115+
@available(*, deprecated, message: "Use createShoppingCart(_:) and pass an array of specific products instead")
116+
public func createTemporaryDomainShoppingCart(siteID: Int,
117+
domainSuggestion: DomainSuggestion,
118+
privacyProtectionEnabled: Bool,
119+
success: @escaping (CartResponse) -> Void,
120+
failure: @escaping (Error) -> Void) {
121+
createShoppingCart(siteID: siteID,
122+
products: [.domain(domainSuggestion, privacyProtectionEnabled)],
123+
temporary: true,
124+
success: success,
125+
failure: failure)
126+
}
127+
128+
/// Creates a persistent shopping cart for a domain purchase
129+
@available(*, deprecated, message: "Use createShoppingCart(_:) and pass an array of specific products instead")
130+
public func createPersistentDomainShoppingCart(siteID: Int,
131+
domainSuggestion: DomainSuggestion,
132+
privacyProtectionEnabled: Bool,
133+
success: @escaping (CartResponse) -> Void,
134+
failure: @escaping (Error) -> Void) {
135+
createShoppingCart(siteID: siteID,
136+
products: [.domain(domainSuggestion, privacyProtectionEnabled)],
137+
temporary: false,
138+
success: success,
139+
failure: failure)
140+
}
141+
}
142+
143+
public enum TransactionsServiceProduct {
144+
public typealias ProductId = Int
145+
public typealias PrivacyProtection = Bool
146+
147+
case domain(DomainSuggestion, PrivacyProtection)
148+
case plan(ProductId)
149+
case other([String: AnyObject])
132150
}
133151

134152
public struct CartResponse {

0 commit comments

Comments
 (0)