這是爲我工作。但迅速
func cardSendToPayFort(number:String, exp_month:String, exp_year:String, cvc:String, name:String, completion: ((success: Bool, json: NSDictionary?) -> Void))
{
let username = "test_open_k_91ffe6d8f9efe14fcc91"
let password = ""
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
let orderURL = NSURL(string: "https://api.start.payfort.com/tokens/")
let request = NSMutableURLRequest(URL: orderURL!)
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
let postString = "number=\(number)&exp_month=\(exp_month)&exp_year=\(exp_year)&cvc=\(cvc)&name=\(name)"
//let postString = "number=4242424242424242&exp_month=11&exp_year=2016&cvc=123&name=sapana"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = "POST"
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 201 {
print("statusCode should be 201, but is \(httpStatus.statusCode)")
print("response = \(response)")
dispatch_async(dispatch_get_main_queue()){
completion(success: false, json: nil)
}
} else {
let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
let jsonData = responseString?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let json = try! NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
dispatch_async(dispatch_get_main_queue()){
completion(success: true, json: json)
}
}
}
task.resume()
}
請閱讀[如何提出好的問題(// stackoverflow.com/help/how-to-ask),並嘗試編輯你的問題。另外,請修改您的期望以及該過程。 **堆棧溢出不是免費的代碼寫入服務**。看看'NSURLRequest'和相關的文檔。謝謝! – SmokeDispenser