When a person attends before a magistrate, grand jury, or court, as a witness on behalf of the state, upon a subpoena or pursuant to an undertaking, and it appears that he has come from a place out of the county or that he is indigent, the court, if the attendance of a witness is for a trial, by an order entered upon its minutes, or in any other case, the circuit judge, by a written order, may direct the county treasurer to pay the witness a reasonable sum, to be specified in the order, for his expenses. Upon the production of the order, or a certified copy thereof, the county treasurer must pay the witness the sum specified therein, out of the county treasury.
SDCL 23A-14-7.1