【问题标题】:Stripe / Rails Issue: "Invalid source object: must be a dictionary or a non-empty string"Stripe / Rails 问题:“无效的源对象:必须是字典或非空字符串”
【发布时间】:2020-11-13 14:15:02
【问题描述】:

我正在我的 rails 应用程序中构建一个自定义结帐表单,并在发布到控制器时收到此错误:

“无效的源对象:必须是字典或非空字符串”

这是我的表格:

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

    <%= form_tag registrations_path, :id=>"stripeForm" do |f| %>
      <h2>Register for the <%= @workshop.name %> Workshop ($<%= @workshop.price %>)</h2>

      <div class="row">
        <div class="form-group col-sm-6">
          <%= text_field_tag(:f_name, nil, :placeholder=>"First Name", :class=>"form-control") %>
        </div>
        <div class="form-group col-sm-6">
          <%= text_field_tag(:l_name, nil, :placeholder=>"Last Name", :class=>"form-control") %>
        </div>
      </div>
      <div class="row text-center">
         <button class="buy-button" id="stripe-button">Buy Ticket</button>
         <%= hidden_field_tag 'stripeToken' %>
         <%= hidden_field_tag 'stripeEmail' %>
         <%= hidden_field_tag 'stripeAmount' %>
      </div>

       <script>
     var handler = StripeCheckout.configure({
       key: "<%= ENV['stripe_publishable_key'] %>",
       token: function (token, args) {
         $("#stripeToken").value = token.id;
         $("#stripeEmail").value = token.email;
         $("#stripeAmount").value = <%= @workshop.price * 100 %>;
         $("#stripeForm").submit();
       }
     });

     $('#stripe-button').on('click', function (e) {
       // Open Checkout with further options
       $name = $('input[name=f_name]').val() + " " + $('input[name=l_name]').val();
       handler.open({
         name: $name,
         description: "<%= @workshop.name %>" + " workshop",
         amount: <%= @workshop.price * 100 %>
       });
     e.preventDefault();
     });

     $(window).on('popstate', function() {
       handler.close();
     });
   </script>

    <% end %>

这是我的控制器操作:

def create
    # Amount in cents
    amount = params[:stripeAmount].to_i * 100

    # Create the customer in Stripe
    customer = Stripe::Customer.create(
      email: params[:stripeEmail],
      card: params[:stripeToken]
    )

    # Create the charge using the customer data returned by Stripe API
    charge = Stripe::Charge.create(
      customer: customer.id,
      amount: amount,
      description: 'Rails Stripe customer',
      currency: 'usd'
    )

    # place more code upon successfully creating the charge
  rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to charges_path
    flash[:notice] = "Please try again"
  end

基本上用户填写名字和姓氏,点击支付按钮。然后他们填写条带信息并提交。我试过在他们点击提交后立即使用调试器,所有的令牌信息等都在那里。

但是,一旦它到达控制器中的创建操作,我就会收到一个错误,并显示所有条带参数的空字符串。

我哪里出错了?

编辑:将以下强参数添加到控制器但没有效果:

protected

  def registration_params
    params.require(:registration).permit(:stripeEmail, :stripeToken, :stripeAmount)
  end

【问题讨论】:

  • 您使用的是什么版本的 Rails?如果 Rails 4+,你应该从帮助函数中的 params 散列中列出你需要的东西。
  • 嗯.. 所以我将 strong_params 添加到我的控制器并得到完全相同的错误(并且参数仍然为空)

标签: ruby-on-rails stripe-payments


【解决方案1】:

看起来你的 jQuery 有一个小问题。令牌在那里,但它看起来不像是通过表单提交的。这应该是 Stripe 抱怨空字符串的原因。试试这个:

$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#stripeAmount").val('<%= j @workshop.price * 100 %>');

【讨论】:

    【解决方案2】:

    仅当您尝试更新用户订阅但付款来源未与 Stripe 客户关联时才会出现此问题。

    如果您尝试将订阅从Trial 更新为Paid,请查看answer,这可能会对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-10-07
      • 2021-03-29
      • 2021-03-26
      • 1970-01-01
      • 2019-01-16
      • 2020-08-31
      • 2022-01-19
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多