在Stripe中创建充电后回复

时间:2018-06-15 09:44:19

标签: ruby stripe-payments stripe.js

我在Ruby文件中有以下代码

post '/create_charge' do
  # Create the charge on Stripe's servers
  begin
    charge = Stripe::Charge.create(
      :amount => params[:amount], # this number should be in cents
      :currency => "usd",
      :source => params[:source],
      :description => "sample"
    )
  rescue Stripe::StripeError => e
    status 402
    return log_info("Error creating charge: #{e.message}")
  end

  status 200
  return log_info("Charge successfully created")
end

我的问题是如何在成功充电后传递json值。

非常感谢任何帮助。

非常感谢

1 个答案:

答案 0 :(得分:0)

如果您想要返回充电请求的json响应,您可以将费用转入响应主体:

post '/create_charge' do
  # Create the charge on Stripe's servers
  charge = {}
  begin
    charge = Stripe::Charge.create(
      :amount => params[:amount], # this number should be in cents
      :currency => "usd",
      :source => params[:source],
      :description => "sample"
    )
  rescue Stripe::StripeError => e
    status 402
    return log_info("Error creating charge: #{e.message}")
  end

  status 200
  content_type :json
  body charge.to_json   
  return log_info("Charge successfully created")
end