/**
  * @param $user
  * @param $pass
  * @param ShopgateCustomer $customer
  * @throws ShopgateLibraryException
  */
 public function registerCustomer($user, $pass, ShopgateCustomer $customer)
 {
     if (!Validate::isEmail($user)) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_REGISTER_CUSTOMER_ERROR, 'E-mail Address validation error', true);
     }
     if ($pass && !Validate::isPasswd($pass)) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_REGISTER_CUSTOMER_ERROR, 'Password validation error', true);
     }
     /** @var CustomerCore | Customer $customerModel */
     $customerModel = new Customer();
     if ($customerModel->getByEmail($user)) {
         throw new ShopgateLibraryException(ShopgateLibraryException::REGISTER_USER_ALREADY_EXISTS);
     }
     $customerModel->active = 1;
     $customerModel->lastname = $customer->getLastName();
     $customerModel->firstname = $customer->getFirstName();
     $customerModel->email = $user;
     $customerModel->passwd = Tools::encrypt($pass);
     $customerModel->id_gender = $this->mapGender($customer->getGender());
     $customerModel->birthday = $customer->getBirthday();
     $customerModel->newsletter = $customer->getNewsletterSubscription();
     $shopgateCustomFieldsHelper = new ShopgateCustomFieldsHelper();
     $shopgateCustomFieldsHelper->saveCustomFields($customerModel, $customer->getCustomFields());
     $validateMessage = $customerModel->validateFields(false, true);
     if ($validateMessage !== true) {
         throw new ShopgateLibraryException(ShopgateLibraryException::REGISTER_FAILED_TO_ADD_USER, $validateMessage, true);
     }
     $customerModel->save();
     /**
      * addresses
      */
     foreach ($customer->getAddresses() as $address) {
         $this->createAddress($address, $customerModel);
     }
     return $customerModel->id;
 }
    public function getCustomer($user, $pass)
    {
        $id_customer = (int) Db::getInstance()->getValue('
				SELECT `id_customer`
				FROM `' . _DB_PREFIX_ . 'customer`
				WHERE
				`active` AND
				`email` = \'' . pSQL($user) . '\' AND
				`passwd` = \'' . Tools::encrypt($pass) . '\' AND
				`deleted` = 0
				' . (version_compare(_PS_VERSION_, '1.4.1.0', '>=') ? ' AND `is_guest` = 0' : ''));
        if (!$id_customer) {
            throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_WRONG_USERNAME_OR_PASSWORD, 'Username or password is incorrect');
        }
        $customer = new Customer($id_customer);
        $gender = array(1 => 'm', 2 => 'f', 9 => null);
        $shopgateCustomer = new ShopgateCustomer();
        $shopgateCustomer->setCustomerId($customer->id);
        $shopgateCustomer->setCustomerNumber($customer->id);
        $shopgateCustomer->setCustomerGroup(Db::getInstance()->getValue('SELECT `name` FROM `' . _DB_PREFIX_ . 'group_lang` WHERE `id_group`=\'' . $customer->id_default_group . '\' AND `id_lang`=' . $this->id_lang));
        $shopgateCustomer->setCustomerGroupId($customer->id_default_group);
        $shopgateCustomer->setFirstName($customer->firstname);
        $shopgateCustomer->setLastName($customer->lastname);
        $shopgateCustomer->setGender(isset($gender[$customer->id_gender]) ? $gender[$customer->id_gender] : null);
        $shopgateCustomer->setBirthday($customer->birthday);
        $shopgateCustomer->setMail($customer->email);
        $shopgateCustomer->setNewsletterSubscription($customer->newsletter);
        $addresses = array();
        foreach ($customer->getAddresses($this->id_lang) as $a) {
            $address = new ShopgateAddress();
            $address->setId($a['id_address']);
            $address->setFirstName($a['firstname']);
            $address->setLastName($a['lastname']);
            $address->setCompany($a['company']);
            $address->setStreet1($a['address1']);
            $address->setStreet2($a['address2']);
            $address->setCity($a['city']);
            $address->setZipcode($a['postcode']);
            $address->setCountry($a['country']);
            $address->setState($a['state']);
            $address->setPhone($a['phone']);
            $address->setMobile($a['phone_mobile']);
            array_push($addresses, $address);
        }
        $shopgateCustomer->setAddresses($addresses);
        return $shopgateCustomer;
    }
Example #3
0
 /**
  *
  * Represents the "register_customer" action.
  *
  * @throws ShopgateLibraryException
  * @see http://wiki.shopgate.com/Shopgate_Plugin_API_register_customer
  */
 protected function registerCustomer()
 {
     if (!isset($this->params['user'])) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_USER);
     }
     if (!isset($this->params['pass'])) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_PASS);
     }
     if (!isset($this->params['user_data'])) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_USER_DATA, "missing user_data", true);
     }
     if (!$this->config->getEnableGetCustomer()) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_DISABLED_ACTION, "Action 'get_customer' is not activated but is needed by register_customer", true);
     }
     $user = $this->params['user'];
     $pass = $this->params['pass'];
     $customer = new ShopgateCustomer($this->params['user_data']);
     $userData = $this->params["user_data"];
     if (isset($userData['addresses']) && is_array($userData['addresses'])) {
         $addresses = array();
         foreach ($userData['addresses'] as $address) {
             $addresses[] = new ShopgateAddress($address);
         }
         $customer->setAddresses($addresses);
     }
     $this->plugin->registerCustomer($user, $pass, $customer);
     $newCustomer = $this->plugin->getCustomer($user, $pass);
     $customerData = $newCustomer->toArray();
     $addressList = $customerData['addresses'];
     unset($customerData['addresses']);
     if (empty($this->response)) {
         $this->response = new ShopgatePluginApiResponseAppJson($this->trace_id);
     }
     $this->responseData["user_data"] = $customerData;
     $this->responseData["addresses"] = $addressList;
 }
Example #4
0
 /**
  * @param Mage_Customer_Model_Address_Abstract|Mage_Sales_Model_Abstract $magentoObject
  * @param ShopgateOrder|ShopgateAddress|ShopgateCustomer                 $shopgateObject
  * @return mixed
  */
 public function setCustomFields($magentoObject, $shopgateObject)
 {
     foreach ($shopgateObject->getCustomFields() as $field) {
         $magentoObject->setData($field->getInternalFieldName(), $field->getValue());
     }
     return $magentoObject;
 }
Example #5
0
 public function visitCustomer(ShopgateCustomer $c)
 {
     // get properties
     $properties = $c->buildProperties();
     // iterate the simple variables
     $properties = $this->iterateSimpleProperties($properties);
     // iterate ShopgateAddress objects
     $properties['custom_fields'] = $this->iterateObjectList($properties['custom_fields']);
     $properties['addresses'] = $this->iterateObjectList($properties['addresses']);
     $properties['customer_groups'] = $this->iterateObjectList($properties['customer_groups']);
     // set last value to converted array
     $this->array = $properties;
 }
Example #6
0
 /**
  * add addresses to the customer
  *
  * @param Mage_Customer_Model_Customer $magentoCustomer
  * @param ShopgateCustomer             $shopgateCustomer
  */
 protected function _registerAddCustomerAddresses($magentoCustomer, $shopgateCustomer)
 {
     foreach ($shopgateCustomer->getAddresses() as $shopgateCustomerAddress) {
         $magentoCustomerAddress = $this->getMagentoCustomerAddress($shopgateCustomerAddress);
         $magentoCustomerAddress->setCustomer($magentoCustomer);
         $magentoCustomerAddress->setCustomerId($magentoCustomer->getId());
         $magentoCustomerAddress->save();
         if ($magentoCustomerAddress->getIsInvoiceAddress() && !$magentoCustomer->getDefaultBillingAddress()) {
             $magentoCustomer->setDefaultBilling($magentoCustomerAddress->getId());
         }
         if ($magentoCustomerAddress->getIsDeliveryAddress() && !$magentoCustomer->getDefaultShippingAddress()) {
             $magentoCustomer->setDefaultShipping($magentoCustomerAddress->getId());
         }
     }
     $magentoCustomer->save();
 }
 /**
  * @param ShopgateOrder $order
  *
  * @return array
  * @throws PrestaShopException
  * @throws ShopgateLibraryException
  */
 public function addOrder(ShopgateOrder $order)
 {
     /**
      * check exits shopgate order
      */
     if (ShopgateOrderPrestashop::loadByOrderNumber($order->getOrderNumber())->status == 1) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DUPLICATE_ORDER, sprintf('external_order_id: %s', $order->getOrderNumber()), true);
     }
     /** @var CarrierCore $sgCarrier */
     $sgCarrier = new Carrier(Configuration::get('SG_CARRIER_ID'));
     if (version_compare(_PS_VERSION_, '1.5.0', '<') && empty($sgCarrier->name)) {
         $sgCarrier->name = 'shopgate_tmp_carrier';
     }
     if ($order->getShippingType() != ShopgateShipping::DEFAULT_PLUGIN_API_KEY) {
         if ($order->getShippingInfos()->getAmount() > 0) {
             if (version_compare(_PS_VERSION_, '1.5.0', '<')) {
                 ShopgateModObjectModel::updateShippingPrice(pSQL($order->getShippingInfos()->getAmount()));
             } else {
                 $data = array('price' => pSQL($order->getShippingInfos()->getAmount()));
                 $where = 'a.id_carrier = ' . (int) Configuration::get('SG_CARRIER_ID');
                 ObjectModel::updateMultishopTable('Delivery', $data, $where);
             }
             $sgCarrier->is_free = 0;
         } else {
             $sgCarrier->is_free = 1;
         }
     }
     $sgCarrier->update();
     $customerModel = new ShopgateItemsCustomerImportJson($this->getPlugin());
     $paymentModel = new ShopgatePayment($this->getModule());
     $shippingModel = new ShopgateShipping($this->getModule());
     /**
      * read / check customer
      */
     if (!($customerId = Customer::customerExists($order->getMail(), true, false))) {
         /**
          * prepare customer
          */
         $shopgateCustomerItem = new ShopgateCustomer();
         $shopgateCustomerItem->setLastName($order->getInvoiceAddress()->getLastName());
         $shopgateCustomerItem->setFirstName($order->getInvoiceAddress()->getFirstName());
         $shopgateCustomerItem->setGender($order->getInvoiceAddress()->getGender());
         $shopgateCustomerItem->setBirthday($order->getInvoiceAddress()->getBirthday());
         $shopgateCustomerItem->setNewsletterSubscription(Configuration::get('SG_SUBSCRIBE_NEWSLETTER') ? true : false);
         $customerId = $customerModel->registerCustomer($order->getMail(), md5(_COOKIE_KEY_ . time()), $shopgateCustomerItem);
     }
     /** @var CustomerCore $customer */
     $customer = new Customer($customerId);
     /**
      * prepare cart
      */
     if (!$order->getDeliveryAddress()->getPhone()) {
         $order->getDeliveryAddress()->setPhone($order->getPhone());
     }
     if (!$order->getInvoiceAddress()->getPhone()) {
         $order->getInvoiceAddress()->setPhone($order->getPhone());
     }
     $this->getCart()->id_address_delivery = $customerModel->createAddress($order->getDeliveryAddress(), $customer);
     $this->getCart()->id_address_invoice = $customerModel->createAddress($order->getInvoiceAddress(), $customer);
     $this->getCart()->id_customer = $customerId;
     // 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
     if (version_compare(_PS_VERSION_, '1.4.1.0', '>=')) {
         $this->getCart()->id_guest = $customer->is_guest;
     }
     $this->getCart()->secure_key = $customer->secure_key;
     $this->getCart()->id_carrier = $shippingModel->getCarrierIdByApiOrder($order);
     $shopgateCustomFieldsHelper = new ShopgateCustomFieldsHelper();
     $shopgateCustomFieldsHelper->saveCustomFields($this->getCart(), $order->getCustomFields());
     $this->getCart()->add();
     /**
      * add cart items
      */
     $canCreateOrder = true;
     $errorMessages = array();
     foreach ($order->getItems() as $item) {
         list($productId, $attributeId) = ShopgateHelper::getProductIdentifiers($item);
         if ($productId == 0) {
             continue;
         }
         $updateCart = $this->getCart()->updateQty($item->getQuantity(), $productId, $attributeId, false, 'up', $this->getCart()->id_address_delivery);
         if ($updateCart !== true) {
             $canCreateOrder = false;
             $errorMessages[] = array('product_id' => $productId, 'attribute_id' => $attributeId, 'quantity' => $item->getQuantity(), 'result' => $updateCart, 'reason' => $updateCart == -1 ? 'minimum quantity not reached' : '');
         }
     }
     /**
      * coupons
      */
     foreach ($order->getExternalCoupons() as $coupon) {
         /** @var CartRuleCore $cartRule */
         $cartRule = new CartRule(CartRule::getIdByCode($coupon->getCode()));
         if (Validate::isLoadedObject($cartRule)) {
             $this->getCart()->addCartRule($cartRule->id);
             $this->getCart()->save();
         }
     }
     if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
         /**
          * this field is not available in version 1.4.x.x
          * set delivery option
          */
         $delivery_option = array($this->getCart()->id_address_delivery => $shippingModel->getCarrierIdByApiOrder($order) . ',');
         $this->getCart()->setDeliveryOption($delivery_option);
     }
     /**
      * store shopgate order
      */
     $shopgateOrderItem = new ShopgateOrderPrestashop();
     $shopgateOrderItem->fillFromOrder($this->getCart(), $order, $this->getPlugin()->getShopgateConfig()->getShopNumber());
     if (version_compare(_PS_VERSION_, '1.6.0.0', '<')) {
         $shopgateOrderItem->add();
     }
     /**
      * create order
      */
     if ($canCreateOrder) {
         /**
          * get first item from order stats
          */
         $this->getCart()->save();
         $idOrderState = reset($paymentModel->getOrderStateId($order));
         $validateOder = $this->getModule()->validateOrder($this->getCart()->id, $idOrderState, $this->getCart()->getOrderTotal(true, defined('Cart::BOTH') ? Cart::BOTH : 3), $paymentModel->getPaymentTitleByKey($order->getPaymentMethod()), null, array(), null, false, $this->getCart()->secure_key);
         if (version_compare(_PS_VERSION_, '1.5.0.0', '<') && (int) $this->getModule()->currentOrder > 0 && $order->getShippingType() != ShopgateShipping::DEFAULT_PLUGIN_API_KEY && $order->getShippingInfos()->getAmount() > 0) {
             ShopgateLogger::log('PS < 1.5.0.0: update shipping and payment cost', ShopgateLogger::LOGTYPE_DEBUG);
             // in versions below 1.5.0.0 the shipping and payment costs must be updated after the order was imported
             /** @var OrderCore $updateShopgateOrder */
             $updateShopgateOrder = new Order($this->getModule()->currentOrder);
             $updateShopgateOrder->total_shipping = $order->getAmountShipping() + $order->getAmountShopPayment();
             $updateShopgateOrder->total_paid += $order->getAmountShipping() + $order->getAmountShopPayment();
             $updateShopgateOrder->total_paid_real += $order->getAmountShipping() + $order->getAmountShopPayment();
             $updateShopgateOrder->update();
         }
         /**
          * update shopgate order
          */
         if ($validateOder) {
             $shopgateOrderItem->id_order = $this->getModule()->currentOrder;
             $shopgateOrderItem->status = 1;
             $shopgateOrderItem->save();
             return array('external_order_id' => $shopgateOrderItem->id_order, 'external_order_number' => $shopgateOrderItem->id_order);
         }
     }
     $shopgateOrderItem->delete();
     throw new ShopgateLibraryException(ShopgateLibraryException::UNKNOWN_ERROR_CODE, 'Unable to create order:' . print_r($errorMessages, true), true);
 }
 /**
  * @param $user
  * @param $pass
  * @return ShopgateCustomer
  * @throws ShopgateLibraryException
  */
 public function getCustomer($user, $pass)
 {
     $customerId = $this->getCustomerIdByEmailAndPassword($user, $pass);
     if (!$customerId) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_WRONG_USERNAME_OR_PASSWORD, 'Username or password is incorrect');
     }
     /** @var CustomerCore $customer */
     $customer = new Customer($customerId);
     $shopgateCustomer = new ShopgateCustomer();
     $shopgateCustomer->setCustomerId($customer->id);
     $shopgateCustomer->setCustomerNumber($customer->id);
     $shopgateCustomer->setFirstName($customer->firstname);
     $shopgateCustomer->setLastName($customer->lastname);
     $shopgateCustomer->setGender($this->mapGender($customer->id_gender));
     $shopgateCustomer->setBirthday($customer->birthday);
     $shopgateCustomer->setMail($customer->email);
     $shopgateCustomer->setNewsletterSubscription($customer->newsletter);
     $shopgateCustomer->setCustomerToken(ShopgateCustomerPrestashop::getToken($customer));
     $addresses = array();
     foreach ($customer->getAddresses($this->getPlugin()->getLanguageId()) as $address) {
         $addressItem = new ShopgateAddress();
         $addressItem->setId($address['id_address']);
         $addressItem->setFirstName($address['firstname']);
         $addressItem->setLastName($address['lastname']);
         $addressItem->setCompany($address['company']);
         $addressItem->setStreet1($address['address1']);
         $addressItem->setStreet2($address['address2']);
         $addressItem->setCity($address['city']);
         $addressItem->setZipcode($address['postcode']);
         $addressItem->setCountry($address['country']);
         $addressItem->setState($address['state']);
         $addressItem->setPhone($address['phone']);
         $addressItem->setMobile($address['phone_mobile']);
         if ($address['alias'] == 'Default invoice address') {
             $addressItem->setAddressType(ShopgateAddress::INVOICE);
         } elseif ($address['alias'] == 'Default delivery address') {
             $addressItem->setAddressType(ShopgateAddress::DELIVERY);
         } else {
             $addressItem->setAddressType(ShopgateAddress::BOTH);
         }
         $addresses[] = $addressItem;
     }
     $shopgateCustomer->setAddresses($addresses);
     /**
      * customer groups
      */
     $customerGroups = array();
     if (is_array($customer->getGroups())) {
         foreach ($customer->getGroups() as $customerGroupId) {
             $groupItem = new Group($customerGroupId, $this->getPlugin()->getLanguageId(), $this->getPlugin()->getContext()->shop->id ? $this->getPlugin()->getContext()->shop->id : false);
             $group = new ShopgateCustomerGroup();
             $group->setId($groupItem->id);
             $group->setName($groupItem->name);
             $customerGroups[] = $group;
         }
     }
     $shopgateCustomer->setCustomerGroups($customerGroups);
     return $shopgateCustomer;
 }
Example #9
0
 /**
  * @param ShopgateCustomer             $shopgateCustomer
  * @param Mage_Customer_Model_Customer $magentoCustomer
  * @return ShopgateCustomer
  */
 protected function _getCustomerSetAddresses(&$shopgateCustomer, $magentoCustomer)
 {
     $aAddresses = array();
     foreach ($magentoCustomer->getAddresses() as $magentoCustomerAddress) {
         /** @var  Mage_Customer_Model_Address $magentoCustomerAddress */
         $shopgateAddress = new ShopgateAddress();
         $shopgateAddress->setId($magentoCustomerAddress->getId());
         $shopgateAddress->setIsDeliveryAddress(1);
         $shopgateAddress->setIsInvoiceAddress(1);
         $shopgateAddress->setFirstName($magentoCustomerAddress->getFirstname());
         $shopgateAddress->setLastName($magentoCustomerAddress->getLastname());
         $shopgateAddress->setGender($this->_getCustomerHelper()->getShopgateCustomerGender($magentoCustomerAddress));
         $shopgateAddress->setCompany($magentoCustomerAddress->getCompany());
         $shopgateAddress->setMail($magentoCustomerAddress->getMail());
         $shopgateAddress->setPhone($magentoCustomerAddress->getTelephone());
         $shopgateAddress->setStreet1($magentoCustomerAddress->getStreet1());
         $shopgateAddress->setStreet2($magentoCustomerAddress->getStreet2());
         $shopgateAddress->setCity($magentoCustomerAddress->getCity());
         $shopgateAddress->setZipcode($magentoCustomerAddress->getPostcode());
         $shopgateAddress->setCountry($magentoCustomerAddress->getCountry());
         $shopgateAddress->setState($this->_getHelper()->getIsoStateByMagentoRegion($magentoCustomerAddress));
         $aAddresses[] = $shopgateAddress;
     }
     $shopgateCustomer->setAddresses($aAddresses);
     return $shopgateCustomer;
 }