首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用1提交的Rails Stripe付款和表单

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

Stack Overflow用户
提问于 2017-06-14 17:53:35
回答 1查看 725关注 0票数 1

我的问题非常类似于Looking to Submit Rails Form Only After Successful Stripe Checkout Payment。我试过了那里接受的解决办法,但对我没有用。

我使用的是Rails 4.2,Stripe集成已经实现,我在与Stripe支付页面相同的页面中添加了一个Survey表单。如果使用“部分”中的“提交”按钮,则“调查”表单将正确显示并保存。但是我试图提交表单(创建和保存),如果表单和信用卡支付是有效的使用一个提交按钮在Stripe页面上。

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

支付/部分付款/_票证_支付小组.ticket

代码语言:javascript
运行
复制
<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 %>

该条提交作品,但它没有提交调查表格。我尝试在js中添加var $survey = $('#survey-form');并提交。但它要么只提交表格,而不是条形付款,反之亦然,取决于哪一个第一。

我还试图在控制器中添加保存。

pay_for_controller

代码语言:javascript
运行
复制
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 

这是我的调查表格

代码语言:javascript
运行
复制
<%= bootstrap_form_for(@survey, :html => { :id => "survey-form" } ) do |f| %>

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

<% end %>

目前,他们单独提交/保存正确,所以我认为模型和控制器之间的联系是好的。但我似乎无法改变js代码或控制器,以获得调查表单和Stripe付款以保存。

EN

回答 1

Stack Overflow用户

发布于 2017-06-15 05:56:11

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

当两种表单(条带、调查)准备就绪时,将调用假设Stripe请求。然后,您需要在成功地从从Stripe获得AJAX请求(post调查数据)之后,然后提交

代码语言:javascript
运行
复制
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
              }
            });
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44551537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档