Agree with Ryan. I have used the WooCommerce subscriptions plugin before as well and it works well plus the support is very good. There are some other options out there - like s2member for one. These are both huge projects - trying to code something that handled this yourself would be extremely time consuming.