alpha_card 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d1b3608b60661b5688717b2f96768ac0e36a83c
4
- data.tar.gz: 8d0a9f755a5faa7802d36feac0828bd1d3877cc1
3
+ metadata.gz: 294f83e708f2952a60d2ecf8480ae58af870e368
4
+ data.tar.gz: 1fa31126072357beaed859e4415c03245a937e13
5
5
  SHA512:
6
- metadata.gz: c752b3a03ce5299126ae280a3456d7d7507803e63a8c0bd0cca7dd3cffaa2ec34764694da072d962658666d7b876231ba3b026a27d99554edbfb87d075729d9b
7
- data.tar.gz: 531987ff7b2dcf6555e29816eccd92394bbf6d1577219064e321020a6a75713d8c37c7037e1a117a8d77ef5c7f184f6ab6a7834108676467419bf1970cdb92b0
6
+ metadata.gz: 6c715932f21918d1914c83df62fd8a96e963b5a322c20da13b7facc00efc60cb8353ca6be255f403d0b684cb5db91fb4b92f623026adb7bdafb2a415ea54bb56
7
+ data.tar.gz: 1e091ed0479b8d61741f7cd37fea94f2b58ef6f5c34403743d4bd846d8e985080628ecd82a04b22fa15ffff96e0a150cb88c1a4a9daca67c24cd5a34b7dab988
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile CHANGED
@@ -3,4 +3,6 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'virtus'
6
- gem 'rest_client'
6
+ gem 'rest_client'
7
+ gem 'rack'
8
+ #gem 'simplecov', :require => false
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Ruby lib for creating payments with AlphaCard DirectPost API
2
- [![Gem Version](https://badge.fury.io/rb/alpha_card.png)](http://badge.fury.io/rb/alpha_card)
2
+ [![Gem Version](https://badge.fury.io/rb/alpha_card.svg)](http://badge.fury.io/rb/alpha_card)
3
3
  [![Dependency Status](https://gemnasium.com/budev/alpha_card.png)](https://gemnasium.com/budev/alpha_card)
4
4
 
5
5
  This gem can help your ruby/rails application to integrate with AlphaCard service.
@@ -212,6 +212,23 @@ Example of exception:
212
212
  AlphaCard::AlphaCardError: Invalid Credit Card Number REFID:127145481
213
213
  ```
214
214
 
215
+ ## Contributing
216
+
217
+ You are very welcome to help improve alpha_card if you have suggestions for features that other people can use.
218
+
219
+ To contribute:
220
+
221
+ 1. Fork the project
222
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
223
+ 3. Make your changes
224
+ 4. Add tests
225
+ 5. Run `rake` to make sure all tests pass
226
+ 6. Commit your changes (`git commit -am 'Add new feature'`)
227
+ 7. Push to the branch (`git push origin my-new-feature`)
228
+ 8. Create new pull request
229
+
230
+ Thanks.
231
+
215
232
  ## Copyright
216
233
 
217
234
  Copyright (c) 2014 Nikita Bulaj (bulajnikita@gmail.com).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'alpha_card'
3
- gem.version = '0.1.4'
3
+ gem.version = '0.1.5'
4
4
  gem.date = '2014-06-25'
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'
@@ -11,4 +11,6 @@ Gem::Specification.new do |gem|
11
11
  gem.homepage = 'http://github.com/budev/alpha_card'
12
12
  gem.license = 'MIT'
13
13
  gem.required_ruby_version = '>= 1.9.3'
14
+
15
+ gem.add_development_dependency "rspec", '~> 3'
14
16
  end
@@ -19,7 +19,7 @@ module AlphaCard
19
19
  @api_base = 'https://secure.alphacardgateway.com/api/transact.php'
20
20
 
21
21
  # Global Payment Systems (NDC) Credit Card Authorization Codes
22
- CREDIT_CARD_CODES = YAML.load_file(File.expand_path('../alpha_card/data/codes.yml', __FILE__)) unless defined? CREDIT_CARD_CODES
22
+ CREDIT_CARD_CODES = YAML.load_file(File.expand_path('../alpha_card/data/codes.yml', __FILE__)) unless defined? CREDIT_CARD_CODES
23
23
 
24
24
  class << self
25
25
  attr_accessor :api_base
@@ -38,10 +38,14 @@ module AlphaCard
38
38
  raise AlphaCardError.new('You must set credentials to create the sale!')
39
39
  end
40
40
 
41
- response = RestClient.post(@api_base, params.merge(account.attributes))
41
+ begin
42
+ response = RestClient.post(@api_base, params.merge(account.attributes))
43
+ rescue => e
44
+ handle_connection_errors(e)
45
+ end
42
46
 
43
47
  alpha_card_response = AlphaCardResponse.new(response.to_str)
44
- handle_errors(alpha_card_response)
48
+ handle_alpha_card_errors(alpha_card_response)
45
49
 
46
50
  alpha_card_response
47
51
  end
@@ -56,9 +60,34 @@ module AlphaCard
56
60
  #
57
61
  # response = AlphaCard.request(params, account)
58
62
  # handle_errors(response) #=> nil or Exception
59
- def self.handle_errors(response)
63
+ def self.handle_alpha_card_errors(response)
60
64
  code = response.text
61
65
  raise AlphaCardError.new(CREDIT_CARD_CODES[code] || code) unless response.success?
62
66
  end
67
+
68
+ # AlphaCard.handle_connection_errors(exception) -> Exception
69
+ #
70
+ # Raise an exception if AlphaCard Gateway return some exception
71
+ # due to connection problems or some other net errors.
72
+ #
73
+ # response = RestClient.post(URL, {param: 'value'})
74
+ # handle_connection_errors(response) #=> Exception
75
+ def self.handle_connection_errors(e)
76
+ case e
77
+ when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
78
+ message = "Could not connect to Alpha Card Gateway (#{@api_base}). " +
79
+ 'Please check your internet connection and try again. ' +
80
+ 'If this problem persists, you should check Alpha Card services status.'
81
+
82
+ when SocketError
83
+ message = 'Unexpected error communicating when trying to connect to Alpha Card Gateway. ' +
84
+ 'You may be seeing this message because your DNS is not working.'
85
+
86
+ else
87
+ message = 'Unexpected error communicating with Alpha Card Gateway.'
88
+ end
89
+
90
+ raise AlphaCardError.new(message + "\n\n(Network error: #{e.message})")
91
+ end
63
92
  end
64
93
 
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe AlphaCard::AlphaCardResponse do
4
+ let!(:successful_response_mock) {
5
+ "authcode=083319&avsresponse=&cvvresponse=M&orderid=1&response=1&response_code=100&responsetext=AP&transactionid=2303767426&type=sale"
6
+ }
7
+
8
+ let!(:declined_response_mock) {
9
+ "authcode=&avsresponse=&cvvresponse=&orderid=1&response=2&response_code=220&responsetext=INVLD+ACCT&transactionid=2302720045&type=sale"
10
+ }
11
+
12
+ let!(:error_response_mock) {
13
+ "authcode=&avsresponse=&cvvresponse=&orderid=1&response=3&response_code=220&responsetext=ERROR&transactionid=2302620041&type=sale"
14
+ }
15
+
16
+ context 'successful request' do
17
+ let!(:response) { AlphaCard::AlphaCardResponse.new(successful_response_mock) }
18
+
19
+ it 'should be success' do
20
+ expect(response.success?).to be_truthy
21
+ end
22
+
23
+ it 'should return responce code' do
24
+ expect(response.code).to eq('100')
25
+ end
26
+
27
+ it 'should return transaction id' do
28
+ expect(response.transaction_id).to eq('2303767426')
29
+ end
30
+ end
31
+
32
+ context 'declined request' do
33
+ let!(:response) { AlphaCard::AlphaCardResponse.new(declined_response_mock) }
34
+
35
+ it 'should be declined' do
36
+ expect(response.declined?).to be_truthy
37
+ end
38
+
39
+ it 'should return responce code' do
40
+ expect(response.code).to eq('220')
41
+ end
42
+
43
+ it 'should return transaction id' do
44
+ expect(response.transaction_id).to eq('2302720045')
45
+ end
46
+ end
47
+
48
+ context 'error request' do
49
+ let!(:response) { AlphaCard::AlphaCardResponse.new(error_response_mock) }
50
+
51
+ it 'should be error' do
52
+ expect(response.error?).to be_truthy
53
+ end
54
+
55
+ it 'should return responce code' do
56
+ expect(response.code).to eq('220')
57
+ end
58
+
59
+ it 'should return transaction id' do
60
+ expect(response.transaction_id).to eq('2302620041')
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,106 @@
1
+ require "spec_helper"
2
+
3
+ describe AlphaCard do
4
+ # Shared objects
5
+ let!(:account) { AlphaCard::Account.new('demo', 'password') }
6
+ let!(:billing) { AlphaCard::Billing.new({email: 'test@example.com'}) }
7
+ let!(:shipping) { AlphaCard::Shipping.new({address_1: '22 N str.'}) }
8
+ let!(:order) { AlphaCard::Order.new({orderid: '1', billing: billing, shipping: shipping}) }
9
+ let!(:card_exp) { "#{'%02d' % Time.now.month}/#{Time.now.year.next}" }
10
+
11
+ #TODO: Create rest client mock to imitate requests, normal tests for error exceptions
12
+
13
+ context 'With valid attributes' do
14
+ let!(:sale) { AlphaCard::Sale.new({ccexp: card_exp, ccnumber: '4111111111111111', amount: '5.00'}) }
15
+
16
+ it 'should successfully create the sale' do
17
+ expect(sale.create(order, account)).to be_truthy
18
+ end
19
+ end
20
+
21
+ context 'With invalid Card number' do
22
+ let!(:sale) { AlphaCard::Sale.new({ccexp: card_exp, ccnumber: 'Invalid', amount: '5.00'}) }
23
+
24
+ it 'should raise an AlphaCardError' do
25
+ expect { sale.create(order, account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
26
+ expect(e.message).to include('Card number must contain only digits')
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'With invalid amount' do
32
+ let!(:sale) { AlphaCard::Sale.new({ccexp: card_exp, ccnumber: '4111111111111111', amount: '0.00'}) }
33
+
34
+ it 'should raise an AlphaCardError' do
35
+ expect { sale.create(order, account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
36
+ expect(e.message).to include('Invalid amount')
37
+ end
38
+ end
39
+ end
40
+
41
+ context 'With invalid Card expiration date' do
42
+ let!(:sale) { AlphaCard::Sale.new({ccexp: 'Invalid', ccnumber: '4111111111111111', amount: '5.00'}) }
43
+
44
+ it 'should raise an AlphaCardError' do
45
+ expect { sale.create(order, account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
46
+ expect(e.message).to include('Card expiration should be in the format')
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'With invalid Card CVV' do
52
+ let!(:sale) { AlphaCard::Sale.new({ccexp: card_exp, cvv: 'Invalid', ccnumber: '4111111111111111', amount: '5.00'}) }
53
+
54
+ it 'should raise an AlphaCardError' do
55
+ expect { sale.create(order, account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
56
+ expect(e.message).to include('CVV must be')
57
+ end
58
+ end
59
+ end
60
+
61
+ context 'With invalid account credentials' do
62
+ let!(:invalid_account) { AlphaCard::Account.new('demo', 'Invalid password') }
63
+ let!(:sale) { AlphaCard::Sale.new({ccexp: card_exp, ccnumber: '4111111111111111', amount: '5.00'}) }
64
+
65
+ it 'should raise an AlphaCardError' do
66
+ expect { sale.create(order, invalid_account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
67
+ expect(e.message).to include('Authentication Failed')
68
+ end
69
+ end
70
+ end
71
+
72
+ context 'With blank account credentials' do
73
+ let!(:blank_account) { AlphaCard::Account.new(nil, '') }
74
+ let!(:sale) { AlphaCard::Sale.new({ccexp: card_exp, ccnumber: '4111111111111111', amount: '5.00'}) }
75
+
76
+ it 'should raise an AlphaCardError' do
77
+ expect { sale.create(order, blank_account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
78
+ expect(e.message).to include('You must set credentials to create the sale')
79
+ end
80
+ end
81
+ end
82
+
83
+ context 'With connection errors' do
84
+ let!(:rest_client_error) { RestClient::RequestTimeout.new }
85
+ let!(:socket_error) { SocketError.new }
86
+ let!(:unclassified_error) { StandardError.new('Some error') }
87
+
88
+ it 'should raise an Exception if Rest Client Error' do
89
+ expect { AlphaCard.handle_connection_errors(rest_client_error) }.to raise_error(AlphaCard::AlphaCardError) do |e|
90
+ expect(e.message).to include('Could not connect to Alpha Card Gateway')
91
+ end
92
+ end
93
+
94
+ it 'should raise an Exception if Socket Error' do
95
+ expect { AlphaCard.handle_connection_errors(socket_error) }.to raise_error(AlphaCard::AlphaCardError) do |e|
96
+ expect(e.message).to include('Unexpected error communicating when trying to connect to Alpha Card Gateway')
97
+ end
98
+ end
99
+
100
+ it 'should raise an Exception if Unclassified Error' do
101
+ expect { AlphaCard.handle_connection_errors(unclassified_error) }.to raise_error(AlphaCard::AlphaCardError) do |e|
102
+ expect(e.message).to include('Unexpected error communicating with Alpha Card Gateway')
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,12 @@
1
+ # Uncomment for coverage statistics
2
+ # require 'simplecov'
3
+ # SimpleCov.start
4
+
5
+ require 'bundler/setup'
6
+ Bundler.setup
7
+
8
+ require 'alpha_card'
9
+
10
+ RSpec.configure do |config|
11
+ config.order = 'random'
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alpha_card
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Bulaj
@@ -9,16 +9,33 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-06-25 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
13
27
  description: Gem for creating sales with Alpha Card Services DirectPost API
14
28
  email: bulajnikita@gmail.com
15
29
  executables: []
16
30
  extensions: []
17
31
  extra_rdoc_files: []
18
32
  files:
33
+ - ".gitignore"
34
+ - ".rspec"
19
35
  - Gemfile
20
36
  - LICENSE
21
37
  - README.md
38
+ - Rakefile
22
39
  - alpha_card.gemspec
23
40
  - lib/alpha_card.rb
24
41
  - lib/alpha_card/account.rb
@@ -31,6 +48,9 @@ files:
31
48
  - lib/alpha_card/order.rb
32
49
  - lib/alpha_card/sale.rb
33
50
  - lib/alpha_card/shipping.rb
51
+ - spec/alpha_card/alpha_card_response_spec.rb
52
+ - spec/alpha_card/alpha_card_spec.rb
53
+ - spec/spec_helper.rb
34
54
  homepage: http://github.com/budev/alpha_card
35
55
  licenses:
36
56
  - MIT