public UsedActionsController(UsedActionFactory factory, UserFactory userFactory, ActionFactory actionFactory, PartnerFactory partnerFactory)
 {
     _factory        = factory;
     _partnerFactory = partnerFactory;
     _actionFactory  = actionFactory;
     _userFactory    = userFactory;
 }
예제 #2
0
        /// <summary>
        /// Adds a settlement event to the analytics.
        /// </summary>
        /// <param name="userId">
        /// The canonical user ID to associate with the event.
        /// </param>
        /// <param name="settlementEventId">
        /// The ID of this settlement event.
        /// </param>
        /// <param name="correlationId">
        /// The ID of the event to which this event will be correlated.
        /// </param>
        /// <param name="dealId">
        /// The ID of the deal being redeemed.
        /// </param>
        /// <param name="currency">
        /// The currency involved in the transaction in which the deal was redeeemed.
        /// </param>
        /// <param name="settlementAmount">
        /// The settlement amount in the transaction in which the deal was redeemed.
        /// </param>
        /// <param name="discountAmount">
        /// The discount amount from the transaction in which the deal was redeemed.
        /// </param>
        /// <param name="discountId">
        /// The ID of the discount within the deal that was specifically redeemed.
        /// </param>
        /// <param name="partnerMerchantId">
        /// The ID of the merchant as assigned by the processing partner.
        /// </param>
        public static void AddSettlementEvent(Guid userId,
                                              Guid settlementEventId,
                                              Guid correlationId,
                                              Guid dealId,
                                              string currency,
                                              int settlementAmount,
                                              int discountAmount,
                                              Guid discountId,
                                              string partnerMerchantId)
        {
            if (CommerceWorkerConfig.Instance.EnableServiceHealth == true)
            {
                AnalyticsClient.Payloads.DealSettlementPayload payload = new AnalyticsClient.Payloads.DealSettlementPayload
                {
                    SettlementAmount  = settlementAmount,
                    Currency          = currency,
                    DiscountAmount    = discountAmount,
                    DiscountId        = discountId,
                    PartnerMerchantId = partnerMerchantId
                };

                AnalyticsClient.AnalyticsItem item = new AnalyticsClient.AnalyticsItem
                {
                    Action        = AnalyticsClient.Actions.Actions.Settlement,
                    UserId        = GetAnidFromUserId(userId, CommerceWorkerConfig.Instance),
                    ClientId      = AnalyticsClientId,
                    EventId       = settlementEventId,
                    ParentEventId = correlationId,
                    DealId        = dealId,
                    JPayload      = JObject.FromObject(payload)
                };

                PartnerFactory.AnalyticsClient(CommerceWorkerConfig.Instance).Add(item);
            }
        }
예제 #3
0
        /// <summary>
        /// Obtains an authorization token for the specified resource.
        /// </summary>
        /// <param name="resource">
        /// The resource for which to obtain an authorization token.
        /// </param>
        /// <returns>
        /// The authorization token.
        /// </returns>
        internal static string ObtainAuthorizationToken(string resource)
        {
            string result;

            // Build the fully qualified resource name.
            string environment            = String.Concat("commerce-", CommerceWorkerConfig.Instance.Environment);
            string fullyQualifiedResource = String.Format("https://{0}.TODO_INSERT_YOUR_DOMAIN_HERE/api/commerce/service/{1}", environment,
                                                          resource);

            // If the token has already been retrieved and is still valid, use it.
            if (Tokens.ContainsKey(fullyQualifiedResource) == true && Tokens[fullyQualifiedResource].Received + TokenLifetime > DateTime.Now)
            {
                result = Tokens[fullyQualifiedResource].Token;
            }
            // Otherwise, obtain and store a new token.
            else
            {
                ISimpleWebTokenRequestor requestor = PartnerFactory.SimpleWebTokenRequestor();
                result = requestor.RequestToken("Orchestrated Job Queue", CommerceWorkerConfig.Instance.AcsClientCredential,
                                                environment, fullyQualifiedResource);
                Tokens[fullyQualifiedResource] = new SimpleWebToken {
                    Token = result, Received = DateTime.Now
                };
            }

            return(result);
        }
예제 #4
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);
        }
예제 #5
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);
        }
예제 #6
0
 public AmexOfferRegistrationFileSyncJob()
 {
     Context   = new CommerceContext("Amex Offer Registration File Sync Job", CommerceWorkerConfig.Instance);
     Scheduler = PartnerFactory.Scheduler(CommerceWorkerConfig.Instance.SchedulerQueueName,
                                          CommerceWorkerConfig.Instance.SchedulerTableName,
                                          CommerceWorkerConfig.Instance);
 }
예제 #7
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;
        }
예제 #8
0
 public MainController(
     PartnerTypeFactory partnerTypeFactory,
     PartnerFactory partnerFactory,
     PartnerActionMapFactory partnerActionMapFactory
     )
 {
     _partnerTypeFactory      = partnerTypeFactory;
     _partnerFactory          = partnerFactory;
     _partnerActionMapFactory = partnerActionMapFactory;
 }
        /// <summary>
        /// Adds a redemption reward for the transaction in the context.
        /// </summary>
        internal void AddRedemptionRewards()
        {
            RedeemedDealInfo redeemedDealInfo = (RedeemedDealInfo)Context[Key.RedeemedDealInfo];

            if (Context.Config.EnableRedemptionRewards == true && (ReimbursementTender)redeemedDealInfo.ReimbursementTenderId == ReimbursementTender.MicrosoftEarn)
            {
                IRewardOperations rewardOperations = CommerceOperationsFactory.RewardOperations(Context);
                Context[Key.RewardId] = Context.Config.FirstEarnRewardId;
                Context[Key.FirstEarnRewardAmount]      = Context.Config.FirstEarnRewardAmount;
                Context[Key.FirstEarnRewardExplanation] = Context.Config.FirstEarnRewardExplanation;
                ConcurrentDictionary <string, string> payload = new ConcurrentDictionary <string, string>();
                IScheduler scheduler = PartnerFactory.Scheduler(Context.Config.SchedulerQueueName,
                                                                Context.Config.SchedulerTableName,
                                                                Context.Config);
                if (rewardOperations.AddRedemptionReward() == ResultCode.Success)
                {
                    // Add a job to potentially reward user for their first Earn.
                    payload[Key.RewardPayoutId.ToString()]        = ((Guid)Context[Key.RewardPayoutId]).ToString();
                    payload[Key.PartnerCardId.ToString()]         = (string)Context[Key.PartnerCardId];
                    payload[Key.PartnerRedeemedDealId.ToString()] = redeemedDealInfo.PartnerRedeemedDealId;
                    payload[Key.RewardId.ToString()] = Context.Config.FirstEarnRewardId.ToString();
                    ScheduledJobDetails scheduledJobDetails = new ScheduledJobDetails
                    {
                        JobId          = Guid.NewGuid(),
                        JobType        = ScheduledJobType.ApplyRedemptionReward,
                        JobDescription = redeemedDealInfo.GlobalUserId.ToString(),
                        Orchestrated   = true,
                        Payload        = payload
                    };
                    scheduler.ScheduleJobAsync(scheduledJobDetails).Wait();
                }

                // Add a job to potentially reward the person who referred this user for this user's first Earn.
                Context[Key.RedeemedDealId] = ((RedeemedDeal)Context[Key.RedeemedDeal]).Id;
                Guid globalUserId = ((RedeemedDealInfo)Context[Key.RedeemedDealInfo]).GlobalUserId;
                Context[Key.GlobalUserId] = globalUserId;
                string userId = globalUserId.ToString();
                if (rewardOperations.AddReferredRedemptionReward() == ResultCode.Success)
                {
                    payload[Key.GlobalUserId.ToString()]  = userId;
                    payload[Key.ReferralEvent.ToString()] = ReferralEvent.Signup.ToString();
                    ScheduledJobDetails scheduledJobDetails = new ScheduledJobDetails
                    {
                        JobId          = Guid.NewGuid(),
                        JobType        = ScheduledJobType.ApplyReferralReward,
                        JobDescription = userId,
                        Orchestrated   = true,
                        StartTime      = DateTime.UtcNow,
                        Payload        = payload
                    };
                    scheduler.ScheduleJobAsync(scheduledJobDetails).Wait();
                }
            }
        }
예제 #10
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));
        }
예제 #11
0
 public MainController(
     UserFactory userFactory,
     PartnerFactory partnerFactory,
     PartnerActionMapFactory partnerActionMapFactory,
     UsedActionFactory usedActionFactory
     )
 {
     _userFactory             = userFactory;
     _partnerFactory          = partnerFactory;
     _partnerActionMapFactory = partnerActionMapFactory;
     _usedActionFactory       = usedActionFactory;
 }
예제 #12
0
        /// <summary>
        /// Initializes a new instance of the CommerceWorker class.
        /// </summary>
        public CommerceWorker()
        {
            ExemptConfigurationItems = new[] { ProcessJobsPropertyKey };
            Scheduler = PartnerFactory.Scheduler(CommerceWorkerConfig.Instance.SchedulerQueueName,
                                                 CommerceWorkerConfig.Instance.SchedulerTableName,
                                                 CommerceWorkerConfig.Instance);

            // Register Analytics Service
            Analytics.Initialize(CommerceWorkerConfig.Instance);

#if IntDebug || IntRelease
            ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
#endif
        }
예제 #13
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);
        }
예제 #14
0
 /// <summary>
 /// Adds an item to the analytics
 /// </summary>
 /// <param name="analyticsAction">
 /// Type of action
 /// </param>
 /// <param name="trackingId">
 /// Tracking Id for the request, AKA Event ID.
 /// </param>
 /// <param name="userId">
 /// Id of the user performing the action
 /// </param>
 /// <param name="dealId">
 /// Id of the deal
 /// </param>
 /// <param name="partnerDealId">
 /// Partner Deal Id
 /// </param>
 public static void Add(AnalyticsAction analyticsAction,
                        Guid trackingId,
                        Guid dealId = default(Guid))
 {
     if (CommerceServiceConfig.Instance.EnableServiceHealth == true)
     {
         AnalyticsClient.AnalyticsItem item = new AnalyticsClient.AnalyticsItem
         {
             Action   = analyticsAction.ToString(),
             ClientId = AnalyticsClientId,
             EventId  = trackingId,
             DealId   = dealId,
         };
         PartnerFactory.AnalyticsClient(CommerceServiceConfig.Instance).Add(item);
     }
 }
예제 #15
0
        /// <summary>
        /// Adds a user registration event to the analytics.
        /// </summary>
        /// <param name="userId">
        /// The canonical user ID to associate with the event.
        /// </param>
        /// <param name="registerUserEventId">
        /// The ID of this register user event.
        /// </param>
        /// <param name="correlationId">
        /// The ID of the event to which this event will be correlated.
        /// </param>
        /// <param name="referrer">
        /// The referrer to which to associate the registration of this user, if any.
        /// </param>
        public static void AddRegisterUserEvent(Guid userId,
                                                Guid registerUserEventId,
                                                Guid correlationId,
                                                string referrer)
        {
            if (CommerceServiceConfig.Instance.EnableServiceHealth == true)
            {
                AnalyticsClient.AnalyticsItem item = new AnalyticsClient.AnalyticsItem
                {
                    Action           = AnalyticsClient.Actions.Actions.RegisterUser,
                    UserId           = GetAnidFromUserId(userId, CommerceServiceConfig.Instance),
                    ClientId         = AnalyticsClientId,
                    EventId          = registerUserEventId,
                    ParentEventId    = correlationId,
                    ReferrerClientId = referrer
                };

                PartnerFactory.AnalyticsClient(CommerceServiceConfig.Instance).Add(item);
            }
        }
예제 #16
0
        /// <summary>
        /// Adds an add card event to the analytics.
        /// </summary>
        /// <param name="userId">
        /// The canonical user ID to associate with the event.
        /// </param>
        /// <param name="addCardEventId">
        /// The ID of this add card event.
        /// </param>
        /// <param name="correlationId">
        /// The ID of the event to which this event will be correlated.
        /// </param>
        /// <param name="referrer">
        /// The referrer to which to associate the adding of this card, if any.
        /// </param>
        public static void AddAddCardEvent(Guid userId,
                                           Guid addCardEventId,
                                           Guid correlationId,
                                           string referrer)
        {
            if (CommerceServiceConfig.Instance.EnableServiceHealth == true)
            {
                AnalyticsClient.AnalyticsItem item = new AnalyticsClient.AnalyticsItem
                {
                    Action           = AnalyticsAction.AddCard.ToString(),
                    UserId           = userId.ToString(),
                    ClientId         = AnalyticsClientId,
                    EventId          = addCardEventId,
                    ParentEventId    = correlationId,
                    ReferrerClientId = referrer
                };

                PartnerFactory.AnalyticsClient(CommerceServiceConfig.Instance).Add(item);
            }
        }
예제 #17
0
        /// <summary>
        /// Adds a redemption event to the analytics.
        /// </summary>
        /// <param name="userId">
        /// The canonical user ID to associate with the event.
        /// </param>
        /// <param name="redemptionEventId">
        /// The ID of this redemption event.
        /// </param>
        /// <param name="correlationId">
        /// The ID of the event to which this event will be correlated.
        /// </param>
        /// <param name="dealId">
        /// The ID of the deal being redeemed.
        /// </param>
        /// <param name="currency">
        /// The currency involved in the transaction in which the deal was redeeemed.
        /// </param>
        /// <param name="authorizationAmount">
        /// The authorization amount in the transaction in which the deal was redeemed.
        /// </param>
        /// <param name="discountAmount">
        /// The discount amount from the transaction in which the deal was redeemed.
        /// </param>
        /// <param name="discountId">
        /// The ID of the discount within the deal that was specifically redeemed.
        /// </param>
        /// <param name="partnerMerchantId">
        /// The ID of the merchant as assigned by the processing partner.
        /// </param>
        /// <param name="config">
        /// Optional configuration
        /// </param>
        public static void AddRedemptionEvent(Guid userId,
                                              Guid redemptionEventId,
                                              Guid correlationId,
                                              Guid dealId,
                                              string currency,
                                              int authorizationAmount,
                                              int discountAmount,
                                              Guid discountId,
                                              string partnerMerchantId,
                                              CommerceConfig config = null)
        {
            if (config == null)
            {
                config = CommerceServiceConfig.Instance;
            }

            if (config.EnableServiceHealth == true)
            {
                AnalyticsClient.Payloads.DealRedemptionPayload payload = new AnalyticsClient.Payloads.DealRedemptionPayload
                {
                    AuthorizationAmount = authorizationAmount,
                    Currency            = currency,
                    DiscountAmount      = discountAmount,
                    DiscountId          = discountId,
                    PartnerMerchantId   = partnerMerchantId
                };

                AnalyticsClient.AnalyticsItem item = new AnalyticsClient.AnalyticsItem
                {
                    Action        = AnalyticsClient.Actions.Actions.RedeemedDeal,
                    UserId        = GetAnidFromUserId(userId, config),
                    ClientId      = AnalyticsClientId,
                    EventId       = redemptionEventId,
                    ParentEventId = correlationId,
                    DealId        = dealId,
                    JPayload      = JObject.FromObject(payload)
                };

                PartnerFactory.AnalyticsClient(config).Add(item);
            }
        }
예제 #18
0
        /// <summary>
        /// Process the rewards if applicable
        /// </summary>
        /// <param name="settlementDetail">
        /// Settlement Details
        /// </param>
        /// <returns>
        /// Async Task Wrapper
        /// </returns>
        internal async Task ProcessRewardPayoutAsync(SettlementDetail settlementDetail)
        {
            if (settlementDetail.TransactionType == TransactionType.SettlementRedemption)
            {
                // First add a redemption reward to the redeeming user if they're enabled.
                if (EnableRedemptionRewards == true && WorkerActions.RewardRedemption(RewardOperations, Context) == ResultCode.Success)
                {
                    // Add job to process the reward payout. Note that this job will be scheduled 30 minutes in the
                    // future to guard against applying a reward for a transaction that was reversed in a later
                    // record.
                    ConcurrentDictionary <string, string> payload = new ConcurrentDictionary <string, string>();
                    payload[Key.RewardPayoutId.ToString()]        = ((Guid)Context[Key.RewardPayoutId]).ToString();
                    payload[Key.PartnerCardId.ToString()]         = (string)Context[Key.PartnerCardId];
                    payload[Key.PartnerRedeemedDealId.ToString()] = settlementDetail.TransactionId;

                    IScheduler scheduler = PartnerFactory.Scheduler(CommerceWorkerConfig.Instance.SchedulerQueueName,
                                                                    CommerceWorkerConfig.Instance.SchedulerTableName,
                                                                    CommerceWorkerConfig.Instance);
                    ScheduledJobDetails scheduledJobDetails = new ScheduledJobDetails
                    {
                        JobId          = Guid.NewGuid(),
                        JobType        = ScheduledJobType.ApplyRedemptionReward,
                        JobDescription = settlementDetail.ConsumerId,
                        Orchestrated   = true,
                        StartTime      = DateTime.UtcNow.AddMinutes(30),
                        Payload        = payload
                    };

                    await scheduler.ScheduleJobAsync(scheduledJobDetails).ConfigureAwait(false);
                }

                // Then add a referred redemption reward to the user who referred the redeeming user.
                WorkerActions.RewardReferredRedemption(RewardOperations, Context);
            }
            else
            {
                Context.Log.Verbose("No Bing Reward can be given for a reversed transaction.");
            }
        }
예제 #19
0
        /// <summary>
        /// Queues claiming already claimed deals for the new card.
        /// </summary>
        /// <param name="response">
        /// The AddCardResponse being built.
        /// </param>
        private void QueueClaimingDeals(AddCardResponse response)
        {
            Context.Log.Verbose("Queueing claiming user's existing claimed deals for the new card.");
            string userId = ((User)Context[Key.User]).GlobalId.ToString();
            ConcurrentDictionary <string, string> payload = new ConcurrentDictionary <string, string>();

            payload[Key.GlobalUserId.ToString()] = userId;
            payload[Key.CardId.ToString()]       = General.IntegerFromGuid(response.NewCardId).ToString();
            ScheduledJobDetails scheduledJobDetails = new ScheduledJobDetails
            {
                JobId          = Guid.NewGuid(),
                JobType        = ScheduledJobType.ClaimDiscountsForNewCard,
                JobDescription = userId,
                Orchestrated   = true,
                StartTime      = DateTime.UtcNow,
                Payload        = payload
            };
            IScheduler scheduler = PartnerFactory.Scheduler(CommerceServiceConfig.Instance.SchedulerQueueName,
                                                            CommerceServiceConfig.Instance.SchedulerTableName,
                                                            CommerceServiceConfig.Instance);

            scheduler.ScheduleJobAsync(scheduledJobDetails).Wait();
        }
예제 #20
0
        /// <summary>
        /// Adds a log entry to the log table
        /// </summary>
        /// <param name="requestId">
        /// Request Id that uniquely identifies the service request to the commerce server
        /// </param>
        /// <param name="resultCode">
        /// Result code of the service invocation
        /// </param>
        /// <param name="resultSummary">
        /// Summary explanation of the result code
        /// </param>
        /// <param name="config">
        /// The configuration to use to get settings.
        /// </param>
        public static void Add(Guid requestId,
                               ResultCode resultCode,
                               string resultSummary,
                               CommerceConfig config)
        {
            try
            {
                if (config == null)
                {
                    throw new ArgumentNullException("config", "Parameter config cannot be null.");
                }

                if (config.EnableServiceHealth == true)
                {
                    DashboardLogEntity logEntity = new DashboardLogEntity(requestId, resultCode.ToString(), resultSummary);
                    PartnerFactory.AzureTable(config).InsertEntity(logEntity);
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception, string.Format("Unable to log the data to azure table : {0} ", exception.Message));
            }
        }
        /// <summary>
        /// Process the transaction log file
        /// </summary>
        /// <returns>
        /// Async Task Wrapper
        /// </returns>
        public async Task Process()
        {
            TransactionLogParser transactionLogParser = new TransactionLogParser(Context.Log);
            TransactionLogFile   transactionLogFile   = transactionLogParser.Parse(TransactionLogFileName, TransactionLogFileStream);

            if (transactionLogFile != null)
            {
                foreach (TransactionLogDetail detail in transactionLogFile.TransactionLogRecords)
                {
                    // block the reversed transactions
                    if (TransactionIdSet.Contains(detail.TransactionId) || detail.TransactionAmount <= 0)
                    {
                        continue;
                    }

                    TransactionIdSet.Add(detail.TransactionId);

                    // 1. process the detail record here -> Insert as redeemed deal
                    RedeemedDeal redeemedDeal = new RedeemedDeal()
                    {
                        AnalyticsEventId = Guid.NewGuid()
                    };
                    Context[Key.RedeemedDeal] = redeemedDeal;
                    MarshalRedeemDeal(detail);
                    ResultCode result = RedeemedDealOperations.AddRedeemedDeal();

                    //2. If the record was processed successfully, attempt to add a redemption reward if applicable and analytics
                    if (result == ResultCode.Created)
                    {
                        RedeemedDealInfo redemptionInfo = (RedeemedDealInfo)Context[Key.RedeemedDealInfo];
                        // First add a redemption reward to the redeeming user if they're enabled.
                        if (EnableRedemptionRewards)
                        {
                            if (WorkerActions.RewardRedemption(RewardOperations, Context) == ResultCode.Success)
                            {
                                // Add job to process the reward payout.
                                ConcurrentDictionary <string, string> payload = new ConcurrentDictionary <string, string>();
                                payload[Key.RewardPayoutId.ToString()]        = ((Guid)Context[Key.RewardPayoutId]).ToString();
                                payload[Key.PartnerCardId.ToString()]         = (string)Context[Key.PartnerCardId];
                                payload[Key.PartnerRedeemedDealId.ToString()] = redemptionInfo.PartnerRedeemedDealId;

                                IScheduler scheduler = PartnerFactory.Scheduler(CommerceWorkerConfig.Instance.SchedulerQueueName,
                                                                                CommerceWorkerConfig.Instance.SchedulerTableName,
                                                                                CommerceWorkerConfig.Instance);
                                ScheduledJobDetails scheduledJobDetails = new ScheduledJobDetails
                                {
                                    JobId          = Guid.NewGuid(),
                                    JobType        = ScheduledJobType.ApplyRedemptionReward,
                                    JobDescription = redemptionInfo.GlobalUserId.ToString(),
                                    Orchestrated   = true,
                                    Payload        = payload
                                };

                                await scheduler.ScheduleJobAsync(scheduledJobDetails).ConfigureAwait(false);
                            }
                        }

                        // Then add a referred redemption reward to the user who referred the redeeming user.
                        Context[Key.RedeemedDealId] = ((RedeemedDeal)Context[Key.RedeemedDeal]).Id;
                        Context[Key.GlobalUserId]   = ((RedeemedDealInfo)Context[Key.RedeemedDealInfo]).GlobalUserId;
                        WorkerActions.RewardReferredRedemption(RewardOperations, Context);

                        // Update analytics.
                        // For FDC this happens at AUTH time
                        // Butfor Amex flow, we put analytics at the time of Transaction File Processing
                        SharedUserLogic sharedUserLogic = new SharedUserLogic(Context,
                                                                              CommerceOperationsFactory.UserOperations(Context));
                        Context[Key.GlobalUserId] = redemptionInfo.GlobalUserId;
                        User user = sharedUserLogic.RetrieveUser();
                        Analytics.AddRedemptionEvent(redemptionInfo.GlobalUserId, redeemedDeal.AnalyticsEventId, user.AnalyticsEventId,
                                                     redemptionInfo.ParentDealId, redemptionInfo.Currency,
                                                     redeemedDeal.AuthorizationAmount, redemptionInfo.DiscountAmount,
                                                     redemptionInfo.GlobalId, (string)Context[Key.PartnerMerchantId],
                                                     CommerceWorkerConfig.Instance);
                    }
                }
            }
        }
예제 #22
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);
        }
예제 #23
0
        /// <summary>
        /// Creates the appropriate Log instance for the current environment.
        /// </summary>
        /// <param name="deploymentId">
        /// * If running in Azure, the ID of the deployment that created the VM hosting this application
        /// * Else null.
        /// </param>
        /// <param name="instanceId">
        /// * If running in Azure, the ID of the role instance currently hosting this application
        /// * Else null.
        /// </param>
        /// <param name="traceListener">
        /// * If running in Azure, the Azure diagnostic monitor trace listener to use for the log.
        /// * Else null.
        /// </param>
        /// <param name="logVerbosity">
        /// Specified the verbosity level for entries to commit to the log.
        /// </param>
        /// <param name="forceEventLog">
        /// Specifies whether to force use of the event log instead of other logging mechanisms.
        /// </param>
        /// <param name="source">
        /// The source under which to log events.
        /// </param>
        /// <param name="configuration">
        /// The configuration to use to get settings.
        /// </param>
        internal static void CreateLogInstance(string deploymentId,
                                               string instanceId,
                                               TraceListener traceListener,
                                               SourceLevels logVerbosity,
                                               bool forceEventLog,
                                               string source,
                                               CommerceConfig configuration)
        {
            lock (CreateLogInstanceLock)
            {
                string dashboardConnectionKey;

                if (General.RunningInAzure == true)
                {
                    // If the event log is not set to be used regardless of environment, create a TraceLog and a listener to
                    // funnel log entries into central storage.
                    if (forceEventLog == false)
                    {
                        Log.Instance = new TraceLog(new List <TraceListener> {
                            traceListener
                        }, logVerbosity)
                        {
                            Source = source
                        };
                    }
                    else
                    {
                        Log.Instance = new EventLogLog(logVerbosity)
                        {
                            Source = source
                        };
                    }

                    // Set the server ID to a value useful in Azure.
                    RequestInformationExtensions.ServerId = String.Format("{0}_{1}", deploymentId,
                                                                          instanceId.Substring(instanceId.LastIndexOf("_", StringComparison.OrdinalIgnoreCase) + 1));

                    dashboardConnectionKey = CloudConfigurationManager.GetSetting(DashboardConnectionString);
                }
                else
                {
                    Log.Instance = new EventLogLog(logVerbosity)
                    {
                        Source = source
                    };

                    dashboardConnectionKey = ConfigurationManager.AppSettings[DashboardConnectionString];
                }

                // Initialize the dashboard table into which a subset of warnings will be logged.
                if (String.IsNullOrEmpty(dashboardConnectionKey) == false)
                {
                    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(dashboardConnectionKey);
                    PartnerFactory.AzureTable(configuration).Initialize(cloudStorageAccount, DashboardTableName,
                                                                        new ExponentialRetry(TimeSpan.FromSeconds(5), 1));
                }

                // Flag log instance as set so CommerceLog won't attempt to create one.
                CommerceLog.LogInstanceSet = true;
                CommerceLog.Config         = configuration;
            }
        }
 public PartnerActionMapController(PartnerActionMapFactory factory, PartnerFactory partnerFactory, ActionFactory actionFactory)
 {
     _factory        = factory;
     _partnerFactory = partnerFactory;
     _actionFactory  = actionFactory;
 }
예제 #25
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);
            }
        }
예제 #26
0
 public PartnersController(PartnerFactory partnerFactory, PartnerTypeFactory partnerTypeFactory)
 {
     _partnerFactory     = partnerFactory;
     _partnerTypeFactory = partnerTypeFactory;
 }
예제 #27
0
 /// <summary>
 /// Initializes the Analytics client
 /// </summary>
 /// <param name="commerceConfig">
 /// The CommerceConfig object to use during initialization.
 /// </param>
 public static void Initialize(CommerceConfig commerceConfig)
 {
     PartnerFactory.AnalyticsClient(commerceConfig).Initialize("commerce");
 }
예제 #28
0
 public UsersController(UserFactory userFactory, PartnerFactory partnerFactory, AspNetUserManager <DiscountsUser> userManager)
 {
     _userFactory    = userFactory;
     _partnerFactory = partnerFactory;
     _userManager    = userManager;
 }