Masjids are built for the purpose of worship. It is not one of the functions of masjid to be a place of buying and selling, and the majority agree that it is forbidden to buy and sell in the masjid. The Hanafi scholars though, allowed a small scale business, but the strongest opinion is that the large scale transaction inside Masjid is forbidden while the small scale is disproved (Makrouh). However, it is more adequate to honor the masjid and prevent these transactions from taking place.
Dr Muhammad Bakr Ismail says:
Masjids are built for the purpose of worship and remembrance, and to gain knowledge and any other related religious matters, so it is not allowed to buy or sell in the masjid, according to the Hanbalis.
According to Ahmad in his book “Musnad” and in the “Sunan of Abu Dawoud” , that Amr bin Shuaib reported from his father, whom reported from his grandfather said: “The prophet ﷺ cautioned against buying and selling in the masjid”.
The Hanafi scholars on the other hand allowed buying and selling in the masjid if it is only small items, but if it becomes so much that the the masjid becomes a market place then it is highly disliked by them.
The most correct opinion is that dealing in transaction inside masjid is absolutely forbidden in case of large scale transaction, and it is disliked even if it is in a small quantity apart from when necessary. For example the masjid may have a library to sell religious texts or likewise things like pens and other stationary items, and they have been kept in a designated place.