public Guid Save(PaymentNotificationResponse entity)
        {

            PaymentNotificationResponse existingEntity = _ctx.AsynchronousPaymentNotificationResponse.FirstOrDefault
                                                               (n => n.Id == entity.Id);

            if (existingEntity == null)
            {
                existingEntity = new PaymentNotificationResponse();
                existingEntity.ClientRequestResponseType = ClientRequestResponseType.AsynchronousPaymentNotification;
                existingEntity.DateCreated = DateTime.Now;
                existingEntity.TimeStamp = DateTime.Now;
                existingEntity.Id = entity.Id;
                _ctx.AsynchronousPaymentNotificationResponse.Add(existingEntity);
            }
           // existingEntity.PaymentNotificationDetails
            foreach (var item in entity.PaymentNotificationDetails)
            {
                var i =_ctx.PaymentNotificationListItems.FirstOrDefault(s => s.Id == item.Id);
                if (i == null)
                {
                    i= new PaymentNotificationListItem();
                    i.Id = item.Id;
                    _ctx.PaymentNotificationListItems.Add(i);
                }
                i.BalanceDue = item.BalanceDue;
                i.PaidAmount = item.PaidAmount;
                i.Status = item.Status;
                i.TimeStamp = item.TimeStamp;
                i.TotalAmount = item.TotalAmount;
                i.ResponseId = existingEntity.Id;
            }
            existingEntity.DistributorCostCenterId = entity.DistributorCostCenterId;
            existingEntity.TransactionRefId = entity.TransactionRefId;
            existingEntity.BalanceDue = entity.BalanceDue;
            existingEntity.Currency = entity.Currency;
            existingEntity.PaidAmount = entity.PaidAmount;
            existingEntity.SDPReferenceId = entity.SDPReferenceId;
          
            //_ctx.AsynchronousPaymentNotificationResponse.Add(existingEntity);
            _ctx.SaveChanges();
            return existingEntity.Id;

        }
        PaymentInfo Map(PaymentNotificationListItem detail, ReceiptLineItem item, PaymentNotificationResponse paymentNotif)
        {
            var info = new PaymentInfo
                           {
                               Amount = (decimal) detail.PaidAmount,
                               Id = Guid.NewGuid(),
                               PaymentModeUsed = PaymentMode.MMoney,
                               IsConfirmed = true,
                               IsNew = true,
                               MMoneyPaymentType = item.MMoneyPaymentType,
                               PaymentRefId = item.PaymentRefId,
                               PaymentTypeDisplayer = item.MMoneyPaymentType,
                               NotificationId = detail.Id + ";",
                               Description = MMoneyDescription((decimal) detail.PaidAmount,
                                                               paymentNotif.Currency,
                                                               paymentNotif.SubscriberId,
                                                               paymentNotif.AccountId, "",
                                                               paymentNotif.SDPReferenceId)
                           };

            return info;
        }
        public ClientRequestResponseBase ProcessClientPaymentNotificationRequest(ClientRequestResponseBase crrReqMsg)
        {
            ClientRequestResponseBase response = new ClientRequestResponseBase();

            try
            {
                IEnumerable<ClientRequestResponseBase> sdpRequests = new List<ClientRequestResponseBase>();
                if (crrReqMsg is PaymentNotificationRequest)
                {
                    sdpRequests = _paymentRequestRepository.GetByTransactionRefId(crrReqMsg.TransactionRefId).OfType
                        <PaymentNotificationRequest>().ToList();//.FirstOrDefault();

                    if (sdpRequests.Count() > 0)
                    {
                        var any = sdpRequests.OrderBy(n => ((PaymentNotificationRequest)n).SDPTimeStamp).FirstOrDefault(s=>!string.IsNullOrEmpty(s.TransactionRefId)) as PaymentNotificationRequest;

                        var paymentResp = _paymentRequestRepository.GetByTransactionRefId(crrReqMsg.TransactionRefId).OfType
                                <PaymentRequest>().FirstOrDefault();

                        var paymentInfoDetails = new List<PaymentNotificationListItem>();
                        foreach (var re in sdpRequests)
                        {
                            var apnr = re as PaymentNotificationRequest;
                           
                            var detail = new PaymentNotificationListItem
                                            {
                                                Id          = apnr.Id,
                                                PaidAmount  = apnr.SDPPaidAmount,
                                                TotalAmount = apnr.SDPTotalAmount,
                                                BalanceDue  = apnr.SDPBalanceDue,
                                                TimeStamp   = apnr.SDPTimeStamp,
                                                Status      = apnr.SDPStatusCode +"; "+apnr.SDPStatusDetail,
                                                ResponseId = apnr.Id
                                                
                                            };
                            paymentInfoDetails.Add(detail);
                        }
                        response = new PaymentNotificationResponse
                                                        {
                                                            Id                        = any.Id,
                                                            TransactionRefId = any.TransactionRefId,
                                                            DistributorCostCenterId   = any.DistributorCostCenterId,
                                                            ClientRequestResponseType = any.ClientRequestResponseType,

                                                            SDPReferenceId             = any.SDPReferenceId,
                                                            SDPTransactionRefId        = any.SDPTransactionRefId,
                                                            Currency                   = any.SDPCurrency,
                                                            StatusCode                 = any.SDPStatusCode,
                                                            StatusDetail               = any.SDPStatusDetail,
                                                            DateCreated                = any.DateCreated,
                                                            PaymentNotificationDetails = paymentInfoDetails,
                                                            AccountId                  = paymentResp.AccountId,
                                                            SubscriberId               = paymentResp.SubscriberId,
                                                            Items = paymentInfoDetails,
                                                        };

                        return response;
                    }
                    goto pending;
                }

                #region BuyGoodsNotificationRequest
                if (crrReqMsg is BuyGoodsNotificationRequest)
                {
                    var sdpBgRequests = _paymentRequestRepository.GetByReceiptNumber(crrReqMsg.TransactionRefId).OfType
                        <BuyGoodsNotificationRequest>().LastOrDefault();
                    if (sdpBgRequests == null)
                        goto pending;
                    BuyGoodsNotificationRequest bgnr = sdpBgRequests as BuyGoodsNotificationRequest;
                    response = new BuyGoodsNotificationResponse
                                   {
                                       Id = bgnr.Id,
                                       ClientRequestResponseType = ClientRequestResponseType.BuyGoodsNotification,
                                       Currency = bgnr.Currency,
                                       Date = bgnr.Date,
                                       DateCreated = bgnr.DateCreated,
                                       DistributorCostCenterId = crrReqMsg.DistributorCostCenterId,
                                       MerchantBalance = bgnr.MerchantBalance,
                                       PaidAmount = bgnr.PaidAmount,
                                       ReceiptNumber = bgnr.ReceiptNumber,
                                       SDPTransactionRefId = bgnr.SDPTransactionRefId,
                                       StatusCode = bgnr.StatusCode,
                                       StatusDetail = bgnr.StatusDetail,
                                       SubscriberName = bgnr.SubscriberName ?? "",
                                       Time = bgnr.Time,
                                       TransactionRefId = bgnr.TransactionRefId
                                   };
                    return response;
                }
                #endregion
            }
            catch (Exception ex)
            {
                string msg = string.Format("Notification; Id: {0}; TransactionRefId: " + crrReqMsg.Id, crrReqMsg.TransactionRefId);

                _auditLogRepository.AddLog(crrReqMsg.DistributorCostCenterId,
                                           crrReqMsg.ClientRequestResponseType.ToString(),
                                           "To Mobile",
                                           "Error in ProcessClientPaymentNotificationRequest \n"
                                           + msg + "\nException details: \n"
                                           + ex.Message + ex.InnerException != null ? "\n" + ex.InnerException.Message : "");
                goto pending;
            }

        pending:
            response = new PaymentNotificationResponse
            {
                Id = Guid.NewGuid(),
                ClientRequestResponseType = crrReqMsg.ClientRequestResponseType,
                TransactionRefId = crrReqMsg.TransactionRefId,
                StatusDetail = "Pending",
            };

            return response;
        }