问题
I am making an api call from my iOS app in swift 3 like so:
@IBAction func registerAccountButtonWasPressed(sender: UIButton) {
let dob = self.dobTextField.text!.components(separatedBy: "/")
let URL = "https://splitterstripeservertest.herokuapp.com/account/create"
let params = [
"first_name": firstNameTextField.text!,
"last_name": lastNameTextField.text!,
"line1": addressLine1TextField.text!,
"city": cityTextField.text!,
"postal_code": postCodeTextField.text!,
"country": countryTextField.text!,
"day": UInt(dob[0])! as UInt,
"month": UInt(dob[1])! as UInt,
"year": UInt(dob[2])! as UInt] as [String : Any]
let manager = AFHTTPSessionManager()
manager.responseSerializer.acceptableContentTypes = NSSet(objects: "text/plain", "text/html", "application/json", "audio/wav", "application/octest-stream") as? Set<String>
manager.post(URL, parameters: params, success: { (operation, responseObject) -> Void in
print(responseObject!)
}) { (operation, error) -> Void in
self.handleError(error as NSError)
}
}
func handleError(_ error: NSError) {
UIAlertView(title: "Please Try Again",
message: error.localizedDescription,
delegate: nil,
cancelButtonTitle: "OK").show()
}
Where the backend function looks like this:
post '/account/create' do
begin
@account = Stripe::Account.create(
:managed => true,
:country => params[:country],
:legal_entity => {
:first_name => params[:first_name],
:last_name => params[:last_name],
:dob => {
:day => params[:day],
:month => params[:month],
:year => params[:year]
},
:address => {
:line1 => params[:line1],
:city => params[:city],
:postal_code => params[:postal_code]
},
:type => "individual"
},
:external_account => {
:object => "bank_account",
:country => 'US',
:currency => "usd",
:routing_number => "110000000",
:account_number => "000123456789"
},
:tos_acceptance => {
:date => Time.now.to_i,
:ip => request.ip
}
)
rescue Stripe::StripeError => e
status 402
return "Error creating managed cutomer account: #{e.message}"
end
status 200
return "Charge successfully created"
end
if i change the folowing line:
manager.responseSerializer.acceptableContentTypes = NSSet(objects: "text/plain", "text/html", "application/json", "audio/wav", "application/octest-stream") as? Set<String>
to:
manager.responseSerializer = AFJSONResponseSerializer()
I get: Request failed: payment required(402)
Which is odd as stripe dashboard registers a new account with the first line but not the second.
I have searched around but all I can see is that I should be getting a JSON response, but instead, i get this error, and I'm not sure how to debug it. I apologize now as it may be simple and I'm relatively new to swift as you may have guessed already!
Any help would be great. Thanks!
来源:https://stackoverflow.com/questions/41569017/stripe-api-response-the-data-couldnt-be-read-because-it-isnt-in-the-correct-f