fraudlabspro.php (7816B)
1 <?php 2 class ModelExtensionFraudFraudLabsPro extends Model { 3 public function check($data) { 4 // Do not perform fraud check if FraudLabs Pro is disabled or API key is not provided. 5 if (!$this->config->get('fraud_fraudlabspro_status') ||!$this->config->get('fraud_fraudlabspro_key')) { 6 return; 7 } 8 9 $risk_score = 0; 10 11 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "fraudlabspro` WHERE order_id = '" . (int)$data['order_id'] . "'"); 12 13 // Do not call FraudLabs Pro API if order is already screened. 14 if ($query->num_rows) { 15 return; 16 } 17 18 $ip = $data['ip']; 19 20 // Detect client IP is store is behind CloudFlare protection. 21 if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)){ 22 $ip = $_SERVER['HTTP_CF_CONNECTING_IP']; 23 } 24 25 // Get real client IP is they are behind proxy server. 26 if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)){ 27 $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 28 } 29 30 // Overwrite client IP if simulate IP is provided. 31 if (filter_var($this->config->get('fraud_fraudlabspro_simulate_ip'), FILTER_VALIDATE_IP)) { 32 $ip = $this->config->get('fraud_fraudlabspro_simulate_ip'); 33 } 34 35 $request['key'] = $this->config->get('fraud_fraudlabspro_key'); 36 $request['ip'] = $ip; 37 $request['first_name'] = $data['firstname']; 38 $request['last_name'] = $data['lastname']; 39 $request['bill_city'] = $data['payment_city']; 40 $request['bill_state'] = $data['payment_zone']; 41 $request['bill_country'] = $data['payment_iso_code_2']; 42 $request['bill_zip_code'] = $data['payment_postcode']; 43 $request['email_domain'] = utf8_substr(strrchr($data['email'], '@'), 1); 44 $request['user_phone'] = $data['telephone']; 45 46 if ($data['shipping_method']) { 47 $request['ship_addr'] = $data['shipping_address_1']; 48 $request['ship_city'] = $data['shipping_city']; 49 $request['ship_state'] = $data['shipping_zone']; 50 $request['ship_zip_code'] = $data['shipping_postcode']; 51 $request['ship_country'] = $data['shipping_iso_code_2']; 52 } 53 54 $request['email'] = $data['email']; 55 $request['email_hash'] = $this->hashIt($data['email']); 56 $request['amount'] = $this->currency->format($data['total'], $data['currency_code'], $data['currency_value'], false); 57 $request['quantity'] = 1; 58 $request['currency'] = $data['currency_code']; 59 $request['payment_mode'] = $data['payment_code']; 60 $request['user_order_id'] = $data['order_id']; 61 $request['flp_checksum'] = (isset($_COOKIE['flp_checksum'])) ? $_COOKIE['flp_checksum'] : ''; 62 $request['format'] = 'json'; 63 $request['source'] = 'opencart'; 64 $request['source_version'] = '2.1.0.2'; 65 66 $curl = curl_init(); 67 curl_setopt($curl, CURLOPT_URL, 'https://api.fraudlabspro.com/v1/order/screen?' . http_build_query($request)); 68 curl_setopt($curl, CURLOPT_HEADER, 0); 69 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 70 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 71 curl_setopt($curl, CURLOPT_FORBID_REUSE, 1); 72 curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1); 73 74 $response = curl_exec($curl); 75 76 curl_close($curl); 77 78 $risk_score = 0; 79 80 if (is_null($json = json_decode($response)) === FALSE) { 81 $this->db->query("REPLACE INTO `" . DB_PREFIX . "fraudlabspro` SET order_id = '" . (int)$data['order_id'] . "', 82 is_country_match = '" . $this->db->escape($json->is_country_match) . "', 83 is_high_risk_country = '" . $this->db->escape($json->is_high_risk_country) . "', 84 distance_in_km = '" . $this->db->escape($json->distance_in_km) . "', 85 distance_in_mile = '" . $this->db->escape($json->distance_in_mile) . "', 86 ip_country = '" . $this->db->escape($json->ip_country) . "', 87 ip_region = '" . $this->db->escape($json->ip_region) . "', 88 ip_city = '" . $this->db->escape($json->ip_city) . "', 89 ip_continent = '" . $this->db->escape($json->ip_continent) . "', 90 ip_latitude = '" . $this->db->escape($json->ip_latitude) . "', 91 ip_longitude = '" . $this->db->escape($json->ip_longitude) . "', 92 ip_timezone = '" . $this->db->escape($json->ip_timezone) . "', 93 ip_elevation = '" . $this->db->escape($json->ip_elevation) . "', 94 ip_domain = '" . $this->db->escape($json->ip_domain) . "', 95 ip_mobile_mnc = '" . $this->db->escape($json->ip_mobile_mnc) . "', 96 ip_mobile_mcc = '" . $this->db->escape($json->ip_mobile_mcc) . "', 97 ip_mobile_brand = '" . $this->db->escape($json->ip_mobile_brand) . "', 98 ip_netspeed = '" . $this->db->escape($json->ip_netspeed) . "', 99 ip_isp_name = '" . $this->db->escape($json->ip_isp_name) . "', 100 ip_usage_type = '" . $this->db->escape($json->ip_usage_type) . "', 101 is_free_email = '" . $this->db->escape($json->is_free_email) . "', 102 is_new_domain_name = '" . $this->db->escape($json->is_new_domain_name) . "', 103 is_proxy_ip_address = '" . $this->db->escape($json->is_proxy_ip_address) . "', 104 is_bin_found = '" . $this->db->escape($json->is_bin_found) . "', 105 is_bin_country_match = '" . $this->db->escape($json->is_bin_country_match) . "', 106 is_bin_name_match = '" . $this->db->escape($json->is_bin_name_match) . "', 107 is_bin_phone_match = '" . $this->db->escape($json->is_bin_phone_match) . "', 108 is_bin_prepaid = '" . $this->db->escape($json->is_bin_prepaid) . "', 109 is_address_ship_forward = '" . $this->db->escape($json->is_address_ship_forward) . "', 110 is_bill_ship_city_match = '" . $this->db->escape($json->is_bill_ship_city_match) . "', 111 is_bill_ship_state_match = '" . $this->db->escape($json->is_bill_ship_state_match) . "', 112 is_bill_ship_country_match = '" . $this->db->escape($json->is_bill_ship_country_match) . "', 113 is_bill_ship_postal_match = '" . $this->db->escape($json->is_bill_ship_postal_match) . "', 114 is_ip_blacklist = '" . $this->db->escape($json->is_ip_blacklist) . "', 115 is_email_blacklist = '" . $this->db->escape($json->is_email_blacklist) . "', 116 is_credit_card_blacklist = '" . $this->db->escape($json->is_credit_card_blacklist) . "', 117 is_device_blacklist = '" . $this->db->escape($json->is_device_blacklist) . "', 118 is_user_blacklist = '" . $this->db->escape($json->is_user_blacklist) . "', 119 fraudlabspro_score = '" . $this->db->escape($json->fraudlabspro_score) . "', 120 fraudlabspro_distribution = '" . $this->db->escape($json->fraudlabspro_distribution) . "', 121 fraudlabspro_status = '" . $this->db->escape($json->fraudlabspro_status) . "', 122 fraudlabspro_id = '" . $this->db->escape($json->fraudlabspro_id) . "', 123 fraudlabspro_error = '" . $this->db->escape($json->fraudlabspro_error_code) . "', 124 fraudlabspro_message = '" . $this->db->escape($json->fraudlabspro_message) . "', 125 fraudlabspro_credits = '" . $this->db->escape($json->fraudlabspro_credits) . "', 126 api_key = '" . $this->config->get('fraud_fraudlabspro_key') . "', 127 ip_address = '" . $ip . "'" 128 ); 129 130 $risk_score = (int)$json->fraudlabspro_score; 131 } 132 133 // Do not perform any action if error found 134 if ($json->fraudlabspro_error_code) { 135 return; 136 } 137 138 if ($risk_score > $this->config->get('fraud_fraudlabspro_score')) { 139 return $this->config->get('fraud_fraudlabspro_order_status_id'); 140 } 141 142 if ($json->fraudlabspro_status == 'REVIEW') { 143 return $this->config->get('fraud_fraudlabspro_review_status_id'); 144 } 145 146 if ($json->fraudlabspro_status == 'APPROVE') { 147 return $this->config->get('fraud_fraudlabspro_approve_status_id'); 148 } 149 150 if ($json->fraudlabspro_status == 'REJECT') { 151 return $this->config->get('fraudlabspro_reject_status_id'); 152 } 153 } 154 155 private function hashIt($s) { 156 $hash = 'fraudlabspro_' . $s; 157 158 for ($i = 0; $i < 65536; $i++) 159 $hash = sha1('fraudlabspro_' . $hash); 160 161 return $hash; 162 } 163 }