-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathApplePayComponentExample.swift
More file actions
159 lines (127 loc) · 5.27 KB
/
ApplePayComponentExample.swift
File metadata and controls
159 lines (127 loc) · 5.27 KB
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
//
// Copyright (c) 2023 Adyen N.V.
//
// This file is open source and available under the MIT license. See the LICENSE file for more info.
//
import Adyen
import AdyenComponents
import AdyenSession
import Contacts
import PassKit
internal final class ApplePayComponentExample: InitialDataFlowProtocol {
// MARK: - Properties
internal var session: Session?
internal weak var presenter: PresenterExampleProtocol?
internal var applePayComponent: ApplePayComponent?
internal lazy var apiClient = ApiClientHelper.generateApiClient()
internal var context: AdyenContext?
// MARK: - Initializers
internal init() {}
internal func start() {
presenter?.showLoadingIndicator()
Task {
do {
try await initializeExampleAppAdyenContext()
loadSession { [weak self] response in
guard let self else { return }
self.presenter?.hideLoadingIndicator()
switch response {
case let .success(session):
self.session = session
self.presentComponent(with: session)
case let .failure(error):
self.presentAlert(with: error)
}
}
} catch {
self.presenter?.hideLoadingIndicator()
self.presentAlert(with: error)
}
}
}
// MARK: - Networking
internal func loadSession(completion: @escaping (Result<Session, Error>) -> Void) {
requestSessionInitialInfo { [weak self] response in
guard let self else { return }
switch response {
case let .success(model):
// Session.initialize(
// with: configuration,
// delegate: self,
// presentationDelegate: self,
// completion: completion
// )
break
case let .failure(error):
completion(.failure(error))
}
}
}
// MARK: Presentation
internal func presentComponent(with session: Session) {
do {
let component = try applePayComponent(from: session)
let componentViewController = component.viewController
presenter?.present(viewController: componentViewController, completion: nil)
applePayComponent = component
} catch {
self.presentAlert(with: error)
}
}
internal func applePayComponent(from session: Session) throws -> ApplePayComponent {
let paymentMethods = session.state.paymentMethods
guard let paymentMethod = paymentMethods.paymentMethod(ofType: ApplePayPaymentMethod.self) else {
throw IntegrationError.paymentMethodNotAvailable(paymentMethod: ApplePayPaymentMethod.self)
}
guard let context else {
fatalError("AdyenContext is not initialized")
}
var config = try ConfigurationConstants.current.applePayConfiguration(using: .demoWithShippingFields)
config.onAuthorize = { payment in
if ConfigurationConstants.current.applePaySettings.didAuthorizeSuccessful {
return PKPaymentAuthorizationResult(status: .success, errors: nil)
} else {
let postalCodeError = PKPaymentRequest.paymentShippingAddressInvalidError(
withKey: CNPostalAddressPostalCodeKey,
localizedDescription: "Wrong postal code"
)
return PKPaymentAuthorizationResult(status: .failure, errors: [postalCodeError])
}
}
let component = try ApplePayComponent(
paymentMethod: paymentMethod,
context: context,
configuration: config
)
component.delegate = session
return component
}
private func present(_ component: PresentableComponent) {
presenter?.present(viewController: component.viewController, completion: nil)
}
// MARK: - Alert handling
internal func presentAlert(with error: Error, retryHandler: (() -> Void)? = nil) {
presenter?.presentAlert(with: error, retryHandler: retryHandler)
}
internal func dismissAndShowAlert(_ success: Bool, _ message: String) {
// dismiss ourselves as the auto dismiss flag is false
presenter?.dismiss {
// Payment is processed. Add your code here.
let title = success ? "Success" : "Error"
self.presenter?.presentAlert(withTitle: title, message: message)
}
}
}
extension ApplePayComponentExample: SessionDelegate {
func didComplete(with result: CheckoutResult, component: Component, session: Session) {
dismissAndShowAlert(result.resultCode.isSuccess, result.resultCode.rawValue)
}
func didFail(with error: Error, from component: Component, session: Session) {
dismissAndShowAlert(false, error.localizedDescription)
}
func didOpenExternalApplication(component: ActionComponent, session: Session) {}
}
extension ApplePayComponentExample: PresentationDelegate {
/// The implementation of this delegate method is not needed when using Session
internal func present(component: PresentableComponent) {}
}