worldpay 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ba5ac557e47c95a2cbcd40e6cd9db6a9ed0ae91c
4
+ data.tar.gz: 7213b0ca02e188b1b00cca6b6ea5244f84c424f7
5
+ SHA512:
6
+ metadata.gz: b085b2dbca8cdb6ce370a73cfdb7bd615a167ce6b2061ebc0391c28ee8b22dff9058cfe1a7e699f7dfb0587da5a20fe7223a25a3f8f0a3b3d859f8725a0e9407
7
+ data.tar.gz: 587ab42bd9a536445d0279f8679f08ffaf5a92dca80f45d30dbeaec002172d5949c933fec7cc2bfee348ec8a50a657accfe7326eed6debd011336882ed631524
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /gem/Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in worldpay.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ worldpay (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.3.2)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.6)
16
+ rake
17
+ worldpay!
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 ao
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Worldpay
2
+
3
+ Use Worldpay/Clearwater with this gem
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'worldpay'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install worldpay
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "worldpay"
25
+ wp = Worldpay.new('api_key', 3)
26
+ wp.Order::createOrder({
27
+ 'token' => 'worldpay-token',
28
+ 'orderDescription' => 'order description',
29
+ 'amount' => 2000,
30
+ 'currencyCode' => 'GBP',
31
+ 'name' => 'name',
32
+ 'orderType' => 'order type',
33
+ 'customerOrderCode' => 'customer order code'
34
+ })
35
+ ```
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/ao/clearwater-lib-ruby/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList["test/**/*_test.rb"]
7
+ t.verbose = true
8
+ end
9
+
10
+ task default: :test
11
+
12
+ task :console do
13
+ exec "irb -r worldpay -I ./lib"
14
+ end
data/lib/worldpay.rb ADDED
@@ -0,0 +1,279 @@
1
+ require "uri"
2
+ require "net/http"
3
+ require "net/https"
4
+ require "json"
5
+
6
+
7
+ class Worldpay
8
+
9
+ @service_key = ''
10
+ @timeout = 0
11
+ @disable_ssl = false
12
+ @endpoint = ''
13
+
14
+ @errors = {}
15
+ @raise_errors = true
16
+
17
+
18
+ def initialize(service_key='', timeout=3, raise_errors=true)
19
+ if service_key.empty?
20
+ raise "Please set a Service Key"
21
+ else
22
+ @service_key = service_key
23
+ @timeout = timeout
24
+
25
+ @disable_ssl = false
26
+ @endpoint = 'prodb-api.worldpay.io'
27
+
28
+ @errors = {
29
+ 'ip' => 'Invalid parameters',
30
+ 'cine' => 'php_curl was not found',
31
+ 'to' => 'Request timed out',
32
+ 'nf' => 'Not found',
33
+ 'apierror' => 'API Error',
34
+ 'uanv' => 'Worldpay is currently unavailable, please try again later',
35
+ 'contact' => 'Error contacting Worldpay, please try again later',
36
+ 'ssl' => 'You must enable SSL check in production mode',
37
+ 'verify' => 'Worldpay not verifiying SSL connection',
38
+ 'orderInput' => {
39
+ 'token' => 'No token found',
40
+ 'orderDescription' => 'No order_description found',
41
+ 'amount' => 'No amount found, or it is not a whole number',
42
+ 'currencyCode' => 'No currency_code found',
43
+ 'name' => 'No name found',
44
+ 'billingAddress' => 'No billing_address found'
45
+ },
46
+ 'notificationPost' => 'Notification Error: Not a post',
47
+ 'notificationUnknown' => 'Notification Error: Cannot be processed',
48
+ 'refund' => {
49
+ 'ordercode' => 'No order code entered'
50
+ },
51
+ 'json' => 'JSON could not be decoded',
52
+ 'key' => 'Please enter your service key',
53
+ 'sslerror' => 'Worldpay SSL certificate could not be validated'
54
+ }
55
+ @raise_errors = raise_errors
56
+ end
57
+ end
58
+
59
+
60
+ #Disable SSL Check ~ Use only for testing!
61
+ def disableSSLCheck(disable = false)
62
+ @disable_ssl = disable
63
+
64
+ end
65
+
66
+ #Set timeout
67
+ def setTimeout(timeout = 3)
68
+ @timeout = timeout
69
+ end
70
+
71
+
72
+ #ERRORS
73
+ def onError(error, message=false, code=nil, httpStatusCode=nil, description=nil, customCode=nil)
74
+
75
+
76
+
77
+ =begin
78
+ if (message)
79
+ message = ' - '+message
80
+ end
81
+ =end
82
+
83
+ if (@raise_errors)
84
+ raise message.to_s
85
+ else
86
+ p message.to_s
87
+ end
88
+ end
89
+
90
+
91
+
92
+
93
+
94
+ #ORDERS
95
+ def checkOrderInput(order)
96
+ errors2 = []
97
+
98
+ if (!order.any?)
99
+ onError('ip')
100
+ end
101
+ if (order['token']==nil)
102
+ errors2 << @errors['orderInput']['token']
103
+ end
104
+ if (order['orderDescription']==nil)
105
+ errors2 << @errors['orderInput']['orderDescription']
106
+ end
107
+ if (order['amount']==nil)
108
+ errors2 << @errors['orderInput']['amount']
109
+ end
110
+ if (order['currencyCode']==nil)
111
+ errors2 << @errors['orderInput']['currencyCode']
112
+ end
113
+ if (order['name']==nil)
114
+ errors2 << @errors['orderInput']['name']
115
+ end
116
+ if (order['billingAddress']==nil)
117
+ errors2 << @errors['orderInput']['billingAddress']
118
+ end
119
+
120
+ if (errors2.length > 0)
121
+ onError('ip', errors2.join(', '))
122
+ end
123
+ end
124
+
125
+ def createOrder(order={})
126
+
127
+ self.checkOrderInput(order)
128
+
129
+ defaults = {
130
+ 'orderType' => 'ECOM',
131
+ 'customerIdentifiers' => nil,
132
+ 'billingAddress' => nil
133
+ }
134
+
135
+ order = defaults.merge(order)
136
+
137
+ json = {
138
+ 'token' => order['token'],
139
+ 'orderDescription' => order['orderDescription'],
140
+ 'amount' => order['amount'],
141
+ 'currencyCode' => order['currencyCode'],
142
+ 'name' => order['name'],
143
+ 'orderType' => order['orderType'],
144
+ 'billingAddress' => order['billingAddress'],
145
+ 'customerOrderCode' => order['customerOrderCode'],
146
+ 'customerIdentifiers' => order['customerIdentifiers']
147
+ }.to_json
148
+
149
+ response = sendRequest('orders', json, true)
150
+
151
+ if (response['body']['orderCode'])
152
+ #success
153
+ return response
154
+ else
155
+ onError('apierror', response.to_s)
156
+ end
157
+
158
+ end
159
+
160
+ #Refund Worldpay order
161
+ def refundOrder(orderCode=false)
162
+ if (orderCode==false || orderCode!=orderCode.to_s)
163
+ onError('ip', @errors['refund']['ordercode'])
164
+ end
165
+ sendRequest('orders/'+orderCode+'/refund')
166
+ end
167
+
168
+
169
+
170
+
171
+
172
+ #REQUESTS
173
+ def sendRequest(action='', json=false, expectResponse=false, method='POST')
174
+ url = @endpoint
175
+
176
+ net = Net::HTTP.new(url, "443")
177
+ if (method=="POST")
178
+ request = Net::HTTP::Post.new('/v1/'+action)
179
+ elsif (method=="GET")
180
+ request = Net::HTTP::Get.new('/v1/'+action)
181
+ elsif (method=="PUT")
182
+ request = Net::HTTP::Put.new('/v1/'+action)
183
+ end
184
+
185
+ net.use_ssl = true
186
+ net.verify_mode = @disable_ssl ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
187
+
188
+ #request.set_form_data(JSON.parse(json))
189
+ if (json != false)
190
+ request.body = json
191
+ end
192
+ request.add_field("Authorization", @service_key)
193
+ request.add_field("Content-Type", "application/json")
194
+ if (json != false)
195
+ request.add_field("Content-Length", json.length)
196
+ end
197
+
198
+ net.set_debug_output $stdout #useful to see the raw messages going over the wire
199
+ net.read_timeout = @timeout
200
+ net.open_timeout = @timeout
201
+
202
+ response = net.start do |http|
203
+ http.request(request)
204
+ end
205
+ #puts response.code
206
+ #puts response.read_body
207
+
208
+ return {
209
+ "code"=>response.code,
210
+ "body"=>(JSON.parse(response.read_body) if response.read_body && response.read_body.length >= 2)
211
+ }
212
+
213
+ end
214
+
215
+
216
+
217
+
218
+
219
+
220
+ #RESPONSES
221
+ def handleResponse(response)
222
+ response = response.to_json
223
+ end
224
+
225
+
226
+
227
+
228
+
229
+ #TOKENS
230
+ #Get card details from Worldpay token
231
+ def getStoredCardDetails(token=false)
232
+ if (token==false || token!=token.to_s)
233
+ onError('ip', @erro
234
+ rs['orderInput']['token'])
235
+ end
236
+
237
+ response = sendRequest('tokens/'+token, false, true, 'GET')
238
+
239
+ if (response['body']['paymentMethod'])
240
+ return response['body']['paymentMethod']
241
+ else
242
+ #onError('apierror')
243
+ return ""
244
+ end
245
+ end
246
+
247
+
248
+
249
+
250
+
251
+ #WEBHOOKS
252
+ #Process Worldpay webhooks
253
+ def processWebhook()
254
+
255
+ while line = $stdin.gets
256
+ puts line
257
+ end
258
+
259
+ exit
260
+
261
+ if (request.POST['orderCode'] && request.POST['paymentStatus'])
262
+ json = handleResponse(res)
263
+
264
+ if (json == nil || json == false)
265
+ onError('notificationUnknown')
266
+ end
267
+
268
+ return json
269
+ else
270
+ onError('notificationUnknown')
271
+ end
272
+
273
+ end
274
+
275
+
276
+
277
+
278
+
279
+ end
@@ -0,0 +1,2 @@
1
+ require "minitest/autorun"
2
+ require "worldpay"
@@ -0,0 +1,29 @@
1
+ require "test_helper"
2
+
3
+ class WorldpayTest < MiniTest::Unit::TestCase
4
+
5
+ # Version
6
+ def test_version
7
+ assert_equal "0.0.1", Worldpay::VERSION
8
+ end
9
+
10
+ # Service Key
11
+ def test_servicekey
12
+ Worldpay::service_key = 'yourkey'
13
+ assert_equal 'yourkey', Worldpay::service_key
14
+ end
15
+
16
+ # Create Order
17
+ def test_createorder
18
+
19
+ order = {
20
+
21
+ }
22
+
23
+ Worldpay::service_key = 'yourkey'
24
+ res = Worldpay::Order::createOrder(order)
25
+
26
+ assert_equal 'fds', res
27
+ end
28
+
29
+ end
data/worldpay.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "worldpay"
7
+ spec.version = "0.0.2"
8
+ spec.authors = ["Andrew Odendaal"]
9
+ spec.email = ["andrew.odendaal@worldpay.com"]
10
+ spec.summary = %q{developer.worldpay.com ruby lib.}
11
+ spec.description = %q{This is the ruby library used on developer.worldpay.com.}
12
+ spec.homepage = "https://developer.worldpay.com"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ end
23
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: worldpay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Odendaal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This is the ruby library used on developer.worldpay.com.
42
+ email:
43
+ - andrew.odendaal@worldpay.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - Guardfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/worldpay.rb
56
+ - test/test_helper.rb
57
+ - test/worldpay_test.rb
58
+ - worldpay.gemspec
59
+ homepage: https://developer.worldpay.com
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: developer.worldpay.com ruby lib.
83
+ test_files:
84
+ - test/test_helper.rb
85
+ - test/worldpay_test.rb