Rails条纹错误“无法向没有活动卡的客户收费”

时间:2016-08-30 03:15:56

标签: ruby-on-rails stripe-payments

发生了什么事?当使用测试键时,一切都工作正常,但更改为实时键给我这个错误,“无法向没有活动卡的客户收费”。

我的Charges Controller

class ChargesController < ApplicationController

before_action :check_if_user_already_subscribed, only: [:new]

    def new 

    end


    def create
    # Amount in cents
    @amount = 100



    # Create the charge on Stripe's servers - this will charge the user's card
    begin
        # Get the credit card details submitted by the form
    customer = Stripe::Customer.create(
        :email => params[:email],
        :source  => params[:stripeToken]
    )

      Stripe::Charge.create(
          :amount => @amount,
          :currency => 'usd',
          :customer => customer.id,
          :description => 'Example charge custom form'
      )

      current_user.subscribed = true
      current_user.stripe_id = customer.id
      current_user.expiry_date = Date.today + 90.days
      current_user.save


      flash[:success] = "Thank you for subscribing. Your account has been unlocked."
      redirect_to root_path

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


end

    private

    def check_if_user_already_subscribed

        if current_user.subscribed
        flash[:danger] = "You have already subscribed. Please wait until your subscription runs out to resubscribe."
        redirect_to root_path
        end


    end 

end

我的new.html.erb

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

<%= form_tag charges_path, id: 'payment-form' do %>


<div class = 'charge-form'>
  <span class="payment-errors"></span>

  <div class="form-group">
    <label>
      <input value="<%= current_user.email if current_user %>" type="hidden" data-stripe="email" >
    </label>
  </div>

     <div class="form-group">
    <label>
      <span>Full name</span><span class = 'small-text'> (e.g. John Smith)</span>
      <input type="text" size="5" data-stripe="name"  class = 'form-field'>
    </label>
  </div>

   <div class="form-group">
    <label>
      <span>Country</span><span class = 'small-text'> (e.g. United States)</span>
      <input type="text" size="10" data-stripe="address_country" class = 'form-field'>
    </label>
  </div>

   <div class="form-group">
    <label>
      <span>Address</span>
      <input type="text" size="10" data-stripe="address_line1" class = 'form-field'>
    </label>
  </div>

  <div class="form-group">
    <label>
      <span>Postal Code / Zip Code</span>
      <input type="text" size="4" data-stripe="address_zip" class = 'form-field'>
    </label>
  </div>

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

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

  </div>

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



  <input type="submit" class="c-btn-submit-charge" value="Submit Payment">

  </div>
<% end %>

<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>

任何人都知道怎么了?我现在已经在这工作了几个小时,但我没有取得任何进展。

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为stripe.js无法找到付款方式。 解决方案是

= form_tag charges_path, id: 'payment-form' do

你应该这样做:

= form_tag charges_path do
   #payment-form