net_registry 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +28 -0
- data/LICENSE.txt +23 -0
- data/README.md +108 -0
- data/Rakefile +2 -0
- data/lib/net_registry/card.rb +42 -0
- data/lib/net_registry/client.rb +83 -0
- data/lib/net_registry/errors.rb +27 -0
- data/lib/net_registry/helpers.rb +29 -0
- data/lib/net_registry/response.rb +61 -0
- data/lib/net_registry/response_builder.rb +203 -0
- data/lib/net_registry/transaction.rb +48 -0
- data/lib/net_registry/version.rb +27 -0
- data/lib/net_registry.rb +36 -0
- data/net_registry.gemspec +53 -0
- data/spec/card_spec.rb +73 -0
- data/spec/client_spec.rb +265 -0
- data/spec/response_builder_spec.rb +326 -0
- data/spec/response_spec.rb +89 -0
- data/spec/spec_helper.rb +103 -0
- data/spec/transaction_spec.rb +97 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5ff510ba930514e5dd870986c3f6ae24fa305115
|
4
|
+
data.tar.gz: f7ed940f85609d8038cd729070e53401403dc558
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e6dbd45eea7926c888585cb941071cfd02168dca65c210656afd20df0bae116dfe0d66230054d9ec19717167bd9fb259990a0c25a01fc03e62a3d9ecbce95d02
|
7
|
+
data.tar.gz: e2a3be6fd1389c05cb9c16db8288543fb5dbc9550c6e5a4df9976d872760d1f755d955b24d8f6180b48583e6b0ea694ca0f8ebe3fda085f988d82830341db1b5
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Copyright (c) 2015 Car Next Door
|
2
|
+
# Author: Ray Tung
|
3
|
+
#
|
4
|
+
# MIT License
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
source 'https://rubygems.org'
|
26
|
+
|
27
|
+
# Specify your gem's dependencies in net_registry.gemspec
|
28
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2015 Car Next Door
|
2
|
+
Author: Ray Tung
|
3
|
+
|
4
|
+
MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# NetRegistry
|
2
|
+
|
3
|
+
This gem serves as a Ruby wrapper for the NetRegistry Payment Gateway's
|
4
|
+
API. Official documentation can be found [http://www.netregistry.com.au/ee-images/uploads/support/NR-ecom-gateway8.pdf](here)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
``` ruby
|
11
|
+
gem 'net_registry'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
``` BASH
|
17
|
+
$ bundle
|
18
|
+
```
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
``` BASH
|
23
|
+
$ gem install net_registry
|
24
|
+
```
|
25
|
+
|
26
|
+
## Quick start
|
27
|
+
|
28
|
+
``` ruby
|
29
|
+
client = NetRegistry::Client.new(merchant_id: 1234, password: 1234)
|
30
|
+
response = client.purchase(AMOUNT: 100, CCNUM: "111111111111", CCEXP: "10/15")
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
This gem currently support "purchase", "refund", "status", "preauth",
|
36
|
+
and each of these methods take a `hash` parameter. The required keys
|
37
|
+
are specified in the following:
|
38
|
+
|
39
|
+
#### Purchase
|
40
|
+
|
41
|
+
Input:
|
42
|
+
|
43
|
+
``` ruby
|
44
|
+
{
|
45
|
+
AMOUNT: (Integer, Float, or String). The amount you would like to
|
46
|
+
charge. Don't add "$", just the numerical amount.,
|
47
|
+
|
48
|
+
CCNUM: (Integer, String). The credit card number. NO SPACES OR
|
49
|
+
DASH,
|
50
|
+
|
51
|
+
CCEXP: (String). Credit card expiry date. Must be in the format of
|
52
|
+
"mm/yy".
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
Returns: NetRegistry::Response object.
|
57
|
+
|
58
|
+
#### Refund
|
59
|
+
|
60
|
+
Input:
|
61
|
+
``` ruby
|
62
|
+
{
|
63
|
+
AMOUNT: (Integer, Float, or String). The amount you would like to
|
64
|
+
charge. Don't add "$", just the numerical amount.,
|
65
|
+
|
66
|
+
TXNREF: (String). Transaction reference number
|
67
|
+
}
|
68
|
+
```
|
69
|
+
|
70
|
+
Returns: NetRegistry::Response object.
|
71
|
+
|
72
|
+
#### Preauth
|
73
|
+
|
74
|
+
Input:
|
75
|
+
|
76
|
+
``` ruby
|
77
|
+
{
|
78
|
+
AMOUNT: (Integer, Float, or String). The amount you would like to
|
79
|
+
charge. Don't add "$", just the numerical amount.,
|
80
|
+
|
81
|
+
CCNUM: (Integer, String). The credit card number. NO SPACES OR
|
82
|
+
DASH,
|
83
|
+
|
84
|
+
CCEXP: (String). Credit card expiry date. Must be in the format of
|
85
|
+
"mm/yy".
|
86
|
+
}
|
87
|
+
```
|
88
|
+
|
89
|
+
Returns: NetRegistry::Response object.
|
90
|
+
|
91
|
+
#### Status
|
92
|
+
|
93
|
+
Input:
|
94
|
+
``` ruby
|
95
|
+
{
|
96
|
+
TXNREF: (String). Transaction reference number
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
Returns: NetRegistry::Response object.
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
1. Fork it ( https://github.com/carnextdoor/net_registry/fork )
|
105
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
106
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
107
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
108
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Copyright (c) 2015 Car Next Door
|
2
|
+
# Author: Ray Tung
|
3
|
+
#
|
4
|
+
# MIT License
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
module NetRegistry
|
26
|
+
class Card
|
27
|
+
attr_accessor :number,
|
28
|
+
:description,
|
29
|
+
:expiry,
|
30
|
+
:ccv,
|
31
|
+
:type
|
32
|
+
|
33
|
+
def initialize(number: nil, description: nil, expiry: nil, ccv: nil, type: nil)
|
34
|
+
@number = number
|
35
|
+
@description = description
|
36
|
+
@expiry = expiry
|
37
|
+
@ccv = ccv
|
38
|
+
@type = type
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# Copyright (c) 2015 Car Next Door
|
2
|
+
# Author: Ray Tung
|
3
|
+
#
|
4
|
+
# MIT License
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
require "net/http"
|
26
|
+
|
27
|
+
module NetRegistry
|
28
|
+
class Client
|
29
|
+
|
30
|
+
attr_accessor :merchant_id, :password, :base_url, :factory, :url, :login
|
31
|
+
|
32
|
+
def initialize(merchant_id: ENV["NET_REGISTRY_MERCHANT"], password: ENV["NET_REGISTRY_PASSWORD"])
|
33
|
+
@merchant_id, @password = merchant_id, password
|
34
|
+
|
35
|
+
@login = "#{@merchant_id}/#{@password}"
|
36
|
+
@base_url = "https://paygate.ssllock.net/external2.pl"
|
37
|
+
@uri = URI(@base_url)
|
38
|
+
@factory = NetRegistry::ResponseBuilder.new
|
39
|
+
end
|
40
|
+
|
41
|
+
# Alias for
|
42
|
+
# request(COMMAND: "purchase", AMOUNT: 100 ... etc)
|
43
|
+
def purchase(params = {})
|
44
|
+
raise TypeError, "params is not a hash" if !params.is_a?(Hash)
|
45
|
+
request(params.merge!(COMMAND: "purchase"))
|
46
|
+
end
|
47
|
+
|
48
|
+
# Alias for
|
49
|
+
# request(COMMAND: "refund", AMOUNT: 100 ... etc)
|
50
|
+
def refund(params = {})
|
51
|
+
raise TypeError, "params is not a hash" if !params.is_a?(Hash)
|
52
|
+
request(params.merge!(COMMAND: "refund"))
|
53
|
+
end
|
54
|
+
|
55
|
+
# Alias for
|
56
|
+
# request(COMMAND: "status", AMOUNT: 100 ... etc)
|
57
|
+
def status(params = {})
|
58
|
+
raise TypeError, "params is not a hash" if !params.is_a?(Hash)
|
59
|
+
request(params.merge!(COMMAND: "status"))
|
60
|
+
end
|
61
|
+
|
62
|
+
# Alias for
|
63
|
+
# request(COMMAND: "preauth", AMOUNT: 100 ... etc)
|
64
|
+
def preauth(params = {})
|
65
|
+
raise TypeError, "params is not a hash" if !params.is_a?(Hash)
|
66
|
+
request(params.merge!(COMMAND: "preauth"))
|
67
|
+
end
|
68
|
+
|
69
|
+
def request(params = {})
|
70
|
+
raise TypeError, "params is not a hash" if !params.is_a?(Hash)
|
71
|
+
params.merge!(LOGIN: @login)
|
72
|
+
@factory.verify_params(params) ? send_request(params) : @factory.create
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def send_request(params)
|
78
|
+
res = Net::HTTP.post_form(@uri, params)
|
79
|
+
@factory.parse(res.body).create
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# Copyright (c) 2015 Car Next Door
|
2
|
+
# Author: Ray Tung
|
3
|
+
#
|
4
|
+
# MIT License
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
module NetRegistry
|
26
|
+
class CommandNotFoundError < ArgumentError; end
|
27
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Copyright (c) 2015 Car Next Door
|
2
|
+
# Author: Ray Tung
|
3
|
+
#
|
4
|
+
# MIT License
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
module NetRegistry
|
26
|
+
module Helpers
|
27
|
+
EXPIRY_REGEX = /\d{2}\/\d{2}/
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Copyright (c) 2015 Car Next Door
|
2
|
+
# Author: Ray Tung
|
3
|
+
#
|
4
|
+
# MIT License
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
module NetRegistry
|
26
|
+
class Response
|
27
|
+
attr_accessor :text, :status, :full_response
|
28
|
+
attr_reader :code, :result
|
29
|
+
|
30
|
+
def initialize(text: "Unknown Error", code: -1, status: "failed", result: -1)
|
31
|
+
@text, @code, @status, @result = text, code, status, result
|
32
|
+
end
|
33
|
+
|
34
|
+
def failed?
|
35
|
+
@code == -1 ||
|
36
|
+
(!@full_response.nil? && @full_response.first == "failed")
|
37
|
+
end
|
38
|
+
|
39
|
+
def success?
|
40
|
+
!failed?
|
41
|
+
end
|
42
|
+
|
43
|
+
def code=(code)
|
44
|
+
@code = code.to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
def result=(result)
|
48
|
+
@result = result.to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
def transaction=(transaction)
|
52
|
+
raise TypeError, "Not NetRegistry::Transaction" if !transaction.is_a?(NetRegistry::Transaction)
|
53
|
+
@transaction = transaction
|
54
|
+
end
|
55
|
+
|
56
|
+
def transaction
|
57
|
+
@transaction ||= NetRegistry::Transaction.new
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
# Copyright (c) 2015 Car Next Door
|
2
|
+
# Author: Ray Tung
|
3
|
+
#
|
4
|
+
# MIT License
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
require 'date'
|
26
|
+
|
27
|
+
module NetRegistry
|
28
|
+
class ResponseBuilder
|
29
|
+
|
30
|
+
attr_reader :response
|
31
|
+
|
32
|
+
def initialize
|
33
|
+
@response = NetRegistry::Response.new
|
34
|
+
end
|
35
|
+
|
36
|
+
# command (String): Denotes which action we're taking.
|
37
|
+
# Only accepts the following actions:
|
38
|
+
# purchase, refund, preauth, status.
|
39
|
+
# params (Hash): Variables to pass to NetRegistry
|
40
|
+
def verify_params(params = {})
|
41
|
+
success = false
|
42
|
+
params = process_params(params)
|
43
|
+
case params[:COMMAND]
|
44
|
+
when "purchase"
|
45
|
+
@response.text, success = validate_purchase_params(params)
|
46
|
+
when "refund"
|
47
|
+
@response.text, success = validate_refund_params(params)
|
48
|
+
when "preauth"
|
49
|
+
@response.text, success = validate_preauth_params(params)
|
50
|
+
when "status"
|
51
|
+
@response.text, success = validate_status_params(params)
|
52
|
+
else
|
53
|
+
@response.text = "Invalid command. Only [purchase status preauth refund] are valid."
|
54
|
+
success = false
|
55
|
+
end
|
56
|
+
@response.code = 0 if success
|
57
|
+
@response.result = 0 if success
|
58
|
+
@response.status = "" if success
|
59
|
+
|
60
|
+
@response.success?
|
61
|
+
end
|
62
|
+
|
63
|
+
def create
|
64
|
+
@response
|
65
|
+
end
|
66
|
+
|
67
|
+
# parse HTTP request response body into a response object
|
68
|
+
# return factory itself.
|
69
|
+
# To get the response object, use #create method
|
70
|
+
def parse(response)
|
71
|
+
raise TypeError, "Response is not a string" if !response.is_a?(String)
|
72
|
+
@full_response = response.split("\n").map(&:strip)
|
73
|
+
if @full_response.first == "failed"
|
74
|
+
# remove all spaces until the dot
|
75
|
+
lines = @full_response.drop_while { |x| x != "." }
|
76
|
+
if lines.empty?
|
77
|
+
@response.text = @full_response[1]
|
78
|
+
else
|
79
|
+
lines.shift
|
80
|
+
lines[0].slice!("response_text=")
|
81
|
+
@response.text = lines[0]
|
82
|
+
end
|
83
|
+
@response.status = "failed"
|
84
|
+
@response.code = -1
|
85
|
+
else
|
86
|
+
@full_response.each do |line|
|
87
|
+
data = line.split("=")
|
88
|
+
case data.first
|
89
|
+
when "card_number", "card_no"
|
90
|
+
@response.transaction.card.number = data[1]
|
91
|
+
when "response_text"
|
92
|
+
@response.text = data[1].to_s
|
93
|
+
when "amount", "total_amount"
|
94
|
+
@response.transaction.amount = data[1]
|
95
|
+
when "status"
|
96
|
+
@response.status = data[1]
|
97
|
+
when "txnref", "txn_ref"
|
98
|
+
@response.transaction.reference = data[1]
|
99
|
+
when "transaction_no"
|
100
|
+
@response.transaction.number = data[1]
|
101
|
+
when "bank_ref"
|
102
|
+
@response.transaction.bank_reference = data[1]
|
103
|
+
when "card_desc"
|
104
|
+
@response.transaction.card.description = data[1]
|
105
|
+
when "response_code"
|
106
|
+
@response.code = data[1]
|
107
|
+
when "card_type"
|
108
|
+
@response.transaction.card.type = data[1]
|
109
|
+
when "time"
|
110
|
+
@response.transaction.time = data[1]
|
111
|
+
when "command"
|
112
|
+
@response.transaction.command = data[1]
|
113
|
+
when "card_expiry"
|
114
|
+
@response.transaction.card.expiry = data[1]
|
115
|
+
when "result"
|
116
|
+
@response.result = data[1]
|
117
|
+
when "settlement_date"
|
118
|
+
@response.transaction.settlement_date = data[1]
|
119
|
+
when "rrn"
|
120
|
+
@response.transaction.rrn = data[1]
|
121
|
+
when "MID"
|
122
|
+
@response.transaction.merchant_id = data[1]
|
123
|
+
else
|
124
|
+
end
|
125
|
+
end
|
126
|
+
@receipt = @full_response.drop_while { |line| !line.include?("Reciept follows") }
|
127
|
+
if @receipt.include?("Reciept follows")
|
128
|
+
@receipt = @receipt[1...-2]
|
129
|
+
@response.transaction.receipt = @receipt.join("\n")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
self
|
134
|
+
end
|
135
|
+
|
136
|
+
protected
|
137
|
+
# Preliminary validation for the purchase method
|
138
|
+
# Returns a Response Object
|
139
|
+
def validate_purchase_params(params)
|
140
|
+
if params[:AMOUNT].nil? || params[:AMOUNT].empty?
|
141
|
+
return "AMOUNT not found", false
|
142
|
+
elsif params[:CCNUM].nil? || params[:CCNUM].empty?
|
143
|
+
return "CCNUM not found", false
|
144
|
+
elsif params[:CCEXP].nil? || params[:CCEXP].empty?
|
145
|
+
return "CCEXP not found", false
|
146
|
+
elsif !valid_expiry_format?(params[:CCEXP])
|
147
|
+
return "CCEXP invalid format", false
|
148
|
+
else
|
149
|
+
return "", true
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def validate_refund_params(params)
|
154
|
+
if params[:AMOUNT].nil? || params[:AMOUNT].empty?
|
155
|
+
return "AMOUNT not found", false
|
156
|
+
elsif params[:TXNREF].nil? || params[:TXNREF].empty?
|
157
|
+
return "TXNREF not found", false
|
158
|
+
else
|
159
|
+
return "", true
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def validate_preauth_params(params)
|
164
|
+
if params[:CCNUM].nil? || params[:CCNUM].empty?
|
165
|
+
return "CCNUM not found", false
|
166
|
+
elsif params[:CCEXP].nil? || params[:CCEXP].empty?
|
167
|
+
return "CCEXP not found", false
|
168
|
+
elsif !valid_expiry_format?(params[:CCEXP])
|
169
|
+
return "CCEXP invalid format", false
|
170
|
+
elsif params[:AMOUNT].nil? || params[:AMOUNT].empty?
|
171
|
+
return "AMOUNT not found", false
|
172
|
+
else
|
173
|
+
return "", true
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def validate_status_params(params)
|
178
|
+
if params[:TXNREF].nil? || params[:TXNREF].empty?
|
179
|
+
return "TXNREF not found", false
|
180
|
+
else
|
181
|
+
return "", true
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
def valid_expiry_format?(card_expiry)
|
187
|
+
raise TypeError if !card_expiry.is_a?(String)
|
188
|
+
begin
|
189
|
+
Date.parse(card_expiry)
|
190
|
+
!NetRegistry::Helpers::EXPIRY_REGEX.match(card_expiry).nil?
|
191
|
+
rescue ArgumentError
|
192
|
+
false
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# Pre-process parameters. In this instance, pre-process
|
197
|
+
# all parameters into strings, for easy params validation
|
198
|
+
def process_params(params)
|
199
|
+
params.each { |key, value| params[key] = value.to_s }
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|