Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace emag;
- class Emag {
- public $config;
- public $db;
- public $id;
- public $country;
- public $id_emag_mp_api_call;
- public $date_created;
- public $resource;
- public $action;
- public $last_definition;
- public $message_out;
- public $message_in;
- public $status;
- public $date_sent;
- public $id_order;
- public $emag_mp_api_url = null;
- public $emag_mp_vendorcode = null;
- public $emag_mp_vendorusername = null;
- public $emag_mp_vendorpassword = null;
- public $data = null;
- public $message_in_json = null;
- public $module_version = '1.0';
- protected $registry;
- //EMAG BG
- public $EMAG_BG_VENDOR_CODE = ' Yakobg';
- public $EMAG_BG_VENDOR_USERNAME = 'Yakobg';
- public $EMAG_BG_VENDOR_PASSWORD = 'yakobg5022';
- public $EMAG_BG_API_URL = 'https://marketplace.emag.bg/api-3';
- //EMAG RO
- public $EMAG_RO_VENDOR_CODE = 'saxosport';
- public $EMAG_RO_VENDOR_USERNAME = 'saxosport';
- public $EMAG_RO_VENDOR_PASSWORD = 'saxosport10';
- public $EMAG_RO_API_URL = 'https://marketplace.emag.ro/api-3';
- /**
- * @see ObjectModel::$definition
- */
- /**
- * @todo
- * Kato napravq vryzkata i izprashtaneto, da napravq i tabica, koqto da pazi rezultatite
- */
- /* public $definition = array(
- 'table' => 'emagmp_api_calls',
- 'primary' => 'id_emagmp_api_call',
- 'fields' => array(
- 'date_created' => array('type' => 'string'),
- 'resource' => array('type' => 'string'),
- 'action' => array('type' => 'string'),
- 'last_definition' => array('type' => 'string'),
- 'message_out' => array('type' => 'string'),
- 'message_in' => array('type' => 'string'),
- 'status' => array('type' => 'string', 'default' => 'pending'),
- 'date_sent' => array('type' => 'string'),
- 'id_order' => array('type' => 'integer')
- )
- );*/
- public function __construct() {
- // $this->config = $registry->get('config');
- // $this->db = $registry->get('db');
- // $this->request = $registry->get('request');
- // $this->log = $registry->get('log');
- // $this->registry = $registry;
- // require("/home/new/public_html/admin/config.php");
- $this->initConnection();
- }
- public function initConnection($country){
- $this->country = $country;
- if($country !== 'ro') {
- $this->emag_mp_api_url = $this->EMAG_BG_API_URL;
- $this->emag_mp_vendorcode = $this->EMAG_BG_VENDOR_CODE;
- $this->emag_mp_vendorusername = $this->EMAG_BG_VENDOR_USERNAME;
- $this->emag_mp_vendorpassword = $this->EMAG_BG_VENDOR_PASSWORD;
- } elseif($country !== 'bg') {
- $this->emag_mp_api_url = $this->EMAG_RO_API_URL;
- $this->emag_mp_vendorcode = $this->EMAG_RO_VENDOR_CODE;
- $this->emag_mp_vendorusername = $this->EMAG_RO_VENDOR_USERNAME;
- $this->emag_mp_vendorpassword = $this->EMAG_RO_VENDOR_PASSWORD;
- } else {
- return "Can not initialize bg or ro parameters! ";
- }
- // require("/home/new/public_html/admin/config.php");
- }
- public function execute() {
- $debug_info = array(
- 'site' => HTTP_SERVER,
- 'platform' => 'OpenCart',
- 'version' => VERSION,
- 'extension_version' => $this->module_version,
- 'others' => ''
- );
- $hash = sha1(http_build_query($this->data) . sha1($this->emag_mp_vendorpassword));
- $requestData = array(
- 'code' => $this->emag_mp_vendorcode,
- 'username' => $this->emag_mp_vendorusername,
- 'data' => $this->data,
- 'hash' => $hash,
- 'debug_info' => $debug_info
- );
- $ch = curl_init();
- $url = $this->emag_mp_api_url.'/'.$this->resource.'/'.$this->action;
- if ($this->resource == 'order' && $this->action == 'acknowledge')
- $url .= '/'.$this->data['id'];
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_HEADER, false);
- //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 60);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
- $fp = fopen(DIR_LOGS.'emag_mp_call_result.txt', 'a');
- /*curl_setopt($ch, CURLOPT_VERBOSE, true);
- curl_setopt($ch, CURLOPT_STDERR, $fp);*/
- $result = curl_exec($ch);
- $this->date_sent = date("Y-m-d H:i:s");
- /*ob_start();
- echo "\n\n---------------------------------------------------------------------------\n";
- print_r($this->data);
- print_r(curl_getinfo($ch, CURLINFO_HEADER_OUT));
- fwrite($fp, ob_get_contents());
- ob_end_clean();*/
- //fwrite($fp, "\n\n\n".$result);
- if (curl_errno($ch))
- {
- $this->message_in = curl_error($ch);
- $this->status = 'error';
- }
- else
- {
- $this->message_in = $result;
- $this->message_in_json = json_decode($result);
- if (is_object($this->message_in_json) && $this->message_in_json->isError === false)
- {
- $this->status = 'success';
- }
- else
- {
- $this->status = 'error';
- }
- }
- curl_close($ch);
- fclose($fp);
- // save last sent data for products and orders
- /* if ($this->status == 'success' && $this->action == 'save')
- {
- switch ($this->resource)
- {
- case 'product_offer':
- $definition_table_name = 'emag_mp_product_combinations';
- $definition_table_primary_field = 'combination_id';
- break;
- case 'order':
- $definition_table_name = 'emag_mp_order_history';
- $definition_table_primary_field = 'emag_order_id';
- break;
- }
- if ($definition_table_name && $definition_table_primary_field)
- {
- $this->db->query('
- UPDATE `'.DB_PREFIX.$definition_table_name.'` SET
- last_definition = \''.$this->db->escape($this->last_definition).'\'
- WHERE '.$definition_table_primary_field.' = '.(int)$this->data[0]['id'].'
- ');
- }
- }*/
- // save last eMAG order definition
- /* if ($this->status == 'success' && $this->action == 'read' && $this->resource == 'order' && isset($this->data['id']))
- {
- $definition_table_name = 'emagmp_order_history';
- $definition_table_primary_field = 'emag_order_id';
- $this->db->query('
- UPDATE `'.DB_PREFIX.$definition_table_name.'` SET
- emag_definition = \''.$this->db->escape(serialize($this->message_in_json->results[0])).'\'
- WHERE '.$definition_table_primary_field.' = '.(int)$this->data['id'].'
- ');
- }*/
- }
- public function save()
- {
- $fp = fopen(DIR_LOGS.'emag_mp_call_queue.txt', 'a');
- ob_start();
- echo "\n\n---------------------------------------------------------------------------\n";
- print_r($this->data);
- echo $this->resource.'/'.$this->action."\n";
- //fwrite($fp, ob_get_contents());
- ob_end_clean();
- fclose($fp);
- $this->message_out = serialize($this->data);
- $sql_action = "INSERT INTO";
- $sql_condition = "";
- if ($this->id)
- {
- $sql_action = "UPDATE";
- $sql_condition = "WHERE ".$this->definition['primary']." = ".(int)$this->id;
- }
- $sql_fields = "";
- foreach ($this->definition['fields'] as $field_name => $field_definition)
- {
- if ($sql_fields)
- $sql_fields .= ", ";
- switch ($field_definition['type'])
- {
- case 'integer':
- $value = (int)$this->{$field_name};
- break;
- case 'string':
- $value = "'".$this->db->escape($this->{$field_name})."'";
- break;
- }
- $sql_fields .= "$field_name = $value";
- }
- $this->db->query("
- $sql_action ".DB_PREFIX.$this->definition['table']." SET
- $sql_fields
- $sql_condition
- ");
- $this->id = $this->db->getLastId();
- $this->{$this->definition['primary']} = $this->id;
- return true;
- }
- /**
- * Vryshta pozvolenite harakteristiki za syotvetnata kategoriq
- */
- /**
- * Vryshta naj - golqmoto id +1
- */
- function product_get_max_id($id) {
- $product_max_ext_id = $db->query("SELECT MAX(vendor_ext_id) as last_vendor_id FROM product");
- $product_option_value_max_ext_id = $db->query("SELECT MAX(vendor_ext_id) as last_vendor_id FROM product_option_value");
- if ($product_max_ext_id['last_vendor_id'] > $product_option_value_max_ext_id['last_vendor_id']) {
- return $product_max_ext_id['MAX(vendor_ext_id)'] + 1;
- }
- return $product_option_value_max_ext_id['MAX(vendor_ext_id)'] + 1;
- }
- function generate_product_vendor_ext_id (){
- $select_product_zero_ids = $db->query("SELECT product_id, vendor_ext_id FROM product WHERE vendor_ext_id = 0");
- foreach ($select_product_zero_ids as $select_product_zero_id) {
- $product = product_get_max_id($id);
- $db->query("UPDATE product SET vendor_ext_id = '". $product ."' WHERE (product_id = '". $select_product_zero_id['product_id'] ."') ");
- echo "Сетнато vendor_ext_id - " . $product . " За продукт с Код - " . $select_product_zero_id['product_id'] . "<br />";
- }
- $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");
- foreach ($select_product_option_value_zero_ids as $select_product_option_value_zero_id)
- {
- $product = product_get_max_id($id);
- $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'] ."') ");
- echo "Сетнато vendor_ext_id - " . $product . " За вариант на продукт с Код - " . $select_product_option_value_zero_id['product_option_value_id'] . "<br />";
- }
- // echo " Max Vendor Id is - " . (product_get_max_id($id)) . PHP_EOL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement