Whenever any application is made under this code and the application is accompanied by any fee which is excessive or not legally due, or whenever the department in consequence of any error either of fact or of law as to the proper amount of any fee or any penalty thereon or as to the necessity of obtaining any privilege under this code collects any fee or penalty which is excessive, erroneous, or not legally due, the person who has paid the erroneous or excessive fee or penalty, or his agent on his behalf, may apply for and receive a refund of the amount thereof as provided in this article, or the department may refund the same within three years after the date of the payment or collection.