judopay 1.0.2.pre → 2.0.0

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 (63) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -0
  3. data/CHANGELOG.md +3 -0
  4. data/README.md +2 -0
  5. data/Rakefile +17 -2
  6. data/judopay.gemspec +11 -10
  7. data/lib/faraday/raise_http_exception.rb +1 -18
  8. data/lib/judopay.rb +12 -9
  9. data/lib/judopay/connection.rb +28 -18
  10. data/lib/judopay/core_ext/string.rb +4 -3
  11. data/lib/judopay/error.rb +55 -97
  12. data/lib/judopay/model.rb +49 -40
  13. data/lib/judopay/models/card_payment.rb +5 -10
  14. data/lib/judopay/models/card_preauth.rb +1 -1
  15. data/lib/judopay/models/collection.rb +1 -1
  16. data/lib/judopay/models/refund.rb +1 -1
  17. data/lib/judopay/models/register_card.rb +28 -0
  18. data/lib/judopay/models/save_card.rb +26 -0
  19. data/lib/judopay/models/token_payment.rb +1 -0
  20. data/lib/judopay/models/void.rb +19 -0
  21. data/lib/judopay/models/web_payments/payment.rb +3 -0
  22. data/lib/judopay/models/web_payments/web_payment_operation.rb +8 -0
  23. data/lib/judopay/request.rb +1 -1
  24. data/lib/judopay/serializer.rb +1 -5
  25. data/lib/judopay/version.rb +2 -1
  26. data/spec/factories.rb +64 -19
  27. data/spec/faraday/response_spec.rb +10 -12
  28. data/spec/fixtures/card_payments/create_bad_request.json +11 -4
  29. data/spec/fixtures/transactions/register_card.json +29 -0
  30. data/spec/fixtures/transactions/save_card.json +28 -0
  31. data/spec/fixtures/transactions/void.json +27 -0
  32. data/spec/judopay/card_address_spec.rb +1 -1
  33. data/spec/judopay/card_payment_spec.rb +1 -2
  34. data/spec/judopay/collection_spec.rb +1 -1
  35. data/spec/judopay/error_spec.rb +36 -15
  36. data/spec/judopay/judopay_spec.rb +1 -1
  37. data/spec/judopay/market/collection_spec.rb +1 -1
  38. data/spec/judopay/market/payment_spec.rb +1 -1
  39. data/spec/judopay/market/preauth_spec.rb +1 -1
  40. data/spec/judopay/market/refund_spec.rb +1 -1
  41. data/spec/judopay/market/transaction_spec.rb +1 -1
  42. data/spec/judopay/payment_spec.rb +1 -1
  43. data/spec/judopay/preauth_spec.rb +1 -1
  44. data/spec/judopay/refund_spec.rb +1 -1
  45. data/spec/judopay/register_card_spec.rb +24 -0
  46. data/spec/judopay/save_card_spec.rb +23 -0
  47. data/spec/judopay/transaction_spec.rb +1 -1
  48. data/spec/judopay/void_spec.rb +24 -0
  49. data/spec/spec_helper.rb +6 -2
  50. data/test/authentication_test.rb +23 -0
  51. data/test/base/integration_base.rb +20 -0
  52. data/test/base/payments_tests.rb +40 -0
  53. data/test/base/token_payment_tests.rb +91 -0
  54. data/test/card_details_test.rb +21 -0
  55. data/test/configuration_test.rb +35 -0
  56. data/test/helper/assertion_helper.rb +29 -0
  57. data/test/payment_test.rb +10 -0
  58. data/test/preauth_test.rb +10 -0
  59. data/test/register_card_test.rb +37 -0
  60. data/test/token_payment_test.rb +10 -0
  61. data/test/token_preauth_test.rb +10 -0
  62. data/test/void_test.rb +44 -0
  63. metadata +107 -50
@@ -1,17 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Faraday::Response do
4
- Judopay.configure
5
- {
6
- 400 => Judopay::BadRequest,
7
- 401 => Judopay::NotAuthorized,
8
- 404 => Judopay::NotFound,
9
- 409 => Judopay::Conflict,
10
- 500 => Judopay::InternalServerError,
11
- 503 => Judopay::ServiceUnavailable
12
- }.each do |status, exception|
4
+ [
5
+ 400,
6
+ 401,
7
+ 404,
8
+ 409,
9
+ 500,
10
+ 503
11
+ ].each do |status|
13
12
  context "when response status is #{status}" do
14
-
15
13
  before do
16
14
  stub_get('/transactions').
17
15
  to_return(:status => status,
@@ -19,10 +17,10 @@ describe Faraday::Response do
19
17
  :headers => { 'Content-Type' => 'application/json' })
20
18
  end
21
19
 
22
- it "should raise #{exception.name} error" do
20
+ it 'should raise APIError exception' do
23
21
  expect(lambda do
24
22
  Judopay::Transaction.all
25
- end).to raise_error(exception)
23
+ end).to raise_error(Judopay::APIError)
26
24
  end
27
25
  end
28
26
  end
@@ -1,5 +1,12 @@
1
1
  {
2
- "errorMessage": "Please check the card token.",
3
- "errorType": "9",
4
- "modelErrors": ["Something went pear-shaped"]
5
- }
2
+ "details": [
3
+ {
4
+ "code": 46,
5
+ "fieldName": "ExpiryDate",
6
+ "message": "Sorry, but the expiry date entered is in the past. Please check your details and try again."
7
+ }
8
+ ],
9
+ "message": "Sorry, we're unable to process your request. Please check your details and try again.",
10
+ "code": 1,
11
+ "category": 2
12
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "receiptId": "5050240",
3
+ "yourPaymentReference": "Judo_RegisterCardPreAuth_636059257945294942",
4
+ "type": "PreAuth",
5
+ "createdAt": "2016-08-04T16:43:14.7482+01:00",
6
+ "result": "Success",
7
+ "message": "AuthCode: 910333",
8
+ "judoId": 100435197,
9
+ "merchantName": "Oleg",
10
+ "appearsOnStatementAs": "APL\/Oleg ",
11
+ "originalAmount": "1.01",
12
+ "amountCollected": "0.00",
13
+ "netAmount": "1.01",
14
+ "amount": "1.01",
15
+ "currency": "GBP",
16
+ "cardDetails": {
17
+ "cardLastfour": "3436",
18
+ "endDate": "1220",
19
+ "cardToken": "123123123",
20
+ "cardType": 1
21
+ },
22
+ "consumer": {
23
+ "consumerToken": "123123123",
24
+ "yourConsumerReference": "12345"
25
+ },
26
+ "risks": {
27
+ "postCodeCheck": "UNKNOWN"
28
+ }
29
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "receiptId": "123123123",
3
+ "yourPaymentReference": "Judo_RegisterCard_636051228548063948",
4
+ "type": "Register",
5
+ "createdAt": "2016-07-26T09:40:54.9313+01:00",
6
+ "result": "Success",
7
+ "message": "Register Card",
8
+ "judoId": 323123123,
9
+ "merchantName": "SomeMerch",
10
+ "appearsOnStatementAs": "APL123",
11
+ "originalAmount": "0.00",
12
+ "netAmount": "0.00",
13
+ "amount": "0.00",
14
+ "currency": "GBP",
15
+ "cardDetails": {
16
+ "cardLastfour": "3436",
17
+ "endDate": "1220",
18
+ "cardToken": "abc",
19
+ "cardType": 1
20
+ },
21
+ "consumer": {
22
+ "consumerToken": "abc",
23
+ "yourConsumerReference": "12345"
24
+ },
25
+ "risks": {
26
+ "postCodeCheck": "UNKNOWN"
27
+ }
28
+ }
@@ -0,0 +1,27 @@
1
+ {
2
+ "receiptId": "XXXXXX",
3
+ "originalReceiptId": "12345",
4
+ "yourPaymentReference": "12345",
5
+ "type": "VOID",
6
+ "createdAt": "2016-08-11T13:30:38.1849+01:00",
7
+ "result": "Success",
8
+ "message": "Void successful",
9
+ "judoId": 123123123,
10
+ "merchantName": "xxxx",
11
+ "appearsOnStatementAs": "xxxxxxx",
12
+ "originalAmount": "1.02",
13
+ "amountCollected": "0.00",
14
+ "netAmount": "1.02",
15
+ "amount": "1.02",
16
+ "currency": "GBP",
17
+ "cardDetails": {
18
+ "cardLastfour": "3436",
19
+ "endDate": "1220",
20
+ "cardToken": "xxxxxxxxxxxxxxxxxxxx",
21
+ "cardType": 1
22
+ },
23
+ "consumer": {
24
+ "consumerToken": "xxxxxxxxxxxxxxxx",
25
+ "yourConsumerReference": "12345"
26
+ }
27
+ }
@@ -5,6 +5,6 @@ describe Judopay::CardAddress do
5
5
  it "should not allow querying of the API if a resource path isn't set on the model" do
6
6
  expect(lambda do
7
7
  Judopay::CardAddress.all
8
- end).to raise_error(Judopay::Error)
8
+ end).to raise_error(Judopay::ValidationError)
9
9
  end
10
10
  end
@@ -34,7 +34,6 @@ describe Judopay::CardPayment do
34
34
  end
35
35
 
36
36
  it "should use the configured Judo ID if one isn't provided in the payment request" do
37
-
38
37
  stub_post('/transactions/payments').
39
38
  to_return(:status => 200,
40
39
  :body => lambda { |_request| fixture('card_payments/create.json') })
@@ -43,7 +42,7 @@ describe Judopay::CardPayment do
43
42
  config.judo_id = '123-456'
44
43
  end
45
44
 
46
- payment = build(:card_payment)
45
+ payment = build(:card_payment, :judo_id => nil)
47
46
  payment.create
48
47
 
49
48
  expect(payment.valid?).to eq(true)
@@ -9,7 +9,7 @@ describe Judopay::Collection do
9
9
 
10
10
  transactions = Judopay::Collection.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
 
15
15
  it 'should create a new collection given a valid payment reference' do
@@ -1,23 +1,40 @@
1
1
  require 'spec_helper'
2
2
  require_relative '../../lib/judopay/error'
3
+ require 'active_model'
3
4
 
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')
5
+ describe Judopay::FieldError do
6
+ it 'returns fields that was set in message' do
7
+ e = Judopay::FieldError.new('msg', 123, 'field', 'detail')
8
+ expect(e.to_s).to eq('Field "field" (code 123): msg')
8
9
  end
10
+ end
11
+
12
+ describe Judopay::ValidationError do
13
+ it 'returns fields that was set' do
14
+ errors = ActiveModel::Errors.new(nil)
15
+ errors['some_field'] = 'some error'
16
+ e = Judopay::ValidationError.new('Error', errors)
17
+ expect(e.to_s).to eq("Error\nField errors:\nsome_field: some error")
18
+ end
19
+ end
9
20
 
10
- it "returns the 'message' that was set" do
21
+ describe Judopay::APIError do
22
+ it 'returns the fields that was set' do
11
23
  message = 'An explicitly set message'
12
- e = Judopay::Error.new(message)
24
+
25
+ e = Judopay::APIError.new(message, 1, 12, 23, [])
13
26
  expect(e.message).to eq(message)
27
+ expect(e.error_code).to eq(1)
28
+ expect(e.status_code).to eq(12)
29
+ expect(e.category).to eq(23)
30
+ expect(e.field_errors).to eq([])
14
31
  end
15
32
 
16
- it 'contains exceptions in Judopay' do
17
- expect(Judopay::BadRequest.new).to be_a_kind_of(Judopay::APIError)
33
+ it 'return class name when there is no message and field errors' do
34
+ expect(Judopay::APIError.new(nil).message).to eq('Judopay::APIError')
18
35
  end
19
36
 
20
- it 'makes error information available on the exception object for API errors' do
37
+ it 'creates valid APIError from response using factory method' do
21
38
  stub_post('/transactions/payments').
22
39
  to_return(:status => 400,
23
40
  :body => lambda { |_request| fixture('card_payments/create_bad_request.json') },
@@ -27,16 +44,20 @@ describe Judopay::Error do
27
44
 
28
45
  begin
29
46
  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.')
47
+ rescue Judopay::APIError => e
48
+ expect(e.status_code).to eq(400)
49
+ expect(e.field_errors).to be_a_kind_of(Array)
50
+ expect(e.field_errors[0]).to be_a_kind_of(Judopay::FieldError)
51
+ expect(e.field_errors[0].code).to eq(46)
52
+ expect(e.error_code).to eq(1)
53
+ expect(e.category).to eq(2)
54
+ expect(e.message).to eq("Sorry, we're unable to process your request. Please check your details and try again.\n\
55
+ Fields errors:\nField \"ExpiryDate\" (code 46): Sorry, but the expiry date entered is in the past. \
56
+ Please check your details and try again.")
35
57
  end
36
58
  end
37
59
 
38
60
  it 'makes error information available on the exception object for validation errors' do
39
-
40
61
  payment = Judopay::CardPayment.new
41
62
 
42
63
  begin
@@ -9,7 +9,7 @@ describe Judopay do
9
9
  config.use_production = true
10
10
  end
11
11
 
12
- expect(Judopay.configuration.endpoint_url).to include('production')
12
+ expect(Judopay.configuration.endpoint_url).to_not include('sandbox')
13
13
 
14
14
  # Reset to sandbox for other tests
15
15
  Judopay.configure do |config|
@@ -9,7 +9,7 @@ describe Judopay::Market::Collection do
9
9
 
10
10
  transactions = Judopay::Market::Collection.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
 
15
15
  it 'should create a new collection given a valid payment reference' do
@@ -9,6 +9,6 @@ describe Judopay::Market::Payment do
9
9
 
10
10
  transactions = Judopay::Market::Payment.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
  end
@@ -9,6 +9,6 @@ describe Judopay::Market::Preauth do
9
9
 
10
10
  transactions = Judopay::Market::Preauth.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
  end
@@ -9,7 +9,7 @@ describe Judopay::Market::Refund do
9
9
 
10
10
  transactions = Judopay::Market::Refund.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
 
15
15
  it 'should create a new refund given a valid payment reference' do
@@ -9,6 +9,6 @@ describe Judopay::Market::Transaction do
9
9
 
10
10
  transactions = Judopay::Market::Transaction.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
  end
@@ -9,6 +9,6 @@ describe Judopay::Payment do
9
9
 
10
10
  transactions = Judopay::Payment.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
  end
@@ -9,6 +9,6 @@ describe Judopay::Preauth do
9
9
 
10
10
  transactions = Judopay::Preauth.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
  end
@@ -9,7 +9,7 @@ describe Judopay::Refund do
9
9
 
10
10
  transactions = Judopay::Refund.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
 
15
15
  it 'should create a new refund given a valid payment reference' do
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/register_card'
3
+
4
+ describe Judopay::RegisterCard do
5
+ it 'should register a new card with valid card details' do
6
+ stub_post('/transactions/registercard').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/register_card.json') })
9
+
10
+ register_card = build(:register_card)
11
+ response = register_card.create
12
+
13
+ expect(response).to be_a(Hash)
14
+ expect(response.result).to eq('Success')
15
+ expect(response.type).to eq('PreAuth')
16
+ expect(response.amount).to eq('1.01')
17
+ end
18
+
19
+ it 'should return a bad request exception if basic validation fails' do
20
+ expect(lambda do
21
+ Judopay::RegisterCard.new.create
22
+ end).to raise_error(Judopay::ValidationError)
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/save_card'
3
+
4
+ describe Judopay::SaveCard do
5
+ it 'should save a new card with valid card details' do
6
+ stub_post('/transactions/savecard').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/save_card.json') })
9
+
10
+ save_card = build(:save_card)
11
+ response = save_card.create
12
+
13
+ expect(response).to be_a(Hash)
14
+ expect(response.result).to eq('Success')
15
+ expect(response.type).to eq('Register')
16
+ end
17
+
18
+ it 'should return a bad request exception if basic validation fails' do
19
+ expect(lambda do
20
+ Judopay::SaveCard.new.create
21
+ end).to raise_error(Judopay::ValidationError)
22
+ end
23
+ end
@@ -9,7 +9,7 @@ describe Judopay::Transaction do
9
9
 
10
10
  transactions = Judopay::Transaction.all
11
11
  expect(transactions).to be_a(Hash)
12
- expect(transactions.results[0].amount).to equal(1.01)
12
+ expect(transactions.results[0].amount).to eq(1.01)
13
13
  end
14
14
 
15
15
  it 'should give details of a single transaction given a valid receipt ID' do
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/judopay/models/void'
3
+
4
+ describe Judopay::Void do
5
+ it 'should create new void with valid details' do
6
+ stub_post('/transactions/voids').
7
+ to_return(:status => 200,
8
+ :body => lambda { |_request| fixture('transactions/void.json') })
9
+
10
+ void = build(:void)
11
+ response = void.create
12
+
13
+ expect(response).to be_a(Hash)
14
+ expect(response.result).to eq('Success')
15
+ expect(response.type).to eq('VOID')
16
+ expect(response.original_amount).to eq('1.02')
17
+ end
18
+
19
+ it 'should return a bad request exception if basic validation fails' do
20
+ expect(lambda do
21
+ Judopay::Void.new.create
22
+ end).to raise_error(Judopay::ValidationError)
23
+ end
24
+ end
data/spec/spec_helper.rb CHANGED
@@ -17,8 +17,12 @@ RSpec.configure do |config|
17
17
  end
18
18
  end
19
19
 
20
- # Use Judopay default configuration
21
- Judopay.configure
20
+ # Mock configuration
21
+ Judopay.configure do |config|
22
+ config.judo_id = 'id'
23
+ config.api_token = 'token'
24
+ config.api_secret = 'secret'
25
+ end
22
26
 
23
27
  def stub_get(path)
24
28
  stub_request(:get, /judopay/i)