PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/catalog/model/checkout/fraud.php

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