条纹 / Rails 问题:"Invalid source object: must be a dictionary or a non-empty string"

Stripe / Rails Issue: "Invalid source object: must be a dictionary or a non-empty string"

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

"Invalid source object: must be a dictionary or a non-empty string"

这是我的表格:

<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

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

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

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

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