예제 #1
0
파일: Helper.cs 프로젝트: iwen65/libfintx
        /// <summary>
        /// Parsing segment -> UPD, BPD
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="BLZ"></param>
        /// <param name="HBCIVersion"></param>
        /// <param name="Message"></param>
        /// <returns></returns>
        public static List <HBCIBankMessage> Parse_Segment(FinTsClient client, string Message)
        {
            try
            {
                var connDetails = client.ConnectionDetails;
                List <HBCIBankMessage> result = new List <HBCIBankMessage>();

                string[] values = Message.Split('\'');

                string msg = string.Join(Environment.NewLine, values);

                string bpd = string.Empty;
                string upd = string.Empty;

                var bpaMatch = Regex.Match(msg, @"(HIBPA.+?)\b(HNHBS|HISYN|HIUPA)\b", RegexOptions.Singleline);
                if (bpaMatch.Success)
                {
                    bpd = bpaMatch.Groups[1].Value;
                }

                var upaMatch = Regex.Match(msg, @"(HIUPA.+?)\b(HITAN|HNHBS)\b", RegexOptions.Singleline);
                if (upaMatch.Success)
                {
                    upd = upaMatch.Groups[1].Value;
                }

                // BPD
                SaveBPD(connDetails.Blz, bpd);
                BPD.ParseBpd(bpd);

                // UPD
                SaveUPD(connDetails.Blz, connDetails.UserId, upd);
                UPD.ParseUpd(upd);

                foreach (var item in values)
                {
                    if (item.Contains("HIRMG"))
                    {
                        // HIRMG:2:2+9050::Die Nachricht enthält Fehler.+9800::Dialog abgebrochen+9010::Initialisierung fehlgeschlagen, Auftrag nicht bearbeitet.
                        // HIRMG:2:2+9800::Dialogabbruch.

                        string[] HIRMG_messages = item.Split('+');
                        foreach (var HIRMG_message in HIRMG_messages)
                        {
                            var message = Parse_BankCode_Message(HIRMG_message);
                            if (message != null)
                            {
                                result.Add(message);
                            }
                        }
                    }

                    if (item.Contains("HIRMS"))
                    {
                        // HIRMS:3:2:2+9942::PIN falsch. Zugang gesperrt.'
                        string[] HIRMS_messages = item.Split('+');
                        foreach (var HIRMS_message in HIRMS_messages)
                        {
                            var message = Parse_BankCode_Message(HIRMS_message);
                            if (message != null)
                            {
                                result.Add(message);
                            }
                        }

                        var securityMessage = result.FirstOrDefault(m => m.Code == "3920");
                        if (securityMessage != null)
                        {
                            string message = securityMessage.Message;

                            string TAN  = string.Empty;
                            string TANf = string.Empty;

                            string[] procedures = Regex.Split(message, @"\D+");

                            foreach (string value in procedures)
                            {
                                if (!string.IsNullOrEmpty(value) && int.TryParse(value, out int i))
                                {
                                    if (Convert.ToString(i).StartsWith("9"))
                                    {
                                        if (string.IsNullOrEmpty(TAN))
                                        {
                                            TAN = i.ToString();
                                        }

                                        if (string.IsNullOrEmpty(TANf))
                                        {
                                            TANf = i.ToString();
                                        }
                                        else
                                        {
                                            TANf += $";{i}";
                                        }
                                    }
                                }
                            }
                            if (string.IsNullOrEmpty(client.HIRMS))
                            {
                                client.HIRMS = TAN;
                            }
                            else
                            {
                                if (!TANf.Contains(client.HIRMS))
                                {
                                    throw new Exception($"Invalid HIRMS/Tan-Mode {client.HIRMS} detected. Please choose one of the allowed modes: {TANf}");
                                }
                            }
                            client.HIRMSf = TANf;

                            // Parsing TAN processes
                            if (!string.IsNullOrEmpty(client.HIRMS))
                            {
                                Parse_TANProcesses(client, bpd);
                            }
                        }
                    }

                    if (item.Contains("HNHBK"))
                    {
                        var ID = Parse_String(item.ToString(), "+1+", ":1");
                        client.HNHBK = ID;
                    }

                    if (item.Contains("HISYN"))
                    {
                        var ID = item.Substring(item.IndexOf("+") + 1);
                        client.SystemId = ID;

                        Log.Write("Customer System ID: " + ID);
                    }

                    if (item.Contains("HNHBS"))
                    {
                        var item_ = item + "'";

                        var MSG = Parse_String(item_.Replace("HNHBS:", ""), "+", "'");

                        if (MSG.Equals("0") || MSG == null)
                        {
                            client.HNHBS = "2";
                        }
                        else
                        {
                            client.HNHBS = Convert.ToString(Convert.ToInt16(MSG) + 1);
                        }
                    }

                    if (item.Contains("HISALS"))
                    {
                        var SEG = Parse_String(item.Replace("HISALS:", ""), ":", ":");

                        client.HISALS = SEG;

                        client.HISALSf = item;
                    }

                    if (item.Contains("HITANS"))
                    {
                        var TAN = Parse_String(item.Replace("HITANS:", ""), ":", "+").Replace(":", "+");

                        client.HITANS = TAN;
                    }

                    if (item.Contains("HKKAZ"))
                    {
                        string pattern  = @"HKKAZ;.*?;";
                        Regex  rgx      = new Regex(pattern);
                        string sentence = item;

                        foreach (Match match in rgx.Matches(sentence))
                        {
                            var VER = Parse_String(match.Value, "HKKAZ;", ";");

                            if (string.IsNullOrEmpty(client.HKKAZ))
                            {
                                client.HKKAZ = VER;
                            }
                            else
                            {
                                if (int.Parse(VER) > int.Parse(client.HKKAZ))
                                {
                                    client.HKKAZ = VER;
                                }
                            }
                        }
                    }

                    if (item.Contains("HISPAS"))
                    {
                        if (item.Contains("pain.001.001.03"))
                        {
                            client.HISPAS = 1;
                        }
                        else if (item.Contains("pain.001.002.03"))
                        {
                            client.HISPAS = 2;
                        }
                        else if (item.Contains("pain.001.003.03"))
                        {
                            client.HISPAS = 3;
                        }

                        if (client.HISPAS == 0)
                        {
                            client.HISPAS = 3; // -> Fallback. Most banks accept the newest pain version
                        }
                    }
                }

                // Fallback if HKKAZ is not delivered by BPD (eg. Postbank)
                if (string.IsNullOrEmpty(client.HKKAZ))
                {
                    client.HKKAZ = "5";
                }

                return(result);
            }
            catch (Exception ex)
            {
                Log.Write(ex.ToString());

                throw new InvalidOperationException($"Software error.", ex);
            }
        }
예제 #2
0
파일: Helper.cs 프로젝트: Zengage/libfintx
        /// <summary>
        /// Parsing segment -> UPD, BPD
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="BLZ"></param>
        /// <param name="HBCIVersion"></param>
        /// <param name="Message"></param>
        /// <returns></returns>
        public static List <HBCIBankMessage> Parse_Segments(FinTsClient client, string Message)
        {
            try
            {
                var connDetails = client.ConnectionDetails;
                List <HBCIBankMessage> result = new List <HBCIBankMessage>();

                List <string> values = SplitSegments(Message);

                List <Segment> segments = new List <Segment>();
                foreach (var item in values)
                {
                    Segment segment = Parse_Segment(item);
                    if (segment != null)
                    {
                        segments.Add(segment);
                    }
                }

                string msg = string.Join(Environment.NewLine, values);

                string bpd = string.Empty;
                string upd = string.Empty;

                var bpaMatch = Regex.Match(msg, @"(HIBPA.+?)\b(HNHBS|HISYN|HIUPA)\b", RegexOptions.Singleline);
                if (bpaMatch.Success)
                {
                    bpd = bpaMatch.Groups[1].Value;
                }

                var upaMatch = Regex.Match(msg, @"(HIUPA.+?)\b(HITAN|HNHBS)\b", RegexOptions.Singleline);
                if (upaMatch.Success)
                {
                    upd = upaMatch.Groups[1].Value;
                }

                // BPD
                SaveBPD(connDetails.Blz, bpd);
                BPD.ParseBpd(bpd);

                // UPD
                SaveUPD(connDetails.Blz, connDetails.UserId, upd);
                UPD.ParseUpd(upd);

                //Add BIC to Account information (Not retrieved bz UPD??)
                foreach (AccountInformation accInfo in UPD.HIUPD.AccountList)
                {
                    accInfo.AccountBic = connDetails.Bic;
                }

                foreach (var segment in segments)
                {
                    if (segment.Name == "HIRMG")
                    {
                        // HIRMG:2:2+9050::Die Nachricht enthält Fehler.+9800::Dialog abgebrochen+9010::Initialisierung fehlgeschlagen, Auftrag nicht bearbeitet.
                        // HIRMG:2:2+9800::Dialogabbruch.

                        string[] HIRMG_messages = segment.Payload.Split('+');
                        foreach (var HIRMG_message in HIRMG_messages)
                        {
                            var message = Parse_BankCode_Message(HIRMG_message);
                            if (message != null)
                            {
                                result.Add(message);
                            }
                        }
                    }

                    if (segment.Name == "HIRMS")
                    {
                        // HIRMS:3:2:2+9942::PIN falsch. Zugang gesperrt.'
                        string[] HIRMS_messages = segment.Payload.Split('+');
                        foreach (var HIRMS_message in HIRMS_messages)
                        {
                            var message = Parse_BankCode_Message(HIRMS_message);
                            if (message != null)
                            {
                                result.Add(message);
                            }
                        }

                        var securityMessage = result.FirstOrDefault(m => m.Code == "3920");
                        if (securityMessage != null)
                        {
                            string message = securityMessage.Message;

                            string TAN  = string.Empty;
                            string TANf = string.Empty;

                            string[] procedures = Regex.Split(message, @"\D+");

                            foreach (string value in procedures)
                            {
                                if (!string.IsNullOrEmpty(value) && int.TryParse(value, out int i))
                                {
                                    if (Convert.ToString(i).StartsWith("9"))
                                    {
                                        if (string.IsNullOrEmpty(TAN))
                                        {
                                            TAN = i.ToString();
                                        }

                                        if (string.IsNullOrEmpty(TANf))
                                        {
                                            TANf = i.ToString();
                                        }
                                        else
                                        {
                                            TANf += $";{i}";
                                        }
                                    }
                                }
                            }
                            if (string.IsNullOrEmpty(client.HIRMS))
                            {
                                client.HIRMS = TAN;
                            }
                            else
                            {
                                if (!TANf.Contains(client.HIRMS))
                                {
                                    throw new Exception($"Invalid HIRMS/Tan-Mode {client.HIRMS} detected. Please choose one of the allowed modes: {TANf}");
                                }
                            }
                            client.HIRMSf = TANf;

                            // Parsing TAN processes
                            if (!string.IsNullOrEmpty(client.HIRMS))
                            {
                                Parse_TANProcesses(client, bpd);
                            }
                        }
                    }

                    if (segment.Name == "HNHBK")
                    {
                        var ID = Parse_String(segment.Payload, "+1+", ":1");
                        client.HNHBK = ID;
                    }

                    if (segment.Name == "HISYN")
                    {
                        client.SystemId = segment.Payload;
                        Log.Write("Customer System ID: " + client.SystemId);
                    }

                    if (segment.Name == "HNHBS")
                    {
                        if (segment.Payload == null || segment.Payload == "0")
                        {
                            client.HNHBS = 2;
                        }
                        else
                        {
                            client.HNHBS = Convert.ToInt32(segment.Payload) + 1;
                        }
                    }

                    if (segment.Name == "HISALS")
                    {
                        client.HISALS = segment.Version;
                    }

                    if (segment.Name == "HITANS")
                    {
                        if (client.HITANS != 0 && segment.Version == 7)
                        {
                            ; // Ignore HKTAN version 7 if other version is available and version 7 isn't implemented in libfintx
                        }
                        else
                        {
                            client.HITANS = segment.Version;
                        }
                    }

                    if (segment.Name == "HITAN")
                    {
                        client.HITAN = Parse_String(segment.Payload.Replace("?+", "??"), "++", "+").Replace("??", "?+");
                    }

                    if (segment.Name == "HIKAZS")
                    {
                        if (client.HIKAZS == 0)
                        {
                            client.HIKAZS = segment.Version;
                        }
                        else
                        {
                            if (segment.Version > client.HIKAZS)
                            {
                                client.HIKAZS = segment.Version;
                            }
                        }
                    }

                    if (segment.Name == "HISPAS")
                    {
                        if (segment.Payload.Contains("pain.001.001.03"))
                        {
                            client.HISPAS = 1;
                        }
                        else if (segment.Payload.Contains("pain.001.002.03"))
                        {
                            client.HISPAS = 2;
                        }
                        else if (segment.Payload.Contains("pain.001.003.03"))
                        {
                            client.HISPAS = 3;
                        }

                        if (client.HISPAS == 0)
                        {
                            client.HISPAS = 3; // -> Fallback. Most banks accept the newest pain version
                        }
                    }
                }

                // Fallback if HIKAZS is not delivered by BPD (eg. Postbank)
                if (client.HIKAZS == 0)
                {
                    client.HIKAZS = 0;
                }

                return(result);
            }
            catch (Exception ex)
            {
                Log.Write(ex.ToString());

                throw new InvalidOperationException($"Software error.", ex);
            }
        }