They don't. They probably just inspect an order and see that a code was used. Not the same thing as generating one.
And no... you cannot just discount a cart. Shopify works by selling products with prices. You can invent any crazy scheme you want to mess with prices.... and call it discounting. That is your burden to bear. Many Apps go that route.