Stripe :: AuthenticationError未提供API密钥。使用“Stripe.api_key = <api-key>”设置API密钥

时间:2017-01-16 20:12:21

标签: ruby-on-rails ruby api heroku stripe-payments

更新: 我将可发布的密钥直接内联设置为:Stripe.setPublishableKey("pk_live_*****"); 它的工作原理!! 问题是没有使用ENV['stripe_publishable_key']正确配置。

原始邮件:

我正在使用Stroud的koudoku gem。一切都在本地工作正常,但当我推送到heroku时,我收到错误:

Stripe::AuthenticationError (No API key provided. Set your API key using "Stripe.api_key = <API-KEY>".

即使它在开发中设置了可发布的密钥。另外,如果我运行heroku配置,我会在那里看到密钥。

这里发生了什么?为什么不设置密钥??

application.yml

development:
  stripe_api_key: 'sk_test_***'
  stripe_publishable_key: 'pk_test_***'

production:
  stripe_api_key: 'sk_live_***'
  stripe_publishable_key: 'pk_live_***'

配置/初始化/ koudoku.rb

Koudoku.setup do |config|
  config.subscriptions_owned_by = :user
  config.stripe_publishable_key = ENV['stripe_publishable_key']
  config.stripe_secret_key = ENV['stripe_api_key']

  Stripe.api_version = '2015-01-11' #Making sure the API version used is compatible.
  # config.prorate = false # Default is true, set to false to disable prorating subscriptions
  # config.free_trial_length = 30

  # Specify layout you want to use for the subscription pages, default is application
  config.layout = 'application'

  # you can subscribe to additional webhooks here
  # we use stripe_event under the hood and you can subscribe using the 
  # stripe_event syntax on the config object: 
  # config.subscribe 'charge.failed', Koudoku::ChargeFailed

end

_card.html.erb

<div class="padding_page">
  <div class="wrapper_form_dark wrapper_form_sign_on">

<%# content_for :koudoku do %>

<%# end %>

<%= form_for @subscription, url: url, html: {id: 'payment-form', class: 'form-horizontal'} do |f| %>

  <fieldset>
<div class="form_section">
    <legend class="page_title">Update Payment Info</legend>



      <label class="label_standard">Card Number</label>
      <div class="controls">
        <input type="text" size="20" autocomplete="off" class="card-number input_standard"/>
      </div>
    </div>


    <div class="form_section">
      <label class="label_standard">Expiration (MM/YYYY)</label>
      <div class="controls">
        <input type="text" size="2" class="card-expiry-month input_mini"/>
        <span> / </span>
        <input type="text" size="4" class="card-expiry-year input_mini"/>
      </div>
    </div>

    <div class="form_section">
      <label class="label_standard">CVC</label>
      <div class="controls">
        <input type="text" size="4" autocomplete="off" class="card-cvc input_mini"/>
      </div>
    </div>

    <div class="alert alert-error payment-errors red"></div>
    <%= f.hidden_field :plan_id %>

  </fieldset>

  <div class="form_section">
    <div class="actions">
      <% if Koudoku.free_trial? %>
        <button type="submit" class="btn_primary submit-button">Save Billing Information</button>
      <% else %>
        <button type="submit" class="btn_primary_large submit-button">Update Card Information</button>
      <% end %>
      <%= link_to "Cancel", owner_subscriptions_path(@owner), class: 'btn red' %>
    </div>
  </div>

<% end %>
</div>
</div>
<script type="text/javascript">

  // All this code taken from Stripe's own examples at:
  // https://stripe.com/docs/tutorials/forms .

  function stripeResponseHandler(status, response) {

      if (response.error) {
          // show the errors on the form
          $(".payment-errors").text(response.error.message).show();
          $(".submit-button").removeAttr("disabled");
      } else {
          var form$ = $("#payment-form");
          // token contains id, last4, and card type
          // insert the token into the form so it gets submitted to the server
          form$.append("<input type='hidden' name='subscription[credit_card_token]' value='" + response['id'] + "'/>");
          form$.append("<input type='hidden' name='subscription[last_four]' value='" + response['last4'] + "'/>");
          form$.append("<input type='hidden' name='subscription[card_type]' value='" + response['card_type'] + "'/>");
          // and submit
          form$.get(0).submit();
      }
  }

  $(document).ready(function() {



    Stripe.setPublishableKey("<%= Koudoku.stripe_publishable_key %>");

    // By default, don't show errors.
    $(".payment-errors").hide()

    $("#payment-form").submit(function(event) {

      // disable the submit button to prevent repeated clicks
      $('.submit-button').attr("disabled", "disabled");

      Stripe.createToken({
          number: $('.card-number').val(),
          cvc: $('.card-cvc').val(),
          exp_month: $('.card-expiry-month').val(),
          exp_year: $('.card-expiry-year').val()
      }, stripeResponseHandler);

      // prevent the form from submitting with the default action
      return false;
    });
  });

</script>

更新: 我在条带的日志中看到帖子的日志,它的响应为200 ......

{
  "card": {
    "number": "************5458",
    "cvc": "***",
    "exp_month": "11",
    "exp_year": "2019"
  },
  "key": "pk_live_***",
  "payment_user_agent": "stripe.js/81eca10",
  "callback": "sjsonp1484600040115",
  "_method": "POST",
  "_accept_language": "en-US"
}

但该应用程序仍然抛出相同的无密钥错误。希望这有帮助吗?

2 个答案:

答案 0 :(得分:1)

我经常发现需要在终端中运行spring stop,以便我的应用程序能够获取环境变量的更改。

答案 1 :(得分:0)

只是重新启动本地服务器对我有用