This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTransactionsServiceRemote.swift
More file actions
183 lines (148 loc) · 7.29 KB
/
TransactionsServiceRemote.swift
File metadata and controls
183 lines (148 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import Foundation
import WordPressShared
@objc public class TransactionsServiceRemote: ServiceRemoteWordPressComREST {
public enum ResponseError: Error {
case decodingFailure
}
private enum Constants {
static let freeDomainPaymentMethod = "WPCOM_Billing_WPCOM"
}
@objc public func getSupportedCountries(success: @escaping ([WPCountry]) -> Void,
failure: @escaping (Error) -> Void) {
let endPoint = "me/transactions/supported-countries/"
let servicePath = path(forEndpoint: endPoint, withVersion: ._1_1)
wordPressComRestApi.GETData(servicePath, parameters: nil) { result in
result
.flatMap { data, _ in
Result {
try JSONDecoder.apiDecoder.decode([WPCountry].self, from: data)
}
}
.invoke(success, or: failure)
}
}
/// Creates a shopping cart for a domain purchase
/// - Parameters:
/// - siteID: id of the current site
/// - domainSuggestion: suggested new domain to purchase
/// - temporary: true if the card is temporary, false otherwise
/// - privacyProtectionEnabled: true if privacy protection on the given domain is enabled
private func createDomainShoppingCart(siteID: Int,
domainSuggestion: DomainSuggestion,
privacyProtectionEnabled: Bool,
temporary: Bool,
success: @escaping (CartResponse) -> Void,
failure: @escaping (Error) -> Void) {
let endPoint = "me/shopping-cart/\(siteID)"
let urlPath = path(forEndpoint: endPoint, withVersion: ._1_1)
var productDictionary: [String: AnyObject] = ["product_id": domainSuggestion.productID as AnyObject,
"meta": domainSuggestion.domainName as AnyObject]
if privacyProtectionEnabled {
productDictionary["extra"] = ["privacy": true] as AnyObject
}
let parameters: [String: AnyObject] = ["temporary": (temporary ? "true" : "false") as AnyObject,
"products": [productDictionary] as AnyObject]
wordPressComRestApi.POST(urlPath,
parameters: parameters,
success: { (response, _) in
guard let jsonResponse = response as? [String: AnyObject],
let cart = CartResponse(jsonDictionary: jsonResponse),
!cart.products.isEmpty else {
failure(TransactionsServiceRemote.ResponseError.decodingFailure)
return
}
success(cart)
}) { (error, _) in
failure(error)
}
}
/// Creates a temporary shopping cart for a domain purchase
public func createTemporaryDomainShoppingCart(siteID: Int,
domainSuggestion: DomainSuggestion,
privacyProtectionEnabled: Bool,
success: @escaping (CartResponse) -> Void,
failure: @escaping (Error) -> Void) {
createDomainShoppingCart(siteID: siteID,
domainSuggestion: domainSuggestion,
privacyProtectionEnabled: privacyProtectionEnabled,
temporary: true,
success: success,
failure: failure)
}
/// Creates a persistent shopping cart for a domain purchase
public func createPersistentDomainShoppingCart(siteID: Int,
domainSuggestion: DomainSuggestion,
privacyProtectionEnabled: Bool,
success: @escaping (CartResponse) -> Void,
failure: @escaping (Error) -> Void) {
createDomainShoppingCart(siteID: siteID,
domainSuggestion: domainSuggestion,
privacyProtectionEnabled: privacyProtectionEnabled,
temporary: false,
success: success,
failure: failure)
}
public func redeemCartUsingCredits(cart: CartResponse,
domainContactInformation: [String: String],
success: @escaping () -> Void,
failure: @escaping (Error) -> Void) {
let endPoint = "me/transactions"
let urlPath = path(forEndpoint: endPoint, withVersion: ._1_1)
let paymentDict = ["payment_method": Constants.freeDomainPaymentMethod]
let parameters: [String: AnyObject] = ["domain_details": domainContactInformation as AnyObject,
"cart": cart.jsonRepresentation() as AnyObject,
"payment": paymentDict as AnyObject]
wordPressComRestApi.POST(urlPath, parameters: parameters, success: { (_, _) in
success()
}) { (error, _) in
failure(error)
}
}
}
public struct CartResponse {
let blogID: Int
let cartKey: Int
let products: [Product]
init?(jsonDictionary: [String: AnyObject]) {
guard
let cartKey = jsonDictionary["cart_key"] as? Int,
let blogID = jsonDictionary["blog_id"] as? Int,
let products = jsonDictionary["products"] as? [[String: AnyObject]]
else {
return nil
}
let mappedProducts = products.compactMap { (product) -> Product? in
guard
let productID = product["product_id"] as? Int else {
return nil
}
let meta = product["meta"] as? String
let extra = product["extra"] as? [String: AnyObject]
return Product(productID: productID, meta: meta, extra: extra)
}
self.blogID = blogID
self.cartKey = cartKey
self.products = mappedProducts
}
fileprivate func jsonRepresentation() -> [String: AnyObject] {
return ["blog_id": blogID as AnyObject,
"cart_key": cartKey as AnyObject,
"products": products.map { $0.jsonRepresentation() } as AnyObject]
}
}
public struct Product {
let productID: Int
let meta: String?
let extra: [String: AnyObject]?
fileprivate func jsonRepresentation() -> [String: AnyObject] {
var returnDict: [String: AnyObject] = [:]
returnDict["product_id"] = productID as AnyObject
if let meta = meta {
returnDict["meta"] = meta as AnyObject
}
if let extra = extra {
returnDict["extra"] = extra as AnyObject
}
return returnDict
}
}