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:



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

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



62
63
64
# File 'lib/pay/attributes.rb', line 62

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

#payment_processorObject



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

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:



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

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