Class: Pay::Abacatepay::Subscription

Inherits:
Subscription
  • Object
show all
Defined in:
lib/pay/abacatepay/subscription.rb

Constant Summary collapse

STATUS_FROM_EVENT =
{
  "subscription.completed" => "active",
  "subscription.renewed" => "active",
  "subscription.cancelled" => "canceled"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sync(processor_id, event: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pay/abacatepay/subscription.rb', line 10

def self.sync(processor_id, event: nil)
  return if processor_id.blank?

  subscription = Pay::Abacatepay::Subscription.find_by(processor_id: processor_id)
  customer = subscription&.customer
  customer ||= Pay::Customer.find_by(processor: "abacatepay", processor_id: event&.customer_id) if event

  if customer.nil?
    Rails.logger.warn("[pay-abacatepay] cannot sync subscription #{processor_id}: Pay::Customer not found")
    return
  end

  subscription ||= Pay::Abacatepay::Subscription.new(customer: customer, processor_id: processor_id)
  assign_attributes_from(subscription, event) if event
  subscription.save!
  subscription
end

Instance Method Details

#api_recordObject



79
80
81
82
83
# File 'lib/pay/abacatepay/subscription.rb', line 79

def api_record
  ::AbacatePay.subscriptions.send(:request, "GET", "get", params: {id: processor_id})
rescue ::AbacatePay::Error => e
  raise Pay::Abacatepay::Error, e.message
end

#cancel(**options) ⇒ Object



44
45
46
47
48
49
# File 'lib/pay/abacatepay/subscription.rb', line 44

def cancel(**options)
  Rails.logger.warn(
    "[pay-abacatepay] AbacatePay does not support cancel-at-period-end; cancelling immediately"
  )
  cancel_now!(**options)
end

#cancel_now!(**_options) ⇒ Object



51
52
53
54
55
56
# File 'lib/pay/abacatepay/subscription.rb', line 51

def cancel_now!(**_options)
  ::AbacatePay.subscriptions.send(:request, "POST", "cancel", params: {id: processor_id})
  update!(status: "canceled", ends_at: Time.current)
rescue ::AbacatePay::Error => e
  raise Pay::Abacatepay::Error, e.message
end

#change_quantity(_quantity, **_options) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
71
# File 'lib/pay/abacatepay/subscription.rb', line 68

def change_quantity(_quantity, **_options)
  raise NotImplementedError,
    "AbacatePay does not support quantity changes"
end

#past_due?Boolean

AbacatePay does not emit payment-failure events, so we cannot observe a past_due state. Always false.

Returns:

  • (Boolean)


75
76
77
# File 'lib/pay/abacatepay/subscription.rb', line 75

def past_due?
  false
end

#resumeObject

Raises:

  • (NotImplementedError)


58
59
60
61
# File 'lib/pay/abacatepay/subscription.rb', line 58

def resume
  raise NotImplementedError,
    "AbacatePay does not support resuming cancelled subscriptions; create a new subscription"
end

#swap(_plan, **_options) ⇒ Object

Raises:

  • (NotImplementedError)


63
64
65
66
# File 'lib/pay/abacatepay/subscription.rb', line 63

def swap(_plan, **_options)
  raise NotImplementedError,
    "AbacatePay does not support plan swap; cancel the subscription and create a new one"
end