Skip to content

Commit

Permalink
Change RubyMoney bank to currencylayer
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljames-dj committed Jan 5, 2025
1 parent a7dfce9 commit d9cffea
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ AVATARS_PUBLIC_STORAGE=local
AVATARS_PRIVATE_STORAGE=local_private
DUMP_HOST=https://assets.worldcubeassociation.org
CRONJOB_POLLING_SECONDS=0
CURRENCY_LAYER_API_KEY=9d254ec37e1591eec964dac947fafa09
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ STAGING_OAUTH_SECRET=example-secret
AVATARS_PUBLIC_STORAGE=local
AVATARS_PRIVATE_STORAGE=local_private
DUMP_HOST=https://assets.worldcubeassociation.org
CURRENCY_LAYER_API_KEY=9d254ec37e1591eec964dac947fafa09
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 @@ -462,6 +462,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 @@ -884,6 +888,7 @@ DEPENDENCIES
mail_form
mini_magick
momentjs-rails!
money-currencylayer-bank
money-rails
mysql2
newrelic_rpm
Expand Down
2 changes: 2 additions & 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 All @@ -106,6 +107,7 @@ def vault_file(secret_name, file_path, refresh: true)
mandatory :OIDC_SECRET_KEY, :string
mandatory :STAGING_OAUTH_CLIENT, :string
mandatory :STAGING_OAUTH_SECRET, :string
mandatory :CURRENCY_LAYER_API_KEY, :string

optional :AWS_ACCESS_KEY_ID, :string, ''
optional :AWS_SECRET_ACCESS_KEY, :string, ''
Expand Down
13 changes: 11 additions & 2 deletions config/initializers/ruby_money.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# frozen_string_literal: true

require 'money/bank/currencylayer_bank'

Money.locale_backend = :i18n

eu_bank = EuCentralBank.new
Money.default_bank = eu_bank
mclb = Money::Bank::CurrencylayerBank.new
mclb.access_key = AppSecrets.CURRENCY_LAYER_API_KEY
mclb.currencylayer = true
mclb.ttl_in_seconds = 86_400
mclb.cache = :redis_cache_store, { url: EnvConfig.CACHE_REDIS_URL }

mclb.update_rates

Money.default_bank = mclb
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 d9cffea

Please sign in to comment.