【问题标题】:Rails: Return JSON of Stripe Response from ServerRails:从服务器返回条带响应的 JSON
【发布时间】:2017-06-18 12:56:05
【问题描述】:

我有多个客户依赖我的服务器来处理 Stripe 收费请求。处理费用后,我想向我的客户发送有关费用是否成功创建的 JSON,如果未成功创建,原因是什么。

可以查看我的服务器here

我的控制器的代码如下:

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:

curl -XPOST https://murmuring-wave-13313.herokuapp.com/charges.json?stripeToken=tok_*****************&stripeEmail=rsheeler@gmail.com

我假设我需要访问一些 metadata,但我不确定如何访问。

这会导致500 Response

如何正确构建 Charges 控制器以返回 Stripe 响应的 JSON?

【问题讨论】:

    标签: ruby-on-rails json stripe-payments


    【解决方案1】:

    为什么这不起作用?

    #*WHAT I TRIED DOING THAT DIDN'T WORK*
     respond_to do |format|
       msg = { :status => "ok", :message => "Success!"}
       format.json  { render :json => msg } # don't do msg.to_json
       format.html  { render :template => "charges/create"}
     end
    

    您的日志中有哪些错误?

    【讨论】:

    • 我意识到我搞砸了,忘记在这种情况下将format.html { render :template =&gt; "charges/create"} 提交给heroku,所以我得到的错误与我原来的问题无关(至少我不认为)。很抱歉有任何混淆。
    【解决方案2】:

    所以我在打自己。我意识到,在您创建 Stripe::Charge 对象后,会为其分配一个 JSON 序列化的 Charge 对象。

    因此,您只需调用charge.attribute_name 即可访问Charge 实例中的所有元数据。例如,如果它是有效的收费,charge.status 将返回“成功”。因为分配回费用的是 JSON,如果请求的格式是 JSON,您可以简单地返回 render charge

    正常工作的 Charge 控制器如下所示:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      相关资源
      最近更新 更多