From aaef462433547a695402d0648ab9bd675fe10930 Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Thu, 2 Apr 2026 19:24:38 -0400 Subject: [PATCH] clear low balance key on successful auto top-up --- lib/low_balance.rb | 26 +++++++++++++++++++------- test/test_low_balance.rb | 18 ++++++++++++++++++ test/test_web.rb | 6 ++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/low_balance.rb b/lib/low_balance.rb index 7e5af3ac7a91326fef6bed23576ac9c476e05c72..89735cb5d2a6eefde42a109b4dbdfa9d3e88ed77 100644 --- a/lib/low_balance.rb +++ b/lib/low_balance.rb @@ -137,6 +137,20 @@ class LowBalance ) end + # @param e [StandardError] + # @return [String] + def failure_msg(e) + "Automatic top-up transaction for " \ + "$#{'%.2f' % top_up_amount} failed: #{e.message}" + end + + # @param tx [Transaction] + # @return [String] + def success_msg(tx) + "Automatic top-up has charged your default " \ + "payment method and added #{tx} to your balance." + end + def failed(e) churnbuster(e) @method && REDIS.setex( @@ -144,18 +158,16 @@ class LowBalance 60 * 60 * 24 * 30, Time.now ) - @message.body = - "Automatic top-up transaction for " \ - "$#{'%.2f' % top_up_amount} failed: #{e.message}" + @message.body = failure_msg(e) 0 end def notify! sale.then { |tx| - @message.body = - "Automatic top-up has charged your default " \ - "payment method and added #{tx} to your balance." - tx.total + @message.body = success_msg(tx) + REDIS.del( + "jmp_customer_low_balance-#{@customer.billing_customer_id}" + ).then { tx.total } }.catch(&method(:failed)).then { |amount| @customer.stanza_to(@message) amount diff --git a/test/test_low_balance.rb b/test/test_low_balance.rb index 5672077117e951fad53f07a5b947e415dce75630..4e9047b8d6b492c987c8fec6e9c82306d63bb8b9 100644 --- a/test/test_low_balance.rb +++ b/test/test_low_balance.rb @@ -331,5 +331,23 @@ class LowBalanceTest < Minitest::Test assert_requested req end em :test_decline_notify! + + def test_auto_topup_clears_low_balance_key + tx = OpenStruct.new(total: 13) + LowBalance::AutoTopUp::CreditCardSale.expect( + :create, + EMPromise.resolve(tx), + [@customer], amount: 100 + ) + LowBalance::AutoTopUp::REDIS.expect( + :del, + EMPromise.resolve(100), + ["jmp_customer_low_balance-test"] + ) + @customer.expect(:stanza_to, nil, [Blather::Stanza::Message]) + @auto_top_up.notify!.sync + assert_mock LowBalance::AutoTopUp::REDIS + end + em :test_auto_topup_clears_low_balance_key end end diff --git a/test/test_web.rb b/test/test_web.rb index ad8f228bf62647f9eeaf8580bee67b34a305815c..fb6e85f713b4902602743b9d14694665cb027c27 100644 --- a/test/test_web.rb +++ b/test/test_web.rb @@ -259,6 +259,11 @@ class WebTest < Minitest::Test 0, ["jmp_auto_top_up_block-abcd"] ) + LowBalance::AutoTopUp::REDIS.expect( + :del, + EMPromise.resolve(1), + ["jmp_customer_low_balance-customerid_topup"] + ) braintree_customer = Minitest::Mock.new CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer) payment_methods = OpenStruct.new(payment_methods: [ @@ -296,6 +301,7 @@ class WebTest < Minitest::Test assert_mock ExpiringLock::REDIS assert_mock Customer::BLATHER assert_mock LowBalance::AutoTopUp::CreditCardSale + assert_mock LowBalance::AutoTopUp::REDIS assert_mock CustomerPlan::DB end em :test_outbound_low_balance_top_up