module Pay::Attributes::CustomerExtension

Public Instance Methods

cancel_active_pay_subscriptions!() click to toggle source
# 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) click to toggle source

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

# 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