Looking to submit a Rails form only after a successful payment

I am using Rails 4.2 and trying to integrate Stripe Checkout ( https://stripe.com/docs/checkout/guides/rails ) in my rails application and have a script, i.e. Note. I tried the built-in integration of forms from several online resources, but could not get it to work, so I decided to do a check.

In my rails application, I have an order table, and the main thing I'm trying to execute is the form in which the user can send his personal information (non-payment) to place an order. Then integration with lane checking will allow them to pay; however, an order record will not be created in the database without registering the strip. I could not accomplish this using a separate β€œcharge” controller, which offers a strip, and also tried to include the strip code in my order controller (see below).

I should note that I HAVE was able to get the check button to send to the strip, and the fees are processed, but I MUST managed to get the order record to be created in my database.

I searched broadly and broadly for an answer to this question (currently awaiting an answer from the support band). Any suggestions would be much appreciated!

orders_controller.rb

(this is an example when I tried to combine the strip code from the board controller that they offer into my own order controller. I'm not sure what to do now after the board has been processed to receive it for form submission)

def create @order = Order.new(order_params) customer = Stripe::Customer.create( :email => ' example@stripe.com ', :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 leave the code for all the other fields in my form, it just shows my submit button and the check strip button. Ideally, I could combine the actions into one button or just transfer when the payment is successfully processed through the strip)

 <%= 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 subscription" data-amount="500"></script> 
+5
source share
1 answer

Usually you do ...

 def create @order = Order.new(order_params) charge_error = nil if @order.valid? begin customer = Stripe::Customer.create( :email => ' example@stripe.com ', :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 

Thus, the fee is not paid if @order is not checked, and @order is not saved if the charge is not performed.

+11
source

Source: https://habr.com/ru/post/1211043/


All Articles