为什么我的条纹元素形式在轨道上不起作用?

时间:2019-02-09 22:45:00

标签: ruby-on-rails ruby-on-rails-5 stripe-payments

我正试图让Stripe.js在我的Rails应用程序上工作。我正在使用条纹元素。我已按照条纹文档上的说明进行操作。表格卡输入没有显示在应有的位置。它只显示提交按钮。

我正在尝试将Stripe表单放置在现有的Rails表单中。

<!DOCTYPE html>
<html>
<head>
<title>Bennett</title>
 <%= csrf_meta_tags %>
 <%= csp_meta_tag %>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="https://js.stripe.com/v3/"></script>
<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= render '/home/nav' %>
<%= yield %>
</br>
<%= render '/home/footer' %>
</body>
</html>

访问/ _FORM

<%= simple_form_for([@service, @visit]) do |f| %>

  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
  <%= f.input :visit_date, as: :date , :label => "Service date:"%>
  <%= f.input :visit_time, as: :time , :label => "Service time:" %>
  <%= f.input :note, :label => "Anything we should know?" %>

<%= render 'charges/payment-form' %>

<%= f.button :submit, class:'btn btn-bennett' %>

<% end %>

_PAYMENT-FORM

<form action="/charge" method="post" id="payment-form">
  <div class="form-row">
    <label for="card-element">
      Credit or debit card
    </label>
    <div id="card-element">
      <!-- A Stripe Element will be inserted here. -->
    </div>

    <!-- Used to display form errors. -->
    <div id="card-errors" role="alert"></div>
  </div>

  <button>Submit Payment</button>
</form>

CHARGE.JS

// Create a Stripe client.
var stripe = Stripe('XXXXXXXXXXXXXX');

// Create an instance of Elements.
var elements = stripe.elements({
    fonts: [
      {
        cssSrc: 'https://fonts.googleapis.com/css?family=Roboto',
      },
    ],
    // Stripe's examples are localized to specific languages, but if
    // you wish to have Elements automatically detect your user's locale,
    // use `locale: 'auto'` instead.
    locale: window.__exampleLocale
  });


// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
  base: {
    color: '#32325d',
    lineHeight: '18px',
    fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
    fontSmoothing: 'antialiased',
    fontSize: '16px',
    '::placeholder': {
      color: '#aab7c4'
    }
  },
  invalid: {
    color: '#fa755a',
    iconColor: '#fa755a'
  }
};

// Create an instance of the card Element.
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});

// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
  event.preventDefault();

  stripe.createToken(card).then(function(result) {
    if (result.error) {
      // Inform the user if there was an error.
      var errorElement = document.getElementById('card-errors');
      errorElement.textContent = result.error.message;
    } else {
      // Send the token to your server.
      stripeTokenHandler(result.token);
    }
  });
});

// Submit the form with the token ID.
function stripeTokenHandler(token) {
  // Insert the token ID into the form so it gets submitted to the server
  var form = document.getElementById('payment-form');
  var hiddenInput = document.createElement('input');
  hiddenInput.setAttribute('type', 'hidden');
  hiddenInput.setAttribute('name', 'stripeToken');
  hiddenInput.setAttribute('value', token.id);
  form.appendChild(hiddenInput);

  // Submit the form
  form.submit();
}

我不知道为什么表格没有显示。

基本上,我要达到的目标是实现这一目标,因此当我通过“访问”表单创建新的“访问”时,我还通过条处理付款,所有付款都通过一个按钮提交。也许有人也可以帮我吗?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,通过将其添加到CSS中解决了这个问题:

.StripeElement{
    min-width: 300px;
}

答案 1 :(得分:0)

您似乎不是在等待页面加载以执行脚本,因此找不到#card-element的错误。

您应该包装代码以等待页面加载:

document.addEventListener('turbolinks:load',function () {
 ....Your code
}