import { getReferenceType } from "../shared/utils.js" ; import { Data } from "./types" ; import QRCode from "@schoero/qrcode" ; export default function generateQRCode ( data , size ) { let qrString = "" ; qrString += "SPC" ; qrString += "\n0200" ; qrString += "\n1" ; qrString += "\n" + data . creditor . account ?? "\n" ; if ( data . creditor . buildingNumber !== undefined ) { qrString += "\nS" ; qrString += "\n" + data . creditor . name ; qrString += "\n" + data . creditor . address ; qrString += "\n" + data . creditor . buildingNumber ; qrString += "\n" + data . creditor . zip ; qrString += "\n" + data . creditor . city ; } else { qrString += "\nK" ; qrString += "\n" + data . creditor . name ; qrString += "\n" + data . creditor . address ; if ( ( data . creditor . zip + " " + data . creditor . city ) . length > 70 ) { throw new Error ( "Creditor zip plus city must be a maximum of 70 characters." ) ; } qrString += "\n" + data . creditor . zip + " " + data . creditor . city ; qrString += "\n" ; qrString += "\n" ; } qrString += "\n" + data . creditor . country ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; if ( data . amount !== undefined ) { qrString += "\n" + data . amount . toFixed ( 2 ) ; } else { qrString += "\n" ; } qrString += "\n" + data . currency ; if ( data . debtor !== undefined ) { if ( data . debtor . buildingNumber !== undefined ) { qrString += "\nS" ; qrString += "\n" + data . debtor . name ; qrString += "\n" + data . debtor . address ; qrString += "\n" + data . debtor . buildingNumber ; qrString += "\n" + data . debtor . zip ; qrString += "\n" + data . debtor . city ; } else { qrString += "\nK" ; qrString += "\n" + data . debtor . name ; qrString += "\n" + data . debtor . address ; if ( ( data . debtor . zip + " " + data . debtor . city ) . length > 70 ) { throw new Error ( "Debtor zip plus city must be a maximum of 70 characters." ) ; } qrString += "\n" + data . debtor . zip + " " + data . debtor . city ; qrString += "\n" ; qrString += "\n" ; } qrString += "\n" + data . debtor . country ; } else { qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; qrString += "\n" ; } qrString += "\n" + getReferenceType ( data . reference ) ; if ( data . reference !== undefined ) { qrString += "\n" + data . reference ; } else { qrString += "\n" ; } if ( data . message !== undefined ) { qrString += "\n" + data . message ; } else { qrString += "\n" ; } qrString += "\n" + "EPD" ; if ( data . additionalInformation !== undefined ) { qrString += "\n" + data . additionalInformation ; } else { qrString += "\n" ; } if ( data . av1 !== undefined ) { qrString += "\n" + data . av1 ; } if ( data . av2 !== undefined ) { qrString += "\n" + data . av2 ; } const qrcodeString = QRCode . toString ( qrString , { type : "svg" , margin : 0 , width : size , errorCorrectionLevel : "M" } , ( ) => { } ) as unknown as string ; return getSVGPathFromQRCodeString ( qrcodeString ) ! ; } function getSVGPathFromQRCodeString ( qrcodeString ) { const regex = /<path fill="#000000" d="(.*?)"\/>/m ; const match = regex . exec ( qrcodeString ) ; if ( match !== null ) { return match [ 1 ] ; } return ; }