forked from omise/omise-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductDetailViewController.swift
183 lines (163 loc) · 7.94 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
import UIKit
import OmiseSDK
class ProductDetailViewController: BaseViewController {
let omiseSDK = {
// Initialize OmiseSDK instance with given publicKey and optional configuration used for testing
let omiseSDK = OmiseSDK(
publicKey: LocalConfig.default.publicKey,
configuration: LocalConfig.default.configuration
)
// Setup shared instance to use from other screens if required
OmiseSDK.shared = omiseSDK
return omiseSDK
}()
private func loadCapability() {
omiseSDK.client.capability { (result) in
if case .success(let capability) = result {
print("Capability Country: \(capability.countryCode)")
}
}
}
@IBAction private func showModalCreditCardPayment(_ sender: Any) {
omiseSDK.presentCreditCardPayment(from: self, delegate: self)
}
@IBAction private func showChoosePaymentMethods(_ sender: Any) {
omiseSDK.presentChoosePaymentMethod(
from: self,
amount: paymentAmount,
currency: paymentCurrencyCode,
allowedPaymentMethods: usesCapabilityDataForPaymentMethods ? nil : allowedPaymentMethods,
isCardPaymentAllowed: true,
handleErrors: true,
delegate: self
)
}
@IBAction private func showCustomCreditCardPayment(_ sender: Any) {
let customCreditCardPaymentController = CustomCreditCardPaymentController(nibName: nil, bundle: nil)
customCreditCardPaymentController.delegate = self
show(customCreditCardPaymentController, sender: sender)
}
@IBAction private func handlingAuthorizingPayment(_ sender: UIBarButtonItem) {
let alertController = UIAlertController(title: "Authorizing Payment",
message: "Please input your given authorize 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) { [weak self ](_) in
guard let self = self,
let textField = alertController.textFields?.first,
let text = textField.text,
let url = URL(string: text) else { return }
// Optional 3DS challenge screen customization
let toolbarUI = ThreeDSToolbarCustomization(
backgroundColorHex: "#FFFFFF",
headerText: "Secure Checkout",
buttonText: "Close",
textFontName: "Arial-BoldMT",
textColorHex: "#000000",
textFontSize: 20
)
let threeDSUICustomization = ThreeDSUICustomization(toolbarCustomization: toolbarUI)
self.omiseSDK.presentAuthorizingPayment(
from: self,
authorizeURL: url,
expectedReturnURLStrings: ["https://omise.co"],
threeDSRequestorAppURLString: AppDeeplink.threeDSChallenge.urlString,
threeDSUICustomization: threeDSUICustomization,
delegate: self
)
})
present(alertController, animated: true, completion: nil)
}
}
// MARK: - Authorizing Payment View Controller Delegate
extension ProductDetailViewController: AuthorizingPaymentDelegate {
func authorizingPaymentDidComplete(with redirectedURL: URL?) {
print("Payment is authorized with redirect url `\(String(describing: redirectedURL))`")
omiseSDK.dismiss()
}
func authorizingPaymentDidCancel() {
print("Payment is not authorized")
omiseSDK.dismiss()
}
}
/// Processing result of choosing Payment Method screen
extension ProductDetailViewController: ChoosePaymentMethodDelegate {
func choosePaymentMethodDidComplete(with source: Source) {
print("Source is created with id '\(source.id)'")
omiseSDK.dismiss {
let alertController = UIAlertController(
title: "Source Created\n(\(source.paymentInformation.sourceType.rawValue))",
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)
self.present(alertController, animated: true, completion: nil)
}
}
func choosePaymentMethodDidComplete(with token: Token) {
print("Token is created with id '\(token.id)'")
omiseSDK.dismiss {
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 choosePaymentMethodDidComplete(with error: Error) {
let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
let vc = omiseSDK.presentedViewController ?? self
vc.present(alertController, animated: true, completion: nil)
}
func choosePaymentMethodDidCancel() {
omiseSDK.dismiss()
}
func choosePaymentMethodDidComplete(with source: Source, token: Token) {
print("White-label installment payment Token is created with id '\(token.id)', Source id: '\(source.id)'")
omiseSDK.dismiss {
let alertController = UIAlertController(
title: "Token & Source Created\n(\(source.paymentInformation.sourceType.rawValue))",
message: "A token with id of \(token.id) and 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)
self.present(alertController, animated: true, completion: nil)
}
}
}
// MARK: - Custom Credit Card Form View Controller Delegate
extension ProductDetailViewController: CustomCreditCardPaymentControllerDelegate {
func creditCardFormViewController(_ controller: CustomCreditCardPaymentController, didSucceedWithToken token: Token) {
print("Token is created by using custom form with id '\(token.id)'")
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: CustomCreditCardPaymentController, 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)
}
}
}