minfraud 1.0.2 → 1.3.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 (75) hide show
  1. checksums.yaml +5 -13
  2. data/.github/workflows/test.yml +46 -0
  3. data/.gitignore +2 -0
  4. data/.rubocop.yml +108 -0
  5. data/.travis.yml +19 -3
  6. data/CHANGELOG.md +65 -1
  7. data/CODE_OF_CONDUCT.md +4 -4
  8. data/Gemfile +11 -2
  9. data/LICENSE.txt +2 -1
  10. data/README.dev.md +4 -0
  11. data/README.md +245 -59
  12. data/Rakefile +18 -3
  13. data/bin/console +4 -3
  14. data/lib/maxmind/geoip2/model/city.rb +99 -0
  15. data/lib/maxmind/geoip2/model/country.rb +94 -0
  16. data/lib/maxmind/geoip2/model/insights.rb +38 -0
  17. data/lib/maxmind/geoip2/record/abstract.rb +46 -0
  18. data/lib/maxmind/geoip2/record/city.rb +62 -0
  19. data/lib/maxmind/geoip2/record/continent.rb +61 -0
  20. data/lib/maxmind/geoip2/record/country.rb +78 -0
  21. data/lib/maxmind/geoip2/record/location.rb +97 -0
  22. data/lib/maxmind/geoip2/record/maxmind.rb +41 -0
  23. data/lib/maxmind/geoip2/record/place.rb +52 -0
  24. data/lib/maxmind/geoip2/record/postal.rb +54 -0
  25. data/lib/maxmind/geoip2/record/represented_country.rb +47 -0
  26. data/lib/maxmind/geoip2/record/subdivision.rb +72 -0
  27. data/lib/maxmind/geoip2/record/traits.rb +233 -0
  28. data/lib/minfraud.rb +48 -8
  29. data/lib/minfraud/assessments.rb +118 -49
  30. data/lib/minfraud/components/account.rb +31 -9
  31. data/lib/minfraud/components/addressable.rb +73 -26
  32. data/lib/minfraud/components/base.rb +35 -11
  33. data/lib/minfraud/components/billing.rb +5 -0
  34. data/lib/minfraud/components/credit_card.rb +67 -18
  35. data/lib/minfraud/components/custom_inputs.rb +25 -0
  36. data/lib/minfraud/components/device.rb +51 -10
  37. data/lib/minfraud/components/email.rb +29 -7
  38. data/lib/minfraud/components/event.rb +60 -13
  39. data/lib/minfraud/components/order.rb +60 -22
  40. data/lib/minfraud/components/payment.rb +165 -21
  41. data/lib/minfraud/components/report/transaction.rb +80 -0
  42. data/lib/minfraud/components/shipping.rb +14 -5
  43. data/lib/minfraud/components/shopping_cart.rb +19 -12
  44. data/lib/minfraud/components/shopping_cart_item.rb +42 -13
  45. data/lib/minfraud/enum.rb +22 -8
  46. data/lib/minfraud/error_handler.rb +45 -12
  47. data/lib/minfraud/errors.rb +22 -2
  48. data/lib/minfraud/http_service.rb +22 -8
  49. data/lib/minfraud/http_service/request.rb +19 -18
  50. data/lib/minfraud/http_service/response.rb +49 -12
  51. data/lib/minfraud/model/abstract.rb +20 -0
  52. data/lib/minfraud/model/address.rb +52 -0
  53. data/lib/minfraud/model/billing_address.rb +11 -0
  54. data/lib/minfraud/model/credit_card.rb +75 -0
  55. data/lib/minfraud/model/device.rb +54 -0
  56. data/lib/minfraud/model/disposition.rb +35 -0
  57. data/lib/minfraud/model/email.rb +54 -0
  58. data/lib/minfraud/model/email_domain.rb +24 -0
  59. data/lib/minfraud/model/error.rb +28 -0
  60. data/lib/minfraud/model/factors.rb +24 -0
  61. data/lib/minfraud/model/geoip2_location.rb +25 -0
  62. data/lib/minfraud/model/insights.rb +68 -0
  63. data/lib/minfraud/model/ip_address.rb +82 -0
  64. data/lib/minfraud/model/issuer.rb +49 -0
  65. data/lib/minfraud/model/score.rb +76 -0
  66. data/lib/minfraud/model/score_ip_address.rb +23 -0
  67. data/lib/minfraud/model/shipping_address.rb +30 -0
  68. data/lib/minfraud/model/subscores.rb +178 -0
  69. data/lib/minfraud/model/warning.rb +63 -0
  70. data/lib/minfraud/report.rb +58 -0
  71. data/lib/minfraud/resolver.rb +25 -16
  72. data/lib/minfraud/validates.rb +187 -0
  73. data/lib/minfraud/version.rb +4 -1
  74. data/minfraud.gemspec +23 -18
  75. metadata +123 -48
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minfraud
4
+ module Components
5
+ module Report
6
+ # Contains the fields used in the Report Transaction API.
7
+ #
8
+ # @see https://dev.maxmind.com/minfraud/report-transaction/
9
+ class Transaction < Base
10
+ include ::Minfraud::Enum
11
+
12
+ # The IP address of the customer placing the order. This should be
13
+ # passed as a string like "152.216.7.110".
14
+ #
15
+ # @return [String, nil]
16
+ attr_accessor :ip_address
17
+
18
+ # A symbol indicating the likelihood that a transaction may be
19
+ # fraudulent.
20
+ #
21
+ # This may be one of +:chargeback+, +:not_fraud+, +:spam_or_abuse+, or
22
+ # +:suspected_fraud+.
23
+ #
24
+ # @!attribute tag
25
+ #
26
+ # @return [Symbol, nil]
27
+ enum_accessor :tag, [:chargeback, :not_fraud, :spam_or_abuse, :suspected_fraud]
28
+
29
+ # A string which is provided by your payment processor indicating the
30
+ # reason for the chargeback.
31
+ #
32
+ # @return [String, nil]
33
+ attr_accessor :chargeback_code
34
+
35
+ # A unique eight character string identifying a minFraud Standard or
36
+ # Premium request. These IDs are returned in the maxmindID field of a
37
+ # response for a successful minFraud request. This field is not
38
+ # required, but you are encouraged to provide it, if possible.
39
+ #
40
+ # @return [String, nil]
41
+ attr_accessor :maxmind_id
42
+
43
+ # A UUID that identifies a minFraud Score, minFraud Insights, or
44
+ # minFraud Factors request. This ID is returned at /id in the response.
45
+ # This field is not required, but you are encouraged to provide it if
46
+ # the request was made to one of these services.
47
+ #
48
+ # @return [String, nil]
49
+ attr_accessor :minfraud_id
50
+
51
+ # Your notes on the fraud tag associated with the transaction. We
52
+ # manually review many reported transactions to improve our scoring for
53
+ # you so any additional details to help us understand context are
54
+ # helpful.
55
+ #
56
+ # @return [String, nil]
57
+ attr_accessor :notes
58
+
59
+ # The transaction ID you originally passed to minFraud. This field is
60
+ # not required, but you are encouraged to provide it or the
61
+ # transaction's maxmind_id or minfraud_id.
62
+ #
63
+ # @return [String, nil]
64
+ attr_accessor :transaction_id
65
+
66
+ # @param params [Hash] Hash of parameters. Each key/value should
67
+ # correspond to one of the available attributes.
68
+ def initialize(params = {})
69
+ @ip_address = params[:ip_address]
70
+ @chargeback_code = params[:chargeback_code]
71
+ @maxmind_id = params[:maxmind_id]
72
+ @minfraud_id = params[:minfraud_id]
73
+ @notes = params[:notes]
74
+ @transaction_id = params[:transaction_id]
75
+ self.tag = params[:tag]
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -1,14 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
2
4
  module Components
5
+ # Shipping corresponds to the shipping object of a minFraud request.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud/#Shipping_(/shipping)
3
8
  class Shipping < Addressable
4
9
  include ::Minfraud::Enum
5
- # @attribute delivery_speed
6
- # @return [String] The shipping delivery speed for the order. The valid values are:
10
+
11
+ # The shipping delivery speed for the order. Must be one of +:same_day+,
12
+ # +:overnight+, +:expedited+, or +:standard+.
13
+ #
14
+ # @!attribute delivery_speed
15
+ #
16
+ # @return [Symbol, nil]
7
17
  enum_accessor :delivery_speed, [:same_day, :overnight, :expedited, :standard]
8
18
 
9
- # Creates Minfraud::Components::Shipping instance
10
- # @param [Hash] params hash of parameters
11
- # @return [Minfraud::Components::Shipping] Shipping instance
19
+ # @param params [Hash] Hash of parameters. Each key/value should
20
+ # correspond to one of the available attributes.
12
21
  def initialize(params = {})
13
22
  self.delivery_speed = params[:delivery_speed]
14
23
  super
@@ -1,27 +1,34 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
2
4
  module Components
5
+ # ShoppingCart corresponds to the shopping_cart object of a minFraud
6
+ # request.
7
+ #
8
+ # @see https://dev.maxmind.com/minfraud/#Shopping_Cart_(/shoppingcart)
3
9
  class ShoppingCart < Base
4
- # @attribute items
5
- # @return [Array] An array of Minfraud::Components::ShoppingCartItem instances
6
-
10
+ # An array of Minfraud::Components::ShoppingCartItem instances.
11
+ #
12
+ # @return [Array<Minfraud::Components::ShoppingCartItem>]
7
13
  attr_accessor :items
8
- # Creates Minfraud::Components::ShoppingCart instance
9
- # @param [Hash] params hash of parameters
10
- # @return [Minfraud::Components::ShoppingCart] ShoppingCart instance
11
- def initialize(params = {})
14
+
15
+ # @param params [Array] Array of shopping cart items. You may provide
16
+ # each item as either as Hash where each key is a symbol corresponding
17
+ # to an item's field, or as a Minfraud:::Components::ShoppingCartItem
18
+ # object.
19
+ def initialize(params = [])
12
20
  @items = params.map(&method(:resolve))
13
21
  end
14
22
 
15
- # @return [Array] a JSON representation of Minfraud::Components::ShoppingCart items
16
- def to_json
23
+ # A JSON representation of Minfraud::Components::ShoppingCart items.
24
+ #
25
+ # @return [Array]
26
+ def to_json(*_args)
17
27
  @items.map(&:to_json)
18
28
  end
19
29
 
20
30
  private
21
31
 
22
- # @param [Hash] params hash of parameters for Minfraud::Components::ShoppingCartItem
23
- # or Minfraud::Components::ShoppingCartItem instance
24
- # @return [Minfraud::Components::ShoppingCart] ShoppingCart instance
25
32
  def resolve(params)
26
33
  params.is_a?(ShoppingCartItem) ? params : ShoppingCartItem.new(params)
27
34
  end
@@ -1,33 +1,62 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
2
4
  module Components
5
+ # ShoppingCartItem corresponds to objects in the shopping_cart object
6
+ # of a minFraud request.
7
+ #
8
+ # @see https://dev.maxmind.com/minfraud/#Shopping_Cart_Item
3
9
  class ShoppingCartItem < Base
4
- # @attribute category
5
- # @return [String] The category of the item
10
+ include Minfraud::Validates
11
+
12
+ # The category of the item. This can also be a hashed value; see link.
13
+ #
14
+ # @see https://dev.maxmind.com/minfraud/#cart-hashing
15
+ #
16
+ # @return [String, nil]
6
17
  attr_accessor :category
7
18
 
8
- # @attribute item_id
9
- # @return [String] The internal ID of the item
19
+ # The internal ID of the item. This can also be a hashed value; see link.
20
+ #
21
+ # @see https://dev.maxmind.com/minfraud/#cart-hashing
22
+ #
23
+ # @return [String, nil]
10
24
  attr_accessor :item_id
11
25
 
12
- # @attribute quantity
13
- # @return [Integer] The quantity of the item in the shopping cart
26
+ # The quantity of the item in the shopping cart. The value must be at
27
+ # least 0, at most 10^13-1, and have no fractional part.
28
+ #
29
+ # @return [Integer, nil]
14
30
  attr_accessor :quantity
15
31
 
16
- # @attribute price
17
- # @return [Float] The per-unit price of this item in the shopping cart. This should use the same currency as the order currency
32
+ # The per-unit price of this item in the shopping cart. This should use
33
+ # the same currency as the order currency. The value must be at least 0
34
+ # and at most 1e14 - 1.
35
+ #
36
+ # @return [Float, nil]
18
37
  attr_accessor :price
19
38
 
20
- # Creates Minfraud::Components::ShoppingCartItem instance
21
- # @param [Hash] params hash of parameters
22
- # @return [Minfraud::Components::ShoppingCartItem] ShoppingCartItem instance
39
+ # @param params [Hash] Hash of parameters. Each key/value should
40
+ # correspond to one of the available attributes.
23
41
  def initialize(params = {})
24
42
  @category = params[:category]
25
43
  @item_id = params[:item_id]
26
44
  @quantity = params[:quantity]
27
45
  @price = params[:price]
46
+
47
+ validate
48
+ end
49
+
50
+ private
51
+
52
+ def validate
53
+ return if !Minfraud.enable_validation
54
+
55
+ validate_string('category', 255, @category)
56
+ validate_string('item_id', 255, @item_id)
57
+ validate_nonnegative_integer('quantity', @quantity)
58
+ validate_nonnegative_number('price', @price)
28
59
  end
29
60
  end
30
61
  end
31
62
  end
32
-
33
-
@@ -1,19 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
4
+ # Enum provides helpers for working with attributes with enumerated types.
2
5
  module Enum
3
6
  def self.included(base)
4
7
  base.extend(ClassMethods)
5
8
  end
6
9
 
10
+ # ClassMethods provides helpers for working with attributes with enumerated
11
+ # types.
7
12
  module ClassMethods
8
- # Returns a hash with in the following format: attribute_name => permitted_values
9
- # @return [Hash] mapping
13
+ # Returns a hash in the following format: attribute_name =>
14
+ # permitted_values
15
+ #
16
+ # @return [Hash]
10
17
  def mapping
11
18
  @mapping ||= {}
12
19
  end
13
20
 
14
- # Creates a set of methods for enum-like behaviour of the attribute
15
- # @param [Symbol] attribute attribute name
16
- # @param [Array] assignable_values a set of values which are permitted
21
+ # Create a set of methods for enum-like behavior of the attribute.
22
+ #
23
+ # @param attribute [Symbol] The attribute name.
24
+ #
25
+ # @param assignable_values [Array] The set of permitted values.
26
+ #
27
+ # @return [nil]
17
28
  def enum_accessor(attribute, assignable_values)
18
29
  mapping[attribute] = assignable_values.map(&:intern)
19
30
 
@@ -21,13 +32,16 @@ module Minfraud
21
32
  define_method("#{attribute}_values") { mapping[attribute] }
22
33
  end
23
34
 
24
- self.class_eval do
25
- define_method("#{attribute}") { instance_variable_get("@#{attribute}") }
35
+ class_eval do
36
+ define_method(attribute.to_s) { instance_variable_get("@#{attribute}") }
26
37
  define_method "#{attribute}=" do |value|
27
- raise NotEnumValueError, 'Value is not permitted' if value && !self.class.mapping[attribute].include?(value.intern)
38
+ raise NotEnumValueError, 'Value is not permitted' if value && !self.class.mapping[attribute].include?(value.intern)
39
+
28
40
  instance_variable_set("@#{attribute}", value ? value.intern : nil)
29
41
  end
30
42
  end
43
+
44
+ nil
31
45
  end
32
46
  end
33
47
  end
@@ -1,22 +1,37 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
4
+ # ErrorHandler provides a method to raise exceptions on errors.
2
5
  module ErrorHandler
3
6
  class << self
4
- # Returns a response if status code is 200, rises an error otherwise
5
- # @param [Minfraud::HTTPService::Response] response
6
- # @return [Minfraud::HTTPService::Response] if status code is 200
7
- def inspect(response)
8
- return response if response.status == 200
7
+ # Return the response if the HTTP status code is 2xx. Otherwise raise
8
+ # an error.
9
+ #
10
+ # @param response [Minfraud::HTTPService::Response]
11
+ #
12
+ # @return [Minfraud::HTTPService::Response]
13
+ #
14
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
15
+ # problem.
16
+ #
17
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
18
+ # of your inputs.
19
+ #
20
+ # @raise [Minfraud::ServerError] If the server reported an error of some
21
+ # kind.
22
+ def examine(response)
23
+ return response if response.status > 199 && response.status < 300
9
24
 
10
- raise *STATUS_CODES.fetch(response.code, [ServerError, 'Server error'])
25
+ raise(*STATUS_CODES.fetch(response.code, [ServerError, 'Server error']))
11
26
  end
12
27
 
13
- # A hash that maps status codes returned by minFraud with errors & messages
28
+ # @!visibility private
14
29
  STATUS_CODES = {
15
30
  IP_ADDRESS_INVALID: [
16
- ClientError, 'You have no supplied a valid IPv4 or IPv6 address'
31
+ ClientError, 'You have not supplied a valid IPv4 or IPv6 address'
17
32
  ],
18
33
  IP_ADDRESS_REQUIRED: [
19
- ClientError, 'You have not supplied an IP address which is required filed'
34
+ ClientError, 'You have not supplied an IP address which is a required field'
20
35
  ],
21
36
  IP_ADDRESS_RESERVED: [
22
37
  ClientError, 'You have supplied an IP address which is reserved'
@@ -24,14 +39,32 @@ module Minfraud
24
39
  JSON_INVALID: [
25
40
  ClientError, 'JSON body cannot be decoded'
26
41
  ],
42
+ MAXMIND_ID_INVALID: [
43
+ ClientError, 'You have not supplied a valid maxmind_id'
44
+ ],
45
+ MINFRAUD_ID_INVALID: [
46
+ ClientError, 'You have not supplied a valid minfraud_id'
47
+ ],
48
+ PARAMETER_UNKNOWN: [
49
+ ClientError, 'You have supplied an unknown parameter'
50
+ ],
51
+ TAG_REQUIRED: [
52
+ ClientError, 'You have not supplied a tag, which is a required field'
53
+ ],
54
+ TAG_INVALID: [
55
+ ClientError, 'You have not supplied a valid tag'
56
+ ],
57
+ ACCOUNT_ID_REQUIRED: [
58
+ AuthorizationError, 'You have not supplied a account ID'
59
+ ],
27
60
  AUTHORIZATION_INVALID: [
28
- AuthorizationError, 'Invalid license key and / or user id'
61
+ AuthorizationError, 'Invalid license key and / or account ID'
29
62
  ],
30
63
  LICENSE_KEY_REQUIRED: [
31
64
  AuthorizationError, 'You have not supplied a license key'
32
65
  ],
33
66
  USER_ID_REQUIRED: [
34
- AuthorizationError, 'You have not supplied a user id'
67
+ AuthorizationError, 'You have not supplied a account id'
35
68
  ],
36
69
  INSUFFICIENT_FUNDS: [
37
70
  ClientError, 'The license key you have provided does not have a sufficient funds to use this service'
@@ -39,7 +72,7 @@ module Minfraud
39
72
  PERMISSION_REQUIRED: [
40
73
  ClientError, 'You do not have permission to use this service'
41
74
  ]
42
- }
75
+ }.freeze
43
76
  end
44
77
  end
45
78
  end
@@ -1,10 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
2
- # A list of Minfraud custom errors
3
- class BaseError < StandardError; end
4
+ # The base error class for Minfraud exceptions.
5
+ class BaseError < StandardError; end
6
+
7
+ # An error indicating the input value is not valid.
8
+ class InvalidInputError < BaseError; end
4
9
 
10
+ # An error indicating the value is not valid for the enumerated type.
5
11
  class NotEnumValueError < BaseError; end
12
+
13
+ # An error indicating the field is not recognized.
6
14
  class RequestFormatError < BaseError; end
15
+
16
+ # An error indicating minFraud cannot serve your request as there is a
17
+ # problem on the client side.
18
+ #
19
+ # For example, this may happen if there is a problem with the format or
20
+ # contents of your request, if you lack funds to use the service, or if you
21
+ # don't have permission to use the service.
7
22
  class ClientError < BaseError; end
23
+
24
+ # An error indicating there is a problem with authorization or
25
+ # authentication.
8
26
  class AuthorizationError < BaseError; end
27
+
28
+ # An error indicating the server had an error handling the request.
9
29
  class ServerError < BaseError; end
10
30
  end
@@ -1,31 +1,45 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday'
2
4
  require 'faraday_middleware'
3
5
 
4
6
  module Minfraud
7
+ # HTTPService holds the HTTP client configuration.
5
8
  module HTTPService
6
9
  class << self
7
- # @return [Hash] default HTTPService configuration
10
+ # The default HTTPService configuration.
11
+ #
12
+ # @return [Hash]
8
13
  def configuration
14
+ server = DEFAULT_SERVER
15
+ if !Minfraud.host.nil?
16
+ server = "https://#{Minfraud.host}/minfraud/v2.0"
17
+ end
18
+
9
19
  {
10
20
  middleware: DEFAULT_MIDDLEWARE,
11
- server: DEFAULT_SERVER
21
+ server: server,
12
22
  }
13
23
  end
14
24
  end
15
25
 
16
- # Minfraud default middleware stack
17
- DEFAULT_MIDDLEWARE = Proc.new do |builder|
26
+ # @!visibility private
27
+ DEFAULT_MIDDLEWARE = proc do |builder|
18
28
  builder.request :json
19
29
 
20
- builder.basic_auth *::Minfraud.configuration.values
30
+ account_id = Minfraud.account_id
31
+ account_id = Minfraud.user_id if account_id.nil?
32
+
33
+ builder.basic_auth account_id, Minfraud.license_key
21
34
 
22
- builder.response :mashify
23
35
  builder.response :json, content_type: /\bjson$/
24
36
 
25
- builder.adapter Faraday.default_adapter
37
+ builder.adapter :net_http_persistent, pool_size: 5 do |http|
38
+ http.idle_timeout = 30
39
+ end
26
40
  end
27
41
 
28
- # Minfraud default server
42
+ # Default base URL for minFraud.
29
43
  DEFAULT_SERVER = 'https://minfraud.maxmind.com/minfraud/v2.0'
30
44
  end
31
45
  end