minfraud 1.1.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 (74) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +22 -0
  5. data/CHANGELOG.md +42 -0
  6. data/CODE_OF_CONDUCT.md +49 -0
  7. data/Gemfile +12 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.dev.md +4 -0
  10. data/README.md +178 -0
  11. data/Rakefile +6 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  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 +224 -0
  28. data/lib/minfraud.rb +51 -0
  29. data/lib/minfraud/assessments.rb +101 -0
  30. data/lib/minfraud/components/account.rb +23 -0
  31. data/lib/minfraud/components/addressable.rb +67 -0
  32. data/lib/minfraud/components/base.rb +35 -0
  33. data/lib/minfraud/components/billing.rb +5 -0
  34. data/lib/minfraud/components/credit_card.rb +52 -0
  35. data/lib/minfraud/components/custom_inputs.rb +14 -0
  36. data/lib/minfraud/components/device.rb +38 -0
  37. data/lib/minfraud/components/email.rb +21 -0
  38. data/lib/minfraud/components/event.rb +44 -0
  39. data/lib/minfraud/components/order.rb +52 -0
  40. data/lib/minfraud/components/payment.rb +152 -0
  41. data/lib/minfraud/components/report/transaction.rb +69 -0
  42. data/lib/minfraud/components/shipping.rb +18 -0
  43. data/lib/minfraud/components/shopping_cart.rb +30 -0
  44. data/lib/minfraud/components/shopping_cart_item.rb +33 -0
  45. data/lib/minfraud/enum.rb +34 -0
  46. data/lib/minfraud/error_handler.rb +65 -0
  47. data/lib/minfraud/errors.rb +10 -0
  48. data/lib/minfraud/http_service.rb +30 -0
  49. data/lib/minfraud/http_service/request.rb +37 -0
  50. data/lib/minfraud/http_service/response.rb +64 -0
  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 +156 -0
  69. data/lib/minfraud/model/warning.rb +63 -0
  70. data/lib/minfraud/report.rb +38 -0
  71. data/lib/minfraud/resolver.rb +33 -0
  72. data/lib/minfraud/version.rb +3 -0
  73. data/minfraud.gemspec +29 -0
  74. metadata +198 -0
@@ -0,0 +1,51 @@
1
+ require 'minfraud'
2
+ require 'minfraud/enum'
3
+ require 'minfraud/components/base'
4
+ require 'minfraud/components/account'
5
+ require 'minfraud/components/addressable'
6
+ require 'minfraud/components/billing'
7
+ require 'minfraud/components/credit_card'
8
+ require 'minfraud/components/custom_inputs'
9
+ require 'minfraud/components/device'
10
+ require 'minfraud/components/email'
11
+ require 'minfraud/components/event'
12
+ require 'minfraud/components/order'
13
+ require 'minfraud/components/payment'
14
+ require 'minfraud/components/report/transaction'
15
+ require 'minfraud/components/shipping'
16
+ require 'minfraud/components/shopping_cart'
17
+ require 'minfraud/components/shopping_cart_item'
18
+ require 'minfraud/resolver'
19
+ require 'minfraud/version'
20
+ require 'minfraud/errors'
21
+ require 'minfraud/http_service'
22
+ require 'minfraud/http_service/request'
23
+ require 'minfraud/http_service/response'
24
+ require 'minfraud/error_handler'
25
+ require 'minfraud/assessments'
26
+ require 'minfraud/report'
27
+
28
+ module Minfraud
29
+ class << self
30
+ # @!attribute user_id
31
+ # @return [String] MaxMind account ID that is used for authorization
32
+ attr_accessor :user_id
33
+
34
+ # @!attribute license_key
35
+ # @return [String] MaxMind license key that is used for authorization
36
+ attr_accessor :license_key
37
+
38
+ # @yield [self] to accept configuration settings
39
+ def configure
40
+ yield self
41
+ end
42
+
43
+ # @return [Hash] current Minfraud configuration
44
+ def configuration
45
+ {
46
+ user_id: @user_id,
47
+ license_key: @license_key
48
+ }
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,101 @@
1
+ module Minfraud
2
+ class Assessments
3
+ include ::Minfraud::HTTPService
4
+ include ::Minfraud::Resolver
5
+
6
+ # @attribute account
7
+ # @return [Minfraud::Components::Account] Account component
8
+ attr_accessor :account
9
+
10
+ # @attribute billing
11
+ # @return [Minfraud::Components::Billing] Billing component
12
+ attr_accessor :billing
13
+
14
+ # @attribute credit_card
15
+ # @return [Minfraud::Components::CreditCard] CreditCard component
16
+ attr_accessor :credit_card
17
+
18
+ # @attribute custom_inputs
19
+ # @return [Minfraud::Components::CustomInputs] CustomInputs component
20
+ attr_accessor :custom_inputs
21
+
22
+ # @attribute device
23
+ # @return [Minfraud::Components::Device] Device component
24
+ attr_accessor :device
25
+
26
+ # @attribute email
27
+ # @return [Minfraud::Components::Email] Email component
28
+ attr_accessor :email
29
+
30
+ # @attribute event
31
+ # @return [Minfraud::Components::Event] Event component
32
+ attr_accessor :event
33
+
34
+ # @attribute order
35
+ # @return [Minfraud::Components::Order] Order component
36
+ attr_accessor :order
37
+
38
+ # @attribute payment
39
+ # @return [Minfraud::Components::Payment] Payment component
40
+ attr_accessor :payment
41
+
42
+ # @!attribute shipping
43
+ # @return [Minfraud::Components::Shipping] Shipping component
44
+ attr_accessor :shipping
45
+
46
+ # @!attribute shopping_cart
47
+ # @return [Minfraud::Components::ShoppingCart] ShoppingCart component
48
+ attr_accessor :shopping_cart
49
+
50
+ # @param [Hash] params hash of parameters
51
+ # @param [Minfraud::Resolver] resolver resolver that maps params to components
52
+ # @note In case when params is a Hash of components it just assigns them to the corresponding instance variables
53
+ # @return [Minfraud::Assessments] Assessments instance
54
+ def initialize(params = {}, resolver = ::Minfraud::Resolver)
55
+ @locales = params.delete('locales')
56
+ @locales = ['en'] if @locales.nil?
57
+
58
+ resolver.assign(self, params)
59
+ end
60
+
61
+ # @!macro [attach] define
62
+ # @method $1
63
+ # Makes a request to minFraud $1 endpoint.
64
+ # Raises an error in case of invalid response
65
+ # @return [Minfraud::HTTPService::Response] Wrapped minFraud response
66
+ def self.define(endpoint)
67
+ define_method(endpoint) do
68
+ raw = request.perform(verb: :post, endpoint: endpoint.to_s, body: request_body)
69
+ response = ::Minfraud::HTTPService::Response.new(
70
+ endpoint: endpoint,
71
+ locales: @locales,
72
+ status: raw.status.to_i,
73
+ body: raw.body,
74
+ headers: raw.headers
75
+ )
76
+
77
+ ::Minfraud::ErrorHandler.examine(response)
78
+ end
79
+ end
80
+
81
+ define :score
82
+ define :insights
83
+ define :factors
84
+
85
+ private
86
+ # Creates a unified request body from components converted to JSON
87
+ # @return [Hash] Request body
88
+ def request_body
89
+ MAPPING.keys.inject({}) do |mem, e|
90
+ next mem unless value = send(e)
91
+ mem.merge!(e.to_s => value.to_json)
92
+ end
93
+ end
94
+
95
+ # Creates memoized Minfraud::HTTPService::Request instance
96
+ # @return [Minfraud::HTTPService::Request] Request instance based on configuration params
97
+ def request
98
+ @request ||= Request.new(::Minfraud::HTTPService.configuration)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,23 @@
1
+ module Minfraud
2
+ module Components
3
+ class Account < Base
4
+ # @attribute user_id
5
+ # @return [String] A unique user ID associated with the end-user in your system.
6
+ # If your system allows the login name for the account to be changed, this should not be the login name for the account,
7
+ # but rather should be an internal ID that does not change. This is not your MaxMind user ID.
8
+ attr_accessor :user_id
9
+
10
+ # @attribute username_md5
11
+ # @return [String] An MD5 hash as a hexadecimal string of the username or login name associated with the account
12
+ attr_accessor :username_md5
13
+
14
+ # Creates Minfraud::Components::Account instance
15
+ # @param [Hash] params hash of parameters
16
+ # @return [Minfraud::Components::Account] an Account instance
17
+ def initialize(params = {})
18
+ @user_id = params[:user_id]
19
+ @username_md5 = params[:username_md5]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,67 @@
1
+ module Minfraud
2
+ module Components
3
+ class Addressable < Base
4
+ # @attribute first_name
5
+ # @return [String] The first name of the end user as provided in their billing / shipping information
6
+ attr_accessor :first_name
7
+
8
+ # @attribute last_name
9
+ # @return [String] The last name of the end user as provided in their billing / shipping information
10
+ attr_accessor :last_name
11
+
12
+ # @attribute company
13
+ # @return [String] The company of the end user as provided in their billing / shipping information
14
+ attr_accessor :company
15
+
16
+ # @attribute address
17
+ # @return [String] The first line of the user’s billing / shipping address
18
+ attr_accessor :address
19
+
20
+ # @attribute address_2
21
+ # @return [String] The second line of the user’s billing / shipping address
22
+ attr_accessor :address_2
23
+
24
+ # @attribute city
25
+ # @return [String] The city of the user's billing / shipping address
26
+ attr_accessor :city
27
+
28
+ # @attribute region
29
+ # @return [String] The ISO 3166-2 subdivision code for the user’s billing / shipping address
30
+ attr_accessor :region
31
+
32
+ # @attribute country
33
+ # @return [String] The two character ISO 3166-1 alpha-2 country code of the user’s billing / shipping address
34
+ attr_accessor :country
35
+
36
+ # @attribute postal
37
+ # @return [String] The postal code of the user’s billing / shipping address
38
+ attr_accessor :postal
39
+
40
+ # @attribute phone_number
41
+ # @return [String] The phone number without the country code for the user’s billing / shipping address
42
+ attr_accessor :phone_number
43
+
44
+ # @attribute phone_country_code
45
+ # @return [String] The country code for phone number associated with the user’s billing / shipping address
46
+ attr_accessor :phone_country_code
47
+
48
+ # Creates Minfraud::Components::Addressable instance
49
+ # @note This class is used as a parent class for Billing and Shipping components
50
+ # @param [Hash] params hash of parameters
51
+ # @return [Minfraud::Components::Addressable] an Addressable instance
52
+ def initialize(params = {})
53
+ @first_name = params[:first_name]
54
+ @last_name = params[:last_name]
55
+ @company = params[:company]
56
+ @address = params[:address]
57
+ @address_2 = params[:address_2]
58
+ @city = params[:city]
59
+ @region = params[:region]
60
+ @country = params[:country]
61
+ @postal = params[:postal]
62
+ @phone_number = params[:phone_number]
63
+ @phone_country_code = params[:phone_country_code]
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,35 @@
1
+ module Minfraud
2
+ module Components
3
+ # @note This class is used as a parent class for all other components
4
+ # @note It defines a method which is used for basic JSON representation of PORO objects
5
+ class Base
6
+ # @return [Hash] a JSON representation of component attributes
7
+ def to_json
8
+ instance_variables.inject({}) { |mem, e| populate!(mem, e) }
9
+ end
10
+
11
+ private
12
+
13
+ # @note This method may modify passed hash. Non-existing instance variables are ignored
14
+ # @param [Hash] hash an accumulator
15
+ # @param [Symbol] v_sym an instance variable symbol
16
+ # @return [Hash] a hash containing a JSON representation of instance variable name and it's value
17
+ def populate!(hash, v_sym)
18
+ return hash unless value = instance_variable_get(v_sym)
19
+
20
+ key = v_sym.to_s.gsub(/@/, '')
21
+ hash.merge!(key => represent(key, value))
22
+ end
23
+
24
+ # param [Symbol] key instance variable symbol
25
+ # param [Object] value instance variable value
26
+ # @return [Object] value representation according to the request format
27
+ def represent(key, value)
28
+ BOOLS.include?(key) ? value : value.to_s
29
+ end
30
+
31
+ # Keys that have to remain boolean
32
+ BOOLS = %w(was_authorized is_gift has_gift_message)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Minfraud
2
+ module Components
3
+ class Billing < Addressable; end
4
+ end
5
+ end
@@ -0,0 +1,52 @@
1
+ module Minfraud
2
+ module Components
3
+ class CreditCard < Base
4
+ # @attribute issuer_id_number
5
+ # # @return [String] The issuer ID number for the credit card. This is the first 6 digits of the credit card number.
6
+ # It identifies the issuing bank
7
+ attr_accessor :issuer_id_number
8
+
9
+ # @attribute last_4_digits
10
+ # @return [String] The last four digits of the credit card number
11
+ attr_accessor :last_4_digits
12
+
13
+ # @attribute bank_name
14
+ # @return [String] The name of the issuing bank as provided by the end user
15
+ attr_accessor :bank_name
16
+
17
+ # @attribute bank_phone_country_code
18
+ # @return [String] The phone country code for the issuing bank as provided by the end user
19
+ attr_accessor :bank_phone_country_code
20
+
21
+ # @attribute bank_phone_number
22
+ # @return [String] The phone number, without the country code, for the issuing bank as provided by the end user
23
+ attr_accessor :bank_phone_number
24
+
25
+ #@attribute token
26
+ #@return [String] A token uniquely identifying the card. The token should consist of non-space printable ASCII characters.
27
+ attr_accessor :token
28
+
29
+ # @attribute avs_result
30
+ # @return [String] The address verification system (AVS) check result, as returned to you by the credit card processor
31
+ attr_accessor :avs_result
32
+
33
+ # @attribute cvv_result
34
+ # @return [String] The card verification value (CVV) code as provided by the payment processor
35
+ attr_accessor :cvv_result
36
+
37
+ # Creates Minfraud::Components::CreditCard instance
38
+ # @param [Hash] params hash of parameters
39
+ # @return [Minfraud::Components::CreditCard] a CreditCard instance
40
+ def initialize(params = {})
41
+ @bank_phone_country_code = params[:bank_phone_country_code]
42
+ @issuer_id_number = params[:issuer_id_number]
43
+ @last_4_digits = params[:last_4_digits]
44
+ @bank_name = params[:bank_name]
45
+ @bank_phone_number = params[:bank_phone_number]
46
+ @avs_result = params[:avs_result]
47
+ @cvv_result = params[:cvv_result]
48
+ @token = params[:token]
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,14 @@
1
+ module Minfraud
2
+ module Components
3
+ class CustomInputs < Base
4
+ # Creates Minfraud::Components::CustomInputs instance
5
+ # @param [Hash] params hash with keys that match your created custom input keys
6
+ # @return [Minfraud::Components::CustomInputs] a CustomInputs instance
7
+ def initialize(params = {})
8
+ params.each do |name, value|
9
+ instance_variable_set("@#{name}", value)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ module Minfraud
2
+ module Components
3
+ class Device < Base
4
+ # @!attribute ip_address
5
+ # @return [String] The IP address associated with the device used by the customer in the transaction.
6
+ # The IP address must be in IPv4 or IPv6 presentation format
7
+ attr_accessor :ip_address
8
+
9
+ # @attribute user_agent
10
+ # @return [String] The HTTP "User-Agent" header of the browser used in the transaction
11
+ attr_accessor :user_agent
12
+
13
+ # @attribute :accept_language
14
+ # @return [String] The HTTP "Accept-Language" header of the browser used in the transaction
15
+ attr_accessor :accept_language
16
+
17
+ # @attribute :session_age
18
+ # @return [Decimal] The number of seconds between the creation of the user’s session and the time of the transaction.
19
+ # Note that session_age is not the duration of the current visit, but the time since the start of the first visit.
20
+ attr_accessor :session_age
21
+
22
+ # @attribute :session_id
23
+ # @return [String] An ID that uniquely identifies a visitor’s session on the site.
24
+ attr_accessor :session_id
25
+
26
+ # Creates Minfraud::Components::Device instance
27
+ # @param [Hash] params hash of parameters
28
+ # @return [Minfraud::Components::Device] a Device instance
29
+ def initialize(params = {})
30
+ @ip_address = params[:ip_address]
31
+ @user_agent = params[:user_agent]
32
+ @accept_language = params[:accept_language]
33
+ @session_age = params[:session_age]
34
+ @session_id = params[:session_id]
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ module Minfraud
2
+ module Components
3
+ class Email < Base
4
+ # @attribute address
5
+ # @return [String] This field must be either a valid email address or an MD5 of the email used in the transaction
6
+ attr_accessor :address
7
+
8
+ # @attribute domain
9
+ # @return [String] The domain of the email address used in the transaction
10
+ attr_accessor :domain
11
+
12
+ # Creates Minfraud::Components::Email instance
13
+ # @param [Hash] params hash of parameters
14
+ # @return [Minfraud::Components::Email] an Email instance
15
+ def initialize(params = {})
16
+ @address = params[:address]
17
+ @domain = params[:domain]
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ module Minfraud
2
+ module Components
3
+ class Event < Base
4
+ include ::Minfraud::Enum
5
+ # @attribute transaction_id
6
+ # @return [String] Internal ID for the transaction. Used to locate a specific transaction in minFraud logs
7
+ attr_accessor :transaction_id
8
+
9
+ # @attribute shop_id
10
+ # @return [String] Internal ID for the shop, affiliate, or merchant this order is coming from
11
+ attr_accessor :shop_id
12
+
13
+ # @attribute time
14
+ # @return [String] The date and time the event occurred. The string must be in the RFC 3339 date-time format.
15
+ # If this field is not in the request, the current time will be used
16
+ attr_accessor :time
17
+
18
+ # @attribute type
19
+ # @return [String] The type of event being scored
20
+ enum_accessor :type,
21
+ [
22
+ :account_creation,
23
+ :account_login,
24
+ :email_change,
25
+ :password_reset,
26
+ :payout_change,
27
+ :purchase,
28
+ :recurring_purchase,
29
+ :referral,
30
+ :survey,
31
+ ]
32
+
33
+ # Creates Minfraud::Components::Event instance
34
+ # @param [Hash] params hash of parameters
35
+ # @return [Minfraud::Components::Event] an Event instance
36
+ def initialize(params = {})
37
+ @transaction_id = params[:transaction_id]
38
+ @shop_id = params[:shop_id]
39
+ @time = params[:time]
40
+ self.type = params[:type]
41
+ end
42
+ end
43
+ end
44
+ end