summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessandro Desantis <desa.alessandro@gmail.com>2020-10-09 14:08:50 +0200
committerAlessandro Desantis <desa.alessandro@gmail.com>2020-10-09 14:36:59 +0200
commit4e17c67811cc1238964a4f1ad00ba53f80e75c1a (patch)
treeb98696f92c8d98e00b06689b52b7ca2418f565fb
parent5a5b2a3201462532f4fa9d38e02867a243c68670 (diff)
Report subscription lifecycle events to Churn Buster
-rw-r--r--README.md18
-rw-r--r--app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb39
-rw-r--r--config/initializers/subscribers.rb1
-rw-r--r--spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb74
4 files changed, 132 insertions, 0 deletions
diff --git a/README.md b/README.md
index 81b4a29..5dbacb6 100644
--- a/README.md
+++ b/README.md
@@ -95,6 +95,24 @@ We suggest using the [Whenever](https://github.com/javan/whenever) gem to schedu
You can find the API documentation [here](https://stoplight.io/p/docs/gh/solidusio-contrib/solidus_subscriptions?group=master).
+### Churn Buster integration
+
+This extension optionally integrates with [Churn Buster](https://churnbuster.io) for failed payment
+recovery. In order to enable the integration, simply add your Churn Buster credentials to your
+configuration:
+
+```ruby
+SolidusSubscriptions.configure do |config|
+ # ...
+
+ config.churn_buster_account_id = 'YOUR_CHURN_BUSTER_ACCOUNT_ID'
+ config.churn_buster_api_key = 'YOUR_CHURN_BUSTER_API_KEY'
+end
+```
+
+The extension will take care of reporting successful/failed payments and payment method changes
+to Churn Buster.
+
## Development
### Testing the extension
diff --git a/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb b/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb
new file mode 100644
index 0000000..aa9dfa4
--- /dev/null
+++ b/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+module SolidusSubscriptions
+ module ChurnBusterSubscriber
+ include ::Spree::Event::Subscriber
+
+ event_action :report_subscription_cancellation, event_name: 'solidus_subscriptions.subscription_canceled'
+ event_action :report_subscription_ending, event_name: 'solidus_subscriptions.subscription_ended'
+ event_action :report_payment_success, event_name: 'solidus_subscriptions.installments_succeeded'
+ event_action :report_payment_failure, event_name: 'solidus_subscriptions.installments_failed_payment'
+ event_action :report_payment_method_change, event_name: 'solidus_subscriptions.subscription_payment_method_changed'
+
+ def report_subscription_cancellation(event)
+ churn_buster&.report_subscription_cancellation(event.payload.fetch(:subscription))
+ end
+
+ def report_subscription_ending(event)
+ churn_buster&.report_subscription_cancellation(event.payload.fetch(:subscription))
+ end
+
+ def report_payment_success(event)
+ churn_buster&.report_successful_payment(event.payload.fetch(:order))
+ end
+
+ def report_payment_failure(event)
+ churn_buster&.report_failed_payment(event.payload.fetch(:order))
+ end
+
+ def report_payment_method_change(event)
+ churn_buster&.report_payment_method_change(event.payload.fetch(:subscription))
+ end
+
+ private
+
+ def churn_buster
+ SolidusSubscriptions.churn_buster
+ end
+ end
+end
diff --git a/config/initializers/subscribers.rb b/config/initializers/subscribers.rb
index fe4c88b..ea016b8 100644
--- a/config/initializers/subscribers.rb
+++ b/config/initializers/subscribers.rb
@@ -2,4 +2,5 @@
Spree.config do |config|
config.events.subscribers << 'SolidusSubscriptions::EventStorageSubscriber'
+ config.events.subscribers << 'SolidusSubscriptions::ChurnBusterSubscriber'
end
diff --git a/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb b/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb
new file mode 100644
index 0000000..572913b
--- /dev/null
+++ b/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb
@@ -0,0 +1,74 @@
+RSpec.describe SolidusSubscriptions::ChurnBusterSubscriber do
+ describe '#report_subscription_cancellation' do
+ it 'reports the cancellation to Churn Buster' do
+ churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
+ allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)
+
+ subscription = create(:subscription)
+ Spree::Event.fire('solidus_subscriptions.subscription_canceled', subscription: subscription)
+
+ expect(churn_buster).to have_received(:report_subscription_cancellation).with(subscription)
+ end
+ end
+
+ describe '#report_subscription_ending' do
+ it 'reports the cancellation to Churn Buster' do
+ churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
+ allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)
+
+ subscription = create(:subscription)
+ Spree::Event.fire('solidus_subscriptions.subscription_ended', subscription: subscription)
+
+ expect(churn_buster).to have_received(:report_subscription_cancellation).with(subscription)
+ end
+ end
+
+ describe '#report_payment_success' do
+ it 'reports the success to Churn Buster' do
+ churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
+ allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)
+
+ order = build_stubbed(:order)
+ installments = build_list(:installment, 2)
+ Spree::Event.fire(
+ 'solidus_subscriptions.installments_succeeded',
+ installments: installments,
+ order: order,
+ )
+
+ expect(churn_buster).to have_received(:report_successful_payment).with(order)
+ end
+ end
+
+ describe '#report_payment_failure' do
+ it 'reports the failure to Churn Buster' do
+ churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
+ allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)
+
+ order = build_stubbed(:order)
+ installments = build_list(:installment, 2)
+ Spree::Event.fire(
+ 'solidus_subscriptions.installments_failed_payment',
+ installments: installments,
+ order: order,
+ )
+
+ expect(churn_buster).to have_received(:report_failed_payment).with(order)
+ end
+ end
+
+ describe '#report_payment_method_change' do
+ it 'reports the payment method change to Churn Buster' do
+ churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
+ allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)
+
+ subscription = create(:subscription)
+ Spree::Event.fire(
+ 'solidus_subscriptions.subscription_payment_method_changed',
+ subscription: subscription,
+ )
+
+ expect(churn_buster).to have_received(:report_payment_method_change).with(subscription)
+ end
+ end
+end