alpha_card 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -8
- data/alpha_card.gemspec +2 -2
- data/lib/alpha_card.rb +2 -2
- data/lib/alpha_card/alpha_card_response.rb +89 -2
- data/lib/alpha_card/utils.rb +40 -52
- data/spec/alpha_card/alpha_card_account_spec.rb +18 -0
- data/spec/alpha_card/alpha_card_utils_spec.rb +25 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9578c7b0b9b2681146ac0370640b26ed9b6d7cd4
|
4
|
+
data.tar.gz: a9ade4a67954c16fc8c3bff0dd8b92838b820358
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9e4b44f63a547a82f1c4a4fd405790436fa663bb31aabe3ba22c995cccc5b7333b0c6f25c4bdc2802209e29c03141899d8bd1df5ed9fa95917c769eb6663a42
|
7
|
+
data.tar.gz: bff105616d044b9560ed7f53dc2b47702134b2aa4d3fc86f2a7939abadccebea6bcd9569b8210151f4456f817c6ffb7520c796c970fb8ddc301f5ab13cae47c5
|
data/README.md
CHANGED
@@ -212,14 +212,16 @@ You are very welcome to help improve alpha_card if you have suggestions for feat
|
|
212
212
|
|
213
213
|
To contribute:
|
214
214
|
|
215
|
-
1. Fork the project
|
216
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
217
|
-
3.
|
218
|
-
4. Add
|
219
|
-
5. Run
|
220
|
-
6.
|
221
|
-
7.
|
222
|
-
8.
|
215
|
+
1. Fork the project.
|
216
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
217
|
+
3. Implement your feature or bug fix.
|
218
|
+
4. Add documentation for your feature or bug fix.
|
219
|
+
5. Run <tt>rake doc:yard</tt>. If your changes are not 100% documented, go back to step 4.
|
220
|
+
6. Add tests for your feature or bug fix.
|
221
|
+
7. Run `rake` to make sure all tests pass.
|
222
|
+
8. Commit your changes (`git commit -am 'Add new feature'`).
|
223
|
+
9. Push to the branch (`git push origin my-new-feature`).
|
224
|
+
10. Create new pull request.
|
223
225
|
|
224
226
|
Thanks.
|
225
227
|
|
data/alpha_card.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = 'alpha_card'
|
3
|
-
gem.version = '0.1.
|
4
|
-
gem.date = '2014-07-
|
3
|
+
gem.version = '0.1.9'
|
4
|
+
gem.date = '2014-07-02'
|
5
5
|
gem.summary = 'Alpha Card Services DirectPost API for Ruby'
|
6
6
|
gem.description = 'Gem for creating sales with Alpha Card Services DirectPost API'
|
7
7
|
gem.authors = ['Nikita Bulaj']
|
data/lib/alpha_card.rb
CHANGED
@@ -25,7 +25,7 @@ module AlphaCard
|
|
25
25
|
# Global Payment Systems (NDC) Credit Card Authorization Codes
|
26
26
|
#
|
27
27
|
# @see http://floristwiki.ftdi.com/images/c/ce/Appendix_A_-_Credit_Card_Authorization_Codes.pdf Credit Card Authorization Codes
|
28
|
-
CREDIT_CARD_CODES
|
28
|
+
CREDIT_CARD_CODES ||= YAML.load_file(File.expand_path('../alpha_card/data/codes.yml', __FILE__))
|
29
29
|
|
30
30
|
class << self
|
31
31
|
# @return [String] Alpha Card Gateway DirectPost API URL.
|
@@ -35,7 +35,7 @@ module AlphaCard
|
|
35
35
|
##
|
36
36
|
# Send the POST request to the AlphaCard Gateway from the
|
37
37
|
# specified account. Request must contains params - Alpha Card
|
38
|
-
#
|
38
|
+
# transaction variables.
|
39
39
|
#
|
40
40
|
# @param [Hash] params
|
41
41
|
# Alpha Card transaction variables.
|
@@ -4,6 +4,8 @@ module AlphaCard
|
|
4
4
|
# Contains all the data, that Alpha Card Gateway
|
5
5
|
# returned for the request.
|
6
6
|
class AlphaCardResponse
|
7
|
+
# Alpha Card Gateway response as a <code>Hash</code>.
|
8
|
+
# @attr_reader [Hash] data
|
7
9
|
attr_reader :data
|
8
10
|
|
9
11
|
# Success response code
|
@@ -13,30 +15,115 @@ module AlphaCard
|
|
13
15
|
# Error response code
|
14
16
|
ERROR = '3'
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
+
##
|
19
|
+
# AlphaCardResponse constructor.
|
20
|
+
#
|
21
|
+
# @param [String] response_body
|
22
|
+
# Alpha Card Gateway response body text
|
23
|
+
#
|
24
|
+
# @return [AlphaCardResponse] AlphaCardResponse object
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# AlphaCard::AlphaCardResponse.new('response=1&responsetext=Test')
|
28
|
+
#
|
29
|
+
# #=> #<AlphaCard::AlphaCardResponse:0x00000003f2b568 @data={"response"=>"1", "responsetext"=>"Test"}>
|
30
|
+
def initialize(response_body)
|
31
|
+
@data = AlphaCard::Utils.parse_query(response_body)
|
18
32
|
end
|
19
33
|
|
34
|
+
##
|
35
|
+
# The text of the Alpha Card Gateway response.
|
36
|
+
#
|
37
|
+
# @return [String] text of the response
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
#
|
41
|
+
# r = AlphaCardResponse.new("response=1&responsetext=Test")
|
42
|
+
# r.text
|
43
|
+
#
|
44
|
+
# #=> 'Test'
|
20
45
|
def text
|
21
46
|
@data['responsetext']
|
22
47
|
end
|
23
48
|
|
49
|
+
##
|
50
|
+
# The ID of the transaction. Can be used to process
|
51
|
+
# refund operation.
|
52
|
+
#
|
53
|
+
# @return [String] transaction ID
|
54
|
+
#
|
55
|
+
# @example
|
56
|
+
#
|
57
|
+
# r = AlphaCardResponse.new("response=1&transactionid=123")
|
58
|
+
# r.transaction_id
|
59
|
+
#
|
60
|
+
# #=> '123'
|
24
61
|
def transaction_id
|
25
62
|
@data['transactionid']
|
26
63
|
end
|
27
64
|
|
65
|
+
##
|
66
|
+
# The code of the Alpha Card Gateway response.
|
67
|
+
#
|
68
|
+
# @return [String] code of the response
|
69
|
+
#
|
70
|
+
# @example
|
71
|
+
#
|
72
|
+
# r = AlphaCardResponse.new("response=1&response_code=100")
|
73
|
+
# r.code
|
74
|
+
#
|
75
|
+
# #=> '100'
|
28
76
|
def code
|
29
77
|
@data['response_code']
|
30
78
|
end
|
31
79
|
|
80
|
+
##
|
81
|
+
# Indicate the state of the request to the
|
82
|
+
# Alpha Card Gateway. Returns <i>true</i> if
|
83
|
+
# response was <i>approved</i>.
|
84
|
+
#
|
85
|
+
# @return [Bool] true if request if successful
|
86
|
+
#
|
87
|
+
# @example
|
88
|
+
#
|
89
|
+
# r = AlphaCardResponse.new("response=1")
|
90
|
+
# r.success?
|
91
|
+
#
|
92
|
+
# #=> true
|
32
93
|
def success?
|
33
94
|
@data['response'] == APPROVED
|
34
95
|
end
|
35
96
|
|
97
|
+
##
|
98
|
+
# Indicate the state of the request to the
|
99
|
+
# Alpha Card Gateway. Returns <i>true</i> if
|
100
|
+
# response was <i>declined</i>.
|
101
|
+
#
|
102
|
+
# @return [Bool] true if request was declined
|
103
|
+
#
|
104
|
+
# @example
|
105
|
+
#
|
106
|
+
# r = AlphaCardResponse.new("response=2")
|
107
|
+
# r.declined?
|
108
|
+
#
|
109
|
+
# #=> true
|
36
110
|
def declined?
|
37
111
|
@data['response'] == DECLINED
|
38
112
|
end
|
39
113
|
|
114
|
+
##
|
115
|
+
# Indicate the state of the request to the
|
116
|
+
# Alpha Card Gateway. Returns <i>true</i> if
|
117
|
+
# response has some <i>errors</i>.
|
118
|
+
#
|
119
|
+
# @return [Bool] true if request has some errors
|
120
|
+
#
|
121
|
+
# @example
|
122
|
+
#
|
123
|
+
# r = AlphaCardResponse.new("response=3")
|
124
|
+
# r.error?
|
125
|
+
#
|
126
|
+
# #=> true
|
40
127
|
def error?
|
41
128
|
@data['response'] == ERROR
|
42
129
|
end
|
data/lib/alpha_card/utils.rb
CHANGED
@@ -11,67 +11,55 @@ module AlphaCard
|
|
11
11
|
# Default separators for the query string.
|
12
12
|
DEFAULT_SEP = /[&;] */n
|
13
13
|
|
14
|
-
# A part of the rack gem.
|
15
|
-
class KeySpaceConstrainedParams
|
16
|
-
def initialize(limit = 65536)
|
17
|
-
@limit = limit
|
18
|
-
@size = 0
|
19
|
-
@params = {}
|
20
|
-
end
|
21
|
-
|
22
|
-
def [](key)
|
23
|
-
@params[key]
|
24
|
-
end
|
25
|
-
|
26
|
-
def []=(key, value)
|
27
|
-
@size += key.size if key && !@params.key?(key)
|
28
|
-
raise RangeError, 'exceeded available parameter key space' if @size > @limit
|
29
|
-
@params[key] = value
|
30
|
-
end
|
31
|
-
|
32
|
-
##
|
33
|
-
# Returns <code>true</code> if the given key is present
|
34
|
-
# in <i>params</i>.
|
35
|
-
def key?(key)
|
36
|
-
@params.key?(key)
|
37
|
-
end
|
38
|
-
|
39
|
-
##
|
40
|
-
# Converts params to <code>Hash</code> object.
|
41
|
-
def to_params_hash
|
42
|
-
hash = @params
|
43
|
-
hash.keys.each do |key|
|
44
|
-
value = hash[key]
|
45
|
-
if value.kind_of?(self.class)
|
46
|
-
hash[key] = value.to_params_hash
|
47
|
-
elsif value.kind_of?(Array)
|
48
|
-
value.map! { |x| x.kind_of?(self.class) ? x.to_params_hash : x }
|
49
|
-
end
|
50
|
-
end
|
51
|
-
hash
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
14
|
##
|
56
15
|
# Unescapes a URI escaped string with +encoding+. +encoding+ will be the
|
57
16
|
# target encoding of the string returned, and it defaults to UTF-8
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
17
|
+
#
|
18
|
+
# @param [String] s
|
19
|
+
# String to unescape.
|
20
|
+
#
|
21
|
+
# @param [Encoding] encoding
|
22
|
+
# Type of <code>Encoding</code>.
|
23
|
+
#
|
24
|
+
# @return [String] URI encoded string
|
25
|
+
#
|
26
|
+
# @raise [ArgumentError] if invalid Encoding is passed
|
27
|
+
#
|
28
|
+
# @example
|
29
|
+
#
|
30
|
+
# unescape('Test%20str')
|
31
|
+
# #=> "Test str"
|
32
|
+
#
|
33
|
+
# unescape('Test%20str', Encoding::WINDOWS_31J)
|
34
|
+
# #=> "Test str"
|
35
|
+
def unescape(s, encoding = Encoding::UTF_8)
|
36
|
+
URI.decode_www_form_component(s, encoding)
|
66
37
|
end
|
67
38
|
|
68
39
|
##
|
69
|
-
#
|
40
|
+
# Parse query string to a <code>Hash</code> by breaking it up
|
70
41
|
# at the '&' and ';' characters.
|
42
|
+
#
|
43
|
+
# @param [String] qs
|
44
|
+
# query string
|
45
|
+
# @param [String] d
|
46
|
+
# delimiter for the params
|
47
|
+
# @yieldparam unescaper
|
48
|
+
# method, that will unescape the param value
|
49
|
+
#
|
50
|
+
# @return [Hash] query
|
51
|
+
#
|
52
|
+
# @example
|
53
|
+
#
|
54
|
+
# query = AlphaCard::Utils.parse_query("param1=value1¶ms2=")
|
55
|
+
# #=> {"param1"=>"value1", "params2"=>""}
|
56
|
+
#
|
57
|
+
# query = AlphaCard::Utils.parse_query("cars[]=Saab&cars[]=Audi")
|
58
|
+
# #=> {"cars[]"=>["Saab", "Audi"]}
|
71
59
|
def parse_query(qs, d = nil, &unescaper)
|
72
60
|
unescaper ||= method(:unescape)
|
73
61
|
|
74
|
-
params =
|
62
|
+
params = {}
|
75
63
|
|
76
64
|
(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
|
77
65
|
next if p.empty?
|
@@ -88,7 +76,7 @@ module AlphaCard
|
|
88
76
|
end
|
89
77
|
end
|
90
78
|
|
91
|
-
params
|
79
|
+
params
|
92
80
|
end
|
93
81
|
end
|
94
82
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe AlphaCard::Account do
|
4
|
+
let!(:valid_account) { AlphaCard::Account.new('demo', 'password') }
|
5
|
+
let!(:invalid_account) { AlphaCard::Account.new('', '') }
|
6
|
+
|
7
|
+
context 'with valid credentials' do
|
8
|
+
it 'should return true if credentials are filled' do
|
9
|
+
expect(valid_account.filled?).to be_truthy
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with invalid credentials' do
|
14
|
+
it 'should return false if credentials are filled' do
|
15
|
+
expect(invalid_account.filled?).to be_falsey
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe AlphaCard::Utils do
|
5
|
+
it 'parse query strings correctly' do
|
6
|
+
expect(AlphaCard::Utils.parse_query("foo=bar")).to eq("foo" => "bar")
|
7
|
+
expect(AlphaCard::Utils.parse_query('cars[]=Saab&cars[]=Audi')).to eq({'cars[]' => ['Saab', 'Audi']})
|
8
|
+
expect(AlphaCard::Utils.parse_query("foo=bar&foo=quux")).to eq("foo" => ["bar", "quux"])
|
9
|
+
expect(AlphaCard::Utils.parse_query("foo=1&bar=2")).to eq("foo" => "1", "bar" => "2")
|
10
|
+
expect(AlphaCard::Utils.parse_query("my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F")).to eq("my weird field" => "q1!2\"'w$5&7/z8)?")
|
11
|
+
expect(AlphaCard::Utils.parse_query("foo%3Dbaz=bar")).to eq("foo=baz" => "bar")
|
12
|
+
expect(AlphaCard::Utils.parse_query("=")).to eq("" => "")
|
13
|
+
expect(AlphaCard::Utils.parse_query("=value")).to eq("" => "value")
|
14
|
+
expect(AlphaCard::Utils.parse_query("key=")).to eq("key" => "")
|
15
|
+
expect(AlphaCard::Utils.parse_query("&key&")).to eq("key" => nil)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'unescape the characters correctly' do
|
19
|
+
expect(AlphaCard::Utils.unescape('%E9%9F%93', Encoding::UTF_8)).to eq('韓')
|
20
|
+
expect(AlphaCard::Utils.unescape("fo%3Co%3Ebar")).to eq("fo<o>bar")
|
21
|
+
expect(AlphaCard::Utils.unescape("a+space")).to eq("a space")
|
22
|
+
expect(AlphaCard::Utils.unescape("a%20space")).to eq("a space")
|
23
|
+
expect(AlphaCard::Utils.unescape("q1%212%22%27w%245%267%2Fz8%29%3F%5C")).to eq("q1!2\"'w$5&7/z8)?\\")
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alpha_card
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nikita Bulaj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -49,8 +49,10 @@ files:
|
|
49
49
|
- lib/alpha_card/sale.rb
|
50
50
|
- lib/alpha_card/shipping.rb
|
51
51
|
- lib/alpha_card/utils.rb
|
52
|
+
- spec/alpha_card/alpha_card_account_spec.rb
|
52
53
|
- spec/alpha_card/alpha_card_response_spec.rb
|
53
54
|
- spec/alpha_card/alpha_card_spec.rb
|
55
|
+
- spec/alpha_card/alpha_card_utils_spec.rb
|
54
56
|
- spec/spec_helper.rb
|
55
57
|
homepage: http://github.com/budev/alpha_card
|
56
58
|
licenses:
|