Class: Order::FraudDetector

Inherits:
BaseService show all
Defined in:
app/services/order/fraud_detector.rb

Overview

Service object: fraud detector.

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Delegated Instance Attributes collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

#initialize(order, payment, _options = {}) ⇒ FraudDetector

Returns a new instance of FraudDetector.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/services/order/fraud_detector.rb', line 10

def initialize(order, payment, _options = {})
  @order = order
  @payment = payment
  @customer = @order.billing_entity
  unless @payment.category.in?(%w[PayPal eCheck])
    @billing_address = billing_address
    @shipping_address = shipping_address
    @is_pickup = @order.is_warehouse_pickup?
    @existing_report = @payment.fraud_report
  end
  @fr = FraudReport.new(payment: @payment)
end

Class Method Details

.potential_fraud(fraud_report) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/services/order/fraud_detector.rb', line 113

def self.potential_fraud(fraud_report)
  reasons = []
  reasons << 'Stripe reports an elevated risk level' if fraud_report.radar_risk_level == 'elevated'
  reasons << 'Paypal order needs to be manually checked by Venu and released.' if fraud_report.radar_risk_level == 'paypal_high'
  reasons << 'eCheck payments over $2,000 need to be manually checked by Venu and released.' if fraud_report.radar_risk_level == 'echeck_high'
  reasons << "Credit card country is outside North America." if fraud_report.credit_card_country.present? && !Country.find_by(iso: fraud_report.credit_card_country).north_america?
  reasons << "Unable to complete fraud report, error from Minfraud: #{fraud_report.minfraud_error}" if fraud_report.minfraud_error.present?
  reasons << "Minfraud reports a high risk score (#{fraud_report.minfraud_risk_score})" if fraud_report.minfraud_risk_score.present? && (fraud_report.minfraud_risk_score > 50)
  if fraud_report.billing_address_line1_check == 'fail'
    reasons << 'Billing address street does not match registered credit card address'
  elsif %w[unavailable unchecked].include?(fraud_report.billing_address_line1_check)
    reasons << 'Billing address street check was unavailable'
  end
  if fraud_report.billing_address_zip_check == 'fail'
    reasons << 'Billing address zip does not match registered credit card address'
  elsif %w[unavailable unchecked].include?(fraud_report.billing_address_zip_check)
    reasons << 'Billing address zip check was unavailable'
  end
  if fraud_report.credit_card_cvc_check == 'fail'
    reasons << 'Credit card CVC check failed'
  elsif %w[unavailable unchecked].include?(fraud_report.credit_card_cvc_check)
    reasons << 'Credit card CVC check was unavailable'
  end
  if fraud_report.is_online_order?
    if fraud_report.shipping_state_different_to_billing_state?
      reasons << 'Shipping address state is different to billing address state'
    elsif fraud_report.shipping_zip_different_to_billing_zip?
      reasons << 'Shipping address zip is different to billing address zip'
    end
    reasons << 'Order is shipping to a high risk province (Quebec)' if fraud_report.shipping_state_is_qc?
  end
  reasons << 'First time order with different billing and shipping address' if fraud_report.billing_and_shipping_address_different? && fraud_report.first_time_order?
  if fraud_report.anonymizer_confidence.present? && fraud_report.anonymizer_confidence > 80
    provider = fraud_report.anonymizer_provider_name.present? ? " (#{fraud_report.anonymizer_provider_name})" : ''
    reasons << "High-confidence VPN/anonymizer detected#{provider} (#{fraud_report.anonymizer_confidence}% confidence)"
  end
  reasons << 'Disposable email address detected' if fraud_report.email_is_disposable?
  reasons << "Email domain is #{fraud_report.email_domain_visit_status.humanize.downcase}" if fraud_report.email_domain_visit_status.present? && fraud_report.email_domain_visit_status.in?(%w[parked dns_error])

  { result: reasons.any?, reasons: reasons }
end

Instance Method Details

#billing_addressObject

Alias for @payment#billing_address

Returns:

  • (Object)

    @payment#billing_address

See Also:



163
# File 'app/services/order/fraud_detector.rb', line 163

delegate :billing_address, to: :@payment

#delete_old_fraud_reportObject



76
77
78
79
80
81
82
# File 'app/services/order/fraud_detector.rb', line 76

def delete_old_fraud_report
  old_reports = FraudReport.where(payment_id: @payment.id)
  return nil if old_reports.empty?

  @fr.previous_minfraud_id = old_reports.map(&:minfraud_id).join(',')
  old_reports.destroy_all
end

#distance_between(address1, address2) ⇒ Object



155
156
157
158
159
160
161
# File 'app/services/order/fraud_detector.rb', line 155

def distance_between(address1, address2)
  res = nil
  geo1 = Geocoder.search(address1.address_for_geocoder)&.first
  geo2 = Geocoder.search(address2.address_for_geocoder)&.first
  res = Geocoder::Calculations.distance_between([geo1.latitude, geo1.longitude], [geo2.latitude, geo2.longitude]) if geo1.present? && geo2.present?
  res
end

#do_minfraud_assessmentObject

Run the minFraud Insights assessment, populating @fr in place. The request
building and response mapping live in Minfraud::FraudReportAssessment.



86
87
88
# File 'app/services/order/fraud_detector.rb', line 86

def do_minfraud_assessment
  Minfraud::FraudReportAssessment.new(@order, @payment, @fr, is_pickup: @is_pickup).assess!
end

#process(force_new_report = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/services/order/fraud_detector.rb', line 23

def process(force_new_report = false)
  # if we've already done a fraud report for this auth just return those results
  # unless a new report has been requested
  if @payment.category == 'PayPal'
    # First time order or lifetime revenue should be at least 50% of the order in question
    # details = Paypal.get_transaction_details(@payment)
    # details['transaction_details'][]
    invoiced_orders = @customer.orders.invoiced
    @fr.radar_risk_level = 'paypal_high' if invoiced_orders.empty? || invoiced_orders.sum(&:total) > (@payment.amount * 0.5)
  elsif @payment.category == 'eCheck'
    @fr.radar_risk_level = 'echeck_high' if @payment.amount > Payment::ECHECK_MIN_AMOUNT_WITHOUT_SUPERVISION
  elsif @existing_report && (force_new_report == false)
    @existing_report
  else
    delete_old_fraud_report
    @fr.is_online_order = @order.online_order?
    @fr.first_time_order = @customer.orders.invoiced.empty?
    @fr.shipping_state_is_qc = @shipping_address.state_code == 'QC'
    # Here we implement billing and shipping address comparisons unless auth billing address is nil AND the auth is either a www apple_pay auth which hides the billing address, or crm auth linked to a legacy crm that did not store the billing address, at which point we skip the comparisons
    unless @payment.billing_address_line1.nil? && (@payment.is_www_apple_pay? || @payment.is_crm_legacy_vault?)
      @fr.shipping_state_different_to_billing_state = @is_pickup ? false : @billing_address.state_code != @shipping_address.state_code
      @fr.shipping_zip_different_to_billing_zip = @is_pickup ? false : zip_codes_different?(@billing_address, @shipping_address)
      @fr.billing_and_shipping_address_different = false
      unless @shipping_address.is_warehouse
        # here we test for non warehouse pickups
        # if the billing address and shipping address can geocode and are less than ~50 feet from each other, consider this OK
        if (d = distance_between(@billing_address, @shipping_address)).present? && d < 0.01 # miles
        else
          # otherwise use the address same_as method to see if the billing address and shipping address are different, this method sometimes gives false positives based on weird characters or trivial differences
          @fr.billing_and_shipping_address_different = true unless @billing_address.same_as(@shipping_address)
        end
      end
    end

    @fr.radar_risk_level = @payment.radar_risk_level
    @fr.radar_reason = @payment.radar_reason

    @fr.billing_address_line1_check = @payment.address_line1_check
    @fr.billing_address_zip_check = @payment.address_zip_check
    @fr.credit_card_cvc_check = @payment.cvc_check
    @fr.credit_card_country = @payment.card_country

    do_minfraud_assessment if @order.online_order? && !@payment.skip_minfraud? && Rails.env.production?
  end
  @fr.save!
  returned_report = @fr

  Result.new(
    fraud_report: returned_report,
    potential_fraud: Order::FraudDetector.potential_fraud(@fr)[:result]
  )
end

#shipping_addressObject



165
166
167
168
169
# File 'app/services/order/fraud_detector.rb', line 165

def shipping_address
  address = @payment.shipping_address
  address.is_warehouse = warehouse_pickup?
  address
end

#warehouse_pickup?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'app/services/order/fraud_detector.rb', line 171

def warehouse_pickup?
  @payment.shipping_address_line1.try(:downcase)&.include?('590 telser') || @payment.shipping_address_line1.try(:downcase)&.include?('300 granton')
end

#zip_codes_different?(billing_address, shipping_address) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/services/order/fraud_detector.rb', line 90

def zip_codes_different?(billing_address, shipping_address)
  if billing_address.zip.present? && shipping_address.zip.present?
    b_zip = billing_address.zip.upcase.delete(' ')
    s_zip = shipping_address.zip.upcase.delete(' ')
    if (billing_address.country_iso3 == 'USA') && (shipping_address.country_iso3 == 'USA')
      # both USA addresses, so check if we should only compare the first 5 digits
      if ((b_zip.length == 5) && (s_zip.length > 5)) || ((b_zip.length > 5) && (s_zip.length == 5))
        # b_zip is 5 digits and s_zip is more than 5, or vice versa, so just compare the first 5
        b_zip[0..4] != s_zip[0..4]
      else
        # they're both the same length, so do a full comparison
        b_zip != s_zip
      end
    else
      # one or both are non-USA zips, so compare complete zip codes
      b_zip != s_zip
    end
  else
    # If zip is not present, then we say zip codes are different to force an error.
    true
  end
end