PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/catalog/model/fraud/fraudlabspro.php

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