djaodjin / djaodjin-saas

Django application for software-as-service and subscription businesses
Other
564 stars 124 forks source link

About auto renew implementation #141

Open VictorWangwz opened 5 years ago

VictorWangwz commented 5 years ago

Hi, Thx for ur work on this saas app. I am also working on handling subscription stuff now and ur app really helps a lot. I have a question that how do u realize auto renew, is there any configs to set? Previously, I used celery to create scheduled tasks to check it daily. I read the document that u also run cronjob daily with renew commends but do not explain how it is implemented in detail. Thus I raise this issue here.

smirolo commented 5 years ago

Thank you for the question. There is an auto_renew in the Plan model and an auto_renew in the Subscription model. When a subscriber (i.e. Organization instance) subscribes to a Plan with auto_renew == True, a Subscription with auto_renew == True is created.

The manage.py renewals command calls saas.renewals.extend_subscriptions(at_time). The algorithm looks for all Subscription with auto_renew == True that are also ending within a day of at_time and extends those, creating a Transaction in the process that later is used to create a Charge in saas.renewals.create_charges_for_balance.

The idea behind manage.py renewals and all other complexities in the code base usually have to deal with making sure that running the script a second time with the same at_time will not create unintended charges, etc.

I will keep this issue open until this gets merged into the documentation.

If you have other questions, please ask. We are thriving to improve and feedback greatly helps. Thank you.

smirolo commented 5 years ago

How renewals work and how to configure them is described here. The setup for a cronjob is described here. After re-reading the question, we should add documentation on setting up periodic tasks with Celery.