This may need to be a bit improoved and customized for your situation
The command blocks are numbered from 1 to 7, always from left to right and generally in the order they will be used.
At first I created a scoreboard objective called count, it is supposed to keep track of how much dirt the system owes the player and I did not use a command block for it
/scoreboard objectives add count dummy
Command blocks 1 and 2 count how much dirt you have, by removing one dirt at a time and up to 64 dirt:
1:
/clear @p[scores={count=..63}] dirt 0 1
2:
/scoreboard players add @p count 1
Command block 3 will check if you had 64 dirt, 4 will subtract 64 from count if you did and 5 will give you $16 (if your money is based on a scoreboard objective called money)
3:
/testfor @p[scores={count=64..}]
4:
/scoreboard players remove @p count 64
5:
/scoreboard players add @p money 16
And command blocks 6 and 7 will give you your dirt back, in case you did not have enough:
6:
/give @p[scores={count=1..}] dirt 1
7:
/scoreboard players remove @p count 1
The chain command blocks are conditional and do not require redstone
The repeating command blocks and the regular command block are not conditional and they require redstone
You will probably have to modify this quite a bit to fit your particular setup, since this will break if there is more than one player who has dirt. This can probably be fixed by changing the target seletors to fit your situation