如何在Rails中使用Stripe请求自定义金额?

时间:2017-10-03 03:09:50

标签: ruby-on-rails stripe-payments

我正在尝试按照Stripe / Rails教程让客户选择他们想要支付的费用:https://stripe.com/docs/recipes/variable-amount-checkout

当我尝试这样做时,我不断收到错误消息:

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

    <script>
    var handler = StripeCheckout.configure({
      key: '<%= Rails.configuration.stripe[:publishable_key] %>',
      locale: 'auto',
      name: 'Sand Castles United',
      description: 'One-time donation',
      token: function(token) {
        $('input#stripeToken').val(token.id);
        $('form').submit();
      }
    });

    $('#donateButton').on('click', function(e) {
      e.preventDefault();

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

      var amount = $('input#amount').val();
      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 if (amount < 5.00) {
        $('#error_explanation').html('<p>Donation amount must be at least $5.</p>');
      }
      else {
        amount = amount * 100; // Needs to be an integer!
        handler.open({
          amount: Math.round(amount)
        })
      }
    });

    // Close Checkout on page navigation
    $(window).on('popstate', function() {
      handler.close();
    });

    </script>


    <%= form_tag charges_path do %>
      <div id="error_explanation">
        <% if flash[:error].present? %>
          <p><%= flash[:error] %></p>
        <% end %>
      </div>
      <article>
        <%= label_tag(:amount, 'Donation Amount:') %>
        <%= text_field_tag(:amount) %>
      </article>
      <article>
        <%= hidden_field_tag(:stripeToken) %>
      </article>
      <button id='donateButton'>Donate</button>
    <% end %>

我相信这是因为params [:stripeToken]没有被设置。关于如何解决这个问题的任何建议?

当前的new.html.erb

{{1}}

0 个答案:

没有答案