If any officer, for performing an official duty for which a fee or compensation is allowed or provided by law, knowingly demand and receive a greater fee or compensation than is so allowed or provided, he shall be guilty of a misdemeanor, and, upon conviction, shall be fined not exceeding $50.