Skip to content

Commit

Permalink
Merge pull request #3900 from 3scale/ignore-gateway-init-error-on-buy…
Browse files Browse the repository at this point in the history
…er-destroy

THREESCALE-11383: Ignore errors on payment gateway initialization when destroying buyer
  • Loading branch information
mayorova authored Oct 7, 2024
2 parents c0ec886 + a6719ba commit 07d54df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/models/account/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ def delete_cc_details
end

def unstore_credit_card!
response = provider_payment_gateway.try!(:threescale_unstore, credit_card_auth_code)
log_gateway_response(response, "unstore [auth: #{credit_card_auth_code}]")
begin
response = provider_payment_gateway.try!(:threescale_unstore, credit_card_auth_code)
log_gateway_response(response, "unstore [auth: #{credit_card_auth_code}]")
rescue Account::Gateway::InvalidSettingsError => error
Rails.logger.warn("Couldn't unstore credit card details from the payment gateway: #{error.message}")
end

return if payment_detail.destroyed?

Expand Down
4 changes: 4 additions & 0 deletions app/models/account/gateway.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Account::Gateway
extend ActiveSupport::Concern

class InvalidSettingsError < StandardError; end

included do
has_many :payment_transactions
has_one :payment_gateway_setting, dependent: :destroy, inverse_of: :account
Expand All @@ -18,6 +20,8 @@ def payment_gateway(**options)
return if payment_gateway_type.blank?

PaymentGateway.implementation(payment_gateway_type, **options).new(payment_gateway_options || {})
rescue ArgumentError => exception
raise InvalidSettingsError, exception.message
end

def payment_gateway_configured?
Expand Down
18 changes: 18 additions & 0 deletions test/unit/account/credit_card_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ class Account::CreditCardTest < ActiveSupport::TestCase
assert_nil provider.credit_card_partial_number
end

test 'unstore credit card succeeds if gateway settings are invalid' do
provider = FactoryBot.create(:simple_account, payment_gateway_type: :bogus)
buyer = FactoryBot.create(:simple_buyer, provider_account: provider,
credit_card_auth_code: "fdsa",
credit_card_expires_on: Date.new(2020, 4, 2),
credit_card_partial_number: "0989")

ActiveMerchant::Billing::BogusGateway.expects(:new).raises(ArgumentError.new("Missing required parameter: whatever"))

assert_nothing_raised do
buyer.unstore_credit_card!
end

assert_nil buyer.credit_card_auth_code
assert_equal Time.zone.today.change(:day => 1), buyer.credit_card_expires_on_with_default
assert_nil buyer.credit_card_partial_number
end

test '#credit_card_editable? returns false for master account' do
refute master_account.credit_card_editable?
end
Expand Down

0 comments on commit 07d54df

Please sign in to comment.