FAQ TBarCode .NET
How Can I Create a Swiss QR Code V2.3?
How Can I Create a Swiss QR Code V2.3?
The following sample code in C# .NET shows how to create the data format for Swiss QR Code according to the Swiss QR-bill Implementation Guidelines Version 2.3.
Version 2.3 requires structured addresses. The street, building number, postal code, and town must therefore be provided in separate fields. The address type must be set to S.
public enum CurrencyCodes
{
CHF,
EUR
}
public enum ReferenceTypes
{
NON,
SCOR,
QRR
}
private const string fldSep = "\n";
decimal? Amount = new decimal(99.01);
CurrencyCodes Currency = CurrencyCodes.CHF;
ReferenceTypes ReferenceType = ReferenceTypes.SCOR;
string QRIBAN = "";
string CreditorIBAN = "CH3908704016075473007";
// Structured creditor address
string CreditorName = "Robert Schneider AG";
string CreditorStreet = "Rue du Lac";
string CreditorBuildingNumber = "1268";
string CreditorPostalCode = "2501";
string CreditorTown = "Biel";
string CreditorCountry = "CH";
// Structured debtor address
string DebtorName = "Max Mustermann";
string DebtorStreet = "Musterstrasse";
string DebtorBuildingNumber = "2";
string DebtorPostalCode = "1234";
string DebtorTown = "Musterstadt";
string DebtorCountry = "CH";
string QRReference = "";
string CreditorReference = "RF18539007547034";
string Message = "Optional";
string BillInfo = "";
string Parameter1 = "";
string Parameter2 = "";
/// <summary>
/// Returns a data string valid for Swiss QR Code
/// according to the QR-bill Implementation Guidelines Version 2.3.
/// </summary>
/// <returns>Swiss QR Code data string</returns>
public string GetDataString()
{
var sb = new StringBuilder();
// Header
sb.Append("SPC" + fldSep);
// The data structure version remains 0200 in
// QR-bill Implementation Guidelines Version 2.3.
sb.Append("0200" + fldSep);
sb.Append("1" + fldSep);
// Creditor account
if (ReferenceType == ReferenceTypes.QRR)
sb.Append(QRIBAN + fldSep);
else
sb.Append(CreditorIBAN + fldSep);
// Creditor: structured address
sb.Append("S" + fldSep);
sb.Append(CreditorName + fldSep);
sb.Append(CreditorStreet + fldSep);
sb.Append(CreditorBuildingNumber + fldSep);
sb.Append(CreditorPostalCode + fldSep);
sb.Append(CreditorTown + fldSep);
sb.Append(CreditorCountry + fldSep);
// Ultimate creditor: currently not used
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
// Amount
if (Amount.HasValue)
{
sb.Append(
Amount.Value.ToString(
"#########.00",
System.Globalization.CultureInfo.InvariantCulture)
+ fldSep);
}
else
{
sb.Append(fldSep);
}
sb.Append(Currency + fldSep);
// Debtor: structured address
if (string.IsNullOrEmpty(DebtorName) &&
string.IsNullOrEmpty(DebtorStreet) &&
string.IsNullOrEmpty(DebtorBuildingNumber) &&
string.IsNullOrEmpty(DebtorPostalCode) &&
string.IsNullOrEmpty(DebtorTown) &&
string.IsNullOrEmpty(DebtorCountry))
{
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
sb.Append(fldSep);
}
else
{
sb.Append("S" + fldSep);
sb.Append(DebtorName + fldSep);
sb.Append(DebtorStreet + fldSep);
sb.Append(DebtorBuildingNumber + fldSep);
sb.Append(DebtorPostalCode + fldSep);
sb.Append(DebtorTown + fldSep);
sb.Append(DebtorCountry + fldSep);
}
// Reference
sb.Append(ReferenceType + fldSep);
if (ReferenceType == ReferenceTypes.QRR)
sb.Append(QRReference + fldSep);
else if (ReferenceType == ReferenceTypes.SCOR)
sb.Append(CreditorReference + fldSep);
else
sb.Append(fldSep);
// Additional information
sb.Append(Message + fldSep);
sb.Append("EPD");
// Optional billing information and alternative procedures
if (!string.IsNullOrEmpty(BillInfo) ||
!string.IsNullOrEmpty(Parameter1) ||
!string.IsNullOrEmpty(Parameter2))
{
sb.Append(fldSep);
sb.Append(BillInfo + fldSep);
sb.Append(Parameter1 + fldSep);
sb.Append(Parameter2);
}
// The final line must not end with a field separator.
return sb.ToString();
}