Class: CoreMerchant::SubscriptionManager

Inherits:
Object
  • Object
show all
Includes:
Concerns::SubscriptionManagerNotifications
Defined in:
lib/core_merchant/subscription_manager.rb

Overview

Manages subscriptions in CoreMerchant. This class is responsible for notifying listeners when subscription events occur and checking for and handling renewals. Attributes:

- `listeners` - An array of listeners that will be notified when subscription events occur.

Methods:

- `check_subscriptions` - Checks all subscriptions for renewals
- `add_listener(listener)` - Adds a listener to the list of listeners
- `no_payment_needed_for_renewal(subscription)` - Handles the case where no payment is needed for a renewal.
   Call when a subscription is renewed without payment.
- `processing_payment_for_renewal(subscription)` - Handles the case where payment is being processed for a renewal.
   Call when payment is being processed for a renewal.
- `payment_successful_for_renewal(subscription)` - Handles the case where payment was successful for a renewal.
   Call when payment was successful for a renewal.
- `payment_failed_for_renewal(subscription)` - Handles the case where payment failed for a renewal.
   Call when payment failed for a renewal.

Usage:

```ruby
manager = CoreMerchant.subscription_manager
manager.check_subscriptions

# ... somewhere else in the code ...
manager.payment_successful_for_renewal(subscription1)
manager.payment_failed_for_renewal(subscription2)
```

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSubscriptionManager

Returns a new instance of SubscriptionManager.



39
40
41
# File 'lib/core_merchant/subscription_manager.rb', line 39

def initialize
  @listeners = []
end

Instance Attribute Details

#listenersObject (readonly)

Returns the value of attribute listeners.



37
38
39
# File 'lib/core_merchant/subscription_manager.rb', line 37

def listeners
  @listeners
end

Instance Method Details

#add_listener(listener) ⇒ Object



48
49
50
# File 'lib/core_merchant/subscription_manager.rb', line 48

def add_listener(listener)
  @listeners << listener
end

#check_cancellationsObject



89
90
91
92
93
# File 'lib/core_merchant/subscription_manager.rb', line 89

def check_cancellations
  Subscription.find_each do |subscription|
    process_for_cancellation(subscription) if subscription.pending_cancellation?
  end
end

#check_renewalsObject



52
53
54
55
56
# File 'lib/core_merchant/subscription_manager.rb', line 52

def check_renewals
  Subscription.find_each do |subscription|
    process_for_renewal(subscription) if subscription.due_for_renewal?
  end
end

#check_subscriptionsObject



43
44
45
46
# File 'lib/core_merchant/subscription_manager.rb', line 43

def check_subscriptions
  check_renewals
  check_cancellations
end

#no_payment_needed_for_renewal(subscription) ⇒ Object



64
65
66
# File 'lib/core_merchant/subscription_manager.rb', line 64

def no_payment_needed_for_renewal(subscription)
  renew_subscription(subscription)
end

#payment_failed_for_renewal(subscription) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/core_merchant/subscription_manager.rb', line 78

def payment_failed_for_renewal(subscription)
  is_in_grace_period = subscription.in_grace_period?
  if is_in_grace_period
    subscription.transition_to_past_due
    notify(subscription, :grace_period_started, days_remaining: subscription.days_remaining_in_grace_period)
  else
    subscription.transition_to_expired
    notify(subscription, :expired)
  end
end

#payment_successful_for_renewal(subscription) ⇒ Object



74
75
76
# File 'lib/core_merchant/subscription_manager.rb', line 74

def payment_successful_for_renewal(subscription)
  renew_subscription(subscription)
end

#process_for_cancellation(subscription) ⇒ Object



95
96
97
98
99
# File 'lib/core_merchant/subscription_manager.rb', line 95

def process_for_cancellation(subscription)
  return unless subscription.transition_to_expired

  notify(subscription, :expired)
end

#process_for_renewal(subscription) ⇒ Object



58
59
60
61
62
# File 'lib/core_merchant/subscription_manager.rb', line 58

def process_for_renewal(subscription)
  return unless subscription.transition_to_processing_renewal

  notify(subscription, :due_for_renewal)
end

#processing_payment_for_renewal(subscription) ⇒ Object



68
69
70
71
72
# File 'lib/core_merchant/subscription_manager.rb', line 68

def processing_payment_for_renewal(subscription)
  return unless subscription.transition_to_processing_payment

  notify(subscription, :renewal_payment_processing)
end