It has nothing to do with pied pipers.
There is some debate about the exact origin of the phrase and whether "pay the piper" or "who pays the piper calls the tune" came first.
Wiki suggests that "pays the piper" comes "From the English phrase who pays the piper calls the tune."
In other words, to pay up to gain control of something.
Definition:
(idiomatic) To pay expenses for something, and thus be in a position to be in control (i.e. to be able to call the tune).
(idiomatic) To pay a monetary debt or experience unfavorable consequences, especially when the payment or consequences are inevitable in spite of attempts to avoid them. ?
https://en.wiktionary.org/wiki/pay_the_piper
This site suggests that "pay the piper" came first, and that the "calls the tune" part came afterwards. It changes the meaning a bit, but clearly has nothing to do with rats.
"'He who pays the piper calls the tune.' It is interesting to discover how the usage of this proverb has changed.[1] The simple phrase 'pay the piper' predates the longer version by some centuries. It was used simply to mean 'bear the cost', with no reference at all to controlling the piper's playing. "
[1] See further F. P. Wilson, The Oxford Dictionary of English Proverbs, and B. J. Whiting, Modern Proverbs and Proverbial Sayings.
https://vdocuments.site/english-proverbs-and-dictionaries-of-proverbs-1.html