More churnbuster integration

Stephen Paul Weber created

Change summary

lib/admin_actions/cancel.rb   |  4 +-
lib/churnbuster.rb            | 23 ++++++++++++++++++++
lib/credit_card_sale.rb       | 12 ++++++++-
test/test_admin_command.rb    | 36 +++++++++++++++++++++++++++++++
test/test_credit_card_sale.rb | 42 +++++++++++++++++++++++++++++++++++++
test/test_low_balance.rb      |  9 ++-----
6 files changed, 116 insertions(+), 10 deletions(-)

Detailed changes

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

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)

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

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

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

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