SwissQRCode.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. namespace FastReport.Barcode
  6. {
  7. /// <summary>
  8. /// Represents a class that contains all parameters of Swiss QR Code.
  9. /// </summary>
  10. public class QRSwissParameters
  11. {
  12. #region private fields
  13. private Iban iban;
  14. private Currency? currency;
  15. private Contact creditor;
  16. private Reference reference;
  17. private AdditionalInformation additionalInformation;
  18. private Contact debitor;
  19. private decimal? amount;
  20. private string alternativeProcedure1;
  21. private string alternativeProcedure2;
  22. #endregion
  23. #region public properties
  24. /// <summary>
  25. /// IBAN object
  26. /// </summary>
  27. public Iban Iban { get { return iban; } set { iban = value; } }
  28. /// <summary>
  29. /// (either EUR or CHF)
  30. /// </summary>
  31. public Currency? Currency { get { return currency; } set { currency = value; } }
  32. /// <summary>
  33. /// Creditor (payee) information
  34. /// </summary>
  35. public Contact Creditor { get { return creditor; } set { creditor = value; } }
  36. /// <summary>
  37. /// Reference information
  38. /// </summary>
  39. public Reference Reference { get { return reference; } set { reference = value; } }
  40. /// <summary>
  41. /// Can be null
  42. /// </summary>
  43. public AdditionalInformation AdditionalInformation { get { return additionalInformation; } set { additionalInformation = value; } }
  44. /// <summary>
  45. /// Debitor (payer) information
  46. /// </summary>
  47. public Contact Debitor { get { return debitor; } set { debitor = value; } }
  48. /// <summary>
  49. /// Amount
  50. /// </summary>
  51. public decimal? Amount { get { return amount; } set { amount = value; } }
  52. /// <summary>
  53. /// Optional command for alternative processing mode - line 1
  54. /// </summary>
  55. public string AlternativeProcedure1 { get { return alternativeProcedure1; } set { alternativeProcedure1 = value; } }
  56. /// <summary>
  57. /// Optional command for alternative processing mode - line 2
  58. /// </summary>
  59. public string AlternativeProcedure2 { get { return alternativeProcedure2; } set { alternativeProcedure2 = value; } }
  60. #endregion
  61. }
  62. public class AdditionalInformation
  63. {
  64. private string unstructuredMessage, billInformation, trailer;
  65. /// <summary>
  66. /// Creates an additional information object. Both parameters are optional and must be shorter than 141 chars in combination.
  67. /// </summary>
  68. /// <param name="unstructuredMessage">Unstructured text message</param>
  69. /// <param name="billInformation">Bill information</param>
  70. public AdditionalInformation(string unstructuredMessage, string billInformation)
  71. {
  72. MyRes res = new MyRes("Messages,Swiss");
  73. if (((unstructuredMessage != null ? unstructuredMessage.Length : 0) + (billInformation != null ? billInformation.Length : 0)) > 140)
  74. throw new SwissQrCodeException(res.Get("SwissUnstructBillLength"));
  75. this.unstructuredMessage = unstructuredMessage;
  76. this.billInformation = billInformation;
  77. this.trailer = "EPD";
  78. }
  79. public AdditionalInformation(string addInfo)
  80. {
  81. string[] data = addInfo.Split('\r');
  82. this.trailer = data[1].Trim();
  83. this.unstructuredMessage = data[0].Trim();
  84. this.billInformation = data[2].Trim();
  85. }
  86. public string UnstructureMessage
  87. {
  88. get { return !string.IsNullOrEmpty(unstructuredMessage) ? unstructuredMessage.Replace("\n", "") : null; }
  89. set { this.unstructuredMessage = value; }
  90. }
  91. public string BillInformation
  92. {
  93. get { return !string.IsNullOrEmpty(billInformation) ? billInformation.Replace("\n", "") : null; }
  94. set { this.billInformation = value; }
  95. }
  96. public string Trailer
  97. {
  98. get { return trailer; }
  99. }
  100. }
  101. public class Reference
  102. {
  103. private ReferenceType referenceType;
  104. private string reference;
  105. private ReferenceTextType? referenceTextType;
  106. public ReferenceType RefType
  107. {
  108. get { return referenceType; }
  109. set { referenceType = value; }
  110. }
  111. public string ReferenceText
  112. {
  113. get { return !string.IsNullOrEmpty(reference) ? reference.Replace("\n", "") : null; }
  114. set { reference = value; }
  115. }
  116. public ReferenceTextType? _ReferenceTextType
  117. {
  118. get { return referenceTextType; }
  119. set { referenceTextType = value; }
  120. }
  121. /// <summary>
  122. /// Creates a reference object which must be passed to the SwissQrCode instance
  123. /// </summary>
  124. /// <param name="referenceType">Type of the reference (QRR, SCOR or NON)</param>
  125. /// <param name="reference">Reference text</param>
  126. /// <param name="referenceTextType">Type of the reference text (QR-reference or Creditor Reference)</param>
  127. public Reference(ReferenceType referenceType, string reference, ReferenceTextType? referenceTextType)
  128. {
  129. MyRes res = new MyRes("Messages,Swiss");
  130. this.referenceType = referenceType;
  131. this.referenceTextType = referenceTextType;
  132. if (referenceType == ReferenceType.NON && reference != null)
  133. throw new SwissQrCodeException(res.Get("SwissRefTypeNon"));
  134. if (referenceType != ReferenceType.NON && reference != null && referenceTextType == null)
  135. throw new SwissQrCodeException(res.Get("SwissRefTextTypeNon"));
  136. if (referenceTextType == ReferenceTextType.QrReference && reference != null && (reference.Length > 27))
  137. throw new SwissQrCodeException(res.Get("SwissRefQRLength"));
  138. if (referenceTextType == ReferenceTextType.QrReference && reference != null && !Regex.IsMatch(reference, @"^[0-9]+$"))
  139. throw new SwissQrCodeException(res.Get("SwissRefQRNotOnlyDigits"));
  140. if (referenceTextType == ReferenceTextType.QrReference && reference != null && !ChecksumMod10(reference))
  141. throw new SwissQrCodeException(res.Get("SwissRefQRCheckSum"));
  142. if (referenceTextType == ReferenceTextType.CreditorReferenceIso11649 && reference != null && (reference.Length > 25))
  143. throw new SwissQrCodeException(res.Get("SwissRefISOLength"));
  144. this.reference = reference;
  145. }
  146. public Reference(string reference)
  147. {
  148. string[] data = reference.Split('\r');
  149. switch (data[0].Trim())
  150. {
  151. case "QRR":
  152. this.referenceType = ReferenceType.QRR;
  153. break;
  154. case "SCOR":
  155. this.referenceType = ReferenceType.SCOR;
  156. break;
  157. case "NON":
  158. this.referenceType = ReferenceType.NON;
  159. break;
  160. }
  161. this.reference = data[1].Trim();
  162. }
  163. /// <summary>
  164. /// Reference type. When using a QR-IBAN you have to use either "QRR" or "SCOR"
  165. /// </summary>
  166. public enum ReferenceType
  167. {
  168. QRR,
  169. SCOR,
  170. NON
  171. }
  172. public enum ReferenceTextType
  173. {
  174. QrReference,
  175. CreditorReferenceIso11649
  176. }
  177. public bool ChecksumMod10(string digits)
  178. {
  179. if (string.IsNullOrEmpty(digits) || digits.Length < 2)
  180. return false;
  181. int[] mods = new int[] { 0, 9, 4, 6, 8, 2, 7, 1, 3, 5 };
  182. int remainder = 0;
  183. for (int i = 0; i < digits.Length - 1; i++)
  184. {
  185. int num = Convert.ToInt32(digits[i]) - 48;
  186. remainder = mods[(num + remainder) % 10];
  187. }
  188. int checksum = (10 - remainder) % 10;
  189. return checksum == Convert.ToInt32(digits[digits.Length - 1]) - 48;
  190. }
  191. }
  192. public class Contact
  193. {
  194. private List<string> twoLetterCodes;
  195. private string br = "\r\n";
  196. private string name, streetOrAddressline1, houseNumberOrAddressline2, zipCode, city, country;
  197. private AddressType adrType;
  198. public string Name { get { return name; } set { name = value; } }
  199. public string StreetOrAddressline { get { return streetOrAddressline1; } set { streetOrAddressline1 = value; } }
  200. public string HouseNumberOrAddressline { get { return houseNumberOrAddressline2; } set { houseNumberOrAddressline2 = value; } }
  201. public string ZipCode { get { return zipCode; } set { zipCode = value; } }
  202. public string City { get { return city; } set { city = value; } }
  203. public string Country { get { return country; } set { country = value; } }
  204. /// <summary>
  205. /// Contact type. Can be used for payee, ultimate payee, etc. with address in structured mode (S).
  206. /// </summary>
  207. /// <param name="name">Last name or company (optional first name)</param>
  208. /// <param name="zipCode">Zip-/Postcode</param>
  209. /// <param name="city">City name</param>
  210. /// <param name="country">Two-letter country code as defined in ISO 3166-1</param>
  211. /// <param name="street">Streetname without house number</param>
  212. /// <param name="houseNumber">House number</param>
  213. public Contact(string name, string zipCode, string city, string country, string street, string houseNumber) : this(name, zipCode, city, country, street, houseNumber, AddressType.StructuredAddress)
  214. {
  215. }
  216. /// <summary>
  217. /// Contact type. Can be used for payee, ultimate payee, etc. with address in combined mode (K).
  218. /// </summary>
  219. /// <param name="name">Last name or company (optional first name)</param>
  220. /// <param name="country">Two-letter country code as defined in ISO 3166-1</param>
  221. /// <param name="addressLine1">Adress line 1</param>
  222. /// <param name="addressLine2">Adress line 2</param>
  223. public Contact(string name, string country, string addressLine1, string addressLine2) : this(name, null, null, country, addressLine1, addressLine2, AddressType.CombinedAddress)
  224. {
  225. }
  226. private Contact(string name, string zipCode, string city, string country, string streetOrAddressline1, string houseNumberOrAddressline2, AddressType addressType)
  227. {
  228. twoLetterCodes = ValidTwoLetterCodes();
  229. MyRes res = new MyRes("Messages,Swiss");
  230. MyRes resForms = new MyRes("Forms,BarcodeEditor,Swiss");
  231. //Pattern extracted from https://qr-validation.iso-payments.ch as explained in https://github.com/codebude/QRCoder/issues/97
  232. string charsetPattern = @"^([a-zA-Z0-9\.,;:'\ \+\-/\(\)?\*\[\]\{\}\\`´~ ]|[!""#%&<>÷=@_$£]|[àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ])*$";
  233. this.adrType = addressType;
  234. if (string.IsNullOrEmpty(name))
  235. throw new SwissQrCodeContactException(String.Format(res.Get("SwissEmptyProperty"), resForms.Get("Name")));
  236. if (name.Length > 70)
  237. throw new SwissQrCodeContactException(String.Format(res.Get("SwissLengthMore"), resForms.Get("Name"), 71));
  238. if (!Regex.IsMatch(name, charsetPattern))
  239. throw new SwissQrCodeContactException(String.Format(res.Get("SwissPatternError"), resForms.Get("Name")) + charsetPattern);
  240. this.name = name;
  241. if (AddressType.StructuredAddress == this.adrType)
  242. {
  243. if (!string.IsNullOrEmpty(streetOrAddressline1) && (streetOrAddressline1.Length > 70))
  244. throw new SwissQrCodeContactException(String.Format(res.Get("SwissLengthMore"), resForms.Get("Street"), 71));
  245. if (!string.IsNullOrEmpty(streetOrAddressline1) && !Regex.IsMatch(streetOrAddressline1, charsetPattern))
  246. throw new SwissQrCodeContactException(String.Format(res.Get("SwissPatternError"), resForms.Get("Street")) + charsetPattern);
  247. this.streetOrAddressline1 = streetOrAddressline1;
  248. if (!string.IsNullOrEmpty(houseNumberOrAddressline2) && houseNumberOrAddressline2.Length > 16)
  249. throw new SwissQrCodeContactException(String.Format(res.Get("SwissLengthMore"), resForms.Get("HouseNumber"), 71));
  250. this.houseNumberOrAddressline2 = houseNumberOrAddressline2;
  251. }
  252. else
  253. {
  254. if (!string.IsNullOrEmpty(streetOrAddressline1) && (streetOrAddressline1.Length > 70))
  255. throw new SwissQrCodeContactException(String.Format(res.Get("SwissLengthMore"), "Address line 1", 71));
  256. if (!string.IsNullOrEmpty(streetOrAddressline1) && !Regex.IsMatch(streetOrAddressline1, charsetPattern))
  257. throw new SwissQrCodeContactException(String.Format(res.Get("SwissPatternError"), "Address line 1") + charsetPattern);
  258. this.streetOrAddressline1 = streetOrAddressline1;
  259. if (string.IsNullOrEmpty(houseNumberOrAddressline2))
  260. throw new SwissQrCodeContactException(res.Get("SwissAddressLine2Error"));
  261. if (!string.IsNullOrEmpty(houseNumberOrAddressline2) && (houseNumberOrAddressline2.Length > 70))
  262. throw new SwissQrCodeContactException(String.Format(res.Get("SwissLengthMore"), "Address line 2", 71));
  263. if (!string.IsNullOrEmpty(houseNumberOrAddressline2) && !Regex.IsMatch(houseNumberOrAddressline2, charsetPattern))
  264. throw new SwissQrCodeContactException(String.Format(res.Get("SwissPatternError"), "Address line 2") + charsetPattern);
  265. this.houseNumberOrAddressline2 = houseNumberOrAddressline2;
  266. }
  267. if (AddressType.StructuredAddress == this.adrType)
  268. {
  269. if (string.IsNullOrEmpty(zipCode))
  270. throw new SwissQrCodeContactException(String.Format(res.Get("SwissEmptyProperty"), resForms.Get("ZipCode")));
  271. if (zipCode.Length > 16)
  272. throw new SwissQrCodeContactException(String.Format(res.Get("SwissLengthMore"), resForms.Get("ZipCode"), 17));
  273. if (!Regex.IsMatch(zipCode, charsetPattern))
  274. throw new SwissQrCodeContactException(String.Format(res.Get("SwissPatternError"), resForms.Get("ZipCode")) + charsetPattern);
  275. this.zipCode = zipCode;
  276. if (string.IsNullOrEmpty(city))
  277. throw new SwissQrCodeContactException(String.Format(res.Get("SwissEmptyProperty"), resForms.Get("City")));
  278. if (city.Length > 35)
  279. throw new SwissQrCodeContactException(String.Format(res.Get("SwissLengthMore"), resForms.Get("City"), 36));
  280. if (!Regex.IsMatch(city, charsetPattern))
  281. throw new SwissQrCodeContactException(String.Format(res.Get("SwissPatternError"), resForms.Get("City")) + charsetPattern);
  282. this.city = city;
  283. }
  284. else
  285. {
  286. this.zipCode = this.city = string.Empty;
  287. }
  288. if (!IsValidTwoLetterCode(country))
  289. throw new SwissQrCodeContactException(res.Get("SwissCountryTwoLetters"));
  290. this.country = country;
  291. }
  292. private bool IsValidTwoLetterCode(string code)
  293. {
  294. return twoLetterCodes.Contains(code);
  295. }
  296. private List<string> ValidTwoLetterCodes()
  297. {
  298. string[] codes = new string[] { "AF", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "CV", "KH", "CM", "CA", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", "CI", "HR", "CU", "CW", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "SZ", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "MK", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU", "RW", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", "SD", "SR", "SJ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW", "AX" };
  299. List<string> codesList = new List<string>();
  300. foreach (string str in codes)
  301. {
  302. codesList.Add(str);
  303. }
  304. return codesList;
  305. }
  306. public Contact(string contact)
  307. {
  308. string[] data = contact.Split('\r');
  309. if (data[0].Trim() == "S")
  310. this.adrType = AddressType.StructuredAddress;
  311. else
  312. this.adrType = AddressType.CombinedAddress;
  313. name = data[1].Trim();
  314. streetOrAddressline1 = data[2].Trim();
  315. houseNumberOrAddressline2 = data[3].Trim();
  316. zipCode = data[4].Trim();
  317. city = data[5].Trim();
  318. country = data[6].Trim();
  319. }
  320. public override string ToString()
  321. {
  322. string contactData = ""; //AdrTp
  323. if (AddressType.StructuredAddress == adrType)
  324. contactData += "S";
  325. else
  326. contactData += "K";
  327. contactData += br;
  328. contactData += name.Replace("\n", "") + br; //Name
  329. contactData += (!string.IsNullOrEmpty(streetOrAddressline1) ? streetOrAddressline1.Replace("\n", "") : string.Empty) + br; //StrtNmOrAdrLine1
  330. contactData += (!string.IsNullOrEmpty(houseNumberOrAddressline2) ? houseNumberOrAddressline2.Replace("\n", "") : string.Empty) + br; //BldgNbOrAdrLine2
  331. contactData += zipCode.Replace("\n", "") + br; //PstCd
  332. contactData += city.Replace("\n", "") + br; //TwnNm
  333. contactData += country + br; //Ctry
  334. return contactData;
  335. }
  336. public enum AddressType
  337. {
  338. StructuredAddress,
  339. CombinedAddress
  340. }
  341. public class SwissQrCodeContactException : SwissQrCodeException
  342. {
  343. public SwissQrCodeContactException()
  344. {
  345. }
  346. public SwissQrCodeContactException(string message)
  347. : base(message)
  348. {
  349. }
  350. public SwissQrCodeContactException(string message, SwissQrCodeException inner)
  351. : base(message, inner)
  352. {
  353. }
  354. }
  355. }
  356. public class Iban
  357. {
  358. private string iban;
  359. private IbanType? ibanType;
  360. public IbanType? TypeIban { get { return ibanType; } set { ibanType = value; } }
  361. public string _Iban { get { return iban; } set { iban = value; } }
  362. /// <summary>
  363. /// IBAN object with type information
  364. /// </summary>
  365. /// <param name="iban">IBAN</param>
  366. /// <param name="ibanType">Type of IBAN (normal or QR-IBAN)</param>
  367. public Iban(string iban, IbanType ibanType)
  368. {
  369. MyRes res = new MyRes("Messages,Swiss");
  370. if (ibanType == IbanType.Iban && !IsValidIban(iban))
  371. throw new SwissQrCodeException(res.Get("SwissIbanNotValid"));
  372. if (ibanType == IbanType.QrIban && !IsValidQRIban(iban))
  373. throw new SwissQrCodeException(res.Get("SwissQRIbanNotValid"));
  374. if (!iban.StartsWith("CH") && !iban.StartsWith("LI"))
  375. throw new SwissQrCodeException("SwissQRStartNotValid");
  376. this.iban = iban;
  377. this.ibanType = ibanType;
  378. }
  379. public bool IsQrIban
  380. {
  381. get { return ibanType == IbanType.QrIban; }
  382. }
  383. public Iban(string iban)
  384. {
  385. this.iban = iban;
  386. }
  387. public override string ToString()
  388. {
  389. return iban.Replace("-", "").Replace("\n", "").Replace(" ", "");
  390. }
  391. public enum IbanType
  392. {
  393. Iban,
  394. QrIban
  395. }
  396. private bool IsValidIban(string iban)
  397. {
  398. //Clean IBAN
  399. string ibanCleared = iban.ToUpper().Replace(" ", "").Replace("-", "");
  400. //Check for general structure
  401. bool structurallyValid = Regex.IsMatch(ibanCleared, @"^[a-zA-Z]{2}[0-9]{2}([a-zA-Z0-9]?){16,30}$");
  402. //Check IBAN checksum
  403. char[] charSum = (ibanCleared.Substring(4) + ibanCleared.Substring(0, 4)).ToCharArray();
  404. string sum = "";
  405. foreach (char c in charSum)
  406. {
  407. sum += (char.IsLetter(c) ? (c - 55).ToString() : c.ToString());
  408. }
  409. decimal sumDec;
  410. if (!decimal.TryParse(sum, out sumDec))
  411. return false;
  412. bool checksumValid = (sumDec % 97) == 1;
  413. return structurallyValid && checksumValid;
  414. }
  415. private bool IsValidQRIban(string iban)
  416. {
  417. bool foundQrIid = false;
  418. try
  419. {
  420. string ibanCleared = iban.ToUpper().Replace(" ", "").Replace("-", "");
  421. int possibleQrIid = Convert.ToInt32(ibanCleared.Substring(4, 5));
  422. foundQrIid = possibleQrIid >= 30000 && possibleQrIid <= 31999;
  423. }
  424. catch { }
  425. return IsValidIban(iban) && foundQrIid;
  426. }
  427. }
  428. public enum Currency
  429. {
  430. CHF = 756,
  431. EUR = 978
  432. }
  433. }