When a person, as a witness in a criminal or municipal court action, appears before a magistrate, grand jury, or court, upon a subpoena or in pursuance of an undertaking and it appears that the person:
1. Has come from a place outside the county; or2. Is poor and unable to pay the expenses of such attendance, the court, if the attendance of the witness is upon a trial, by order upon its minutes, or in any other case, the judge, by a written order, may direct the state in district court cases or the city in municipal court cases to pay the witness a reasonable sum to be specified in the order for the necessary expenses of the witness's attendance. Upon the production of the order or a certified copy thereof, the state or city, to whichever entity the order is directed, must pay the witness the sum specified. In district court cases, the attorney general shall pay prosecution witness expenses and the supreme court shall pay other witness expenses.