Responce back after create charge in Stripe

55 views Asked by At

I have below code in Ruby file which is

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

and my question is how to pass json value after creating successfully charge.

Any help is highly appreciated.

Many Thanks

1

There are 1 answers

7
shan kulkarni On

If you want to return json response of the charge request, you can pass charge into body of response:

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