Skip to content

Commit

Permalink
Implemented encoding constraints in SwissQRCode and GiroCode payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
codebude committed May 22, 2024
1 parent e3664d1 commit a692a55
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,9 @@ public class SwissQrCode : Payload
private readonly Reference reference;
private readonly AdditionalInformation additionalInformation;

public override QRCodeGenerator.ECCLevel EccLevel { get { return QRCodeGenerator.ECCLevel.M; } }
public override QRCodeGenerator.EciMode EciMode { get { return QRCodeGenerator.EciMode.Utf8; } }

/// <summary>
/// Generates the payload for a SwissQrCode v2.0. (Don't forget to use ECC-Level=M, EncodingMode=UTF-8 and to set the Swiss flag icon to the final QR code.)
/// </summary>
Expand Down Expand Up @@ -1198,6 +1201,7 @@ public class Girocode : Payload
private readonly GirocodeEncoding encoding;
private readonly TypeOfRemittance typeOfRemittance;

public override QRCodeGenerator.ECCLevel EccLevel { get { return QRCodeGenerator.ECCLevel.M; } }

/// <summary>
/// Generates the payload for a Girocode (QR-Code with credit transfer information).
Expand Down
31 changes: 31 additions & 0 deletions QRCoderTests/PayloadGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using static QRCoder.PayloadGenerator.SwissQrCode.Reference;
using System.Reflection;
using static QRCoder.PayloadGenerator.SwissQrCode.AdditionalInformation;
using static QRCoder.QRCodeGenerator;

namespace QRCoderTests
{
Expand Down Expand Up @@ -1335,6 +1336,21 @@ public void girocode_generator_should_throw_usermessage_exception()
exception.Message.ShouldBe("Message to the Girocode-User reader texts have to shorter than 71 chars.");
}

[Fact]
[Category("PayloadGenerator/Girocode")]
public void girocode_generator_sets_encoding_parameters()
{
var iban = "DE33100205000001194700";
var bic = "BFSWDE33BER";
var name = "Wikimedia Fördergesellschaft";
var amount = 10.00m;

var payload = new PayloadGenerator.Girocode(iban, bic, name, amount);

payload.EccLevel.ShouldBe<ECCLevel>(ECCLevel.M);
payload.EciMode.ShouldBe<EciMode>(EciMode.Default);
payload.Version.ShouldBe(-1);
}

[Fact]
[Category("PayloadGenerator/BezahlCode")]
Expand Down Expand Up @@ -2567,6 +2583,21 @@ public void swissqrcode_generator_should_generate_swisscode_full()
.ShouldBe("SPC\r\n0200\r\n1\r\nCH2430043000000789012\r\nS\r\nJohn Doe\r\nParlamentsgebäude\r\n1\r\n3003\r\nBern\r\nCH\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n100.25\r\nCHF\r\nS\r\nJohn Doe\r\nParlamentsgebäude\r\n1\r\n3003\r\nBern\r\nCH\r\nQRR\r\n990005000000000320071012303\r\nThis is my unstructured message.\r\nEPD\r\nSome bill information here...");
}

[Fact]
[Category("PayloadGenerator/SwissQrCode")]
public void swissqrcode_generator_sets_encoding_parameters()
{
var creditor = PayloadGenerator.SwissQrCode.Contact.WithStructuredAddress("John Doe", "3003", "Bern", "CH", "Parlamentsgebäude", "1");
var iban = new PayloadGenerator.SwissQrCode.Iban("CH2430043000000789012", PayloadGenerator.SwissQrCode.Iban.IbanType.QrIban);
var reference = new PayloadGenerator.SwissQrCode.Reference(ReferenceType.QRR, "990005000000000320071012303", ReferenceTextType.QrReference);
var currency = PayloadGenerator.SwissQrCode.Currency.EUR;

var payload = new PayloadGenerator.SwissQrCode(iban, currency, creditor, reference);

payload.EccLevel.ShouldBe<ECCLevel>(ECCLevel.M);
payload.EciMode.ShouldBe<EciMode>(EciMode.Utf8);
payload.Version.ShouldBe(-1);
}

[Fact]
[Category("PayloadGenerator/SwissQrCode")]
Expand Down

0 comments on commit a692a55

Please sign in to comment.