You should replace a part of your code with "Tools::getShopDomainSsl(true)", it will resolve the problem of redirect.