diff --git a/lib/registration.rb b/lib/registration.rb index 00bbc2fe08004929621bb0039ca6d1c0868ba1c6..a5cc16ce3489f95a698e6256eee7959c2d4c0082 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -229,16 +229,27 @@ class Registration class CreditCard Payment.kinds[:credit_card] = ->(*args, **kw) { self.for(*args, **kw) } - def self.for(customer, tel, finish: Finish, **) - customer.payment_methods.then do |payment_methods| + def self.for(in_customer, tel, finish: Finish, **) + reload_customer(in_customer).then do |(customer, payment_methods)| + if customer.balance >= CONFIG[:activation_amount_accept] + next BillPlan.new(customer, tel, finish: finish) + end + if (method = payment_methods.default_payment_method) - Activate.new(customer, method, tel, finish: finish) - else - new(customer, tel, finish: finish) + next Activate.new(customer, method, tel, finish: finish) end + + new(customer, tel, finish: finish) end end + def self.reload_customer(customer) + EMPromise.all([ + Command.execution.customer_repo.find(customer.customer_id), + customer.payment_methods + ]) + end + def initialize(customer, tel, finish: Finish) @customer = customer @tel = tel @@ -250,7 +261,7 @@ class Registration oob.url = CONFIG[:credit_card_url].call( reply.to.stripped.to_s.gsub("\\", "%5C"), @customer.customer_id - ) + ) + "&amount=#{CONFIG[:activation_amount]}" oob.desc = "Add credit card, save, then next here to continue" oob end @@ -299,9 +310,6 @@ class Registration "is a US card that does not support international " \ "transactions, as JMP is not based in the USA, though " \ "we do support transactions in USD.\n\n" \ - "If you were trying a prepaid card, you may wish to use "\ - "Privacy.com instead, as they do support international " \ - "transactions.\n\n " \ "You may add another card" def decline_oob(reply) @@ -309,7 +317,7 @@ class Registration oob.url = CONFIG[:credit_card_url].call( reply.to.stripped.to_s.gsub("\\", "%5C"), @customer.customer_id - ) + ) + "&amount=#{CONFIG[:activation_amount]}" oob.desc = DECLINE_MESSAGE oob end diff --git a/test/test_helper.rb b/test/test_helper.rb index 94ca984f6e85b5ce8312100758234d5402a70470..6d479f2815f6b7dbf911e65dff121939db02aa5c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -97,7 +97,7 @@ CONFIG = { realm: "sip.example.com", app: "sipappid" }, - credit_card_url: ->(*) { "http://creditcard.example.com" }, + credit_card_url: ->(*) { "http://creditcard.example.com?" }, electrum_notify_url: ->(*) { "http://notify.example.com" }, keep_area_codes: ["556"], keep_area_codes_in: { diff --git a/test/test_registration.rb b/test/test_registration.rb index b2455957c8a7f299d22d2c2004a2c2c4ade0f708..e947478089ba172506ceb0e9c2dd174bc97ed997 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -285,11 +285,14 @@ class RegistrationTest < Minitest::Test { var: "activation_method", value: "credit_card" }, { var: "plan_name", value: "test_usd" } ] - result = Registration::Payment.for( - iq, - cust, - "+15555550000" - ).sync + result = execute_command do + Command.execution.customer_repo.expect(:find, cust, ["test"]) + Registration::Payment.for( + iq, + cust, + "" + ).sync + end assert_kind_of Registration::Payment::CreditCard, result assert_mock cust end @@ -377,21 +380,41 @@ class RegistrationTest < Minitest::Test end def test_for - customer = Minitest::Mock.new(customer) - customer.expect( + cust = Minitest::Mock.new(customer) + cust.expect( :payment_methods, EMPromise.resolve(OpenStruct.new(default_payment_method: :test)) ) - assert_kind_of( - Registration::Payment::CreditCard::Activate, - Registration::Payment::CreditCard.for( - customer, - "+15555550000" - ).sync - ) + execute_command do + Command.execution.customer_repo.expect(:find, cust, ["test"]) + assert_kind_of( + Registration::Payment::CreditCard::Activate, + Registration::Payment::CreditCard.for( + cust, + "+15555550000" + ).sync + ) + end end em :test_for + def test_for_has_balance + cust = Minitest::Mock.new(customer) + cust.expect(:balance, 100) + cust.expect(:payment_methods, EMPromise.resolve(nil)) + execute_command do + Command.execution.customer_repo.expect(:find, cust, ["test"]) + assert_kind_of( + Registration::BillPlan, + Registration::Payment::CreditCard.for( + cust, + "+15555550000" + ).sync + ) + end + end + em :test_for_has_balance + def test_write result = execute_command do Command::COMMAND_MANAGER.expect( @@ -401,7 +424,7 @@ class RegistrationTest < Minitest::Test assert_equal [:execute, :next, :prev], reply.allowed_actions assert_equal( "Add credit card, save, then next here to continue: " \ - "http://creditcard.example.com", + "http://creditcard.example.com?&amount=1", reply.note.content ) end] @@ -505,7 +528,7 @@ class RegistrationTest < Minitest::Test [Matching.new do |reply| assert_equal :error, reply.note_type assert_equal( - "#{msg}: http://creditcard.example.com", + "#{msg}: http://creditcard.example.com?&amount=1", reply.note.content ) end]