Rails:从服务器返回条带响应的JSON [英] Rails: Return JSON of Stripe Response from Server
问题描述
我有多个客户端依靠我的服务器来处理Stripe收费请求.处理费用后,我想向我的客户发送回JSON,以表明费用是否已成功创建,如果不是,则说明原因.
I have multiple clients relying on my server that processes Stripe charge requests. When a charge is processed, I want to send my client back JSON of whether the charge was successfully created, and if it wasn't, the reasons why.
可以在此处进行查看.
我的控制器的代码如下:
The code for my controller is the following:
class ChargesController < ApplicationController
protect_from_forgery
skip_before_action :verify_authenticity_token
def new
end
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
#*WHAT I TRIED DOING THAT DIDN'T WORK*
# respond_to do |format|
# msg = { :status => "ok", :message => "Success!"}
# format.json { render :json => msg }
# end
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
我正在尝试使用以下URL调用我的RESTful API:
I am trying to call my RESTful API with the following URL:
curl -XPOST https://murmuring-wave-13313.herokuapp.com/charges.json?stripeToken=tok_*****************&stripeEmail=rsheeler@gmail.com
我假设我需要访问某些元数据,但是我我不确定该怎么做.
I'm assuming I need to access some of the metadata, but I'm unsure how to.
这将导致500 Response
如何正确构造我的Charges控制器以返回Stripe响应的JSON?
How can I properly structure my Charges controller in order to return JSON of Stripe's response?
推荐答案
所以我在myself自己.我意识到在创建Stripe::Charge
对象后,会为其分配一个JSON序列化的Charge
对象.
So I'm smacking myself. What I realized was after you make the Stripe::Charge
object, a JSON-serialized Charge
object is assigned to it.
因此,您可以通过简单地调用charge.attribute_name
来访问Charge
实例中的所有元数据.例如,如果这是有效费用,则charge.status
将返回成功".由于分配给您的费用是JSON,因此如果请求的格式是JSON,则只需返回render charge
即可.
Because of this, you can access all the metadata in the Charge
instance by simply calling charge.attribute_name
. For instance, if it was a valid charge, charge.status
would return "succeeded". Because what assigned back to charge is JSON you can simply return render charge
if the requested format is JSON.
工作的Charge控制器如下所示:
The working Charge controller looks like the following:
class ChargesController < ApplicationController
protect_from_forgery
skip_before_action :verify_authenticity_token
def new
end
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
# If in test mode, you can stick this here to inspect `charge`
# as long as you've imported byebug in your Gemfile
byebug
respond_to do |format|
format.json { render :json => charge }
format.html { render :template => "charges/create"}
end
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
这篇关于Rails:从服务器返回条带响应的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!