Skip to content

Commit

Permalink
Change RubyMoney bank to currencylayer (#10520)
Browse files Browse the repository at this point in the history
* Change RubyMoney bank to currencylayer

* Review changes

* Adding dummy API key in env

* Adding currencylayer only for production

* Removed unwanted appsecret

* Remove env entries
  • Loading branch information
danieljames-dj authored Feb 3, 2025
1 parent 64d379f commit 508edad
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ gem 'time_will_tell', github: 'thewca/time_will_tell'
gem 'redcarpet'
gem 'bootstrap-table-rails'
gem 'money-rails'
gem 'money-currencylayer-bank'
gem 'octokit'
gem 'stripe'
gem 'oauth2'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ GEM
money (~> 6.12)
money (6.16.0)
i18n (>= 0.6.4, <= 2)
money-currencylayer-bank (0.7.2)
json (>= 1.8)
monetize (~> 1.4)
money (~> 6.7)
money-rails (1.15.0)
activesupport (>= 3.0)
monetize (~> 1.9)
Expand Down Expand Up @@ -888,6 +892,7 @@ DEPENDENCIES
mail_form
mini_magick
momentjs-rails!
money-currencylayer-bank
money-rails
mysql2
newrelic_rpm
Expand Down
1 change: 1 addition & 0 deletions app_secrets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def vault_file(secret_name, file_path, refresh: true)
vault :TNOODLE_PUBLIC_KEY
vault :WRC_WEBHOOK_USERNAME
vault :WRC_WEBHOOK_PASSWORD
vault :CURRENCY_LAYER_API_KEY

# To allow logging in to staging with your prod account
unless EnvConfig.WCA_LIVE_SITE?
Expand Down
25 changes: 23 additions & 2 deletions config/initializers/ruby_money.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
# frozen_string_literal: true

require 'money/bank/currencylayer_bank'

Money.locale_backend = :i18n

eu_bank = EuCentralBank.new
Money.default_bank = eu_bank
if Rails.env.production? && EnvConfig.WCA_LIVE_SITE?
mclb = Money::Bank::CurrencylayerBank.new
mclb.access_key = AppSecrets.CURRENCY_LAYER_API_KEY
mclb.currencylayer = true
mclb.ttl_in_seconds = 86_400
mclb.cache = proc do |payload|
key = 'money:currencylayer_bank'
if payload
Rails.cache.write(key, payload)
else
Rails.cache.read(key)
end
end
mclb.update_rates
Money.default_bank = mclb
else
eu_bank = EuCentralBank.new
eu_bank.update_rates
Money.default_bank = eu_bank
end

Money.default_currency = Money::Currency.new("USD")
Money.rounding_mode = BigDecimal::ROUND_HALF_UP
7 changes: 0 additions & 7 deletions lib/dues_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# frozen_string_literal: true

module DuesCalculator
def self.update_exchange_rates_if_needed
if !Money.default_bank.rates_updated_at || Money.default_bank.rates_updated_at < 1.day.ago
Money.default_bank.update_rates
end
end

def self.dues_for_n_competitors(country_iso2, base_entry_fee_lowest_denomination, currency_code, n)
dues_per_competitor_in_usd_money = dues_per_competitor_in_usd(country_iso2, base_entry_fee_lowest_denomination, currency_code)
if dues_per_competitor_in_usd_money.present?
Expand All @@ -21,7 +15,6 @@ def self.dues_for_n_competitors(country_iso2, base_entry_fee_lowest_denomination
def self.dues_per_competitor_in_usd(country_iso2, base_entry_fee_lowest_denomination, currency_code)
country_band = CountryBand.find_by(iso2: country_iso2)&.number

DuesCalculator.update_exchange_rates_if_needed
input_money_us_dollars = Money.new(base_entry_fee_lowest_denomination, currency_code).exchange_to("USD")

registration_fee_dues_us_dollars = input_money_us_dollars * CountryBand.percent_registration_fee_used_for_due_amount(country_band)
Expand Down

0 comments on commit 508edad

Please sign in to comment.