Skip to content

Commit

Permalink
fixup! Replace Stripe Plan API with the Prices API
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Oct 22, 2024
1 parent a33dad6 commit c314f96
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
6 changes: 4 additions & 2 deletions app/controllers/concerns/alaveteli_pro/stripe_namespace.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module AlaveteliPro::StripeNamespace
extend ActiveSupport::Concern

def add_stripe_namespace(string)
def add_stripe_namespace(string, prefix: nil)
return string if namespace.blank?
return string if prefix && string.start_with?(/#{prefix}_/)
return string if string.start_with?(/#{namespace}-/)

[namespace, string].join('-')
end

def remove_stripe_namespace(string)
def remove_stripe_namespace(string, prefix: nil)
return string if namespace.blank?
return string if prefix && string.start_with?(/#{prefix}_/)
return string unless string.start_with?(/#{namespace}-/)

string.sub(/^#{namespace}-/, '')
Expand Down
15 changes: 8 additions & 7 deletions app/models/alaveteli_pro/price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ class AlaveteliPro::Price < SimpleDelegator
tax :unit_amount

def self.list
AlaveteliConfiguration.stripe_price_ids.inject([]) do |arr, id|
price = retrieve(id)
arr << price if price
AlaveteliConfiguration.stripe_prices.inject([]) do |arr, (_, id)|
arr << retrieve(id)
arr
end
end

def self.retrieve(id)
new(Stripe::Price.retrieve(add_stripe_namespace(id)))
rescue Stripe::InvalidRequestError
nil
new(Stripe::Price.retrieve(add_stripe_namespace(id, prefix: 'price')))
end

def to_param
remove_stripe_namespace(id)
AlaveteliConfiguration.stripe_prices[id]
end

def ===(other)
self.id == other.id
end

# product
Expand Down
16 changes: 8 additions & 8 deletions config/general.yml-example
Original file line number Diff line number Diff line change
Expand Up @@ -1202,20 +1202,20 @@ STRIPE_SECRET_KEY: ''
# ---
STRIPE_NAMESPACE: ''

# List of Stripe Subscription Prices IDs which if a user signs up to will grant
# them access to Alaveteli Pro. Rendered on the Pro pricing pages in the ordered
# defined here.
# List of Stripe Prices which if a user signs up to will grant them access to
# Alaveteli Pro. Rendered on the Pro pricing pages in the order defined here.
#
# STRIPE_PRICE_IDS - Array Stripe Price IDs
# STRIPE_PRICES - Hash of objects with key equla to the Stripe Price IDs as the
# value equal to parameterise short human readable string
#
# Examples:
#
# STRIPE_PRICE_IDS:
# - pro
# - pro-annual-billing
# STRIPE_PRICES:
# price_123: pro
# price:456: pro-annual-billing
#
# ---
STRIPE_PRICE_IDS: []
STRIPE_PRICES: []

# Stripe.com webhook secret. Only required for Alaveteli Pro.
#
Expand Down
10 changes: 8 additions & 2 deletions lib/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module AlaveteliConfiguration
SMTP_MAILER_PORT: 25,
SMTP_MAILER_USER_NAME: '',
STRIPE_NAMESPACE: '',
STRIPE_PRICE_IDS: [],
STRIPE_PRICES: {},
STRIPE_PUBLISHABLE_KEY: '',
STRIPE_SECRET_KEY: '',
STRIPE_TAX_RATE: '0.20',
Expand Down Expand Up @@ -146,7 +146,13 @@ def self.get(key, default)
def self.method_missing(name)
key = name.to_s.upcase
if DEFAULTS.key?(key.to_sym)
get(key, DEFAULTS[key.to_sym])
value = get(key, DEFAULTS[key.to_sym])
case value
when Hash
value.with_indifferent_access
else
value
end
else
super
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/alaveteli_pro/plans_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
let(:product) { stripe_helper.create_product }

before do
allow(AlaveteliConfiguration).to receive(:stripe_price_ids).
and_return(['pro'])
allow(AlaveteliConfiguration).to receive(:stripe_prices).
and_return(pro: { enabled: true })
end

let!(:pro_price) do
Expand Down
21 changes: 16 additions & 5 deletions spec/models/alaveteli_pro/price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@
describe '.list' do
before { described_class.instance_variable_set(:@list, nil) }

let(:price_ids) { %w[price_1 price_2 price_3] }
let(:price_1) { double('AlaveteliPro::Price') }
let(:price_2) { double('AlaveteliPro::Price') }
let(:prices) do
{
price_1: { id: 'price_1', enabled: true },
price_2: { enabled: true },
price_3: { id: 'price_3', enabled: false }, # not enabled
price_4: { enabled: true } # does not exist
}
end

let(:price_1) { AlaveteliPro::Price.new(double('Stripe::Price')) }
let(:price_2) { AlaveteliPro::Price.new(double('Stripe::Price')) }
let(:price_3) { AlaveteliPro::Price.new(double('Stripe::Price')) }

before do
allow(AlaveteliConfiguration).to receive(:stripe_price_ids).
and_return(price_ids)
allow(AlaveteliConfiguration).to receive(:stripe_prices).
and_return(prices)
allow(described_class).to receive(:retrieve).with('price_1').
and_return(price_1)
allow(described_class).to receive(:retrieve).with('price_2').
and_return(price_2)
allow(described_class).to receive(:retrieve).with('price_3').
and_return(price_3)
allow(described_class).to receive(:retrieve).with('price_4').
and_return(nil)
end

Expand Down

0 comments on commit c314f96

Please sign in to comment.