From 6c15d2086e00455447730a07e6d749b9da704a04 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Fri, 1 Jan 2021 12:41:54 +0100 Subject: [PATCH] implement balance getting --- uncloud/urls.py | 1 + uncloud_pay/models.py | 10 ------- uncloud_pay/selectors.py | 23 ++++++++++++++ uncloud_pay/serializers.py | 23 +++----------- .../templates/uncloud_pay/list_stripe.html | 30 ------------------- .../uncloud_pay/register_stripe.html | 18 +++++++---- uncloud_pay/views.py | 13 ++++++++ 7 files changed, 53 insertions(+), 65 deletions(-) create mode 100644 uncloud_pay/selectors.py delete mode 100644 uncloud_pay/templates/uncloud_pay/list_stripe.html diff --git a/uncloud/urls.py b/uncloud/urls.py index 3ee5988..15623bd 100644 --- a/uncloud/urls.py +++ b/uncloud/urls.py @@ -75,6 +75,7 @@ router.register(r'v2/net/wireguardvpnsizes', netviews.WireGuardVPNSizes, basenam # Payment related router.register(r'v2/payment/credit-card', payviews.CreditCardViewSet, basename='credit-card') router.register(r'v2/payment/payment', payviews.PaymentViewSet, basename='payment') +router.register(r'v2/payment/balance', payviews.BalanceViewSet, basename='payment-balance') urlpatterns = [ diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 0c880c3..4eb6698 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -68,16 +68,6 @@ class Currency(models.TextChoices): # USD = 'USD', _('US Dollar') -def get_balance_for_user(user): - bills = reduce( - lambda acc, entry: acc + entry.total, - Bill.objects.filter(owner=user), - 0) - payments = reduce( - lambda acc, entry: acc + entry.amount, - Payment.objects.filter(owner=user), - 0) - return payments - bills ### # Stripe diff --git a/uncloud_pay/selectors.py b/uncloud_pay/selectors.py new file mode 100644 index 0000000..ba53c74 --- /dev/null +++ b/uncloud_pay/selectors.py @@ -0,0 +1,23 @@ +from django.utils import timezone +from django.db import transaction + +from .models import * + +def get_payments_for_user(user): + payments = [ payment.amount for payment in Payment.objects.filter(owner=user) ] + + return sum(payments) + +def get_spendings_for_user(user): + orders = Order.objects.filter(owner=user) + + amount = 0 + for order in orders: + amount += order.one_time_price + amount += order.recurring_price * order.count_used(when=timezone.now()) + + return amount + +@transaction.atomic +def get_balance_for_user(user): + return get_payments_for_user(user) - get_spendings_for_user(user) diff --git a/uncloud_pay/serializers.py b/uncloud_pay/serializers.py index 14cac0b..361ff1c 100644 --- a/uncloud_pay/serializers.py +++ b/uncloud_pay/serializers.py @@ -5,6 +5,7 @@ from django.utils.translation import gettext_lazy as _ from .models import * import uncloud_pay.stripe as uncloud_stripe +from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS ### # 2020-12 Checked code @@ -32,29 +33,13 @@ class PaymentSerializer(serializers.ModelSerializer): return data +class BalanceSerializer(serializers.Serializer): + balance = serializers.DecimalField(max_digits=AMOUNT_MAX_DIGITS, decimal_places=AMOUNT_DECIMALS) + ################################################################################ # Unchecked code -### -# Payments and Payment Methods. - - -class UpdatePaymentMethodSerializer(serializers.ModelSerializer): - class Meta: - model = PaymentMethod - fields = ['description', 'primary'] - -class ChargePaymentMethodSerializer(serializers.Serializer): - amount = serializers.DecimalField(max_digits=10, decimal_places=2) - -class CreatePaymentMethodSerializer(serializers.ModelSerializer): - please_visit = serializers.CharField(read_only=True) - - class Meta: - model = PaymentMethod - fields = ['source', 'description', 'primary', 'please_visit'] - ### # Orders & Products. diff --git a/uncloud_pay/templates/uncloud_pay/list_stripe.html b/uncloud_pay/templates/uncloud_pay/list_stripe.html deleted file mode 100644 index b5cba17..0000000 --- a/uncloud_pay/templates/uncloud_pay/list_stripe.html +++ /dev/null @@ -1,30 +0,0 @@ -{% extends 'uncloud/base.html' %} - -{% block header %} - -{% endblock %} - -{% block body %} -
-

Your credit cards registered with Stripe

- - - - -

List of stripe credit cards: -

-

-
- -{% endblock %} diff --git a/uncloud_pay/templates/uncloud_pay/register_stripe.html b/uncloud_pay/templates/uncloud_pay/register_stripe.html index 82aca74..eaf1da4 100644 --- a/uncloud_pay/templates/uncloud_pay/register_stripe.html +++ b/uncloud_pay/templates/uncloud_pay/register_stripe.html @@ -21,17 +21,17 @@ terms of my agreement with you.

- -
- - +
The card will be registered with stripe.
+ + {% endblock %} diff --git a/uncloud_pay/views.py b/uncloud_pay/views.py index 48c24a8..073f7c9 100644 --- a/uncloud_pay/views.py +++ b/uncloud_pay/views.py @@ -24,12 +24,15 @@ import logging from .models import * from .serializers import * +from .selectors import * + from datetime import datetime from vat_validator import sanitize_vat import uncloud_pay.stripe as uncloud_stripe logger = logging.getLogger(__name__) + ### # 2020-12 checked code @@ -75,6 +78,16 @@ class PaymentViewSet(viewsets.ModelViewSet): def get_queryset(self): return Payment.objects.filter(owner=self.request.user) +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) + ### # Payments and Payment Methods.