Module: Pay::Attributes::CustomerExtension

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

Instance Method Summary collapse

Instance Method Details

#cancel_active_pay_subscriptions!Object



38
39
40
# File 'lib/pay/attributes.rb', line 38

def cancel_active_pay_subscriptions!
  subscriptions.active.each(&:cancel_now!)
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:



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pay/attributes.rb', line 25

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