Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
Escape Sepa
Browse files Browse the repository at this point in the history
  • Loading branch information
torstenprivate committed Aug 3, 2023
1 parent 5a97709 commit 9f7ce8d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
50 changes: 50 additions & 0 deletions src/libfintx.Sepa/Helper/SepaHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace libfintx.Sepa.Helper
{
public static class SepaHelper
{
public static string Escape(string str)
{
// Zunächst XML-escapen
var escaped = SecurityElement.Escape(str);
// Dann sicherstellen, dass nur gemäß SEPA gültige Zeichen verwendet werden
return ConvertToValidSepaString(escaped);
}

public static string ConvertToValidSepaString(string str)
{
return new string(str.Select(c => ConvertToValidSepaString(c)).SelectMany(s => s).ToArray());
}

/// <summary>
/// Hier findet kein XML-Escaping statt, sondern es wird sichergestellt, dass nur die in SEPA gültigen Zeichen verwendet werden.
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
private static string ConvertToValidSepaString(char c)
{
switch (c)
{
case 'Ä': return "Ae";
case 'Ö': return "Oe";
case 'Ü': return "Ue";
case 'ä': return "ae";
case 'ö': return "oe";
case 'ü': return "ue";
case 'ß': return "ss";
}

if (!Regex.Match($"{c}", @"^[\sa-zA-Z0-9/?:\(\)\.,'+-]$").Success)
return string.Empty;

return $"{c}";
}
}
}
7 changes: 4 additions & 3 deletions src/libfintx.Sepa/Pain/pain00100203.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Collections.Generic;
using System.Security;
using libfintx.Globals;
using libfintx.Sepa.Helper;

namespace libfintx.Sepa
{
Expand Down Expand Up @@ -52,9 +53,9 @@ public static string Create(string Accountholder, string AccountholderIBAN, stri

var Amount_ = Amount.ToString().Replace(",", ".");

var Accountholder_ = SecurityElement.Escape(Accountholder);
var Receiver_ = SecurityElement.Escape(Receiver);
var Usage_ = SecurityElement.Escape(Usage);
var Accountholder_ = SepaHelper.Escape(Accountholder);
var Receiver_ = SepaHelper.Escape(Receiver);
var Usage_ = SepaHelper.Escape(Usage);

string Message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.001.002.03\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd\">" +
Expand Down
7 changes: 4 additions & 3 deletions src/libfintx.Sepa/Pain/pain00100303.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Collections.Generic;
using System.Security;
using libfintx.Globals;
using libfintx.Sepa.Helper;

namespace libfintx.Sepa
{
Expand Down Expand Up @@ -53,9 +54,9 @@ public static string Create(string Accountholder, string AccountholderIBAN, stri

var Amount_ = Amount.ToString().Replace(",", ".");

var Accountholder_ = SecurityElement.Escape(Accountholder);
var Receiver_ = SecurityElement.Escape(Receiver);
var Usage_ = SecurityElement.Escape(Usage);
var Accountholder_ = SepaHelper.Escape(Accountholder);
var Receiver_ = SepaHelper.Escape(Receiver);
var Usage_ = SepaHelper.Escape(Usage);

string Message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.001.003.03\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:iso:std:iso:20022:tech:xsd:pain.001.003.03 pain.001.003.03.xsd\">" +
Expand Down
7 changes: 4 additions & 3 deletions src/libfintx.Sepa/Pain/pain00800202.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Collections.Generic;
using System.Security;
using libfintx.Globals;
using libfintx.Sepa.Helper;

namespace libfintx.Sepa
{
Expand Down Expand Up @@ -63,9 +64,9 @@ public static string Create(string Accountholder, string AccountholderIBAN, stri

var Amount_ = Amount.ToString().Replace(",", ".");

var Accountholder_ = SecurityElement.Escape(Accountholder);
var Payer_ = SecurityElement.Escape(Payer);
var Usage_ = SecurityElement.Escape(Usage);
var Accountholder_ = SepaHelper.Escape(Accountholder);
var Payer_ = SepaHelper.Escape(Payer);
var Usage_ = SepaHelper.Escape(Usage);

string Message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.008.002.02\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:iso:std:iso:20022:tech:xsd:pain.008.002.02 pain.008.002.02.xsd\">" +
Expand Down
6 changes: 6 additions & 0 deletions src/libfintx.Sepa/libfintx.Sepa.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
<ProjectReference Include="..\libfintx.Globals\libfintx.Globals.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="Helper\" />
</ItemGroup>
<ItemGroup>
<None Update="Pain\pain_001_001_03\pain.001.001.03.xsd">
<SubType>Designer</SubType>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Helper\" />
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions src/libfintx.Tests/Pain/pain00100103Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@
using libfintx.FinTS;
using System.Collections.Generic;
using libfintx.Sepa;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
using libfintx.Sepa.Helper;


namespace libfintx.Tests.Pain
{
public class pain00100103Tests
{
[Fact]
public void Test_Escape()
{
string str = SepaHelper.Escape(@"Hübner;;;\\\");
Assert.Equal("Huebner", str);

str = SepaHelper.Escape(@"Der Verwendungszweck der Überweisung ####ist die Mietzahlung.");
Assert.Equal("Der Verwendungszweck der Ueberweisung ist die Mietzahlung.", str);
}

[Fact(Skip = "You have to set the Arrange variables for this test")]
public void Create_StateUnderTest_ExpectedBehavior()
{
Expand Down

0 comments on commit 9f7ce8d

Please sign in to comment.