Skip to content

Commit d8f8f66

Browse files
Encodable for setupToken payment source, pr feedback
Co-authored-by: Ricardo Herrera <[email protected]>
1 parent 9363cc4 commit d8f8f66

File tree

5 files changed

+71
-38
lines changed

5 files changed

+71
-38
lines changed

Demo/Demo/Models/PaymentTokenResponse.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ struct Customer: Codable, Equatable {
1414

1515
struct PaymentSource: Decodable, Equatable {
1616

17-
var card: BasicCard?
18-
var paypal: BasicPayPal?
17+
var card: CardPaymentSource?
18+
var paypal: PayPalPaymentSource?
1919
}
2020

21-
struct BasicCard: Decodable, Equatable {
22-
21+
struct CardPaymentSource: Decodable, Equatable {
22+
2323
let brand: String?
2424
let lastDigits: String
2525
let expiry: String
2626
}
2727

28-
struct BasicPayPal: Decodable, Equatable {
28+
struct PayPalPaymentSource: Decodable, Equatable {
2929

3030
let emailAddress: String
3131
}
Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,68 @@
11
import Foundation
22

3-
enum PaymentSourceType {
3+
struct VaultExperienceContext: Encodable {
4+
5+
let returnUrl = "sdk.ios.paypal://vault/success"
6+
let cancelUrl = "sdk.ios.paypal://vault/cancel"
7+
8+
enum CodingKeys: String, CodingKey {
9+
case returnUrl = "return_url"
10+
case cancelUrl = "cancel_url"
11+
}
12+
}
13+
14+
struct PayPal: Encodable {
15+
16+
var usageType: String
17+
let experienceContext = VaultExperienceContext()
18+
19+
enum CodingKeys: String, CodingKey {
20+
case usageType = "usage_type"
21+
case experienceContext = "experience_context"
22+
}
23+
}
24+
25+
enum PaymentSourceType: Encodable {
426
case card
527
case paypal(usageType: String)
28+
29+
private enum CodingKeys: String, CodingKey {
30+
case card, paypal
31+
}
32+
33+
func encode(to encoder: Encoder) throws {
34+
var container = encoder.container(keyedBy: CodingKeys.self)
35+
switch self {
36+
case .card:
37+
try container.encode(EmptyBodyParams(), forKey: .card)
38+
case .paypal(let usageType):
39+
try container.encode(PayPal(usageType: usageType), forKey: .paypal)
40+
}
41+
}
642
}
743

8-
struct SetUpTokenRequest {
9-
44+
struct VaultCustomer: Encodable {
45+
46+
var id: String?
47+
48+
private enum CodingKeys: String, CodingKey {
49+
case id
50+
}
51+
}
52+
53+
struct SetupTokenRequestBody: Encodable {
54+
55+
var customer: VaultCustomer?
56+
let paymentSource: PaymentSourceType
57+
58+
enum CodingKeys: String, CodingKey {
59+
case paymentSource = "payment_source"
60+
case customer
61+
}
62+
}
63+
64+
struct SetUpTokenRequest: Encodable {
65+
1066
let customerID: String?
1167
let paymentSource: PaymentSourceType
1268

@@ -21,28 +77,9 @@ struct SetUpTokenRequest {
2177
var headers: [String: String] {
2278
["Content-Type": "application/json"]
2379
}
24-
25-
var body: Data? {
26-
var paymentSourceDict: [String: Any] = [:]
2780

28-
switch paymentSource {
29-
case .card:
30-
paymentSourceDict["card"] = [:]
31-
case .paypal(let usageType):
32-
paymentSourceDict["paypal"] = [
33-
"usage_type": usageType,
34-
"experience_context": [
35-
"return_url": "sdk.ios.paypal://vault/success",
36-
"cancel_url": "sdk.ios.paypal://vault/cancel"
37-
]
38-
]
39-
}
40-
41-
let requestBody: [String: Any] = [
42-
"customer": ["id": customerID],
43-
"payment_source": paymentSourceDict
44-
]
45-
46-
return try? JSONSerialization.data(withJSONObject: requestBody)
81+
var body: Data? {
82+
let requestBodyParam = SetupTokenRequestBody(customer: VaultCustomer(id: customerID), paymentSource: paymentSource)
83+
return try? JSONEncoder().encode(requestBodyParam)
4784
}
4885
}

Demo/Demo/Models/SetupTokenResponse.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ struct SetUpTokenResponse: Decodable, Equatable {
1010
let customer: Customer?
1111
let links: [Link]
1212
var paypalURL: String? {
13-
if let link = links.first(where: { $0.rel == "approve" }) {
14-
let url = link.href
15-
return url
16-
}
17-
return nil
13+
links.first { $0.rel == "approve" }?.href
1814
}
1915

2016
struct Customer: Decodable {

Demo/Demo/SwiftUIComponents/PayPalVaultViews/PayPalVaultResultView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import PayPalWebPayments
33

44
struct PayPalVaultResultView: View {
55

6-
@ObservedObject var paypalVaultViewModel: PayPalVaultViewModel
6+
@ObservedObject var viewModel: PayPalVaultViewModel
77

88
var body: some View {
9-
switch paypalVaultViewModel.state.paypalVaultTokenResponse {
9+
switch viewModel.state.paypalVaultTokenResponse {
1010
case .idle, .loading:
1111
EmptyView()
1212
case .loaded(let vaultResult):

Demo/Demo/SwiftUIComponents/PayPalVaultViews/PayPalVaultView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct PayPalVaultView: View {
2323
.buttonStyle(RoundedBlueButtonStyle())
2424
.padding()
2525
}
26-
PayPalVaultResultView(paypalVaultViewModel: paypalVaultViewModel)
26+
PayPalVaultResultView(viewModel: paypalVaultViewModel)
2727
if let paypalVaultResult = paypalVaultViewModel.state.paypalVaultToken {
2828
CreatePaymentTokenView(
2929
vaultViewModel: paypalVaultViewModel,

0 commit comments

Comments
 (0)