@@ -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
134152public struct CartResponse {
0 commit comments