Skip to content

Commit

Permalink
Added Basic User info
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasnim Alam Shovon committed Apr 4, 2019
1 parent 545cabd commit 72b9a48
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 29 deletions.
36 changes: 23 additions & 13 deletions Example/iPaySDK/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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)
}
Expand All @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions iPaySDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
21 changes: 17 additions & 4 deletions iPaySDK/Classes/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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"

Expand Down Expand Up @@ -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
}
}

}
51 changes: 49 additions & 2 deletions iPaySDK/Classes/SDKServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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()
}
}
46 changes: 38 additions & 8 deletions iPaySDK/Classes/iPaySDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public enum DevelopmentEnvironment: String {
public protocol iPaySDKDelegate {
func oauthDidSuccess()
func oauthDidFail()

func paymentDidSuccess()
func paymentDidFail()
}
Expand Down Expand Up @@ -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")
}
}
Expand Down Expand Up @@ -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")
Expand All @@ -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")
}
}

Expand All @@ -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{
Expand Down Expand Up @@ -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()
}
}
}
}
Expand Down

0 comments on commit 72b9a48

Please sign in to comment.