条带API响应:数据无法读取,因为它的格式不正确

时间:2017-01-10 12:35:18

标签: swift swift3 stripe-payments

我正在通过我的iOS应用程序在swift 3中进行api调用,如下所示:

@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()
}

后端功能如下所示:

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

如果我更改以下行:

manager.responseSerializer.acceptableContentTypes = NSSet(objects: "text/plain", "text/html", "application/json", "audio/wav", "application/octest-stream") as? Set<String>

到:

manager.responseSerializer = AFJSONResponseSerializer()

我得到:Request failed: payment required(402)

这很奇怪,因为条带仪表板在第一行注册了一个新帐户,但没有注册第二行。

我已经搜索过了,但我能看到的是我应该得到一个JSON响应,但相反,我得到了这个错误,我不知道如何调试它。我现在道歉,因为它可能很简单,而且我可能已经猜到了,这对于迅速而言相对较新!

任何帮助都会很棒。谢谢!

0 个答案:

没有答案