Class: Order::FraudDetector
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
105
106
107
108
109
110
111
112
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
|
# File 'app/services/order/fraud_detector.rb', line 105
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?
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?
{ result: reasons.any?, reasons: reasons }
end
|
Instance Method Details
#billing_address ⇒ Object
Alias for @payment#billing_address
147
|
# File 'app/services/order/fraud_detector.rb', line 147
delegate :billing_address, to: :@payment
|
#delete_old_fraud_report ⇒ Object
74
75
76
77
78
79
|
# File 'app/services/order/fraud_detector.rb', line 74
def delete_old_fraud_report
old_reports = FraudReport.where(payment_id: @payment.id)
return nil if old_reports.empty?
old_reports.destroy_all
end
|
#distance_between(address1, address2) ⇒ Object
139
140
141
142
143
144
145
|
# File 'app/services/order/fraud_detector.rb', line 139
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
|
#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
|
# File 'app/services/order/fraud_detector.rb', line 23
def process(force_new_report = false)
if @payment.category == 'PayPal'
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'
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
if (d = distance_between(@billing_address, @shipping_address)).present? && d < 0.01 else
@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
end
@fr.save!
returned_report = @fr
Result.new(
fraud_report: returned_report,
potential_fraud: Order::FraudDetector.potential_fraud(@fr)[:result]
)
end
|
#shipping_address ⇒ Object
149
150
151
152
153
|
# File 'app/services/order/fraud_detector.rb', line 149
def shipping_address
address = @payment.shipping_address
address.is_warehouse = warehouse_pickup?
address
end
|
#warehouse_pickup? ⇒ Boolean
155
156
157
|
# File 'app/services/order/fraud_detector.rb', line 155
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'app/services/order/fraud_detector.rb', line 82
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')
if ((b_zip.length == 5) && (s_zip.length > 5)) || ((b_zip.length > 5) && (s_zip.length == 5))
b_zip[0..4] != s_zip[0..4]
else
b_zip != s_zip
end
else
b_zip != s_zip
end
else
true
end
end
|