lib/pay/attributes.rb in pay-7.3.0 vs lib/pay/attributes.rb in pay-8.0.0

- old
+ new

@@ -29,24 +29,32 @@ # - Removes the default flag from all other Pay::Customers # - Removes the default flag from all Pay::PaymentMethods 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).first_or_initialize + 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 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 + # 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 def payment_processor @@ -73,10 +81,10 @@ end def set_merchant_processor(processor_name, **attributes) ActiveRecord::Base.transaction do pay_merchants.update_all(default: false) - pay_merchant = pay_merchants.where(processor: processor_name).first_or_initialize + pay_merchant = pay_merchants.where(processor: processor_name, type: "Pay::#{processor_name.to_s.classify}::Merchant").first_or_initialize pay_merchant.update!(attributes.merge(default: true)) end # Return new payment processor reload_merchant_processor