/// <summary> /// Gets existing customer. /// </summary> /// <param name="httpContextBase">The HTTP context base.</param> /// <param name="idToken">The identifier token.</param> /// <param name="identityProviderType">Type of the identity provider.</param> /// <returns>The customer associated with the token.</returns> private async Task <Tuple <Customer, bool> > GetExistingCustomer(HttpContextBase httpContextBase, string idToken, IdentityProviderType identityProviderType) { EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(httpContextBase, idToken, identityProviderType); Customer currentCustomer = null; bool isRequestToLinkToExistingCustomerPending = false; try { CustomerOperationsHandler customerOperationsHandler = new CustomerOperationsHandler(ecommerceContext); currentCustomer = await customerOperationsHandler.GetCustomer(); } catch (UserAuthorizationException ex) { if (ex.ErrorResourceId == AuthenticationErrors.CommerceIdentityNotFound) { var message = "Customer read failed. No customer is associated with external identity. It is okay to create a new customer."; RetailLogger.Log.OnlineStoreNoCustomerAssociatedWithExternalId(ecommerceContext.IdentityProviderType.ToString(), message, ex); } else if (ex.ErrorResourceId == AuthenticationErrors.UserNotActivated) { var message = "Customer read failed. There is a pending request for linking the current external id to a customer account."; RetailLogger.Log.OnlineStoreInactiveLinkBetweenAnExistingCustomerAndExternalId(ecommerceContext.IdentityProviderType.ToString(), message, ex); isRequestToLinkToExistingCustomerPending = true; } else { throw; } } return(new Tuple <Customer, bool>(currentCustomer, isRequestToLinkToExistingCustomerPending)); }
/// <summary> /// Gets the loyalty cards. /// </summary> /// <param name="queryResultSettings">The query result settings.</param> /// <returns>Response containing the loyalty cards associated with the current user.</returns> public async Task <ActionResult> GetLoyaltyCards(QueryResultSettings queryResultSettings) { EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(this.HttpContext); CustomerOperationsHandler customerOperationsHandler = new CustomerOperationsHandler(ecommerceContext); PagedResult <LoyaltyCard> loyaltyCards = await customerOperationsHandler.GetLoyaltyCards(queryResultSettings); return(this.Json(loyaltyCards.Results)); }
/// <summary> /// Generates the loyalty card identifier. /// </summary> /// <returns> /// Response containing a newly generated loyalty card. /// </returns> public async Task <ActionResult> GenerateLoyaltyCardId() { EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(this.HttpContext); CustomerOperationsHandler customerOperationsHandler = new CustomerOperationsHandler(ecommerceContext); LoyaltyCard loyaltyCard = await customerOperationsHandler.GenerateLoyaltyCardId(); return(this.Json(loyaltyCard)); }
/// <summary> /// Gets the order history of a customer. /// </summary> /// <param name="queryResultSettings">The query result settings.</param> /// <returns>A sakes order response.</returns> public async Task <ActionResult> GetOrderHistory(QueryResultSettings queryResultSettings) { EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(this.HttpContext); CustomerOperationsHandler customerOperationsHandler = new CustomerOperationsHandler(ecommerceContext); PagedResult <SalesOrder> salesOrders = await customerOperationsHandler.GetOrderHistory(queryResultSettings); return(this.Json(salesOrders)); }
/// <summary> /// Updates the customer. /// </summary> /// <param name="customer">The customer.</param> /// <returns> /// The updated customer entity. /// </returns> public async Task <ActionResult> UpdateCustomer(Customer customer) { EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(this.HttpContext); CustomerOperationsHandler customerOperationsHandler = new CustomerOperationsHandler(ecommerceContext); Customer updatedCustomer = await customerOperationsHandler.UpdateCustomer(customer); return(this.Json(updatedCustomer)); }
/// <summary> /// Unlinks the account confirm. /// </summary> /// <returns>A redirect action to the sign out action.</returns> public async Task <ActionResult> UnlinkAccountConfirm() { if (!this.HttpContext.Request.IsAuthenticated) { return(new HttpUnauthorizedResult("User must sign in first")); } EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(this.HttpContext); CustomerOperationsHandler customerOperationsHandler = new CustomerOperationsHandler(ecommerceContext); await customerOperationsHandler.UnlinkExternalIdFromExistingCustomer(); return(this.RedirectToAction(SignInController.SignOutActionName, SignInController.ControllerName)); }
public async Task <ActionResult> FinishSignUp() { IdentityProviderType selectedAuthenticationProviderType; Enum.TryParse(this.Request.Form["SelectedAuthenticationProviderType"], out selectedAuthenticationProviderType); string selectedAuthenticationProviderToken = this.Request.Form["SelectedAuthenticationProviderToken"]; EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(this.HttpContext, selectedAuthenticationProviderToken, selectedAuthenticationProviderType); string signUpRadioButtonSelection = this.Request.Form["SignUpRadioButton"]; if (string.Equals(signUpRadioButtonSelection, NewAccount, StringComparison.OrdinalIgnoreCase)) { string firstName = this.Request.Form["FirstName"]; string lastName = this.Request.Form["LastName"]; string email = this.Request.Form["Email"]; Customer customer = new Customer(); customer.Email = email; customer.AccountNumber = string.Empty; customer.FirstName = firstName; customer.LastName = lastName; CustomerOperationsHandler customerOperationsHandler = new CustomerOperationsHandler(ecommerceContext); customer = await customerOperationsHandler.Create(customer); this.UpdateClaims( this.HttpContext, selectedAuthenticationProviderToken, selectedAuthenticationProviderType, email, customer.AccountNumber, customer.FirstName, customer.LastName); return(this.RedirectToAction(HomeController.DefaultActionName, HomeController.ControllerName)); } else { string emailOfExistingCustomer = this.Request.Form["EmailOfExistingCustomer"]; LinkToExistingCustomerResult linkToExistingCustomerResult = null; try { CustomerOperationsHandler customerOperationsHandler = new CustomerOperationsHandler(ecommerceContext); linkToExistingCustomerResult = await customerOperationsHandler.InitiateLinkExternalIdToExistingCustomer(emailOfExistingCustomer, "emailTemplateId", null); } catch (DataValidationException ex) { if (string.Equals(ex.ErrorResourceId, DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_CustomerNotFound.ToString(), StringComparison.OrdinalIgnoreCase)) { RetailLogger.Log.OnlineStoreCustomerForLinkingNotFound(Utilities.GetMaskedEmailAddress(emailOfExistingCustomer), ex, ex.InnerException); this.TempData["BadLinkUpEmail"] = emailOfExistingCustomer; this.TempData["AuthToken"] = selectedAuthenticationProviderToken; this.TempData["ExternalIdProviderType"] = selectedAuthenticationProviderType; this.TempData["LogOnEmail"] = this.Request.Form["LogOnEmail"]; return(this.RedirectToAction(SignInController.StartSignUpActionName, SignInController.ControllerName)); } else { throw ex; } } string maskedEmailAddress = Utilities.GetMaskedEmailAddress(linkToExistingCustomerResult.EmailAddress); ////Clean up auth cookies completely. We need to be signed out. this.TempData["IsSignUpFlow"] = true; this.TempData["MaskedEmail"] = maskedEmailAddress; return(this.RedirectToAction(SignInController.AccountLinkUpPendingActionName, SignInController.ControllerName)); } }