In any action for the recovery of a debt, the defendant may plead payment of the debt (or of so much as is due by the condition) before action brought.
In any personal action, the defendant may pay into court, to the clerk, a sum of money on account of what is claimed, or by way of compensation or amends, and plead that he is not indebted to the plaintiff (or that the plaintiff has not sustained damages) to a greater amount than such sum.
The plaintiff may accept such sum, either in full satisfaction and then have judgment for his costs, or in part satisfaction and reply to the plea generally and if issue thereon be found for the defendant judgment shall be given for the defendant and he shall recover his costs.
In a suit for any debt, the defendant may at the trial prove and have allowed against such debt any payment or setoff which is so described in his plea, or in an account filed therewith, as to give the plaintiff notice of its nature, but not otherwise. Although the claim of the plaintiff be jointly against several persons, and the setoff be of a debt, not to all, but only to a part of them, this section shall extend to such setoff, if it appear that the persons against whom such claim is, stand in the relation of principal and surety, and that the person entitled to the setoff is the principal. And when the defendant is allowed to file and prove an account of setoff to the plaintiff's demand, the plaintiff shall be allowed to file and prove an account of counter setoff, and make such other defense as he might have made had an original action been brought upon such setoff, and, in the issue, the jury or judge shall ascertain the true state of indebtedness between the parties, and judgment shall be rendered accordingly.
In any action on a contract, the defendant may file a plea alleging any such failure in the consideration of the contract, or fraud in its procurement, or any such breach of any warranty to him of the title to real property or of the title or the soundness of personal property, for the price or value whereof he entered into the contract, or any other matter, as would entitle him either to recover damages at law from the plaintiff, or the person under whom the plaintiff claims, or to relief in equity, in whole or in part, against the obligation of the contract; or, if the contract be by deed, alleging any such matter existing before its execution, or any such mistake therein, or in the execution thereof, or any such other matter, as would entitle him to such relief in equity; and in either case alleging the amount to which he is entitled by reason of the matters contained in the plea. Every such plea shall be verified by affidavit.
If a defendant entitled to such plea as is mentioned in the preceding section shall not tender it, or though he tender it, if it be rejected for not being offered in due time, he shall not be precluded from such relief in equity as he would have been entitled to if the preceding section had not been enacted. If an issue in fact is joined on such plea and the same be found against the defendant, he shall be barred of relief in equity upon the matters alleged in the plea, unless upon such ground as would entitle a party to relief against a judgment in other cases. Every such issue in fact shall be upon a general replication that the plea is not true; and the plaintiff may give in evidence on such issue, any matter, which could be given in evidence under a special replication, if such replication were allowed.
Nothing in this article shall impair or affect the obligation of any bond or other deed deemed voluntary in law, upon any party thereto, or his representatives.
If the defendant file a plea or account of setoff which covers or applies to part of the plaintiff's demand, judgment may forthwith be rendered for the part not controverted and the costs accrued until the filing of the plea or account, and the case shall be proceeded with for the residue as if the part for which judgment was rendered had not been included therein. And if, in addition to such plea or account, the defendant plead some other plea, going to the whole or residue of the demand, the case shall not be continued as to the part not controverted by plea or account of setoff, unless good cause be shown for such continuance. A failure to take such judgment, however, at the term the plea or account is filed, shall not effect a discontinuance of the cause.
A defendant who files a plea or account under this article shall be deemed to have brought an action against the plaintiff (at the time of filing the same) for the matters mentioned in such plea or account, and the plaintiff shall not, after the plea or account is filed, dismiss his case without the defendant's consent, but shall be entitled to every ground of defense against the defendant's demand of which he might have availed himself by special plea or otherwise in any action brought against him upon the same demand. On the trial of the issue in such case, the jury shall ascertain the amount to which the defendant is entitled and apply it as a setoff against the plaintiff's demand, and, if such amount be more than the plaintiff is entitled to, shall ascertain the amount of the excess, including principal and interest. Judgment in such case shall be for the defendant against the plaintiff for such excess, with interest from the date of the judgment till payment.