2020-12-25 09:31:42 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2021-07-30 07:04:32 +00:00
|
|
|
from django.views.generic.base import TemplateView, View
|
2020-02-27 10:21:38 +00:00
|
|
|
from django.shortcuts import render
|
2020-03-03 15:55:56 +00:00
|
|
|
from django.db import transaction
|
2020-02-27 11:38:04 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-04-15 13:17:38 +00:00
|
|
|
from rest_framework import viewsets, mixins, permissions, status, views
|
2020-03-05 09:23:34 +00:00
|
|
|
from rest_framework.renderers import TemplateHTMLRenderer
|
2020-02-27 11:10:26 +00:00
|
|
|
from rest_framework.response import Response
|
2020-02-27 11:38:04 +00:00
|
|
|
from rest_framework.decorators import action
|
2020-03-05 09:23:34 +00:00
|
|
|
from rest_framework.reverse import reverse
|
|
|
|
from rest_framework.decorators import renderer_classes
|
2020-05-07 10:03:28 +00:00
|
|
|
from vat_validator import validate_vat, vies
|
|
|
|
from vat_validator.countries import EU_COUNTRY_CODES
|
2020-05-07 13:38:49 +00:00
|
|
|
from hardcopy import bytestring_to_pdf
|
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
|
|
|
from django.http import FileResponse
|
|
|
|
from django.template.loader import render_to_string
|
2020-05-08 08:07:44 +00:00
|
|
|
from copy import deepcopy
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 14:50:46 +00:00
|
|
|
import json
|
2020-04-18 09:51:13 +00:00
|
|
|
import logging
|
2020-02-27 14:50:46 +00:00
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
from .models import *
|
|
|
|
from .serializers import *
|
2021-01-01 11:41:54 +00:00
|
|
|
from .selectors import *
|
2021-07-30 07:04:32 +00:00
|
|
|
from .utils import get_order_total_with_vat
|
2021-01-01 11:41:54 +00:00
|
|
|
|
2020-02-27 11:10:26 +00:00
|
|
|
from datetime import datetime
|
2020-05-07 10:03:28 +00:00
|
|
|
from vat_validator import sanitize_vat
|
2020-03-04 10:05:21 +00:00
|
|
|
import uncloud_pay.stripe as uncloud_stripe
|
2021-07-19 14:36:10 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.http import JsonResponse
|
|
|
|
import stripe
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-04-18 09:51:13 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-07-30 07:04:32 +00:00
|
|
|
|
|
|
|
class PricingView(View):
|
|
|
|
def get(self, request, **args):
|
2021-08-12 10:28:19 +00:00
|
|
|
address = get_billing_address_for_user(self.request.user)
|
|
|
|
vat_rate = False
|
|
|
|
vat_validation_status = False
|
|
|
|
if address:
|
|
|
|
vat_rate = VATRate.get_vat_rate(address)
|
|
|
|
vat_validation_status = "verified" if address.vat_number_validated_on and address.vat_number_verified else False
|
2021-07-30 07:04:32 +00:00
|
|
|
pricing = get_order_total_with_vat(
|
|
|
|
request.GET.get('cores'),
|
|
|
|
request.GET.get('memory'),
|
|
|
|
request.GET.get('storage'),
|
2021-08-12 10:28:19 +00:00
|
|
|
pricing_name = args['name'],
|
|
|
|
vat_rate = vat_rate * 100,
|
|
|
|
vat_validation_status = vat_validation_status
|
2021-07-30 07:04:32 +00:00
|
|
|
)
|
|
|
|
return JsonResponse(pricing)
|
2020-12-25 09:31:42 +00:00
|
|
|
|
2021-08-12 10:28:19 +00:00
|
|
|
class CardActivateView(View):
|
|
|
|
def post(self, request, **args):
|
|
|
|
card_id = request.POST.get('card_id')
|
|
|
|
if card_id:
|
|
|
|
matched_card = StripeCreditCard.objects.filter(owner=self.request.user, card_id=card_id).first()
|
|
|
|
matched_card.activate()
|
|
|
|
return JsonResponse({'success': 1})
|
|
|
|
else:
|
|
|
|
return JsonResponse({'error': "Please select a card"})
|
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
class RegisterCard(TemplateView):
|
2020-12-25 16:33:01 +00:00
|
|
|
template_name = "uncloud_pay/register_stripe.html"
|
2020-12-25 09:31:42 +00:00
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super().dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2020-12-25 09:31:42 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
customer_id = uncloud_stripe.get_customer_id_for(self.request.user)
|
|
|
|
setup_intent = uncloud_stripe.create_setup_intent(customer_id)
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['client_secret'] = setup_intent.client_secret
|
2021-07-19 14:36:10 +00:00
|
|
|
context['username'] = self.request.user.username
|
2020-12-25 09:31:42 +00:00
|
|
|
context['stripe_pk'] = uncloud_stripe.public_api_key
|
|
|
|
return context
|
|
|
|
|
2020-12-26 10:22:51 +00:00
|
|
|
|
2020-12-28 22:35:34 +00:00
|
|
|
class CreditCardViewSet(mixins.RetrieveModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
viewsets.GenericViewSet):
|
2020-12-26 10:22:51 +00:00
|
|
|
|
2020-12-28 22:35:34 +00:00
|
|
|
serializer_class = StripeCreditCardSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2020-12-26 10:22:51 +00:00
|
|
|
|
2020-12-28 22:35:34 +00:00
|
|
|
def list(self, request):
|
|
|
|
uncloud_stripe.sync_cards_for_user(self.request.user)
|
|
|
|
return super().list(request)
|
2020-12-26 10:22:51 +00:00
|
|
|
|
2020-12-28 22:35:34 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return StripeCreditCard.objects.filter(owner=self.request.user)
|
|
|
|
|
2020-12-29 00:43:33 +00:00
|
|
|
class PaymentViewSet(viewsets.ModelViewSet):
|
2020-12-28 22:35:34 +00:00
|
|
|
serializer_class = PaymentSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Payment.objects.filter(owner=self.request.user)
|
2020-12-26 10:22:51 +00:00
|
|
|
|
2021-01-01 11:41:54 +00:00
|
|
|
class BalanceViewSet(viewsets.ViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def list(self, request):
|
|
|
|
serializer = BalanceSerializer(data={
|
|
|
|
'balance': get_balance_for_user(self.request.user)
|
|
|
|
})
|
|
|
|
serializer.is_valid()
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2020-12-29 00:43:33 +00:00
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
class ListCards(TemplateView):
|
2020-12-28 22:35:34 +00:00
|
|
|
template_name = "uncloud_pay/list_stripe.html"
|
2020-12-26 10:22:51 +00:00
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super().dispatch(*args, **kwargs)
|
|
|
|
|
2020-12-26 10:22:51 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
customer_id = uncloud_stripe.get_customer_id_for(self.request.user)
|
|
|
|
cards = uncloud_stripe.get_customer_cards(customer_id)
|
|
|
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['cards'] = cards
|
|
|
|
context['username'] = self.request.user
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
2020-03-05 09:27:33 +00:00
|
|
|
###
|
|
|
|
# Bills and Orders.
|
|
|
|
|
|
|
|
class BillViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
serializer_class = BillSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Bill.objects.filter(owner=self.request.user)
|
|
|
|
|
2020-05-07 11:12:38 +00:00
|
|
|
|
|
|
|
@action(detail=False, methods=['get'])
|
2020-03-05 09:27:33 +00:00
|
|
|
def unpaid(self, request):
|
2020-05-07 11:12:38 +00:00
|
|
|
serializer = self.get_serializer(
|
|
|
|
Bill.get_unpaid_for(self.request.user),
|
|
|
|
many=True)
|
|
|
|
return Response(serializer.data)
|
2020-03-05 09:27:33 +00:00
|
|
|
|
2020-05-07 13:38:49 +00:00
|
|
|
@action(detail=True, methods=['get'])
|
|
|
|
def download(self, *args, **kwargs):
|
2020-08-01 12:05:56 +00:00
|
|
|
"""
|
|
|
|
Allow to download
|
|
|
|
"""
|
2020-05-07 13:38:49 +00:00
|
|
|
bill = self.get_object()
|
2020-10-25 12:52:36 +00:00
|
|
|
provider = UncloudProvider.get_provider()
|
2020-05-07 13:38:49 +00:00
|
|
|
output_file = NamedTemporaryFile()
|
|
|
|
bill_html = render_to_string("bill.html.j2", {'bill': bill})
|
|
|
|
|
|
|
|
bytestring_to_pdf(bill_html.encode('utf-8'), output_file)
|
|
|
|
response = FileResponse(output_file, content_type="application/pdf")
|
|
|
|
response['Content-Disposition'] = 'filename="{}_{}.pdf"'.format(
|
|
|
|
bill.reference, bill.uuid
|
|
|
|
)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2020-03-05 09:27:33 +00:00
|
|
|
|
|
|
|
class OrderViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
serializer_class = OrderSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Order.objects.filter(owner=self.request.user)
|
|
|
|
|
2021-06-20 09:51:27 +00:00
|
|
|
class VATRateViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
serializer_class = VATRateSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
queryset = VATRate.objects.all()
|
|
|
|
|
2020-04-15 13:17:38 +00:00
|
|
|
class BillingAddressViewSet(mixins.CreateModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
viewsets.GenericViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.action == 'update':
|
|
|
|
return UpdateBillingAddressSerializer
|
|
|
|
else:
|
|
|
|
return BillingAddressSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2021-07-19 14:36:10 +00:00
|
|
|
return self.request.user.billing_addresses.all()
|
2020-04-15 13:17:38 +00:00
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-04-15 14:01:31 +00:00
|
|
|
|
|
|
|
# Validate VAT numbers.
|
|
|
|
country = serializer.validated_data["country"]
|
|
|
|
|
|
|
|
# We ignore empty VAT numbers.
|
2020-05-02 18:42:09 +00:00
|
|
|
if 'vat_number' in serializer.validated_data and serializer.validated_data["vat_number"] != "":
|
|
|
|
vat_number = serializer.validated_data["vat_number"]
|
|
|
|
|
2020-04-15 14:01:31 +00:00
|
|
|
if not validate_vat(country, vat_number):
|
|
|
|
return Response(
|
|
|
|
{'error': 'Malformed VAT number.'},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
elif country in EU_COUNTRY_CODES:
|
2020-04-18 09:51:13 +00:00
|
|
|
# XXX: make a synchroneous call to a third patry API here might not be a good idea..
|
|
|
|
try:
|
|
|
|
vies_state = vies.check_vat(country, vat_number)
|
|
|
|
if not vies_state.valid:
|
|
|
|
return Response(
|
|
|
|
{'error': 'European VAT number does not exist in VIES.'},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning(e)
|
2020-04-15 14:01:31 +00:00
|
|
|
return Response(
|
2020-04-18 09:51:13 +00:00
|
|
|
{'error': 'Could not validate EU VAT number against VIES. Try again later..'},
|
|
|
|
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
|
2020-04-15 14:01:31 +00:00
|
|
|
|
2020-04-15 13:17:38 +00:00
|
|
|
serializer.save(owner=request.user)
|
2020-04-15 14:01:31 +00:00
|
|
|
return Response(serializer.data)
|
2020-03-05 09:23:34 +00:00
|
|
|
|
2020-02-27 11:21:25 +00:00
|
|
|
###
|
2020-05-07 10:45:06 +00:00
|
|
|
# Admin stuff.
|
2020-02-27 11:21:25 +00:00
|
|
|
|
|
|
|
class AdminPaymentViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = PaymentSerializer
|
2020-05-07 11:12:38 +00:00
|
|
|
permission_classes = [permissions.IsAdminUser]
|
2020-02-27 11:21:25 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-02-27 11:42:24 +00:00
|
|
|
return Payment.objects.all()
|
2020-02-27 11:21:25 +00:00
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save(timestamp=datetime.now())
|
|
|
|
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
|
2020-05-07 11:12:38 +00:00
|
|
|
# Bills are generated from orders and should not be created or updated by hand.
|
2020-05-07 13:45:04 +00:00
|
|
|
class AdminBillViewSet(BillViewSet):
|
2020-02-27 11:21:25 +00:00
|
|
|
serializer_class = BillSerializer
|
2020-05-07 11:12:38 +00:00
|
|
|
permission_classes = [permissions.IsAdminUser]
|
2020-02-27 11:21:25 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-02-27 11:42:24 +00:00
|
|
|
return Bill.objects.all()
|
2020-02-27 11:21:25 +00:00
|
|
|
|
2020-05-07 11:12:38 +00:00
|
|
|
@action(detail=False, methods=['get'])
|
2020-02-27 11:21:25 +00:00
|
|
|
def unpaid(self, request):
|
2020-05-07 11:12:38 +00:00
|
|
|
unpaid_bills = []
|
|
|
|
# XXX: works but we can do better than number of users + 1 SQL requests...
|
|
|
|
for user in get_user_model().objects.all():
|
|
|
|
unpaid_bills = unpaid_bills + Bill.get_unpaid_for(self.request.user)
|
2020-02-27 11:21:25 +00:00
|
|
|
|
2020-05-07 11:12:38 +00:00
|
|
|
serializer = self.get_serializer(unpaid_bills, many=True)
|
|
|
|
return Response(serializer.data)
|
2020-02-27 11:42:24 +00:00
|
|
|
|
2020-05-08 08:42:04 +00:00
|
|
|
@action(detail=False, methods=['post'])
|
|
|
|
def generate(self, request):
|
|
|
|
users = get_user_model().objects.all()
|
|
|
|
|
|
|
|
generated_bills = []
|
|
|
|
for user in users:
|
|
|
|
now = timezone.now()
|
|
|
|
generated_bills = generated_bills + Bill.generate_for(
|
|
|
|
year=now.year,
|
|
|
|
month=now.month,
|
|
|
|
user=user)
|
|
|
|
|
|
|
|
return Response(
|
|
|
|
map(lambda b: b.reference, generated_bills),
|
|
|
|
status=status.HTTP_200_OK)
|
|
|
|
|
2020-05-07 11:12:38 +00:00
|
|
|
class AdminOrderViewSet(mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.CreateModelMixin,
|
2020-05-08 08:07:44 +00:00
|
|
|
mixins.UpdateModelMixin,
|
2020-05-07 11:12:38 +00:00
|
|
|
viewsets.GenericViewSet):
|
2020-05-07 13:38:49 +00:00
|
|
|
serializer_class = OrderSerializer
|
2020-05-07 10:45:06 +00:00
|
|
|
permission_classes = [permissions.IsAdminUser]
|
2020-02-27 11:42:24 +00:00
|
|
|
|
2020-05-07 10:05:26 +00:00
|
|
|
def get_serializer(self, *args, **kwargs):
|
2020-05-07 13:38:49 +00:00
|
|
|
return self.serializer_class(*args, **kwargs, admin=True)
|
2020-05-07 10:05:26 +00:00
|
|
|
|
2020-02-27 11:42:24 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return Order.objects.all()
|
2020-05-08 07:31:46 +00:00
|
|
|
|
2020-05-08 08:07:44 +00:00
|
|
|
# Updates create a new order and terminate the 'old' one.
|
|
|
|
@transaction.atomic
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
order = self.get_object()
|
|
|
|
partial = kwargs.pop('partial', False)
|
|
|
|
serializer = self.get_serializer(order, data=request.data, partial=partial)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
# Clone existing order for replacement.
|
|
|
|
replacing_order = deepcopy(order)
|
|
|
|
|
|
|
|
# Yes, that's how you make a new entry in DB:
|
|
|
|
# https://docs.djangoproject.com/en/3.0/topics/db/queries/#copying-model-instances
|
|
|
|
replacing_order.pk = None
|
|
|
|
|
|
|
|
for attr, value in serializer.validated_data.items():
|
|
|
|
setattr(replacing_order, attr, value)
|
|
|
|
|
|
|
|
# Save replacing order and terminate 'previous' one.
|
|
|
|
replacing_order.save()
|
|
|
|
order.replaced_by = replacing_order
|
|
|
|
order.save()
|
|
|
|
order.terminate()
|
|
|
|
|
|
|
|
return Response(replacing_order)
|
|
|
|
|
2020-05-08 07:31:46 +00:00
|
|
|
@action(detail=True, methods=['post'])
|
|
|
|
def terminate(self, request, pk):
|
|
|
|
order = self.get_object()
|
|
|
|
if order.is_terminated:
|
|
|
|
return Response(
|
|
|
|
{'error': 'Order is already terminated.'},
|
|
|
|
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
|
|
|
|
else:
|
|
|
|
order.terminate()
|
|
|
|
return Response({}, status=status.HTTP_200_OK)
|