Ruby on rails Stripe“此API调用不能使用可发布的API密钥进行。”给出了什么?

Ruby on rails Stripe“此API调用不能使用可发布的API密钥进行。”给出了什么?,ruby-on-rails,stripe-payments,Ruby On Rails,Stripe Payments,我在stripe和rails集成期间遇到了这个错误,我不知道为什么 我的费用控制员: class ChargesController < ApplicationController before_action :check_if_user_already_subscribed, only: [:new] def new end def create # Amount in cents @amount = 100 # Crea

我在stripe和rails集成期间遇到了这个错误,我不知道为什么

我的费用控制员:

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
我使用的是figaro,因此configuration.yml我替换了实际的键值:

PUBLISHABLE_KEY: "sk_test_key"
SECRET_KEY: "pk_test_key"
我的看法是:

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

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

<div id= 'charges-new-promo'>
<div class = 'container-fluid'>

  <div class = 'index-header'>
    <h2 class ='text-center charge-title'>Upgrade Now.</h2>
   <p class = ' text-center charge-sub-title'>Instantly unlock the entire site for 90 days.</p>
   <p class = ' text center charge-stripe-title'><%= image_tag("stripe.png", :class => "stripe-img" ) %></p>
  </div>


</div>
</div>

<div class = 'container'>
<div class = 'row'>
<div class = 'col-md-5 col-sm-12'>
<%= 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 %>

</div>

<div class ='col-md-4 col-sm-12'>
  <div class = 'payment-box'>
  <p> Information about your order:</p>
  <ul>
    <li>You are subscribing to the full membership.</li>
    <li>This subscription lasts for 3 months (90 days).</li>
    <li>Do not subscribe again if you are already subscribed and your expiry date has not been passed.</li>
    <li>Subscribing grants you access to every test and practice material on this website.</li>
    <li>At the moment, this consists of 19 online DAT reading comprehension tests along with answer explanations.</li>
    <li>Any additional premium material added (such as more tests or more DAT sections) after you subscribe will be automatically available to you with
    no extra charge.</li>


  </ul>

  <div class = 'inner-box'>
    <p class = 'text-center'> Total cost: $20 </p>


  </div>

  <div class ='inner-box-payment'>


  </div>



</div>



</div>

</div>
</div>

<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>
就这样。我现在完全迷路了。出了什么问题

我使用的是figaro,所以configuration.yml我已经替换了实际的键 价值观:

可发布密钥:sk_测试密钥

密钥:pk\U测试密钥

设置的键值不正确

是:

必须:


p.s sk_test_key-sk->secret_key

您是否更改了密钥值

看起来这个答案应该能解决你的问题

因此,在您的yml中,您应该:

PUBLISHABLE_KEY: "pk_test_key"
SECRET_KEY: "sk_test_key"

Pk_密钥指的是可发布的密钥,而sk_密钥指的是密钥。

您在这里混淆了API密钥。客户端使用的可发布密钥以pk_开头,服务器端使用的密钥以sk_开头。你做了相反的事是我的错我修正了10分钟后我发布了问题。但问题仍然存在。确切的错误是关于API键的,所以我100%确定您还没有完全修复它。只需添加日志以输出您正在设置的密钥,您就会发现它是错误的。请注意上面的内容。所以答案不是按时间顺序排列的。考虑一个对回答或使用Primink的人的回答。你完全正确,我编辑了我的答案。非常感谢。
PUBLISHABLE_KEY: "sk_test_key"

SECRET_KEY: "pk_test_key"
PUBLISHABLE_KEY: "pk_test_key"

SECRET_KEY: "sk_test_key"
PUBLISHABLE_KEY: "pk_test_key"
SECRET_KEY: "sk_test_key"