public function addOrder(ShopgateOrder $order) { $this->log("PS start add_order", ShopgateLogger::LOGTYPE_DEBUG); $shopgateOrder = PSShopgateOrder::instanceByOrderNumber($order->getOrderNumber()); if ($shopgateOrder->id) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DUPLICATE_ORDER, 'external_order_id: ' . $shopgateOrder->id_order, true); } $comments = array(); // generate products array $products = $this->insertOrderItems($order); //Get or create customer $id_customer = Customer::customerExists($order->getMail(), true, false); $customer = new Customer($id_customer ? $id_customer : (int) $order->getExternalCustomerId()); if (!$customer->id) { $customer = $this->createCustomer($customer, $order); } // prepare addresses: company has to be shorten. add mobile phone / telephone $this->prepareAddresses($order); //Get invoice and delivery addresses $invoiceAddress = $this->getPSAddress($customer, $order->getInvoiceAddress()); $deliveryAddress = $order->getInvoiceAddress() == $order->getDeliveryAddress() ? $invoiceAddress : $this->getPSAddress($customer, $order->getDeliveryAddress()); //Creating currency $this->log("PS setting currency", ShopgateLogger::LOGTYPE_DEBUG); $id_currency = $order->getCurrency() ? Currency::getIdByIsoCode($order->getCurrency()) : $this->id_currency; $currency = new Currency($id_currency ? $id_currency : $this->id_currency); //Creating new cart $this->log("PS set cart variables", ShopgateLogger::LOGTYPE_DEBUG); $cart = new Cart(); $cart->id_lang = $this->id_lang; $cart->id_currency = $currency->id; $cart->id_address_delivery = $deliveryAddress->id; $cart->id_address_invoice = $invoiceAddress->id; $cart->id_customer = $customer->id; if (version_compare(_PS_VERSION_, '1.4.1.0', '>=')) { // id_guest is a connection to a ps_guest entry which includes screen width etc. // is_guest field only exists in Prestashop 1.4.1.0 and higher $cart->id_guest = $customer->is_guest; } $cart->recyclable = 0; $cart->gift = 0; $cart->id_carrier = (int) Configuration::get('SHOPGATE_CARRIER_ID'); $cart->secure_key = $customer->secure_key; $this->log("PS try to create cart", ShopgateLogger::LOGTYPE_DEBUG); if (!$cart->add()) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DATABASE_ERROR, 'Unable to create cart', true); } //Adding items to cart $this->log("PS adding items to cart", ShopgateLogger::LOGTYPE_DEBUG); foreach ($products as $p) { $this->log("PS cart updateQty product id: " . $p['id_product'], ShopgateLogger::LOGTYPE_DEBUG); $this->log("PS cart updateQty product quantity: " . $p['quantity'], ShopgateLogger::LOGTYPE_DEBUG); $this->log("PS cart updateQty product quantity_difference: " . $p['quantity_difference'], ShopgateLogger::LOGTYPE_DEBUG); $this->log("PS cart updateQty product id_product_attribute: " . $p['id_product_attribute'], ShopgateLogger::LOGTYPE_DEBUG); $this->log("PS cart updateQty product delivery address: " . $deliveryAddress->id, ShopgateLogger::LOGTYPE_DEBUG); //TODO deal with customizations $id_customization = false; if ($p['quantity'] - $p['quantity_difference'] > 0) { // only if the result of $p['quantity'] - $p['quantity_difference'] is higher then 0 $cart->updateQty($p['quantity'] - $p['quantity_difference'], $p['id_product'], $p['id_product_attribute'], $id_customization, 'up', $deliveryAddress->id); } if ($p['quantity_difference'] > 0) { $this->log("PS try to add cart message ", ShopgateLogger::LOGTYPE_DEBUG); $message = new Message(); $message->id_cart = $cart->id; $message->private = 1; $message->message = 'Warning, wanted quantity for product "' . $p['name'] . '" was ' . $p['quantity'] . ' unit(s), however, the amount in stock is ' . $p['quantity_in_stock'] . ' unit(s). Only ' . $p['quantity_in_stock'] . ' unit(s) were added to the order'; $message->save(); } } $id_order_state = 0; $shopgate = new Shopgate(); $payment_name = $shopgate->getTranslation('Mobile Payment'); $this->log("PS map payment method", ShopgateLogger::LOGTYPE_DEBUG); if (!$order->getIsShippingBlocked()) { $id_order_state = $this->getOrderStateId('PS_OS_PREPARATION'); switch ($order->getPaymentMethod()) { case 'SHOPGATE': $payment_name = $shopgate->getTranslation('Shopgate'); break; case 'PREPAY': $payment_name = $shopgate->getTranslation('Bankwire'); $id_order_state = $this->getOrderStateId('PS_OS_BANKWIRE'); break; case 'COD': $payment_name = $shopgate->getTranslation('Cash on Delivery'); break; case 'PAYPAL': $payment_name = $shopgate->getTranslation('PayPal'); break; default: break; } } else { $id_order_state = $this->getOrderStateId('PS_OS_SHOPGATE'); switch ($order->getPaymentMethod()) { case 'SHOPGATE': $payment_name = $shopgate->getTranslation('Shopgate'); break; case 'PREPAY': $payment_name = $shopgate->getTranslation('Bankwire'); break; case 'COD': $payment_name = $shopgate->getTranslation('Cash on Delivery'); break; case 'PAYPAL': $id_order_state = $this->getOrderStateId('PS_OS_PAYPAL'); $payment_name = $shopgate->getTranslation('PayPal'); break; default: $id_order_state = $this->getOrderStateId('PS_OS_SHOPGATE'); break; } } $shippingCosts = $order->getAmountShipping() + $order->getAmountShopPayment(); //Creates shopgate order record and save shipping cost for future use $this->log("PS set PSShopgateOrder object variables", ShopgateLogger::LOGTYPE_DEBUG); $shopgateOrder = new PSShopgateOrder(); $shopgateOrder->order_number = $order->getOrderNumber(); $shopgateOrder->shipping_cost = $shippingCosts; $shopgateOrder->shipping_service = Configuration::get('SHOPGATE_SHIPPING_SERVICE'); $shopgateOrder->id_cart = $cart->id; $shopgateOrder->shop_number = $this->config->getShopNumber(); $shopgateOrder->comments = $this->jsonEncode($comments); if (version_compare(_PS_VERSION_, '1.4.0.2', '<')) { $this->log("PS lower 1.4.0.2: ", ShopgateLogger::LOGTYPE_DEBUG); // Fix: sets in database ps_delivery all zones of passed shippingCosts $this->setShippingCosts(0); } $this->log("PS try creating PSShopgateOrder object", ShopgateLogger::LOGTYPE_DEBUG); if (!$shopgateOrder->add()) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DATABASE_ERROR, 'Unable to create shopgate order', true); } //PS 1.5 compatibility if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) { $this->log("PS 1.5.x.x: set cart context", ShopgateLogger::LOGTYPE_DEBUG); $this->context = Context::getContext(); $this->context->cart = $cart; $this->log("PS 1.5.x.x: \$cart->setDeliveryOption(array(\$cart->id_address_delivery => \$cart->id_carrier.','))\n\n==============", ShopgateLogger::LOGTYPE_DEBUG); $cart->setDeliveryOption(array($cart->id_address_delivery => $cart->id_carrier . ',')); $this->log("PS 1.5.x.x: \$cart->update()", ShopgateLogger::LOGTYPE_DEBUG); $cart->update(); $cart->id_carrier = (int) Configuration::get('SHOPGATE_CARRIER_ID'); } $amountPaid = $order->getAmountComplete(); if (version_compare(_PS_VERSION_, '1.4.0.2', '<')) { // substract the shipping costs. $amountPaid -= $shippingCosts; } $this->log("\$shopgate->validateOrder(\$cart->id, \$id_order_state, \$amountPaid, \$payment_name, NULL, array(), NULL, false, \$cart->secure_key", ShopgateLogger::LOGTYPE_DEBUG); $this->log("\$cart->id = " . var_export($cart->id, true) . "\n\$id_order_state = " . var_export($id_order_state, true) . "\n\$amountPaid = " . var_export($amountPaid, true) . "\n\$payment_name = " . var_export($payment_name, true) . "\n\$cart->secure_key" . var_export($cart->secure_key, true) . "\n==============", ShopgateLogger::LOGTYPE_DEBUG); try { $shopgate->validateOrder($cart->id, $id_order_state, $amountPaid, $payment_name, NULL, array(), NULL, false, $cart->secure_key); } catch (Swift_Message_MimeException $ex) { $this->log("\$shopgate->validateOrder(\$cart->id, \$id_order_state, \$amountPaid, \$payment_name, NULL, array(), NULL, false, \$cart->secure_key) FAILED with Swift_Message_MimeException", ShopgateLogger::LOGTYPE_ERROR); // catch Exception if there is a problem with sending mails } if (version_compare(_PS_VERSION_, '1.4.0.2', '<') && (int) $shopgate->currentOrder > 0) { $this->log("PS < 1.4.0.2: update shipping and payment cost", ShopgateLogger::LOGTYPE_DEBUG); // in versions below 1.4.0.2 the shipping and payment costs must be updated after the order $updateShopgateOrder = new Order($shopgate->currentOrder); $updateShopgateOrder->total_paid = $order->getAmountComplete(); $updateShopgateOrder->total_paid_real = $order->getAmountComplete(); $updateShopgateOrder->total_products_wt = $order->getAmountItems(); $updateShopgateOrder->total_shipping = $order->getAmountShipping() + $order->getAmountShopPayment(); $updateShopgateOrder->update(); } if ((int) $shopgate->currentOrder > 0) { $this->log("\$shopgateOrder->update()", ShopgateLogger::LOGTYPE_DEBUG); $shopgateOrder->id_order = $shopgate->currentOrder; $shopgateOrder->update(); return array('external_order_id' => $shopgate->currentOrder, 'external_order_number' => $shopgate->currentOrder); } else { $this->log("\$shopgateOrder->delete()", ShopgateLogger::LOGTYPE_DEBUG); $shopgateOrder->delete(); throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DATABASE_ERROR, 'Unable to create order', true); } }