Undoubtedly a person who buys something then does not pay for it is stealing it, and stealing is a major sin, for which the Lawgiver has stipulated the hadd punishment of cutting off the right hand:
“And (as for) the male thief and the female thief, cut off (from the wrist joint) their (right) hands as a recompense for that which they committed, a punishment by way of example from Allaah”
[al-Maa'idah 5:38 – interpretation of the meaning]
The one who does this must also give back to people what is rightfully theirs. Whoever does not this is exposing himself to punishment and disgrace.
It was narrated that Abu Humayd al-Saa’idi said: The Messenger of Allaah (peace and blessings of Allaah be upon him) said: “By Allaah, none of you takes something that is not rightfully his but he will meet Allaah carrying it on the Day of Resurrection. I shall certainly recognize some of you coming to meet Allaah carrying a camel bellowing, or a cow mooing, or a sheep bleating.” Then he raised his hand so high that I could see the whiteness of his armpit and said, “O Allaah, have I conveyed (the message)?” (Narrated by al-Bukhaari, 6578; Muslim, 1832).
This person can return the property to its owner in whatever way he sees fit, without exposing his identity to him. If the person from whom it was stolen is in a place that he cannot reach, or he does not know who he is, then he has to give the value of the item in charity on behalf of its owner. If he knows the person then he may give him the choice between his accepting the fact that he gave charity on his behalf or taking what is rightfully his. If he chooses the charity option, he will have the reward of that, otherwise he (the thief) must give him what is rightfully his, and the (reward for) charity will go to the one who has repented, if his repentance is sincere.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) said:
If you have stolen from a person or from an organization, then you have to contact the person from whom you stole, and tell him “I have such and such that belongs to you.” Then you have to come to an agreement. But a person may feel that this is too difficult and that he cannot go to a person and say – for example – “I stole such and such from you and I took such and such from you.” In this case, he can make sure that this money – for example – reaches him in another, indirect, manner. For example, he could give it to one of the person’s friends, tell him the story and say, “I have now repented to Allaah and I hope that you will give it to him.”
If he does that, then Allaah says (interpretation of the meaning):
“And whosoever fears Allaah and keeps his duty to Him, He will make a way for him to get out (from every difficulty)”
“and whosoever fears Allaah and keeps his duty to Him, He will make his matter easy for him”
If we assume that you have stolen from someone and you do not know who he is or where he is, then this is easier, because you can give in charity an amount equal to what you stole, with the intention that the reward be for the owner (of the stolen goods). In that case you will have discharged your duty.
The story told by the questioner shows that a person must keep away from such things, because he may steal in a moment of ignorance and foolishness, and not get caught, but then if Allaah blesses him with guidance, he will have a hard time ridding himself of that.
Fataawa Islamiyyah, 4/162
The scholars of the Standing Committee said, concerning a soldier who stole from a person:
If he knows who the person is or knows someone who knows him, then he has to look for him and give him his silver coins, or the equivalent, or whatever he agrees upon with him. But if he does not know who he is, or he thinks that he will never find him, then he should give an equivalent amount of money in charity on behalf of the owner. Then if he finds him after that, he should tell him what he has done. If he accepts what he has done (giving charity), all well and good; and if does not accept what he did and asks him for his money, he should give it to him, and the (reward for) the charity will then be his. And he must ask Allaah for forgiveness and repent to him, and make du’aa’ for the owner.
Fataawa Islamiyyah, 4/165
And Allaah knows best.