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

时间:2017-06-14 17:53:35

标签: ruby-on-rails forms stripe-payments ruby-on-rails-4.2

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

我使用的是Rails 4.2,Stripe集成已经实现,我在{Str。付款页面>的同一页面添加了Survey表单。如果我使用部分提交按钮,则调查表单会正确显示并保存。但是,如果表单和信用卡付款使用条带付款页面上的一个提交按钮有效,我会尝试提交表单(创建并保存)。

看起来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 %>

条纹提交有效,但不提交调查表。我尝试添加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 :(得分:0)

  

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

假设当两个表单(条带,调查)准备就绪时将调用Stripe请求。然后,您需要在从Stripe成功获取token之后发出AJAX请求(发布调查数据),然后提交条纹表单

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
              }
            });
        }
    }