diff --git a/lib/alavetelitheme.rb b/lib/alavetelitheme.rb index b071ba62..f49cadf0 100644 --- a/lib/alavetelitheme.rb +++ b/lib/alavetelitheme.rb @@ -55,6 +55,7 @@ def prepend_theme_assets 'mailer_patches.rb', 'analytics_event.rb', 'help_page_history.rb', + 'pro_account_bans.rb', 'public_body_questions.rb', 'school_late_calculator.rb', 'volunteer_contact_form.rb', diff --git a/lib/model_patches.rb b/lib/model_patches.rb index cbb0d078..5ecdb75d 100644 --- a/lib/model_patches.rb +++ b/lib/model_patches.rb @@ -69,6 +69,10 @@ def late_calculator ] } + ProAccount.class_eval do + prepend ProAccountBans::ModelMethods + end + PublicBody.class_eval do # Return the domain part of an email address, canonicalised and with common # extra UK Government server name parts removed. diff --git a/lib/pro_account_bans.rb b/lib/pro_account_bans.rb new file mode 100644 index 00000000..0ed02fcc --- /dev/null +++ b/lib/pro_account_bans.rb @@ -0,0 +1,31 @@ +module ProAccountBans + module ModelMethods + PRO_ACCOUNT_BANS_CONFIG = Rails.root.join('config/pro_account_bans.yml') + + def update_source + return super unless pro_account_banned? + + raise ProAccount::CardError, _("The card issuer couldn't authorize " \ + "payment.") + end + + def pro_account_banned? + return false unless bans_config + + bans_config.any? do |ban| + ban.all? do |k, banned_value| + value = @token.card[k] + value.upcase.gsub(/\S/, '') if k == 'address_key' + value == banned_value + end + end + end + + private + + def bans_config + return unless File.exist?(PRO_ACCOUNT_BANS_CONFIG) + YAML.load_file(PRO_ACCOUNT_BANS_CONFIG) + end + end +end