public async Task <ActionResult> CreateAccount()
        {
            var enteredData = _employerAccountOrchestrator.GetCookieData();

            if (enteredData == null)
            {
                // N.B CHANGED THIS FROM SelectEmployer which went nowhere.
                _employerAccountOrchestrator.DeleteCookieData();

                return(RedirectToAction(ControllerConstants.SearchForOrganisationActionName, ControllerConstants.SearchOrganisationControllerName));
            }

            var request = new CreateAccountModel
            {
                UserId                      = GetUserId(),
                OrganisationType            = enteredData.EmployerAccountOrganisationData.OrganisationType,
                OrganisationReferenceNumber = enteredData.EmployerAccountOrganisationData.OrganisationReferenceNumber,
                OrganisationName            = enteredData.EmployerAccountOrganisationData.OrganisationName,
                OrganisationAddress         = enteredData.EmployerAccountOrganisationData.OrganisationRegisteredAddress,
                OrganisationDateOfInception = enteredData.EmployerAccountOrganisationData.OrganisationDateOfInception,
                PayeReference               = enteredData.EmployerAccountPayeRefData.PayeReference,
                AccessToken                 = enteredData.EmployerAccountPayeRefData.AccessToken,
                RefreshToken                = enteredData.EmployerAccountPayeRefData.RefreshToken,
                OrganisationStatus          = string.IsNullOrWhiteSpace(enteredData.EmployerAccountOrganisationData.OrganisationStatus) ? null : enteredData.EmployerAccountOrganisationData.OrganisationStatus,
                EmployerRefName             = enteredData.EmployerAccountPayeRefData.EmployerRefName,
                PublicSectorDataSource      = enteredData.EmployerAccountOrganisationData.PublicSectorDataSource,
                Sector                      = enteredData.EmployerAccountOrganisationData.Sector,
                HashedAccountId             = _accountCookieStorage.Get(_hashedAccountIdCookieName),
                Aorn = enteredData.EmployerAccountPayeRefData.AORN
            };

            var response = await _employerAccountOrchestrator.CreateOrUpdateAccount(request, HttpContext);

            if (response.Status == HttpStatusCode.BadRequest)
            {
                response.Status       = HttpStatusCode.OK;
                response.FlashMessage = new FlashMessageViewModel {
                    Headline = "There was a problem creating your account"
                };
                return(RedirectToAction(ControllerConstants.SummaryActionName));
            }

            _employerAccountOrchestrator.DeleteCookieData();

            var returnUrlCookie = _returnUrlCookieStorageService.Get(ReturnUrlCookieName);

            _accountCookieStorage.Delete(_hashedAccountIdCookieName);

            _returnUrlCookieStorageService.Delete(ReturnUrlCookieName);

            if (returnUrlCookie != null && !returnUrlCookie.Value.IsNullOrWhiteSpace())
            {
                return(Redirect(returnUrlCookie.Value));
            }

            return(RedirectToAction(ControllerConstants.WhenDoYouWantToView, ControllerConstants.EmployerAgreementControllerName, new { hashedAccountId = response.Data.EmployerAgreement.HashedAccountId, agreementId = response.Data.EmployerAgreement.HashedAgreementId }));
        }
        public async Task <ActionResult> GateWayResponse()
        {
            try
            {
                _logger.Info("Starting processing gateway response");

                var response = await _employerAccountOrchestrator.GetGatewayTokenResponse(Request.Params[ControllerConstants.CodeKeyName], Url.Action(ControllerConstants.GateWayResponseActionName, ControllerConstants.EmployerAccountControllerName, null, Request.Url.Scheme), System.Web.HttpContext.Current?.Request.QueryString);

                if (response.Status != HttpStatusCode.OK)
                {
                    _logger.Warn($"Gateway response does not indicate success. Status = {response.Status}.");
                    response.Status = HttpStatusCode.OK;

                    AddFlashMessageToCookie(response.FlashMessage);

                    return(RedirectToAction(ControllerConstants.GatewayInformActionName));
                }

                var externalUserId = OwinWrapper.GetClaimValue(ControllerConstants.UserRefClaimKeyName);
                _logger.Info($"Gateway response is for user identity ID {externalUserId}");

                var email  = OwinWrapper.GetClaimValue(ControllerConstants.EmailClaimKeyName);
                var empref = await _employerAccountOrchestrator.GetHmrcEmployerInformation(response.Data.AccessToken, email);

                _logger.Info($"Gateway response is for empref {empref.Empref} \n {JsonConvert.SerializeObject(empref)}");

                var enteredData = _employerAccountOrchestrator.GetCookieData(HttpContext);

                enteredData.EmployerRefName = empref.EmployerLevyInformation?.Employer?.Name?.EmprefAssociatedName ?? "";
                enteredData.PayeReference   = empref.Empref;
                enteredData.AccessToken     = response.Data.AccessToken;
                enteredData.RefreshToken    = response.Data.RefreshToken;
                enteredData.EmpRefNotFound  = empref.EmprefNotFound;
                _employerAccountOrchestrator.UpdateCookieData(HttpContext, enteredData);

                _logger.Info("Finished processing gateway response");
                return(RedirectToAction(ControllerConstants.SummaryActionName));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Error processing Gateway response - {ex.Message}");
                throw;
            }
        }