Skip to content

Instantly share code, notes, and snippets.

@dariushoule
Last active August 29, 2015 14:14
Show Gist options
  • Save dariushoule/6e9ee6d551842d8f13ec to your computer and use it in GitHub Desktop.
Save dariushoule/6e9ee6d551842d8f13ec to your computer and use it in GitHub Desktop.
Tangocard API example code in Swift, ordering a $15.00 Amazon card
func placeOrder() {
// Platform and account information
let platform = "<platform>"
let key = "<key>"
let customer = "<customer>"
let account_identifier = "<account>"
// Prepare basic auth
let loginString = NSString(format: "%@:%@", platform, key)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(nil)
// Set the URL and headers
let url = NSURL(string: "https://sandbox.tangocard.com/raas/v1/orders")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
// Build the POST data
let postBody = [
"customer": customer,
"account_identifier": account_identifier,
"campaign": "",
"recipient": [
"name": "Test Recipient",
"email": "[email protected]"
],
"sku": "AMZN-E-V-STD",
"amount": 1500,
"reward_from": "Test Sender",
"reward_subject": "Test Reward Subject",
"reward_message": "Here is your reward!",
"send_reward": true
]
var serializationError: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(postBody,
options:NSJSONWritingOptions.PrettyPrinted,
error:&serializationError);
if serializationError == nil {
// Perform the network request
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
if error == nil {
var parseError: NSError?
let orderCreateResponse: AnyObject? = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions.AllowFragments,
error:&parseError)
// Verify response and print the order id and claim code from the response
if parseError == nil {
if let responseDict = orderCreateResponse as? NSDictionary {
if responseDict["success"] === true {
let order = responseDict["order"] as NSDictionary
let orderId = order["order_id"] as NSString
let reward = order["reward"] as NSDictionary
let claim = reward["number"] as NSString
println("Order id: \(orderId)")
println("Order claim code: \(claim)")
} else {
let errorStr = responseDict["error_message"] as NSString
println("There was an error placing the order: \(errorStr)")
}
} else {
println("There was an error decoding the response, was not a JSON object")
}
} else {
println("There was an error decoding the response: \(parseError!.description)")
}
} else {
println("There was an error making the request: \(error.description)")
}
}
} else {
println("There was an error encoding the POST data: \(serializationError!.description)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment