Module: Pay::Billable

Extended by:
ActiveSupport::Concern
Defined in:
lib/pay/billable.rb,
lib/pay/billable/sync_email.rb

Defined Under Namespace

Modules: SyncEmail

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.includersObject (readonly)

Returns the value of attribute includers.



9
10
11
# File 'lib/pay/billable.rb', line 9

def includers
  @includers
end

Class Method Details

.included(base = nil, &block) ⇒ Object



12
13
14
15
16
# File 'lib/pay/billable.rb', line 12

def self.included(base = nil, &block)
  @includers ||= []
  @includers << base if base
  super
end

Instance Method Details

#braintree?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/pay/billable.rb', line 114

def braintree?
  processor == "braintree"
end

#charge(amount_in_cents, options = {}) ⇒ Object



50
51
52
53
# File 'lib/pay/billable.rb', line 50

def charge(amount_in_cents, options = {})
  check_for_processor
  send("create_#{processor}_charge", amount_in_cents, options)
end

#customerObject

Raises:



37
38
39
40
41
42
43
44
# File 'lib/pay/billable.rb', line 37

def customer
  check_for_processor
  raise Pay::Error, I18n.t("errors.email_required") if email.nil?

  customer = send("#{processor}_customer")
  update_card(card_token) if card_token.present?
  customer
end

#customer_nameObject



46
47
48
# File 'lib/pay/billable.rb', line 46

def customer_name
  [try(:first_name), try(:last_name)].compact.join(" ")
end

#has_incomplete_payment?(name: Pay.default_product_name) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/pay/billable.rb', line 126

def has_incomplete_payment?(name: Pay.default_product_name)
  subscription(name: name)&.has_incomplete_payment?
end

#invoice!(options = {}) ⇒ Object



102
103
104
# File 'lib/pay/billable.rb', line 102

def invoice!(options = {})
  send("#{processor}_invoice!", options)
end

#on_generic_trial?Boolean

Returns:

  • (Boolean)


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

def on_generic_trial?
  trial_ends_at? && trial_ends_at > Time.zone.now
end

#on_trial?(name: Pay.default_product_name, plan: nil) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/pay/billable.rb', line 66

def on_trial?(name: Pay.default_product_name, plan: nil)
  return true if default_generic_trial?(name, plan)

  sub = subscription(name: name)
  return sub&.on_trial? if plan.nil?

  sub&.on_trial? && sub.processor_plan == plan
end

#on_trial_or_subscribed?(name: Pay.default_product_name, processor_plan: nil) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/pay/billable.rb', line 93

def on_trial_or_subscribed?(name: Pay.default_product_name, processor_plan: nil)
  on_trial?(name: name, plan: processor_plan) ||
    subscribed?(name: name, processor_plan: processor_plan)
end

#paddle?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/pay/billable.rb', line 122

def paddle?
  processor == "paddle"
end

#paypal?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/pay/billable.rb', line 118

def paypal?
  braintree? && card_type == "PayPal"
end

#processor=(value) ⇒ Object



32
33
34
35
# File 'lib/pay/billable.rb', line 32

def processor=(value)
  super(value)
  self.processor_id = nil if processor_changed?
end

#processor_subscription(subscription_id, options = {}) ⇒ Object



79
80
81
82
# File 'lib/pay/billable.rb', line 79

def processor_subscription(subscription_id, options = {})
  check_for_processor
  send("#{processor}_subscription", subscription_id, options)
end

#stripe?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/pay/billable.rb', line 110

def stripe?
  processor == "stripe"
end

#subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options) ⇒ Object



55
56
57
58
# File 'lib/pay/billable.rb', line 55

def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options)
  check_for_processor
  send("create_#{processor}_subscription", name, plan, options)
end

#subscribed?(name: Pay.default_product_name, processor_plan: nil) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
# File 'lib/pay/billable.rb', line 84

def subscribed?(name: Pay.default_product_name, processor_plan: nil)
  subscription = subscription(name: name)

  return false if subscription.nil?
  return subscription.active? if processor_plan.nil?

  subscription.active? && subscription.processor_plan == processor_plan
end

#subscription(name: Pay.default_product_name) ⇒ Object



98
99
100
# File 'lib/pay/billable.rb', line 98

def subscription(name: Pay.default_product_name)
  subscriptions.loaded? ? subscriptions.reverse.detect { |s| s.name == name } : subscriptions.for_name(name).last
end

#upcoming_invoiceObject



106
107
108
# File 'lib/pay/billable.rb', line 106

def upcoming_invoice
  send("#{processor}_upcoming_invoice")
end

#update_card(token) ⇒ Object



60
61
62
63
64
# File 'lib/pay/billable.rb', line 60

def update_card(token)
  check_for_processor
  customer if processor_id.nil?
  send("update_#{processor}_card", token)
end