Praise be to Allah.
In this case, one of the following must apply:
1.Either the debt is deferred (to be paid at a later date), in which case there is nothing wrong with giving charity, if he hopes to be able to pay off the debt when it becomes due. Ibn ‘Uthaymeen (may Allah have mercy on him) said: If the debt is deferred, and when it comes you will have enough to pay it off, then you give charity and there is nothing wrong with that, because you are able to afford it. End quote from ash-Sharh al-Kaafi.
2.If the debt is due immediately, or it was deferred but the time for payment has now come, then it is not permissible for the debtor to give charity before paying off the debt, because paying off the debt is obligatory whereas charity is recommended, and that which is recommended cannot be given precedence over that which is obligatory. And delaying payment of debt (when it is due and one can afford to pay) is forbidden because the Prophet (blessings and peace of Allah be upon him) said: “For a rich man to delay payment (of debt) is wrongdoing.” Narrated by al-Bukhaari, 2287; Muslim, 1564
And it was narrated that Abu Hurayrah said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “There is no charity except when one is independent of means (and can afford it).” Narrated by Imam Ahmad, 6858.
Imam al-Bukhaari (may Allah have mercy on him) said: Whoever gives charity when he is in need or his family is in need, or when he owes a debt, should understand that the debt is more deserving of being paid off than charity, freeing a slave or giving a gift, and that (charity or gift) is invalid. He does not have the right to waste people’s money. The Prophet (blessings and peace of Allah be upon him) said: ‘Whoever takes people’s wealth intending to waste, Allah will destroy him.”
End quote from Saheeh al-Bukhaari, 2/112
Badr ad-Deen al-‘Ayni (may Allah have mercy on him) said: What is meant is that the condition of giving charity is that one should not be in need and one’s family should not be in need, and one should not be in debt. If one is indebt, what is required is to pay off one’s debts. Paying off debts is more important than giving charity, freeing slaves and giving gifts, because one should start with obligatory matters before naafil matters. No one has the right to destroy himself and his family whilst saving others. Rather he should help others after taking care of himself and his family, because taking care of them (himself and his family) is more obligatory for him than taking care of other people.
End quote from ‘Umdat al-Qaari’ Sharh Saheeh al-Bukhaari, 13/327.
Ibn Battaal (may Allah have mercy on him) said: With regard to his words, “Whoever gives charity when he owes a debt, should understand that the debt is more deserving of being paid off than charity, freeing a slave or giving a gift, and that (charity or gift) is invalid,” there is scholarly consensus concerning that.
End quote from Sharh Saheeh al-Bukhaari, 3/430
It says in al-Minhaaj ma‘a Sharhihi Mughni al-Muhtaaj, 4/197: If a person owes a debt, it is mustahabb for him not to give charity until he has paid off what he owes. I said: The more correct view is that it is haraam for him to give in charity what he needs for the maintenance of those on whom he is obliged to spend or to pay towards a debt he has no hope of being able to pay off. See also Rawdat at-Taalibeen, 2/342
Ibn Qudaamah (may Allah have mercy on him) said: If a person owes a debt, it is not permissible for him to give charity that will prevent him paying it off, because it is obligatory (to pay it off) and it is not permissible to refrain from doing so.
End quote from al-Kaafi, 1/431
But the scholars (may Allah have mercy on them) made an exception with regard to small amounts which will not prevent one from paying off a debt. Al-Adhra‘i, who was one of the Shaafa‘is, said: This – the prohibition (on a debtor giving charity) – is not absolute, because no one says, as far as I know, that if the one who owes a mahr and the like gives something like a loaf of bread in charity, which we may be certain that if he kept it he would not give it towards paying off the debt, that it is not mustahabb for him to give it in charity. Rather what is meant is that hastening to pay one’s dues is more important and more necessary than doing voluntary actions in general terms.
End quote from Nihaayat al-Muhtaaj, 7/181. See also: Haashiyat Qalyooni wa ‘Umayrah, 3/206
And Allah knows best.