diff --git a/lib/admin_actions/cancel.rb b/lib/admin_actions/cancel.rb index a45d9f32882d52404e7a80d688dcaece3d31eedf..bffce6d339d53ac27e5fb560ffe38d5b5d4df5bb 100644 --- a/lib/admin_actions/cancel.rb +++ b/lib/admin_actions/cancel.rb @@ -8,9 +8,9 @@ class AdminAction m.body = "Your JMP account has been cancelled." customer.stanza_to(m).then { EMPromise.all([ + Churnbuster.new.cancellation(customer), customer.stanza_to(Blather::Stanza::Iq::IBR.new(:set).tap(&:remove!)), - customer.deregister!, - customer_repo.disconnect_tel(customer) + customer.deregister!, customer_repo.disconnect_tel(customer) ]) } end diff --git a/lib/churnbuster.rb b/lib/churnbuster.rb index 668bca34bebe657b807b3cd7ffdcb677b203fd56..29565b41aed01d5e610b65a0511c651df5858a53 100644 --- a/lib/churnbuster.rb +++ b/lib/churnbuster.rb @@ -26,6 +26,29 @@ class Churnbuster ) end + def successful_payment(customer, amount, txid) + post_json( + "https://api.churnbuster.io/v1/successful_payments", + { + customer: format_customer(customer), + payment: format_tx(customer, amount, txid) + } + ) + end + + def cancellation(customer) + post_json( + "https://api.churnbuster.io/v1/cancellations", + { + customer: format_customer(customer), + subscription: { + source: "braintree", + source_id: customer.customer_id + } + } + ) + end + protected def format_tx(customer, amount, txid) diff --git a/lib/credit_card_sale.rb b/lib/credit_card_sale.rb index 5335ffaa8ec4f7c83f8ddcfc66fcb5fe0b9abd79..1c886774a31668cde8052e60fb8d9ae10aee7ed2 100644 --- a/lib/credit_card_sale.rb +++ b/lib/credit_card_sale.rb @@ -80,12 +80,20 @@ protected end end + def churnbuster_success(response) + Churnbuster.new.successful_payment( + @customer, + @amount, + response.transaction.id + ) + end + def decline_guard(response) if response.success? + churnbuster_success(response) REDIS.setex( "jmp_customer_credit_card_lock-#{@customer.customer_id}", - 60 * 60 * 24, - "1" + 60 * 60 * 24, "1" ) return response.transaction end diff --git a/test/test_admin_command.rb b/test/test_admin_command.rb index 788db4f556672ac81489f13a40c12dd3aef0a5d8..6ddf49eee7acfcd5c777414519c5d279f9eb01c5 100644 --- a/test/test_admin_command.rb +++ b/test/test_admin_command.rb @@ -179,6 +179,23 @@ class AdminCommandTest < Minitest::Test em :test_action_launch_snikket def test_action_cancel_account + req = stub_request(:post, "https://api.churnbuster.io/v1/cancellations") + .with( + body: { + customer: { + source: "braintree", + source_id: "test", + email: "test@smtp.cheogram.com", + properties: {} + }, + subscription: { + source: "braintree", + source_id: "test" + } + }.to_json + ) + .to_return(status: 200, body: "", headers: {}) + sgx, admin = admin_command Customer::BLATHER.expect( @@ -226,10 +243,28 @@ class AdminCommandTest < Minitest::Test assert_mock sgx assert_mock BackendSgx::IQ_MANAGER assert_mock Customer::BLATHER + assert_requested req end em :test_action_cancel_account def test_action_cancel_account_keep_number + req = stub_request(:post, "https://api.churnbuster.io/v1/cancellations") + .with( + body: { + customer: { + source: "braintree", + source_id: "test", + email: "test@smtp.cheogram.com", + properties: {} + }, + subscription: { + source: "braintree", + source_id: "test" + } + }.to_json + ) + .to_return(status: 200, body: "", headers: {}) + sgx, admin = admin_command("+15566667777") Customer::BLATHER.expect( @@ -276,6 +311,7 @@ class AdminCommandTest < Minitest::Test assert_mock sgx assert_mock BackendSgx::IQ_MANAGER assert_mock Customer::BLATHER + assert_requested req end em :test_action_cancel_account_keep_number end diff --git a/test/test_credit_card_sale.rb b/test/test_credit_card_sale.rb index 7a1eea1f3d14423fd53b914c20b6b157498c2dbe..f2e037b5ee2bd6f705472ff1db672eca9ecc3b1f 100644 --- a/test/test_credit_card_sale.rb +++ b/test/test_credit_card_sale.rb @@ -112,6 +112,26 @@ class CreditCardSaleTest < Minitest::Test em :test_sale_locked def test_sale + req = stub_request( + :post, + "https://api.churnbuster.io/v1/successful_payments" + ).with( + body: { + customer: { + source: "braintree", + source_id: "test", + email: "test@smtp.cheogram.com", + properties: {} + }, + payment: { + source: "braintree", + source_id: "transaction", + amount_in_cents: 9900, + currency: "USD" + } + }.to_json + ).to_return(status: 200, body: "", headers: {}) + CreditCardSale::REDIS.expect( :exists, EMPromise.resolve(0), @@ -162,6 +182,7 @@ class CreditCardSaleTest < Minitest::Test assert_mock CreditCardSale::REDIS assert_mock TrustLevelRepo::REDIS assert_mock TrustLevelRepo::DB + assert_requested req end em :test_sale @@ -182,6 +203,26 @@ class CreditCardSaleTest < Minitest::Test end def test_create + req = stub_request( + :post, + "https://api.churnbuster.io/v1/successful_payments" + ).with( + body: { + customer: { + source: "braintree", + source_id: "test", + email: "test@smtp.cheogram.com", + properties: {} + }, + payment: { + source: "braintree", + source_id: "transaction", + amount_in_cents: 9900, + currency: "USD" + } + }.to_json + ).to_return(status: 200, body: "", headers: {}) + CreditCardSale::REDIS.expect( :exists, EMPromise.resolve(0), @@ -253,6 +294,7 @@ class CreditCardSaleTest < Minitest::Test assert_mock CreditCardSale::REDIS assert_mock TrustLevelRepo::REDIS assert_mock TrustLevelRepo::DB + assert_requested req end em :test_create end diff --git a/test/test_low_balance.rb b/test/test_low_balance.rb index 53a05b7a62e372d6ef89bdebd6b3bc45a73e9914..4d296c9b3dace14ce4f2e278ec30aded84526d23 100644 --- a/test/test_low_balance.rb +++ b/test/test_low_balance.rb @@ -244,7 +244,7 @@ class LowBalanceTest < Minitest::Test em :test_border_low_balance_notify! def test_decline_notify! - stub_request(:post, "https://api.churnbuster.io/v1/failed_payments") + req = stub_request(:post, "https://api.churnbuster.io/v1/failed_payments") .with( body: { customer: { @@ -259,11 +259,7 @@ class LowBalanceTest < Minitest::Test amount_in_cents: 10000, currency: "USD" } - }.to_json, - headers: { - "Authorization" => ["", ""], - "Content-Type" => "application/json" - } + }.to_json ).to_return(status: 200, body: "", headers: {}) @customer.expect( @@ -286,6 +282,7 @@ class LowBalanceTest < Minitest::Test ) @auto_top_up.notify!.sync assert_mock @customer + assert_requested req end em :test_decline_notify! end