Skip to content

Commit 11da801

Browse files
committed
vault with purchase create order PayPal
1 parent dbc73a7 commit 11da801

File tree

6 files changed

+109
-13
lines changed

6 files changed

+109
-13
lines changed

Demo/Demo/Models/CreateOrderParams.swift

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct CreateOrderParams: Encodable {
33
let applicationContext: ApplicationContext?
44
let intent: String
55
var purchaseUnits: [PurchaseUnit]?
6-
var paymentSource: VaultCardPaymentSource?
6+
var paymentSource: VaultPaymentSource?
77
}
88

99
struct ApplicationContext: Codable {
@@ -17,6 +17,38 @@ struct ApplicationContext: Codable {
1717
}
1818
}
1919

20+
enum VaultPaymentSource: Encodable {
21+
case card(VaultCardPaymentSource)
22+
case paypal(VaultPayPalPaymentSource)
23+
24+
func encode(to encoder: Encoder) throws {
25+
var container = encoder.singleValueContainer()
26+
switch self {
27+
case .card(let cardSource):
28+
try container.encode(cardSource)
29+
case .paypal(let paypalSource):
30+
try container.encode(paypalSource)
31+
}
32+
}
33+
}
34+
35+
struct VaultPayPalPaymentSource: Encodable {
36+
37+
let paypal: VaultPayPal
38+
}
39+
40+
struct VaultPayPal: Encodable {
41+
42+
let attributes: Attributes
43+
let experienceContext: ExperienceContext
44+
}
45+
46+
struct ExperienceContext: Encodable {
47+
48+
let returnURL: String
49+
let cancelURL: String
50+
}
51+
2052
struct VaultCardPaymentSource: Encodable {
2153

2254
let card: VaultCard
@@ -30,15 +62,17 @@ struct VaultCard: Encodable {
3062
struct Attributes: Encodable {
3163

3264
let vault: Vault
33-
let customer: CardVaultCustomer?
65+
let customer: VaultCustomer?
3466
}
3567

3668
struct Vault: Encodable {
3769

3870
let storeInVault: String
71+
let usageType: String?
72+
let customerType: String?
3973
}
4074

41-
struct CardVaultCustomer: Encodable {
75+
struct VaultCustomer: Encodable {
4276

4377
let id: String
4478
}

Demo/Demo/Models/Order.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ struct Order: Codable, Equatable {
33
let id: String
44
let status: String
55
var paymentSource: PaymentSource?
6-
6+
var links: [Link]?
7+
78
struct PaymentSource: Codable, Equatable {
89

910
let card: Card?
1011
let paypal: PayPal?
1112
}
1213

14+
struct Link: Codable, Equatable {
15+
16+
let href: String?
17+
let rel, method: String?
18+
}
19+
1320
init(id: String, status: String, paymentSource: PaymentSource? = nil) {
1421
self.id = id
1522
self.status = status
@@ -25,7 +32,8 @@ struct Order: Codable, Equatable {
2532

2633
struct PayPal: Codable, Equatable {
2734

28-
let emailAddress: String
35+
let emailAddress: String?
36+
let attributes: Attributes?
2937
}
3038

3139
struct Attributes: Codable, Equatable {

Demo/Demo/SwiftUIComponents/PayPalWebViews/CreateOrderPayPalWebView.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ struct CreateOrderPayPalWebView: View {
55
@ObservedObject var paypalWebViewModel: PayPalWebViewModel
66

77
@State private var selectedIntent: Intent = .authorize
8+
@State private var vaultCustomerID: String = ""
9+
@State var shouldVaultSelected = false
810

911
let selectedMerchantIntegration: MerchantIntegration
1012

@@ -22,6 +24,11 @@ struct CreateOrderPayPalWebView: View {
2224
Text("CAPTURE").tag(Intent.capture)
2325
}
2426
.pickerStyle(SegmentedPickerStyle())
27+
HStack {
28+
Toggle("Should Vault with Purchase", isOn: $shouldVaultSelected)
29+
Spacer()
30+
}
31+
FloatingLabelTextField(placeholder: "Vault Customer ID (Optional)", text: $vaultCustomerID)
2532
ZStack {
2633
Button("Create an Order") {
2734
Task {
@@ -30,7 +37,10 @@ struct CreateOrderPayPalWebView: View {
3037
try await paypalWebViewModel.createOrder(
3138
amount: "10.00",
3239
selectedMerchantIntegration: DemoSettings.merchantIntegration,
33-
intent: selectedIntent.rawValue)
40+
intent: selectedIntent.rawValue,
41+
shouldVault: shouldVaultSelected,
42+
customerID: vaultCustomerID.isEmpty ? nil : vaultCustomerID
43+
)
3444
} catch {
3545
print("Error in getting setup token. \(error.localizedDescription)")
3646
}

Demo/Demo/SwiftUIComponents/PayPalWebViews/PayPalWebOrderCompletionResultView.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ struct PayPalWebOrderCompletionResultView: View {
3939
LeadingText("Email", weight: .bold)
4040
LeadingText("\(email)")
4141
}
42+
if let vaultID = orderResponse.paymentSource?.paypal?.attributes?.vault.id {
43+
LeadingText("Vault ID / Payment Token", weight: .bold)
44+
LeadingText("\(vaultID)")
45+
}
46+
if let customerID = orderResponse.paymentSource?.paypal?.attributes?.vault.customer.id {
47+
LeadingText("Customer ID", weight: .bold)
48+
LeadingText("\(customerID)")
49+
}
4250
Text("")
4351
.id("bottomView")
4452
}

Demo/Demo/ViewModels/CardPaymentViewModel.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@ class CardPaymentViewModel: ObservableObject, CardDelegate {
2121
let amountRequest = Amount(currencyCode: "USD", value: amount)
2222
// TODO: might need to pass in payee as payee object or as auth header
2323

24-
var vaultPaymentSource: VaultCardPaymentSource?
24+
var vaultCardPaymentSource: VaultCardPaymentSource?
2525
if shouldVault {
26-
var customer: CardVaultCustomer?
26+
var customer: VaultCustomer?
2727
if let customerID {
28-
customer = CardVaultCustomer(id: customerID)
28+
customer = VaultCustomer(id: customerID)
2929
}
30-
let attributes = Attributes(vault: Vault(storeInVault: "ON_SUCCESS"), customer: customer)
30+
let attributes = Attributes(vault: Vault(storeInVault: "ON_SUCCESS", usageType: nil, customerType: nil), customer: customer)
3131
let card = VaultCard(attributes: attributes)
32-
vaultPaymentSource = VaultCardPaymentSource(card: card)
32+
vaultCardPaymentSource = VaultCardPaymentSource(card: card)
33+
}
34+
35+
var vaultPaymentSource: VaultPaymentSource?
36+
if let vaultCardPaymentSource {
37+
vaultPaymentSource = .card(vaultCardPaymentSource)
3338
}
3439

3540
let orderRequestParams = CreateOrderParams(

Demo/Demo/ViewModels/PayPalWebViewModel.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,46 @@ class PayPalWebViewModel: ObservableObject, PayPalWebCheckoutDelegate {
1010

1111
let configManager = CoreConfigManager(domain: "PayPalWeb Payments")
1212

13-
func createOrder(amount: String, selectedMerchantIntegration: MerchantIntegration, intent: String) async throws {
13+
func createOrder(
14+
amount: String,
15+
selectedMerchantIntegration: MerchantIntegration,
16+
intent: String,
17+
shouldVault: Bool,
18+
customerID: String? = nil
19+
) async throws {
1420
// might need to pass in payee as payee object or as auth header
1521

1622
let amountRequest = Amount(currencyCode: "USD", value: amount)
1723
// TODO: might need to pass in payee as payee object or as auth header
24+
25+
var vaultPayPalPaymentSource: VaultPayPalPaymentSource?
26+
if shouldVault {
27+
var customer: VaultCustomer?
28+
if let customerID {
29+
customer = VaultCustomer(id: customerID)
30+
}
31+
let attributes = Attributes(
32+
vault: Vault(
33+
storeInVault: "ON_SUCCESS",
34+
usageType: "MERCHANT",
35+
customerType: "CONSUMER"
36+
),
37+
customer: customer
38+
)
39+
let paypal = VaultPayPal(attributes: attributes, experienceContext: ExperienceContext(returnURL: "https://example.com/returnUrl", cancelURL: "https://example.com/cancelUrl"))
40+
vaultPayPalPaymentSource = VaultPayPalPaymentSource(paypal: paypal)
41+
}
42+
43+
var vaultPaymentSource: VaultPaymentSource?
44+
if let vaultPayPalPaymentSource {
45+
vaultPaymentSource = .paypal(vaultPayPalPaymentSource)
46+
}
47+
1848
let orderRequestParams = CreateOrderParams(
1949
applicationContext: nil,
2050
intent: intent,
21-
purchaseUnits: [PurchaseUnit(amount: amountRequest)]
51+
purchaseUnits: [PurchaseUnit(amount: amountRequest)],
52+
paymentSource: vaultPaymentSource
2253
)
2354

2455
do {

0 commit comments

Comments
 (0)