diff --git a/Example/iPaySDK/ViewController.swift b/Example/iPaySDK/ViewController.swift index f08e8bc..b5d8600 100644 --- a/Example/iPaySDK/ViewController.swift +++ b/Example/iPaySDK/ViewController.swift @@ -11,6 +11,14 @@ import iPaySDK class ViewController: UIViewController, iPaySDKDelegate { func paymentDidSuccess() { + let alert = UIAlertController.init(title: "Successful", message: "Thank you for the payment", preferredStyle: .alert) + let action = UIAlertAction.init(title: "OK", style: .default) { (action) in + alert.dismiss(animated: true, completion: nil) + } + alert.addAction(action) + + alert.show() + iPaySDK.shared.getBalance { (balance) in DispatchQueue.main.async { self.balanceLabel.text = "iPay Balance: \(balance)" @@ -19,7 +27,7 @@ class ViewController: UIViewController, iPaySDKDelegate { } func paymentDidFail() { - let alert = UIAlertController.init(title: "SDK", message: "Payment Failed", preferredStyle: .alert) + let alert = UIAlertController.init(title: "Failed", message: "Payment Failed", preferredStyle: .alert) let action = UIAlertAction.init(title: "OK", style: .default) { (action) in alert.dismiss(animated: true, completion: nil) } @@ -29,27 +37,29 @@ class ViewController: UIViewController, iPaySDKDelegate { } func oauthDidSuccess() { - DispatchQueue.main.async { - self.tokenLabel.text = "oAuth was successful" - self.connectButton.setTitle("Linked with iPay", for: .normal) - self.connectButton.backgroundColor = UIColor.green - self.connectButton.setTitleColor(UIColor.black, for: .normal) - } + self.tokenLabel.text = "oAuth was successful" + self.connectButton.setTitle("Linked with iPay", for: .normal) + self.connectButton.backgroundColor = UIColor.green + self.connectButton.setTitleColor(UIColor.black, for: .normal) iPaySDK.shared.getBalance { (balance) in DispatchQueue.main.async { self.balanceLabel.text = "iPay Balance: \(balance)" } } + + iPaySDK.shared.getUserInfo { (model) in + DispatchQueue.main.async { + self.tokenLabel.text = "\(model.name ?? "") \n \(model.primaryEmailAddress ?? "")" + } + } } func oauthDidFail() { - DispatchQueue.main.async { - self.connectButton.setTitle("Link with iPay", for: .normal) - self.connectButton.backgroundColor = UIColor.red - self.connectButton.setTitleColor(UIColor.black, for: .normal) - self.tokenLabel.text = "oAuth Failed" - } + self.connectButton.setTitle("Link with iPay", for: .normal) + self.connectButton.backgroundColor = UIColor.red + self.connectButton.setTitleColor(UIColor.black, for: .normal) + self.tokenLabel.text = "oAuth Failed" } @IBOutlet weak var connectButton: UIButton! diff --git a/iPaySDK.podspec b/iPaySDK.podspec index 04fa9b8..b9908f9 100644 --- a/iPaySDK.podspec +++ b/iPaySDK.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "iPaySDK" - s.version = "0.8.0" + s.version = "0.9.0" s.summary = "A framework for connecting with iPay and smooth payment experience." # This description is used to generate tags and improve search results. @@ -74,7 +74,7 @@ Pod::Spec.new do |s| # Supports git, hg, bzr, svn and HTTP. # - s.source = { :git => "https://github.com/ipay-systems/iPaySDK.git", :tag => "0.8.0" } + s.source = { :git => "https://github.com/ipay-systems/iPaySDK.git", :tag => "0.9.0" } # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # diff --git a/iPaySDK/Classes/Endpoints.swift b/iPaySDK/Classes/Endpoints.swift index 7d88a59..4a0e987 100644 --- a/iPaySDK/Classes/Endpoints.swift +++ b/iPaySDK/Classes/Endpoints.swift @@ -9,14 +9,14 @@ import Foundation struct Endpoints { enum AuthEnvironment: String { - case Production = "xyz" - case Sandbox = "abc" + case Production = "https://app.ipay.com.bd" + case Sandbox = "https://demo.ipay.com.bd" case Development = "http://10.10.10.10:8089" } enum PaymentEnvironment: String { - case Production = "xyz" - case Sandbox = "abc" + case Production = "https://app.ipay.com.bd" + case Sandbox = "https://demo.ipay.com.bd" case Development = "http://10.10.10.11:8088" } @@ -25,6 +25,7 @@ struct Endpoints { static var verifyTokenEndpoint: String = "/oauth2/v1/auth/session/verify" static var refreshTokenEndpoint: String = "/oauth2/v1/auth/token" + static var userInfoEndpoint: String = "/oauth2/v1/auth/user/user-info" static var balanceEndpoint: String = "/api/psdk/balance" static var paymentEndpoint: String = "/api/psdk/payment" @@ -93,4 +94,16 @@ struct Endpoints { return PaymentEnvironment.Sandbox.rawValue + paymentEndpoint } } + + public static func getUserInfoURL(development: DevelopmentEnvironment) -> String { + switch development { + case .Production: + return AuthEnvironment.Production.rawValue + userInfoEndpoint + case .Development: + return AuthEnvironment.Development.rawValue + userInfoEndpoint + case .Sandbox: + return AuthEnvironment.Sandbox.rawValue + userInfoEndpoint + } + } + } diff --git a/iPaySDK/Classes/SDKServices.swift b/iPaySDK/Classes/SDKServices.swift index a60e223..4aa956e 100644 --- a/iPaySDK/Classes/SDKServices.swift +++ b/iPaySDK/Classes/SDKServices.swift @@ -17,6 +17,13 @@ struct PaymentModel: Codable { var amount: Double? } +public struct UserInfoModel: Codable { + public var message: String? + public var name: String? + public var primaryEmailAddress: String? + public var profilePictureUrl: String? +} + struct SDKServices { public static func getBalance(completion: @escaping (BalanceResponseModel?) -> Void){ guard let url = URL.init(string: Endpoints.getBalanceURL(development: iPaySDK.shared.environment)) else { @@ -90,7 +97,7 @@ struct SDKServices { SVProgressHUD.dismiss() } - guard let data = data, error == nil else { + guard let _ = data, error == nil else { print(error?.localizedDescription ?? "No data") return } @@ -102,7 +109,47 @@ struct SDKServices { completion(false) } } - }.resume() + }.resume() } + + public static func getUserInfo(completion: @escaping (UserInfoModel?) -> Void){ + guard let url = URL.init(string: Endpoints.getUserInfoURL(development: iPaySDK.shared.environment)) else { + return + } + + SVProgressHUD.show() + + var request: URLRequest = URLRequest.init(url: url) + request.httpMethod = "GET" + + let token = Settings.getTokenFromDefaults() + + var headers = request.allHTTPHeaderFields ?? [:] + headers["Content-Type"] = "application/json" + headers["access-token"] = token?.accessToken + request.allHTTPHeaderFields = headers + + URLSession.shared.dataTask(with: request) { (data, response, error) in + + DispatchQueue.main.async { + SVProgressHUD.dismiss() + } + + guard let data = data, error == nil else { + print(error?.localizedDescription ?? "No data") + return + } + if let httpResponse = response as? HTTPURLResponse { + print("statusCode: \(httpResponse.statusCode)") + if httpResponse.statusCode == 200 { + if let userInfoModel = try? JSONDecoder().decode(UserInfoModel.self, from: data) { + completion(userInfoModel) + } + }else{ + completion(nil) + } + } + }.resume() + } } diff --git a/iPaySDK/Classes/iPaySDK.swift b/iPaySDK/Classes/iPaySDK.swift index a5dc85e..ea3332f 100644 --- a/iPaySDK/Classes/iPaySDK.swift +++ b/iPaySDK/Classes/iPaySDK.swift @@ -17,7 +17,6 @@ public enum DevelopmentEnvironment: String { public protocol iPaySDKDelegate { func oauthDidSuccess() func oauthDidFail() - func paymentDidSuccess() func paymentDidFail() } @@ -53,10 +52,14 @@ public class iPaySDK { Services.verifyToken(model: model) { (status) in self.isAuthenticated = status if self.isAuthenticated { - self.delegate?.oauthDidSuccess() + DispatchQueue.main.async { + self.delegate?.oauthDidSuccess() + } print("Authenticated") }else{ - self.delegate?.oauthDidFail() + DispatchQueue.main.async { + self.delegate?.oauthDidFail() + } print("Not Authenticated") } } @@ -102,7 +105,9 @@ public class iPaySDK { }else if url.absoluteString.contains("cancel"){ //cancel case print("iPaySDK:== failed") - delegate?.oauthDidFail() + DispatchQueue.main.async { + self.delegate?.oauthDidFail() + } }else{ //unknown case print("iPaySDK:== unknown error") @@ -127,6 +132,23 @@ public class iPaySDK { SDKServices.getBalance { (model) in completion(model?.balance ?? Double.nan) } + }else{ + print("Not authenticated") + } + } + + /// Fetch Users basic info + /// + /// If there is a valid token returns users name, email, profile picture url + public func getUserInfo(completion: @escaping (UserInfoModel) -> Void) { + if self.isAuthenticated{ + SDKServices.getUserInfo { (model) in + if let userModel = model { + completion(userModel) + } + } + }else { + print("Not authenticated") } } @@ -139,9 +161,13 @@ public class iPaySDK { let model: PaymentModel = PaymentModel.init(amount: amount) SDKServices.makePayment(model: model) { (status) in if status { - self.delegate?.paymentDidSuccess() + DispatchQueue.main.async { + self.delegate?.paymentDidSuccess() + } }else{ - self.delegate?.paymentDidFail() + DispatchQueue.main.async { + self.delegate?.paymentDidFail() + } } } }else{ @@ -177,13 +203,17 @@ public class iPaySDK { self.makePayment(amount: self.infoDictionary?[DictionaryKeys.amountKey.rawValue] as! Double) self.infoDictionary = nil case .None: - self.delegate?.oauthDidSuccess() + DispatchQueue.main.async { + self.delegate?.oauthDidSuccess() + } } }else{ self.isAuthenticated = false - self.delegate?.oauthDidFail() + DispatchQueue.main.async { + self.delegate?.oauthDidFail() + } } } }