def payment(account_uuid, payment, time): account = _get_account(account_uuid) last_balance = AccountController.update_balances(account_uuid, time) principal_owed = last_balance.principal_owed interest_owed = last_balance.interest_owed try: principal, interest = make_payment(principal_owed, interest_owed, payment) except ValueError as ex: raise APIError(str(ex), SC.UNPROCESSABLE) account.balances.append( _create_balance(time=time, principal_owed=principal, interest_owed=interest, max_credit=account.max_credit)) account.payments.append( Payment(uuid=str(uuid.uuid4()), amount=payment, time=time)) current_app.db.add(account) current_app.db.commit() return serialize_account(account)