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-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 14:50:46 +00:00
|
|
|
import json
|
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
from .models import *
|
|
|
|
from .serializers import *
|
2020-02-27 11:10:26 +00:00
|
|
|
from datetime import datetime
|
2020-03-04 10:05:21 +00:00
|
|
|
import uncloud_pay.stripe as uncloud_stripe
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-03-05 09:27:33 +00:00
|
|
|
###
|
|
|
|
# Payments and Payment Methods.
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 11:21:25 +00:00
|
|
|
class PaymentViewSet(viewsets.ReadOnlyModelViewSet):
|
2020-02-27 10:59:28 +00:00
|
|
|
serializer_class = PaymentSerializer
|
2020-02-27 10:21:38 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-02-27 11:10:26 +00:00
|
|
|
return Payment.objects.filter(owner=self.request.user)
|
2020-02-27 10:59:28 +00:00
|
|
|
|
2020-02-27 19:37:19 +00:00
|
|
|
class OrderViewSet(viewsets.ReadOnlyModelViewSet):
|
2020-02-27 11:42:24 +00:00
|
|
|
serializer_class = OrderSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Order.objects.filter(owner=self.request.user)
|
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
class PaymentMethodViewSet(viewsets.ModelViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
2020-03-02 21:26:40 +00:00
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.action == 'create':
|
|
|
|
return CreatePaymentMethodSerializer
|
2020-03-05 10:28:43 +00:00
|
|
|
elif self.action == 'update':
|
|
|
|
return UpdatePaymentMethodSerializer
|
2020-03-03 17:16:25 +00:00
|
|
|
elif self.action == 'charge':
|
|
|
|
return ChargePaymentMethodSerializer
|
2020-03-02 21:26:40 +00:00
|
|
|
else:
|
|
|
|
return PaymentMethodSerializer
|
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return PaymentMethod.objects.filter(owner=self.request.user)
|
|
|
|
|
2020-03-03 15:55:56 +00:00
|
|
|
# XXX: Handling of errors is far from great down there.
|
|
|
|
@transaction.atomic
|
2020-02-27 14:15:12 +00:00
|
|
|
def create(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
2020-03-05 14:19:25 +00:00
|
|
|
# Set newly created method as primary if no other method is.
|
|
|
|
if PaymentMethod.get_primary_for(request.user) == None:
|
|
|
|
serializer.validated_data['primary'] = True
|
|
|
|
|
2020-03-05 09:23:34 +00:00
|
|
|
if serializer.validated_data['source'] == "stripe":
|
|
|
|
# Retrieve Stripe customer ID for user.
|
2020-03-05 10:03:47 +00:00
|
|
|
customer_id = uncloud_stripe.get_customer_id_for(request.user)
|
2020-03-05 09:23:34 +00:00
|
|
|
if customer_id == None:
|
|
|
|
return Response(
|
2020-03-03 15:55:56 +00:00
|
|
|
{'error': 'Could not resolve customer stripe ID.'},
|
|
|
|
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
|
2020-03-05 10:03:47 +00:00
|
|
|
try:
|
|
|
|
setup_intent = uncloud_stripe.create_setup_intent(customer_id)
|
|
|
|
except Exception as e:
|
|
|
|
return Response({'error': str(e)},
|
|
|
|
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
|
2020-03-05 09:23:34 +00:00
|
|
|
payment_method = PaymentMethod.objects.create(
|
|
|
|
owner=request.user,
|
2020-03-05 10:03:47 +00:00
|
|
|
stripe_setup_intent_id=setup_intent.id,
|
2020-03-05 09:23:34 +00:00
|
|
|
**serializer.validated_data)
|
2020-03-03 15:55:56 +00:00
|
|
|
|
2020-03-05 09:23:34 +00:00
|
|
|
# TODO: find a way to use reverse properly:
|
|
|
|
# https://www.django-rest-framework.org/api-guide/reverse/
|
2020-03-05 10:03:47 +00:00
|
|
|
path = "payment-method/{}/register-stripe-cc".format(
|
|
|
|
payment_method.uuid)
|
|
|
|
stripe_registration_url = reverse('api-root', request=request) + path
|
2020-03-05 09:23:34 +00:00
|
|
|
return Response({'please_visit': stripe_registration_url})
|
2020-03-05 10:45:37 +00:00
|
|
|
else:
|
|
|
|
serializer.save(owner=request.user, **serializer.validated_data)
|
|
|
|
return Response(serializer.data)
|
2020-02-27 14:15:12 +00:00
|
|
|
|
2020-02-28 10:10:31 +00:00
|
|
|
@action(detail=True, methods=['post'])
|
|
|
|
def charge(self, request, pk=None):
|
|
|
|
payment_method = self.get_object()
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-03-03 17:16:25 +00:00
|
|
|
amount = serializer.validated_data['amount']
|
|
|
|
try:
|
|
|
|
payment = payment_method.charge(amount)
|
|
|
|
output_serializer = PaymentSerializer(payment)
|
|
|
|
return Response(output_serializer.data)
|
|
|
|
except Exception as e:
|
|
|
|
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
2020-02-28 10:10:31 +00:00
|
|
|
|
2020-03-05 09:23:34 +00:00
|
|
|
@action(detail=True, methods=['get'], url_path='register-stripe-cc', renderer_classes=[TemplateHTMLRenderer])
|
|
|
|
def register_stripe_cc(self, request, pk=None):
|
|
|
|
payment_method = self.get_object()
|
2020-03-05 14:19:25 +00:00
|
|
|
|
2020-03-05 10:03:47 +00:00
|
|
|
if payment_method.source != 'stripe':
|
|
|
|
return Response(
|
|
|
|
{'error': 'This is not a Stripe-based payment method.'},
|
|
|
|
template_name='error.html.j2')
|
|
|
|
|
|
|
|
if payment_method.active:
|
|
|
|
return Response(
|
|
|
|
{'error': 'This payment method is already active'},
|
|
|
|
template_name='error.html.j2')
|
|
|
|
|
|
|
|
try:
|
|
|
|
setup_intent = uncloud_stripe.get_setup_intent(
|
|
|
|
payment_method.stripe_setup_intent_id)
|
|
|
|
except Exception as e:
|
|
|
|
return Response(
|
|
|
|
{'error': str(e)},
|
|
|
|
template_name='error.html.j2')
|
|
|
|
|
|
|
|
# TODO: find a way to use reverse properly:
|
|
|
|
# https://www.django-rest-framework.org/api-guide/reverse/
|
|
|
|
callback_path= "payment-method/{}/activate-stripe-cc/".format(
|
|
|
|
payment_method.uuid)
|
|
|
|
callback = reverse('api-root', request=request) + callback_path
|
2020-03-05 09:23:34 +00:00
|
|
|
|
|
|
|
# Render stripe card registration form.
|
|
|
|
template_args = {
|
2020-03-05 10:03:47 +00:00
|
|
|
'client_secret': setup_intent.client_secret,
|
|
|
|
'stripe_pk': uncloud_stripe.public_api_key,
|
|
|
|
'callback': callback
|
2020-03-05 09:23:34 +00:00
|
|
|
}
|
|
|
|
return Response(template_args, template_name='stripe-payment.html.j2')
|
|
|
|
|
2020-03-05 10:03:47 +00:00
|
|
|
@action(detail=True, methods=['post'], url_path='activate-stripe-cc')
|
|
|
|
def activate_stripe_cc(self, request, pk=None):
|
2020-03-05 09:23:34 +00:00
|
|
|
payment_method = self.get_object()
|
2020-03-05 10:03:47 +00:00
|
|
|
try:
|
|
|
|
setup_intent = uncloud_stripe.get_setup_intent(
|
|
|
|
payment_method.stripe_setup_intent_id)
|
|
|
|
except Exception as e:
|
|
|
|
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
2020-03-05 09:23:34 +00:00
|
|
|
|
|
|
|
# Card had been registered, fetching payment method.
|
2020-03-05 10:03:47 +00:00
|
|
|
print(setup_intent)
|
|
|
|
if setup_intent.payment_method:
|
|
|
|
payment_method.stripe_payment_method_id = setup_intent.payment_method
|
2020-03-05 09:23:34 +00:00
|
|
|
payment_method.save()
|
|
|
|
|
|
|
|
return Response({
|
|
|
|
'uuid': payment_method.uuid,
|
|
|
|
'activated': payment_method.active})
|
|
|
|
else:
|
|
|
|
error = 'Could not fetch payment method from stripe. Please try again.'
|
|
|
|
return Response({'error': error})
|
|
|
|
|
2020-03-05 14:19:25 +00:00
|
|
|
@action(detail=True, methods=['post'], url_path='set-as-primary')
|
|
|
|
def set_as_primary(self, request, pk=None):
|
|
|
|
payment_method = self.get_object()
|
|
|
|
payment_method.set_as_primary_for(request.user)
|
|
|
|
|
|
|
|
serializer = self.get_serializer(payment_method)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def unpaid(self, request):
|
|
|
|
return Bill.objects.filter(owner=self.request.user, paid=False)
|
|
|
|
|
|
|
|
|
|
|
|
class OrderViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
serializer_class = OrderSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Order.objects.filter(owner=self.request.user)
|
|
|
|
|
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):
|
|
|
|
return self.request.user.billingaddress_set.all()
|
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save(owner=request.user)
|
2020-03-05 09:23:34 +00:00
|
|
|
|
2020-02-27 11:21:25 +00:00
|
|
|
###
|
2020-03-05 09:27:33 +00:00
|
|
|
# Old admin stuff.
|
2020-02-27 11:21:25 +00:00
|
|
|
|
|
|
|
class AdminPaymentViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = PaymentSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
class AdminBillViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = BillSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-02-27 11:42:24 +00:00
|
|
|
return Bill.objects.all()
|
2020-02-27 11:21:25 +00:00
|
|
|
|
|
|
|
def unpaid(self, request):
|
|
|
|
return Bill.objects.filter(owner=self.request.user, paid=False)
|
|
|
|
|
2020-02-27 10:59:28 +00:00
|
|
|
def create(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-02-27 14:50:46 +00:00
|
|
|
serializer.save(creation_date=datetime.now())
|
2020-02-27 10:59:28 +00:00
|
|
|
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
2020-02-27 11:42:24 +00:00
|
|
|
|
|
|
|
class AdminOrderViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = OrderSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Order.objects.all()
|
2020-04-03 16:41:17 +00:00
|
|
|
|
|
|
|
# PDF tests
|
|
|
|
from django.views.generic import TemplateView
|
|
|
|
from hardcopy.views import PDFViewMixin, PNGViewMixin
|
|
|
|
|
|
|
|
class MyPDFView(PDFViewMixin, TemplateView):
|
|
|
|
template_name = "bill.html"
|
|
|
|
# def get_filename(self):
|
|
|
|
# return "my_file_{}.pdf".format(now().strftime('Y-m-d'))
|