private static XElement GenerateAutoResponse(string guid, MFEPMessage <PaymentNotificationTransaction> deserializedRequest, string fileGuid)
        {
            XElement serializedResponse = null;
            MFEPMessage <PaymentNotificationTransaction> deserializedResponse = new MFEPMessage <PaymentNotificationTransaction>();

            deserializedResponse.Header.TimeStamp    = DateTime.Now.ToString("s");
            deserializedResponse.Header.Guid         = guid;
            deserializedResponse.Header.TransmitInfo = new TransmitInfo()
            {
                SenderCode   = deserializedRequest.Header.TransmitInfo.ReceiverCode,
                ResponseType = ProcessesCodes.BLRPMTNTFRS
            };

            deserializedResponse.Header.Result = new Result();

            PaymentNotificationTransaction transaction = new PaymentNotificationTransaction
            {
                Transactions = new Collection <PaymentNotificationEntity>()
            };

            foreach (PaymentNotificationEntity paymentNotificationEntity in deserializedRequest.Body.Transactions)
            {
                PaymentNotificationEntity notificationEntity = new PaymentNotificationEntity
                {
                    JOEBPPSTrx  = paymentNotificationEntity.JOEBPPSTrx,
                    ProcessDate = paymentNotificationEntity.ProcessDate,
                    STMTDate    = paymentNotificationEntity.STMTDate,
                    Result      = new Result()
                };
                transaction.Transactions.Add(notificationEntity);
            }

            deserializedResponse.Body = transaction;

            XElement pmtResponse = deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >();

            string signatureValue = string.Empty;

            if (EnableSecuity)
            {
                signatureValue = CertificateManager.SignMessage(pmtResponse, new Collection <string>()
                {
                    ".//MsgBody"
                }, BillerCertificateSerialNumber, (SignMode)Enum.Parse(typeof(SignMode), SecurityMode));
            }

            deserializedResponse.Footer = new MFEPFooter()
            {
                Security =
                    new Security()
                {
                    Signature = signatureValue
                }
            };

            serializedResponse = deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >();
            WriteToPhysicalPath(serializedResponse, SuccessPMTNotiResponsePath, fileGuid);
            return(serializedResponse);
        }
        public XElement ReceivePaymentNotification(string guid, XElement paymentNotification)
        {
            MFEPMessage <PaymentNotificationTransaction> deserializedResponse = new MFEPMessage <PaymentNotificationTransaction>();
            XElement serializedResponse = null;
            string   fileName           = Guid.NewGuid().ToString();

            try
            {
                MFEPMessage <PaymentNotificationTransaction> deserializedRequest = paymentNotification.FromXElement <MFEPMessage <PaymentNotificationTransaction> >();
                deserializedResponse.Header.TimeStamp    = DateTime.Now.ToString("s");
                deserializedResponse.Header.Guid         = guid;
                deserializedResponse.Header.TransmitInfo = new TransmitInfo()
                {
                    SenderCode   = deserializedRequest.Header.TransmitInfo.ReceiverCode,
                    ResponseType = ProcessesCodes.BLRPMTNTFRS
                };

                deserializedResponse.Header.Result = new Result();
                fileName = deserializedRequest.Body.Transactions[0].ServiceType.SrvsTypValue.ToString() + fileName;
                if (!IsTestReponse)
                {
                    // Verifying
                    if (EnableSecuity)
                    {
                        if (deserializedRequest == null || deserializedRequest.Footer.Security == null || deserializedRequest.Footer.Security.Signature == null || string.IsNullOrWhiteSpace(deserializedRequest.Footer.Security.Signature.Trim()))
                        {
                            Result result = new Result();
                            result.ErrorCode = (int)ServiceErrors.InvalidSignature;
                            result.ErrorDesc = ServiceErrors.InvalidSignature.ToString();
                            result.Severity  = Severity.Error;
                            deserializedResponse.Header.Result = result;
                            WriteToPhysicalPath(paymentNotification, FailedPMTNotiRequestPath, fileName); // Write to failed notifications
                            serializedResponse = deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >();
                            WriteToPhysicalPath(serializedResponse, FailedPMTNotiResponsePath, fileName);
                            return(serializedResponse);
                        }
                        else
                        {
                            VerifyMode verifyMode = VerifyMode.WithFormat;
                            if (SecurityMode.Equals("WithOutFormat", StringComparison.CurrentCultureIgnoreCase))
                            {
                                verifyMode = VerifyMode.WithOutFormat;
                            }
                            Collection <string> xPath = new Collection <string> {
                                XPath
                            };
                            bool verifiedSuccessfully = CertificateManager.VerifyMessageSignature(paymentNotification, xPath, deserializedRequest.Footer.Security.Signature, BillerCertificateSerialNumber, verifyMode);
                            if (!verifiedSuccessfully)
                            {
                                Result result = new Result();
                                result.ErrorCode = (int)ServiceErrors.InvalidSignature;
                                result.ErrorDesc = ServiceErrors.InvalidSignature.ToString();
                                result.Severity  = Severity.Error;
                                deserializedResponse.Header.Result = result;
                                WriteToPhysicalPath(paymentNotification, FailedPMTNotiRequestPath, fileName); // Write to failed notifications
                                serializedResponse = deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >();
                                WriteToPhysicalPath(serializedResponse, FailedPMTNotiResponsePath, fileName);
                                return(serializedResponse);
                            }
                        }
                    }

                    // Validation
                    // Validate  MFEP code
                    if (deserializedRequest.Header.TransmitInfo.SenderCode != MFEPCode)
                    {
                        Result result = new Result();
                        result.ErrorCode = (int)ServiceErrors.InvalidSenderCode;
                        result.ErrorDesc = ServiceErrors.InvalidSenderCode.ToString();
                        result.Severity  = Severity.Error;
                        deserializedResponse.Header.Result = result;
                        WriteToPhysicalPath(paymentNotification, FailedPMTNotiRequestPath, fileName); // Write to failed notifications
                        serializedResponse = deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >();
                        WriteToPhysicalPath(serializedResponse, FailedPMTNotiResponsePath, fileName);
                        return(serializedResponse);
                    }

                    // Validate Biller code
                    if (deserializedRequest.Header.TransmitInfo.ReceiverCode != BillerCode)
                    {
                        Result result = new Result();
                        result.ErrorCode = (int)ServiceErrors.UnsuccessfulPaymentNotification;
                        result.ErrorDesc = ServiceErrors.UnsuccessfulPaymentNotification.ToString();
                        result.Severity  = Severity.Error;
                        deserializedResponse.Header.Result = result;
                        WriteToPhysicalPath(paymentNotification, FailedPMTNotiRequestPath, fileName); // Write to failed notifications
                        serializedResponse = deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >();
                        WriteToPhysicalPath(serializedResponse, FailedPMTNotiResponsePath, fileName);
                        return(serializedResponse);
                    }
                    // if the serviceType is IndividualSecurity then insert it to database otherwise just write it ti physical directory
                    if (ServiceType.Equals(deserializedRequest.Body.Transactions[0].ServiceType.SrvsTypValue.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Insert this payment
                        DAL dal = new DAL();
                        dal.InsertPaymentNotification(deserializedRequest.Body.Transactions[0].AccountInfo.BillingNumber);
                    }
                    PaymentNotificationTransaction transaction = new PaymentNotificationTransaction
                    {
                        Transactions = new Collection <PaymentNotificationEntity>()
                    };
                    foreach (PaymentNotificationEntity pne in deserializedRequest.Body.Transactions)
                    {
                        PaymentNotificationEntity notificationEntity = new PaymentNotificationEntity
                        {
                            JOEBPPSTrx  = pne.JOEBPPSTrx,
                            ProcessDate = pne.ProcessDate,
                            STMTDate    = pne.STMTDate,
                            Result      = new Result()
                        };
                        transaction.Transactions.Add(notificationEntity);
                    }
                    deserializedResponse.Body = transaction;
                    // Sign the response
                    if (EnableSecuity)
                    {
                        SignMode signMode = SignMode.WithFormat;
                        if (SecurityMode.Equals("WithOutFormat", StringComparison.CurrentCultureIgnoreCase))
                        {
                            signMode = SignMode.WithOutFormat;
                        }
                        Collection <string> xPath = new Collection <string> {
                            XPath
                        };
                        deserializedResponse.Footer = new MFEPFooter();

                        deserializedResponse.Footer.Security.Signature = CertificateManager.SignMessage(deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >(), xPath, BillerCertificateSerialNumber, signMode);
                    }
                    else
                    {
                        deserializedResponse.Footer = new MFEPFooter();
                    }
                    WriteToPhysicalPath(paymentNotification, SuccessPMTNotiRequestPath, fileName); // Write to success notifications
                    serializedResponse = deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >();
                    WriteToPhysicalPath(serializedResponse, SuccessPMTNotiResponsePath, fileName);
                    return(serializedResponse);
                }
                else
                {
                    return(GenerateAutoResponse(guid, deserializedRequest, guid));
                }
            }
            catch (Exception exception)
            {
                string ex = exception.Message + "\n" + exception.StackTrace;
                WriteException(ex, ExceptionPath);

                Result result = new Result();
                result.ErrorCode = (int)ServiceErrors.InternalError;
                result.ErrorDesc = ServiceErrors.InternalError.ToString();
                result.Severity  = Severity.Error;
                deserializedResponse.Header.Result = result;
                WriteToPhysicalPath(paymentNotification, FailedPMTNotiRequestPath, fileName); // Write to success notifications
                serializedResponse = deserializedResponse.ToXElement <MFEPMessage <PaymentNotificationTransaction> >();
                WriteToPhysicalPath(serializedResponse, FailedPMTNotiResponsePath, fileName);
                return(serializedResponse);
            }
        }