ruby-on-rails - Stripe "This API call cannot be made with a publishable API key. "给出了什么?

标签 ruby-on-rails stripe-payments

我在 stripe 和 rails 集成过程中遇到了这个错误,我不知道为什么。

我的收费 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

我的初始化器 stripe.rb:

Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

我正在使用 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>

就是这样。我现在完全迷路了。出了什么问题?

最佳答案

I'm using figaro, so configuration.yml (I've replaced the actual key values):

PUBLISHABLE_KEY: "sk_test_key"

SECRET_KEY: "pk_test_key"

您错误地设置了键值。

是:

PUBLISHABLE_KEY: "sk_test_key"

SECRET_KEY: "pk_test_key"

必须:

PUBLISHABLE_KEY: "pk_test_key"

SECRET_KEY: "sk_test_key"

P.s "sk_test_key"- sk -> secret_key

关于ruby-on-rails - Stripe "This API call cannot be made with a publishable API key. "给出了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39239731/

相关文章:

ruby-on-rails - 究竟是什么保存/保存!做?

ruby-on-rails - rake 中止!加载错误 : no such file to load -- active_record/connection_adapters/postgresql/explain_pretty_printer

stripe-payments - Stripe 卡 token 的有效期是多久?

stripe-payments - 前端 <-> 后端 : Secure Stripe Integration

c# - 使用 .net Framework 4.7.2 而不是 .net core 实现 strip 订阅

html - 有没有办法在 Rails 中的表单上左对齐?

ruby-on-rails - ActiveAdmin 登录页面

javascript - 如何使用 Stripe Payment Intents 提供账单详细信息以确保 SCA 合规性?

ruby-on-rails - 用于 rails 4 的 Stripe webhooks

mysql - Rails i18n API 三重破折号/连字符、省略号和换行符