/// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private ResultCode GetEarnBurnHistory()
        {
            List <EarnBurnTransactionItemDataContract> historyItems = new List <EarnBurnTransactionItemDataContract>();

            // Retrieve list of redemption history items.
            Context.Log.Verbose("Retrieving earn/burn redemption history belonging to user {0}", ((User)Context[Key.User]).GlobalId);
            IEnumerable <RedemptionHistoryItem> items = RedemptionHistoryOperations.RetrieveMicrosoftEarnRedemptionHistory();

            Context.Log.Verbose("{0} earn/burn redemption history entries were retrieved from the data store.", items.Count());

            double totalEarnedCredits = 0;
            double totalBurnedCredits = 0;

            // If any redemption history items were returned, add them to the context.
            if (items.Any())
            {
                // Build the list of HistoryItemDataContracts from the list of redemption history items.
                foreach (RedemptionHistoryItem redemptionHistoryItem in items)
                {
                    double discountAmount = ((double)redemptionHistoryItem.DiscountAmount / 100);

                    CreditStatus creditStatus = redemptionHistoryItem.CreditStatus;
                    historyItems.Add(new EarnBurnTransactionItemDataContract()
                    {
                        RedemptionType  = this.GetRedemptionTypeForEarnBurn(redemptionHistoryItem.ReimbursementTender),
                        MerchantName    = redemptionHistoryItem.MerchantName,
                        DiscountSummary = redemptionHistoryItem.DiscountSummary,
                        DealPercent     = redemptionHistoryItem.DealPercent,
                        EventDateTime   = redemptionHistoryItem.EventDateTime,
                        EventAmount     = string.Format("{0:C}", ((double)redemptionHistoryItem.EventAmount / 100)),
                        Reversed        = redemptionHistoryItem.Reversed,
                        CreditStatus    = creditStatus.ToString(),
                        DiscountAmount  = string.Format("{0:C}", discountAmount),
                        LastFourDigits  = redemptionHistoryItem.LastFourDigits,
                        CardBrand       = redemptionHistoryItem.CardBrand.ToString()
                    });

                    //Consider only the settled earn transactions to calculate total earned credits
                    //For calculating burned credits, include both cleared and pending transaction
                    if (redemptionHistoryItem.ReimbursementTender == ReimbursementTender.MicrosoftEarn && creditStatus == CreditStatus.CreditGranted)
                    {
                        totalEarnedCredits += discountAmount;
                    }
                    else if (redemptionHistoryItem.ReimbursementTender == ReimbursementTender.MicrosoftBurn)
                    {
                        totalBurnedCredits += discountAmount;
                    }
                }
            }

            double availableCredits = (totalEarnedCredits > totalBurnedCredits) ? (totalEarnedCredits - totalBurnedCredits) : 0;

            GetEarnBurnTransactionHistoryResponse response = (GetEarnBurnTransactionHistoryResponse)Context[Key.Response];

            response.RedemptionHistory = historyItems;
            response.CreditBalance     = string.Format("{0:C}", availableCredits);

            return(ResultCode.Success);
        }
예제 #2
0
        //To be used only by certain apis.
        public static async Task <string> GetTotalEarnedAmount(UserModel user, string secureToken)
        {
            try
            {
                string key          = "earn_amount:" + user.UserId;
                string earnedAmount = HttpRuntime.Cache.Get(key) as string;
                if (earnedAmount != null)
                {
                    return(earnedAmount);
                }

                GetEarnBurnTransactionHistoryResponse history = await GetTransactionHistory(user, secureToken);

                HttpRuntime.Cache.Insert(key, history.CreditBalance, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10));
                return(history.CreditBalance);
            }
            catch (Exception)
            {
                return("N/A");
            }
        }
예제 #3
0
        public static async Task <GetEarnBurnTransactionHistoryResponse> GetTransactionHistory(UserModel user, string secureToken)
        {
            string key = "trans_history:" + user.UserId;

            GetEarnBurnTransactionHistoryResponse response = HttpRuntime.Cache.Get(key) as GetEarnBurnTransactionHistoryResponse;

            if (response != null)
            {
                return(response);
            }

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("X-FD-BingIDToken", secureToken);
                client.Timeout = TimeSpan.FromMilliseconds(5000);
                client.DefaultRequestHeaders.Add("X-Flight-ID", "Earn");
                string data = await client.GetStringAsync("https://commerce.earnbymicrosoft.com/api/commerce/redemptionhistory/getearnhistory");

                response = JsonConvert.DeserializeObject <GetEarnBurnTransactionHistoryResponse>(data);
                HttpRuntime.Cache.Insert(key, response, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5));
                return(response);
            }
        }
예제 #4
0
        public async Task <ActionResult> History()
        {
            try
            {
                LiveIdAuthResult    liveIdAuthResult = HttpContext.Items["liveauthstate"] as LiveIdAuthResult;
                AuthorizeUserResult userResult       = await AuthorizeUser(liveIdAuthResult);

                if (!userResult.Authorized)
                {
                    return(userResult.Result);
                }

                if (liveIdAuthResult != null)
                {
                    ViewBag.ProfileName     = liveIdAuthResult.ProfileName;
                    ViewBag.SignOutHtmlLink = liveIdAuthResult.SignOutHtmlLink;
                }

                string    state       = (HttpContext.Items["state"] as string) ?? "wa";
                string    revIpHeader = HttpContext.Request.Headers["X-FD-RevIP"];
                string    secureToken = HttpContext.Items["backendtoken"] as string;
                UserModel userModel   = new UserModel(User.Identity as ClaimsIdentity);
                GetEarnBurnTransactionHistoryResponse response = await CommerceService.GetTransactionHistory(userModel, secureToken);

                if (response != null && response.RedemptionHistory != null)
                {
                    AccountsPageModel pageModel = new AccountsPageModel
                    {
                        RedemptionHistory        = response.RedemptionHistory.Where(x => x.CreditStatus == "CreditGranted").ToList(),
                        EarnTotal                = response.CreditBalance,
                        PendingRedemptionHistory = response.RedemptionHistory.Where(x => x.CreditStatus == "AuthorizationReceived" || x.CreditStatus == "ClearingReceived" || x.CreditStatus == "StatementCreditRequested").ToList(),
                        UserId = userModel.UserId,
                        Page   = "history"
                    };

                    List <EarnBurnTransactionItemDataContract> removeList = new List <EarnBurnTransactionItemDataContract>();
                    if (pageModel.PendingRedemptionHistory.Count > 0)
                    {
                        foreach (EarnBurnTransactionItemDataContract item in pageModel.PendingRedemptionHistory)
                        {
                            List <EarnBurnTransactionItemDataContract> result = pageModel.RedemptionHistory.Where(x =>
                                                                                                                  x.CardBrand == item.CardBrand &&
                                                                                                                  x.EventDateTime == item.EventDateTime &&
                                                                                                                  x.RedemptionType == item.RedemptionType &&
                                                                                                                  x.MerchantName == item.MerchantName &&
                                                                                                                  x.LastFourDigits == item.LastFourDigits &&
                                                                                                                  x.EventAmount == item.EventAmount &&
                                                                                                                  x.DiscountAmount == item.DiscountAmount).ToList();

                            if (result != null && result.Count > 0)
                            {
                                removeList.AddRange(result);
                            }
                        }

                        if (removeList.Count > 0)
                        {
                            foreach (EarnBurnTransactionItemDataContract item in removeList)
                            {
                                pageModel.PendingRedemptionHistory.RemoveAll(x =>
                                                                             x.EventDateTime == item.EventDateTime &&
                                                                             x.EventAmount == item.EventAmount &&
                                                                             x.RedemptionType == item.RedemptionType &&
                                                                             x.DiscountAmount == item.DiscountAmount &&
                                                                             x.LastFourDigits == item.LastFourDigits &&
                                                                             x.CardBrand == item.CardBrand &&
                                                                             x.MerchantName == item.MerchantName);
                            }
                        }

                        if (pageModel.PendingRedemptionHistory != null && pageModel.PendingRedemptionHistory.Count > 0)
                        {
                            foreach (var pendingTransaction in pageModel.PendingRedemptionHistory)
                            {
                                if (pendingTransaction.MerchantName.Trim() == "Shell")
                                {
                                    pendingTransaction.DiscountAmount = "TBD";
                                    pendingTransaction.EventAmount    = "TBD";
                                }
                            }
                        }
                    }

                    return(View("~/offers/earn/views/account/history.cshtml", pageModel));
                }
            }
            catch (Exception e)
            {
            }

            return(HandleServerError());
        }