11import Foundation
22
33public protocol PaymentRemoteProtocol {
4+ func loadPlan( thatMatchesID productID: Int64 ) async throws -> WPComPlan
5+
46 func createCart( siteID: Int64 , productID: Int64 ) async throws -> CreateCartResponse
57}
68
79/// WPCOM Payment Endpoints
810///
911public class PaymentRemote : Remote , PaymentRemoteProtocol {
12+ public func loadPlan( thatMatchesID productID: Int64 ) async throws -> WPComPlan {
13+ let path = Path . products
14+ let request = DotcomRequest ( wordpressApiVersion: . mark1_5, method: . get, path: path)
15+ let plans : [ WPComPlan ] = try await enqueue ( request)
16+ guard let plan = plans. first ( where: { $0. productID == productID } ) else {
17+ throw LoadPlanError . noMatchingPlan
18+ }
19+ return plan
20+ }
21+
1022 public func createCart( siteID: Int64 , productID: Int64 ) async throws -> CreateCartResponse {
1123 let path = " \( Path . cartCreation) / \( siteID) "
1224
@@ -24,12 +36,29 @@ public class PaymentRemote: Remote, PaymentRemoteProtocol {
2436 }
2537}
2638
39+ public struct WPComPlan : Decodable {
40+ public let productID : Int64
41+ public let name : String
42+ public let formattedPrice : String
43+
44+ private enum CodingKeys : String , CodingKey {
45+ case productID = " product_id "
46+ case name = " product_name "
47+ case formattedPrice = " formatted_price "
48+ }
49+ }
50+
51+ public enum LoadPlanError : Error {
52+ case noMatchingPlan
53+ }
54+
2755public struct CreateCartResponse : Decodable { }
2856
2957// MARK: - Constants
3058//
3159private extension PaymentRemote {
3260 enum Path {
61+ static let products = " plans "
3362 static let cartCreation = " me/shopping-cart "
3463 }
3564}
0 commit comments