PageRenderTime 235ms CodeModel.GetById 36ms RepoModel.GetById 6ms app.codeStats 0ms

/catalog/model/extension/fraud/maxmind.php

https://gitlab.com/dadangnh/sb1-bon
PHP | 380 lines | 307 code | 65 blank | 8 comment | 106 complexity | fe4473964cd84e5f0dc28e6525711c57 MD5 | raw file
  1. <?php
  2. class ModelExtensionFraudMaxMind extends Model {
  3. public function check($order_info) {
  4. $risk_score = 0;
  5. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "maxmind` WHERE order_id = '" . (int)$order_info['order_id'] . "'");
  6. if ($query->num_rows) {
  7. $risk_score = $query->row['risk_score'];
  8. } else {
  9. /*
  10. maxmind api
  11. http://www.maxmind.com/app/ccv
  12. paypal api
  13. https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_IPNandPDTVariables
  14. */
  15. $request = 'i=' . urlencode($order_info['ip']);
  16. $request .= '&city=' . urlencode($order_info['payment_city']);
  17. $request .= '&region=' . urlencode($order_info['payment_zone']);
  18. $request .= '&postal=' . urlencode($order_info['payment_postcode']);
  19. $request .= '&country=' . urlencode($order_info['payment_country']);
  20. $request .= '&domain=' . urlencode(utf8_substr(strrchr($order_info['email'], '@'), 1));
  21. $request .= '&custPhone=' . urlencode($order_info['telephone']);
  22. $request .= '&license_key=' . urlencode($this->config->get('maxmind_key'));
  23. if ($order_info['shipping_method']) {
  24. $request .= '&shipAddr=' . urlencode($order_info['shipping_address_1']);
  25. $request .= '&shipCity=' . urlencode($order_info['shipping_city']);
  26. $request .= '&shipRegion=' . urlencode($order_info['shipping_zone']);
  27. $request .= '&shipPostal=' . urlencode($order_info['shipping_postcode']);
  28. $request .= '&shipCountry=' . urlencode($order_info['shipping_country']);
  29. }
  30. $request .= '&user_agent=' . urlencode($order_info['user_agent']);
  31. $request .= '&forwardedIP=' . urlencode($order_info['forwarded_ip']);
  32. $request .= '&emailMD5=' . urlencode(md5(utf8_strtolower($order_info['email'])));
  33. //$request .= '&passwordMD5=' . urlencode($order_info['password']);
  34. $request .= '&accept_language=' . urlencode($order_info['accept_language']);
  35. $request .= '&order_amount=' . urlencode($this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false));
  36. $request .= '&order_currency=' . urlencode($order_info['currency_code']);
  37. $curl = curl_init('https://minfraud1.maxmind.com/app/ccv2r');
  38. curl_setopt($curl, CURLOPT_HEADER, 0);
  39. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  40. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  41. curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
  42. curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
  43. curl_setopt($curl, CURLOPT_POST, 1);
  44. curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
  45. $response = curl_exec($curl);
  46. curl_close($curl);
  47. $risk_score = 0;
  48. if ($response) {
  49. $order_id = $order_info['order_id'];
  50. $customer_id = $order_info['customer_id'];
  51. $response_info = array();
  52. $parts = explode(';', $response);
  53. foreach ($parts as $part) {
  54. list($key, $value) = explode('=', $part);
  55. $response_info[$key] = $value;
  56. }
  57. if (isset($response_info['countryMatch'])) {
  58. $country_match = $response_info['countryMatch'];
  59. } else {
  60. $country_match = '';
  61. }
  62. if (isset($response_info['countryCode'])) {
  63. $country_code = $response_info['countryCode'];
  64. } else {
  65. $country_code = '';
  66. }
  67. if (isset($response_info['highRiskCountry'])) {
  68. $high_risk_country = $response_info['highRiskCountry'];
  69. } else {
  70. $high_risk_country = '';
  71. }
  72. if (isset($response_info['distance'])) {
  73. $distance = $response_info['distance'];
  74. } else {
  75. $distance = '';
  76. }
  77. if (isset($response_info['ip_region'])) {
  78. $ip_region = $response_info['ip_region'];
  79. } else {
  80. $ip_region = '';
  81. }
  82. if (isset($response_info['ip_city'])) {
  83. $ip_city = $response_info['ip_city'];
  84. } else {
  85. $ip_city = '';
  86. }
  87. if (isset($response_info['ip_latitude'])) {
  88. $ip_latitude = $response_info['ip_latitude'];
  89. } else {
  90. $ip_latitude = '';
  91. }
  92. if (isset($response_info['ip_longitude'])) {
  93. $ip_longitude = $response_info['ip_longitude'];
  94. } else {
  95. $ip_longitude = '';
  96. }
  97. if (isset($response_info['ip_isp'])) {
  98. $ip_isp = $response_info['ip_isp'];
  99. } else {
  100. $ip_isp = '';
  101. }
  102. if (isset($response_info['ip_org'])) {
  103. $ip_org = $response_info['ip_org'];
  104. } else {
  105. $ip_org = '';
  106. }
  107. if (isset($response_info['ip_asnum'])) {
  108. $ip_asnum = $response_info['ip_asnum'];
  109. } else {
  110. $ip_asnum = '';
  111. }
  112. if (isset($response_info['ip_userType'])) {
  113. $ip_user_type = $response_info['ip_userType'];
  114. } else {
  115. $ip_user_type = '';
  116. }
  117. if (isset($response_info['ip_countryConf'])) {
  118. $ip_country_confidence = $response_info['ip_countryConf'];
  119. } else {
  120. $ip_country_confidence = '';
  121. }
  122. if (isset($response_info['ip_regionConf'])) {
  123. $ip_region_confidence = $response_info['ip_regionConf'];
  124. } else {
  125. $ip_region_confidence = '';
  126. }
  127. if (isset($response_info['ip_cityConf'])) {
  128. $ip_city_confidence = $response_info['ip_cityConf'];
  129. } else {
  130. $ip_city_confidence = '';
  131. }
  132. if (isset($response_info['ip_postalConf'])) {
  133. $ip_postal_confidence = $response_info['ip_postalConf'];
  134. } else {
  135. $ip_postal_confidence = '';
  136. }
  137. if (isset($response_info['ip_postalCode'])) {
  138. $ip_postal_code = $response_info['ip_postalCode'];
  139. } else {
  140. $ip_postal_code = '';
  141. }
  142. if (isset($response_info['ip_accuracyRadius'])) {
  143. $ip_accuracy_radius = $response_info['ip_accuracyRadius'];
  144. } else {
  145. $ip_accuracy_radius = '';
  146. }
  147. if (isset($response_info['ip_netSpeedCell'])) {
  148. $ip_net_speed_cell = $response_info['ip_netSpeedCell'];
  149. } else {
  150. $ip_net_speed_cell = '';
  151. }
  152. if (isset($response_info['ip_metroCode'])) {
  153. $ip_metro_code = $response_info['ip_metroCode'];
  154. } else {
  155. $ip_metro_code = '';
  156. }
  157. if (isset($response_info['ip_areaCode'])) {
  158. $ip_area_code = $response_info['ip_areaCode'];
  159. } else {
  160. $ip_area_code = '';
  161. }
  162. if (isset($response_info['ip_timeZone'])) {
  163. $ip_time_zone = $response_info['ip_timeZone'];
  164. } else {
  165. $ip_time_zone = '';
  166. }
  167. if (isset($response_info['ip_regionName'])) {
  168. $ip_region_name = $response_info['ip_regionName'];
  169. } else {
  170. $ip_region_name = '';
  171. }
  172. if (isset($response_info['ip_domain'])) {
  173. $ip_domain = $response_info['ip_domain'];
  174. } else {
  175. $ip_domain = '';
  176. }
  177. if (isset($response_info['ip_countryName'])) {
  178. $ip_country_name = $response_info['ip_countryName'];
  179. } else {
  180. $ip_country_name = '';
  181. }
  182. if (isset($response_info['ip_continentCode'])) {
  183. $ip_continent_code = $response_info['ip_continentCode'];
  184. } else {
  185. $ip_continent_code = '';
  186. }
  187. if (isset($response_info['ip_corporateProxy'])) {
  188. $ip_corporate_proxy = $response_info['ip_corporateProxy'];
  189. } else {
  190. $ip_corporate_proxy = '';
  191. }
  192. if (isset($response_info['anonymousProxy'])) {
  193. $anonymous_proxy = $response_info['anonymousProxy'];
  194. } else {
  195. $anonymous_proxy = '';
  196. }
  197. if (isset($response_info['proxyScore'])) {
  198. $proxy_score = $response_info['proxyScore'];
  199. } else {
  200. $proxy_score = '';
  201. }
  202. if (isset($response_info['isTransProxy'])) {
  203. $is_trans_proxy = $response_info['isTransProxy'];
  204. } else {
  205. $is_trans_proxy = '';
  206. }
  207. if (isset($response_info['freeMail'])) {
  208. $free_mail = $response_info['freeMail'];
  209. } else {
  210. $free_mail = '';
  211. }
  212. if (isset($response_info['carderEmail'])) {
  213. $carder_email = $response_info['carderEmail'];
  214. } else {
  215. $carder_email = '';
  216. }
  217. if (isset($response_info['highRiskUsername'])) {
  218. $high_risk_username = $response_info['highRiskUsername'];
  219. } else {
  220. $high_risk_username = '';
  221. }
  222. if (isset($response_info['highRiskPassword'])) {
  223. $high_risk_password = $response_info['highRiskPassword'];
  224. } else {
  225. $high_risk_password = '';
  226. }
  227. if (isset($response_info['binMatch'])) {
  228. $bin_match = $response_info['binMatch'];
  229. } else {
  230. $bin_match = '';
  231. }
  232. if (isset($response_info['binCountry'])) {
  233. $bin_country = $response_info['binCountry'];
  234. } else {
  235. $bin_country = '';
  236. }
  237. if (isset($response_info['binNameMatch'])) {
  238. $bin_name_match = $response_info['binNameMatch'];
  239. } else {
  240. $bin_name_match = '';
  241. }
  242. if (isset($response_info['binName'])) {
  243. $bin_name = $response_info['binName'];
  244. } else {
  245. $bin_name = '';
  246. }
  247. if (isset($response_info['binPhoneMatch'])) {
  248. $bin_phone_match = $response_info['binPhoneMatch'];
  249. } else {
  250. $bin_phone_match = '';
  251. }
  252. if (isset($response_info['binPhone'])) {
  253. $bin_phone = $response_info['binPhone'];
  254. } else {
  255. $bin_phone = '';
  256. }
  257. if (isset($response_info['custPhoneInBillingLoc'])) {
  258. $customer_phone_in_billing_location = $response_info['custPhoneInBillingLoc'];
  259. } else {
  260. $customer_phone_in_billing_location = '';
  261. }
  262. if (isset($response_info['shipForward'])) {
  263. $ship_forward = $response_info['shipForward'];
  264. } else {
  265. $ship_forward = '';
  266. }
  267. if (isset($response_info['cityPostalMatch'])) {
  268. $city_postal_match = $response_info['cityPostalMatch'];
  269. } else {
  270. $city_postal_match = '';
  271. }
  272. if (isset($response_info['shipCityPostalMatch'])) {
  273. $ship_city_postal_match = $response_info['shipCityPostalMatch'];
  274. } else {
  275. $ship_city_postal_match = '';
  276. }
  277. if (isset($response_info['score'])) {
  278. $score = $response_info['score'];
  279. } else {
  280. $score = '';
  281. }
  282. if (isset($response_info['explanation'])) {
  283. $explanation = $response_info['explanation'];
  284. } else {
  285. $explanation = '';
  286. }
  287. if (isset($response_info['riskScore'])) {
  288. $risk_score = $response_info['riskScore'];
  289. } else {
  290. $risk_score = '';
  291. }
  292. if (isset($response_info['queriesRemaining'])) {
  293. $queries_remaining = $response_info['queriesRemaining'];
  294. } else {
  295. $queries_remaining = '';
  296. }
  297. if (isset($response_info['maxmindID'])) {
  298. $maxmind_id = $response_info['maxmindID'];
  299. } else {
  300. $maxmind_id = '';
  301. }
  302. if (isset($response_info['err'])) {
  303. $error = $response_info['err'];
  304. } else {
  305. $error = '';
  306. }
  307. $this->db->query("INSERT INTO `" . DB_PREFIX . "maxmind` SET order_id = '" . (int)$order_id . "', customer_id = '" . (int)$customer_id . "', country_match = '" . $this->db->escape($country_match) . "', country_code = '" . $this->db->escape($country_code) . "', high_risk_country = '" . $this->db->escape($high_risk_country) . "', distance = '" . (int)$distance . "', ip_region = '" . $this->db->escape($ip_region) . "', ip_city = '" . $this->db->escape($ip_city) . "', ip_latitude = '" . $this->db->escape($ip_latitude) . "', ip_longitude = '" . $this->db->escape($ip_longitude) . "', ip_isp = '" . $this->db->escape($ip_isp) . "', ip_org = '" . $this->db->escape($ip_org) . "', ip_asnum = '" . (int)$ip_asnum . "', ip_user_type = '" . $this->db->escape($ip_user_type) . "', ip_country_confidence = '" . $this->db->escape($ip_country_confidence) . "', ip_region_confidence = '" . $this->db->escape($ip_region_confidence) . "', ip_city_confidence = '" . $this->db->escape($ip_city_confidence) . "', ip_postal_confidence = '" . $this->db->escape($ip_postal_confidence) . "', ip_postal_code = '" . $this->db->escape($ip_postal_code) . "', ip_accuracy_radius = '" . (int)$ip_accuracy_radius . "', ip_net_speed_cell = '" . $this->db->escape($ip_net_speed_cell) . "', ip_metro_code = '" . (int)$ip_metro_code . "', ip_area_code = '" . (int)$ip_area_code . "', ip_time_zone = '" . $this->db->escape($ip_time_zone) . "', ip_region_name = '" . $this->db->escape($ip_region_name) . "', ip_domain = '" . $this->db->escape($ip_domain) . "', ip_country_name = '" . $this->db->escape($ip_country_name) . "', ip_continent_code = '" . $this->db->escape($ip_continent_code) . "', ip_corporate_proxy = '" . $this->db->escape($ip_corporate_proxy) . "', anonymous_proxy = '" . $this->db->escape($anonymous_proxy) . "', proxy_score = '" . (float)$proxy_score . "', is_trans_proxy = '" . $this->db->escape($is_trans_proxy) . "', free_mail = '" . $this->db->escape($free_mail) . "', carder_email = '" . $this->db->escape($carder_email) . "', high_risk_username = '" . $this->db->escape($high_risk_username) . "', high_risk_password = '" . $this->db->escape($high_risk_password) . "', bin_match = '" . $this->db->escape($bin_match) . "', bin_country = '" . $this->db->escape($bin_country) . "', bin_name_match = '" . $this->db->escape($bin_name_match) . "', bin_name = '" . $this->db->escape($bin_name) . "', bin_phone_match = '" . $this->db->escape($bin_phone_match) . "', bin_phone = '" . $this->db->escape($bin_phone) . "', customer_phone_in_billing_location = '" . $this->db->escape($customer_phone_in_billing_location) . "', ship_forward = '" . $this->db->escape($ship_forward) . "', city_postal_match = '" . $this->db->escape($city_postal_match) . "', ship_city_postal_match = '" . $this->db->escape($ship_city_postal_match) . "', score = '" . (float)$score . "', explanation = '" . $this->db->escape($explanation) . "', risk_score = '" . (float)$risk_score . "', queries_remaining = '" . (int)$queries_remaining . "', maxmind_id = '" . $this->db->escape($maxmind_id) . "', error = '" . $this->db->escape($error) . "', date_added = NOW()");
  308. }
  309. }
  310. if ($risk_score > $this->config->get('maxmind_score') && $this->config->get('maxmind_key')) {
  311. return $this->config->get('maxmind_order_status_id');
  312. }
  313. }
  314. }