Class: RubyPsigate::Account

Inherits:
Request
  • Object
show all
Defined in:
lib/ruby_psigate/account.rb

Instance Attribute Summary collapse

Attributes inherited from Request

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Request

credential, credential=, #post, storeid, storeid=

Constructor Details

#initialize(attributes = {}) ⇒ Account

Returns a new instance of Account.



17
18
19
20
21
22
23
24
25
# File 'lib/ruby_psigate/account.rb', line 17

def initialize(attributes={})
  attributes.each_pair do |attribute, value|
    if self.respond_to?(attribute)
      setter = "#{attribute}=".to_sym
      self.send(setter, value)
    end
  end
  super
end

Instance Attribute Details

#accountidObject

Returns the value of attribute accountid.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def accountid
  @accountid
end

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/ruby_psigate/account.rb', line 5

def action
  @action
end

#address1Object

Returns the value of attribute address1.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def address1
  @address1
end

#address2Object

Returns the value of attribute address2.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def address2
  @address2
end

#cityObject

Returns the value of attribute city.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def city
  @city
end

#commentsObject

Returns the value of attribute comments.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def comments
  @comments
end

#companyObject

Returns the value of attribute company.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def company
  @company
end

#countryObject

Returns the value of attribute country.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def country
  @country
end

#credit_cardObject

Returns the value of attribute credit_card.



10
11
12
# File 'lib/ruby_psigate/account.rb', line 10

def credit_card
  @credit_card
end

#emailObject

Returns the value of attribute email.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def email
  @email
end

#errorObject

Returns the value of attribute error.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def error
  @error
end

#faxObject

Returns the value of attribute fax.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def fax
  @fax
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def name
  @name
end

#phoneObject

Returns the value of attribute phone.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def phone
  @phone
end

#postalcodeObject Also known as: zipcode

Returns the value of attribute postalcode.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def postalcode
  @postalcode
end

#provinceObject Also known as: state

Returns the value of attribute province.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def province
  @province
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/ruby_psigate/account.rb', line 5

def response
  @response
end

#returncodeObject

Returns the value of attribute returncode.



5
6
7
# File 'lib/ruby_psigate/account.rb', line 5

def returncode
  @returncode
end

#returnmessageObject

Returns the value of attribute returnmessage.



5
6
7
# File 'lib/ruby_psigate/account.rb', line 5

def returnmessage
  @returnmessage
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/ruby_psigate/account.rb', line 4

def status
  @status
end

Class Method Details

.disable(accountid) ⇒ Object



192
193
194
195
# File 'lib/ruby_psigate/account.rb', line 192

def self.disable(accountid)
  response = self.do(:action => "AMA09", :accountid => accountid, :success => "RPA-0040")
  response
end

.enable(accountid) ⇒ Object



187
188
189
190
# File 'lib/ruby_psigate/account.rb', line 187

def self.enable(accountid)
  response = self.do(:action => "AMA08", :accountid => accountid, :success => "RPA-0046")
  response      
end

.find(accountid) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ruby_psigate/account.rb', line 142

def self.find(accountid)
  begin
    params = {
      :Request => {
        :CID => credential.cid,
        :UserID => credential.userid,
        :Password => credential.password,
        :Action => "AMA05",
        :Condition => { :AccountID => accountid }
      }
    }
    
    @result = Request.new
    @result.params = params
    @result = @result.post        

    if @result.returncode == "RPA-0020"
      # Adds basic attributes
      attributes = {}
      %w( AccountID Name Company Address1 Address2 City Province Postalcode Country Phone Fax Email Comments ).each do |attribute|
        attributes[attribute.downcase.to_sym] = @result.send(attribute.downcase.to_sym)
      end
      
      attributes[:status] = @result.response["Account"]["Status"]
      
      # Back end info
      %w( returnmessage returncode action ).each do |info|
        attributes[info.downcase.to_sym] = @result.send(info.downcase.to_sym)
      end
      
      # Adds credit card attribute
      %w( status serialno cardholder cardnumber cardexpmonth cardexpyear cardtype ).each do |cc|
        # TODO
      end
      
      @account = .new(attributes)
    else
      @account = nil
    end
  rescue ConnectionError => e
    @account = nil
  end
  @account
end

Instance Method Details

#add_payment_method(credit_card) ⇒ Object

Add/Delete/Enable/Disable payment method returns the serial no (of the payment method) if successful, else false



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ruby_psigate/account.rb', line 96

def add_payment_method(credit_card)
  begin
    # Action
    action = "AMA11"
    @request[:Request][:Action] = action
    
    @request[:Request][:Account] = {
      :AccountID => accountid,
      :CardInfo => {}
    }
    
    %w( CardHolder CardNumber CardExpMonth CardExpYear ).each do |c|
      value = credit_card.send((c.downcase).to_sym)
      @request[:Request][:Account][:CardInfo][c.to_sym] = value if value
    end
    
    # Creates parameters
    params = RubyPsigate::Serializer.new(@request, :header => true).to_xml
    connection = RubyPsigate::Connection.new(self.class.credential.endpoint)
    response = connection.post(params)
    response = Response.new(response)
    self.response = response
    
    if response.success? && response.returncode == "RPA-0015"
      result = response.serialno
    else
      result = false
    end
  rescue ConnectionError => e
    result = false
  end
  result
end

#attributes=(attributes = {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/ruby_psigate/account.rb', line 27

def attributes=(attributes={})
  attributes.each_pair do |attribute, value|
    if self.respond_to?(attribute)
      setter = "#{attribute}=".to_sym
      self.send(setter, value)
    end
  end
end

#delete_payment_methodObject



130
131
132
# File 'lib/ruby_psigate/account.rb', line 130

def delete_payment_method
  
end

#disable_payment_methodObject



138
139
140
# File 'lib/ruby_psigate/account.rb', line 138

def disable_payment_method
  
end

#enable_payment_methodObject



134
135
136
# File 'lib/ruby_psigate/account.rb', line 134

def enable_payment_method
  
end

#new_record?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/ruby_psigate/account.rb', line 36

def new_record?
  status.nil? ? true : false
end

#saveObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_psigate/account.rb', line 40

def save
  begin
    # Action
    action = new_record? ? "AMA01" : "AMA02"
    @request[:Request][:Action] = action

    if new_record?
      # Account Details
      @request[:Request][:Account] = {}
      %w( AccountID Name Company Address1 Address2 City Province Postalcode Country Phone Fax Email Comments ).each do |a|
        value = self.send((a.downcase).to_sym)
        @request[:Request][:Account][a.to_sym] = value if value
      end
        
      # Credit Card
      if !credit_card.nil? && credit_card.valid?
        @request[:Request][:Account][:CardInfo] = {}
        %w( CardHolder CardNumber CardExpMonth CardExpYear ).each do |c|
          value = credit_card.send((c.downcase).to_sym)
          @request[:Request][:Account][:CardInfo][c.to_sym] = value if value
        end
      end
    else # Is an update
      # Set account ID
      @request[:Request][:Condition] = {:AccountID => accountid}
      
      @request[:Request][:Update] = {}
      %w( Name Company Address1 Address2 City Province Postalcode Country Phone Fax Email Comments ).each do |a|
        value = self.send((a.downcase).to_sym)
        @request[:Request][:Update][a.to_sym] = value if value
      end
    end
    
    # Creates parameters
    params = RubyPsigate::Serializer.new(@request, :header => true).to_xml
    connection = RubyPsigate::Connection.new(self.class.credential.endpoint)
    response = connection.post(params)
    response = Response.new(response)
    self.response = response

    if response.success? && (response.returncode == "RPA-0000" && new_record?) || (response.returncode == "RPA-0022" && !new_record?)
      %w( accountid name company address1 address2 city province postalcode country phone fax comments ).each do |attribute|
        self.send("#{attribute}=".to_sym, response.send(attribute.to_sym))
      end          
      result = true
    else
      self.send(:error=, response.returnmessage)
      result = false
    end
  rescue ConnectionError => e
    result = false
  end
  result
end