条纹 iOS ruby

Stripe iOS ruby

我正在使用 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。 return JSON 只需添加 json gem 和 return 即可。

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