Skip to content

Commit

Permalink
setupTokenPaymentSource
Browse files Browse the repository at this point in the history
  • Loading branch information
KunJeongPark committed Nov 30, 2023
1 parent 29307de commit 8d1cc4e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
26 changes: 19 additions & 7 deletions Demo/Demo/Models/SetupTokenRequest.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import Foundation

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

struct SetUpTokenRequest {

let customerID: String?

let paymentSource: PaymentSourceType

var path: String {
"/setup_tokens/"
}
Expand All @@ -17,13 +23,19 @@ struct SetUpTokenRequest {
}

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

switch paymentSource {
case .card:
paymentSourceDict["card"] = [:]
case .paypal(let usageType):
paymentSourceDict["paypal"] = ["usage_type": usageType]
paymentSourceDict["experience_context"] = ["return_url": "https://example.com/returnUrl", "cance_url": "https://example.com/cancelUrl"]
}

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

return try? JSONSerialization.data(withJSONObject: requestBody)
Expand Down
6 changes: 6 additions & 0 deletions Demo/Demo/Models/SetupTokenResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ struct SetUpTokenResponse: Decodable, Equatable {

let id, status: String
let customer: Customer?
let links: [Link]

struct Customer: Decodable {

let id: String
}

struct Link: Decodable {

let href, rel, method: String
}
}
2 changes: 1 addition & 1 deletion Demo/Demo/Networking/DemoMerchantAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class DemoMerchantAPI {
// TODO: pass in headers depending on integration type
// Different request struct or integration type property
// in SetUpTokenRequest to conditionally add header
let request = SetUpTokenRequest(customerID: customerID)
let request = SetUpTokenRequest(customerID: customerID, paymentSource: .card)
let urlRequest = try createSetupTokenUrlRequest(
setupTokenRequest: request,
environment: DemoSettings.environment,
Expand Down

0 comments on commit 8d1cc4e

Please sign in to comment.