It's certainly legal for a merchant to charge the customers a convenience or surcharge fee to accept credit and debit cards since the merchant is charged a percentage of the transaction when they accept credit and debit cards.
The merchant must let the customer know that a convenience fee or surcharge fee will be charged but yes they can charge you a surcharge to accept the credit card or debit card.
In actuality every merchant really does charge surcharge fees to accept credit and debit cards and many don't realize it.
They charge the surcharge by adding it into the price of the merchandise or services you buy with your debit card or credit card in stores and online.
When you use cash you're still paying other peoples surcharges through the purchase price of the product you're buying as well.