Skip to content

Commit

Permalink
Encodable for setupToken payment source, pr feedback
Browse files Browse the repository at this point in the history
Co-authored-by: Ricardo Herrera <[email protected]>
  • Loading branch information
KunJeongPark and richherrera committed Dec 12, 2023
1 parent 9363cc4 commit d8f8f66
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 38 deletions.
10 changes: 5 additions & 5 deletions Demo/Demo/Models/PaymentTokenResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ struct Customer: Codable, Equatable {

struct PaymentSource: Decodable, Equatable {

var card: BasicCard?
var paypal: BasicPayPal?
var card: CardPaymentSource?
var paypal: PayPalPaymentSource?
}

struct BasicCard: Decodable, Equatable {
struct CardPaymentSource: Decodable, Equatable {

let brand: String?
let lastDigits: String
let expiry: String
}

struct BasicPayPal: Decodable, Equatable {
struct PayPalPaymentSource: Decodable, Equatable {

let emailAddress: String
}
87 changes: 62 additions & 25 deletions Demo/Demo/Models/SetupTokenRequest.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,68 @@
import Foundation

enum PaymentSourceType {
struct VaultExperienceContext: Encodable {

let returnUrl = "sdk.ios.paypal://vault/success"
let cancelUrl = "sdk.ios.paypal://vault/cancel"

enum CodingKeys: String, CodingKey {
case returnUrl = "return_url"
case cancelUrl = "cancel_url"
}
}

struct PayPal: Encodable {

var usageType: String
let experienceContext = VaultExperienceContext()

enum CodingKeys: String, CodingKey {
case usageType = "usage_type"
case experienceContext = "experience_context"
}
}

enum PaymentSourceType: Encodable {
case card
case paypal(usageType: String)

private enum CodingKeys: String, CodingKey {
case card, paypal
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .card:
try container.encode(EmptyBodyParams(), forKey: .card)
case .paypal(let usageType):
try container.encode(PayPal(usageType: usageType), forKey: .paypal)
}
}
}

struct SetUpTokenRequest {

struct VaultCustomer: Encodable {

var id: String?

private enum CodingKeys: String, CodingKey {
case id
}
}

struct SetupTokenRequestBody: Encodable {

var customer: VaultCustomer?
let paymentSource: PaymentSourceType

enum CodingKeys: String, CodingKey {
case paymentSource = "payment_source"
case customer
}
}

struct SetUpTokenRequest: Encodable {

let customerID: String?
let paymentSource: PaymentSourceType

Expand All @@ -21,28 +77,9 @@ struct SetUpTokenRequest {
var headers: [String: String] {
["Content-Type": "application/json"]
}

var body: Data? {
var paymentSourceDict: [String: Any] = [:]

switch paymentSource {
case .card:
paymentSourceDict["card"] = [:]
case .paypal(let usageType):
paymentSourceDict["paypal"] = [
"usage_type": usageType,
"experience_context": [
"return_url": "sdk.ios.paypal://vault/success",
"cancel_url": "sdk.ios.paypal://vault/cancel"
]
]
}

let requestBody: [String: Any] = [
"customer": ["id": customerID],
"payment_source": paymentSourceDict
]

return try? JSONSerialization.data(withJSONObject: requestBody)
var body: Data? {
let requestBodyParam = SetupTokenRequestBody(customer: VaultCustomer(id: customerID), paymentSource: paymentSource)
return try? JSONEncoder().encode(requestBodyParam)
}
}
6 changes: 1 addition & 5 deletions Demo/Demo/Models/SetupTokenResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ struct SetUpTokenResponse: Decodable, Equatable {
let customer: Customer?
let links: [Link]
var paypalURL: String? {
if let link = links.first(where: { $0.rel == "approve" }) {
let url = link.href
return url
}
return nil
links.first { $0.rel == "approve" }?.href
}

struct Customer: Decodable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import PayPalWebPayments

struct PayPalVaultResultView: View {

@ObservedObject var paypalVaultViewModel: PayPalVaultViewModel
@ObservedObject var viewModel: PayPalVaultViewModel

var body: some View {
switch paypalVaultViewModel.state.paypalVaultTokenResponse {
switch viewModel.state.paypalVaultTokenResponse {
case .idle, .loading:
EmptyView()
case .loaded(let vaultResult):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct PayPalVaultView: View {
.buttonStyle(RoundedBlueButtonStyle())
.padding()
}
PayPalVaultResultView(paypalVaultViewModel: paypalVaultViewModel)
PayPalVaultResultView(viewModel: paypalVaultViewModel)
if let paypalVaultResult = paypalVaultViewModel.state.paypalVaultToken {
CreatePaymentTokenView(
vaultViewModel: paypalVaultViewModel,
Expand Down

0 comments on commit d8f8f66

Please sign in to comment.