Module: Pay::Attributes::CustomerExtension

Extended by:
ActiveSupport::Concern
Defined in:
lib/pay/attributes.rb

Instance Method Summary collapse

Instance Method Details

#add_payment_processor(processor_name, allow_fake: false, **attributes) ⇒ Object

Raises:


42
43
44
45
46
47
48
# File 'lib/pay/attributes.rb', line 42

def add_payment_processor(processor_name, allow_fake: false, **attributes)
  raise Pay::Error, "Processor `#{processor_name}` is not allowed" if processor_name.to_s == "fake_processor" && !allow_fake

  pay_customer = pay_customers.active.where(processor: processor_name).first_or_initialize
  pay_customer.update!(attributes)
  pay_customer
end

#cancel_active_pay_subscriptions!Object


60
61
62
# File 'lib/pay/attributes.rb', line 60

def cancel_active_pay_subscriptions!
  subscriptions.active.each(&:cancel_now!)
end

#payment_processorObject


50
51
52
53
54
55
56
57
58
# File 'lib/pay/attributes.rb', line 50

def payment_processor
  current_processor = super

  if current_processor.blank? && self.class.pay_default_payment_processor.present?
    set_payment_processor(self.class.pay_default_payment_processor, allow_fake: true)
  else
    current_processor
  end
end

#set_payment_processor(processor_name, allow_fake: false, **attributes) ⇒ Object

Changes a user's payment processor

This has several effects:

  • Finds or creates a Pay::Customer for the process and marks it as default

  • Removes the default flag from all other Pay::Customers

  • Removes the default flag from all Pay::PaymentMethods

Raises:


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pay/attributes.rb', line 29

def set_payment_processor(processor_name, allow_fake: false, **attributes)
  raise Pay::Error, "Processor `#{processor_name}` is not allowed" if processor_name.to_s == "fake_processor" && !allow_fake

  ActiveRecord::Base.transaction do
    pay_customers.update_all(default: false)
    pay_customer = pay_customers.active.where(processor: processor_name).first_or_initialize
    pay_customer.update!(attributes.merge(default: true))
  end

  # Return new payment processor
  reload_payment_processor
end