试图将stripe.js与rails一起使用,无法弄明白

时间:2016-08-07 21:31:34

标签: ruby-on-rails

尝试按照此设置条带。这是一个非常好的结帐指南特定的导轨:https://stripe.com/docs/checkout/rails

但是,我找不到使用stripe.js的类似指南(因为我想要自己的自定义表单。我在实现条带方面遇到了麻烦,我找到的唯一指南是{{3哪个不适合使用rails。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

下面是根据我的经验快速操作方法(您可以找到我的虚拟Rails-Stripe集成here):

首先,将gem 'stripe'添加到您的Gemfile中。运行bundle install并重新启动您的网络服务器。

创建charges_controller.rb添加charge_customform操作:

def charge_customform
    # Amount in cents
    @amount = 99

    # Get the credit card details submitted by the form
    customer = Stripe::Customer.create(
        :email => params[:email],
        :source  => params[:stripeToken]
    )

    # Create the charge on Stripe's servers - this will charge the user's card
    begin
      Stripe::Charge.create(
          :amount => @amount,
          :currency => 'usd',
          :customer => customer.id,
          :description => 'Example charge custom form'
      )
      redirect_to charges_thanks_path
      # remember to create a thanks.html.erb page in view/charges :)

      rescue Stripe::CardError => e
      flash[:error] = e.message
      redirect_to root_path
    end

end

在您的视图中,首先在页面中导入所需的JS:

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

然后设置您的可发布密钥

<script type="text/javascript">
  Stripe.setPublishableKey('your_key_here');
</script>

添加自定义form_tag:

<%= form_tag charge_customform_charges_path, id: 'payment-form' do %>
  <span class="payment-errors"></span>

  <div class="form-group">
    <label>
      <span>Your email</span>
      <input value="<%= current_user.email if current_user %>" type="text" size="20" data-stripe="email">
    </label>
  </div>

  <div class="form-group">
    <label>
      <span>Card Number</span>
      <input type="text" size="20" data-stripe="number">
    </label>
  </div>

  <div class="form-group">
    <label>
      <span>Expiration (MM/YY)</span>
      <input type="text" size="2" data-stripe="exp_month">
    </label>
    <span> / </span>
    <input type="text" size="2" data-stripe="exp_year">
  </div>

  <div class="form-group">
    <label>
      <span>CVC</span>
      <input type="text" size="4" data-stripe="cvc">
    </label>
  </div>

  <input type="submit" class="submit" value="Submit Payment">
<% end %>

最后添加JS以调用Stripe的API并使用令牌处理响应:

<script>
  $(function() {
    var $form = $('#payment-form');
    $form.submit(function(event) {
      // Disable the submit button to prevent repeated clicks:
      $form.find('.submit').prop('disabled', true);
      // Request a token from Stripe:
      Stripe.card.createToken($form, stripeResponseHandler);
      // Prevent the form from being submitted:
      return false;
    });
  });
  function stripeResponseHandler(status, response) {
    // Grab the form:
    var $form = $('#payment-form');
    if (response.error) { // Problem!
      // Show the errors on the form:
      $form.find('.payment-errors').text(response.error.message);
      $form.find('.submit').prop('disabled', false); // Re-enable submission
    } else { // Token was created!
      // Get the token ID:
      var token = response.id;
      // Insert the token ID into the form so it gets submitted to the server:
      $form.append($('<input type="hidden" name="stripeToken">').val(token));
      // Submit the form:
      $form.get(0).submit();
    }
  };
</script>

修改

在我的第一个回答中,我提到了this Railscast,它解释了如何使用自定义表单设置计划和订阅。仍然是一个很好的基础,但可能不是100%与问题相关。