Created
October 25, 2015 08:58
-
-
Save brianmc/b2f9f51c0262055afd0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func callVDAPIGET(payload: String) { | |
println("CAlling REST API") | |
let urlPath = "https://rich-archery-782.appspot.com/touchpay" | |
let url = NSURL(string: urlPath) | |
let session = NSURLSession.sharedSession() | |
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in | |
println("Task completed") | |
if(error != nil) { | |
// If there is an error in the web request, print it to the console | |
println(error.localizedDescription) | |
} | |
var err: NSError? | |
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary | |
if(err != nil) { | |
// If there is an error parsing JSON, print it to the console | |
println("JSON Error \(err!.localizedDescription)") | |
} | |
println(jsonResult) | |
//let results: NSArray = jsonResult["results"] as NSArray | |
}) | |
task.resume() | |
} | |
func callVDAPIPOST(params: Dictionary<String, String>) -> Bool | |
{ | |
var request = NSMutableURLRequest(URL: NSURL(string: "https://hidden-bond-93218.appspot.com/touchpay")!) | |
var session = NSURLSession.sharedSession() | |
request.HTTPMethod = "POST" | |
//var params = ["sender":"blob", "receiver":"PAN"] as Dictionary<String, String> | |
var err: NSError? | |
let data = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err) | |
request.HTTPBody = data | |
var strBody = NSString(data: data!, encoding: NSUTF8StringEncoding) | |
println(strBody) | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in | |
println("Response: \(response)") | |
var strData = NSString(data: data, encoding: NSUTF8StringEncoding) | |
println("Body: \(strData)") | |
self.resultLabel.text = strData | |
var err: NSError? | |
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary | |
// Did the JSONObjectWithData constructor return an error? If so, log the error to the console | |
if(err != nil) { | |
println(err!.localizedDescription) | |
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding) | |
println("Error could not parse JSON: '\(jsonStr)'") | |
self.resultLabel.text = "Unable to send money right now" | |
} | |
else { | |
// The JSONObjectWithData constructor didn't return an error. But, we should still | |
// check and make sure that json has a value using optional binding. | |
if let parseJSON = json { | |
// Okay, the parsedJSON is here, let's get the value for 'success' out of it | |
var success = parseJSON["success"] as? Int | |
println("Succes: \(success)") | |
} | |
else { | |
// Woa, okay the json object was nil, something went worng. Maybe the server isn't running? | |
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding) | |
println("Error could not parse JSON: \(jsonStr)") | |
self.resultLabel.text = "Error sending money" | |
} | |
} | |
}) | |
task.resume() | |
return true | |
} | |
func callANETAPIPOST(params: Dictionary<String, String>) -> Bool | |
{ | |
var request = NSMutableURLRequest(URL: NSURL(string: "https://apitest.authorize.net/xml/v1/request.api")!) | |
var session = NSURLSession.sharedSession() | |
request.HTTPMethod = "POST" | |
var myMerchantParams = ["name":"89nE4Beh", "transactionKey":"7s2g3yWC3TfC92p2"] as Dictionary<String, String> | |
var myCardParams = ["cardNumber":"5424000000000015", "expirationDate":"1220", "cardCode":"999"] as Dictionary<String, String> | |
var myPaymentParams = ["creditCard":myCardParams] as Dictionary<String, AnyObject> | |
var myTransactionParams: [[String:AnyObject]] = [["transactionType":"authCaptureTransaction"], ["amount":"5"], ["payment":myPaymentParams]] | |
var myRequestParams = ["transactionRequest":myTransactionParams, "refId":"578787","merchantAuthentication":myMerchantParams] as Dictionary<String, AnyObject> | |
var myRequest = ["createTransactionRequest":myRequestParams] as Dictionary<String, AnyObject> | |
var err: NSError? | |
let data = NSJSONSerialization.dataWithJSONObject(myTransactionParams, options: nil, error: &err) | |
request.HTTPBody = data | |
var strBody = NSString(data: data!, encoding: NSUTF8StringEncoding) | |
println(strBody) | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in | |
println("Response: \(response)") | |
var strData = NSString(data: data, encoding: NSUTF8StringEncoding) | |
println("Body: \(strData)") | |
self.resultLabel.text = strData | |
var err: NSError? | |
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary | |
// Did the JSONObjectWithData constructor return an error? If so, log the error to the console | |
if(err != nil) { | |
println(err!.localizedDescription) | |
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding) | |
println("Error could not parse JSON: '\(jsonStr)'") | |
self.resultLabel.text = "Unable to send money right now" | |
} | |
else { | |
// The JSONObjectWithData constructor didn't return an error. But, we should still | |
// check and make sure that json has a value using optional binding. | |
if let parseJSON = json { | |
// Okay, the parsedJSON is here, let's get the value for 'success' out of it | |
var success = parseJSON["success"] as? Int | |
println("Succes: \(success)") | |
} | |
else { | |
// Woa, okay the json object was nil, something went worng. Maybe the server isn't running? | |
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding) | |
println("Error could not parse JSON: \(jsonStr)") | |
self.resultLabel.text = "Error sending money" | |
} | |
} | |
}) | |
task.resume() | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment