minfraud 1.2.0 → 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.
@@ -2,10 +2,19 @@
2
2
 
3
3
  module Minfraud
4
4
  module Components
5
+ # Payment corresponds to the payment object of a minFraud request.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud/#Payment_(/payment)
5
8
  class Payment < Base
6
9
  include ::Minfraud::Enum
7
- # @attribute processor
8
- # @return [String] The payment processor used for the transaction
10
+ include Minfraud::Validates
11
+
12
+ # The payment processor used for the transaction. The value is one
13
+ # listed as a valid value, as a symbol.
14
+ #
15
+ # @!attribute processor
16
+ #
17
+ # @return [Symbol, nil]
9
18
  enum_accessor :processor, [
10
19
  :adyen,
11
20
  :affirm,
@@ -139,25 +148,36 @@ module Minfraud
139
148
  :worldpay
140
149
  ]
141
150
 
142
- # @attribute was_authorized
143
- # @return [Boolean] The authorization outcome from the payment processor.
144
- # If the transaction has not yet been approved or denied, do not include
145
- # this field
151
+ # The authorization outcome from the payment processor. If the
152
+ # transaction has not yet been approved or denied, do not include this
153
+ # field.
154
+ #
155
+ # @return [Boolean, nil]
146
156
  attr_accessor :was_authorized
147
157
 
148
- # @attribute decline_code
149
- # @return [String] The decline code as provided by your payment
150
- # processor. If the transaction was not declined, do not include this
151
- # field
158
+ # The decline code as provided by your payment processor. If the
159
+ # transaction was not declined, do not include this field.
160
+ #
161
+ # @return [String, nil]
152
162
  attr_accessor :decline_code
153
163
 
154
- # Creates Minfraud::Components::Payment instance
155
- # @param [Hash] params hash of parameters
156
- # @return [Minfraud::Components::Payment] Payment instance
164
+ # @param params [Hash] Hash of parameters. Each key/value should
165
+ # correspond to one of the available attributes.
157
166
  def initialize(params = {})
158
167
  @was_authorized = params[:was_authorized]
159
168
  @decline_code = params[:decline_code]
160
169
  self.processor = params[:processor]
170
+
171
+ validate
172
+ end
173
+
174
+ private
175
+
176
+ def validate
177
+ return if !Minfraud.enable_validation
178
+
179
+ validate_boolean('was_authorized', @was_authorized)
180
+ validate_string('decline_code', 255, @decline_code)
161
181
  end
162
182
  end
163
183
  end
@@ -3,57 +3,68 @@
3
3
  module Minfraud
4
4
  module Components
5
5
  module Report
6
- # Contains all of the fields which are used in the report transaction API
6
+ # Contains the fields used in the Report Transaction API.
7
+ #
8
+ # @see https://dev.maxmind.com/minfraud/report-transaction/
7
9
  class Transaction < Base
8
10
  include ::Minfraud::Enum
9
11
 
10
- # @!attribute ip_address
11
- # @return [String, nil] The IP address of the customer placing the order. This
12
- # should be passed as a string like "44.55.66.77" or "2001:db8::2:1".
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]
13
16
  attr_accessor :ip_address
14
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
+ #
15
24
  # @!attribute tag
16
- # This may be one of +:chargeback+, +:not_fraud+, +:spam_or_abuse+ or +:suspected_fraud+
17
- # @return [Symbol, nil] A symbol indicating the likelihood that a transaction
18
- # may be fraudulent.
25
+ #
26
+ # @return [Symbol, nil]
19
27
  enum_accessor :tag, [:chargeback, :not_fraud, :spam_or_abuse, :suspected_fraud]
20
28
 
21
- # @attribute chargeback_code
22
- # @return [String, nil] A string which is provided by your payment processor
23
- # indicating the reason for the chargeback.
29
+ # A string which is provided by your payment processor indicating the
30
+ # reason for the chargeback.
31
+ #
32
+ # @return [String, nil]
24
33
  attr_accessor :chargeback_code
25
34
 
26
- # @attribute maxmind_id
27
- # @return [String, nil] A unique eight character string identifying a minFraud
28
- # Standard or Premium request. These IDs are returned in the maxmindID
29
- # field of a response for a successful minFraud request. This field is
30
- # not required, but you are encouraged to provide it, if possible.
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]
31
41
  attr_accessor :maxmind_id
32
42
 
33
- # @attribute minfraud_id
34
- # @return [String, nil] A UUID that identifies a minFraud Score, minFraud
35
- # Insights, or minFraud Factors request. This ID is returned at /id in
36
- # the response. This field is not required, but you are encouraged to
37
- # provide it if the request was made to one of these services.
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]
38
49
  attr_accessor :minfraud_id
39
50
 
40
- # @attribute notes
41
- # @return [String, nil] Your notes on the fraud tag associated with the
42
- # transaction. We manually review many reported transactions to improve
43
- # our scoring for you so any additional details to help us understand
44
- # context are helpful.
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]
45
57
  attr_accessor :notes
46
58
 
47
- # @attribute transaction_id
48
- # @return [String, nil] The transaction ID you originally passed to minFraud.
49
- # This field is not required, but you are encouraged to provide it or
50
- # the transaction's maxmind_id or minfraud_id
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]
51
64
  attr_accessor :transaction_id
52
65
 
53
- # Creates Minfraud::Components::Report::Transaction instance
54
- # @param [Hash] params hash of parameters
55
- # @return [Minfraud::Components::Report::Transaction] a Report::Transaction
56
- # instance
66
+ # @param params [Hash] Hash of parameters. Each key/value should
67
+ # correspond to one of the available attributes.
57
68
  def initialize(params = {})
58
69
  @ip_address = params[:ip_address]
59
70
  @chargeback_code = params[:chargeback_code]
@@ -2,15 +2,22 @@
2
2
 
3
3
  module Minfraud
4
4
  module Components
5
+ # Shipping corresponds to the shipping object of a minFraud request.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud/#Shipping_(/shipping)
5
8
  class Shipping < Addressable
6
9
  include ::Minfraud::Enum
7
- # @attribute delivery_speed
8
- # @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]
9
17
  enum_accessor :delivery_speed, [:same_day, :overnight, :expedited, :standard]
10
18
 
11
- # Creates Minfraud::Components::Shipping instance
12
- # @param [Hash] params hash of parameters
13
- # @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.
14
21
  def initialize(params = {})
15
22
  self.delivery_speed = params[:delivery_speed]
16
23
  super
@@ -2,29 +2,33 @@
2
2
 
3
3
  module Minfraud
4
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)
5
9
  class ShoppingCart < Base
6
- # @attribute items
7
- # @return [Array] An array of Minfraud::Components::ShoppingCartItem instances
8
-
10
+ # An array of Minfraud::Components::ShoppingCartItem instances.
11
+ #
12
+ # @return [Array<Minfraud::Components::ShoppingCartItem>]
9
13
  attr_accessor :items
10
14
 
11
- # Creates Minfraud::Components::ShoppingCart instance
12
- # @param [Hash] params hash of parameters
13
- # @return [Minfraud::Components::ShoppingCart] ShoppingCart instance
14
- def initialize(params = {})
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 = [])
15
20
  @items = params.map(&method(:resolve))
16
21
  end
17
22
 
18
- # @return [Array] a JSON representation of Minfraud::Components::ShoppingCart items
23
+ # A JSON representation of Minfraud::Components::ShoppingCart items.
24
+ #
25
+ # @return [Array]
19
26
  def to_json(*_args)
20
27
  @items.map(&:to_json)
21
28
  end
22
29
 
23
30
  private
24
31
 
25
- # @param [Hash] params hash of parameters for Minfraud::Components::ShoppingCartItem
26
- # or Minfraud::Components::ShoppingCartItem instance
27
- # @return [Minfraud::Components::ShoppingCart] ShoppingCart instance
28
32
  def resolve(params)
29
33
  params.is_a?(ShoppingCartItem) ? params : ShoppingCartItem.new(params)
30
34
  end
@@ -2,31 +2,60 @@
2
2
 
3
3
  module Minfraud
4
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
5
9
  class ShoppingCartItem < Base
6
- # @attribute category
7
- # @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]
8
17
  attr_accessor :category
9
18
 
10
- # @attribute item_id
11
- # @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]
12
24
  attr_accessor :item_id
13
25
 
14
- # @attribute quantity
15
- # @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]
16
30
  attr_accessor :quantity
17
31
 
18
- # @attribute price
19
- # @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]
20
37
  attr_accessor :price
21
38
 
22
- # Creates Minfraud::Components::ShoppingCartItem instance
23
- # @param [Hash] params hash of parameters
24
- # @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.
25
41
  def initialize(params = {})
26
42
  @category = params[:category]
27
43
  @item_id = params[:item_id]
28
44
  @quantity = params[:quantity]
29
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)
30
59
  end
31
60
  end
32
61
  end
@@ -1,21 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Minfraud
4
+ # Enum provides helpers for working with attributes with enumerated types.
4
5
  module Enum
5
6
  def self.included(base)
6
7
  base.extend(ClassMethods)
7
8
  end
8
9
 
10
+ # ClassMethods provides helpers for working with attributes with enumerated
11
+ # types.
9
12
  module ClassMethods
10
- # Returns a hash with in the following format: attribute_name => permitted_values
11
- # @return [Hash] mapping
13
+ # Returns a hash in the following format: attribute_name =>
14
+ # permitted_values
15
+ #
16
+ # @return [Hash]
12
17
  def mapping
13
18
  @mapping ||= {}
14
19
  end
15
20
 
16
- # Creates a set of methods for enum-like behaviour of the attribute
17
- # @param [Symbol] attribute attribute name
18
- # @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]
19
28
  def enum_accessor(attribute, assignable_values)
20
29
  mapping[attribute] = assignable_values.map(&:intern)
21
30
 
@@ -31,6 +40,8 @@ module Minfraud
31
40
  instance_variable_set("@#{attribute}", value ? value.intern : nil)
32
41
  end
33
42
  end
43
+
44
+ nil
34
45
  end
35
46
  end
36
47
  end
@@ -1,18 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Minfraud
4
+ # ErrorHandler provides a method to raise exceptions on errors.
4
5
  module ErrorHandler
5
6
  class << self
6
- # Returns a response if status code is 2xx, raises an error otherwise
7
- # @param [Minfraud::HTTPService::Response] response
8
- # @return [Minfraud::HTTPService::Response] if status code is 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.
9
22
  def examine(response)
10
23
  return response if response.status > 199 && response.status < 300
11
24
 
12
25
  raise(*STATUS_CODES.fetch(response.code, [ServerError, 'Server error']))
13
26
  end
14
27
 
15
- # A hash that maps status codes returned by minFraud with errors & messages
28
+ # @!visibility private
16
29
  STATUS_CODES = {
17
30
  IP_ADDRESS_INVALID: [
18
31
  ClientError, 'You have not supplied a valid IPv4 or IPv6 address'
@@ -1,12 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Minfraud
4
- # A list of Minfraud custom errors
5
- class BaseError < StandardError; end
4
+ # The base error class for Minfraud exceptions.
5
+ class BaseError < StandardError; end
6
6
 
7
+ # An error indicating the input value is not valid.
8
+ class InvalidInputError < BaseError; end
9
+
10
+ # An error indicating the value is not valid for the enumerated type.
7
11
  class NotEnumValueError < BaseError; end
12
+
13
+ # An error indicating the field is not recognized.
8
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.
9
22
  class ClientError < BaseError; end
23
+
24
+ # An error indicating there is a problem with authorization or
25
+ # authentication.
10
26
  class AuthorizationError < BaseError; end
27
+
28
+ # An error indicating the server had an error handling the request.
11
29
  class ServerError < BaseError; end
12
30
  end
@@ -4,13 +4,16 @@ require 'faraday'
4
4
  require 'faraday_middleware'
5
5
 
6
6
  module Minfraud
7
+ # HTTPService holds the HTTP client configuration.
7
8
  module HTTPService
8
9
  class << self
9
- # @return [Hash] default HTTPService configuration
10
+ # The default HTTPService configuration.
11
+ #
12
+ # @return [Hash]
10
13
  def configuration
11
14
  server = DEFAULT_SERVER
12
15
  if !Minfraud.host.nil?
13
- server = 'https://' + Minfraud.host + '/minfraud/v2.0'
16
+ server = "https://#{Minfraud.host}/minfraud/v2.0"
14
17
  end
15
18
 
16
19
  {
@@ -20,7 +23,7 @@ module Minfraud
20
23
  end
21
24
  end
22
25
 
23
- # Minfraud default middleware stack
26
+ # @!visibility private
24
27
  DEFAULT_MIDDLEWARE = proc do |builder|
25
28
  builder.request :json
26
29
 
@@ -31,10 +34,12 @@ module Minfraud
31
34
 
32
35
  builder.response :json, content_type: /\bjson$/
33
36
 
34
- builder.adapter Faraday.default_adapter
37
+ builder.adapter :net_http_persistent, pool_size: 5 do |http|
38
+ http.idle_timeout = 30
39
+ end
35
40
  end
36
41
 
37
- # Minfraud default server
42
+ # Default base URL for minFraud.
38
43
  DEFAULT_SERVER = 'https://minfraud.maxmind.com/minfraud/v2.0'
39
44
  end
40
45
  end