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.
- checksums.yaml +4 -4
- data/.rubocop.yml +11 -30
- data/.travis.yml +0 -1
- data/CHANGELOG.md +13 -0
- data/README.md +169 -54
- data/lib/maxmind/geoip2/record/traits.rb +9 -0
- data/lib/minfraud.rb +15 -0
- data/lib/minfraud/assessments.rb +107 -51
- data/lib/minfraud/components/account.rb +29 -9
- data/lib/minfraud/components/addressable.rb +71 -26
- data/lib/minfraud/components/base.rb +20 -10
- data/lib/minfraud/components/billing.rb +3 -0
- data/lib/minfraud/components/credit_card.rb +62 -20
- data/lib/minfraud/components/custom_inputs.rb +12 -3
- data/lib/minfraud/components/device.rb +43 -15
- data/lib/minfraud/components/email.rb +27 -7
- data/lib/minfraud/components/event.rb +46 -12
- data/lib/minfraud/components/order.rb +56 -21
- data/lib/minfraud/components/payment.rb +33 -13
- data/lib/minfraud/components/report/transaction.rb +44 -33
- data/lib/minfraud/components/shipping.rb +12 -5
- data/lib/minfraud/components/shopping_cart.rb +15 -11
- data/lib/minfraud/components/shopping_cart_item.rb +40 -11
- data/lib/minfraud/enum.rb +16 -5
- data/lib/minfraud/error_handler.rb +17 -4
- data/lib/minfraud/errors.rb +20 -2
- data/lib/minfraud/http_service.rb +10 -5
- data/lib/minfraud/http_service/request.rb +17 -19
- data/lib/minfraud/http_service/response.rb +15 -12
- data/lib/minfraud/model/subscores.rb +3 -0
- data/lib/minfraud/report.rb +28 -10
- data/lib/minfraud/resolver.rb +11 -5
- data/lib/minfraud/validates.rb +187 -0
- data/lib/minfraud/version.rb +2 -1
- data/minfraud.gemspec +4 -2
- metadata +45 -9
data/lib/minfraud/assessments.rb
CHANGED
@@ -1,58 +1,76 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Minfraud
|
4
|
+
# Assessments is used to perform minFraud Score, Insights, and Factors
|
5
|
+
# requests.
|
6
|
+
#
|
7
|
+
# @see https://dev.maxmind.com/minfraud/
|
4
8
|
class Assessments
|
5
9
|
include ::Minfraud::HTTPService
|
6
10
|
include ::Minfraud::Resolver
|
7
11
|
|
8
|
-
#
|
9
|
-
#
|
12
|
+
# The Account component.
|
13
|
+
#
|
14
|
+
# @return [Minfraud::Components::Account, nil]
|
10
15
|
attr_accessor :account
|
11
16
|
|
12
|
-
#
|
13
|
-
#
|
17
|
+
# The Billing component.
|
18
|
+
#
|
19
|
+
# @return [Minfraud::Components::Billing, nil]
|
14
20
|
attr_accessor :billing
|
15
21
|
|
16
|
-
#
|
17
|
-
#
|
22
|
+
# The CreditCard component.
|
23
|
+
#
|
24
|
+
# @return [Minfraud::Components::CreditCard, nil]
|
18
25
|
attr_accessor :credit_card
|
19
26
|
|
20
|
-
#
|
21
|
-
#
|
27
|
+
# The CustomInputs component.
|
28
|
+
#
|
29
|
+
# @return [Minfraud::Components::CustomInputs, nil]
|
22
30
|
attr_accessor :custom_inputs
|
23
31
|
|
24
|
-
#
|
25
|
-
#
|
32
|
+
# The Device component.
|
33
|
+
#
|
34
|
+
# @return [Minfraud::Components::Device, nil]
|
26
35
|
attr_accessor :device
|
27
36
|
|
28
|
-
#
|
29
|
-
#
|
37
|
+
# The Email component.
|
38
|
+
#
|
39
|
+
# @return [Minfraud::Components::Email, nil]
|
30
40
|
attr_accessor :email
|
31
41
|
|
32
|
-
#
|
33
|
-
#
|
42
|
+
# The Event component.
|
43
|
+
#
|
44
|
+
# @return [Minfraud::Components::Event, nil]
|
34
45
|
attr_accessor :event
|
35
46
|
|
36
|
-
#
|
37
|
-
#
|
47
|
+
# The Order component.
|
48
|
+
#
|
49
|
+
# @return [Minfraud::Components::Order, nil]
|
38
50
|
attr_accessor :order
|
39
51
|
|
40
|
-
#
|
41
|
-
#
|
52
|
+
# The Payment component.
|
53
|
+
#
|
54
|
+
# @return [Minfraud::Components::Payment, nil]
|
42
55
|
attr_accessor :payment
|
43
56
|
|
44
|
-
#
|
45
|
-
#
|
57
|
+
# The Shipping component.
|
58
|
+
#
|
59
|
+
# @return [Minfraud::Components::Shipping, nil]
|
46
60
|
attr_accessor :shipping
|
47
61
|
|
48
|
-
#
|
49
|
-
#
|
62
|
+
# The ShoppingCart component.
|
63
|
+
#
|
64
|
+
# @return [Minfraud::Components::ShoppingCart, nil]
|
50
65
|
attr_accessor :shopping_cart
|
51
66
|
|
52
|
-
# @param
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
67
|
+
# @param params [Hash] Hash of parameters. Each key is a symbol
|
68
|
+
# corresponding to one of the available component attributes. Values may
|
69
|
+
# be component objects or hashes that will be provided to the component
|
70
|
+
# constructors.
|
71
|
+
#
|
72
|
+
# @param resolver [Minfraud::Resolver] Resolver that maps parameters to
|
73
|
+
# components.
|
56
74
|
def initialize(params = {}, resolver = ::Minfraud::Resolver)
|
57
75
|
@locales = params.delete('locales')
|
58
76
|
@locales = ['en'] if @locales.nil?
|
@@ -60,34 +78,74 @@ module Minfraud
|
|
60
78
|
resolver.assign(self, params)
|
61
79
|
end
|
62
80
|
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
+
# Perform a minFraud Factors request.
|
82
|
+
#
|
83
|
+
# @return [Minfraud::HTTPService::Response]
|
84
|
+
#
|
85
|
+
# @raise [Minfraud::AuthorizationError] If there was an authentication
|
86
|
+
# problem.
|
87
|
+
#
|
88
|
+
# @raise [Minfraud::ClientError] If there was a critical problem with one
|
89
|
+
# of your inputs.
|
90
|
+
#
|
91
|
+
# @raise [Minfraud::ServerError] If the server reported an error of some
|
92
|
+
# kind.
|
93
|
+
def factors
|
94
|
+
perform_request(:factors)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Perform a minFraud Insights request.
|
98
|
+
#
|
99
|
+
# @return [Minfraud::HTTPService::Response]
|
100
|
+
#
|
101
|
+
# @raise [Minfraud::AuthorizationError] If there was an authentication
|
102
|
+
# problem.
|
103
|
+
#
|
104
|
+
# @raise [Minfraud::ClientError] If there was a critical problem with one
|
105
|
+
# of your inputs.
|
106
|
+
#
|
107
|
+
# @raise [Minfraud::ServerError] If the server reported an error of some
|
108
|
+
# kind.
|
109
|
+
def insights
|
110
|
+
perform_request(:insights)
|
81
111
|
end
|
82
112
|
|
83
|
-
|
84
|
-
|
85
|
-
|
113
|
+
# Perform a minFraud Score request.
|
114
|
+
#
|
115
|
+
# @return [Minfraud::HTTPService::Response]
|
116
|
+
#
|
117
|
+
# @raise [Minfraud::AuthorizationError] If there was an authentication
|
118
|
+
# problem.
|
119
|
+
#
|
120
|
+
# @raise [Minfraud::ClientError] If there was a critical problem with one
|
121
|
+
# of your inputs.
|
122
|
+
#
|
123
|
+
# @raise [Minfraud::ServerError] If the server reported an error of some
|
124
|
+
# kind.
|
125
|
+
def score
|
126
|
+
perform_request(:score)
|
127
|
+
end
|
86
128
|
|
87
129
|
private
|
88
130
|
|
89
|
-
|
90
|
-
|
131
|
+
def perform_request(endpoint)
|
132
|
+
raw = request.perform(
|
133
|
+
verb: :post,
|
134
|
+
endpoint: endpoint.to_s,
|
135
|
+
body: request_body,
|
136
|
+
)
|
137
|
+
|
138
|
+
response = ::Minfraud::HTTPService::Response.new(
|
139
|
+
endpoint: endpoint,
|
140
|
+
locales: @locales,
|
141
|
+
status: raw.status.to_i,
|
142
|
+
body: raw.body,
|
143
|
+
headers: raw.headers
|
144
|
+
)
|
145
|
+
|
146
|
+
::Minfraud::ErrorHandler.examine(response)
|
147
|
+
end
|
148
|
+
|
91
149
|
def request_body
|
92
150
|
MAPPING.keys.reduce({}) do |mem, e|
|
93
151
|
next mem unless (value = send(e))
|
@@ -96,8 +154,6 @@ module Minfraud
|
|
96
154
|
end
|
97
155
|
end
|
98
156
|
|
99
|
-
# Creates memoized Minfraud::HTTPService::Request instance
|
100
|
-
# @return [Minfraud::HTTPService::Request] Request instance based on configuration params
|
101
157
|
def request
|
102
158
|
@request ||= Request.new(::Minfraud::HTTPService.configuration)
|
103
159
|
end
|
@@ -2,23 +2,43 @@
|
|
2
2
|
|
3
3
|
module Minfraud
|
4
4
|
module Components
|
5
|
+
# Account corresponds to the account object of a minFraud request.
|
6
|
+
#
|
7
|
+
# @see https://dev.maxmind.com/minfraud/#Account_(/account)
|
5
8
|
class Account < Base
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
#
|
9
|
+
include Minfraud::Validates
|
10
|
+
|
11
|
+
# A unique user ID associated with the end-user in your system. If your
|
12
|
+
# system allows the login name for the account to be changed, this should
|
13
|
+
# not be the login name for the account, but rather should be an internal
|
14
|
+
# ID that does not change. This is not your MaxMind account ID. No
|
15
|
+
# specific format is required.
|
16
|
+
#
|
17
|
+
# @return [String, nil]
|
10
18
|
attr_accessor :user_id
|
11
19
|
|
12
|
-
#
|
13
|
-
#
|
20
|
+
# An MD5 hash as a hexadecimal string of the username or login name
|
21
|
+
# associated with the account.
|
22
|
+
#
|
23
|
+
# @return [String, nil]
|
14
24
|
attr_accessor :username_md5
|
15
25
|
|
16
|
-
#
|
17
|
-
#
|
18
|
-
# @return [Minfraud::Components::Account] an Account instance
|
26
|
+
# @param params [Hash] Hash of parameters. Each key/value should
|
27
|
+
# correspond to one of the available attributes.
|
19
28
|
def initialize(params = {})
|
20
29
|
@user_id = params[:user_id]
|
21
30
|
@username_md5 = params[:username_md5]
|
31
|
+
|
32
|
+
validate
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def validate
|
38
|
+
return if !Minfraud.enable_validation
|
39
|
+
|
40
|
+
validate_string('user_id', 255, @user_id)
|
41
|
+
validate_md5('username_md5', @username_md5)
|
22
42
|
end
|
23
43
|
end
|
24
44
|
end
|
@@ -2,55 +2,80 @@
|
|
2
2
|
|
3
3
|
module Minfraud
|
4
4
|
module Components
|
5
|
+
# This is a parent class for the Billing and Shipping components.
|
5
6
|
class Addressable < Base
|
6
|
-
|
7
|
-
|
7
|
+
include Minfraud::Validates
|
8
|
+
|
9
|
+
# The first name of the end user as provided in their billing / shipping
|
10
|
+
# information.
|
11
|
+
#
|
12
|
+
# @return [String, nil]
|
8
13
|
attr_accessor :first_name
|
9
14
|
|
10
|
-
#
|
11
|
-
#
|
15
|
+
# The last name of the end user as provided in their billing / shipping
|
16
|
+
# information.
|
17
|
+
#
|
18
|
+
# @return [String, nil]
|
12
19
|
attr_accessor :last_name
|
13
20
|
|
14
|
-
#
|
15
|
-
#
|
21
|
+
# The company of the end user as provided in their billing / shipping
|
22
|
+
# information.
|
23
|
+
#
|
24
|
+
# @return [String, nil]
|
16
25
|
attr_accessor :company
|
17
26
|
|
18
|
-
#
|
19
|
-
#
|
27
|
+
# The first line of the user's billing / shipping address.
|
28
|
+
#
|
29
|
+
# @return [String, nil]
|
20
30
|
attr_accessor :address
|
21
31
|
|
22
|
-
#
|
23
|
-
#
|
32
|
+
# The second line of the user's billing / shipping address.
|
33
|
+
#
|
34
|
+
# @return [String, nil]
|
24
35
|
attr_accessor :address_2
|
25
36
|
|
26
|
-
#
|
27
|
-
#
|
37
|
+
# The city of the user's billing / shipping address.
|
38
|
+
#
|
39
|
+
# @return [String, nil]
|
28
40
|
attr_accessor :city
|
29
41
|
|
30
|
-
#
|
31
|
-
#
|
42
|
+
# The ISO 3166-2 subdivision code for the user's billing / shipping
|
43
|
+
# address.
|
44
|
+
#
|
45
|
+
# @see https://en.wikipedia.org/wiki/ISO_3166-2
|
46
|
+
#
|
47
|
+
# @return [String, nil]
|
32
48
|
attr_accessor :region
|
33
49
|
|
34
|
-
#
|
35
|
-
#
|
50
|
+
# The two character ISO 3166-1 alpha-2 country code of the user's billing
|
51
|
+
# / shipping address.
|
52
|
+
#
|
53
|
+
# @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
|
54
|
+
#
|
55
|
+
# @return [String, nil]
|
36
56
|
attr_accessor :country
|
37
57
|
|
38
|
-
#
|
39
|
-
#
|
58
|
+
# The postal code of the user's billing / shipping address.
|
59
|
+
#
|
60
|
+
# @return [String, nil]
|
40
61
|
attr_accessor :postal
|
41
62
|
|
42
|
-
#
|
43
|
-
#
|
63
|
+
# The phone number without the country code for the user's billing /
|
64
|
+
# shipping address. Punctuation characters will be stripped. After
|
65
|
+
# stripping punctuation characters, the number must contain only digits.
|
66
|
+
#
|
67
|
+
# @return [String, nil]
|
44
68
|
attr_accessor :phone_number
|
45
69
|
|
46
|
-
#
|
47
|
-
#
|
70
|
+
# The country code for the phone number associated with the user's
|
71
|
+
# billing / shipping address. If you provide this information then you
|
72
|
+
# must provide at least one digit.
|
73
|
+
#
|
74
|
+
# @return [String, nil]
|
48
75
|
attr_accessor :phone_country_code
|
49
76
|
|
50
|
-
#
|
51
|
-
#
|
52
|
-
# @param [Hash] params hash of parameters
|
53
|
-
# @return [Minfraud::Components::Addressable] an Addressable instance
|
77
|
+
# @param params [Hash] Hash of parameters. Each key/value should
|
78
|
+
# correspond to one of the available attributes.
|
54
79
|
def initialize(params = {})
|
55
80
|
@first_name = params[:first_name]
|
56
81
|
@last_name = params[:last_name]
|
@@ -63,6 +88,26 @@ module Minfraud
|
|
63
88
|
@postal = params[:postal]
|
64
89
|
@phone_number = params[:phone_number]
|
65
90
|
@phone_country_code = params[:phone_country_code]
|
91
|
+
|
92
|
+
validate
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def validate
|
98
|
+
return if !Minfraud.enable_validation
|
99
|
+
|
100
|
+
validate_string('first_name', 255, @first_name)
|
101
|
+
validate_string('last_name', 255, @last_name)
|
102
|
+
validate_string('company', 255, @company)
|
103
|
+
validate_string('address', 255, @address)
|
104
|
+
validate_string('address_2', 255, @address_2)
|
105
|
+
validate_string('city', 255, @city)
|
106
|
+
validate_subdivision_code('region', @region)
|
107
|
+
validate_country_code('country', @country)
|
108
|
+
validate_string('postal', 255, @postal)
|
109
|
+
validate_string('phone_number', 255, @phone_number)
|
110
|
+
validate_telephone_country_code('phone_country_code', @phone_country_code)
|
66
111
|
end
|
67
112
|
end
|
68
113
|
end
|
@@ -2,20 +2,26 @@
|
|
2
2
|
|
3
3
|
module Minfraud
|
4
4
|
module Components
|
5
|
-
#
|
6
|
-
#
|
5
|
+
# This is a parent class for all components. It defines a method which is
|
6
|
+
# used for basic JSON representation of the component objects.
|
7
7
|
class Base
|
8
|
-
#
|
8
|
+
# A JSON representation of component attributes.
|
9
|
+
#
|
10
|
+
# @return [Hash]
|
9
11
|
def to_json(*_args)
|
10
12
|
instance_variables.reduce({}) { |mem, e| populate!(mem, e) }
|
11
13
|
end
|
12
14
|
|
13
15
|
private
|
14
16
|
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
# @
|
17
|
+
# Create a hash containing a JSON representation of instance variable
|
18
|
+
# name and its value.
|
19
|
+
#
|
20
|
+
# @param hash [Hash] An accumulator.
|
21
|
+
#
|
22
|
+
# @param v_sym [Symbol] An instance variable symbol.
|
23
|
+
#
|
24
|
+
# @return [Hash]
|
19
25
|
def populate!(hash, v_sym)
|
20
26
|
return hash unless (value = instance_variable_get(v_sym))
|
21
27
|
|
@@ -23,9 +29,13 @@ module Minfraud
|
|
23
29
|
hash.merge!(key => represent(key, value))
|
24
30
|
end
|
25
31
|
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# @
|
32
|
+
# Return the value according to the request format.
|
33
|
+
#
|
34
|
+
# @param key [Symbol] An instance variable symbol.
|
35
|
+
#
|
36
|
+
# @param value [Object] An instance variable value.
|
37
|
+
#
|
38
|
+
# @return [Object]
|
29
39
|
def represent(key, value)
|
30
40
|
BOOLS.include?(key) ? value : value.to_s
|
31
41
|
end
|