I borrowed some money, but before I paid it back to the person from whom I borrowed it, he asked me to buy something for him and said that he would pay me for it later on. Can I tell him when he gives the money to me that he does not need to do that, as what I bought for him is in return for what I owe him, even if what I owe him is less than what he owes me?.
Giving loans is a type of kindness and charity, and it is not permissible to stipulate that favours be done for the lender or to agree that he will get some benefit. The scholars are unanimously agreed that every loan which brings a benefit is riba.
What you asked about implies two things:
You are buying something for him. If this does not inconvenience you in any way or before borrowing from him you used to buy things for him, there is nothing wrong with it in that case. But if that will inconvenience you, and such services are usually paid for, and you did not do such things for him before you borrowed from him, then it is not permissible to do that for free, because then it is a benefit that results from the loan, and that is riba, as stated above.
It says in Zaad al-Mustaqni‘: If he gives something to the lender for free before paying off the loan that he does not usually give, that is not permissible unless the lender intends to respond in kind or deducts it from the debt.
You want to let him off with regard to the money that is additional to what you owe. There is nothing wrong with this letting him off if it was not stipulated when the loan was given. That is indicated by the report narrated by al-Bukhaari (2393) from the hadeeth of Abu Hurayrah (may Allah be pleased with him) who said: The Prophet (blessings and peace of Allah be upon him) owed a camel of a certain age to a man, and a man came to him and asked him to pay back the loan. The Prophet (blessings and peace of Allah be upon him) said: “Give it to him.” They looked for a camel of the same age but they could not find anything except a camel that was more mature. He said: “Give it to him, for the best of you is the best in paying off debts.”
Ibn Qudaamah (may Allah have mercy on him) said: If he lends in general terms without any stipulations, then the borrower pays back something that is more valuable or better than what he borrowed, or is less valuable or worse, with the consent of both parties, that is permissible. … Concessions allowing that were granted by Ibn ‘Umar, Sa‘eed ibn al-Musayyab, al-Hasan, al-Nakha‘i, al-Shu‘bi, al-Zuhri, Makhool, Qataadah, Maalik, al-Shaafa‘i and Ishaaq. And because the Prophet (blessings and peace of Allah upon him) borrowed a young camel and gave back something better than it, and he said: “The best of you is the best in paying off debts.” Agreed upon. And because this extra was not stipulated when the loan was given and was not a means of obtaining the loan, therefore it becomes halaal.
If a man is known for being good in paying off loans, it is not makrooh to lend to him. Al-Qaadi said: And there is a different opinion, that it is makrooh, because he has hope based on that person’s good habit. But this is not correct. The Prophet (blessings and peace of Allah be upon him) was known for being good in paying off loans but that does that make it justifiable for anyone to say that lending to him is makrooh. Moreover, the one who is known for being good in paying off loans is the best of people and is the most deserving of people to have his needs met and his requests answered and his hardship relieved. So it is not possible for that to be makrooh; rather what is not allowed is the additional amount that is stipulated.
End quote from al-Mughni, 4/212.
And Allah knows best.