class NmiDirectPost::CustomerVault

Attributes

report_type[R]
customer_vault[R]
customer_vault_id[R]
report_type[R]

Public Class Methods

all() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 160
def all
  limit
end
all_ids() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 146
def all_ids
  @report_type = :customer_vault
  safe_params = generate_query_string([:report_type])
  NmiDirectPost.logger.debug { "Loading all NMI customer vaults using query: #{safe_params}" }
  begin
    customers = get(all_params(safe_params))["customer_vault"]
  ensure
    @report_type = nil
  end
  return [] if customers.nil?
  customers = customers["customer"]
  customers.collect { |customer| customer["customer_vault_id"].to_i }
end
all_params(safe_params, target=self) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 164
def all_params(safe_params, target=self)
  [safe_params, generate_query_string(Base::AUTH_PARAMS, target)].join('&')
end
find_by_customer_vault_id(customer_vault_id, username=nil, password=nil) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 129
def find_by_customer_vault_id(customer_vault_id, username=nil, password=nil)
  raise StandardError, "CustomerVaultID cannot be blank" if customer_vault_id.blank?
  begin
    new(customer_vault_id: customer_vault_id, username: username, password: password)
  rescue CustomerVaultNotFoundError
    return nil
  end
end
first(limit = 1) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 138
def first(limit = 1)
  limit(0, limit-1).first
end
last(limit = 1) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 142
def last(limit = 1)
  limit(-limit, -1).first
end
new(attributes) click to toggle source
Calls superclass method NmiDirectPost::Base::new
# File lib/nmi_direct_post/customer_vault.rb, line 41
def initialize(attributes)
  super
  if attributes[:customer_vault_id].blank?
    set_attributes(attributes.dup) unless attributes.empty?
  else
    @customer_vault_id = attributes[:customer_vault_id].to_i
    reload
  end
end

Private Class Methods

attr_accessor_with_tracking_of_changes(*list) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 17
def self.attr_accessor_with_tracking_of_changes(*list)
  list.each do |attr|
    attr_reader attr
    define_method("#{attr}=") do |val|
      (@attributes_to_save ||=[]) << attr
      instance_variable_set("@#{attr}", val)
    end
  end
end
limit(first = 0, last = -1) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 169
def limit(first = 0, last = -1)
  all_ids[first..last].collect { |id| new(:customer_vault_id => id) }
end

Public Instance Methods

checking?() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 109
def checking?
  !@check_hash.blank?
end
create() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 51
def create
  post_action(:add)
  self.success?
end
credit_card?() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 105
def credit_card?
  !@cc_hash.blank?
end
destroy() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 73
def destroy
  post_action(:delete)
  self
end
find!() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 113
def find!
  begin
    @report_type = :customer_vault
    safe_params = generate_query_string(MERCHANT_DEFINED_FIELDS + [:last_name, :email, :report_type]) # These are the only fields you can use when looking up without a customer_vault_id
    logger.info { "Querying NMI customer vault: #{safe_params}" }
    @customer_vault_id = self.class.get(self.class.all_params(safe_params, self))['customer_vault'][0]['customer_vault_id'] # This assumes there is only 1 result.
    # TODO: When there are multiple results, we don't know which one you want.  Maybe raise an error in that case?
    reload
  ensure
    @report_type = nil
  end
end
reload() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 78
def reload
  @report_type = :customer_vault
  if invalid?
    @report_type = nil
    return self
  end
  begin
    safe_params = customer_vault_instance_params
    logger.debug { "Loading NMI customer vault from customer_vault_id(#{customer_vault_id}) using query: #{safe_params}" }
    response = self.class.get(self.class.all_params(safe_params, self))["customer_vault"]
    raise CustomerVaultNotFoundError, "No record found for customer vault ID #{self.customer_vault_id}" if response.nil?
    attributes = response["customer"].with_indifferent_access
    READ_ONLY_ATTRIBUTES.each do |a|
      if attributes.key?(a)
        val = attributes.delete(a)
        instance_variable_set("@#{a}",val)
      end
    end
    set_attributes(attributes.tap { |_| _.delete(:customer_vault_id) })
  ensure
    @report_type = nil
    @attributes_to_update = nil
    @attributes_to_save = nil
  end
  self
end
save!() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 67
def save!
  post_action(:update)
  reload
  self.success?
end
update!(attributes) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 56
def update!(attributes)
  begin
    set_attributes(attributes)
    post_action(:update)
  ensure
    @attributes_to_save.delete_if {|v| @attributes_to_update.include?(v) } if @attributes_to_save
    @attributes_to_update = nil
  end
  self
end

Private Instance Methods

billing_information_present?() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 214
def billing_information_present?
  self.errors.add(:billing_information, "Either :cc_number (a credit card number) and :cc_exp (the credit card expiration date), or :check_account, :check_aba (the routing number of the checking account) and :check_name (a nickname for the account), must be present") if (missing_cc_information? && missing_checking_information?)
end
customer_vault_instance_params() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 175
def customer_vault_instance_params
  generate_query_string([:customer_vault, :customer_vault_id, :report_type])
end
missing_cc_information?() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 222
def missing_cc_information?
  self.cc_exp.blank? || self.cc_number.blank?
end
missing_checking_information?() click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 218
def missing_checking_information?
  self.check_account.blank? || self.check_aba.blank? || self.check_name.blank?
end
post(safe_params) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 179
def post(safe_params)
  logger.info { "Sending Direct Post to NMI: #{safe_params}" }
  response = self.class.post(self.class.all_params(safe_params, self))
  @response, @response_text, @response_code = response["response"].to_i, response["responsetext"], response["response_code"].to_i
  @customer_vault_id = response["customer_vault_id"].to_i if :add_customer == self.customer_vault
end
post_action(action) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 226
def post_action(action)
  @customer_vault = :"#{action}_customer"
  safe_params = case action.to_sym
  when :delete
    customer_vault_instance_params
  when :add
    [customer_vault_instance_params, generate_query_string(WHITELIST_ATTRIBUTES)].join("&")
  when :update
    [customer_vault_instance_params, generate_query_string(@attributes_to_update || @attributes_to_save)].join("&")
  else
    raise CustomerVaultInvalidPostActionError, "#{action} is not a valid post action.  NmiDirectPost allows the following post actions: :add, :update, :delete"
  end
  begin
    post(safe_params) if valid?
  ensure
    @customer_vault = nil
  end
end
set_attributes(attributes) click to toggle source
# File lib/nmi_direct_post/customer_vault.rb, line 186
def set_attributes(attributes)
  attributes = attributes.with_indifferent_access
  @attributes_to_update = []
  merchant_defined_fields = []
  if attributes.key?(:merchant_defined_field) && attributes[:merchant_defined_field].is_a?(String)
    self.merchant_defined_field_1 = attributes.delete(:merchant_defined_field)
  end
  WHITELIST_ATTRIBUTES.each do |a|
    if attributes.key?(a)
      val = attributes.delete(a)
      @attributes_to_update << a
    end
    merchant_defined_field_index = a.to_s.split('merchant_defined_field_')[1]
    if (!merchant_defined_field_index.nil? && val.nil? && attributes.key?(:merchant_defined_field) && attributes[:merchant_defined_field].is_a?(Array))
      index = merchant_defined_field_index.to_i - 1
      if attributes[:merchant_defined_field].size > index
        val = attributes[:merchant_defined_field][index]
        attributes[:merchant_defined_field][index] = nil
        @attributes_to_update << a
      end
    end
    self.__send__("#{a}=", val) if @attributes_to_update.include?(a)
  end
  attributes.delete(:merchant_defined_field) unless attributes.key?(:merchant_defined_field) && attributes[:merchant_defined_field].any?
  @id = @id.to_i if @id
  raise MassAssignmentSecurity::Error, "Cannot mass-assign the following attributes: #{attributes.keys.join(", ")}" unless attributes.empty?
end