UNPKG

payme-generator

Version:
5 lines (3 loc) 4.14 kB
import{simplePayment}from"bysquare";var IBAN_REGEX=/^[A-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}$/,PI_REGEX=/^\/VS[a-zA-Z0-9]{0,10}\/SS[a-zA-Z0-9]{0,10}\/KS[a-zA-Z0-9]{0,4}$/;class PayMeLink{version;IBAN;amount;currency;dueDate;paymentIdentifier;message;creditorName;constructor(input){let paramsObject;if(!input)paramsObject={V:"1",IBAN:"",AM:0,CC:"EUR"};else if(typeof input==="string"||input instanceof URL){let url=new URL(input.toString());paramsObject={V:url.searchParams.get("V"),IBAN:url.searchParams.get("IBAN"),AM:parseFloat(url.searchParams.get("AM")),CC:url.searchParams.get("CC"),DT:url.searchParams.get("DT"),PI:url.searchParams.get("PI"),MSG:url.searchParams.get("MSG"),CN:url.searchParams.get("CN")}}else if(typeof input==="object")paramsObject=input;else throw new Error("Invalid input type");if(!IBAN_REGEX.test(paramsObject.IBAN||"")&&input)throw new Error("Invalid IBAN format");if(this.version=paramsObject.V,this.IBAN=paramsObject.IBAN,this.amount=paramsObject.AM,this.version==="1"&&paramsObject.CC!=="EUR")throw new Error("Invalid currency code for version 1");if(this.currency=paramsObject.CC,this.dueDate=paramsObject.DT?new Date(PayMeLink.dateConverterISO2JS(paramsObject.DT)):void 0,this.paymentIdentifier=paramsObject.PI,this.paymentIdentifier&&this.paymentIdentifier.length>35)throw new Error("Payment identifier is too long, maximum length is 35 characters");else if(this.paymentIdentifier&&!PI_REGEX.test(this.paymentIdentifier))throw new Error("Invalid payment identifier format. Please use the following format “/VS{0,10}/SS{0,10}/KS{0,4}”");if(this.message=paramsObject.MSG,this.message&&this.message.length>140)throw new Error("Message is too long, maximum length is 140 characters");if(this.creditorName=paramsObject.CN,this.creditorName&&this.creditorName.length>70)throw new Error("Creditor name is too long, maximum length is 70 characters")}get params(){let params={V:this.version,IBAN:this.IBAN,AM:this.amount,CC:this.currency};if(this.dueDate)params.DT=this.dueDate.toISOString().split("T")[0];if(this.paymentIdentifier)params.PI=this.paymentIdentifier;if(this.message)params.MSG=this.message;if(this.creditorName)params.CN=this.creditorName;return params}setVersion(version){return this.version=version,this}setIBAN(iban){if(!IBAN_REGEX.test(iban))throw new Error("Invalid IBAN format");return this.IBAN=iban,this}setAmount(amount){if(amount<0)throw new Error("Amount must be a positive number");else if(amount>9999999)throw new Error("Amount is too large, maximum amount is 9999999");return this.amount=amount,this}setCurrency(currency){if(this.version==="1"&&currency!=="EUR")throw new Error("Invalid currency code for version 1");return this.currency=currency,this}setDueDate(dueDate){if(this.dueDate=new Date(dueDate),this.dueDate.toString()==="Invalid Date")throw new Error("Invalid date format");return this}setPaymentIdentifier(paymentIdentifier){if(paymentIdentifier.length>35)throw new Error("Payment identifier is too long, maximum length is 35 characters");return this.paymentIdentifier=paymentIdentifier,this}setMessage(message){if(message.length>140)throw new Error("Message is too long, maximum length is 140 characters");return this.message=message,this}setCreditorName(creditorName){if(creditorName.length>70)throw new Error("Creditor name is too long, maximum length is 70 characters");return this.creditorName=creditorName,this}getLink(){return this.toString()}getPayBySquare(){let[_symbol,variableSymbol,specificSymbol,constantSymbol]=this.paymentIdentifier?.split("/").map((_value)=>_value.match(/\d/g)?.join(""))||Array(4).fill("");return simplePayment({iban:this.IBAN,amount:this.amount,currencyCode:this.currency,variableSymbol,specificSymbol,constantSymbol,paymentNote:this.message})}toString(){return`https://payme.sk/?${new URLSearchParams({...this.params}).toString()}`}static dateConverterISO2JS(date){let[_,year,month,day]=date.match(/(\d{4})(\d{2})(\d{2})/)||[];if(!year||!month||!day)throw new Error("Invalid date format");return new Date(`${year}-${month}-${day}`)}}export{PayMeLink}; //# debugId=0599B3883D39CB9264756E2164756E21 //# sourceMappingURL=index.js.map