Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.84 KB | None | 0 0
  1. <?php
  2.  
  3. namespace emag;
  4.  
  5.  
  6. class Emag {
  7.  
  8. public $config;
  9. public $db;
  10.  
  11. public $id;
  12. public $country;
  13.  
  14. public $id_emag_mp_api_call;
  15. public $date_created;
  16. public $resource;
  17. public $action;
  18. public $last_definition;
  19. public $message_out;
  20. public $message_in;
  21. public $status;
  22. public $date_sent;
  23. public $id_order;
  24.  
  25. public $emag_mp_api_url = null;
  26. public $emag_mp_vendorcode = null;
  27. public $emag_mp_vendorusername = null;
  28. public $emag_mp_vendorpassword = null;
  29.  
  30. public $data = null;
  31. public $message_in_json = null;
  32.  
  33. public $module_version = '1.0';
  34.  
  35. protected $registry;
  36.  
  37. //EMAG BG
  38.  
  39. public $EMAG_BG_VENDOR_CODE = ' Yakobg';
  40. public $EMAG_BG_VENDOR_USERNAME = 'Yakobg';
  41. public $EMAG_BG_VENDOR_PASSWORD = 'yakobg5022';
  42. public $EMAG_BG_API_URL = 'https://marketplace.emag.bg/api-3';
  43.  
  44. //EMAG RO
  45.  
  46. public $EMAG_RO_VENDOR_CODE = 'saxosport';
  47. public $EMAG_RO_VENDOR_USERNAME = 'saxosport';
  48. public $EMAG_RO_VENDOR_PASSWORD = 'saxosport10';
  49. public $EMAG_RO_API_URL = 'https://marketplace.emag.ro/api-3';
  50.  
  51. /**
  52. * @see ObjectModel::$definition
  53. */
  54. /**
  55. * @todo
  56. * Kato napravq vryzkata i izprashtaneto, da napravq i tabica, koqto da pazi rezultatite
  57. */
  58.  
  59. /* public $definition = array(
  60. 'table' => 'emagmp_api_calls',
  61. 'primary' => 'id_emagmp_api_call',
  62. 'fields' => array(
  63. 'date_created' => array('type' => 'string'),
  64. 'resource' => array('type' => 'string'),
  65. 'action' => array('type' => 'string'),
  66. 'last_definition' => array('type' => 'string'),
  67. 'message_out' => array('type' => 'string'),
  68. 'message_in' => array('type' => 'string'),
  69. 'status' => array('type' => 'string', 'default' => 'pending'),
  70. 'date_sent' => array('type' => 'string'),
  71. 'id_order' => array('type' => 'integer')
  72. )
  73. );*/
  74.  
  75. public function __construct() {
  76.  
  77. // $this->config = $registry->get('config');
  78. // $this->db = $registry->get('db');
  79. // $this->request = $registry->get('request');
  80. // $this->log = $registry->get('log');
  81. // $this->registry = $registry;
  82. // require("/home/new/public_html/admin/config.php");
  83. $this->initConnection();
  84. }
  85.  
  86. public function initConnection($country){
  87. $this->country = $country;
  88. if($country !== 'ro') {
  89. $this->emag_mp_api_url = $this->EMAG_BG_API_URL;
  90. $this->emag_mp_vendorcode = $this->EMAG_BG_VENDOR_CODE;
  91. $this->emag_mp_vendorusername = $this->EMAG_BG_VENDOR_USERNAME;
  92. $this->emag_mp_vendorpassword = $this->EMAG_BG_VENDOR_PASSWORD;
  93. } elseif($country !== 'bg') {
  94. $this->emag_mp_api_url = $this->EMAG_RO_API_URL;
  95. $this->emag_mp_vendorcode = $this->EMAG_RO_VENDOR_CODE;
  96. $this->emag_mp_vendorusername = $this->EMAG_RO_VENDOR_USERNAME;
  97. $this->emag_mp_vendorpassword = $this->EMAG_RO_VENDOR_PASSWORD;
  98. } else {
  99. return "Can not initialize bg or ro parameters! ";
  100. }
  101. // require("/home/new/public_html/admin/config.php");
  102. }
  103.  
  104. public function execute() {
  105.  
  106. $debug_info = array(
  107. 'site' => HTTP_SERVER,
  108. 'platform' => 'OpenCart',
  109. 'version' => VERSION,
  110. 'extension_version' => $this->module_version,
  111. 'others' => ''
  112. );
  113.  
  114. $hash = sha1(http_build_query($this->data) . sha1($this->emag_mp_vendorpassword));
  115. $requestData = array(
  116. 'code' => $this->emag_mp_vendorcode,
  117. 'username' => $this->emag_mp_vendorusername,
  118. 'data' => $this->data,
  119. 'hash' => $hash,
  120. 'debug_info' => $debug_info
  121. );
  122.  
  123. $ch = curl_init();
  124. $url = $this->emag_mp_api_url.'/'.$this->resource.'/'.$this->action;
  125.  
  126. if ($this->resource == 'order' && $this->action == 'acknowledge')
  127. $url .= '/'.$this->data['id'];
  128.  
  129. curl_setopt($ch, CURLOPT_URL, $url);
  130. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  131. //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  132. curl_setopt($ch, CURLOPT_HEADER, false);
  133. //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  134. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  135. curl_setopt($ch, CURLOPT_POST, true);
  136. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  137. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
  138.  
  139. $fp = fopen(DIR_LOGS.'emag_mp_call_result.txt', 'a');
  140. /*curl_setopt($ch, CURLOPT_VERBOSE, true);
  141. curl_setopt($ch, CURLOPT_STDERR, $fp);*/
  142.  
  143. $result = curl_exec($ch);
  144.  
  145. $this->date_sent = date("Y-m-d H:i:s");
  146.  
  147. /*ob_start();
  148. echo "\n\n---------------------------------------------------------------------------\n";
  149. print_r($this->data);
  150. print_r(curl_getinfo($ch, CURLINFO_HEADER_OUT));
  151. fwrite($fp, ob_get_contents());
  152. ob_end_clean();*/
  153.  
  154. //fwrite($fp, "\n\n\n".$result);
  155.  
  156. if (curl_errno($ch))
  157. {
  158. $this->message_in = curl_error($ch);
  159. $this->status = 'error';
  160. }
  161. else
  162. {
  163. $this->message_in = $result;
  164. $this->message_in_json = json_decode($result);
  165.  
  166. if (is_object($this->message_in_json) && $this->message_in_json->isError === false)
  167. {
  168. $this->status = 'success';
  169. }
  170. else
  171. {
  172. $this->status = 'error';
  173. }
  174. }
  175.  
  176. curl_close($ch);
  177.  
  178. fclose($fp);
  179.  
  180. // save last sent data for products and orders
  181. /* if ($this->status == 'success' && $this->action == 'save')
  182. {
  183. switch ($this->resource)
  184. {
  185. case 'product_offer':
  186. $definition_table_name = 'emag_mp_product_combinations';
  187. $definition_table_primary_field = 'combination_id';
  188. break;
  189. case 'order':
  190. $definition_table_name = 'emag_mp_order_history';
  191. $definition_table_primary_field = 'emag_order_id';
  192. break;
  193. }
  194.  
  195. if ($definition_table_name && $definition_table_primary_field)
  196. {
  197. $this->db->query('
  198. UPDATE `'.DB_PREFIX.$definition_table_name.'` SET
  199. last_definition = \''.$this->db->escape($this->last_definition).'\'
  200. WHERE '.$definition_table_primary_field.' = '.(int)$this->data[0]['id'].'
  201. ');
  202. }
  203. }*/
  204.  
  205. // save last eMAG order definition
  206. /* if ($this->status == 'success' && $this->action == 'read' && $this->resource == 'order' && isset($this->data['id']))
  207. {
  208. $definition_table_name = 'emagmp_order_history';
  209. $definition_table_primary_field = 'emag_order_id';
  210. $this->db->query('
  211. UPDATE `'.DB_PREFIX.$definition_table_name.'` SET
  212. emag_definition = \''.$this->db->escape(serialize($this->message_in_json->results[0])).'\'
  213. WHERE '.$definition_table_primary_field.' = '.(int)$this->data['id'].'
  214. ');
  215. }*/
  216. }
  217.  
  218. public function save()
  219. {
  220. $fp = fopen(DIR_LOGS.'emag_mp_call_queue.txt', 'a');
  221. ob_start();
  222. echo "\n\n---------------------------------------------------------------------------\n";
  223. print_r($this->data);
  224. echo $this->resource.'/'.$this->action."\n";
  225. //fwrite($fp, ob_get_contents());
  226. ob_end_clean();
  227. fclose($fp);
  228.  
  229. $this->message_out = serialize($this->data);
  230.  
  231. $sql_action = "INSERT INTO";
  232. $sql_condition = "";
  233.  
  234. if ($this->id)
  235. {
  236. $sql_action = "UPDATE";
  237. $sql_condition = "WHERE ".$this->definition['primary']." = ".(int)$this->id;
  238. }
  239.  
  240. $sql_fields = "";
  241. foreach ($this->definition['fields'] as $field_name => $field_definition)
  242. {
  243. if ($sql_fields)
  244. $sql_fields .= ", ";
  245. switch ($field_definition['type'])
  246. {
  247. case 'integer':
  248. $value = (int)$this->{$field_name};
  249. break;
  250. case 'string':
  251. $value = "'".$this->db->escape($this->{$field_name})."'";
  252. break;
  253. }
  254. $sql_fields .= "$field_name = $value";
  255. }
  256.  
  257. $this->db->query("
  258. $sql_action ".DB_PREFIX.$this->definition['table']." SET
  259. $sql_fields
  260. $sql_condition
  261. ");
  262.  
  263. $this->id = $this->db->getLastId();
  264. $this->{$this->definition['primary']} = $this->id;
  265.  
  266. return true;
  267. }
  268.  
  269. /**
  270. * Vryshta pozvolenite harakteristiki za syotvetnata kategoriq
  271. */
  272.  
  273. /**
  274. * Vryshta naj - golqmoto id +1
  275. */
  276.  
  277. function product_get_max_id($id) {
  278. $product_max_ext_id = $db->query("SELECT MAX(vendor_ext_id) as last_vendor_id FROM product");
  279.  
  280. $product_option_value_max_ext_id = $db->query("SELECT MAX(vendor_ext_id) as last_vendor_id FROM product_option_value");
  281.  
  282. if ($product_max_ext_id['last_vendor_id'] > $product_option_value_max_ext_id['last_vendor_id']) {
  283. return $product_max_ext_id['MAX(vendor_ext_id)'] + 1;
  284. }
  285. return $product_option_value_max_ext_id['MAX(vendor_ext_id)'] + 1;
  286. }
  287.  
  288. function generate_product_vendor_ext_id (){
  289.  
  290. $select_product_zero_ids = $db->query("SELECT product_id, vendor_ext_id FROM product WHERE vendor_ext_id = 0");
  291.  
  292. foreach ($select_product_zero_ids as $select_product_zero_id) {
  293. $product = product_get_max_id($id);
  294. $db->query("UPDATE product SET vendor_ext_id = '". $product ."' WHERE (product_id = '". $select_product_zero_id['product_id'] ."') ");
  295. echo "Сетнато vendor_ext_id - " . $product . " За продукт с Код - " . $select_product_zero_id['product_id'] . "<br />";
  296. }
  297.  
  298. $select_product_option_value_zero_ids = $db->query("SELECT product_option_value_id, vendor_ext_id FROM product_option_value WHERE vendor_ext_id = 0");
  299.  
  300. foreach ($select_product_option_value_zero_ids as $select_product_option_value_zero_id)
  301. {
  302. $product = product_get_max_id($id);
  303. $db->query("UPDATE product_option_value SET vendor_ext_id = '". $product ."' WHERE (product_option_value_id = '". $select_product_option_value_zero_id['product_option_value_id'] ."') ");
  304. echo "Сетнато vendor_ext_id - " . $product . " За вариант на продукт с Код - " . $select_product_option_value_zero_id['product_option_value_id'] . "<br />";
  305. }
  306.  
  307. // echo " Max Vendor Id is - " . (product_get_max_id($id)) . PHP_EOL;
  308. }
  309.  
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement