Skip to content

Commit

Permalink
Merge pull request #35 from xegole/handle_response_data_from_successf…
Browse files Browse the repository at this point in the history
…ul_transaction

Fixed handle response data from successful transaction
  • Loading branch information
securesubmit-buildmaster authored Jul 20, 2021
2 parents 0b375cb + 97a3f8f commit 9dff942
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Example/RXPiOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class ViewController: UIViewController, HPPManagerDelegate, GenericHPPMana
}

// Is called in case of regular HPPManager()
func HPPManagerCompletedWithResult(_ result: [String: String]) {
func HPPManagerCompletedWithResult(_ result: [String: Any]) {
display(result: NSString(format: "%@", result) as String)
}

Expand Down
58 changes: 46 additions & 12 deletions Pod/Classes/RealexComponent/HPPManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit

/// The delegate callbacks which allow the host app to receive all possible results form the component.
@objc public protocol HPPManagerDelegate: class {
@objc func HPPManagerCompletedWithResult(_ result: [String: String])
@objc func HPPManagerCompletedWithResult(_ result: [String: Any])
@objc func HPPManagerFailedWithError(_ error: Error?)
@objc func HPPManagerCancelled()
}
Expand Down Expand Up @@ -44,7 +44,7 @@ fileprivate class AnyGenericHPPManagerDelegate<T: Decodable>: GenericHPPManagerD

/// The main object the host app creates.
/// A convenience payment manager for payment service responses that have a `[String: String]` structure
public class HPPManager: GenericHPPManager<[String: String]> { }
public class HPPManager: GenericHPPManager<[String: String?]> { }

/// The main object the host app creates.
/// A payment manager that can decode payment service responses that have a generic structure
Expand Down Expand Up @@ -353,17 +353,25 @@ public class GenericHPPManager<T: Decodable>: NSObject, HPPViewControllerDelegat
UIApplication.shared.isNetworkActivityIndicatorVisible = false
do {
if let receivedData = data {
let decodedResponse = try JSONDecoder().decode(T.self, from: receivedData)
if let genericDelegate = self.genericDelegate {
genericDelegate.HPPManagerCompletedWithResult(decodedResponse)
return
if var jsonData = try JSONSerialization.jsonObject(with: receivedData, options: []) as? [String: Any]{
switch jsonData["response"] {
case is String, nil:
self.decodeDataOnCompleted(receivedData)
break
case let response as [String:String]:
jsonData["responseCode"] = response["code"]
jsonData["responseMessage"] = response["message"]
jsonData["response"] = nil
self.decodeDataOnCompleted(jsonData: jsonData as? [String:String])
break
default:
let error = HPPManagerError.typeMismatch()
self.HPPViewControllerFailedWithError(error)
break
}
}else{
self.decodeDataOnCompleted(receivedData)
}
guard let dictResponse = decodedResponse as? [String: String] else {
let error = HPPManagerError.typeMismatch()
self.HPPViewControllerFailedWithError(error)
return
}
self.delegate?.HPPManagerCompletedWithResult(dictResponse)
} else {
self.HPPViewControllerFailedWithError(error)
}
Expand All @@ -374,6 +382,32 @@ public class GenericHPPManager<T: Decodable>: NSObject, HPPViewControllerDelegat
})
dataTask.resume()
}

private func decodeDataOnCompleted(_ receivedData: Data? = nil, jsonData: [String: String]? = nil ){
do {
if let data = receivedData {
let decodedResponse = try JSONDecoder().decode(T.self, from: data)
if let genericDelegate = self.genericDelegate {
genericDelegate.HPPManagerCompletedWithResult(decodedResponse)
return
}
guard let dictResponse = decodedResponse as? [String: Any] else {
let error = HPPManagerError.typeMismatch()
self.HPPViewControllerFailedWithError(error)
return
}
self.delegate?.HPPManagerCompletedWithResult(dictResponse)
}

if let data = jsonData{
self.delegate?.HPPManagerCompletedWithResult(data)
print(data)
}

} catch {
self.HPPViewControllerFailedWithError(error)
}
}

// MARK: - HPPViewControllerDelegate

Expand Down
4 changes: 2 additions & 2 deletions RXPiOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "RXPiOS"
s.version = "1.7.1"
s.version = "1.7.2"
s.summary = "The official Realex Payments iOS SDK for HPP and Remote API."
s.homepage = "https://developer.realexpayments.com"
s.license = 'MIT'
Expand All @@ -24,4 +24,4 @@ Pod::Spec.new do |s|

s.source_files = 'Pod/Classes/**/*'

end
end

0 comments on commit 9dff942

Please sign in to comment.