When a payment is made upon any debt, it shall be applied first to the discharge of any interest due at the time, and the balance, if any, shall be applied to the reduction of the principal. If the payment does not extinguish the interest then due, no interest shall be calculated on such balance of interest and interest shall be calculated only on the principal amount up to the time of the next payment. Notwithstanding the foregoing restrictions against charging interest on unpaid interest:
(1) On loans having first priority on real estate and on loans secured by the pledge or assignment of instruments evidencing loans having first priority on real estate, the parties by written contract may lawfully agree that unpaid interest when due shall be added to the unpaid principal balance of the indebtedness and that the increased principal balance of the indebtedness bear interest pursuant to the terms of the contract; and(2) On loans secured by real estate or secured by real estate and other collateral, the parties by written contract may lawfully agree that, in the event of bankruptcy, the lender or creditor may include interest on its claim pursuant to the terms of the contract.