The Messenger of Allah ﷺ said, "A man said, 'I will surely give a charity tonight.' So he went out with his charity and placed it in the hand of a thief. In the morning, people were talking and saying, 'Charity was given to a thief last night.' He said, 'O Allah, to You belongs all praise. Charity to a thief! I will surely give a charity tonight.' So he went out with his charity and placed it in the hand of a prostitute. In the morning, people were talking and saying, 'Charity was given to a prostitute last night.' He said, 'O Allah, to You belongs all praise. Charity to a prostitute! I will surely give a charity tonight.' So he went out with his charity and placed it in the hand of a rich man. In the morning, people were talking and saying, 'Charity was given to a rich man.' He said, 'O Allah, to You belongs all praise. Charity to a prostitute, to a thief, and to a rich man!'Then he was visited and told, 'As for your charity, it has been accepted. As for the prostitute, perhaps she will refrain through it from her fornication. As for the thief, perhaps he will refrain through it from his theft. And as for the rich man, perhaps he will take heed and spend from what Allah has given him.'"
أخبرنا عمران بن بكار قال حدثنا علي بن عياش قال حدثنا شعيب قال حدثني أبو الزناد مما حدثه عبد الرحمن الأعرج مما ذكر أنه سمع
به عن رسول الله ﷺ وقال قال رجل لأتصدقن بصدقة فخرج بصدقته فوضعها في يد سارق فأصبحوا يتحدثون تصدق على سارق فقال اللهم لك الحمد على سارق لأتصدقن بصدقة فخرج بصدقته فوضعها في يد زانية فأصبحوا يتحدثون تصدق الليلة على زانية فقال اللهم لك الحمد على زانية لأتصدقن بصدقة فخرج بصدقته فوضعها في يد غني فأصبحوا يتحدثون تصدق على غني قال اللهم لك الحمد على زانية وعلى سارق وعلى غني فأتي فقيل له أما صدقتك فقد تقبلت أما الزانية فلعلها أن تستعف به من زناها ولعل السارق أن يستعف به عن سرقته ولعل الغني أن يعتبر فينفق مما أعطاه الله عز وجل
Bukhari transmitted it in the Book of Zakat, chapter "If one gives charity to a rich person without knowing it," hadith no. 1421, "etc.," hadith no. 1022, from the hadith of Shuayb bin Abu Hamza, and Muslim transmitted it in the Book of Zakat, chapter on the establishment of reward for the one who gives charity, the hadith of Abu Zinad with the same wording. It is also in al-Kubra, hadith no. 2302.