Class: Eway

Inherits:
Object
  • Object
show all
Defined in:
lib/eway.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEway

Returns a new instance of Eway.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/eway.rb', line 6

def initialize
  if Rails.env == "development"
    settings = YAML.load_file('config/eway.yml')
  elsif Rails.env == "production"
    settings = YAML.load_file('config/eway.yml')
  end
  @xml_url = settings["eway"]["xml_url"]
  @json_url = settings["eway"]["json_url"]
  @soap_url = settings["eway"]["soap_url"]      
  @username = settings["eway"]["username"]      
  @password = settings["eway"]["password"]   
  @active_merchant_login = settings["eway"]["active_merchant_login"]   
  @active_merchant_password = settings["eway"]["active_merchant_password"]   
end

Instance Attribute Details

#access_codeObject

Returns the value of attribute access_code.



2
3
4
# File 'lib/eway.rb', line 2

def access_code
  @access_code
end

#access_code_responseObject

Returns the value of attribute access_code_response.



2
3
4
# File 'lib/eway.rb', line 2

def access_code_response
  @access_code_response
end

#first_nameObject

Returns the value of attribute first_name.



2
3
4
# File 'lib/eway.rb', line 2

def first_name
  @first_name
end

#form_action_urlObject

Returns the value of attribute form_action_url.



2
3
4
# File 'lib/eway.rb', line 2

def form_action_url
  @form_action_url
end

#last_nameObject

Returns the value of attribute last_name.



2
3
4
# File 'lib/eway.rb', line 2

def last_name
  @last_name
end

#method_typeObject

Returns the value of attribute method_type.



2
3
4
# File 'lib/eway.rb', line 2

def method_type
  @method_type
end

#resultObject

Returns the value of attribute result.



2
3
4
# File 'lib/eway.rb', line 2

def result
  @result
end

Instance Method Details

#access_code_resultObject



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/eway.rb', line 186

def access_code_result    
  c = Curl::Easy.http_get("https://api.sandbox.ewaypayments.com/AccessCode/#{self.access_code}") do |curl|
    curl.http_auth_types = :basic
    curl.username = @username
    curl.password = @password
    curl.verbose = true
  end
  c.perform
  @res = c.body_str
  self.access_code_response = @res   
end

#create_customer_token(method_type, params = {}) ⇒ Object

example params xml, json , and soap, and it will converted based on method type “txtTokenCustomerID”=>“NULL”, “RedirectUrl”=>“localhost:3000/results”, “IPAdress”=>“127.0.0.1”, “Payment”=>{“TotalAmount”=>“”, “CurrencyCode”=>“AUD”, “InvoiceNumber”=>“Inv 21540”, “InvoiceReference”=>“513456”, “InvoiceDescription”=>“Individual Invoice Description”, “request_options”=>“”, “option1”=>“”, “option2”=>“”, “option3”=>“”, “Customer”=>“CustomerReference”=>“A12345”, “FirstName”=>“John”, “LastName”=>“Doe”, “CompanyName”=>“WEB ACTIVE”, “JobDescription”=>“Developer”, “Street1”=>“15 Smith St”, “City”=>“Phillip”, “State”=>“ACT”, “PostalCode”=>“2602”, “Country”=>“au”, “Email”=>“”, “Phone”=>“1800 10 10 65”, “Mobile”=>“1800 10 10 65”, “Method”=>“CreateTokenCustomer”, “commit”=>“submit”}



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/eway.rb', line 36

def create_customer_token(method_type, params = {})
  self.method_type = method_type
  
  self.result = case method_type
  when "xml"
    customer_token_xml(params)
  when "soap"
    customer_token_soap(params)
  when "json"
    customer_token_json(params)
  end
end

#customer_token_json(params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/eway.rb', line 68

def customer_token_json(params)    
  tmp = params.except("utf8", "authenticity_token", "action", "controller", "commit","request_method_options")    
  json_call = params
  json_call = tmp.to_json if params.class == Hash
  
  c = Curl::Easy.http_post(@json_url,json_call) do |curl|
    curl.headers["Accept"] = "application/json"
    curl.headers["Content-Type"] = "application/json"
    curl.http_auth_types = :basic
    curl.username = @username
    curl.password = @password
    curl.verbose = true
    curl.on_complete {|response, err|
      code = response.body_str
    }
  end      
        
  self.result = code
end

#customer_token_soap(params) ⇒ Object



88
89
90
91
92
93
94
95
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
129
130
131
132
133
134
135
136
137
138
# File 'lib/eway.rb', line 88

def customer_token_soap(params)        
  xm = <<-EOF
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
      <CustomerIP>10.10.10.1</CustomerIP>
      <Method>CreateTokenCustomer</Method>
  <CreateAccessCode xmlns="https://api.ewaypayments.com/">
    <request>
      <RedirectUrl>#{params['RedirectUrl']}</RedirectUrl>
      <Customer>          
        <Reference>#{params["Customer"]["Reference"]}</Reference>
        <Title>#{params["Customer"]["Title"]}</Title>
        <FirstName>#{params["Customer"]["FirstName"]}</FirstName>
        <LastName>#{params["Customer"]["LastName"]}</LastName>
        <CompanyName>#{params["Customer"]["CompanyName"]}</CompanyName>
        <JobDescription>#{params["Customer"]["JobDescription"]}</JobDescription>
        <Street1>#{params["Customer"]["JobDescription"]}</Street1>
        <City>#{params["Customer"]["Citys"]}</City>
        <State>#{params["Customer"]["State"]}</State>
        <PostalCode>#{params["Customer"]["PostalCode"]}</PostalCode>
        <Country>#{params["Customer"]["Country"]}</Country>
        <Phone>#{params["Customer"]["Phone"]}</Phone>
        <Mobile>#{params["Customer"]["Mobile"]}</Mobile>
      </Customer>         
      <Payment>
        <TotalAmount>#{params["Payment"]["TotalAmount"].nil?? '100' : "100"}</TotalAmount>
        <InvoiceNumber>#{params["Payment"]["InvoiceNumber"]}</InvoiceNumber>
        <InvoiceDescription>#{params["Payment"]["InvoiceDescription"]}</InvoiceDescription>
        <InvoiceReference>#{params["Payment"]["InvoiceReference"]}</InvoiceReference>
        <CurrencyCode>#{params["Payment"]["CurrencyCode"]}</CurrencyCode>
      </Payment>        
    </request>
  </CreateAccessCode>
</soap:Body>
</soap:Envelope>
  EOF
  client = Savon::Client.new(@soap_url) do
    http.auth.basic "44DD7C70Jre1dVgIsULcEyi+A+/cX9V5SAHkIiyVdWrHRG2tZm0rdintfZz85Pa/kGwq/1", "Abcd1234"
      
  end

  puts client.wsdl.soap_actions

  response = client.request(:create_access_code) do
    soap.xml = xm
  end

  @res = response.to_hash
  self.result = response.to_hash
end

#customer_token_xml(params) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/eway.rb', line 49

def customer_token_xml(params)    
  tmp = params.except("utf8", "authenticity_token", "action", "controller", "commit","request_method_options")
  xml_hash = {"CreateAccessCode" => tmp} if self.method_type == "xml"
  
  xml = params
  xml = xml_hash.to_xml.gsub("<hash>","").gsub("</hash>","") if params.class == Hash
  
  c = Curl::Easy.http_post("https://api.sandbox.ewaypayments.com/AccessCodes") do |curl|
    curl.headers["Content-Type"] = "text/xml"
    curl.http_auth_types = :basic
    curl.username = @username
    curl.password = @password
    curl.verbose = true
  end

  c.post(xml)
  self.result = c.body_str
end

#paymentObject



140
141
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
# File 'lib/eway.rb', line 140

def payment
  case self.method_type
  when "xml"
    @result = Nokogiri::XML.parse(self.result)

    @access_code = @result.xpath('//CreateAccessCodeResponse/AccessCode').text
    @customer_data = @result.xpath('//CreateAccessCodeResponse/Customer').children
    @customer_arr = @customer_data.map{|x| x.name + "," + x.text}
    @first_name = @customer_data.xpath('//FirstName').text
    @last_name = @customer_data.xpath('//LastName').text

    @form_action = @result.xpath('//CreateAccessCodeResponse/FormActionURL').text
    @payment_data  = @result.xpath('//CreateAccessCodeResponse/Payment').children
    @payment_arr = @payment_data.map{|x| x.name + "," + x.text}
    
  when "json"      
    @result = JSON.parse(self.result)

    @access_code = @result["AccessCode"]
    @customer_data = @result["Customer"]      
    @customer_arr = @customer_data
    @first_name = @customer_data["FirstName"]
    @last_name = @customer_data["LastName"]

    @form_action = @result["FormActionURL"]
    @payment_data  = @result["Payment"]
    @payment_arr = @payment_data
  when "soap"      
    @result = self.result[:create_access_code_response][:create_access_code_result]
    @access_code = @result["AccessCode".underscore.to_sym]
    @customer_data = @result["Customer".underscore.to_sym]
    @customer_arr = @customer_data
    @first_name = @customer_data["FirstName".underscore.to_sym]
    @last_name = @customer_data["LastName".underscore.to_sym]

    @form_action = @result["FormActionURL".underscore.to_sym]
    @payment_data  = @result["Payment".underscore.to_sym]
    @payment_arr = @payment_data
  end
  self.access_code = @access_code
  self.first_name = @first_name
  self.last_name = @last_name
  self.form_action_url = @form_action
end

#post_cc(params) ⇒ Object

}



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/eway.rb', line 207

def post_cc(params)
  params.merge!(
    {"first_name" => self.first_name, "last_name" => self.last_name, 
      "EWAY_ACCESSCODE" => self.access_code}
  )
  ### post to Eway api
  c = Curl::Easy.http_post(self.form_action_url) do |curl|
    #curl.headers["Content-Type"] = "text/xml"
    curl.http_auth_types = :basic
    curl.username = @username
    curl.password = @password
    curl.verbose = true
  end
  c.post(params)
end

#post_shopify(params) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/eway.rb', line 223

def post_shopify(params)
  ## post to shopify
  if post_cc(params) ## if success post to eway payment      
    ActiveMerchant::Billing::Base.mode = :test
    
    gateway = ActiveMerchant::Billing::TrustCommerceGateway.new(
      :login => @active_merchant_login,
      :password => @active_merchant_password)
    
    # ActiveMerchant accepts all amounts as Integer values in cents
    amount = JSON.parse(self.access_code_response)["TotalAmount"]# $10.00
    
    # The card verification value is also known as CVV2, CVC2, or CID
    credit_card = ActiveMerchant::Billing::CreditCard.new(
      :first_name         => params["first_name"],
      :last_name          => params["last_name"],
      :number             => params["EWAY_CARDNUMBER"],
      :month              => params["EWAY_CARDEXPIRYMONTH"],
      :year               => params["EWAY_CARDEXPIRYYEAR"],
      :verification_value => params["EWAY_CARDCVN"])
    
    # Validating the card automatically detects the card type
    if credit_card.valid?
      # Capture $10 from the credit card
      response = gateway.purchase(amount, credit_card)
      
      if response.success?          
        #"/succesfully_submitted"
        return :json => {:success => true, :text_message => "$#{sprintf("%.2f", amount)} to the credit card #{credit_card.display_number}"}
      else
        return :json => {:success => false, :text_message => "#{response}"}
      end
    else
      
    end
  end
end

#soap_format(params) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/eway.rb', line 262

def soap_format(params)
  xm = <<-EOF
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
      <CustomerIP>10.10.10.1</CustomerIP>
      <Method>CreateTokenCustomer</Method>
  <CreateAccessCode xmlns="https://api.ewaypayments.com/">
    <request>
      <RedirectUrl>#{params['RedirectUrl']}</RedirectUrl>
      <Customer>          
        <Reference>#{params["Customer"]["Reference"]}</Reference>
        <Title>#{params["Customer"]["Title"]}</Title>
        <FirstName>#{params["Customer"]["FirstName"]}</FirstName>
        <LastName>#{params["Customer"]["LastName"]}</LastName>
        <CompanyName>#{params["Customer"]["CompanyName"]}</CompanyName>
        <JobDescription>#{params["Customer"]["JobDescription"]}</JobDescription>
        <Street1>#{params["Customer"]["JobDescription"]}</Street1>
        <City>#{params["Customer"]["Citys"]}</City>
        <State>#{params["Customer"]["State"]}</State>
        <PostalCode>#{params["Customer"]["PostalCode"]}</PostalCode>
        <Country>#{params["Customer"]["Country"]}</Country>
        <Phone>#{params["Customer"]["Phone"]}</Phone>
        <Mobile>#{params["Customer"]["Mobile"]}</Mobile>
      </Customer>         
      <Payment>
        <TotalAmount>#{params["Payment"]["TotalAmount"].nil?? '100' : "100"}</TotalAmount>
        <InvoiceNumber>#{params["Payment"]["InvoiceNumber"]}</InvoiceNumber>
        <InvoiceDescription>#{params["Payment"]["InvoiceDescription"]}</InvoiceDescription>
        <InvoiceReference>#{params["Payment"]["InvoiceReference"]}</InvoiceReference>
        <CurrencyCode>#{params["Payment"]["CurrencyCode"]}</CurrencyCode>
      </Payment>        
    </request>
  </CreateAccessCode>
</soap:Body>
</soap:Envelope>
  EOF
  
  return xm
end