Codefree.co has a paid tutorial on how to do this:
Also, you can investigate how to create and store a random number (your coupon code) when your user is created and then when the person sends an invite, you send the coupon code that´s in their user record.
Here´s a couple of threads here that will shed some light on the subject: