summaryrefslogtreecommitdiff
path: root/app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb
blob: 34540038207f8529254acbca217b31832afaac0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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.installment_succeeded'
    event_action :report_payment_failure, event_name: 'solidus_subscriptions.installment_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