ios - Stripe iOS ruby

标签 ios ruby swift sinatra

我正在使用 stripe 在 swift 2.2 中编写一个应用程序。我的后端运行 ruby​​ 脚本

post '/charge' do

  # Get the credit card details submitted by the form
  Stripe.api_key = params[:stripeAPIKey]
  token = params[:stripeToken]

  # Create the charge on Stripe's servers - this will charge the user's card
  begin
    charge = Stripe::Charge.create(
      :amount => params[:amount], # this number should be in cents
      :currency => "eur",
      :card => token,
      :description => params[:description],
      :receipt_email => params[:email],
      :statement_descriptor => params[:statement]
    )
    #rescue Stripe::CardError => e
    # Since it's a decline, Stripe::CardError will be caught
  end

  status 200
  return "Succès"
end

但我不知道如何在出现错误时获取 Stripe 返回的 JSON 消息。

在我的应用程序中,我使用 Alamofire

Alamofire.request(.POST, requestString, parameters: (params as! [String:  AnyObject]))
        .responseJSON { response in
            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }

最佳答案

您似乎正在使用 Sinatra。要返回 JSON,只需添加 json gem 并返回它。

post '/charge' do
  # ...
  begin
    charge = Stripe::Charge.create(
      # ...
    )

  rescue Stripe::CardError => e
    content_type :json
    return { error: true, field: 'value' }.to_json
  end

  # ...
end

但是,对于成功和错误这两种情况,最好坚持使用一种内容类型。

post '/charge' do
  content_type :json
  result = {}

  # ...
  begin
    charge = Stripe::Charge.create(
      # ...
    )
    status 200
    { status: 'success', field: 'value'}.to_json
  rescue Stripe::CardError => e
    status 500
    { error: true, field: 'value' }.to_json
  end
end

关于ios - Stripe iOS ruby ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466189/

相关文章:

ios - 类型 'ViewController' 没有成员 'btnPressed'

ios - AVPlayer 日志机制 - Swift

ruby-on-rails - Sunspot solr, order_by 整数值彻底坏掉

ios - 存储值类型与指针操作数类型不匹配! - 存储 i64、swift.bridge、dictionaryUpCasts

swift - 如何设置 NSTableColumn 内 NSTextField 的宽度

ios - 如何创建跨越多个部分的粘性标题

ios - 方法【load】是否需要调用【super load】

iphone - ASIHTTPRequest 在失去互联网连接后恢复请求

ruby - 我无法在 Windows 中安装 EventMachine

ruby - 使用 gsub 去除多个字符