首页 文章

Rails Stripe付款和表格使用1提交

提问于
浏览
1

我的问题与Looking to Submit Rails Form Only After Successful Stripe Checkout Payment非常相似 . 我尝试了那里接受的解决方案,但它对我不起作用 .

我正在使用Rails 4.2,Stripe集成已经实现,我在与Stripe付款页面相同的页面中添加了 Survey 表单 . 如果我使用部分提交按钮,则调查表单会正确显示并保存 . 但是,如果表格和信用卡付款有效,请使用条纹付款页面上的 one 提交按钮提交表单(创建并保存) .

看起来Stripe付款表单是通过js提交的 .

付款/谐音/ _ticket_payment_panel.html

<form action='/payments/pay_for/receive_payments' id="ticket-form" method="POST">
  // form fields 
</form>


<% content_for :scripts do %>

  <script type="text/javascript" charset="utf-8">
    $(function () {
        var $form = $('#ticket-form');
        //var $survey = $('#survey-form');
        $form.submit(function (event) {
            // Disable the submit button to prevent repeated clicks:
            $form.find('.submit').prop('disabled', true);
            // Request a token from Stripe:
            Stripe.card.createToken($form, stripeResponseHandler);
            // Prevent the form from being submitted:
            return false;
        });
    });

    function stripeResponseHandler(status, response) {
        var $form = $('#ticket-form');
        if (response.error) {
            // Show the errors on the form
            $('.submit').removeProp('disabled');
        } else {
            // response contains id and card, which contains additional card details
            var token = response.id;
            // Insert the token into the form so it gets submitted to the server
            //execute this block only if plan exists -> helpers/payments/application_helper.rb

            $form.get(0).submit();
        }
    }
  </script>

<% end %>

Stripe提交有效,但不提交调查表 . 我尝试添加 var $survey = $('#survey-form'); 并在js中提交 . 但它要么只提交表格而不提交条纹支付,反之亦然,具体取决于哪个是第一个 .

我还尝试在控制器中添加保存 .

pay_for_controller

def receive_payments
  @survey = Survey.new
  @survey.userprofile_id = current_user.userprofile.id
  skills = params[:skills]
  work_type = params[:work_type]
  @survey.save

  //credit card attempts, delayed jobs code 
end

这是我的调查表

<%= bootstrap_form_for(@survey, :html => { :id => "survey-form" } ) do |f| %>

  //form fields 
  <%= f.submit "Apply", class: 'action-btn btn-rounded btn-large' %>

<% end %>

目前,他们单独提交/保存正确,所以我认为模型和控制器之间的链接很好 . 但我似乎无法改变js代码或控制器以获得Survey表单和Stripe付款以进行保存 .

1 回答

  • 0

    遵循粗略逻辑通过单一提交解释解决方案

    Assuming Stripe request will be called when two forms (stripe, survey) are ready. Then you need to make a AJAX request (post survey data) after successfully getting token from Stripe, then Stripe form submit

    function stripeResponseHandler(status, response) {
            var $form = $('#ticket-form');
            if (response.error) {
                $('.submit').removeProp('disabled');
            } else {
                var token = response.id;
                //here is the ajax call to submit survey
                $.ajax({
                  type: "POST",
                  url: "url/of/your/survey/form/post",
                  data: "{empid: " + empid + "}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function(result){
                       //submit stripe form only if the survey data saved
                       $form.get(0).submit();
                  },
                  error: function(){
                       //handle error situation if post request failed
                  }
                });
            }
        }
    

相关问题