If you are buying an actual gift for someone with your money and not theirs then it is perfectly legal.
If you are buying a gun for someone else that is not permitted to own a firearm for what ever reason it is illegal.
If you are buying a gun for someone with the intention of them paying you back then it is illegal.