Easiest and most reliable affiliate route that saves a lot of tech time to build is a tapfiliate integration. Typically my preference but it incurs a monthly cost to use.
However, I detailed how to do it natively.
I’m pretty sure you got the bulk of it down but maybe there’s some info here that will help.
If you have specific questions on what you get stuck on drop them below and I’ll answer.