eway 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +0 -0
  2. data/Rakefile +14 -0
  3. data/eway.gemspec +33 -0
  4. data/lib/eway.rb +302 -0
  5. metadata +120 -0
File without changes
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('innowhite', '1.0.0') do |p|
6
+ p.description = "Eway Api"
7
+ p.url = "http://github.com/bainur/eway"
8
+ p.author = "bainur"
9
+ p.email = "inoe.bainur@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{eway}
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = [%q{bainur}]
9
+ s.date = %q{2011-08-11}
10
+ s.description = %q{Eway payment Api}
11
+ s.email = %q{inoe.bainur@gmail.com}
12
+ s.extra_rdoc_files = [%q{README.rdoc}, %q{lib/eway.rb}]
13
+ s.files = [%q{README.rdoc}, %q{Rakefile}, %q{lib/eway.rb},%q{eway.gemspec}]
14
+ s.homepage = %q{http://github.com/bainur/eway}
15
+ s.rdoc_options = [%q{--line-numbers}, %q{--inline-source}, %q{--title}, %q{eway}, %q{--main}, %q{README.rdoc}]
16
+ s.require_paths = [%q{lib}]
17
+ s.rubyforge_project = %q{ewat}
18
+ s.rubygems_version = %q{1.9.2}
19
+ s.summary = %q{Eway Api}
20
+ s.add_dependency 'nokogiri', '1.5.5'
21
+ s.add_dependency 'rest-client', '1.6.7'
22
+ s.add_dependency 'savon', '1.2.0'
23
+ s.add_dependency 'activemerchant', '1.28.0'
24
+
25
+ if s.respond_to? :specification_version then
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
29
+ else
30
+ end
31
+ else
32
+ end
33
+ end
@@ -0,0 +1,302 @@
1
+ class Eway
2
+ attr_accessor :result, :method_type, :access_code, :access_code_response,
3
+ :first_name, :last_name, :form_action_url
4
+
5
+
6
+ def initialize
7
+ if Rails.env == "development"
8
+ settings = YAML.load_file('config/eway.yml')
9
+ elsif Rails.env == "production"
10
+ settings = YAML.load_file('config/eway.yml')
11
+ end
12
+ @xml_url = settings["eway"]["xml_url"]
13
+ @json_url = settings["eway"]["json_url"]
14
+ @soap_url = settings["eway"]["soap_url"]
15
+ @username = settings["eway"]["username"]
16
+ @password = settings["eway"]["password"]
17
+ @active_merchant_login = settings["eway"]["active_merchant_login"]
18
+ @active_merchant_password = settings["eway"]["active_merchant_password"]
19
+ end
20
+
21
+ #### example params xml, json , and soap, and it will converted based on method type
22
+ ##{"request_method_options"=>"xml",
23
+ #"txtTokenCustomerID"=>"NULL", "RedirectUrl"=>"http://localhost:3000/results",
24
+ #"IPAdress"=>"127.0.0.1",
25
+ #"Payment"=>{"TotalAmount"=>"", "CurrencyCode"=>"AUD",
26
+ #"InvoiceNumber"=>"Inv 21540", "InvoiceReference"=>"513456",
27
+ #"InvoiceDescription"=>"Individual Invoice Description"},
28
+ #"request_options"=>"", "option1"=>"", "option2"=>"", "option3"=>"",
29
+ #"Customer"=>{"Title"=>"Mr.", "CustomerReference"=>"A12345",
30
+ #"FirstName"=>"John", "LastName"=>"Doe", "CompanyName"=>"WEB ACTIVE",
31
+ #"JobDescription"=>"Developer", "Street1"=>"15 Smith St", "City"=>"Phillip",
32
+ #"State"=>"ACT", "PostalCode"=>"2602", "Country"=>"au", "Email"=>"",
33
+ #"Phone"=>"1800 10 10 65", "Mobile"=>"1800 10 10 65"},
34
+ #"Method"=>"CreateTokenCustomer", "commit"=>"submit"}
35
+
36
+ def create_customer_token(method_type, params = {})
37
+ self.method_type = method_type
38
+
39
+ self.result = case method_type
40
+ when "xml"
41
+ customer_token_xml(params)
42
+ when "soap"
43
+ customer_token_soap(params)
44
+ when "json"
45
+ customer_token_json(params)
46
+ end
47
+ end
48
+
49
+ def customer_token_xml(params)
50
+ tmp = params.except("utf8", "authenticity_token", "action", "controller", "commit","request_method_options")
51
+ xml_hash = {"CreateAccessCode" => tmp} if self.method_type == "xml"
52
+
53
+ xml = params
54
+ xml = xml_hash.to_xml.gsub("<hash>","").gsub("</hash>","") if params.class == Hash
55
+
56
+ c = Curl::Easy.http_post("https://api.sandbox.ewaypayments.com/AccessCodes") do |curl|
57
+ curl.headers["Content-Type"] = "text/xml"
58
+ curl.http_auth_types = :basic
59
+ curl.username = @username
60
+ curl.password = @password
61
+ curl.verbose = true
62
+ end
63
+
64
+ c.post(xml)
65
+ self.result = c.body_str
66
+ end
67
+
68
+ def customer_token_json(params)
69
+ tmp = params.except("utf8", "authenticity_token", "action", "controller", "commit","request_method_options")
70
+ json_call = params
71
+ json_call = tmp.to_json if params.class == Hash
72
+
73
+ c = Curl::Easy.http_post(@json_url,json_call) do |curl|
74
+ curl.headers["Accept"] = "application/json"
75
+ curl.headers["Content-Type"] = "application/json"
76
+ curl.http_auth_types = :basic
77
+ curl.username = @username
78
+ curl.password = @password
79
+ curl.verbose = true
80
+ curl.on_complete {|response, err|
81
+ code = response.body_str
82
+ }
83
+ end
84
+
85
+ self.result = code
86
+ end
87
+
88
+ def customer_token_soap(params)
89
+ xm = <<-EOF
90
+ <?xml version="1.0" encoding="utf-8"?>
91
+ <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
92
+ <soap:Body>
93
+ <CustomerIP>10.10.10.1</CustomerIP>
94
+ <Method>CreateTokenCustomer</Method>
95
+ <CreateAccessCode xmlns="https://api.ewaypayments.com/">
96
+ <request>
97
+ <RedirectUrl>#{params['RedirectUrl']}</RedirectUrl>
98
+ <Customer>
99
+ <Reference>#{params["Customer"]["Reference"]}</Reference>
100
+ <Title>#{params["Customer"]["Title"]}</Title>
101
+ <FirstName>#{params["Customer"]["FirstName"]}</FirstName>
102
+ <LastName>#{params["Customer"]["LastName"]}</LastName>
103
+ <CompanyName>#{params["Customer"]["CompanyName"]}</CompanyName>
104
+ <JobDescription>#{params["Customer"]["JobDescription"]}</JobDescription>
105
+ <Street1>#{params["Customer"]["JobDescription"]}</Street1>
106
+ <City>#{params["Customer"]["Citys"]}</City>
107
+ <State>#{params["Customer"]["State"]}</State>
108
+ <PostalCode>#{params["Customer"]["PostalCode"]}</PostalCode>
109
+ <Country>#{params["Customer"]["Country"]}</Country>
110
+ <Phone>#{params["Customer"]["Phone"]}</Phone>
111
+ <Mobile>#{params["Customer"]["Mobile"]}</Mobile>
112
+ </Customer>
113
+ <Payment>
114
+ <TotalAmount>#{params["Payment"]["TotalAmount"].nil?? '100' : "100"}</TotalAmount>
115
+ <InvoiceNumber>#{params["Payment"]["InvoiceNumber"]}</InvoiceNumber>
116
+ <InvoiceDescription>#{params["Payment"]["InvoiceDescription"]}</InvoiceDescription>
117
+ <InvoiceReference>#{params["Payment"]["InvoiceReference"]}</InvoiceReference>
118
+ <CurrencyCode>#{params["Payment"]["CurrencyCode"]}</CurrencyCode>
119
+ </Payment>
120
+ </request>
121
+ </CreateAccessCode>
122
+ </soap:Body>
123
+ </soap:Envelope>
124
+ EOF
125
+ client = Savon::Client.new(@soap_url) do
126
+ http.auth.basic "44DD7C70Jre1dVgIsULcEyi+A+/cX9V5SAHkIiyVdWrHRG2tZm0rdintfZz85Pa/kGwq/1", "Abcd1234"
127
+
128
+ end
129
+
130
+ puts client.wsdl.soap_actions
131
+
132
+ response = client.request(:create_access_code) do
133
+ soap.xml = xm
134
+ end
135
+
136
+ @res = response.to_hash
137
+ self.result = response.to_hash
138
+ end
139
+
140
+ def payment
141
+ case self.method_type
142
+ when "xml"
143
+ @result = Nokogiri::XML.parse(self.result)
144
+
145
+ @access_code = @result.xpath('//CreateAccessCodeResponse/AccessCode').text
146
+ @customer_data = @result.xpath('//CreateAccessCodeResponse/Customer').children
147
+ @customer_arr = @customer_data.map{|x| x.name + "," + x.text}
148
+ @first_name = @customer_data.xpath('//FirstName').text
149
+ @last_name = @customer_data.xpath('//LastName').text
150
+
151
+ @form_action = @result.xpath('//CreateAccessCodeResponse/FormActionURL').text
152
+ @payment_data = @result.xpath('//CreateAccessCodeResponse/Payment').children
153
+ @payment_arr = @payment_data.map{|x| x.name + "," + x.text}
154
+
155
+ when "json"
156
+ @result = JSON.parse(self.result)
157
+
158
+ @access_code = @result["AccessCode"]
159
+ @customer_data = @result["Customer"]
160
+ @customer_arr = @customer_data
161
+ @first_name = @customer_data["FirstName"]
162
+ @last_name = @customer_data["LastName"]
163
+
164
+ @form_action = @result["FormActionURL"]
165
+ @payment_data = @result["Payment"]
166
+ @payment_arr = @payment_data
167
+ when "soap"
168
+ @result = self.result[:create_access_code_response][:create_access_code_result]
169
+ @access_code = @result["AccessCode".underscore.to_sym]
170
+ @customer_data = @result["Customer".underscore.to_sym]
171
+ @customer_arr = @customer_data
172
+ @first_name = @customer_data["FirstName".underscore.to_sym]
173
+ @last_name = @customer_data["LastName".underscore.to_sym]
174
+
175
+ @form_action = @result["FormActionURL".underscore.to_sym]
176
+ @payment_data = @result["Payment".underscore.to_sym]
177
+ @payment_arr = @payment_data
178
+ end
179
+ self.access_code = @access_code
180
+ self.first_name = @first_name
181
+ self.last_name = @last_name
182
+ self.form_action_url = @form_action
183
+ end
184
+
185
+
186
+ def access_code_result
187
+ c = Curl::Easy.http_get("https://api.sandbox.ewaypayments.com/AccessCode/#{self.access_code}") do |curl|
188
+ curl.http_auth_types = :basic
189
+ curl.username = @username
190
+ curl.password = @password
191
+ curl.verbose = true
192
+ end
193
+ c.perform
194
+ @res = c.body_str
195
+ self.access_code_response = @res
196
+ end
197
+
198
+ ##
199
+ # Example params
200
+ {"EWAY_CARDNAME" => 'TestUser',
201
+ 'EWAY_CARDNUMBER' => "4444333322221111", "EWAY_CARDEXPIRYMONTH" => '09',
202
+ 'EWAY_CARDEXPIRYYEAR' => '2012', "EWAY_CARDSTARTMONTH" => '01',
203
+ 'EWAY_CARDSTARTYEAR'=> '12',
204
+ 'EWAY_CARDISSUENUMBER' => '22','EWAY_CARDCVN' => '123'}
205
+ # }
206
+ ##
207
+ def post_cc(params)
208
+ params.merge!(
209
+ {"first_name" => self.first_name, "last_name" => self.last_name,
210
+ "EWAY_ACCESSCODE" => self.access_code}
211
+ )
212
+ ### post to Eway api
213
+ c = Curl::Easy.http_post(self.form_action_url) do |curl|
214
+ #curl.headers["Content-Type"] = "text/xml"
215
+ curl.http_auth_types = :basic
216
+ curl.username = @username
217
+ curl.password = @password
218
+ curl.verbose = true
219
+ end
220
+ c.post(params)
221
+ end
222
+
223
+ def post_shopify(params)
224
+ ## post to shopify
225
+ if post_cc(params) ## if success post to eway payment
226
+ ActiveMerchant::Billing::Base.mode = :test
227
+
228
+ gateway = ActiveMerchant::Billing::TrustCommerceGateway.new(
229
+ :login => @active_merchant_login,
230
+ :password => @active_merchant_password)
231
+
232
+ # ActiveMerchant accepts all amounts as Integer values in cents
233
+ amount = JSON.parse(self.access_code_response)["TotalAmount"]# $10.00
234
+
235
+ # The card verification value is also known as CVV2, CVC2, or CID
236
+ credit_card = ActiveMerchant::Billing::CreditCard.new(
237
+ :first_name => params["first_name"],
238
+ :last_name => params["last_name"],
239
+ :number => params["EWAY_CARDNUMBER"],
240
+ :month => params["EWAY_CARDEXPIRYMONTH"],
241
+ :year => params["EWAY_CARDEXPIRYYEAR"],
242
+ :verification_value => params["EWAY_CARDCVN"])
243
+
244
+ # Validating the card automatically detects the card type
245
+ if credit_card.valid?
246
+ # Capture $10 from the credit card
247
+ response = gateway.purchase(amount, credit_card)
248
+
249
+ if response.success?
250
+ #"/succesfully_submitted"
251
+ return :json => {:success => true, :text_message => "$#{sprintf("%.2f", amount)} to the credit card #{credit_card.display_number}"}
252
+ else
253
+ return :json => {:success => false, :text_message => "#{response}"}
254
+ end
255
+ else
256
+
257
+ end
258
+ end
259
+ end
260
+
261
+
262
+ def soap_format(params)
263
+ xm = <<-EOF
264
+ <?xml version="1.0" encoding="utf-8"?>
265
+ <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
266
+ <soap:Body>
267
+ <CustomerIP>10.10.10.1</CustomerIP>
268
+ <Method>CreateTokenCustomer</Method>
269
+ <CreateAccessCode xmlns="https://api.ewaypayments.com/">
270
+ <request>
271
+ <RedirectUrl>#{params['RedirectUrl']}</RedirectUrl>
272
+ <Customer>
273
+ <Reference>#{params["Customer"]["Reference"]}</Reference>
274
+ <Title>#{params["Customer"]["Title"]}</Title>
275
+ <FirstName>#{params["Customer"]["FirstName"]}</FirstName>
276
+ <LastName>#{params["Customer"]["LastName"]}</LastName>
277
+ <CompanyName>#{params["Customer"]["CompanyName"]}</CompanyName>
278
+ <JobDescription>#{params["Customer"]["JobDescription"]}</JobDescription>
279
+ <Street1>#{params["Customer"]["JobDescription"]}</Street1>
280
+ <City>#{params["Customer"]["Citys"]}</City>
281
+ <State>#{params["Customer"]["State"]}</State>
282
+ <PostalCode>#{params["Customer"]["PostalCode"]}</PostalCode>
283
+ <Country>#{params["Customer"]["Country"]}</Country>
284
+ <Phone>#{params["Customer"]["Phone"]}</Phone>
285
+ <Mobile>#{params["Customer"]["Mobile"]}</Mobile>
286
+ </Customer>
287
+ <Payment>
288
+ <TotalAmount>#{params["Payment"]["TotalAmount"].nil?? '100' : "100"}</TotalAmount>
289
+ <InvoiceNumber>#{params["Payment"]["InvoiceNumber"]}</InvoiceNumber>
290
+ <InvoiceDescription>#{params["Payment"]["InvoiceDescription"]}</InvoiceDescription>
291
+ <InvoiceReference>#{params["Payment"]["InvoiceReference"]}</InvoiceReference>
292
+ <CurrencyCode>#{params["Payment"]["CurrencyCode"]}</CurrencyCode>
293
+ </Payment>
294
+ </request>
295
+ </CreateAccessCode>
296
+ </soap:Body>
297
+ </soap:Envelope>
298
+ EOF
299
+
300
+ return xm
301
+ end
302
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eway
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - bainur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: savon
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: activemerchant
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.28.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.28.0
78
+ description: Eway payment Api
79
+ email: inoe.bainur@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - README.rdoc
84
+ - lib/eway.rb
85
+ files:
86
+ - README.rdoc
87
+ - Rakefile
88
+ - lib/eway.rb
89
+ - eway.gemspec
90
+ homepage: http://github.com/bainur/eway
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options:
94
+ - --line-numbers
95
+ - --inline-source
96
+ - --title
97
+ - eway
98
+ - --main
99
+ - README.rdoc
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '1.2'
114
+ requirements: []
115
+ rubyforge_project: ewat
116
+ rubygems_version: 1.8.21
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Eway Api
120
+ test_files: []