Your credit cards registered with Stripe
- - - - -List of stripe credit cards: -
-
- {% for card in cards %}
-
- {{ card.brand }} ending in {{ card.last4 }} expiring - {{ card.year }}-{{ card.month }} - {% endfor %} - -
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 %} -
List of stripe credit cards: -