Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ private static bool IsValidQRIban(string iban)
private static bool IsValidBic(string bic)
=> Regex.IsMatch(bic.Replace(" ", ""), @"^([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?)$");

/// <summary>
/// Validates the structure of a BIC with optional requirement check.
/// </summary>
/// <param name="bic">The BIC to validate.</param>
/// <param name="required">Whether the BIC is required. If false, null/empty values are considered valid.</param>
/// <returns>True if the BIC is valid; otherwise, false.</returns>
private static bool IsValidBic(string? bic, bool required)
{
if (string.IsNullOrEmpty(bic))
return !required;
return IsValidBic(bic!);
}

/// <summary>
/// Converts a string to a specified encoding.
Expand Down
6 changes: 3 additions & 3 deletions QRCoder/PayloadGenerator/Girocode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public class Girocode : Payload
/// <param name="version">Girocode version. Either 001 or 002. Default: 001.</param>
/// <param name="encoding">Encoding of the Girocode payload. Default: ISO-8859-1</param>
/// <exception cref="GirocodeException">Thrown when the input values are not valid according to the Girocode specification.</exception>
public Girocode(string iban, string bic, string name, decimal amount, string remittanceInformation = "", TypeOfRemittance typeOfRemittance = TypeOfRemittance.Unstructured, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", GirocodeVersion version = GirocodeVersion.Version1, GirocodeEncoding encoding = GirocodeEncoding.ISO_8859_1)
public Girocode(string iban, string? bic, string name, decimal amount, string remittanceInformation = "", TypeOfRemittance typeOfRemittance = TypeOfRemittance.Unstructured, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", GirocodeVersion version = GirocodeVersion.Version1, GirocodeEncoding encoding = GirocodeEncoding.ISO_8859_1)
{
_version = version;
_encoding = encoding;
if (!IsValidIban(iban))
throw new GirocodeException("The IBAN entered isn't valid.");
_iban = iban.Replace(" ", "").ToUpper();
if (!IsValidBic(bic))
if (!IsValidBic(bic, _version == GirocodeVersion.Version1))
throw new GirocodeException("The BIC entered isn't valid.");
_bic = bic.Replace(" ", "").ToUpper();
_bic = bic?.Replace(" ", "").ToUpper() ?? string.Empty;
if (name.Length > 70)
throw new GirocodeException("(Payee-)Name must be shorter than 71 chars.");
_name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ namespace QRCoder
}
public class Girocode : QRCoder.PayloadGenerator.Payload
{
public Girocode(string iban, string bic, string name, decimal amount, string remittanceInformation = "", QRCoder.PayloadGenerator.Girocode.TypeOfRemittance typeOfRemittance = 1, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", QRCoder.PayloadGenerator.Girocode.GirocodeVersion version = 0, QRCoder.PayloadGenerator.Girocode.GirocodeEncoding encoding = 1) { }
public Girocode(string iban, string? bic, string name, decimal amount, string remittanceInformation = "", QRCoder.PayloadGenerator.Girocode.TypeOfRemittance typeOfRemittance = 1, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", QRCoder.PayloadGenerator.Girocode.GirocodeVersion version = 0, QRCoder.PayloadGenerator.Girocode.GirocodeEncoding encoding = 1) { }
public override QRCoder.QRCodeGenerator.ECCLevel EccLevel { get; }
public override string ToString() { }
public enum GirocodeEncoding
Expand Down
2 changes: 1 addition & 1 deletion QRCoderApiTests/net60-windows/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ namespace QRCoder
}
public class Girocode : QRCoder.PayloadGenerator.Payload
{
public Girocode(string iban, string bic, string name, decimal amount, string remittanceInformation = "", QRCoder.PayloadGenerator.Girocode.TypeOfRemittance typeOfRemittance = 1, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", QRCoder.PayloadGenerator.Girocode.GirocodeVersion version = 0, QRCoder.PayloadGenerator.Girocode.GirocodeEncoding encoding = 1) { }
public Girocode(string iban, string? bic, string name, decimal amount, string remittanceInformation = "", QRCoder.PayloadGenerator.Girocode.TypeOfRemittance typeOfRemittance = 1, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", QRCoder.PayloadGenerator.Girocode.GirocodeVersion version = 0, QRCoder.PayloadGenerator.Girocode.GirocodeEncoding encoding = 1) { }
public override QRCoder.QRCodeGenerator.ECCLevel EccLevel { get; }
public override string ToString() { }
public enum GirocodeEncoding
Expand Down
2 changes: 1 addition & 1 deletion QRCoderApiTests/net60/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ namespace QRCoder
}
public class Girocode : QRCoder.PayloadGenerator.Payload
{
public Girocode(string iban, string bic, string name, decimal amount, string remittanceInformation = "", QRCoder.PayloadGenerator.Girocode.TypeOfRemittance typeOfRemittance = 1, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", QRCoder.PayloadGenerator.Girocode.GirocodeVersion version = 0, QRCoder.PayloadGenerator.Girocode.GirocodeEncoding encoding = 1) { }
public Girocode(string iban, string? bic, string name, decimal amount, string remittanceInformation = "", QRCoder.PayloadGenerator.Girocode.TypeOfRemittance typeOfRemittance = 1, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", QRCoder.PayloadGenerator.Girocode.GirocodeVersion version = 0, QRCoder.PayloadGenerator.Girocode.GirocodeEncoding encoding = 1) { }
public override QRCoder.QRCodeGenerator.ECCLevel EccLevel { get; }
public override string ToString() { }
public enum GirocodeEncoding
Expand Down
2 changes: 1 addition & 1 deletion QRCoderApiTests/netstandard13/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ namespace QRCoder
}
public class Girocode : QRCoder.PayloadGenerator.Payload
{
public Girocode(string iban, string bic, string name, decimal amount, string remittanceInformation = "", QRCoder.PayloadGenerator.Girocode.TypeOfRemittance typeOfRemittance = 1, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", QRCoder.PayloadGenerator.Girocode.GirocodeVersion version = 0, QRCoder.PayloadGenerator.Girocode.GirocodeEncoding encoding = 1) { }
public Girocode(string iban, string? bic, string name, decimal amount, string remittanceInformation = "", QRCoder.PayloadGenerator.Girocode.TypeOfRemittance typeOfRemittance = 1, string purposeOfCreditTransfer = "", string messageToGirocodeUser = "", QRCoder.PayloadGenerator.Girocode.GirocodeVersion version = 0, QRCoder.PayloadGenerator.Girocode.GirocodeEncoding encoding = 1) { }
public override QRCoder.QRCodeGenerator.ECCLevel EccLevel { get; }
public override string ToString() { }
public enum GirocodeEncoding
Expand Down
134 changes: 134 additions & 0 deletions QRCoderTests/PayloadGeneratorTests/GirocodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,138 @@ public void girocode_generator_sets_encoding_parameters()
payload.EciMode.ShouldBe<EciMode>(EciMode.Default);
payload.Version.ShouldBe(-1);
}

[Fact]
public void girocode_generator_version2_with_null_bic_should_succeed()
{
var iban = "NL86INGB0002445588";
var name = "a name";
var remittanceInformation = "some info";
var amount = 1337.99m;

var payload = new PayloadGenerator.Girocode(
iban: iban,
bic: null,
name: name,
amount: amount,
remittanceInformation: remittanceInformation,
version: PayloadGenerator.Girocode.GirocodeVersion.Version2,
encoding: PayloadGenerator.Girocode.GirocodeEncoding.UTF_8);

payload
.ToString()
.ShouldBe("BCD\n002\n1\nSCT\n\na name\nNL86INGB0002445588\nEUR1337.99\n\n\nsome info\n");
}

[Fact]
public void girocode_generator_version2_with_empty_bic_should_succeed()
{
var iban = "NL86INGB0002445588";
var name = "a name";
var remittanceInformation = "some info";
var amount = 1337.99m;

var payload = new PayloadGenerator.Girocode(
iban: iban,
bic: string.Empty,
name: name,
amount: amount,
remittanceInformation: remittanceInformation,
version: PayloadGenerator.Girocode.GirocodeVersion.Version2,
encoding: PayloadGenerator.Girocode.GirocodeEncoding.UTF_8);

payload
.ToString()
.ShouldBe("BCD\n002\n1\nSCT\n\na name\nNL86INGB0002445588\nEUR1337.99\n\n\nsome info\n");
}

[Fact]
public void girocode_generator_version2_with_valid_bic_should_succeed()
{
var iban = "NL86INGB0002445588";
var bic = "INGBNL2A";
var name = "a name";
var remittanceInformation = "some info";
var amount = 1337.99m;

var payload = new PayloadGenerator.Girocode(
iban: iban,
bic: bic,
name: name,
amount: amount,
remittanceInformation: remittanceInformation,
version: PayloadGenerator.Girocode.GirocodeVersion.Version2,
encoding: PayloadGenerator.Girocode.GirocodeEncoding.UTF_8);

payload
.ToString()
.ShouldBe("BCD\n002\n1\nSCT\nINGBNL2A\na name\nNL86INGB0002445588\nEUR1337.99\n\n\nsome info\n");
}

[Fact]
public void girocode_generator_version2_with_invalid_bic_should_throw_exception()
{
var iban = "NL86INGB0002445588";
var bic = "INVALID";
var name = "a name";
var remittanceInformation = "some info";
var amount = 1337.99m;

var exception = Record.Exception(() => new PayloadGenerator.Girocode(
iban: iban,
bic: bic,
name: name,
amount: amount,
remittanceInformation: remittanceInformation,
version: PayloadGenerator.Girocode.GirocodeVersion.Version2,
encoding: PayloadGenerator.Girocode.GirocodeEncoding.UTF_8));

Assert.NotNull(exception);
Assert.IsType<PayloadGenerator.Girocode.GirocodeException>(exception);
exception.Message.ShouldBe("The BIC entered isn't valid.");
}

[Fact]
public void girocode_generator_version1_with_null_bic_should_throw_exception()
{
var iban = "NL86INGB0002445588";
var name = "a name";
var remittanceInformation = "some info";
var amount = 1337.99m;

var exception = Record.Exception(() => new PayloadGenerator.Girocode(
iban: iban,
bic: null,
name: name,
amount: amount,
remittanceInformation: remittanceInformation,
version: PayloadGenerator.Girocode.GirocodeVersion.Version1,
encoding: PayloadGenerator.Girocode.GirocodeEncoding.UTF_8));

Assert.NotNull(exception);
Assert.IsType<PayloadGenerator.Girocode.GirocodeException>(exception);
exception.Message.ShouldBe("The BIC entered isn't valid.");
}

[Fact]
public void girocode_generator_version1_with_empty_bic_should_throw_exception()
{
var iban = "NL86INGB0002445588";
var name = "a name";
var remittanceInformation = "some info";
var amount = 1337.99m;

var exception = Record.Exception(() => new PayloadGenerator.Girocode(
iban: iban,
bic: string.Empty,
name: name,
amount: amount,
remittanceInformation: remittanceInformation,
version: PayloadGenerator.Girocode.GirocodeVersion.Version1,
encoding: PayloadGenerator.Girocode.GirocodeEncoding.UTF_8));

Assert.NotNull(exception);
Assert.IsType<PayloadGenerator.Girocode.GirocodeException>(exception);
exception.Message.ShouldBe("The BIC entered isn't valid.");
}
}