-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSessionHelperModule.swift
69 lines (56 loc) · 2.47 KB
/
SessionHelperModule.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
//
// Copyright (c) 2024 Adyen N.V.
//
// This file is open source and available under the MIT license. See the LICENSE file for more info.
//
import Adyen
import UIKit
protocol SessionResultListener {
func didComplete(with result: Adyen.AdyenSessionResult)
func didFail(with error: Error)
}
@objc(SessionHelper)
internal final class SessionHelperModule: BaseModule, AdyenSessionDelegate {
internal static var sessionListener: SessionResultListener?
func didComplete(with result: Adyen.AdyenSessionResult, component: Adyen.Component, session: Adyen.AdyenSession) {
SessionHelperModule.sessionListener?.didComplete(with: result)
}
func didFail(with error: Error, from component: Adyen.Component, session: Adyen.AdyenSession) {
SessionHelperModule.sessionListener?.didFail(with: error)
}
func didOpenExternalApplication(component: Adyen.ActionComponent, session: Adyen.AdyenSession) {}
override public func supportedEvents() -> [String]! { [Events.didComplete.rawValue, Events.didFail.rawValue] }
@objc
func createSession(_ sessionModelJSON: NSDictionary,
configuration: NSDictionary,
resolver: @escaping RCTPromiseResolveBlock,
rejecter: @escaping RCTPromiseRejectBlock) {
let parser = RootConfigurationParser(configuration: configuration)
let context: AdyenContext
do {
context = try parser.fetchContext(session: BaseModule.session)
} catch {
return rejecter("session", nil, error)
}
guard let id = sessionModelJSON["id"] as? String, let data = sessionModelJSON["sessionData"] as? String else {
return rejecter("session", "Invalid session data", nil)
}
let config = AdyenSession.Configuration(sessionIdentifier: id, initialSessionData: data, context: context)
DispatchQueue.main.async {
AdyenSession.initialize(with: config, delegate: self, presentationDelegate: self) { result in
switch result {
case let .success(session):
let dto = SessionDTO(session: session)
resolver(dto.jsonObject)
BaseModule.session = session
case let .failure(error):
rejecter("session", nil, error)
}
}
}
}
@objc
func hide(_ success: NSNumber, event: NSDictionary) {
dismiss(success.boolValue)
}
}