I am using Rails 4.2, and trying to integrate Stripe Checkout (https://stripe.com/docs/checkout/guides/rails) in my rails app and have a scenario I haven't seen outlined anywhere. Note: I tried custom form integration from a number of online resources but couldn't get it to work so opted to give checkout a go.
In my rails app, I have an orders table, and the main thing I'm trying to accomplish is having a form where the user can submit their personal information (non-payment) to place an order. Then, the stripe checkout integration will allow them to pay; however, a record of the order will not be created in the database without a stripe charge being logged. I've been unable to accomplish this with using the separate "charges" controller that stripe suggests, and also tried incorporating the stripe code into my orders controller (see below).
I should note that I HAVE been able to get checkout button to submit to stripe and the charges are processed, but HAVE NOT been able to get a order record to be created in my database.
I have searched far and wide for an answer to this question (currently waiting on response from stripe support). Any suggestions would be much appreciated!
orders_controller.rb
(this is the example where I tried combining the stripe code from the charges controller they suggest into my own orders controller. i'm not sure now what to do after the charge is processed to get it to submit the form)
def create
@order = Order.new(order_params)
customer = Stripe::Customer.create(
:email => '[email protected]',
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => 5000,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
render 'new'
end
orders/new.html.erb
(I am leaving out code for all the other fields in my form, this just shows my form submit button and the stripe checkout button. Ideally I could combine the actions into one button, or only have the submit go through when the payment successfully processes through stripe)
<%= form_for @order do |f| %>
// lots of form fields
<%= f.submit %>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"></script>
Typically you would do...
def create
@order = Order.new(order_params)
charge_error = nil
if @order.valid?
begin
customer = Stripe::Customer.create(
:email => '[email protected]',
:card => params[:stripeToken])
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => 5000,
:description => 'Rails Stripe customer',
:currency => 'usd')
rescue Stripe::CardError => e
charge_error = e.message
end
if charge_error
flash[:error] = charge_error
render :new
else
@order.save
redirect_to (successful page)
end
else
flash[:error] = 'one or more errors in your order'
render :new
end
end
This way the charge isn't made unless the @order is validated, and the @order isn't saved unless the charge is successful.
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