form_tag 在提交时不产生 stripeToken [英] form_tag does not produce stripeToken upon submit

查看:32
本文介绍了form_tag 在提交时不产生 stripeToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注本教程以获得自定义结帐按钮

我根据需要更改了教程,当我检查 params 时,stripeToken 为空

<块引用>

{"utf8"=>"✓", "authenticity_token"=>"sometokenvalue","stripeToken"=>"", "amount"=>"40000", "item_id"=>"3"}

我怀疑我收到此错误

<块引用>

无效的源对象:必须是字典或非空字符串.看API 文档位于 https://stripe.com/docs'

因此.

我的代码如下:

<%= notice %>

<div class="容器"><div class="row"><div class="col-md-12"><p><strong>名称:</strong><%=@item.name %></p><p><strong>价格:</strong><%=@item.price %></p><script src="https://checkout.stripe.com/checkout.js"></script><%= form_tag item_charges_path(@item, amount: @item.price), id: 'paying-form' do %><% if flash[:error].present?%><div id="error_explanation"><p><%= flash[:error]%</p>

<%结束%><文章></文章><文章><%= hidden_​​field_tag :stripeToken %></文章><button id='donateButton' class='btn btn-primary'>Pay</button><%结束%><%= link_to '编辑', edit_item_path(@item) %>|<%= link_to '返回',items_path %><脚本>var handler = StripeCheckout.configure({key: '<%= Rails.configuration.stripe[:publishable_key] %>',语言环境:'自动',令牌:功能(令牌){$('input#stripeToken').val(token.id);$('form').submit();}});<脚本>document.getElementById('donateButton').addEventListener('click', function(e) {//e.preventDefault();$('#error_explanation').html('');var amount = "#{@item.price}";数量 = 数量.replace(/\$/g, '').replace(/\,/g, '')金额 = parseFloat(金额);如果(isNaN(金额)){$('#error_explanation').html('<p>请输入有效的美元金额 ($).</p>');}别的 {金额 = 金额 * 100;//必须是整数!handler.open({金额:Math.round(金额)})e.preventDefault();}});$(window).on('popstate', function() {handler.close();});

注意:为了简单起见,我将 js 代码放在同一页面中.

谁能告诉我我的表单不生成 stripeToken 有什么问题?或者可能是我遗漏了一些要点?

注意:如果需要更多信息,请告诉我,以便我发布谢谢

解决方案

你的 JS 中有一个错字:

var amount = "#{@item.price}";

...应该是...

var amount = <%= @item.price %>;

然后,将以下代码移动到该行下方的顶部:addEventListener

e.preventDefault();

然后,删除以下内容,因为您不再需要它了,因为 @item.price 直接来自您的数据库:

amount = amount.replace(/\$/g, '').replace(/\,/g, '')金额 = parseFloat(金额);

最后,因为 amount 保证是正确的并且有一个整数值,那么你不再需要以下内容,所以删除它们:

if (isNaN(amount)) {$('#error_explanation').html('<p>请输入有效的美元金额 ($).</p>');}

不要忘记删除悬空的} else { }代码

说明:

P.S.本身还没有使用过 Stripe

I am following this tutorial for having customized checkout button

I changed the tutorial for my need, and when I check my params , stripeToken is empty

{"utf8"=>"✓", "authenticity_token"=>"sometokenvalue", "stripeToken"=>"", "amount"=>"40000", "item_id"=>"3"}

I suspect I get this error

Invalid source object: must be a dictionary or a non-empty string. See API docs at https://stripe.com/docs'

owing to that.

my code as follows :

<p id="notice"><%= notice %></p>
<div class="container">
  <div class="row">
    <div class="col-md-12"><p>
      <strong>Name:</strong>
        <%= @item.name %>
      </p>  
      <p>
        <strong>Price:</strong>
        <%= @item.price %>
      </p>

       <script src="https://checkout.stripe.com/checkout.js"></script>

      <%= form_tag item_charges_path(@item, amount: @item.price), id: 'paying-form' do %>
          <% if flash[:error].present? %>
            <div id="error_explanation">
              <p><%= flash[:error] %></p>
            </div>
          <% end %>
        <article>
        </article>
        <article>      
          <%= hidden_field_tag :stripeToken  %>
        </article>
        <button id='donateButton' class='btn btn-primary'>Pay</button>
      <% end %>

        <%= link_to 'Edit', edit_item_path(@item) %> |
        <%= link_to 'Back', items_path %>


          <script>
            var handler = StripeCheckout.configure({
              key: '<%= Rails.configuration.stripe[:publishable_key] %>',
              locale: 'auto',
              token: function(token) {   
                $('input#stripeToken').val(token.id);
                $('form').submit();
              }
            });
          </script>
          <script>
          document.getElementById('donateButton').addEventListener('click', function(e) {
              //e.preventDefault();

              $('#error_explanation').html('');

              var amount = "#{@item.price}";
              amount = amount.replace(/\$/g, '').replace(/\,/g, '')

              amount = parseFloat(amount);

              if (isNaN(amount)) {
                $('#error_explanation').html('<p>Please enter a valid amount in USD ($).</p>');
              }
              else {
                amount = amount * 100; // Needs to be an integer!
                handler.open({
                  amount: Math.round(amount)
                })
                e.preventDefault();
              }
            });

            $(window).on('popstate', function() {
              handler.close();
            });

          </script>
    </div>
  </div>
</div>

Note: I put the js codes in the same page for simplicity goals.

Could anyone tell me what is wrong that my form does not produce stripeToken? or May be I am missing some points?

Note: please if more info is needed,let me know so I post Thank you

解决方案

You have a typo in your JS:

var amount = "#{@item.price}";

...should instead be...

var amount = <%= @item.price %>;

Then, move the following code at the top just below the line: addEventListener

e.preventDefault();

Then, remove the following because you don't need it anymore because the @item.price comes directly from your database:

amount = amount.replace(/\$/g, '').replace(/\,/g, '')
amount = parseFloat(amount);

Lastly, because amount is guaranteed to be correct and has an Integer value, then you don't need the following anymore and so remove them:

if (isNaN(amount)) {
  $('#error_explanation').html('<p>Please enter a valid amount in USD ($).</p>');
}

don't forget to remove the dangling } else { } code

Explanation:

P.S. Haven't used Stripe per se

这篇关于form_tag 在提交时不产生 stripeToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆