Example #1
0
    /**
     * Main function
     * Handle the login and registration of customers
     * @return [type]
     * @author edudeleon
     * @date   2015-08-10
     */
    public function execute()
    {
        //Getting Api Key from Admin Panel
        $AddShoppersSecret = $this->_addshoppersConnectorData->getApiKey();
        // Validate signature
        $params = json_decode($this->getRequest()->getParam('data'));
        $signature = null;
        $p = array();
        foreach ($params as $key => $value) {
            if ($key == "signature") {
                $signature = $value;
            } else {
                $p[] = $key . "=" . $value;
            }
            $pos = strpos($key, "_email");
            if ($pos) {
                $urlemail = $value;
            }
        }
        asort($p);
        $query = $AddShoppersSecret . implode($p);
        $hashed = hash("md5", $query);
        if ($signature !== $hashed) {
            $this->_logAddshoppersMsg("Invalid AddShoppers key or bad signature request.");
            die;
        }
        // Signature validated, this is a valid request... continue on
        $urluser = $this->getRequest()->getParam("asusrnm");
        //Checking params
        if (!$urluser) {
            //die("Invalid AddShoppers params.");
        }
        if (!$urlemail) {
            //die("Invalid AddShoppers params");
        }
        //Split name into first name and last name
        $arr = explode('_', trim($urluser));
        $firstname = $arr[0];
        $lastname = array_shift($arr);
        $lastname = implode(" ", $arr);
        $email = $urlemail;
        //Check if user is logged in
        if ($this->_customerSession->isLoggedIn()) {
            die;
        }
        //Preparing data
        $customer_email = $email;
        // email adress that will pass by the questionaire
        $customer_fname = $firstname;
        // first name from api
        $customer_lname = $lastname;
        // last name from api
        $passwordLength = 10;
        // the lenght of autogenerated password
        //Getting Website ID
        $websiteId = $this->_storeManager->getWebsite()->getWebsiteId();
        // Instantiate object (this is the most important part)
        $customer = $this->_customerFactory->create();
        $customer->setWebsiteId($websiteId);
        //Loading customer by email
        $customer->loadByEmail($email);
        //If customer exists, login customer.. otherwise create a nuew customer
        if (!$customer->getId()) {
            //Preparing data for new customers
            $customer->setEmail($customer_email);
            $customer->setFirstname($customer_fname);
            $customer->setLastname($customer_lname);
            $pass = $this->_mathRandom->getRandomString($passwordLength);
            $customer->setPassword($pass);
            //Attempt to create new customer
            try {
                //Save new customer and send the new account email.
                $customer->save();
                $customer->setConfirmation(null);
                $customer->save();
                $customer->sendNewAccountEmail();
            } catch (Exception $e) {
                $this->_logAddshoppersMsg($e->getMessage());
            }
            // Login new customer
            $new_customer = $this->_customerAccountManagement->authenticate($customer_email, $pass);
            $this->_customerSession->setCustomerDataAsLoggedIn($new_customer);
            $this->_customerSession->regenerateId();
        } else {
            //Login existence customer. Customer object is already loaded
            $this->_customerSession->setCustomerAsLoggedIn($customer);
            //this is secure because we validated the Social Login call using the signature and secret key
        }
        //Log data returned from Social Media script
        //$this->_logAddshoppersMsg($_GET["data"]."{".$_GET["asusrnm"]."}"."{".$_GET["aseml"]."}");
        //Reloding page (production)
        echo '<script type="text/javascript">
				window.onload = function refreshParent() 
				{ 
			    	window.parent.location.reload(true);
				}
		</script>';
    }