public Guid Save(BuyGoodsNotificationResponse entity)
        {
            /*BuyGoodsNotificationResponse existing = GetById(entity.Id);
            if (existing != null)
                _ctx.Delete(existing);
            siaqodb.StoreObject(entity);
            siaqodb.Flush();
            */
            BuyGoodsNotificationResponse exist = GetById(entity.Id);
            if (exist == null)
            {
                exist = new BuyGoodsNotificationResponse();
                _ctx.BuyGoodsNotificationResponse.Add(exist);
            }
            exist.Id = entity.Id;
            exist.DistributorCostCenterId = entity.DistributorCostCenterId;
            exist.TransactionRefId = entity.TransactionRefId;
            exist.ClientRequestResponseType = entity.ClientRequestResponseType;
            exist.DateCreated = entity.DateCreated;
            exist.SDPTransactionRefId = entity.SDPTransactionRefId;
            exist.SubscriberName = entity.SubscriberName;
            exist.ReceiptNumber = entity.ReceiptNumber;
            exist.Currency = entity.Currency;
            exist.PaidAmount = entity.PaidAmount;
            exist.MerchantBalance = entity.MerchantBalance;
            exist.Date = entity.Date;
            exist.Time = entity.Time;
            exist.StatusCode = entity.StatusCode;
            exist.StatusDetail = entity.StatusDetail;
            
            _ctx.SaveChanges();

            return entity.Id;
        }
        public async Task<BuyGoodsNotificationResponse> GetBuyGoodsNotificationAsync(BuyGoodsNotificationRequest request)
        {
            BuyGoodsNotificationResponse _response = new BuyGoodsNotificationResponse();
            HttpClient httpClient = setupHttpClient();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            string url = "api/bridge/payment/getbuygoodsnotification";
            try
            {
                var response = await httpClient.PostAsJsonAsync(url, request);
                _response = await response.Content.ReadAsAsync<BuyGoodsNotificationResponse>();
            }
            catch (Exception ex)
            { 
                string error = "Failed to retrieve payment notification.\n" +
                               (ex.InnerException == null ? "" : ex.InnerException.Message);
                _log.Error(error);
                _response.StatusCode = "Error";
                _response.StatusDetail = error;
            }

            return _response;
        }
        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;
        }