alpha_card 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -4
  3. data/CHANGELOG.md +21 -3
  4. data/Gemfile +0 -2
  5. data/Gemfile.lock +6 -24
  6. data/README.md +115 -85
  7. data/ROADMAP.md +13 -9
  8. data/alpha_card.gemspec +3 -4
  9. data/lib/alpha_card.rb +44 -72
  10. data/lib/alpha_card/account.rb +51 -0
  11. data/lib/alpha_card/attribute.rb +337 -0
  12. data/lib/alpha_card/data/credit_card_codes.yml +54 -54
  13. data/lib/alpha_card/errors/invalid_attribute_format.rb +14 -0
  14. data/lib/alpha_card/errors/invalid_attribute_type.rb +14 -0
  15. data/lib/alpha_card/errors/invalid_attribute_value.rb +14 -0
  16. data/lib/alpha_card/errors/{invalid_object_error.rb → validation_error.rb} +1 -1
  17. data/lib/alpha_card/{alpha_card_object.rb → resource.rb} +14 -28
  18. data/lib/alpha_card/resources/billing.rb +29 -0
  19. data/lib/alpha_card/{objects → resources}/order.rb +8 -8
  20. data/lib/alpha_card/{objects → resources}/shipping.rb +15 -13
  21. data/lib/alpha_card/{alpha_card_response.rb → response.rb} +21 -6
  22. data/lib/alpha_card/transaction.rb +30 -0
  23. data/lib/alpha_card/transactions/auth.rb +18 -0
  24. data/lib/alpha_card/transactions/capture.rb +32 -0
  25. data/lib/alpha_card/transactions/credit.rb +18 -0
  26. data/lib/alpha_card/{objects → transactions}/refund.rb +9 -2
  27. data/lib/alpha_card/transactions/sale.rb +91 -0
  28. data/lib/alpha_card/transactions/update.rb +61 -0
  29. data/lib/alpha_card/transactions/validate.rb +21 -0
  30. data/lib/alpha_card/transactions/void.rb +26 -0
  31. data/lib/alpha_card/version.rb +1 -1
  32. data/spec/alpha_card/attribute_spec.rb +126 -0
  33. data/spec/alpha_card/response_spec.rb +8 -4
  34. data/spec/alpha_card/transactions/auth_spec.rb +43 -0
  35. data/spec/alpha_card/{objects → transactions}/capture_spec.rb +11 -12
  36. data/spec/alpha_card/transactions/credit_spec.rb +102 -0
  37. data/spec/alpha_card/{objects → transactions}/refund_spec.rb +4 -4
  38. data/spec/alpha_card/{objects → transactions}/sale_spec.rb +42 -41
  39. data/spec/alpha_card/{objects → transactions}/update_spec.rb +4 -4
  40. data/spec/alpha_card/transactions/validate_spec.rb +100 -0
  41. data/spec/alpha_card/{objects → transactions}/void_spec.rb +11 -11
  42. data/spec/spec_helper.rb +4 -0
  43. metadata +36 -47
  44. data/lib/alpha_card/errors/alpha_card_error.rb +0 -29
  45. data/lib/alpha_card/objects/account.rb +0 -48
  46. data/lib/alpha_card/objects/billing.rb +0 -31
  47. data/lib/alpha_card/objects/capture.rb +0 -51
  48. data/lib/alpha_card/objects/sale.rb +0 -82
  49. data/lib/alpha_card/objects/update.rb +0 -54
  50. data/lib/alpha_card/objects/void.rb +0 -45
  51. data/spec/alpha_card/objects/account_spec.rb +0 -20
  52. data/spec/alpha_card/objects/deprecated_methods_spec.rb +0 -32
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlphaCard::Auth do
4
+ let(:order) { AlphaCard::Order.new(id: '1', description: 'Test') }
5
+ let(:card_exp) { (Time.now + 31104000).strftime('%m%y') }
6
+ let(:auth) { AlphaCard::Auth.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
7
+
8
+ context 'with invalid attributes' do
9
+ let(:response) { described_class.new(card_expiration_date: '1190', card_number: '1', amount: '1').create(order) }
10
+
11
+ it 'response with error' do
12
+ expect(response.error?).to be_truthy
13
+ expect(response.message).to eq('Transaction was rejected by gateway')
14
+ end
15
+ end
16
+
17
+ context 'with valid attributes' do
18
+ it 'has valid request params' do
19
+ expected_params = {
20
+ ccexp: card_exp,
21
+ ccnumber: '4111111111111111',
22
+ amount: '5.00',
23
+ payment: 'creditcard',
24
+ type: 'auth'
25
+ }
26
+
27
+ expect(auth.attributes_for_request).to eq(expected_params)
28
+ end
29
+
30
+ it 'processed successfully' do
31
+ response = auth.create(order)
32
+ expect(response.success?).to be_truthy
33
+ expect(response.text).to eq('SUCCESS')
34
+ end
35
+ end
36
+
37
+ context 'with blank attributes' do
38
+ it 'raises an InvalidObject error' do
39
+ expect { AlphaCard::Auth.new.create(order) }.to raise_error(AlphaCard::ValidationError)
40
+ expect { AlphaCard::Auth.new(amount: '1.05').create(order) }.to raise_error(AlphaCard::ValidationError)
41
+ end
42
+ end
43
+ end
@@ -1,13 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AlphaCard::Capture do
4
- let(:account) { AlphaCard::Account.new('demo', 'password') }
5
-
6
4
  context 'with invalid attributes' do
7
5
  let(:capture) { AlphaCard::Capture.new(transaction_id: 'Some ID', amount: '10.05') }
6
+ let(:response) { capture.process }
8
7
 
9
8
  it 'response with error' do
10
- expect { capture.create(account) }.to raise_error(AlphaCard::AlphaCardError)
9
+ expect(response.error?).to be_truthy
10
+ expect(response.message).to eq('Transaction was rejected by gateway')
11
11
  end
12
12
  end
13
13
 
@@ -15,8 +15,8 @@ describe AlphaCard::Capture do
15
15
  let(:capture) { AlphaCard::Capture.new(transaction_id: 'Some ID', amount: '10.05', order_id: '1', shipping_carrier: '2') }
16
16
 
17
17
  let(:order) { AlphaCard::Order.new(id: '1', description: 'Test') }
18
- let(:card_exp) { "#{'%02d' % Time.now.month}/#{Time.now.year.next}" }
19
- let(:sale) { AlphaCard::Sale.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
18
+ let(:card_exp) { (Time.now + 31104000).strftime('%m%y') }
19
+ let(:auth) { AlphaCard::Auth.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
20
20
 
21
21
  it 'has valid request params' do
22
22
  expected_params = {
@@ -31,21 +31,20 @@ describe AlphaCard::Capture do
31
31
  end
32
32
 
33
33
  it 'processed successfully' do
34
- allow_any_instance_of(AlphaCard::Sale).to receive(:type).and_return('auth')
35
- success, response = sale.create(order, account)
36
- expect(success).to be_truthy
34
+ response = auth.create(order)
35
+ expect(response.success?).to be_truthy
37
36
  expect(response.transaction_id).not_to be_nil
38
37
 
39
- success, response = AlphaCard::Capture.new(transaction_id: response.transaction_id, amount: '2.00').create(account)
40
- expect(success).to be_truthy
38
+ response = AlphaCard::Capture.new(transaction_id: response.transaction_id, amount: '2.00').create
39
+ expect(response.success?).to be_truthy
41
40
  expect(response.text).to eq('SUCCESS')
42
41
  end
43
42
  end
44
43
 
45
44
  context 'with blank attributes' do
46
45
  it 'raises an InvalidObject error' do
47
- expect { AlphaCard::Refund.new.create(account) }.to raise_error(AlphaCard::InvalidObjectError)
48
- expect { AlphaCard::Refund.new(amount: '1.05').create(account) }.to raise_error(AlphaCard::InvalidObjectError)
46
+ expect { AlphaCard::Refund.new.process }.to raise_error(AlphaCard::ValidationError)
47
+ expect { AlphaCard::Refund.new(amount: '1.05').process }.to raise_error(AlphaCard::ValidationError)
49
48
  end
50
49
  end
51
50
  end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlphaCard::Credit do
4
+ let(:order) { AlphaCard::Order.new(id: '1', description: 'Test') }
5
+ let(:card_exp) { (Time.now + 31104000).strftime('%m%y') }
6
+
7
+ context 'with valid attributes' do
8
+ let(:credit) { AlphaCard::Credit.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
9
+
10
+ it 'has valid request params' do
11
+ expected_params = {
12
+ ccexp: card_exp,
13
+ ccnumber: '4111111111111111',
14
+ amount: '5.00',
15
+ payment: 'creditcard',
16
+ orderid: '1',
17
+ orderdescription: 'Test',
18
+ type: 'credit',
19
+ }
20
+
21
+ expect(credit.send(:params_for_sale, order)).to eq(expected_params)
22
+ end
23
+
24
+ it 'successfully creates the credit' do
25
+ response = credit.create(order)
26
+ expect(response.success?).to be_truthy
27
+ end
28
+ end
29
+
30
+ context 'with invalid amount' do
31
+ let(:credit) { AlphaCard::Credit.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '0.00') }
32
+ let(:response) { credit.process(order) }
33
+
34
+ it 'returns an error' do
35
+ expect(response.error?).to be_truthy
36
+ expect(response.text).to include('Invalid amount')
37
+ end
38
+ end
39
+
40
+ context 'without attributes' do
41
+ let(:credit) { AlphaCard::Credit.new }
42
+
43
+ it 'raises an InvalidObjectError exception' do
44
+ expect { credit.create(order) }.to raise_error(AlphaCard::ValidationError)
45
+ end
46
+ end
47
+
48
+ context 'with invalid account credentials' do
49
+ let(:credit) { AlphaCard::Credit.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
50
+ let(:response) { credit.process(order, username: 'demo', password: 'Invalid password') }
51
+
52
+ it 'returns an error' do
53
+ expect(response.error?).to be_truthy
54
+ expect(response.text).to include('Authentication Failed')
55
+ end
56
+ end
57
+
58
+ context 'with blank account credentials' do
59
+ let(:credit) { AlphaCard::Credit.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
60
+
61
+ it 'raises an ArgumentError' do
62
+ expect { credit.process(order, username: nil, password: '') }.to raise_error(ArgumentError) do |e|
63
+ expect(e.message).to include('You must pass a Hash with Account credentials!')
64
+ end
65
+ end
66
+ end
67
+
68
+ context 'with connection errors' do
69
+ let(:timeout_error) { Timeout::Error.new }
70
+ let(:socket_error) { SocketError.new }
71
+ let(:unclassified_error) { StandardError.new('Some error') }
72
+
73
+ it 'raises an APIConnectionError if Timeout Error' do
74
+ expect { AlphaCard.handle_connection_errors(timeout_error) }.to raise_error(AlphaCard::APIConnectionError) do |e|
75
+ expect(e.message).to include('Could not connect to Alpha Card Gateway')
76
+ end
77
+ end
78
+
79
+ it 'raises an APIConnectionError if Socket Error' do
80
+ expect { AlphaCard.handle_connection_errors(socket_error) }.to raise_error(AlphaCard::APIConnectionError) do |e|
81
+ expect(e.message).to include('Unexpected error communicating when trying to connect to Alpha Card Gateway')
82
+ end
83
+ end
84
+
85
+ it 'raises an APIConnectionError if Unclassified Error' do
86
+ expect { AlphaCard.handle_connection_errors(unclassified_error) }.to raise_error(AlphaCard::APIConnectionError) do |e|
87
+ expect(e.message).to include('Unexpected error communicating with Alpha Card Gateway')
88
+ end
89
+ end
90
+
91
+ context 'with request exception' do
92
+ let(:credit) { AlphaCard::Credit.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
93
+
94
+ it 'handles an error' do
95
+ AlphaCard.api_base = 'https://not-existing.com'
96
+ expect { credit.create(order) }.to raise_error(AlphaCard::APIConnectionError)
97
+
98
+ AlphaCard.api_base = 'https://secure.alphacardgateway.com/api/transact.php'
99
+ end
100
+ end
101
+ end
102
+ end
@@ -1,13 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AlphaCard::Refund do
4
- let(:account) { AlphaCard::Account.new('demo', 'password') }
5
-
6
4
  context 'with invalid attributes' do
7
5
  let(:refund) { AlphaCard::Refund.new(transaction_id: 'Some ID', amount: '10.05') }
6
+ let(:response) { refund.process }
8
7
 
9
8
  it 'response with error' do
10
- expect { refund.create(account) }.to raise_error(AlphaCard::AlphaCardError)
9
+ expect(response.error?).to be_truthy
10
+ expect(response.message).to eq('Transaction was rejected by gateway')
11
11
  end
12
12
  end
13
13
 
@@ -29,7 +29,7 @@ describe AlphaCard::Refund do
29
29
  let(:refund) { AlphaCard::Refund.new }
30
30
 
31
31
  it 'raises an InvalidObject error' do
32
- expect { refund.create(account) }.to raise_error(AlphaCard::InvalidObjectError)
32
+ expect { refund.process }.to raise_error(AlphaCard::ValidationError)
33
33
  end
34
34
  end
35
35
  end
@@ -1,19 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AlphaCard::Sale do
4
- # Shared objects
5
- let(:account) { AlphaCard::Account.new('demo', 'password') }
6
- let(:billing) { AlphaCard::Billing.new(email: 'test@example.com', address_1: 'N', address_2: 'Y') }
7
- let(:shipping) { AlphaCard::Shipping.new(first_name: 'John', last_name: 'Doe', address_1: '22 N str.') }
4
+ let(:billing) { AlphaCard::Billing.new(email: 'test@example.com', address_1: 'N', address_2: 'Y', state: 'MN') }
5
+ let(:shipping) { AlphaCard::Shipping.new(first_name: 'John', last_name: 'Doe', address_1: '22 N str.', state: 'MN') }
8
6
  let(:order) { AlphaCard::Order.new(id: '1', description: 'Test', billing: billing, shipping: shipping) }
9
- let(:card_exp) { "#{'%02d' % Time.now.month}/#{Time.now.year.next}" }
7
+ let(:card_exp) { (Time.now + 31104000).strftime('%m%y') }
10
8
 
11
9
  context 'with valid attributes' do
12
10
  let(:sale) { AlphaCard::Sale.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
13
11
 
14
12
  it 'has valid request params' do
15
13
  expected_params = {
16
- ccexp: '09/2017',
14
+ ccexp: card_exp,
17
15
  ccnumber: '4111111111111111',
18
16
  amount: '5.00',
19
17
  payment: 'creditcard',
@@ -21,87 +19,90 @@ describe AlphaCard::Sale do
21
19
  email: 'test@example.com',
22
20
  address1: 'N',
23
21
  address2: 'Y',
22
+ state: 'MN',
24
23
  orderid: '1',
25
24
  orderdescription: 'Test',
26
25
  shipping_address_1: '22 N str.',
27
26
  shipping_first_name: 'John',
28
- shipping_last_name: 'Doe'
27
+ shipping_last_name: 'Doe',
28
+ shipping_state: 'MN'
29
29
  }
30
30
 
31
31
  expect(sale.send(:params_for_sale, order)).to eq(expected_params)
32
32
  end
33
33
 
34
34
  it 'successfully creates the sale' do
35
- expect(sale.create(order, account)).to be_truthy
35
+ expect(sale.create(order)).to be_truthy
36
36
  end
37
37
  end
38
38
 
39
39
  context 'with invalid Card number' do
40
40
  let(:sale) { AlphaCard::Sale.new(card_expiration_date: card_exp, card_number: 'Invalid', amount: '5.00') }
41
+ let(:response) { sale.process(order) }
41
42
 
42
- it 'raises an AlphaCardError' do
43
- expect { sale.create(order, account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
44
- expect(e.message).to include('Card number must contain only digits')
45
- end
43
+ it 'returns an error' do
44
+ expect(response.error?).to be_truthy
45
+ expect(response.text).to include('Card number must contain only digits')
46
46
  end
47
47
  end
48
48
 
49
49
  context 'with invalid amount' do
50
50
  let(:sale) { AlphaCard::Sale.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '0.00') }
51
+ let(:response) { sale.process(order) }
51
52
 
52
- it 'raises an AlphaCardError' do
53
- expect { sale.create(order, account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
54
- expect(e.message).to include('Invalid amount')
55
- end
53
+ it 'returns an error' do
54
+ expect(response.error?).to be_truthy
55
+ expect(response.text).to include('Invalid amount')
56
56
  end
57
57
  end
58
58
 
59
59
  context 'with invalid Card expiration date' do
60
- let(:sale) { AlphaCard::Sale.new(card_expiration_date: 'Invalid', card_number: '4111111111111111', amount: '5.00') }
60
+ let(:sale) { AlphaCard::Sale.new(card_expiration_date: '1299', card_number: '4111111111111111', amount: '5.00') }
61
61
 
62
- it 'raises an AlphaCardError' do
63
- expect { sale.create(order, account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
64
- expect(e.message).to include('Card expiration should be in the format')
65
- end
62
+ it 'returns an error' do
63
+ # Override expiration date without writer (to ignore validation)
64
+ sale.instance_variable_set(:'@card_expiration_date', '9999')
65
+ response = sale.process(order)
66
+
67
+ expect(response.error?).to be_truthy
68
+ expect(response.text).to include('Card expiration should be in the format')
66
69
  end
67
70
  end
68
71
 
69
72
  context 'with invalid Card CVV' do
70
73
  let(:sale) { AlphaCard::Sale.new(card_expiration_date: card_exp, cvv: 'Invalid', card_number: '4111111111111111', amount: '5.00') }
74
+ let(:response) { sale.process(order) }
71
75
 
72
- it 'raises an AlphaCardError' do
73
- expect { sale.create(order, account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
74
- expect(e.message).to include('CVV must be')
75
- end
76
+ it 'returns an error' do
77
+ expect(response.error?).to be_truthy
78
+ expect(response.text).to include('CVV must be')
76
79
  end
77
80
  end
78
81
 
79
- context 'with invalid account credentials' do
80
- let(:invalid_account) { AlphaCard::Account.new('demo', 'Invalid password') }
81
- let(:sale) { AlphaCard::Sale.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
82
+ context 'without attributes' do
83
+ let(:sale) { AlphaCard::Sale.new }
82
84
 
83
- it 'raises an AlphaCardError' do
84
- expect { sale.create(order, invalid_account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
85
- expect(e.message).to include('Authentication Failed')
86
- end
85
+ it 'raises an InvalidObjectError exception' do
86
+ expect { sale.create(order) }.to raise_error(AlphaCard::ValidationError, "card_expiration_date can't be blank")
87
87
  end
88
88
  end
89
89
 
90
- context 'without attributes' do
91
- let(:sale) { AlphaCard::Sale.new }
90
+ context 'with invalid account credentials' do
91
+ let(:sale) { AlphaCard::Sale.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
92
+ let(:response) { sale.create(order, username: 'demo', password: 'Invalid password') }
92
93
 
93
- it 'raises an InvalidObjectError exception' do
94
- expect { sale.create(order, account) }.to raise_error(AlphaCard::InvalidObjectError)
94
+ it 'returns an error' do
95
+ expect(response.error?).to be_truthy
96
+ expect(response.text).to include('Authentication Failed')
95
97
  end
96
98
  end
97
99
 
98
100
  context 'with blank account credentials' do
99
- let(:blank_account) { AlphaCard::Account.new(nil, '') }
100
101
  let(:sale) { AlphaCard::Sale.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '5.00') }
101
102
 
102
- it 'raises an AlphaCardError' do
103
- expect { sale.create(order, blank_account) }.to raise_error(AlphaCard::AlphaCardError) do |e|
104
- expect(e.message).to include('You must set credentials to create the sale')
103
+ it 'raises an ArgumentError' do
104
+ expect { sale.process(order, username: nil, password: '') }.to raise_error(ArgumentError) do |e|
105
+ expect(e.message).to include('You must pass a Hash with Account credentials!')
105
106
  end
106
107
  end
107
108
  end
@@ -134,7 +135,7 @@ describe AlphaCard::Sale do
134
135
 
135
136
  it 'handles an error' do
136
137
  AlphaCard.api_base = 'https://not-existing.com'
137
- expect { sale.create(order, account) }.to raise_error(AlphaCard::APIConnectionError)
138
+ expect { sale.create(order) }.to raise_error(AlphaCard::APIConnectionError)
138
139
 
139
140
  AlphaCard.api_base = 'https://secure.alphacardgateway.com/api/transact.php'
140
141
  end
@@ -1,13 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AlphaCard::Update do
4
- let(:account) { AlphaCard::Account.new('demo', 'password') }
5
-
6
4
  context 'with invalid attributes' do
7
5
  let(:update) { AlphaCard::Update.new(transaction_id: 'Some ID') }
6
+ let(:response) { update.process }
8
7
 
9
8
  it 'response with error' do
10
- expect { update.create(account) }.to raise_error(AlphaCard::AlphaCardError)
9
+ expect(response.error?).to be_truthy
10
+ expect(response.message).to eq('Transaction was rejected by gateway')
11
11
  end
12
12
  end
13
13
 
@@ -30,7 +30,7 @@ describe AlphaCard::Update do
30
30
  let(:update) { AlphaCard::Update.new }
31
31
 
32
32
  it 'raises an InvalidObject error' do
33
- expect { update.create(account) }.to raise_error(AlphaCard::InvalidObjectError)
33
+ expect { update.create }.to raise_error(AlphaCard::ValidationError)
34
34
  end
35
35
  end
36
36
  end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlphaCard::Validate do
4
+ let(:order) { AlphaCard::Order.new(id: '1', description: 'Test') }
5
+ let(:card_exp) { (Time.now + 31104000).strftime('%m%y') }
6
+
7
+ context 'with valid attributes' do
8
+ let(:validate) { AlphaCard::Validate.new(card_expiration_date: card_exp, card_number: '4111111111111111') }
9
+
10
+ it 'has valid request params' do
11
+ expected_params = {
12
+ ccexp: card_exp,
13
+ ccnumber: '4111111111111111',
14
+ payment: 'creditcard',
15
+ orderid: '1',
16
+ orderdescription: 'Test',
17
+ type: 'validate',
18
+ }
19
+
20
+ expect(validate.send(:params_for_sale, order)).to eq(expected_params)
21
+ end
22
+
23
+ it 'successfully processed' do
24
+ response = validate.create(order)
25
+ expect(response.success?).to be_truthy
26
+ end
27
+ end
28
+
29
+ context 'with amount' do
30
+ let(:validate) { described_class.new(card_expiration_date: card_exp, card_number: '4111111111111111', amount: '1.22') }
31
+
32
+ it 'must ignore it' do
33
+ expect(validate.send(:params_for_sale, order)).not_to include(:amount)
34
+ end
35
+ end
36
+
37
+
38
+ context 'without attributes' do
39
+ let(:validate) { described_class.new }
40
+
41
+ it 'raises an InvalidObjectError exception' do
42
+ expect { validate.create(order) }.to raise_error(AlphaCard::ValidationError)
43
+ end
44
+ end
45
+
46
+ context 'with invalid account credentials' do
47
+ let(:validate) { described_class.new(card_expiration_date: card_exp, card_number: '4111111111111111') }
48
+ let(:response) { validate.process(order, username: 'demo', password: 'Invalid password') }
49
+
50
+ it 'returns an error' do
51
+ expect(response.error?).to be_truthy
52
+ expect(response.text).to include('Authentication Failed')
53
+ end
54
+ end
55
+
56
+ context 'with blank account credentials' do
57
+ let(:validate) { described_class.new(card_expiration_date: card_exp, card_number: '4111111111111111') }
58
+
59
+ it 'raises an ArgumentError' do
60
+ expect { validate.process(order, username: nil, password: '') }.to raise_error(ArgumentError) do |e|
61
+ expect(e.message).to include('You must pass a Hash with Account credentials!')
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'with connection errors' do
67
+ let(:timeout_error) { Timeout::Error.new }
68
+ let(:socket_error) { SocketError.new }
69
+ let(:unclassified_error) { StandardError.new('Some error') }
70
+
71
+ it 'raises an APIConnectionError if Timeout Error' do
72
+ expect { AlphaCard.handle_connection_errors(timeout_error) }.to raise_error(AlphaCard::APIConnectionError) do |e|
73
+ expect(e.message).to include('Could not connect to Alpha Card Gateway')
74
+ end
75
+ end
76
+
77
+ it 'raises an APIConnectionError if Socket Error' do
78
+ expect { AlphaCard.handle_connection_errors(socket_error) }.to raise_error(AlphaCard::APIConnectionError) do |e|
79
+ expect(e.message).to include('Unexpected error communicating when trying to connect to Alpha Card Gateway')
80
+ end
81
+ end
82
+
83
+ it 'raises an APIConnectionError if Unclassified Error' do
84
+ expect { AlphaCard.handle_connection_errors(unclassified_error) }.to raise_error(AlphaCard::APIConnectionError) do |e|
85
+ expect(e.message).to include('Unexpected error communicating with Alpha Card Gateway')
86
+ end
87
+ end
88
+
89
+ context 'with request exception' do
90
+ let(:validate) { described_class.new(card_expiration_date: card_exp, card_number: '4111111111111111') }
91
+
92
+ it 'handles an error' do
93
+ AlphaCard.api_base = 'https://not-existing.com'
94
+ expect { validate.create(order) }.to raise_error(AlphaCard::APIConnectionError)
95
+
96
+ AlphaCard.api_base = 'https://secure.alphacardgateway.com/api/transact.php'
97
+ end
98
+ end
99
+ end
100
+ end