i am working on an online store in React with backend in Rails and i use Stripe. After i send the fetch request towards my charges controller gets sent successfully, i can see the parameters , everything good.
Started POST "/charges" for 127.0.0.1 at 2018-02-10 20:22:16 +0000
Processing by ChargesController#create as JSON
Parameters: {"description"=>"Only the Book", "source"=>"tok_1Bu4rRHmkRoa1PQ1R3ndnG2o", "amount"=>100, "charge"=>{"description"=>"Only the Book", "source"=>"tok_1Bu4rRHmkRoa1PQ1R3ndnG2o", "amount"=>100}}
Redirected to http://localhost:3000/charges/new
Completed 302 Found in 2554ms (ActiveRecord: 0.0ms)
But when the controller create action performs the redirection towards the new method, i get this error:
ActionController::UnknownFormat (ChargesController#new is missing a template for this request format and variant.
request.formats: ["application/json"]
request.variant: []):
I tried renspond_to :json in the controller in the new method, does not work, i tried to deserialize the params passing them to instance variables but they wont work because they are nil, i did not try json builder yet because i do not know if this would be the simpler way. This is my code below: charges_controller.rb class ChargesController < ApplicationController
def new
@amount = params[:stripeAmount]
@description = params[:stripeDescription]
end
def create
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => params[:stripeAmount],
:description => params[:stripeDescription],
:currency => 'GBP'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
this is my new.html.erb
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: <%= (@amount) %></span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="<%= @description %>"
data-amount="<%= @amount %>"
data-locale="auto"></script>
<% end %>
Thank you very much for your kind help!
Looks like your not responding with json. Your rescue is a html response, so that will definitely not work. You need to send the response back as json since that's what your react app expects. It's been a while since I've used stripe, but something like this:
The following would assume you have respond_with:
respond_to :json
def create
# ... your stripe customer and charge api requests
respond_with charge
end
Or if you don't have respond_with:
def create
# ... your stripe customer and charge
respond_to do |format|
format.json { render json: charge }
end
end
This is the similar problem: Rails: Return JSON of Stripe Response from Server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With