Class: Micropayment::Customer

Inherits:
Base
  • Object
show all
Defined in:
lib/micropayment-rails/classes/customer.rb

Constant Summary collapse

IDENTIFIER =
:customerId

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#id, valid_attributes

Class Method Details

.create!(params = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/micropayment-rails/classes/customer.rb', line 57

def self.create!(params={})
  params.symbolize_keys!
   = params.delete(:bank_account)
  address_params      = params.delete(:address)
  create_params = {}.tap do |hsh|
    hsh[:customerId]  = params.delete(:customerId)  if params.has_key?(:customerId)
    hsh[:freeParams]  = params
  end
  result = Micropayment::Debit.customerCreate( create_params )
  if result["error"] == "0"
    create_params[:customerId] ||= result["customerId"]
    customer = self.new( create_params )
    customer. =    if 
    customer.address      = address_params        if address_params
    customer
  else
    raise "Customer#create! - #{result["error"]}: #{result["errorMessage"]}"
  end
end

.find(customerId) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/micropayment-rails/classes/customer.rb', line 47

def self.find(customerId)
  result = Micropayment::Debit.customerGet( :customerId => customerId )
  case result["error"]
  when "0"
    self.new( :customerId => customerId, :freeParams => result["freeParams"] )
  else
    raise "Customer#find - #{result["error"]}: #{result["errorMessage"]}"
  end
end

.find_create_or_update_by_id(id, params = {}) ⇒ Object



83
84
85
86
87
# File 'lib/micropayment-rails/classes/customer.rb', line 83

def self.find_create_or_update_by_id(id, params={})
  params.symbolize_keys!
  obj = (find(id) rescue nil)
  obj ? obj.update!(params) : create!( params.merge(:customerId => id) )
end

.find_or_create_by_id(id, params = {}) ⇒ Object



77
78
79
80
81
# File 'lib/micropayment-rails/classes/customer.rb', line 77

def self.find_or_create_by_id(id, params={})
  params.symbolize_keys!
  obj = (find(id) rescue nil)
  obj ? obj : create!( params.merge(:customerId => id) )
end

.session_listObject



89
90
91
92
# File 'lib/micropayment-rails/classes/customer.rb', line 89

def self.session_list
  # TODO wrap in an array
  Micropayment::Debit.sessionList( :customerId => id )
end

Instance Method Details

#addressObject



11
12
13
# File 'lib/micropayment-rails/classes/customer.rb', line 11

def address
  @address ||= (Micropayment::Address.find( id ) rescue nil)
end

#address=(params = {}) ⇒ Object



7
8
9
# File 'lib/micropayment-rails/classes/customer.rb', line 7

def address=(params={})
  @address = Micropayment::Address.create!( id, params )
end

#bank_accountObject



19
20
21
# File 'lib/micropayment-rails/classes/customer.rb', line 19

def 
  @bank_account ||= (Micropayment::BankAccount.find( id ) rescue nil)
end

#bank_account=(params = {}) ⇒ Object



15
16
17
# File 'lib/micropayment-rails/classes/customer.rb', line 15

def bank_account=(params={})
  @bank_account = Micropayment::BankAccount.create!( id, params )
end

#update!(params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/micropayment-rails/classes/customer.rb', line 26

def update!(params={})
  params.symbolize_keys!
   = params.delete(:bank_account)
  address_params      = params.delete(:address)
  update_params = {}.tap do |hsh|
    hsh[:customerId]  = id
    hsh[:freeParams]  = params
  end
  result = Micropayment::Debit.customerSet( update_params )
  if result["error"] == "0"
    self.     =    if 
    self.customer.address = address_params        if address_params
    self
  else
    raise "Customer#update! - #{result["error"]}: #{result["errorMessage"]}"
  end
end