Skip to content

API viewset for Django Flexible Subscriptions and few helper methods

License

Notifications You must be signed in to change notification settings

ydaniels/drf-django-flexible-subscriptions

Repository files navigation

drf-django-flexible-subscriptions

build-status-image pypi-version PyPI - Python Version Django Version DRF Version

Overview

Expose API views for Django Flexible Subscriptions

Requirements

  • Python (3.6, 3.7, 3.8)
  • Django (2.0, 2.1, 2.2, 3.0)
  • Django REST Framework (3.9, 3.10, 3.11)

Installation

Install using pip

$ pip install drf-django-flexible-subscriptions

Example

To use in
settings.py
INSTALLED_APPS = [
                    ...,
                    'rest_framework',
                    'subscriptions_api',
                    ]

urls.py*

url(r'^api/subscriptions/', include('subscriptions_api.urls')),
This will expose the following django-flexible-subscriptions endpoints with only read access for normal user and write access for admin
  • api/subscriptions/plan-tags/
  • api/subscriptions/plan-costs/
  • api/subscriptions/planlist-details/
  • api/subscriptions/planlist/
  • api/subscriptions/subscription-plans/
  • api/subscriptions/subscription-transactions/
  • api/subscriptions/user-subscriptions/

drf-django-flexible-subscriptions provides helper methods and models (check models.py), so you can implement your payment logic in any way you want without binding to a specific view e.g

your models.py

from subscriptions_api.models import PlanCost, UserSubscription

class PaymentModel(models.Model):
     cost = models.ForeignKey(PlanCost, null=True, on_delete=models.SET_NULL)

views.py

#Create SubscriptionPlan and PlanCost from django admin
#A user is added to the group on subscription plan after subscription is activated.
#SubscriptionPlan features field can be json of dict e.g {"can_send_message": true } that can be accessed
#directly from the subscription plan obj

#New user can automatically be subscribed to a plan cost by setting DFS_DEFAULT_PLAN_COST_ID in settings.py  to ID of a plan cost
# you want user to be subscribed to

#you can subscribe user to a plan by
subscription = cost.setup_user_subscription(request.user, active=False, no_multiple_subscription=True)

#with active=False user subscription would not be activated immediately use active=True for otherwise

#no_multiple_subscription prevents user from being subscribed to more than one plan at time previous plan will be removed

#After successful user payment you can activate_subscription by using subscription above or

subscription = UserSubscription.objects.get(user=request.user)

#or for user with multiple active subscription when no_multiple_subscription=False

subscription = UserSubscription.objects.get(user=request.user, cost=cost)

subscription.activate() #Activate  subscription and user is added to the group on subscription plan which can use for your views and general       #django permissions

if user_subscripton.plan_cost.plan.can_send_message: #or Access json feature list of a subscription plan
    send_message()

#deactivate subscription. User is removed from Group on subscription

subscription.deactivate()

#You can also record transaction or swap out BaseSubscriptionTransaction in base_models.py to implement or link with a payment model

class SubscriptionTransaction(BaseSubscriptionTransaction):
     cryptocurrency_payments = GenericRelation(CryptoCurrencyPayment)
     paypal_id = models.OneToOneField(PayPalModel)
     stripe_id = models.OneToOneField(StripeModel)

subscription.record_transaction()

#Lastly override notifications in notification.py to send emails to user regarding their payment and subscription

Testing

Install testing requirements.

$ pip install -r requirements.txt

Run with runtests.

$ ./runtests.py

You can also use the excellent tox testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:

$ tox

Documentation

To build the documentation, you’ll need to install mkdocs.

$ pip install mkdocs

To preview the documentation:

$ mkdocs serve
Running at: http://127.0.0.1:8000/

To build the documentation:

$ mkdocs build

About

API viewset for Django Flexible Subscriptions and few helper methods

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages