Taken from the Man in Seat 61 website:
"There's a daily direct train from Budapest to Zagreb every afternoon around 13:25, journey 6 hours 20 minutes.
You can buy a cheap €29 ticket online at the Hungarian Railways website www.mav-start.hu from €29 (click EN top left for English).
There are other journey options involving a change of train which you can find using the online timetable at www.bahn.de/en, but you'll need to book these at the station as these cannot be booked online."
Buying online, in advance is always cheaper. Buying at the station will at least double, if not triple, the fare. You have waited quite late, so all of the low fares might be gone by now - especially as it is a holiday week. Yes, first class is more expensive than second class, but not really worth it.