mirror of
https://github.com/lemeow125/DRF_Template.git
synced 2025-04-28 10:41:15 +08:00
Overhauled entire project config, added notifications, email templates, optimized stripe subscriptions, redis caching, and webdriver utilities
This commit is contained in:
parent
7cbe8fd720
commit
99dfcef67b
84 changed files with 4300 additions and 867 deletions
0
backend/billing/__init__.py
Normal file
0
backend/billing/__init__.py
Normal file
6
backend/billing/apps.py
Normal file
6
backend/billing/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class BillingConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "billing"
|
0
backend/billing/migrations/__init__.py
Normal file
0
backend/billing/migrations/__init__.py
Normal file
7
backend/billing/urls.py
Normal file
7
backend/billing/urls.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from django.urls import path
|
||||
from billing import views
|
||||
|
||||
urlpatterns = [
|
||||
path('',
|
||||
views.BillingHistoryView.as_view()),
|
||||
]
|
61
backend/billing/views.py
Normal file
61
backend/billing/views.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
from rest_framework import status
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from config.settings import STRIPE_SECRET_KEY
|
||||
from django.core.cache import cache
|
||||
from datetime import datetime
|
||||
import stripe
|
||||
|
||||
|
||||
# Make sure to set your secret key
|
||||
stripe.api_key = STRIPE_SECRET_KEY
|
||||
|
||||
|
||||
class BillingHistoryView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
requesting_user = self.request.user
|
||||
|
||||
if requesting_user.user_group:
|
||||
email = requesting_user.user_group.owner.email
|
||||
else:
|
||||
email = requesting_user.email
|
||||
|
||||
# Check cache
|
||||
key = f'billing_user:{requesting_user.id}'
|
||||
billing_history = cache.get(key)
|
||||
|
||||
if not billing_history:
|
||||
# List customers and filter by email
|
||||
customers = stripe.Customer.list(limit=1, email=email)
|
||||
|
||||
if customers:
|
||||
customer = customers.data[0]
|
||||
|
||||
# List customers and filter by email
|
||||
customers = stripe.Customer.list(limit=1, email=email)
|
||||
|
||||
if len(customers.data) > 0:
|
||||
# Retrieve the customer's charges (billing history)
|
||||
charges = stripe.Charge.list(
|
||||
limit=10, customer=customer.id)
|
||||
|
||||
# Prepare the response
|
||||
billing_history = [
|
||||
{
|
||||
'email': charge['billing_details']['email'],
|
||||
'amount_charged': int(charge['amount']/100),
|
||||
'paid': charge['paid'],
|
||||
'refunded': int(charge['amount_refunded']/100) > 0,
|
||||
'amount_refunded': int(charge['amount_refunded']/100),
|
||||
'last_4': charge['payment_method_details']['card']['last4'],
|
||||
'receipt_link': charge['receipt_url'],
|
||||
'timestamp': datetime.fromtimestamp(charge['created']).strftime("%m-%d-%Y %I:%M %p"),
|
||||
} for charge in charges.auto_paging_iter()
|
||||
]
|
||||
|
||||
cache.set(key, billing_history, 60*60)
|
||||
|
||||
return Response(billing_history, status=status.HTTP_200_OK)
|
Loading…
Add table
Add a link
Reference in a new issue