forked from omise/omise-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductDetailViewController.swift
154 lines (136 loc) · 6.54 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
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 ? [] : allowedPaymentMethods,
forcePaymentMethods: true,
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),
let expectedReturnURL = URLComponents(string: "https://opn.ooo/") else { return }
self.omiseSDK.presentAuthorizedController(
from: self,
authorizeURL: url,
expectedReturnURLPatterns: [expectedReturnURL],
delegate: 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)
omiseSDK.dismiss()
}
func authorizingPaymentViewControllerDidCancel(_ viewController: AuthorizingPaymentViewController) {
omiseSDK.dismiss()
}
}
// MARK: - Custom Credit Card Form View Controller Delegate
extension ProductDetailViewController: CustomCreditCardPaymentControllerDelegate {
func creditCardFormViewController(_ controller: CustomCreditCardPaymentController, didSucceedWithToken token: Token) {
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 creditCardFormViewController(_ controller: CustomCreditCardPaymentController, didFailWithError error: Error) {
omiseSDK.dismiss {
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) {
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) {
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()
}
}