Rails: Return JSON of Stripe Response from Server

浪尽此生 提交于 2019-12-12 03:56:06

问题


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.

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:

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

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


回答1:


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:


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.

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.

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


来源:https://stackoverflow.com/questions/41985521/rails-return-json-of-stripe-response-from-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!