Example #1
0
 public function hookbackOfficeTop($params)
 {
     // Check if the module is configured
     if (!Configuration::get('EBAY_PAYPAL_EMAIL')) {
         return false;
     }
     // If no update yet
     if (!Configuration::get('EBAY_ORDER_LAST_UPDATE')) {
         Configuration::updateValue('EBAY_ORDER_LAST_UPDATE', date('Y-m-d') . 'T' . date('H:i:s') . '.000Z');
     }
     // init Var
     $dateNew = date('Y-m-d') . 'T' . date('H:i:s') . '.000Z';
     if (Configuration::get('EBAY_ORDER_LAST_UPDATE') < date('Y-m-d', strtotime('-30 minutes')) . 'T' . date('H:i:s', strtotime('-30 minutes')) . '.000Z') {
         // Lock
         Configuration::updateValue('EBAY_ORDER_LAST_UPDATE', $dateNew);
         // eBay Request
         $ebay = new eBayRequest();
         $page = 1;
         $orderList = array();
         $orderCount = 0;
         $orderCountTmp = 100;
         while ($orderCountTmp == 100 && $page < 10) {
             $orderListTmp = $ebay->getOrders(date('Y-m-d', strtotime('-30 days')) . 'T' . date('H:i:s', strtotime('-30 days')) . '.000Z', $dateNew, $page);
             $orderCountTmp = count($orderListTmp);
             $orderList = array_merge((array) $orderList, (array) $orderListTmp);
             $orderCount += $orderCountTmp;
             $page++;
         }
         if ($orderList) {
             foreach ($orderList as $korder => $order) {
                 if ($order['status'] == 'Complete' && $order['amount'] > 0.1 && isset($order['product_list']) && count($order['product_list'])) {
                     if (!Db::getInstance()->getValue('SELECT `id_ebay_order` FROM `' . _DB_PREFIX_ . 'ebay_order` WHERE `id_order_ref` = \'' . pSQL($order['id_order_ref']) . '\'')) {
                         $id_customer = (int) Db::getInstance()->getValue('SELECT `id_customer` FROM `' . _DB_PREFIX_ . 'customer` WHERE `active` = 1 AND `email` = \'' . pSQL($order['email']) . '\' AND `deleted` = 0' . (substr(_PS_VERSION_, 0, 3) == '1.3' ? '' : ' AND `is_guest` = 0'));
                         // Check for empty name
                         $order['firstname'] = trim($order['firstname']);
                         $order['familyname'] = trim($order['familyname']);
                         if (empty($order['familyname'])) {
                             $order['familyname'] = $order['firstname'];
                         }
                         if (empty($order['firstname'])) {
                             $order['firstname'] = $order['familyname'];
                         }
                         if (empty($order['phone']) || !Validate::isPhoneNumber($order['phone'])) {
                             $order['phone'] = '0100000000';
                         }
                         if (Validate::isEmail($order['email']) && !empty($order['firstname']) && !empty($order['familyname'])) {
                             // Add customer if he doesn't exist
                             if ($id_customer < 1) {
                                 $customer = new Customer();
                                 $customer->id_gender = 9;
                                 $customer->id_default_group = 1;
                                 $customer->secure_key = md5(uniqid(rand(), true));
                                 $customer->email = $order['email'];
                                 $customer->passwd = md5(pSQL(_COOKIE_KEY_ . rand()));
                                 $customer->last_passwd_gen = pSQL(date('Y-m-d H:i:s'));
                                 $customer->newsletter = 0;
                                 $customer->lastname = pSQL($order['familyname']);
                                 $customer->firstname = pSQL($order['firstname']);
                                 $customer->active = 1;
                                 $customer->add();
                                 $id_customer = $customer->id;
                             }
                             // Search if address exists
                             $id_address = (int) Db::getInstance()->getValue('SELECT `id_address` FROM `' . _DB_PREFIX_ . 'address` WHERE `id_customer` = ' . (int) $id_customer . ' AND `alias` = \'eBay\'');
                             if ($id_address > 0) {
                                 $address = new Address((int) $id_address);
                             } else {
                                 $address = new Address();
                                 $address->id_customer = (int) $id_customer;
                             }
                             $address->id_country = (int) Country::getByIso($order['country_iso_code']);
                             $address->alias = 'eBay';
                             $address->lastname = pSQL($order['familyname']);
                             $address->firstname = pSQL($order['firstname']);
                             $address->address1 = pSQL($order['address1']);
                             $address->address2 = pSQL($order['address2']);
                             $address->postcode = pSQL($order['postalcode']);
                             $address->city = pSQL($order['city']);
                             $address->phone = pSQL($order['phone']);
                             $address->active = 1;
                             if ($id_address > 0 && Validate::isLoadedObject($address)) {
                                 $address->update();
                             } else {
                                 $address->add();
                             }
                             $id_address = $address->id;
                             $flag = 1;
                             foreach ($order['product_list'] as $product) {
                                 if ((int) $product['id_product'] < 1 || !Db::getInstance()->getValue('SELECT `id_product` FROM `' . _DB_PREFIX_ . 'product` WHERE `id_product` = ' . (int) $product['id_product'])) {
                                     $flag = 0;
                                 }
                                 if (isset($product['id_product_attribute']) && $product['id_product_attribute'] > 0 && !Db::getInstance()->getValue('SELECT `id_product_attribute` FROM `' . _DB_PREFIX_ . 'product_attribute` WHERE `id_product` = ' . (int) $product['id_product'] . ' AND `id_product_attribute` = ' . (int) $product['id_product_attribute'])) {
                                     $flag = 0;
                                 }
                             }
                             if ($flag == 1) {
                                 $cartNbProducts = 0;
                                 $cartAdd = new Cart();
                                 $cartAdd->id_customer = $id_customer;
                                 $cartAdd->id_address_invoice = $id_address;
                                 $cartAdd->id_address_delivery = $id_address;
                                 $cartAdd->id_carrier = 1;
                                 $cartAdd->id_lang = $this->id_lang;
                                 $cartAdd->id_currency = Currency::getIdByIsoCode('EUR');
                                 $cartAdd->recyclable = 0;
                                 $cartAdd->gift = 0;
                                 $cartAdd->add();
                                 foreach ($order['product_list'] as $product) {
                                     if ($cartAdd->updateQty((int) $product['quantity'], (int) $product['id_product'], isset($product['id_product_attribute']) && $product['id_product_attribute'] > 0 ? $product['id_product_attribute'] : NULL)) {
                                         $cartNbProducts++;
                                     }
                                 }
                                 $cartAdd->update();
                                 // Check number of products in the cart
                                 if ($cartNbProducts > 0) {
                                     // Fix on sending e-mail
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => 'NOSEND-EBAY'), 'UPDATE', '`id_customer` = ' . (int) $id_customer);
                                     $customerClear = new Customer();
                                     if (method_exists($customerClear, 'clearCache')) {
                                         $customerClear->clearCache(true);
                                     }
                                     // Validate order
                                     $paiement = new eBayPayment();
                                     $paiement->validateOrder(intval($cartAdd->id), Configuration::get('PS_OS_PAYMENT'), floatval($cartAdd->getOrderTotal(true, 3)), 'eBay ' . $order['payment_method'] . ' ' . $order['id_order_seller'], NULL, array(), intval($cartAdd->id_currency));
                                     $id_order = $paiement->currentOrder;
                                     // Fix on date
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'orders', array('date_add' => pSQL($order['date_add'])), 'UPDATE', '`id_order` = ' . (int) $id_order);
                                     // Fix on sending e-mail
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => pSQL($order['email'])), 'UPDATE', '`id_customer` = ' . (int) $id_customer);
                                     // Update price (because of possibility of price impact)
                                     $updateOrder = array('total_paid' => floatval($order['amount']), 'total_paid_real' => floatval($order['amount']), 'total_products' => floatval($order['amount']), 'total_products_wt' => floatval($order['amount']), 'total_shipping' => floatval($order['shippingServiceCost']));
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'orders', $updateOrder, 'UPDATE', '`id_order` = ' . (int) $id_order);
                                     foreach ($order['product_list'] as $product) {
                                         Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_detail', array('product_price' => floatval($product['price']), 'tax_rate' => 0, 'reduction_percent' => 0), 'UPDATE', '`id_order` = ' . (int) $id_order . ' AND `product_id` = ' . (int) $product['id_product'] . ' AND `product_attribute_id` = ' . (int) $product['id_product_attribute']);
                                     }
                                     // Register the ebay order ref
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'ebay_order', array('id_order_ref' => pSQL($order['id_order_ref']), 'id_order' => (int) $id_order), 'INSERT');
                                 } else {
                                     $cartAdd->delete();
                                     $orderList[$korder]['errors'][] = $this->l('Could not add product to cart (maybe your stock quantity is 0)');
                                 }
                             } else {
                                 $orderList[$korder]['errors'][] = $this->l('Could not found products in database');
                             }
                         } else {
                             $orderList[$korder]['errors'][] = $this->l('Invalid e-mail');
                         }
                     } else {
                         $orderList[$korder]['errors'][] = $this->l('Order already imported');
                     }
                 } else {
                     $orderList[$korder]['errors'][] = $this->l('Status not complete or amount less than 0.1 or no product matching');
                 }
             }
             file_put_contents(dirname(__FILE__) . '/log/orders.php', "<?php\n\n" . '$dateLastImport = ' . "'" . date('d/m/Y H:i:s') . "';\n\n" . '$orderList = ' . var_export($orderList, true) . ";\n\n");
         }
     }
 }
Example #2
0
 public function hookheader($params)
 {
     //Change context Shop to be default
     if ($this->isVersionOneDotFive() && Shop::isFeatureActive()) {
         $oldContextShop = $this->getContextShop();
         $this->setContextShop();
     }
     //End of change
     // Check if the module is configured
     if (!Configuration::get('EBAY_PAYPAL_EMAIL')) {
         return false;
     }
     // Fix hook update product attribute
     $this->hookupdateProductAttributeEbay();
     // init date to check from
     if (Configuration::get('EBAY_INSTALL_DATE') < date('Y-m-d', strtotime('-30 days')) . 'T' . date('H:i:s', strtotime('-30 days'))) {
         //If it is more than 30 days that we installed the module
         $dateToCheckFrom = Configuration::get('EBAY_ORDER_LAST_UPDATE');
         $dateToCheckFromArray = explode('T', $dateToCheckFrom);
         $dateToCheckFrom = date("Y-m-d", strtotime($dateToCheckFromArray[0] . " -30 day"));
         $dateToCheckFrom .= 'T' . $dateToCheckFromArray[1];
     } else {
         //If it is less than 30 days that we installed the module
         $dateToCheckFrom = Configuration::get('EBAY_INSTALL_DATE');
         $dateToCheckFromArray = explode('T', $dateToCheckFrom);
         $dateToCheckFrom = date("Y-m-d", strtotime($dateToCheckFromArray[0] . " -1 day"));
         $dateToCheckFrom .= 'T' . $dateToCheckFromArray[1];
     }
     if (Configuration::get('EBAY_ORDER_LAST_UPDATE') < date('Y-m-d', strtotime('-30 minutes')) . 'T' . date('H:i:s', strtotime('-30 minutes')) . '.000Z') {
         $dateNew = date('Y-m-d') . 'T' . date('H:i:s') . '.000Z';
         $this->setConfiguration('EBAY_ORDER_LAST_UPDATE', $dateNew);
         // eBay Request
         $ebay = new eBayRequest();
         $page = 1;
         $orderList = array();
         $orderCount = 0;
         $orderCountTmp = 100;
         while ($orderCountTmp == 100 && $page < 10) {
             $orderListTmp = $ebay->getOrders($dateToCheckFrom, $dateNew, $page);
             $orderCountTmp = count($orderListTmp);
             $orderList = array_merge((array) $orderList, (array) $orderListTmp);
             $orderCount += $orderCountTmp;
             $page++;
         }
         // Lock
         if ($orderList) {
             foreach ($orderList as $korder => $order) {
                 if ($order['status'] == 'Complete' && $order['amount'] > 0.1 && isset($order['product_list']) && count($order['product_list'])) {
                     if (!Db::getInstance()->getValue('SELECT `id_ebay_order` FROM `' . _DB_PREFIX_ . 'ebay_order` WHERE `id_order_ref` = \'' . pSQL($order['id_order_ref']) . '\'')) {
                         // Check for empty name
                         $order['firstname'] = trim($order['firstname']);
                         $order['familyname'] = trim($order['familyname']);
                         if (empty($order['familyname'])) {
                             $order['familyname'] = $order['firstname'];
                         }
                         if (empty($order['firstname'])) {
                             $order['firstname'] = $order['familyname'];
                         }
                         if (empty($order['phone']) || !Validate::isPhoneNumber($order['phone'])) {
                             $order['phone'] = '0100000000';
                         }
                         if (Validate::isEmail($order['email']) && !empty($order['firstname']) && !empty($order['familyname'])) {
                             // Getting the customer
                             $id_customer = (int) Db::getInstance()->getValue('SELECT `id_customer` FROM `' . _DB_PREFIX_ . 'customer` WHERE `active` = 1 AND `email` = \'' . pSQL($order['email']) . '\' AND `deleted` = 0' . (substr(_PS_VERSION_, 0, 3) == '1.3' ? '' : ' AND `is_guest` = 0'));
                             // Add customer if he doesn't exist
                             if ($id_customer < 1) {
                                 $customer = new Customer();
                                 $customer->id_gender = 0;
                                 $customer->id_default_group = 1;
                                 $customer->secure_key = md5(uniqid(rand(), true));
                                 $customer->email = $order['email'];
                                 $customer->passwd = md5(pSQL(_COOKIE_KEY_ . rand()));
                                 $customer->last_passwd_gen = pSQL(date('Y-m-d H:i:s'));
                                 $customer->newsletter = 0;
                                 $customer->lastname = pSQL($order['familyname']);
                                 $customer->firstname = pSQL($order['firstname']);
                                 $customer->active = 1;
                                 $customer->add();
                                 $id_customer = $customer->id;
                             }
                             // Search if address exists
                             $id_address = (int) Db::getInstance()->getValue('SELECT `id_address` FROM `' . _DB_PREFIX_ . 'address` WHERE `id_customer` = ' . (int) $id_customer . ' AND `alias` = \'eBay\'');
                             if ($id_address > 0) {
                                 $address = new Address((int) $id_address);
                             } else {
                                 $address = new Address();
                                 $address->id_customer = (int) $id_customer;
                             }
                             $address->id_country = (int) Country::getByIso($order['country_iso_code']);
                             $address->alias = 'eBay';
                             $address->lastname = pSQL($order['familyname']);
                             $address->firstname = pSQL($order['firstname']);
                             $address->address1 = pSQL($order['address1']);
                             $address->address2 = pSQL($order['address2']);
                             $address->postcode = pSQL($order['postalcode']);
                             $address->city = pSQL($order['city']);
                             $address->phone = pSQL($order['phone']);
                             $address->active = 1;
                             if ($id_address > 0 && Validate::isLoadedObject($address)) {
                                 $address->update();
                             } else {
                                 $address->add();
                             }
                             $id_address = $address->id;
                             $flag = 1;
                             foreach ($order['product_list'] as $product) {
                                 if ((int) $product['id_product'] < 1 || !Db::getInstance()->getValue('SELECT `id_product` FROM `' . _DB_PREFIX_ . 'product` WHERE `id_product` = ' . (int) $product['id_product'])) {
                                     $flag = 0;
                                 }
                                 if (isset($product['id_product_attribute']) && $product['id_product_attribute'] > 0 && !Db::getInstance()->getValue('SELECT `id_product_attribute` FROM `' . _DB_PREFIX_ . 'product_attribute` WHERE `id_product` = ' . (int) $product['id_product'] . ' AND `id_product_attribute` = ' . (int) $product['id_product_attribute'])) {
                                     $flag = 0;
                                 }
                             }
                             if ($flag == 1) {
                                 //Create a Cart for the order
                                 $cartNbProducts = 0;
                                 $cartAdd = new Cart();
                                 Context::getContext()->customer = new Customer($id_customer);
                                 $cartAdd->id_customer = $id_customer;
                                 $cartAdd->id_address_invoice = $id_address;
                                 $cartAdd->id_address_delivery = $id_address;
                                 $cartAdd->id_carrier = 0;
                                 $cartAdd->id_lang = $this->id_lang;
                                 $cartAdd->id_currency = Currency::getIdByIsoCode('EUR');
                                 $cartAdd->recyclable = 0;
                                 $cartAdd->gift = 0;
                                 $cartAdd->add();
                                 $id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
                                 foreach ($order['product_list'] as $product) {
                                     $prod = new Product($product['id_product'], false, $id_lang);
                                     // Qty of product or attribute
                                     if (isset($product['id_product_attribute']) && !empty($product['id_product_attribute'])) {
                                         $minimalQty = (int) Attribute::getAttributeMinimalQty($product['id_product_attribute']);
                                     } else {
                                         $minimalQty = $prod->minimal_quantity;
                                     }
                                     if ($product['quantity'] >= $minimalQty) {
                                         if ($this->isVersionOneDotFive()) {
                                             $update = $cartAdd->updateQty((int) $product['quantity'], (int) $product['id_product'], isset($product['id_product_attribute']) && $product['id_product_attribute'] > 0 ? $product['id_product_attribute'] : NULL, false, 'up', 0, new Shop(Configuration::get('PS_SHOP_DEFAULT')));
                                             if ($update === TRUE) {
                                                 $cartNbProducts++;
                                             }
                                         } elseif ($cartAdd->updateQty((int) $product['quantity'], (int) $product['id_product'], isset($product['id_product_attribute']) && $product['id_product_attribute'] > 0 ? $product['id_product_attribute'] : NULL)) {
                                             $cartNbProducts++;
                                         }
                                     } else {
                                         $templateVars = array('{name_product}' => $prod->name, '{min_qty}' => $minimalQty, '{cart_qty}' => $product['quantity']);
                                         Mail::Send((int) Configuration::get('PS_LANG_DEFAULT'), 'alertEbay', Mail::l('Product quantity', $id_lang), $templateVars, strval(Configuration::get('PS_SHOP_EMAIL')), NULL, strval(Configuration::get('PS_SHOP_EMAIL')), strval(Configuration::get('PS_SHOP_NAME')), NULL, NULL, dirname(__FILE__) . '/mails/');
                                     }
                                 }
                                 $cartAdd->update();
                                 // Check number of products in the cart and check if order has already been taken
                                 if ($cartNbProducts > 0 && !Db::getInstance()->getValue('SELECT `id_ebay_order` FROM `' . _DB_PREFIX_ . 'ebay_order` WHERE `id_order_ref` = \'' . pSQL($order['id_order_ref']) . '\'')) {
                                     // Fix on sending e-mail
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => 'NOSEND-EBAY'), 'UPDATE', '`id_customer` = ' . (int) $id_customer);
                                     $customerClear = new Customer();
                                     if (method_exists($customerClear, 'clearCache')) {
                                         $customerClear->clearCache(true);
                                     }
                                     $paiement = new eBayPayment();
                                     // Validate order
                                     if ($this->isVersionOneDotFive()) {
                                         $customer = new Customer($id_customer);
                                         $paiement->validateOrder(intval($cartAdd->id), Configuration::get('PS_OS_PAYMENT'), floatval($cartAdd->getOrderTotal(true, 3)), 'eBay ' . $order['payment_method'] . ' ' . $order['id_order_seller'], NULL, array(), intval($cartAdd->id_currency), false, $customer->secure_key, new Shop(Configuration::get('PS_SHOP_DEFAULT')));
                                     } else {
                                         $customer = new Customer($id_customer);
                                         $paiement->validateOrder(intval($cartAdd->id), Configuration::get('PS_OS_PAYMENT'), floatval($cartAdd->getOrderTotal(true, 3)), 'eBay ' . $order['payment_method'] . ' ' . $order['id_order_seller'], NULL, array(), intval($cartAdd->id_currency), false, $customer->secure_key);
                                     }
                                     $id_order = $paiement->currentOrder;
                                     // Fix on date
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'orders', array('date_add' => pSQL($order['date_add'])), 'UPDATE', '`id_order` = ' . (int) $id_order);
                                     // Fix on sending e-mail
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => pSQL($order['email'])), 'UPDATE', '`id_customer` = ' . (int) $id_customer);
                                     // Update price (because of possibility of price impact)
                                     foreach ($order['product_list'] as $product) {
                                         $tax_rate = Db::getInstance()->getValue('SELECT `tax_rate` FROM `' . _DB_PREFIX_ . 'order_detail` WHERE `id_order` = ' . (int) $id_order . ' AND `product_id` = ' . (int) $product['id_product'] . ' AND `product_attribute_id` = ' . (int) $product['id_product_attribute']);
                                         Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_detail', array('product_price' => floatval($product['price'] / (1 + $tax_rate / 100)), 'reduction_percent' => 0), 'UPDATE', '`id_order` = ' . (int) $id_order . ' AND `product_id` = ' . (int) $product['id_product'] . ' AND `product_attribute_id` = ' . (int) $product['id_product_attribute']);
                                     }
                                     $updateOrder = array('total_paid' => floatval($order['amount']), 'total_paid_real' => floatval($order['amount']), 'total_products' => floatval(Db::getInstance()->getValue('SELECT SUM(`product_price`) FROM `' . _DB_PREFIX_ . 'order_detail` WHERE `id_order` = ' . (int) $id_order)), 'total_products_wt' => floatval($order['amount'] - $order['shippingServiceCost']), 'total_shipping' => floatval($order['shippingServiceCost']));
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'orders', $updateOrder, 'UPDATE', '`id_order` = ' . (int) $id_order);
                                     // Register the ebay order ref
                                     Db::getInstance()->autoExecute(_DB_PREFIX_ . 'ebay_order', array('id_order_ref' => pSQL($order['id_order_ref']), 'id_order' => (int) $id_order), 'INSERT');
                                     if (!$this->isVersionOneDotFive()) {
                                         //Fix on eBay not updating
                                         $params = array();
                                         foreach ($order['product_list'] as $product) {
                                             $params['product'] = new Product((int) $product['id_product']);
                                             $this->hookaddproduct($params);
                                         }
                                     }
                                 } else {
                                     $cartAdd->delete();
                                     $orderList[$korder]['errors'][] = $this->l('Could not add product to cart (maybe your stock quantity is 0)');
                                 }
                             } else {
                                 $orderList[$korder]['errors'][] = $this->l('Could not found products in database');
                             }
                         } else {
                             $orderList[$korder]['errors'][] = $this->l('Invalid e-mail');
                         }
                     } else {
                         $orderList[$korder]['errors'][] = $this->l('Order already imported');
                     }
                 } else {
                     $orderList[$korder]['errors'][] = $this->l('Status not complete or amount less than 0.1 or no product matching');
                 }
             }
             file_put_contents(dirname(__FILE__) . '/log/orders.php', "<?php\n\n" . '$dateLastImport = ' . "'" . date('d/m/Y H:i:s') . "';\n\n" . '$orderList = ' . var_export($orderList, true) . ";\n\n");
         }
     }
     // Set old Context Shop
     if ($this->isVersionOneDotFive() && Shop::isFeatureActive()) {
         $this->setContextShop($oldContextShop);
     }
 }
Example #3
0
 public function hookbackOfficeTop($params)
 {
     // Check if the module is configured
     if (!Configuration::get('EBAY_PAYPAL_EMAIL')) {
         return false;
     }
     // If no update yet
     if (!Configuration::get('EBAY_ORDER_LAST_UPDATE')) {
         Configuration::updateValue('EBAY_ORDER_LAST_UPDATE', date('Y-m-d') . 'T' . date('H:i:s') . '.000Z');
     }
     // init Var
     $dateNew = date('Y-m-d') . 'T' . date('H:i:s') . '.000Z';
     if (Configuration::get('EBAY_ORDER_LAST_UPDATE') < date('Y-m-d', strtotime('-45 minutes')) . 'T' . date('H:i:s', strtotime('-45 minutes')) . '.000Z') {
         $ebay = new eBayRequest();
         $orderList = $ebay->getOrders(Configuration::get('EBAY_ORDER_LAST_UPDATE'), $dateNew);
         if ($orderList) {
             foreach ($orderList as $order) {
                 if ($order['status'] == 'Complete') {
                     $result = Db::getInstance()->getRow('SELECT `id_customer` FROM `' . _DB_PREFIX_ . 'customer` WHERE `active` = 1 AND `email` = \'' . pSQL($order['email']) . '\' AND `deleted` = 0' . (substr(_PS_VERSION_, 0, 3) == '1.3' ? '' : ' AND `is_guest` = 0'));
                     $id_customer = isset($result['id_customer']) ? $result['id_customer'] : 0;
                     // Add customer if he doesn't exist
                     if ($id_customer < 1) {
                         $customer = new Customer();
                         $customer->id_gender = 9;
                         $customer->id_default_group = 1;
                         $customer->secure_key = md5(uniqid(rand(), true));
                         $customer->email = $order['email'];
                         $customer->passwd = md5(pSQL(_COOKIE_KEY_ . rand()));
                         $customer->last_passwd_gen = pSQL(date('Y-m-d H:i:s'));
                         $customer->newsletter = 0;
                         $customer->lastname = pSQL($order['familyname']);
                         $customer->firstname = pSQL($order['firstname']);
                         $customer->active = 1;
                         $customer->add();
                         $id_customer = $customer->id;
                     }
                     $address = new Address();
                     $address->id_customer = (int) $id_customer;
                     $address->id_country = (int) Country::getByIso($order['country_iso_code']);
                     $address->alias = 'eBay ' . date('Y-m-d H:i:s');
                     $address->lastname = pSQL($order['familyname']);
                     $address->firstname = pSQL($order['firstname']);
                     $address->address1 = pSQL($order['address1']);
                     $address->address2 = pSQL($order['address2']);
                     $address->postcode = pSQL($order['postalcode']);
                     $address->city = pSQL($order['city']);
                     $address->phone = pSQL($order['phone']);
                     $address->active = 1;
                     $address->add();
                     $id_address = $address->id;
                     $flag = 1;
                     foreach ($order['product_list'] as $product) {
                         if ((int) $product['id_product'] < 1 || !Db::getInstance()->getValue('SELECT `id_product` FROM `' . _DB_PREFIX_ . 'product` WHERE `id_product` = ' . (int) $product['id_product'])) {
                             $flag = 0;
                         }
                         if (isset($product['id_product_attribute']) && !Db::getInstance()->getValue('SELECT `id_product_attribute` FROM `' . _DB_PREFIX_ . 'product_attribute` WHERE `id_product` = ' . (int) $product['id_product'] . ' AND `id_product_attribute` = ' . (int) $product['id_product_attribute'])) {
                             $flag = 0;
                         }
                     }
                     if ($flag == 1) {
                         $cartAdd = new Cart();
                         $cartAdd->id_customer = $id_customer;
                         $cartAdd->id_address_invoice = $id_address;
                         $cartAdd->id_address_delivery = $id_address;
                         $cartAdd->id_carrier = 1;
                         $cartAdd->id_lang = $this->id_lang;
                         $cartAdd->id_currency = Currency::getIdByIsoCode('EUR');
                         $cartAdd->add();
                         foreach ($order['product_list'] as $product) {
                             $cartAdd->updateQty((int) $product['quantity'], (int) $product['id_product'], isset($product['id_product_attribute']) ? $product['id_product_attribute'] : NULL);
                         }
                         $cartAdd->update();
                         // Fix on sending e-mail
                         Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => 'NOSEND-EBAY'), 'UPDATE', '`id_customer` = ' . (int) $id_customer);
                         $customerClear = new Customer();
                         if (method_exists($customerClear, 'clearCache')) {
                             $customerClear->clearCache(true);
                         }
                         // Validate order
                         $paiement = new eBayPayment();
                         $paiement->validateOrder(intval($cartAdd->id), _PS_OS_PAYMENT_, floatval($cartAdd->getOrderTotal(true, 3)), 'Paypal eBay', NULL, array(), intval($cartAdd->id_currency));
                         $id_order = $paiement->currentOrder;
                         // Fix on sending e-mail
                         Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => pSQL($order['email'])), 'UPDATE', '`id_customer` = ' . (int) $id_customer);
                         // Update price (because of possibility of price impact)
                         $updateOrder = array('total_paid' => floatval($order['amount']), 'total_paid_real' => floatval($order['amount']), 'total_products' => floatval($order['amount']), 'total_products_wt' => floatval($order['amount']), 'total_shipping' => floatval($order['shippingServiceCost']));
                         Db::getInstance()->autoExecute(_DB_PREFIX_ . 'orders', $updateOrder, 'UPDATE', '`id_order` = ' . (int) $id_order);
                         foreach ($order['product_list'] as $product) {
                             Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_detail', array('product_price' => floatval($product['price']), 'tax_rate' => 0, 'reduction_percent' => 0), 'UPDATE', '`id_order` = ' . (int) $id_order . ' AND `product_id` = ' . (int) $product['id_product'] . ' AND `product_attribute_id` = ' . (int) $product['id_product_attribute']);
                         }
                     }
                 }
             }
         }
         Configuration::updateValue('EBAY_ORDER_LAST_UPDATE', $dateNew);
     }
 }