forked from omise/omise-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductDetailViewController.swift
255 lines (227 loc) · 11.3 KB
/
ProductDetailViewController.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import UIKit
import OmiseSDK
class ProductDetailViewController: BaseViewController {
let omiseSDK = OmiseSDK(
publicKey: LocalConfig.default.publicKey,
configuration: LocalConfig.default.configuration
)
private func loadCapability() {
omiseSDK.client.capability { (result) in
if case .success(let capability) = result {
print("Capability Country: \(capability.countryCode)")
}
}
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "PresentCreditFormWithModal" ||
identifier == "ShowCreditForm" ||
identifier == "PresentPaymentCreator" ||
identifier == "ShowCreditFormWithCustomFields" {
return currentCodePathMode == .storyboard
}
return true
}
override func viewDidLoad() {
super.viewDidLoad()
OmiseSDK.shared = omiseSDK
}
// override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// super.prepare(for: segue, sender: sender)
//
// if segue.identifier == "PresentCreditFormWithModal",
// let creditCardFormNavigationController = segue.destination as? UINavigationController,
// let creditCardFormController = creditCardFormNavigationController.topViewController as? CreditCardFormViewController {
// creditCardFormController.publicKey = publicKey
// creditCardFormController.handleErrors = true
// creditCardFormController.delegate = self
// } else if segue.identifier == "ShowCreditForm",
// let creditCardFormController = segue.destination as? CreditCardFormViewController {
// creditCardFormController.publicKey = publicKey
// creditCardFormController.handleErrors = true
// creditCardFormController.delegate = self
// } else if segue.identifier == "PresentPaymentCreator",
// let paymentCreatorController = segue.destination as? PaymentCreatorController {
// paymentCreatorController.publicKey = self.publicKey
// paymentCreatorController.paymentAmount = paymentAmount
// paymentCreatorController.paymentCurrency = Currency(code: paymentCurrencyCode)
// if usesCapabilityDataForPaymentMethods, let capability = self.capability {
// paymentCreatorController.applyPaymentMethods(from: capability)
// } else {
// paymentCreatorController.allowedPaymentMethods = allowedPaymentMethods
// }
// paymentCreatorController.paymentDelegate = self
// } else if segue.identifier == "ShowCreditFormWithCustomFields",
// let vc = segue.destination as? CustomCreditCardFormViewController {
// vc.delegate = self
// }
// }
@IBAction private func showModalCreditCardForm(_ sender: Any) {
guard currentCodePathMode == .code else {
return
}
// let creditCardFormController = CreditCardFormViewController.makeCreditCardFormViewController(withPublicKey: publicKey)
// creditCardFormController.handleErrors = true
// creditCardFormController.delegate = self
// let navigationController = UINavigationController(rootViewController: creditCardFormController)
// present(navigationController, animated: true, completion: nil)
}
@IBAction private func showCreditCardForm(_ sender: UIButton) {
guard currentCodePathMode == .code else {
return
}
// let creditCardFormController = CreditCardFormViewController.makeCreditCardFormViewController(withPublicKey: publicKey)
// creditCardFormController.handleErrors = true
// creditCardFormController.delegate = self
// show(creditCardFormController, sender: self)
}
@IBAction private func showModalPaymentCreator(_ sender: Any) {
guard currentCodePathMode == .code else {
return
}
if usesCapabilityDataForPaymentMethods {
let viewController = omiseSDK.choosePaymentMethodFromCapabilityController(
amount: paymentAmount,
currency: paymentCurrencyCode,
delegate: self
)
present(viewController, animated: true, completion: nil)
} else {
let viewController = omiseSDK.choosePaymentMethodController(
amount: paymentAmount,
currency: paymentCurrencyCode,
allowedPaymentMethods: allowedPaymentMethods,
allowedCardPayment: true,
delegate: self
)
present(viewController, animated: true, completion: nil)
}
}
@IBAction private func showCustomCreditCardForm(_ sender: Any) {
guard currentCodePathMode == .code else {
return
}
let customCreditCardFormController = CustomCreditCardFormViewController(nibName: nil, bundle: nil)
customCreditCardFormController.delegate = self
show(customCreditCardFormController, sender: sender)
}
@IBAction private func handlingAuthorizingPayment(_ sender: UIBarButtonItem) {
let alertController = UIAlertController(title: "Authorizing Payment",
message: "Please input your given authorized URL",
preferredStyle: .alert)
alertController.addTextField(configurationHandler: nil)
alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
alertController.addAction(UIAlertAction(title: "Go", style: UIAlertAction.Style.default) { (_) in
guard let textField = alertController.textFields?.first,
let text = textField.text,
let url = URL(string: text),
let expectedReturnURL = URLComponents(string: "https://opn.ooo/") else { return }
let handlerController =
AuthorizingPaymentViewController
.makeAuthorizingPaymentViewControllerNavigationWithAuthorizedURL(
url, expectedReturnURLPatterns: [expectedReturnURL], delegate: self)
self.present(handlerController, animated: true, completion: nil)
})
present(alertController, animated: true, completion: nil)
}
}
// MARK: - Credit Card Form View Controller Delegate
extension ProductDetailViewController: CreditCardFormViewControllerDelegate {
func creditCardFormViewControllerDidCancel(_ controller: CreditCardFormViewController) {
dismissForm()
}
func creditCardFormViewController(_ controller: CreditCardFormViewController, didSucceedWithToken token: Token) {
dismissForm {
let alertController = UIAlertController(
title: "Token Created",
message: "A token with id of \(token.id) was successfully created. Please send this id to server to create a charge.",
preferredStyle: .alert
)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
func creditCardFormViewController(_ controller: CreditCardFormViewController, didFailWithError error: Error) {
dismissForm {
let alertController = UIAlertController(
title: "Error",
message: error.localizedDescription,
preferredStyle: .alert
)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
// MARK: - Authorizing Payment View Controller Delegate
extension ProductDetailViewController: AuthorizingPaymentViewControllerDelegate {
func authorizingPaymentViewController(_ viewController: AuthorizingPaymentViewController, didCompleteAuthorizingPaymentWithRedirectedURL redirectedURL: URL) {
print(redirectedURL)
dismiss(animated: true, completion: nil)
}
func authorizingPaymentViewControllerDidCancel(_ viewController: AuthorizingPaymentViewController) {
dismiss(animated: true, completion: nil)
}
}
// MARK: - Custom Credit Card Form View Controller Delegate
extension ProductDetailViewController: CustomCreditCardFormViewControllerDelegate {
func creditCardFormViewController(_ controller: CustomCreditCardFormViewController, didSucceedWithToken token: Token) {
dismissForm {
let alertController = UIAlertController(
title: "Token Created",
message: "A token with id of \(token.id) was successfully created. Please send this id to server to create a charge.",
preferredStyle: .alert
)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
func creditCardFormViewController(_ controller: CustomCreditCardFormViewController, didFailWithError error: Error) {
dismissForm {
let alertController = UIAlertController(
title: "Error",
message: error.localizedDescription,
preferredStyle: .alert
)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
/// Processing result of choosing Payment Method screen
extension ProductDetailViewController: ChoosePaymentMethodDelegate {
func choosePaymentMethodDidComplete(with source: Source) {
let alertController = UIAlertController(
title: "Source Created",
message: "A source with id of \(source.id) was successfully created. Please send this id to server to create a charge.",
preferredStyle: .alert
)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
func choosePaymentMethodDidComplete(with token: Token) {
let alertController = UIAlertController(
title: "Token Created",
message: "A token with id of \(token.id) was successfully created. Please send this id to server to create a charge.",
preferredStyle: .alert
)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
func choosePaymentMethodDidComplete(with error: Error) {
dismissForm {
let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
func choosePaymentMethodDidCancel() {
dismissForm()
}
}