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:



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

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

  # Safety check to make sure this is a valid Pay processor
  klass = "Pay::#{processor_name.to_s.classify}::Customer".constantize
  raise ArgumentError, "not a valid payment processor" if klass.ancestors.exclude?(Pay::Customer)

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

#cancel_active_pay_subscriptions!Object



70
71
72
# File 'lib/pay/attributes.rb', line 70

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

#payment_processorObject



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

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
43
44
45
46
# 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

  # Safety check to make sure this is a valid Pay processor
  klass = "Pay::#{processor_name.to_s.classify}::Customer".constantize
  raise ArgumentError, "not a valid payment processor" if klass.ancestors.exclude?(Pay::Customer)

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

  # Return new payment processor
  reload_payment_processor
end