spreedly 1.4.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/.gitignore +8 -0
  2. data/Gemfile +4 -0
  3. data/HISTORY.md +5 -0
  4. data/README.md +362 -29
  5. data/Rakefile +32 -0
  6. data/lib/certs/cacert.pem +7815 -0
  7. data/lib/spreedly.rb +24 -282
  8. data/lib/spreedly/common/errors_parser.rb +15 -0
  9. data/lib/spreedly/common/fields.rb +90 -0
  10. data/lib/spreedly/connection.rb +40 -0
  11. data/lib/spreedly/environment.rb +176 -0
  12. data/lib/spreedly/error.rb +50 -0
  13. data/lib/spreedly/gateway.rb +10 -0
  14. data/lib/spreedly/model.rb +17 -0
  15. data/lib/spreedly/payment_methods/credit_card.rb +9 -0
  16. data/lib/spreedly/payment_methods/payment_method.rb +34 -0
  17. data/lib/spreedly/payment_methods/paypal.rb +7 -0
  18. data/lib/spreedly/payment_methods/sprel.rb +7 -0
  19. data/lib/spreedly/ssl_requester.rb +65 -0
  20. data/lib/spreedly/transactions/add_payment_method.rb +16 -0
  21. data/lib/spreedly/transactions/auth_purchase.rb +17 -0
  22. data/lib/spreedly/transactions/authorization.rb +7 -0
  23. data/lib/spreedly/transactions/capture.rb +14 -0
  24. data/lib/spreedly/transactions/gateway_transaction.rb +31 -0
  25. data/lib/spreedly/transactions/purchase.rb +7 -0
  26. data/lib/spreedly/transactions/redact_payment_method.rb +14 -0
  27. data/lib/spreedly/transactions/refund.rb +14 -0
  28. data/lib/spreedly/transactions/retain_payment_method.rb +14 -0
  29. data/lib/spreedly/transactions/transaction.rb +41 -0
  30. data/lib/spreedly/transactions/void.rb +9 -0
  31. data/lib/spreedly/urls.rb +64 -0
  32. data/lib/spreedly/version.rb +1 -1
  33. data/spreedly.gemspec +29 -0
  34. data/test/credentials/credentials.yml +9 -0
  35. data/test/credentials/test_credentials.rb +43 -0
  36. data/test/helpers/assertions.rb +29 -0
  37. data/test/helpers/communication_helper.rb +31 -0
  38. data/test/helpers/creation_helper.rb +26 -0
  39. data/test/helpers/stub_response.rb +18 -0
  40. data/test/remote/remote_add_credit_card_test.rb +62 -0
  41. data/test/remote/remote_add_gateway_test.rb +30 -0
  42. data/test/remote/remote_authorize_test.rb +48 -0
  43. data/test/remote/remote_capture_test.rb +71 -0
  44. data/test/remote/remote_find_gateway_test.rb +31 -0
  45. data/test/remote/remote_find_payment_method_test.rb +29 -0
  46. data/test/remote/remote_find_transaction_test.rb +33 -0
  47. data/test/remote/remote_list_transactions_test.rb +36 -0
  48. data/test/remote/remote_purchase_test.rb +69 -0
  49. data/test/remote/remote_redact_test.rb +38 -0
  50. data/test/remote/remote_refund_test.rb +65 -0
  51. data/test/remote/remote_retain_test.rb +39 -0
  52. data/test/remote/remote_void_test.rb +64 -0
  53. data/test/test_helper.rb +23 -0
  54. data/test/unit/add_credit_card_test.rb +74 -0
  55. data/test/unit/add_gateway_test.rb +26 -0
  56. data/test/unit/authorize_test.rb +87 -0
  57. data/test/unit/capture_test.rb +91 -0
  58. data/test/unit/environment_test.rb +18 -0
  59. data/test/unit/fields_test.rb +75 -0
  60. data/test/unit/find_gateway_test.rb +28 -0
  61. data/test/unit/find_payment_method_test.rb +90 -0
  62. data/test/unit/find_transaction_test.rb +31 -0
  63. data/test/unit/list_transactions_test.rb +46 -0
  64. data/test/unit/purchase_test.rb +95 -0
  65. data/test/unit/redact_payment_method_test.rb +51 -0
  66. data/test/unit/refund_test.rb +91 -0
  67. data/test/unit/response_stubs/add_credit_card_stubs.rb +43 -0
  68. data/test/unit/response_stubs/add_gateway_stubs.rb +39 -0
  69. data/test/unit/response_stubs/authorization_stubs.rb +139 -0
  70. data/test/unit/response_stubs/capture_stubs.rb +87 -0
  71. data/test/unit/response_stubs/find_gateway_stubs.rb +38 -0
  72. data/test/unit/response_stubs/find_payment_method_stubs.rb +108 -0
  73. data/test/unit/response_stubs/find_transaction_stubs.rb +43 -0
  74. data/test/unit/response_stubs/list_transactions_stubs.rb +110 -0
  75. data/test/unit/response_stubs/purchase_stubs.rb +139 -0
  76. data/test/unit/response_stubs/redact_payment_method_stubs.rb +54 -0
  77. data/test/unit/response_stubs/refund_stubs.rb +87 -0
  78. data/test/unit/response_stubs/retain_payment_method_stubs.rb +85 -0
  79. data/test/unit/response_stubs/void_stubs.rb +79 -0
  80. data/test/unit/retain_payment_method_test.rb +44 -0
  81. data/test/unit/timeout_test.rb +20 -0
  82. data/test/unit/void_test.rb +96 -0
  83. metadata +215 -29
  84. checksums.yaml +0 -15
  85. data/lib/spreedly/common.rb +0 -44
  86. data/lib/spreedly/mock.rb +0 -221
  87. data/lib/spreedly/test_hacks.rb +0 -27
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteAddGatewayTest < Test::Unit::TestCase
4
+
5
+ # TODO We'll add more to this soon.
6
+
7
+ def setup
8
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
9
+ end
10
+
11
+ def test_invalid_login
12
+ assert_invalid_login do |environment|
13
+ environment.add_gateway(:test)
14
+ end
15
+ end
16
+
17
+ def test_non_existent_gateway_type
18
+ assert_raise_with_message(Spreedly::TransactionCreationError, "The gateway_type parameter is invalid.") do
19
+ @environment.add_gateway(:non_existent)
20
+ end
21
+ end
22
+
23
+ def test_add_test_gateway
24
+ gateway = @environment.add_gateway(:test)
25
+ assert_equal "test", gateway.gateway_type
26
+ assert_equal "retained", gateway.state
27
+ assert_equal "Test", gateway.name
28
+ end
29
+
30
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteAuthorizeTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.authorize_on_gateway('gtoken', 'ptoken', 100)
12
+ end
13
+ end
14
+
15
+ def test_payment_method_not_found
16
+ assert_raise_with_message(Spreedly::TransactionCreationError, "There is no payment method corresponding to the specified payment method token.") do
17
+ @environment.authorize_on_gateway('gateway_token', 'unknown_payment_method', 100)
18
+ end
19
+ end
20
+
21
+ def test_gateway_not_found
22
+ card_token = create_card_on(@environment).token
23
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified gateway.") do
24
+ @environment.authorize_on_gateway('unknown_gateway', card_token, 100)
25
+ end
26
+ end
27
+
28
+ def test_successful_authorize
29
+ gateway_token = @environment.add_gateway(:test).token
30
+ card_token = create_card_on(@environment).token
31
+
32
+ transaction = @environment.authorize_on_gateway(gateway_token, card_token, 899)
33
+ assert transaction.succeeded?
34
+ assert_equal card_token, transaction.payment_method.token
35
+ assert_equal 899, transaction.amount
36
+ end
37
+
38
+ def test_failed_authorize
39
+ gateway_token = @environment.add_gateway(:test).token
40
+ card_token = create_failed_card_on(@environment).token
41
+
42
+ transaction = @environment.authorize_on_gateway(gateway_token, card_token, 144)
43
+ assert !transaction.succeeded?
44
+ assert_equal "Unable to process the authorize transaction.", transaction.message
45
+ assert_equal gateway_token, transaction.gateway_token
46
+ end
47
+
48
+ end
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteCaptureTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.capture_transaction('authorize_token')
12
+ end
13
+ end
14
+
15
+ def test_authorization_token_found
16
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified reference transaction.") do
17
+ @environment.capture_transaction('unknown_auth_token')
18
+ end
19
+ end
20
+
21
+ def test_needs_succeeded_reference
22
+ gateway_token = @environment.add_gateway(:test).token
23
+ card_token = create_failed_card_on(@environment).token
24
+ authorization = @environment.authorize_on_gateway(gateway_token, card_token, 144)
25
+ assert !authorization.succeeded?
26
+
27
+ assert_raise_with_message(Spreedly::TransactionCreationError, "The reference transaction did not succeed. Only successful reference transactions are permitted.") do
28
+ @environment.capture_transaction(authorization.token)
29
+ end
30
+ end
31
+
32
+ def test_successful_capture_full_amount
33
+ gateway_token = @environment.add_gateway(:test).token
34
+ card_token = create_card_on(@environment).token
35
+
36
+ authorization = @environment.authorize_on_gateway(gateway_token, card_token, 800)
37
+ assert authorization.succeeded?
38
+
39
+ capture = @environment.capture_transaction(authorization.token)
40
+ assert capture.succeeded?
41
+ assert_equal authorization.token, capture.reference_token
42
+ assert_equal 800, capture.amount
43
+ end
44
+
45
+ def test_successful_capture_partial_amount
46
+ gateway_token = @environment.add_gateway(:test).token
47
+ card_token = create_card_on(@environment).token
48
+
49
+ authorization = @environment.authorize_on_gateway(gateway_token, card_token, 800)
50
+ assert authorization.succeeded?
51
+
52
+ capture = @environment.capture_transaction(authorization.token, amount: 322)
53
+ assert capture.succeeded?
54
+ assert_equal authorization.token, capture.reference_token
55
+ assert_equal 322, capture.amount
56
+ end
57
+
58
+ def test_failed_capture
59
+ gateway_token = @environment.add_gateway(:test).token
60
+ card_token = create_card_on(@environment).token
61
+ authorization = @environment.authorize_on_gateway(gateway_token, card_token, 800)
62
+ assert authorization.succeeded?
63
+
64
+ capture = @environment.capture_transaction(authorization.token, amount: 44)
65
+ assert !capture.succeeded?
66
+ assert_equal authorization.token, capture.reference_token
67
+ assert_equal 44, capture.amount
68
+ assert_equal 'Unable to process the capture transaction.', capture.message
69
+ end
70
+
71
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteFindGatewayTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.find_gateway("SomeToken")
12
+ end
13
+ end
14
+
15
+ def test_gateway_not_found
16
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified gateway.") do
17
+ @environment.find_gateway("SomeUnknownToken")
18
+ end
19
+ end
20
+
21
+ def test_successfully_find_gateway
22
+ gateway = @environment.add_gateway(:test)
23
+
24
+ found = @environment.find_gateway(gateway.token)
25
+
26
+ assert_equal gateway.token, found.token
27
+ assert_equal "Test", gateway.name
28
+ assert_equal "retained", gateway.state
29
+ end
30
+
31
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteFindPaymentMethodTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.find_payment_method("SomeToken")
12
+ end
13
+ end
14
+
15
+ def test_payment_method_not_found
16
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified payment method.") do
17
+ @environment.find_payment_method("SomeUnknownToken")
18
+ end
19
+ end
20
+
21
+ def test_successfully_find_card
22
+ found = @environment.find_payment_method(create_card_on(@environment).token)
23
+ assert_kind_of(Spreedly::CreditCard, found)
24
+ assert_equal("perrin@wot.com", found.email)
25
+ assert_equal('XXXX-XXXX-XXXX-4444', found.number)
26
+ assert_equal('Aybara', found.last_name)
27
+ end
28
+
29
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteFindTransactionTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.find_transaction("SomeToken")
12
+ end
13
+ end
14
+
15
+ def test_transaction_not_found
16
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the transaction SomeUnknownToken.") do
17
+ @environment.find_transaction("SomeUnknownToken")
18
+ end
19
+ end
20
+
21
+ def test_successfully_find_transaction
22
+ gateway_token = @environment.add_gateway(:test).token
23
+ card_token = create_failed_card_on(@environment).token
24
+ transaction = @environment.purchase_on_gateway(gateway_token, card_token, 144)
25
+
26
+ found = @environment.find_transaction(transaction.token)
27
+
28
+ assert_kind_of(Spreedly::Purchase, found)
29
+ assert_equal transaction.token, found.token
30
+ assert_equal('Aybara', found.payment_method.last_name)
31
+ end
32
+
33
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteTransactionsTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.list_transactions
12
+ end
13
+ end
14
+
15
+ def test_successfully_list_transactions
16
+ gateway_token = @environment.add_gateway(:test).token
17
+ card_token = create_card_on(@environment).token
18
+
19
+ t1 = @environment.purchase_on_gateway(gateway_token, card_token, 144)
20
+ t2 = @environment.authorize_on_gateway(gateway_token, card_token, 444)
21
+ t3 = @environment.capture_transaction(t2.token, amount: 22)
22
+
23
+ first_twenty = @environment.list_transactions
24
+ assert_equal 20, first_twenty.size
25
+ assert_kind_of Spreedly::Model, first_twenty.first
26
+
27
+ transactions = @environment.list_transactions(t1.token)
28
+ assert_equal 2, transactions.size
29
+ assert_kind_of(Spreedly::Authorization, transactions.first)
30
+ assert_kind_of(Spreedly::Capture, transactions.last)
31
+
32
+ assert_equal 444, transactions.first.amount
33
+ assert_equal 22, transactions.last.amount
34
+ end
35
+
36
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class RemotePurchaseTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.purchase_on_gateway('gtoken', 'ptoken', 100)
12
+ end
13
+ end
14
+
15
+ def test_payment_method_not_found
16
+ assert_raise_with_message(Spreedly::TransactionCreationError, "There is no payment method corresponding to the specified payment method token.") do
17
+ @environment.purchase_on_gateway('gateway_token', 'unknown_payment_method', 100)
18
+ end
19
+ end
20
+
21
+ def test_gateway_not_found
22
+ card_token = create_card_on(@environment).token
23
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified gateway.") do
24
+ @environment.purchase_on_gateway('unknown_gateway', card_token, 100)
25
+ end
26
+ end
27
+
28
+ def test_successful_purchase
29
+ gateway_token = @environment.add_gateway(:test).token
30
+ card_token = create_card_on(@environment).token
31
+
32
+ transaction = @environment.purchase_on_gateway(gateway_token, card_token, 144)
33
+ assert transaction.succeeded?
34
+ assert_equal card_token, transaction.payment_method.token
35
+ assert_equal 144, transaction.amount
36
+ end
37
+
38
+ def test_failed_purchase
39
+ gateway_token = @environment.add_gateway(:test).token
40
+ card_token = create_failed_card_on(@environment).token
41
+
42
+ transaction = @environment.purchase_on_gateway(gateway_token, card_token, 144)
43
+ assert !transaction.succeeded?
44
+ assert_equal "Unable to process the purchase transaction.", transaction.message
45
+ assert_equal gateway_token, transaction.gateway_token
46
+ end
47
+
48
+ def test_optional_arguments
49
+ gateway_token = @environment.add_gateway(:test).token
50
+ card_token = create_card_on(@environment, retained: false).token
51
+
52
+ transaction = @environment.purchase_on_gateway(gateway_token, card_token, 344,
53
+ order_id: "8675",
54
+ description: "SuperDuper",
55
+ ip: "183.128.100.103",
56
+ merchant_name_descriptor: "Real Stuff",
57
+ merchant_location_descriptor: "Raleigh",
58
+ retain_on_success: true)
59
+
60
+ assert transaction.succeeded?
61
+ assert_equal "8675", transaction.order_id
62
+ assert_equal "SuperDuper", transaction.description
63
+ assert_equal "183.128.100.103", transaction.ip
64
+ assert_equal "Real Stuff", transaction.merchant_name_descriptor
65
+ assert_equal "Raleigh", transaction.merchant_location_descriptor
66
+ assert_equal "retained", transaction.payment_method.storage_state
67
+ end
68
+
69
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteRedactTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.redact_payment_method('payment_method_token')
12
+ end
13
+ end
14
+
15
+ def test_payment_method_token_token_not_found
16
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified payment method.") do
17
+ @environment.redact_payment_method('unknown_token')
18
+ end
19
+ end
20
+
21
+ def test_successful_redact
22
+ card_token = create_card_on(@environment).token
23
+
24
+ transaction = @environment.redact_payment_method(card_token)
25
+
26
+ assert transaction.succeeded?
27
+ assert_equal 'redacted', transaction.payment_method.storage_state
28
+ end
29
+
30
+ def test_remove_from_gateway_unknown_gateway
31
+ card_token = create_card_on(@environment).token
32
+
33
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified gateway.") do
34
+ transaction = @environment.redact_payment_method(card_token, remove_from_gateway: 'unknown_token')
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteRefundTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @environment = Spreedly::Environment.new(remote_test_environment_key, remote_test_access_secret)
7
+ end
8
+
9
+ def test_invalid_login
10
+ assert_invalid_login do |environment|
11
+ environment.refund_transaction('authorize_token')
12
+ end
13
+ end
14
+
15
+ def test_transaction_token_found
16
+ assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified reference transaction.") do
17
+ @environment.refund_transaction('unknown_transaction_token')
18
+ end
19
+ end
20
+
21
+ def test_needs_succeeded_reference
22
+ gateway_token = @environment.add_gateway(:test).token
23
+ card_token = create_failed_card_on(@environment).token
24
+ purchase = @environment.purchase_on_gateway(gateway_token, card_token, 144)
25
+ assert !purchase.succeeded?
26
+
27
+ assert_raise_with_message(Spreedly::TransactionCreationError, "The reference transaction did not succeed. Only successful reference transactions are permitted.") do
28
+ @environment.refund_transaction(purchase.token)
29
+ end
30
+ end
31
+
32
+ def test_successful_refund_full_amount
33
+ gateway_token = @environment.add_gateway(:test).token
34
+ card_token = create_card_on(@environment).token
35
+ purchase = @environment.purchase_on_gateway(gateway_token, card_token, 944)
36
+
37
+ refund = @environment.refund_transaction(purchase.token)
38
+ assert refund.succeeded?
39
+ assert_equal purchase.token, refund.reference_token
40
+ assert_equal 944, refund.amount
41
+ end
42
+
43
+ def test_successful_refund_partial_amount
44
+ gateway_token = @environment.add_gateway(:test).token
45
+ card_token = create_card_on(@environment).token
46
+ purchase = @environment.purchase_on_gateway(gateway_token, card_token, 1944)
47
+
48
+ refund = @environment.refund_transaction(purchase.token, amount: 323)
49
+ assert refund.succeeded?
50
+ assert_equal 323, refund.amount
51
+ end
52
+
53
+ def test_failed_refund
54
+ gateway_token = @environment.add_gateway(:test).token
55
+ card_token = create_card_on(@environment).token
56
+ purchase = @environment.purchase_on_gateway(gateway_token, card_token, 1944)
57
+
58
+ refund = @environment.refund_transaction(purchase.token, amount: 44)
59
+ assert !refund.succeeded?
60
+ assert_equal purchase.token, refund.reference_token
61
+ assert_equal 44, refund.amount
62
+ assert_equal 'Unable to process the credit transaction.', refund.message
63
+ end
64
+
65
+ end