judopay 1.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +47 -0
  5. data/.yardopts +1 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +43 -0
  9. data/Rakefile +7 -0
  10. data/judopay.gemspec +37 -0
  11. data/lib/certs/rapidssl_ca.crt +64 -0
  12. data/lib/faraday/judo_mashify.rb +13 -0
  13. data/lib/faraday/raise_http_exception.rb +36 -0
  14. data/lib/judopay.rb +67 -0
  15. data/lib/judopay/api.rb +10 -0
  16. data/lib/judopay/connection.rb +67 -0
  17. data/lib/judopay/core_ext/hash.rb +29 -0
  18. data/lib/judopay/core_ext/string.rb +31 -0
  19. data/lib/judopay/error.rb +153 -0
  20. data/lib/judopay/mash.rb +7 -0
  21. data/lib/judopay/model.rb +109 -0
  22. data/lib/judopay/models/card_address.rb +11 -0
  23. data/lib/judopay/models/card_payment.rb +39 -0
  24. data/lib/judopay/models/card_preauth.rb +12 -0
  25. data/lib/judopay/models/collection.rb +16 -0
  26. data/lib/judopay/models/consumer_location.rb +8 -0
  27. data/lib/judopay/models/market/collection.rb +10 -0
  28. data/lib/judopay/models/market/payment.rb +10 -0
  29. data/lib/judopay/models/market/preauth.rb +10 -0
  30. data/lib/judopay/models/market/refund.rb +10 -0
  31. data/lib/judopay/models/market/transaction.rb +10 -0
  32. data/lib/judopay/models/payment.rb +8 -0
  33. data/lib/judopay/models/preauth.rb +8 -0
  34. data/lib/judopay/models/refund.rb +16 -0
  35. data/lib/judopay/models/token_payment.rb +30 -0
  36. data/lib/judopay/models/token_preauth.rb +10 -0
  37. data/lib/judopay/models/transaction.rb +8 -0
  38. data/lib/judopay/models/web_payments/payment.rb +24 -0
  39. data/lib/judopay/models/web_payments/preauth.rb +10 -0
  40. data/lib/judopay/models/web_payments/transaction.rb +19 -0
  41. data/lib/judopay/null_logger.rb +11 -0
  42. data/lib/judopay/request.rb +50 -0
  43. data/lib/judopay/response.rb +7 -0
  44. data/lib/judopay/serializer.rb +31 -0
  45. data/lib/judopay/version.rb +3 -0
  46. data/spec/factories.rb +103 -0
  47. data/spec/faraday/response_spec.rb +29 -0
  48. data/spec/fixtures/card_payments/create.json +22 -0
  49. data/spec/fixtures/card_payments/create_3dsecure.json +8 -0
  50. data/spec/fixtures/card_payments/create_bad_request.json +5 -0
  51. data/spec/fixtures/card_payments/create_declined.json +24 -0
  52. data/spec/fixtures/card_payments/validate.json +5 -0
  53. data/spec/fixtures/token_payments/create.json +22 -0
  54. data/spec/fixtures/transactions/all.json +238 -0
  55. data/spec/fixtures/transactions/find.json +23 -0
  56. data/spec/fixtures/transactions/find_not_found.json +5 -0
  57. data/spec/fixtures/web_payments/payments/create.json +4 -0
  58. data/spec/fixtures/web_payments/payments/find.json +39 -0
  59. data/spec/judopay/card_address_spec.rb +10 -0
  60. data/spec/judopay/card_payment_spec.rb +64 -0
  61. data/spec/judopay/card_preauth_spec.rb +35 -0
  62. data/spec/judopay/collection_spec.rb +26 -0
  63. data/spec/judopay/core_ext/hash_spec.rb +24 -0
  64. data/spec/judopay/core_ext/string_spec.rb +16 -0
  65. data/spec/judopay/error_spec.rb +49 -0
  66. data/spec/judopay/judopay_spec.rb +19 -0
  67. data/spec/judopay/market/collection_spec.rb +26 -0
  68. data/spec/judopay/market/payment_spec.rb +14 -0
  69. data/spec/judopay/market/preauth_spec.rb +14 -0
  70. data/spec/judopay/market/refund_spec.rb +26 -0
  71. data/spec/judopay/market/transaction_spec.rb +14 -0
  72. data/spec/judopay/payment_spec.rb +14 -0
  73. data/spec/judopay/preauth_spec.rb +14 -0
  74. data/spec/judopay/refund_spec.rb +26 -0
  75. data/spec/judopay/token_payment_spec.rb +22 -0
  76. data/spec/judopay/token_preauth_spec.rb +22 -0
  77. data/spec/judopay/transaction_spec.rb +51 -0
  78. data/spec/judopay/web_payments/payment_spec.rb +16 -0
  79. data/spec/judopay/web_payments/preauth_spec.rb +16 -0
  80. data/spec/judopay/web_payments/transaction_spec.rb +15 -0
  81. data/spec/spec_helper.rb +37 -0
  82. data/tutorials/judo_getting_started.md +74 -0
  83. data/tutorials/judo_making_a_payment.md +84 -0
  84. metadata +373 -0
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/card_preauth'
3
+
4
+ describe Judopay::CardPreauth do
5
+ it 'should create a new preauth given valid card details' do
6
+ stub_post('/transactions/preauths').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('card_payments/create.json') })
9
+
10
+ payment = build(:card_preauth)
11
+ response = payment.create
12
+
13
+ expect(response).to be_a(Hash)
14
+ expect(response.result).to eq('Success')
15
+ end
16
+
17
+ it 'should not create a new preauth if the card is declined' do
18
+ stub_post('/transactions/preauths').
19
+ to_return(:status => 200,
20
+ :body => lambda { |_request| fixture('card_payments/create_declined.json') })
21
+
22
+ payment = build(:card_payment)
23
+ payment.card_number = '4221690000004963' # Always declined
24
+ response = payment.create
25
+
26
+ expect(response).to be_a(Hash)
27
+ expect(response.result).to eq('Declined')
28
+ end
29
+
30
+ it 'should return a bad request exception if basic validation fails' do
31
+ expect(lambda do
32
+ Judopay::CardPreauth.new.create
33
+ end).to raise_error(Judopay::ValidationError)
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/collection'
3
+
4
+ describe Judopay::Collection do
5
+ it 'should list all transactions' do
6
+ stub_get('/transactions/collections').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Collection.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+
15
+ it 'should create a new collection given a valid payment reference' do
16
+ stub_post('/transactions/collections').
17
+ to_return(:status => 200,
18
+ :body => lambda { |_request| fixture('card_payments/create.json') })
19
+
20
+ collection = build(:collection)
21
+ response = collection.create
22
+
23
+ expect(response).to be_a(Hash)
24
+ expect(response.result).to eq('Success')
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/judopay/core_ext/hash'
3
+
4
+ describe Hash do
5
+ it 'should convert hash keys to camel case' do
6
+ original = {
7
+ 'under_score' => 123,
8
+ 'more_words_please' => 'value'
9
+ }
10
+ expected_result = {
11
+ 'underScore' => 123,
12
+ 'moreWordsPlease' => 'value'
13
+ }
14
+ expect(original.camel_case_keys!).to eq(expected_result)
15
+ end
16
+
17
+ it 'should convert a hash to a valid query string' do
18
+ original = {
19
+ 'under_score' => 123,
20
+ 'more_words_please' => 'value'
21
+ }
22
+ expect(original.to_query_string).to eq('more_words_please=value&under_score=123')
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/judopay/core_ext/string'
3
+
4
+ describe String do
5
+ it 'should convert an underscored string to camel case' do
6
+ expect('judo_pay'.camel_case).to eq('judoPay')
7
+ end
8
+
9
+ it 'should convert an camel case string to underscored format' do
10
+ expect('JudoPay'.underscore).to eq('judo_pay')
11
+ end
12
+
13
+ it 'should uncapitalize a word' do
14
+ expect('JudoPay'.uncapitalize).to eq('judoPay')
15
+ end
16
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/error'
3
+
4
+ describe Judopay::Error do
5
+ it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do
6
+ e = Judopay::Error.new
7
+ expect(e.message).to eq('Judopay::Error')
8
+ end
9
+
10
+ it "returns the 'message' that was set" do
11
+ message = 'An explicitly set message'
12
+ e = Judopay::Error.new(message)
13
+ expect(e.message).to eq(message)
14
+ end
15
+
16
+ it 'contains exceptions in Judopay' do
17
+ expect(Judopay::BadRequest.new).to be_a_kind_of(Judopay::APIError)
18
+ end
19
+
20
+ it 'makes error information available on the exception object for API errors' do
21
+ stub_post('/transactions/payments').
22
+ to_return(:status => 400,
23
+ :body => lambda { |_request| fixture('card_payments/create_bad_request.json') },
24
+ :headers => { 'Content-Type' => 'application/json' })
25
+
26
+ payment = build(:card_payment)
27
+
28
+ begin
29
+ payment.create
30
+ rescue Judopay::BadRequest => e
31
+ expect(e.http_status).to eq(400)
32
+ expect(e.model_errors).to be_a_kind_of(Hash)
33
+ expect(e.error_type).to eq(9)
34
+ expect(e.message).to eq('Please check the card token.')
35
+ end
36
+ end
37
+
38
+ it 'makes error information available on the exception object for validation errors' do
39
+
40
+ payment = Judopay::CardPayment.new
41
+
42
+ begin
43
+ payment.create
44
+ rescue Judopay::ValidationError => e
45
+ expect(e.model_errors).to be_a_kind_of(Hash)
46
+ expect(e.message).to include('Missing required fields')
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/preauth'
3
+
4
+ describe Judopay do
5
+ it 'should use the production endpoint if use_production is true' do
6
+ expect(Judopay.configuration.endpoint_url).to include('sandbox')
7
+
8
+ Judopay.configure do |config|
9
+ config.use_production = true
10
+ end
11
+
12
+ expect(Judopay.configuration.endpoint_url).to include('production')
13
+
14
+ # Reset to sandbox for other tests
15
+ Judopay.configure do |config|
16
+ config.use_production = false
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/judopay/models/market/collection'
3
+
4
+ describe Judopay::Market::Collection do
5
+ it 'should list all transactions' do
6
+ stub_get('/market/transactions/collections').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Market::Collection.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+
15
+ it 'should create a new collection given a valid payment reference' do
16
+ stub_post('/market/transactions/collections').
17
+ to_return(:status => 200,
18
+ :body => lambda { |_request| fixture('card_payments/create.json') })
19
+
20
+ collection = build(:market_collection)
21
+ response = collection.create
22
+
23
+ expect(response).to be_a(Hash)
24
+ expect(response.result).to eq('Success')
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/judopay/models/market/payment'
3
+
4
+ describe Judopay::Market::Payment do
5
+ it 'should list all transactions' do
6
+ stub_get('/market/transactions/payments').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Market::Payment.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/judopay/models/market/preauth'
3
+
4
+ describe Judopay::Market::Preauth do
5
+ it 'should list all transactions' do
6
+ stub_get('/market/transactions/preauths').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Market::Preauth.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/judopay/models/market/refund'
3
+
4
+ describe Judopay::Market::Refund do
5
+ it 'should list all transactions' do
6
+ stub_get('/market/transactions/refunds').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Market::Refund.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+
15
+ it 'should create a new refund given a valid payment reference' do
16
+ stub_post('/market/transactions/refunds').
17
+ to_return(:status => 200,
18
+ :body => lambda { |_request| fixture('card_payments/create.json') })
19
+
20
+ refund = build(:market_refund)
21
+ response = refund.create
22
+
23
+ expect(response).to be_a(Hash)
24
+ expect(response.result).to eq('Success')
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/judopay/models/market/transaction'
3
+
4
+ describe Judopay::Market::Transaction do
5
+ it 'should list all transactions' do
6
+ stub_get('/market/transactions').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Market::Transaction.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/payment'
3
+
4
+ describe Judopay::Payment do
5
+ it 'should list all payments' do
6
+ stub_get('/transactions').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Payment.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/preauth'
3
+
4
+ describe Judopay::Preauth do
5
+ it 'should list all transactions' do
6
+ stub_get('/transactions/preauths').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Preauth.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/refund'
3
+
4
+ describe Judopay::Refund do
5
+ it 'should list all refunds' do
6
+ stub_get('/transactions/refunds').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Refund.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+
15
+ it 'should create a new refund given a valid payment reference' do
16
+ stub_post('/transactions/refunds').
17
+ to_return(:status => 200,
18
+ :body => lambda { |_request| fixture('card_payments/create.json') })
19
+
20
+ refund = build(:refund)
21
+ response = refund.create
22
+
23
+ expect(response).to be_a(Hash)
24
+ expect(response.result).to eq('Success')
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/token_preauth'
3
+
4
+ describe Judopay::TokenPayment do
5
+ it 'should create a new payment given a valid payment token and card token' do
6
+ stub_post('/transactions/payments').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('token_payments/create.json') })
9
+
10
+ payment = build(:token_payment)
11
+ response = payment.create
12
+
13
+ expect(response).to be_a(Hash)
14
+ expect(response.result).to eq('Success')
15
+ end
16
+
17
+ it 'should return a bad request exception if basic validation fails' do
18
+ expect(lambda do
19
+ Judopay::TokenPayment.new.create
20
+ end).to raise_error(Judopay::ValidationError)
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/token_preauth'
3
+
4
+ describe Judopay::TokenPreauth do
5
+ it 'should create a new preauth given a valid payment token and card token' do
6
+ stub_post('/transactions/preauths').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('token_payments/create.json') })
9
+
10
+ payment = build(:token_preauth)
11
+ response = payment.create
12
+
13
+ expect(response).to be_a(Hash)
14
+ expect(response.result).to eq('Success')
15
+ end
16
+
17
+ it 'should return a bad request exception if basic validation fails' do
18
+ expect(lambda do
19
+ Judopay::TokenPreauth.new.create
20
+ end).to raise_error(Judopay::ValidationError)
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/transaction'
3
+
4
+ describe Judopay::Transaction do
5
+ it 'should list all transactions' do
6
+ stub_get('/transactions').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/all.json') })
9
+
10
+ transactions = Judopay::Transaction.all
11
+ expect(transactions).to be_a(Hash)
12
+ expect(transactions.results[0].amount).to equal(1.01)
13
+ end
14
+
15
+ it 'should give details of a single transaction given a valid receipt ID' do
16
+ stub_get('/transactions/439539').
17
+ to_return(:status => 200,
18
+ :body => lambda { |_request| fixture('transactions/find.json') })
19
+
20
+ receipt_id = '439539'
21
+ transaction = Judopay::Transaction.find(receipt_id)
22
+ expect(transaction).to be_a(Hash)
23
+ expect(transaction.receipt_id).to eq(receipt_id)
24
+ end
25
+
26
+ it 'should send paging parameters with a request' do
27
+ stub_get('/transactions').
28
+ to_return(:status => 200,
29
+ :body => lambda { |_request| fixture('transactions/all.json') })
30
+
31
+ options = {
32
+ :sort => 'time-descending',
33
+ :offset => 10,
34
+ :page_size => 5,
35
+ :dodgy_param => 'banana'
36
+ }
37
+
38
+ expected_request_options = {
39
+ :sort => 'time-descending',
40
+ :offset => 10,
41
+ :pageSize => 5
42
+ }
43
+
44
+ Judopay::Transaction.all(options)
45
+
46
+ WebMock.should have_requested(
47
+ :get,
48
+ Judopay.configuration.endpoint_url + '/transactions'
49
+ ).with(:query => expected_request_options)
50
+ end
51
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/judopay/models/web_payments/payment'
3
+
4
+ describe Judopay::WebPayments::Payment do
5
+ it 'should create a new web payment request' do
6
+ stub_post('/webpayments/payments').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('web_payments/payments/create.json') })
9
+
10
+ payment = build(:web_payment)
11
+ response = payment.create
12
+
13
+ expect(response).to be_a(Hash)
14
+ expect(response.post_url).to eq('https://pay.judopay-sandbox.com/')
15
+ end
16
+ end