Rails:从服务器返回条带响应的JSON

时间:2022-10-24 14:06:25

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.

我有多个客户端依赖我的服务器处理Stripe收费请求。当处理费用时,我想向我的客户发回JSON,说明费用是否已成功创建,如果不是,则说明原因。

My server can be viewed here.

我的服务器可以在这里查看。

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

I am trying to call my RESTful API with the following URL:

我试图用以下URL调用我的RESTful API:

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.

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

Which results in a 500 Response

这导致500响应

How can I properly structure my Charges controller in order to return JSON of Stripe's response?

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

2 个解决方案

#1


0  

Why doesnt this work?

为什么这不起作用?

#*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

What are the errors in your log?

您的日志中有哪些错误?

#2


0  

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.

所以我在打自己。我意识到在你创建Stripe :: Charge对象之后,会为它分配一个JSON序列化的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.attribute_name即可访问Charge实例中的所有元数据。例如,如果它是有效的费用,charge.status将返回“成功”。因为分配回费用的是JSON,如果请求的格式是JSON,您可以简单地返回渲染费用。

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

#1


0  

Why doesnt this work?

为什么这不起作用?

#*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

What are the errors in your log?

您的日志中有哪些错误?

#2


0  

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.

所以我在打自己。我意识到在你创建Stripe :: Charge对象之后,会为它分配一个JSON序列化的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.attribute_name即可访问Charge实例中的所有元数据。例如,如果它是有效的费用,charge.status将返回“成功”。因为分配回费用的是JSON,如果请求的格式是JSON,您可以简单地返回渲染费用。

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