Пример #1
0
        /// <summary>
        /// Pings the Users database.
        /// </summary>
        /// <returns>
        /// * True if the Users database could be pinged.
        /// * Else returns false.
        /// </returns>
        private static bool PingUsers()
        {
            // Ping Users by attempting to retrieve a User known not to exist.
            IUsersDal usersDal = PartnerFactory.UsersDal(CommerceServiceConfig.Instance);

            return(usersDal.GetUserByUserId(Guid.Empty) == null);
        }
Пример #2
0
        /// <summary>
        /// Registers external services for commerce like MVC constructs, security providers and Analytics
        /// </summary>
        internal static void RegisterExternalServices()
        {
            // Register log.
            LogInitializer.CreateLogInstance(CommerceServiceConfig.Instance.LogVerbosity,
                                             CommerceServiceConfig.Instance.ForceEventLog,
                                             General.CommerceLogSource,
                                             CommerceServiceConfig.Instance);

            // Register MVC constructs.
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Register security providers.
            IUsersDal usersDal = PartnerFactory.UsersDal(CommerceServiceConfig.Instance);

            if (CommerceServiceConfig.Instance.EnableDebugSecurityProvider == true)
            {
                Security.Add("user_debug", new UserDebugSecurityProvider(usersDal));
            }
            Security.Add("usertoken", new UserTokenSecurityProvider());

            // Register Analytics Service
            Analytics.Initialize(CommerceServiceConfig.Instance);
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the Notify class.
        /// </summary>
        /// <param name="context">
        /// The context for this API call.
        /// </param>
        protected Notify(CommerceContext context)
        {
            Context  = context;
            UsersDal = PartnerFactory.UsersDal(Context.Config);
            Uri userServicesClientUri = new Uri(Context.Config.UserServicesClientEndpoint);

            UserServicesClient = PartnerFactory.UserServicesClient(userServicesClientUri, Context.Config);
            SmsServiceUrl      = Context.Config.SmsServiceClientEndpoint;
        }
Пример #4
0
        /// <summary>
        /// Obtains the ANID for the specified canonical user ID.
        /// </summary>
        /// <param name="userId">
        /// The canonical user ID for whose ANID to obtain.
        /// </param>
        /// <param name="commerceConfig">
        /// The configuration instance to use.
        /// </param>
        /// <returns>
        /// The ANID for the specified user.
        /// </returns>
        private static string GetAnidFromUserId(Guid userId,
                                                CommerceConfig commerceConfig)
        {
            string puid = String.Empty;
            User   user = PartnerFactory.UsersDal(commerceConfig).GetUserByUserId(userId);

            if (user != null)
            {
                puid = user.MsId;
            }
            return(CommerceAnalyticsFactory.AnalyticsUserInfo(commerceConfig).GetAnidFromPuid(puid));
        }
Пример #5
0
        /// <summary>
        /// Registers external services for commerce like MVC constructs, security providers and Analytics
        /// </summary>
        internal static void RegisterExternalServices()
        {
            // Use only for debugging
            // TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
            TelemetryConfiguration.Active.InstrumentationKey = CloudConfigurationManager.GetSetting("APPINSIGHTS_INSTRUMENTATIONKEY");

            // Register log.
            LogInitializer.CreateLogInstance(CommerceServiceConfig.Instance.LogVerbosity,
                                             CommerceServiceConfig.Instance.ForceEventLog,
                                             General.CommerceLogSource,
                                             CommerceServiceConfig.Instance);

            Log.Info("Started Commerce MBI service.");

            // Register MVC constructs.
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            // Bing FrontDoor and user debug security providers.
            IUsersDal usersDal = PartnerFactory.UsersDal(CommerceServiceConfig.Instance);

            if (CommerceServiceConfig.Instance.EnableDebugSecurityProvider == true)
            {
                Security.Add("user_debug", new UserDebugSecurityProvider(usersDal));
            }

            Security.Add("lomo", new LomoSecurityProvider(usersDal));

            // Register the mutual SSL security provider.
            Security.Add(MutualSslSecurityProvider.Name, new MutualSslSecurityProvider());

            // Register the Simple Web Token security provider.
            string environment      = string.Concat("commerce-", CommerceServiceConfig.Instance.Environment);
            string resourceTemplate = string.Concat(string.Format("https://{0}.TODO_INSERT_YOUR_DOMAIN_HERE/api/commerce/service/",
                                                                  environment), "{0}");

            Security.Add(SimpleWebTokenSecurityProvider.Name,
                         new SimpleWebTokenSecurityProvider(environment, resourceTemplate,
                                                            CommerceServiceConfig.Instance.SimpleWebTokenKey));

            // Amex payment authorization SWT security provider
            Security.Add("Bearer", new SimpleWebTokenSecurityProvider(environment,
                                                                      string.Concat(string.Format("https://{0}.TODO_INSERT_YOUR_DOMAIN_HERE/api/commerce/amex/", environment), "{0}"),
                                                                      CommerceServiceConfig.Instance.SimpleWebTokenKey));

            // Register Analytics Service
            Analytics.Initialize(CommerceServiceConfig.Instance);
        }
Пример #6
0
        /// <summary>
        /// Concludes execution of the Add card call after previous work has been completed.
        /// </summary>
        /// <param name="resultCode">
        /// The ResultCode to set within the call response.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Parameter context cannot be null.
        /// </exception>
        public void Conclude(ResultCode resultCode)
        {
            try
            {
                Context.Log.Verbose("ResultCode when Conclude process begins: {0}.", resultCode);

                // If process succeeded, update internal data storage.
                AddCardResponse response = (AddCardResponse)Context[Key.Response];
                if (resultCode == ResultCode.Created)
                {
                    // Add the card.
                    resultCode = AddCard();
                    if (resultCode == ResultCode.Created || resultCode == ResultCode.Success)
                    {
                        response.NewCardId = General.GuidFromInteger(((Card)Context[Key.Card]).Id);

                        // If a new card was added, kick off confirmation process for unauthenticated users and add needed information to analytics.
                        // TODO: AddCard() above returns ResultCode.Success. So the code below will not execute. Is it ok?
                        if (resultCode == ResultCode.Created)
                        {
                            // Kick off confirmation process for unauthenticated users.
                            bool createUnauthenticatedAccount = false;
                            if (Context[Key.CreateUnauthenticatedAccount] != null)
                            {
                                createUnauthenticatedAccount = (bool)Context[Key.CreateUnauthenticatedAccount];
                            }

                            if (createUnauthenticatedAccount == true)
                            {
                                IUsersDal usersDal = PartnerFactory.UsersDal(Context.Config);
                                Task.Run(() => usersDal.CompleteUnauthenticatedUserSetUp((Guid)Context[Key.GlobalUserId]));
                            }

                            // Add analytics info.
                            Context.Log.Verbose("Adding new card to analytics.");
                            User user = (User)Context[Key.User];
                            Analytics.AddAddCardEvent(user.GlobalId, user.AnalyticsEventId, Guid.Empty, Context[Key.ReferrerId] as string);
                        }

                        // Queue deal claiming if set to do so.
                        bool queueDealClaiming = false;
                        if (Context[Key.QueueJob] != null)
                        {
                            queueDealClaiming = (bool)Context[Key.QueueJob];
                        }

                        // Linking is only for First Data, but by the time execution reaches this part of the code, the card may need to be linked to CLO offers or
                        //  Burn offers, or both, but definitely at least one of them. Therefore, a job has to be scheduled to cover the relevant combination of CLO
                        //  and Burn offers. That Earn offers are not registered with First Data doesn't change this-- the filtering will have to occur as part of the job.
                        if (queueDealClaiming == true)
                        {
                            QueueClaimingDeals(response);
                            resultCode = ResultCode.JobQueued;
                        }
                    }
                }

                response.ResultSummary.SetResultCode(resultCode);
                RestResponder.BuildAsynchronousResponse(Context);
            }
            catch (Exception ex)
            {
                RestResponder.BuildAsynchronousResponse(Context, ex);
            }
        }
Пример #7
0
        /// <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);
        }