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)


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

def braintree?
  ActiveSupport::Deprecation.warn("This will be removed in the next release. Use `@billable.processor.braintree?` instead.")
  processor == "braintree"
end

#create_pay_subscription(subscription, processor, name, plan, options = {}) ⇒ Object

Used for creating a Pay::Subscription in the database



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pay/billable.rb', line 146

def create_pay_subscription(subscription, processor, name, plan, options = {})
  options[:quantity] ||= 1

  options.merge!(
    name: name || "default",
    processor: processor,
    processor_id: subscription.id,
    processor_plan: plan,
    trial_ends_at: payment_processor.trial_end_date(subscription),
    ends_at: nil
  )
  subscriptions.create!(options)
end

#create_setup_intentObject



72
73
74
75
# File 'lib/pay/billable.rb', line 72

def create_setup_intent
  ActiveSupport::Deprecation.warn("This method will be removed in the next release. Use `@billable.payment_processor.create_setup_intent` instead.")
  payment_processor.create_setup_intent
end

#customerObject

Raises:



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

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

  customer = payment_processor.customer
  payment_processor.update_card(card_token) if card_token.present?
  customer
end

#customer_nameObject



68
69
70
# File 'lib/pay/billable.rb', line 68

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

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

Returns:

  • (Boolean)


141
142
143
# File 'lib/pay/billable.rb', line 141

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

#invoice!(options = {}) ⇒ Object



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

def invoice!(options = {})
  ActiveSupport::Deprecation.warn("This will be removed in the next release. Use `@billable.payment_processor.invoice!` instead.")
  payment_processor.invoice!(options)
end

#on_generic_trial?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/pay/billable.rb', line 86

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)


77
78
79
80
81
82
83
84
# File 'lib/pay/billable.rb', line 77

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)


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

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)


136
137
138
139
# File 'lib/pay/billable.rb', line 136

def paddle?
  ActiveSupport::Deprecation.warn("This will be removed in the next release. Use `@billable.processor.paddle?` instead.")
  processor == "paddle"
end

#payment_processorObject



36
37
38
# File 'lib/pay/billable.rb', line 36

def payment_processor
  @payment_processor ||= payment_processor_for(processor).new(self)
end

#payment_processor_for(name) ⇒ Object

Raises:



40
41
42
43
# File 'lib/pay/billable.rb', line 40

def payment_processor_for(name)
  raise Error, "No payment processor set. Assign a payment processor with 'object.processor = :stripe' or any supported processor." if name.blank?
  "Pay::#{name.to_s.classify}::Billable".constantize
end

#paypal?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/pay/billable.rb', line 132

def paypal?
  card_type == "PayPal"
end

#processorObject



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

def processor
  super&.inquiry
end

#processor=(value) ⇒ Object

Reset the payment processor when it changes



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

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

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



90
91
92
# File 'lib/pay/billable.rb', line 90

def processor_subscription(subscription_id, options = {})
  payment_processor.processor_subscription(subscription_id, options)
end

#stripe?Boolean

Returns:

  • (Boolean)


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

def stripe?
  ActiveSupport::Deprecation.warn("This will be removed in the next release. Use `@billable.processor.stripe?` instead.")
  processor == "stripe"
end

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

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
# File 'lib/pay/billable.rb', line 94

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



108
109
110
# File 'lib/pay/billable.rb', line 108

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

#upcoming_invoiceObject



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

def upcoming_invoice
  ActiveSupport::Deprecation.warn("This will be removed in the next release. Use `@billable.payment_processor.upcoming_invoice` instead.")
  payment_processor.upcoming_invoice
end