Set Stripe dynamically instead of at boot (#9862)

* Set Stripe dynamically instead of at boot

* Add stripe SiteConfig

* Fix capitalization
This commit is contained in:
Ben Halpern 2020-08-19 21:24:19 -04:00 committed by GitHub
parent adc9c2ab3c
commit e76f6cbbba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 50 additions and 14 deletions

View file

@ -133,11 +133,6 @@ export SLACK_CHANNEL=""
export SLACK_DEPLOY_CHANNEL=""
export SLACK_WEBHOOK_URL=""
# Stripe for payment system
# (https://stripe.com/docs/api)
export STRIPE_PUBLISHABLE_KEY="Optional"
export STRIPE_SECRET_KEY="Optional"
# For video calling in DEV Connect
# (https://www.twilio.com/docs/video)
export TWILIO_ACCOUNT_SID="Optional"

View file

@ -32,6 +32,8 @@ module Admin
twitter_hashtag
shop_url
payment_pointer
stripe_api_key
stripe_publishable_key
health_check_token
feed_style
default_font

View file

@ -126,4 +126,8 @@ class ApplicationController < ActionController::Base
def api_action?
self.class.to_s.start_with?("Api::")
end
def initialize_stripe
Stripe.api_key = SiteConfig.stripe_api_key
end
end

View file

@ -1,5 +1,6 @@
class CreditsController < ApplicationController
before_action :authenticate_user!
before_action :initialize_stripe
def index
@user_unspent_credits_count = current_user.credits.unspent.size

View file

@ -1,5 +1,6 @@
class StripeActiveCardsController < ApplicationController
before_action :authenticate_user!
before_action :initialize_stripe
AUDIT_LOG_CATEGORY = "user.credit_card.edit".freeze
private_constant :AUDIT_LOG_CATEGORY

View file

@ -7,6 +7,7 @@ class UsersController < ApplicationController
after_action :verify_authorized, except: %i[index signout_confirm add_org_admin remove_org_admin remove_from_org]
before_action :authenticate_user!, only: %i[onboarding_update onboarding_checkbox_update]
before_action :set_suggested_users, only: %i[index]
before_action :initialize_stripe, only: %i[edit]
INDEX_ATTRIBUTES_FOR_SERIALIZATION = %i[id name username summary profile_image].freeze
private_constant :INDEX_ATTRIBUTES_FOR_SERIALIZATION

View file

@ -139,6 +139,16 @@ module Constants
"See: https://github.com/thepracticaldev/dev.to/pull/6345",
placeholder: "$pay.somethinglikethis.co/value"
},
stripe_api_key: {
description: "Secret Stripe key for receiving payments. " \
"See: https://stripe.com/docs/keys",
placeholder: "sk_live_...."
},
stripe_publishable_key: {
description: "Public Stripe key for receiving payments. " \
"See: https://stripe.com/docs/keys",
placeholder: "pk_live_...."
},
mailchimp_newsletter_id: {
description: "Main Newsletter ID",
placeholder: ""

View file

@ -80,7 +80,9 @@ class SiteConfig < RailsSettings::Base
}
# Monetization
field :payment_pointer, type: :string # Experimental
field :payment_pointer, type: :string
field :stripe_api_key, type: :string, default: ApplicationConfig["STRIPE_SECRET_KEY"]
field :stripe_publishable_key, type: :string, default: ApplicationConfig["STRIPE_PUBLISHABLE_KEY"]
field :shop_url, type: :string
# Newsletter

View file

@ -611,6 +611,24 @@
<div class="alert alert-info"><%= Constants::SiteConfig::DETAILS[:shop_url][:description] %></div>
</div>
<div class="form-group">
<%= admin_config_label :stripe_api_key %>
<%= f.text_field :stripe_api_key,
class: "form-control",
value: SiteConfig.stripe_api_key,
placeholder: Constants::SiteConfig::DETAILS[:stripe_api_key][:placeholder] %>
<div class="alert alert-info"><%= Constants::SiteConfig::DETAILS[:stripe_api_key][:description] %></div>
</div>
<div class="form-group">
<%= admin_config_label :stripe_publishable_key %>
<%= f.text_field :stripe_publishable_key,
class: "form-control",
value: SiteConfig.stripe_publishable_key,
placeholder: Constants::SiteConfig::DETAILS[:stripe_publishable_key][:placeholder] %>
<div class="alert alert-info"><%= Constants::SiteConfig::DETAILS[:stripe_publishable_key][:description] %></div>
</div>
<div class="form-group">
<%= admin_config_label :payment_pointer %>
<%= f.text_field :payment_pointer,

View file

@ -144,7 +144,7 @@
</div>
<script>
var stripe = Stripe('<%= ApplicationConfig["STRIPE_PUBLISHABLE_KEY"] %>');
var stripe = Stripe('<%= SiteConfig.stripe_publishable_key %>');
var elements = stripe.elements();
<% if current_user.decorate.dark_theme? %>

View file

@ -46,7 +46,7 @@
<script>
setTimeout(function () {
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
key: '<%= SiteConfig.stripe_publishable_key %>',
image: "https://thepracticaldev.s3.amazonaws.com/i/xoxvppfjorrnxudkzskw.jpg",
locale: 'auto',
token: function (token) {

View file

@ -1,9 +1,3 @@
Rails.configuration.stripe = {
publishable_key: ApplicationConfig["STRIPE_PUBLISHABLE_KEY"]
}
Stripe.api_key = ApplicationConfig["STRIPE_SECRET_KEY"]
if Rails.env.development? && Stripe.api_key.present?
Stripe.log_level = Stripe::LEVEL_INFO
end

View file

@ -290,6 +290,14 @@ RSpec.describe "/admin/config", type: :request do
expect(SiteConfig.payment_pointer).to eq("$pay.yo")
end
it "updates stripe configs" do
post "/admin/config", params: { site_config: { stripe_api_key: "sk_live_yo",
stripe_publishable_key: "pk_live_haha" },
confirmation: confirmation_message }
expect(SiteConfig.stripe_api_key).to eq("sk_live_yo")
expect(SiteConfig.stripe_publishable_key).to eq("pk_live_haha")
end
describe "Shop" do
it "rejects update to shop_url without proper confirmation" do
expected_shop_url = "https://qshop.dev.to"