简体   繁体   中英

How to make a stripe charge in rails form BEFORE form is submitted

I have a form that is filled out by a userA. IF userA says that they will pay then I want a charge button to appear within the form (BEFORE THE FORM IS SUBMITTED) next to the drop down menu that userA chose that they would pay(Im guessing javascript?). Also, the form itself will NOT submit if the charge for payment does NOT complete. If the dropdown menu is set to userB will pay (the other user type) then the form will submit.

I am unsure what I can use to do this I think javascript but I know ruby not too much javascript. Also, the charge MUST get the amount of the charge from within a 2nd dropdown menu on the form. So the charge button would need to pull that amount from the pre-submitted form. Once again puts me in the mind that this can be accomplished with javascript.

I have javascript for my stripe button but need some code to get the button to appear from when the dropdown is selected and also to pull the amount from the other dropdown in the form.

Here is an example of the actual flow:

Submit Form -> Check Attrs :x -> If Attrs X False, Create Record || Elsif Attr True -> Get payment Amount from Attrs: Y, Render Payment View showing Attrs Y as amount due -> Submit Payment Form -> Upon Successful charge -> Create Original Record

You should not rely on javascript if you need things to be authorized in your application. Users can easily circumvent this.

I think you are thinking about this a bit wrong - you are not going to have two users in the same browser session or have somebody sit and wait with the browser window open until a supervisor approves their request. This is not the timesharing era.

一台非常大的计算机

Instead you would have the unprivileged user create a PurchaseRequest when they submit the form. The supervisor would get a notification and can approve or cancel the PurchaseRequest.

Whether you want the ball to roll back to user 1 in this case depends on the exact use case but in my experience the supervisor is usually the one holding onto the company card.

When using stripe or any other payment system you submit a payment and your then get a callback request which tells you if the payment was a success.

  1. Joe submits form to create a request
  2. Jane (supervisor) is out playing golf and approves it 3 days later.
  3. Joe receives a notification that he now can pay the request. He clicks the pay button and the request is forwarded to the payment portal.
  4. He submits the payment, and is redirected back to your app. In this action your need to check the state of the payment and render a view accordingly.

This is a generic example and you should check out the Stripe docs for what flow they recommend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM