In the United States, legal tender (cash in American currency) must be accepted for a debt, that is, an expense you have already incurred. Look at an American dollar bill. It says right on it "THIS NOTE IS LEGAL TENDER FOR ALL DEBTS PUBLIC AND PRIVATE." (Best money in the world. Just ask the drug dealers.)
A vendor such as a restaurant can inform you beforehand that they only accept credit cards. This can be done since no debt has been incurred at that point.
Amazon retail stores do not take cash. You can't go through the front door or turnstile unless you have made prior arrangements to pay another way. It's legal because you have not had the chance to incur a debt.
At a Costco gas station today you have to give the pump your credit card, no cash is accepted. There is no debt incurred when you pay in advance.
If you incur a debt and the vendor declines to take cash then you can offer them a check or to mail them a check later. At worst, the vendor calls the police for your nonpayment and you show the police your money and the police will be on your side.
A vendor is entitled to verify that the cash you offer is legitimate currency, that is, it isn't counterfeit and it adds up to the correct amount. If your purchase is for a large amount, eg, a new fire truck for $500,000, and the contract has already been signed, then they have to take cash.
Personally, in the latter case, as the seller of fire trucks, I would drive to the bank with you, give the bank the cash, and ask them to issue a cashier's check to you for the amount you give them. The bank would count the money automatically and check it for counterfeit bills. They will deposit the cash and issue you a check you can use to buy the fire truck. This is a small service for a customer. If I take a counterfeit bill I am stuck. If the bank takes a counterfeit bill it is stuck. (They could call the police on you, of course. And yes, fire trucks really do cost that much.)
A debt is different from a purchase you intend to make on credit.