0

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

Arun
  • 2,271
  • 1
  • 14
  • 18

1 Answers1

0

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
shan kulkarni
  • 849
  • 7
  • 18
  • Hello Thanks for your responce Can you please tell me how we should handle in JS file :) – Arun Jun 15 '18 at 12:56
  • Okay, if you are using jQuery u can use $.post( "endpoint",{ }, function(data) { var response = jQuery.parseJSON(data); console.log(response) } ) – shan kulkarni Jun 15 '18 at 13:05
  • also you can refer https://stackoverflow.com/questions/9098649/jquery-ajax-request-with-json-response-how-to – shan kulkarni Jun 15 '18 at 13:09
  • Thanks but my responce is coming null Also i deployed my stripe charge code is in heroku. – Arun Jun 15 '18 at 13:54
  • Can you please check in browser network tab if its returning data in response? – shan kulkarni Jun 15 '18 at 14:06
  • 'not found' but in stripe i can see charges service got 200 responce – Arun Jun 15 '18 at 14:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173224/discussion-between-userar-and-shan-kulkarni). – Arun Jun 15 '18 at 14:09