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,10 @@
1
+ require_relative '../../model'
2
+
3
+ module Judopay
4
+ module Market
5
+ class Preauth < Model
6
+ @resource_path = 'market/transactions/preauths'
7
+ @valid_api_methods = [:all]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require_relative '../../model'
2
+
3
+ module Judopay
4
+ module Market
5
+ class Refund < Judopay::Refund
6
+ @resource_path = 'market/transactions/refunds'
7
+ @valid_api_methods = [:all, :create]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require_relative '../../model'
2
+
3
+ module Judopay
4
+ module Market
5
+ class Transaction < Model
6
+ @resource_path = 'market/transactions'
7
+ @valid_api_methods = [:all]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../model'
2
+
3
+ module Judopay
4
+ class Payment < Model
5
+ @resource_path = 'transactions/payments'
6
+ @valid_api_methods = [:all]
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../model'
2
+
3
+ module Judopay
4
+ class Preauth < Model
5
+ @resource_path = 'transactions/preauths'
6
+ @valid_api_methods = [:all]
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ require_relative '../model'
2
+
3
+ module Judopay
4
+ class Refund < Model
5
+ @resource_path = 'transactions/refunds'
6
+ @valid_api_methods = [:all, :create]
7
+
8
+ attribute :receipt_id, Integer
9
+ attribute :amount, Float
10
+ attribute :your_payment_reference, String
11
+
12
+ validates_presence_of :receipt_id,
13
+ :amount,
14
+ :your_payment_reference
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ require_relative '../model'
2
+ require_relative 'payment'
3
+ require_relative 'card_address'
4
+ require_relative 'consumer_location'
5
+
6
+ module Judopay
7
+ class TokenPayment < Model
8
+ @resource_path = 'transactions/payments'
9
+ @valid_api_methods = [:create]
10
+
11
+ attribute :your_consumer_reference, String
12
+ attribute :your_payment_reference, String
13
+ attribute :your_payment_meta_data, Hash
14
+ attribute :judo_id, String
15
+ attribute :amount, Float
16
+ attribute :consumer_token, String
17
+ attribute :card_token, String
18
+ attribute :cv2, String
19
+ attribute :consumer_location, Judopay::ConsumerLocation
20
+ attribute :mobile_number, String
21
+ attribute :email_address, String
22
+
23
+ validates_presence_of :your_consumer_reference,
24
+ :your_payment_reference,
25
+ :judo_id,
26
+ :amount,
27
+ :consumer_token,
28
+ :card_token
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ require_relative '../model'
2
+ require_relative 'token_payment'
3
+
4
+ module Judopay
5
+ # Inherit from TokenPayment - attributes are identical
6
+ class TokenPreauth < TokenPayment
7
+ @resource_path = 'transactions/preauths'
8
+ @valid_api_methods = [:create]
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../model'
2
+
3
+ module Judopay
4
+ class Transaction < Model
5
+ @resource_path = 'transactions'
6
+ @valid_api_methods = [:all, :find]
7
+ end
8
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../../model'
2
+
3
+ module Judopay
4
+ module WebPayments
5
+ class Payment < Model
6
+ @resource_path = 'webpayments/payments'
7
+ @valid_api_methods = [:create]
8
+
9
+ attribute :judo_id, String
10
+ attribute :amount, Float
11
+ attribute :partner_service_fee, Float
12
+ attribute :your_consumer_reference, String
13
+ attribute :your_payment_reference, String
14
+ attribute :your_payment_meta_data, Hash
15
+ attribute :client_ip_address, String
16
+ attribute :client_user_agent, String
17
+
18
+ validates_presence_of :judo_id,
19
+ :your_consumer_reference,
20
+ :your_payment_reference,
21
+ :amount
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ require_relative '../../model'
2
+
3
+ module Judopay
4
+ module WebPayments
5
+ class Preauth < Payment
6
+ @resource_path = 'webpayments/preauths'
7
+ @valid_api_methods = [:create]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../../model'
2
+
3
+ module Judopay
4
+ module WebPayments
5
+ class Transaction < Model
6
+ @resource_path = 'webpayments'
7
+ @valid_api_methods = [:find]
8
+
9
+ # Find a specific web payment given a valid payment reference
10
+ #
11
+ # @param reference [String] Payment reference
12
+ # @return [Judopay::Mash] Mash of the API response
13
+ def self.find(reference)
14
+ api = Judopay::API.new
15
+ api.get(@resource_path + '/' + reference.to_s)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ require 'logger'
2
+
3
+ module Judopay
4
+ class NullLogger < Logger
5
+ def initialize(*)
6
+ end
7
+
8
+ def add(*)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,50 @@
1
+ require 'openssl'
2
+ require 'json'
3
+
4
+ module Judopay
5
+ # Defines HTTP request methods
6
+ module Request
7
+ # Perform an HTTP GET request
8
+ def get(path, options = {}, raw = false)
9
+ request(:get, path, options, raw)
10
+ end
11
+
12
+ # Perform an HTTP POST request
13
+ def post(path, options = {}, raw = false)
14
+ request(:post, path, options, raw)
15
+ end
16
+
17
+ # Perform an HTTP PUT request
18
+ def put(path, options = {}, raw = false)
19
+ request(:put, path, options, raw)
20
+ end
21
+
22
+ # Perform an HTTP DELETE request
23
+ def delete(path, options = {}, raw = false)
24
+ request(:delete, path, options, raw)
25
+ end
26
+
27
+ private
28
+
29
+ # Perform an HTTP request
30
+ def request(method, path, options, raw = false)
31
+ response = connection(raw).send(method) do |request|
32
+ case method
33
+ when :get, :delete
34
+ request.url(path, options)
35
+ when :post, :put
36
+ request.path = path
37
+ unless options.nil?
38
+ request.body = Judopay::Serializer.new(options).as_json
39
+ Judopay.log(Logger::DEBUG, 'Request body: ' + request.body)
40
+ end
41
+ end
42
+ end
43
+
44
+ Judopay.log(Logger::DEBUG, response)
45
+
46
+ return response if raw
47
+ Response.create(response.body)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module Judopay
2
+ module Response
3
+ def self.create(response_hash)
4
+ response_hash
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ module Judopay
2
+ # @private
3
+ class Serializer
4
+ attr_reader :serialized_object
5
+
6
+ def initialize(serialized_object)
7
+ @serialized_object = serialized_object
8
+ end
9
+
10
+ def as_json
11
+ output_hash = hashify(@serialized_object).camel_case_keys!
12
+ JSON.generate(output_hash)
13
+ end
14
+
15
+ # Turn a hash with nested Virtus objects into a simple nested hash
16
+ def hashify(source)
17
+ output_hash = {}
18
+ source.to_hash.each do |key, value|
19
+ next if value.nil?
20
+
21
+ # Is this a Virtus model object that we need to hashify?
22
+ if value.class.included_modules.include?(Virtus::Model::Core)
23
+ output_hash[key] = hashify(value)
24
+ else
25
+ output_hash[key] = value
26
+ end
27
+ end
28
+ output_hash
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Judopay
2
+ VERSION = '1.0.0.pre'
3
+ end
@@ -0,0 +1,103 @@
1
+ models = %w(
2
+ card_payment
3
+ card_preauth
4
+ collection
5
+ refund
6
+ token_payment
7
+ token_preauth
8
+ web_payments/payment
9
+ market/collection
10
+ market/refund
11
+ )
12
+ models.each { |model| require_relative '../lib/judopay/models/' + model }
13
+
14
+ FactoryGirl.define do
15
+ trait :valid_card_details do
16
+ your_consumer_reference 123
17
+ your_payment_reference 456
18
+ judo_id '123-456'
19
+ amount 1.01
20
+ card_number '4976000000003436'
21
+ expiry_date '12/15'
22
+ cv2 452
23
+ end
24
+
25
+ trait :valid_token_details do
26
+ your_consumer_reference '123'
27
+ your_payment_reference '456'
28
+ judo_id '123-456'
29
+ amount 1.01
30
+ consumer_token '3UW4DV9wI0oKkMFS'
31
+ card_token 'SXw4hnv1vJuEujQR'
32
+ cv2 '452'
33
+ end
34
+
35
+ trait :valid_judo_id do
36
+ judo_id '123-456'
37
+ end
38
+
39
+ trait :valid_collection_or_refund_details do
40
+ receipt_id '1234'
41
+ amount 1.01
42
+ your_payment_reference 'payment12412312'
43
+ end
44
+
45
+ trait :client_ip_address_and_user_agent do
46
+ client_ip_address '127.0.0.1'
47
+ client_user_agent 'Mozilla/5.0 (Windows NT 6.2; Win64; x64)...'
48
+ end
49
+
50
+ factory :card_payment, :class => Judopay::CardPayment do
51
+ valid_card_details
52
+ valid_judo_id
53
+ end
54
+
55
+ factory :card_preauth, :class => Judopay::CardPreauth do
56
+ valid_card_details
57
+ valid_judo_id
58
+ end
59
+
60
+ factory :collection, :class => Judopay::Collection do
61
+ valid_collection_or_refund_details
62
+ end
63
+
64
+ factory :refund, :class => Judopay::Refund do
65
+ valid_collection_or_refund_details
66
+ end
67
+
68
+ factory :token_payment, :class => Judopay::TokenPayment do
69
+ valid_token_details
70
+ valid_judo_id
71
+ end
72
+
73
+ factory :token_preauth, :class => Judopay::TokenPreauth do
74
+ valid_token_details
75
+ valid_judo_id
76
+ end
77
+
78
+ factory :web_payment, :class => Judopay::WebPayments::Payment do
79
+ valid_judo_id
80
+ amount 12.34
81
+ partner_service_fee 1.00
82
+ your_payment_reference '123'
83
+ your_consumer_reference '456'
84
+ client_ip_address_and_user_agent
85
+ end
86
+
87
+ factory :web_preauth, :class => Judopay::WebPayments::Payment do
88
+ valid_judo_id
89
+ amount 12.34
90
+ partner_service_fee 1.00
91
+ your_payment_reference '123'
92
+ your_consumer_reference '456'
93
+ client_ip_address_and_user_agent
94
+ end
95
+
96
+ factory :market_collection, :class => Judopay::Market::Collection do
97
+ valid_collection_or_refund_details
98
+ end
99
+
100
+ factory :market_refund, :class => Judopay::Market::Refund do
101
+ valid_collection_or_refund_details
102
+ end
103
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
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|
13
+ context "when response status is #{status}" do
14
+
15
+ before do
16
+ stub_get('/transactions').
17
+ to_return(:status => status,
18
+ :body => lambda { |_request| JSON.generate('errorType' => status) },
19
+ :headers => { 'Content-Type' => 'application/json' })
20
+ end
21
+
22
+ it "should raise #{exception.name} error" do
23
+ expect(lambda do
24
+ Judopay::Transaction.all
25
+ end).to raise_error(exception)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ {
2
+ "receiptId": "1234",
3
+ "type": "Payment",
4
+ "createdAt": "2012-12-05T14:40:53.4529+00:00",
5
+ "result": "Success",
6
+ "message": "Payment was successful",
7
+ "judoId": "123-456",
8
+ "merchantName": "AnyMerchant Ltd",
9
+ "appearsOnStatementAs": "Judo Payments/AnyMerch",
10
+ "refunds": 0.00,
11
+ "netAmount": 12.34,
12
+ "cardDetails": {
13
+ "cardLastFour": "3436",
14
+ "endDate": "12/15",
15
+ "cardToken": "ax035fds95325df3",
16
+ "cardType": 1
17
+ },
18
+ "consumer": {
19
+ "consumerToken": "abcdefghijklABCDDEF",
20
+ "yourConsumerReference": "consumer0053252"
21
+ }
22
+ }