最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Stripe w Ruby on Rails ENV variables in Javascript - Stack Overflow

programmeradmin0浏览0评论

I am not sure how to place my Publishable key into my JavaScript code. When I place the publishable key's value directly into the JavaScript it works fine. When I try to use Environment variables It does not work.

config/initializers/stripe.rb

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

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

javascripts/charges.js.erb

Stripe.setPublishableKey(<%= Rails.configuration.stripe[:publishable_key] %>);

var stripeResponseHandler = function(status, response) {
  var $form = $('#payment-form');

  if (response.error) {

        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
    } else {

        var token = response.id;

        $form.append($('<input type="hidden" name="stripeToken" />').val(token));

        $form.get(0).submit();
    }
};

jQuery(function($) {
    $('#payment-form').submit(function(e) {
      var $form = $(this);


      $form.find('button').prop('disabled', true);

      Stripe.createToken($form, stripeResponseHandler);

      return false;
    });
});

I am not sure how to place my Publishable key into my JavaScript code. When I place the publishable key's value directly into the JavaScript it works fine. When I try to use Environment variables It does not work.

config/initializers/stripe.rb

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

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

javascripts/charges.js.erb

Stripe.setPublishableKey(<%= Rails.configuration.stripe[:publishable_key] %>);

var stripeResponseHandler = function(status, response) {
  var $form = $('#payment-form');

  if (response.error) {

        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
    } else {

        var token = response.id;

        $form.append($('<input type="hidden" name="stripeToken" />').val(token));

        $form.get(0).submit();
    }
};

jQuery(function($) {
    $('#payment-form').submit(function(e) {
      var $form = $(this);


      $form.find('button').prop('disabled', true);

      Stripe.createToken($form, stripeResponseHandler);

      return false;
    });
});
Share Improve this question edited Nov 10, 2013 at 21:55 mu is too short 435k71 gold badges858 silver badges818 bronze badges asked Nov 10, 2013 at 21:38 TonyTauTonyTau 911 silver badge4 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

Not exactly a solution but I like the Railscast way (http://railscasts./episodes/288-billing-with-stripe) of setting the meta tag with an Environment variable and then using Javascript to call upon the value in the meta tag. The bit you want starts at about 4m10s

<%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>

Then the JS code is:

Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))

I'm not a fan of inserting environment variables into JS directly.

What you have looks like it should work. When you start your server are you entering in the proper keys?

PUBLISHABLE_KEY=pk_####_################ SECRET_KEY=sk_####_################# rails s

Although I don't think storing Stripe details in Javascript is wise, you can use a gem called gon to load variables into JS:

#app/controllers/your_controller.rb
gon.push({
  :user_id => 1,
  :user_role => "admin"
})

#app/assets/javascripts/your_javascript.js
gon.variable_name

You must add this to your layout file:

<head>
  <title>some title</title>
  <%= include_gon %>
  <!-- include your action js code -->
  ...

To load ENV variables even in development, I would then use a gem called Figaro which basically allows you load ENV variables in any state:

#app/config/application.yml
YOUR_ENV_VAR: 'information'
发布评论

评论列表(0)

  1. 暂无评论