Skip to content

added SlovenianUpnQr payload generator, manual QR version and ECI mode #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2018
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
147 changes: 125 additions & 22 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ namespace QRCoder
{
public static class PayloadGenerator
{
public class WiFi
public abstract class Payload
{
public virtual int Version { get { return -1; } }
public virtual QRCodeGenerator.ECCLevel EccLevel { get { return QRCodeGenerator.ECCLevel.M; } }
public virtual QRCodeGenerator.EciMode EciMode { get { return QRCodeGenerator.EciMode.Default; } }
public abstract override string ToString();
}

public class WiFi : Payload
{
private readonly string ssid, password, authenticationMode;
private readonly bool isHiddenSsid;
Expand Down Expand Up @@ -45,7 +53,7 @@ public enum Authentication
}
}

public class Mail
public class Mail : Payload
{
private readonly string mailReceiver, subject, message;
private readonly MailEncoding encoding;
Expand Down Expand Up @@ -117,7 +125,7 @@ public enum MailEncoding
}
}

public class SMS
public class SMS : Payload
{
private readonly string number, subject;
private readonly SMSEncoding encoding;
Expand Down Expand Up @@ -170,7 +178,7 @@ public enum SMSEncoding
}
}

public class MMS
public class MMS : Payload
{
private readonly string number, subject;
private readonly MMSEncoding encoding;
Expand Down Expand Up @@ -220,7 +228,7 @@ public enum MMSEncoding
}
}

public class Geolocation
public class Geolocation : Payload
{
private readonly string latitude, longitude;
private readonly GeolocationEncoding encoding;
Expand Down Expand Up @@ -258,7 +266,7 @@ public enum GeolocationEncoding
}
}

public class PhoneNumber
public class PhoneNumber : Payload
{
private readonly string number;

Expand All @@ -277,7 +285,7 @@ public override string ToString()
}
}

public class SkypeCall
public class SkypeCall : Payload
{
private readonly string skypeUsername;

Expand All @@ -296,7 +304,7 @@ public override string ToString()
}
}

public class Url
public class Url : Payload
{
private readonly string url;

Expand All @@ -316,7 +324,7 @@ public override string ToString()
}


public class WhatsAppMessage
public class WhatsAppMessage : Payload
{
private readonly string message;

Expand All @@ -336,7 +344,7 @@ public override string ToString()
}


public class Bookmark
public class Bookmark : Payload
{
private readonly string url, title;

Expand All @@ -357,7 +365,7 @@ public override string ToString()
}
}

public class ContactData
public class ContactData : Payload
{
private readonly string firstname;
private readonly string lastname;
Expand Down Expand Up @@ -534,7 +542,7 @@ public enum ContactOutputType
}
}

public class BitcoinAddress
public class BitcoinAddress : Payload
{
private readonly string address, label, message;
private readonly double? amount;
Expand Down Expand Up @@ -584,8 +592,8 @@ public override string ToString()
return $"bitcoin:{address}{query}";
}
}
public class SwissQrCode

public class SwissQrCode : Payload
{
//Keep in mind, that the ECC level has to be set to "M" when generating a SwissQrCode!
//SwissQrCode specification: https://www.paymentstandards.ch/dam/downloads/ig-qr-bill-de.pdf
Expand Down Expand Up @@ -615,7 +623,7 @@ public class SwissQrCode
public SwissQrCode(Iban iban, Currency currency, Contact creditor, Reference reference, Contact debitor = null, decimal? amount = null, DateTime? requestedDateOfPayment = null, Contact ultimateCreditor = null, string alternativeProcedure1 = null, string alternativeProcedure2 = null)
{
this.iban = iban;

this.creditor = creditor;
this.ultimateCreditor = ultimateCreditor;

Expand Down Expand Up @@ -949,7 +957,7 @@ public SwissQrCodeException(string message, Exception inner)
}
}

public class Girocode
public class Girocode : Payload
{
//Keep in mind, that the ECC level has to be set to "M" when generating a Girocode!
//Girocode specification: http://www.europeanpaymentscouncil.eu/index.cfm/knowledge-bank/epc-documents/quick-response-code-guidelines-to-enable-data-capture-for-the-initiation-of-a-sepa-credit-transfer/epc069-12-quick-response-code-guidelines-to-enable-data-capture-for-the-initiation-of-a-sepa-credit-transfer1/
Expand Down Expand Up @@ -1073,7 +1081,7 @@ public GirocodeException(string message, Exception inner)
}
}

public class BezahlCode
public class BezahlCode : Payload
{
//BezahlCode specification: http://www.bezahlcode.de/wp-content/uploads/BezahlCode_TechDok.pdf

Expand Down Expand Up @@ -1629,7 +1637,7 @@ public BezahlCodeException(string message, Exception inner)
}
}

public class CalendarEvent
public class CalendarEvent : Payload
{
private readonly string subject, description, location, start, end;
private readonly EventEncoding encoding;
Expand Down Expand Up @@ -1678,7 +1686,7 @@ public enum EventEncoding
}
}

public class OneTimePassword
public class OneTimePassword : Payload
{
//https://github.com/google/google-authenticator/wiki/Key-Uri-Format
public OneTimePasswordAuthType Type { get; set; } = OneTimePasswordAuthType.TOTP;
Expand Down Expand Up @@ -1805,7 +1813,7 @@ private void ProcessCommonFields(StringBuilder sb)
}
}

public class ShadowSocksConfig
public class ShadowSocksConfig : Payload
{
private readonly string hostname, password, tag, methodStr;
private readonly Method method;
Expand Down Expand Up @@ -1922,7 +1930,7 @@ public ShadowSocksConfigException(string message, Exception inner)
}
}

public class MoneroTransaction
public class MoneroTransaction : Payload
{
private readonly string address, txPaymentId, recipientName, txDescription;
private readonly float? txAmount;
Expand Down Expand Up @@ -1977,6 +1985,101 @@ public MoneroTransactionException(string message, Exception inner)
}
}

public class SlovenianUpnQr : Payload
{
//Keep in mind, that the ECC level has to be set to "M", version to 15 and ECI to EciMode.Iso8859_2 when generating a SlovenianUpnQr!
//SlovenianUpnQr specification: https://www.upn-qr.si/uploads/files/NavodilaZaProgramerjeUPNQR.pdf

private string _payerName = "";
private string _payerAddress = "";
private string _payerPlace = "";
private string _amount = "";
private string _code = "";
private string _purpose = "";
private string _deadLine = "";
private string _recipientIban = "";
private string _recipientName = "";
private string _recipientAddress = "";
private string _recipientPlace = "";
private string _recipientSiModel = "";
private string _recipientSiReference = "";

public override int Version { get { return 15; } }
public override QRCodeGenerator.ECCLevel EccLevel { get { return QRCodeGenerator.ECCLevel.M; } }
public override QRCodeGenerator.EciMode EciMode { get { return QRCodeGenerator.EciMode.Iso8859_2; } }

private string LimitLength(string value, int maxLength)
{
return (value.Length <= maxLength) ? value : value.Substring(0, maxLength);
}

public SlovenianUpnQr(string payerName, string payerAddress, string payerPlace, string recipientName, string recipientAddress, string recipientPlace, string recipientIban, string description, double amount, string recipientSiModel = "SI00", string recipientSiReference = "", string code = "OTHR") :
this(payerName, payerAddress, payerPlace, recipientName, recipientAddress, recipientPlace, recipientIban, description, amount, null, recipientSiModel, recipientSiReference, code)
{ }

public SlovenianUpnQr(string payerName, string payerAddress, string payerPlace, string recipientName, string recipientAddress, string recipientPlace, string recipientIban, string description, double amount, DateTime? deadline, string recipientSiModel = "SI99", string recipientSiReference = "", string code = "OTHR")
{
_payerName = LimitLength(payerName.Trim(), 33);
_payerAddress = LimitLength(payerAddress.Trim(), 33);
_payerPlace = LimitLength(payerPlace.Trim(), 33);
_amount = FormatAmount(amount);
_code = LimitLength(code.Trim().ToUpper(), 4);
_purpose = LimitLength(description.Trim(), 42);
_deadLine = (deadline == null) ? "" : deadline?.ToString("dd.MM.yyyy");
_recipientIban = LimitLength(recipientIban.Trim(), 34);
_recipientName = LimitLength(recipientName.Trim(), 33);
_recipientAddress = LimitLength(recipientAddress.Trim(), 33);
_recipientPlace = LimitLength(recipientPlace.Trim(), 33);
_recipientSiModel = LimitLength(recipientSiModel.Trim().ToUpper(), 4);
_recipientSiReference = LimitLength(recipientSiReference.Trim(), 22);
}

private string FormatAmount(double amount)
{
int _amt = (int)Math.Round(amount * 100.0);
return String.Format("{0:00000000000}", _amt);
}

private int CalculateChecksum()
{
int _cs = 5 + _payerName.Length; //5 = UPNQR constant Length
_cs += _payerAddress.Length;
_cs += _payerPlace.Length;
_cs += _amount.Length;
_cs += _code.Length;
_cs += _purpose.Length;
_cs += _deadLine.Length;
_cs += _recipientIban.Length;
_cs += _recipientName.Length;
_cs += _recipientAddress.Length;
_cs += _recipientPlace.Length;
_cs += _recipientSiModel.Length;
_cs += _recipientSiReference.Length;
_cs += 19;
return _cs;
}

public override string ToString()
{
var _sb = new StringBuilder();
_sb.Append("UPNQR");
_sb.Append('\n').Append('\n').Append('\n').Append('\n').Append('\n');
_sb.Append(_payerName).Append('\n');
_sb.Append(_payerAddress).Append('\n');
_sb.Append(_payerPlace).Append('\n');
_sb.Append(_amount).Append('\n').Append('\n').Append('\n');
_sb.Append(_code.ToUpper()).Append('\n');
_sb.Append(_purpose).Append('\n');
_sb.Append(_deadLine).Append('\n');
_sb.Append(_recipientIban.ToUpper()).Append('\n');
_sb.Append(_recipientSiModel).Append(_recipientSiReference).Append('\n');
_sb.Append(_recipientName).Append('\n');
_sb.Append(_recipientAddress).Append('\n');
_sb.Append(_recipientPlace).Append('\n');
_sb.AppendFormat("{0:000}", CalculateChecksum()).Append('\n');
return _sb.ToString();
}
}

private static bool IsValidIban(string iban)
{
Expand All @@ -1998,7 +2101,7 @@ private static string ConvertStringToEncoding(string message, string encoding)
return iso.GetString(isoBytes);
#else
return iso.GetString(isoBytes,0, isoBytes.Length);
#endif
#endif
}

private static string EscapeInput(string inp, bool simple = false)
Expand Down
Loading