public virtual PaymentStatisticIncrement GetPaymentChanges(PaymentStatusChangedMessage paymentChangeMsg)
        {
            if (paymentChangeMsg.StatusHistory.Count == 0)
            {
                return(null);
            }

            var increment = new PaymentStatisticIncrement();

            // Update total count
            PopulatePaymentTotalCounters(increment, paymentChangeMsg);

            //skip message processing if current status is intermediate
            if (!IsCurrentStatusFinal(paymentChangeMsg))
            {
                return(increment);
            }

            var sortedStatusHistories =
                paymentChangeMsg.StatusHistory.OrderByDescending(x => x.PaymentStatusHistoryId).ToList();

            var currentStatus = sortedStatusHistories[0].Status;

            var previousFinalStatusHistory = sortedStatusHistories.Skip(1).FirstOrDefault(x => x.Status.IsFinal());
            var previousStatus             = previousFinalStatusHistory?.Status;

            // Here we work with payments in final status ONLY!
            PopulatePaymentSuccessCounters(increment, paymentChangeMsg, previousStatus, currentStatus);

            return(increment);
        }
        private static void PopulatePaymentSuccessCounters(PaymentStatisticIncrement increment,
                                                           PaymentStatusChangedMessage paymentChangeMsg, string previousStatus, string currentStatus)
        {
            var sign = CalculatePaymentStatusIncrementSign(previousStatus, currentStatus);

            increment.SuccessCount   += sign;
            increment.SuccessSum     += sign * paymentChangeMsg.Payment.PaymentAmount;
            increment.SuccessBaseSum += sign * paymentChangeMsg.Payment.BaseAmount;
        }
        public virtual PaymentStatisticIncrement GetPaymentChanges(PaymentStatusChangedMessage paymentChangeMsg)
        {
            var increment = new PaymentStatisticIncrement();

            //skip message processing if current status is intermediate
            if (!IsCurrentStatusFinal(paymentChangeMsg))
            {
                return(increment);
            }

            // Update total count
            PopulatePaymentTotalCounters(increment, paymentChangeMsg);

            var currentStatus = paymentChangeMsg.Status;

            // Here we work with payments in final status ONLY!
            PopulatePaymentSuccessCounters(increment, paymentChangeMsg, currentStatus);

            return(increment);
        }
        /// <summary>
        /// Checks whether current status in message is final.
        /// </summary>
        /// <param name="message"><see cref="PaymentStatusChangedMessage"/>The event message.</param>
        /// <returns>Value indicating whether status is final.</returns>
        private static bool IsCurrentStatusFinal(PaymentStatusChangedMessage message)
        {
            var currentStatus = message.StatusHistory.OrderBy(x => x.PaymentStatusHistoryId).Last().Status;

            return(currentStatus.IsFinal());
        }
 private static void PopulatePaymentTotalCounters(PaymentStatisticIncrement increment, PaymentStatusChangedMessage paymentChangeMsg)
 {
     if (paymentChangeMsg.StatusHistory.Count == 1)
     {
         increment.TotalCount++;
         increment.TotalSum     = paymentChangeMsg.Payment.PaymentAmount;
         increment.TotalBaseSum = paymentChangeMsg.Payment.BaseAmount;
     }
 }
        private static bool IsCurrentStatusFinal(PaymentStatusChangedMessage message)
        {
            var currentStatus = message.Status;

            return(currentStatus.IsFinal());
        }
Ejemplo n.º 7
0
        static void GenerateMessages(IActorRef actor)
        {
            var randomizer = new Random();
            var paymentId  = 1;

            var listOfCustomers =
                new List <string>
            {
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString()
            };

            System.Threading.Tasks.Parallel.ForEach(listOfCustomers, cust =>
            {
                var listOfPaymentRefs =
                    new List <string>
                {
                    Guid.NewGuid().ToString(),
                    Guid.NewGuid().ToString(),
                    Guid.NewGuid().ToString(),
                    Guid.NewGuid().ToString(),
                    Guid.NewGuid().ToString()
                };

                foreach (var pr in listOfPaymentRefs)
                {
                    var methodAction = randomizer.Next(1, 3);
                    var createdDate  = DateTime.Now;
                    var randAmount   = randomizer.Next(100);
                    var currencyCode = "EUR";
                    var fee          = randAmount * 0.05m;

                    var randomStatuses = new List <string> {
                        "Authorized", "Captured", "Error", "Refused"
                    };
                    var statusIndex = randomizer.Next(4);
                    var statuses    = new List <string> {
                        "Created", "InProcess", randomStatuses[statusIndex]
                    };

                    foreach (var st in statuses)
                    {
                        System.Threading.Thread.Sleep(randomizer.Next(100, 500));

                        var paymentMessage =
                            new PaymentStatusChangedMessage("test.com",
                                                            new PaymentInformation(currencyCode, randAmount, currencyCode, randAmount, currencyCode, fee, "VISA", randAmount, randAmount, createdDate, DateTime.Now, pr, pr, paymentId, currencyCode, methodAction),
                                                            new CustomerInformation("GB", cust), st);

                        actor.Tell(paymentMessage);

                        Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(paymentMessage));
                    }
                }

                paymentId++;
            });
        }