Thats why i asked if i try buying GM with debt tokens to take the debt and pay it off with copper in play, and buy GM later. (Without copper in play)
Did i get it right, that we pay coins and potion *before* buying and *debt* after?
It is an illegal move to buy a Grand Market while Copper is in play. You can't even "try and fail." It would be like trying to buy a $6 Grand Market when you only have $3; it's just not something you're ever allowed to do or even attempt to do. So, no, you can't attempt to buy Grand Market with Copper in play just because you want to get the debt for some reason.
One of the "when you buy" effects that always triggers is "gain that card you pointed at".
No, gaining the card is not a when-buy effect, at least not what is normally referred to by that term. It doesn't have the same timing as things that happen "when you buy X"; it always occurs
after all those effects. (I suppose you could think of it the one and only "after-buy" effect, though.)
So, correct me, if i'm wrong, each time i play a treasure in buy phase, i check things i can and cannot buy, after i finished playing treasures i choose something to buy from thing i can buy, then coins and potion gets substracted from my count, then i trigger on-buys and everything goes after?
You "check" at all moments the game allows you to buy something (i.e. when it's during your buy phase and you have buys remaining (or when something else like Black Market tells you to buy something) and don't have any debt); in practice, you would wait until you'd played all treasures you were planning to before checking, but you could examine your options after each treasure you play, sure. At the moment you check, in addition to the requirements of the card needing to exist as the top card of a supply pile (or be allowed by Black Market), and you needing to have enough coins and potions to afford its cost, there may be any number of effects (Grand Market, Contraband, Mission, all "once-per-turn" events, Deluded...) that make a certain choice illegal.
When you make a (legal) choice of a thing to buy, you lose the +buy, the coins, and the potions, and you take the debt tokens, all according to the card's cost. (Losing the +buy is actually slightly different from those other things; the others happen
any time you buy a card, but losing the +buy is specifically part of normal buying during the buy phase and doesn't occur when buying from Black Market.)
(If the thing you bought was an event, then its effect occurs here. The rest applies only to buying cards.)
Then you trigger "when-buy" effects. (Tax's debt-taking happens here, in contrast to the debt-taking that happens when you buy a card with debt cost.)
Then you
would gain a card with the same name that the card you bought had at the time you bought it. (That convolution is required because the when-buy effects may have moved the exact card (see Talisman), so it might end up that you
would gain a different individual card than the one you bought. And it may be determined that there is no card that you
would gain at this point, like if you bought Sir Martin with Talisman out, in which case no "would-gain" effects would occur here.) Any "would-gain" effects happen here; those include effects on Trader and Possession (but does
not include cards like Nomad Camp and Ghost Town stating that they are gained to alternate locations... those just modify how the gain occurs when the gain instruction does not specify a destination, and they cannot be reordered like when-gain effects). If a would-gain effect causes something to happen "instead" of the gain, then the next paragraph gets skipped.
Finally, you gain the card it was determined that you "would gain" in the last paragraph. Again, if a would-gain caused something else to happen instead, you skip this part.
Then "when-gain" effects happen.