3

I am working on a Django application. I need to include subscription service in the application. I am using the fields is_subscribed, subscription_start and subscription_end in the user profile models. I am using Razorpay for the payment integration. After successful payment I am updating the subscription status as follows:

UserProfile.objects.filter(user=request.user).update(is_subscribed=True,subscription_start=datetime.datetime.now(),subscription_end=datetime.datetime.now()+datetime.timedelta(days))

How to update the UserProfile automatically as soon the subscription_end is less than the current time. I want the model to be updated as is_subscribed as False as soon as the time is over. As of now, whenever a user visits any url, I am checking whether the user's subscription is valid. If not , then I am updating the model in this way

user = UserProfile.objects.filter(user=request.user).values()


 use1 = UserProfile.objects.filter(user=request.user,subscription_end__gte=datetime.datetime.now()).values()
 if not use1[0]['is_admin']:
     UserProfile.objects.filter(user=request.user).update(is_subscribed=False,joined=[])

. what is the correct way to update as soon as the subscription ends.

Tony stark
  • 198
  • 7
  • I believe you would need to create a management command and call it from a cron (maybe everyday at midnight). – Benbb96 Feb 09 '21 at 16:01
  • But what if someone's subscription ends in between two midnights(suppose). He can still access the functionalities – Tony stark Feb 09 '21 at 16:11
  • Then you could set your cron to be triggered every minutes but I think it would be better to have an event based trigger like the answer by @radwan-abu-odeh – Benbb96 Feb 09 '21 at 16:22

1 Answers1

0

You need to make use of RazorPay Webhooks, specifically Subscriptions Webhooks.

You need to configure your RazorPay account to trigger your subscription event and specify your Django App's Endpoint that is going to handle that specific event. You need to build your own endpoint that is going to handle that event when it is triggered. [Follow This Guide to configure your RazorPay Account]

The webhook event you need to be waiting for is subscription.pending.

Radwan Abu-Odeh
  • 1,897
  • 9
  • 16