Skip to content

Commit

Permalink
Update the code
Browse files Browse the repository at this point in the history
  • Loading branch information
okhan-okbay-cko committed Aug 5, 2024
1 parent 1aeb8c1 commit 6146607
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions Checkout/Source/Tokenisation/CheckoutAPIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ final public class CheckoutAPIService: CheckoutAPIProtocol {
}

let timeoutInterval: TimeInterval = 10
let taskCompletionQueue = DispatchQueue(label: "taskCompletionQueue")
var isTaskCompleted = false
private let taskCompletionQueue = DispatchQueue(label: "taskCompletionQueue")
private var isTaskCompleted = false

private func createToken(requestParameters: NetworkManager.RequestParameters,
paymentType: TokenRequest.TokenType,
Expand All @@ -168,24 +168,14 @@ final public class CheckoutAPIService: CheckoutAPIProtocol {
return
}

DispatchQueue.global().asyncAfter(deadline: .now() + timeoutInterval) {
self.taskCompletionQueue.sync { [weak self] in
guard let self else { return }
if !isTaskCompleted {
isTaskCompleted = true
completion(.failure(.networkError(.connectionTimeout)))
}
}
}

callRiskSDK(tokenDetails: tokenDetails) { [weak self] result in
self.callRiskSDK(tokenDetails: tokenDetails) { [weak self] in
guard let self else { return }
taskCompletionQueue.sync {
self.taskCompletionQueue.sync {
if !self.isTaskCompleted {
self.isTaskCompleted = true
completion(.success(tokenDetails))
}
}
completion(result)
}

case .errorResponse(let errorResponse):
Expand All @@ -199,18 +189,27 @@ final public class CheckoutAPIService: CheckoutAPIProtocol {
}

private func callRiskSDK(tokenDetails: TokenDetails,
completion: @escaping (Result<TokenDetails, TokenisationError.TokenRequest>) -> Void) {
DispatchQueue.global().async {
completion: @escaping () -> Void) {

DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + timeoutInterval) {
self.taskCompletionQueue.sync { [weak self] in
guard let self else { return }
if !self.isTaskCompleted {
self.isTaskCompleted = true
completion()
}
}

self.riskSDK.configure { [weak self] configurationResult in
guard let self else { return }
switch configurationResult {
case .failure:
completion(.success(tokenDetails))
completion()
logManager.resetCorrelationID()
case .success():
self.riskSDK.publishData(cardToken: tokenDetails.token) { _ in
self.logManager.queue(event: .riskSDKCompletion)
completion(.success(tokenDetails))
completion()
self.logManager.resetCorrelationID()
}
}
Expand Down

0 comments on commit 6146607

Please sign in to comment.