forked from stripe/stripe-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowseViewController.swift
207 lines (180 loc) · 9.81 KB
/
BrowseViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// BrowseViewController.swift
// UI Examples
//
// Created by Ben Guo on 7/18/17.
// Copyright © 2017 Stripe. All rights reserved.
//
import UIKit
import Stripe
class BrowseViewController: UITableViewController, STPAddCardViewControllerDelegate, STPPaymentOptionsViewControllerDelegate, STPShippingAddressViewControllerDelegate {
enum Demo: Int {
static let count = 6
case STPPaymentCardTextField
case STPAddCardViewController
case STPPaymentOptionsViewController
case STPPaymentOptionsFPXViewController
case STPShippingInfoViewController
case ChangeTheme
var title: String {
switch self {
case .STPPaymentCardTextField: return "Card Field"
case .STPAddCardViewController: return "Card Form with Billing Address"
case .STPPaymentOptionsViewController: return "Payment Option Picker"
case .STPPaymentOptionsFPXViewController: return "Payment Option Picker (With FPX)"
case .STPShippingInfoViewController: return "Shipping Info Form"
case .ChangeTheme: return "Change Theme"
}
}
var detail: String {
switch self {
case .STPPaymentCardTextField: return "STPPaymentCardTextField"
case .STPAddCardViewController: return "STPAddCardViewController"
case .STPPaymentOptionsViewController: return "STPPaymentOptionsViewController"
case .STPPaymentOptionsFPXViewController: return "STPPaymentOptionsViewController"
case .STPShippingInfoViewController: return "STPShippingInfoViewController"
case .ChangeTheme: return ""
}
}
}
let customerContext = MockCustomerContext()
let themeViewController = ThemeViewController()
override func viewDidLoad() {
super.viewDidLoad()
title = "Stripe UI Examples"
tableView.tableFooterView = UIView()
tableView.rowHeight = 60
navigationController?.navigationBar.isTranslucent = false
STPAddCardViewController.startMockingAPIClient()
}
// MARK: UITableViewDelegate
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Demo.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
if let example = Demo(rawValue: indexPath.row) {
cell.textLabel?.text = example.title
cell.detailTextLabel?.text = example.detail
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard let example = Demo(rawValue: indexPath.row) else { return }
let theme = themeViewController.theme.stpTheme
switch example {
case .STPPaymentCardTextField:
let viewController = CardFieldViewController()
viewController.theme = theme
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.stp_theme = theme
present(navigationController, animated: true, completion: nil)
case .STPAddCardViewController:
let config = STPPaymentConfiguration()
config.requiredBillingAddressFields = .full
let viewController = STPAddCardViewController(configuration: config, theme: theme)
viewController.delegate = self
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.stp_theme = theme
present(navigationController, animated: true, completion: nil)
case .STPPaymentOptionsFPXViewController:
let config = STPPaymentConfiguration()
config.additionalPaymentOptions = [.default, .FPX]
config.requiredBillingAddressFields = .none
config.appleMerchantIdentifier = "dummy-merchant-id"
let viewController = STPPaymentOptionsViewController(configuration: config,
theme: theme,
customerContext: self.customerContext,
delegate: self)
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.stp_theme = theme
present(navigationController, animated: true, completion: nil)
case .STPPaymentOptionsViewController:
let config = STPPaymentConfiguration()
config.additionalPaymentOptions = .default
config.requiredBillingAddressFields = .none
config.appleMerchantIdentifier = "dummy-merchant-id"
let viewController = STPPaymentOptionsViewController(configuration: config,
theme: theme,
customerContext: self.customerContext,
delegate: self)
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.stp_theme = theme
present(navigationController, animated: true, completion: nil)
case .STPShippingInfoViewController:
let config = STPPaymentConfiguration()
config.requiredShippingAddressFields = [.postalAddress]
let viewController = STPShippingAddressViewController(configuration: config,
theme: theme,
currency: "usd",
shippingAddress: nil,
selectedShippingMethod: nil,
prefilledInformation: nil)
viewController.delegate = self
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.stp_theme = theme
present(navigationController, animated: true, completion: nil)
case .ChangeTheme:
let navigationController = UINavigationController(rootViewController: self.themeViewController)
present(navigationController, animated: true, completion: nil)
}
}
// MARK: STPAddCardViewControllerDelegate
func addCardViewControllerDidCancel(_ addCardViewController: STPAddCardViewController) {
dismiss(animated: true, completion: nil)
}
func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreatePaymentMethod paymentMethod: STPPaymentMethod, completion: @escaping STPErrorBlock) {
dismiss(animated: true, completion: nil)
}
// MARK: STPPaymentOptionsViewControllerDelegate
func paymentOptionsViewControllerDidCancel(_ paymentOptionsViewController: STPPaymentOptionsViewController) {
dismiss(animated: true, completion: nil)
}
func paymentOptionsViewControllerDidFinish(_ paymentOptionsViewController: STPPaymentOptionsViewController) {
dismiss(animated: true, completion: nil)
}
func paymentOptionsViewController(_ paymentOptionsViewController: STPPaymentOptionsViewController, didFailToLoadWithError error: Error) {
dismiss(animated: true, completion: nil)
}
// MARK: STPShippingAddressViewControllerDelegate
func shippingAddressViewControllerDidCancel(_ addressViewController: STPShippingAddressViewController) {
dismiss(animated: true, completion: nil)
}
func shippingAddressViewController(_ addressViewController: STPShippingAddressViewController, didFinishWith address: STPAddress, shippingMethod method: PKShippingMethod?) {
self.customerContext.updateCustomer(withShippingAddress: address, completion: nil)
dismiss(animated: true, completion: nil)
}
func shippingAddressViewController(_ addressViewController: STPShippingAddressViewController, didEnter address: STPAddress, completion: @escaping STPShippingMethodsCompletionBlock) {
let upsGround = PKShippingMethod()
upsGround.amount = 0
upsGround.label = "UPS Ground"
upsGround.detail = "Arrives in 3-5 days"
upsGround.identifier = "ups_ground"
let upsWorldwide = PKShippingMethod()
upsWorldwide.amount = 10.99
upsWorldwide.label = "UPS Worldwide Express"
upsWorldwide.detail = "Arrives in 1-3 days"
upsWorldwide.identifier = "ups_worldwide"
let fedEx = PKShippingMethod()
fedEx.amount = 5.99
fedEx.label = "FedEx"
fedEx.detail = "Arrives tomorrow"
fedEx.identifier = "fedex"
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
if address.country == nil || address.country == "US" {
completion(.valid, nil, [upsGround, fedEx], fedEx)
} else if address.country == "AQ" {
let error = NSError(domain: "ShippingError", code: 123, userInfo: [NSLocalizedDescriptionKey: "Invalid Shipping Address",
NSLocalizedFailureReasonErrorKey: "We can't ship to this country."])
completion(.invalid, error, nil, nil)
} else {
fedEx.amount = 20.99
completion(.valid, nil, [upsWorldwide, fedEx], fedEx)
}
}
}
}