Contract clauses don't make things illegal. In the countries where it's illegal it's because the government has made it so, presumably because of lobbying by the card companies or large businesses. See the UK for example: https://www.gov.uk/government/news/card-surcharge-ban-means-...
Surcharges are generally considered anti-consumer, because businesses often hide them until the final payment step, disguising the true cost of the product and reducing price transparency.