Gauge invariance beyond the electric dipole approximation