/// <summary> /// Executes the Remove card invocation. /// </summary> public async Task Execute() { try { SharedUserLogic sharedUserLogic = new SharedUserLogic(Context, CommerceOperationsFactory.UserOperations(Context)); SharedCardLogic sharedCardLogic = new SharedCardLogic(Context, CommerceOperationsFactory.CardOperations(Context)); RemoveCardConcluder removeCardConcluder = new RemoveCardConcluder(Context); User user = sharedUserLogic.RetrieveUser(); Context[Key.User] = user; if (user != null) { Card card = sharedCardLogic.RetrieveCard(); Context[Key.Card] = card; if (card != null) { if (card.GlobalUserId == user.GlobalId) { Context.Log.Verbose("Attempting to remove the card from all current partners."); RemoveCardInvoker removeCardInvoker = new RemoveCardInvoker(Context); await removeCardInvoker.Invoke(); } else { removeCardConcluder.Conclude(ResultCode.CardDoesNotBelongToUser); } } else { removeCardConcluder.Conclude(ResultCode.UnregisteredCard); } } else { removeCardConcluder.Conclude(ResultCode.UnexpectedUnregisteredUser); } } catch (Exception ex) { ((ResultSummary)Context[Key.ResultSummary]).SetResultCode(ResultCode.UnknownError); RestResponder.BuildAsynchronousResponse(Context, ex); } }
/// <summary> /// Executes the Claim Deal API invocation. /// </summary> public async Task Execute() { try { SharedUserLogic sharedUserLogic = new SharedUserLogic(Context, CommerceOperationsFactory.UserOperations(Context)); SharedCardLogic sharedCardLogic = new SharedCardLogic(Context, CommerceOperationsFactory.CardOperations(Context)); SharedDealLogic sharedDealLogic = new SharedDealLogic(Context, CommerceOperationsFactory.DealOperations(Context)); ClaimDealConcluder claimDealConcluder = new ClaimDealConcluder(Context); ResultCode extractionResult = ExtractClaimDealPayload(); if (extractionResult == ResultCode.Success) { Deal deal = sharedDealLogic.RetrieveDeal(); Context[Key.Deal] = deal; if (deal != null) { Context[Key.DealDiscountSummary] = deal.DiscountSummary; User user = sharedUserLogic.RetrieveUser(); Context[Key.User] = user; if (user != null) { Card card = sharedCardLogic.RetrieveCard(); Context[Key.Card] = card; if (card != null) { // If the deal and card have common ground, claim the deal for the card. if (MustClaim(deal, card) == true) { Context[Key.MerchantName] = deal.MerchantName; Context.Log.Verbose("Trying to claim the deal for the user with the appropriate partner."); ClaimDealInvoker claimDealInvoker = new ClaimDealInvoker(Context); await claimDealInvoker.Invoke(); } else { Context.Log.Verbose("It is not necessary to claim this deal for this card."); ((ResultSummary)Context[Key.ResultSummary]).SetResultCode(ResultCode.Success); RestResponder.BuildAsynchronousResponse(Context); } } else { claimDealConcluder.Conclude(ResultCode.UnregisteredCard); } } else { claimDealConcluder.Conclude(ResultCode.UnexpectedUnregisteredUser); } } else { claimDealConcluder.Conclude(ResultCode.UnactionableDealId); } } else { claimDealConcluder.Conclude(extractionResult); } } catch (Exception ex) { ((ResultSummary)Context[Key.ResultSummary]).SetResultCode(ResultCode.UnknownError); RestResponder.BuildAsynchronousResponse(Context, ex); } }
/// <summary> /// Places the User object representing the person making this request to the context. /// </summary> /// <returns> /// The ResultCode corresponding to the result of the operation. /// </returns> /// <remarks> /// If flagged to do so, a user account will be created and associated with the specified e-mail address, if the e-mail /// address has not already been used. /// </remarks> private ResultCode PlaceUserInContext() { ResultCode result = ResultCode.Success; bool createUnauthenticatedAccount = false; if (Context[Key.CreateUnauthenticatedAccount] != null) { createUnauthenticatedAccount = (bool)Context[Key.CreateUnauthenticatedAccount]; } if (createUnauthenticatedAccount == true) { string emailAddress = Context[Key.EmailAddress] as string; if (String.IsNullOrWhiteSpace(emailAddress) == false) { try { // Ensure the e-mail address may be valid. MailAddress mailAddress = new MailAddress(emailAddress); // Attempt to add a user to User Services via Users Dal and obtain its authentication vector. IUsersDal usersDal = PartnerFactory.UsersDal(Context.Config); Users.Dal.DataModel.User fullUser = usersDal.CreateUnauthenticatedUser(mailAddress.Address, (string)Context[Key.ReferrerId], (string)Context[Key.UserLocation]); UnauthenticatedAddCardResponse response = (UnauthenticatedAddCardResponse)Context[Key.Response]; if (String.IsNullOrWhiteSpace(fullUser.MsId) == true) { response.AuthenticationVector = AuthenticationVector.Email.ToString(); } else if (fullUser.MsId.StartsWith("FB-", StringComparison.OrdinalIgnoreCase) == true) { response.AuthenticationVector = AuthenticationVector.Facebook.ToString(); } else { response.AuthenticationVector = AuthenticationVector.MicrosoftAccount.ToString(); } Guid userId = fullUser.Id; Context[Key.GlobalUserId] = userId; // If the user returned by User Services has not already been registered in the Commerce system, register a new Commerce user. User user = SharedUserLogic.RetrieveUser(); if (user == null) { user = new User(userId, Guid.NewGuid()); Context[Key.User] = user; result = SharedUserLogic.AddUser(); if (result == ResultCode.Created) { Analytics.AddRegisterUserEvent(user.GlobalId, user.AnalyticsEventId, Guid.Empty, Context[Key.ReferrerId] as string); result = ResultCode.Success; } } else { Context[Key.User] = user; } // If the user was added or retrieved successfully, proceed. if (result == ResultCode.Success) { // If the user has not already signed up officially with Bing Offers, proceed. if (response.AuthenticationVector == AuthenticationVector.Email.ToString()) { // If the user has not already added a card, proceed. SharedCardLogic sharedCardLogic = new SharedCardLogic(Context, CommerceOperationsFactory.CardOperations(Context)); if (sharedCardLogic.RetrieveUserCards().Count() == 0) { response.ActivationToken = fullUser.ActivationToken; } else { result = ResultCode.UnauthenticatedUserAlreadyExists; } } else { result = ResultCode.UserAlreadyExists; } } } catch (FormatException) { result = ResultCode.InvalidParameter; } } else { result = ResultCode.ParameterCannotBeNull; } } else { Context[Key.User] = SharedUserLogic.RetrieveUser(); } return(result); }