The Mishna says that one is not allowed to receive payment for judging, testifying, or doing the mitzvos associated with para aduma, and if one receives payment he is penalized that we invalidate what he did. The gemara explains that this is based on a general principal of מה אני בחנם אף אתם בחנם, which is learned from a pasuk that demands that we model ourselves after G-d and not take payment for the doing of mitzvos such as teaching Torah or paskening on questions. Although there is already a prohibition against a judge taking a bribe, Tosafos explains that this source is necessary to forbid him from taking payment that is not meant to influence the outcome of the judgement (but if it influences the outcome in either direction, it is regarded as a bribe).
When the gemara cites the source for not accepting payment as being the requirement to model G-d and do it for free, Rashi gives the examples of authorizing a p'sak i.e. din, and teaching Torah. Rashi fails to mention the issue of testifying and preparing the para aduma waters. The Cheishek Shlomo suggests that accepting payment to testify according to Rashi is only prohibited m'drabonon, not from the Torah. If it were forbidden m'doraysa it would be tantamount to a witness taking money to testify falsely categorizing him as a רשע דחמס and being passul for all testimonly. The Mishna implies that we only invalidate this testimony, implying that his violation isn't d'oraysa. It also isn't clear from Rashi whether payment for other mitzvos is forbidden m'doraysa or only m'drabonon.
The Rashash cites the Bartneura who elaborates about those who officiate gittin and take payment. He is assuming that sidur ha'get qualifies as din and would invalidate the gett. However, the Rama (E.H. 154) says that it doesn't qualify as din, just as limud ha'torah. The Rashash points out that this would justify the gett being kasher, but wouldn't justify the practice to accept payment for officiating a gett since it would still be a violation of מה אני בחנם אף אתם בחנם as we find by talmud torah. Tosafos justifies those who take payment for studying Torah based on the דייני גזירות in Yerushalayim who relied on this for their livelihood and therefore became the community responsibility to support them so that they can maintain their important work. This logic may also apply to mesadrei gittin or those who do other mitzvos i.e. milah for their livelihood. Someone asked me how I can charge to do a bris - i answered that the mitzvah of bris milah is to cut. I don't charge for the mitzvah of doing a bris, I only charge for stopping the bleeding.