From 4bed53c8a87c4220dc34d2d2ac2bb5b5ad225bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Fri, 28 Feb 2020 09:10:36 +0100 Subject: [PATCH] Wire charge-negative-balance to payment methods --- uncloud/uncloud/urls.py | 1 + uncloud/uncloud_pay/helpers.py | 18 +++++++++++++----- .../commands/charge-negative-balance.py | 13 +++++++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index d1a1cb8..8244e0e 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -30,6 +30,7 @@ router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct') # Pay router.register(r'user', payviews.UserViewSet, basename='user') +router.register(r'payment-method', payviews.PaymentMethodViewSet, basename='payment-method') router.register(r'bill', payviews.BillViewSet, basename='bill') router.register(r'order', payviews.OrderViewSet, basename='order') router.register(r'payment', payviews.PaymentViewSet, basename='payment') diff --git a/uncloud/uncloud_pay/helpers.py b/uncloud/uncloud_pay/helpers.py index 9dc39cd..2f68e9e 100644 --- a/uncloud/uncloud_pay/helpers.py +++ b/uncloud/uncloud_pay/helpers.py @@ -1,12 +1,20 @@ from functools import reduce -from .models import Bill, Payment +from .models import Bill, Payment, PaymentMethod def sum_amounts(entries): - return reduce(lambda acc, entry: acc + entry.amount, entries, 0) + return reduce(lambda acc, entry: acc + entry.amount, entries, 0) def get_balance_for(user): - bills = sum_amounts(Bill.objects.filter(owner=user)) - payments = sum_amounts(Payment.objects.filter(owner=user)) - return payments - bills + bills = sum_amounts(Bill.objects.filter(owner=user)) + payments = sum_amounts(Payment.objects.filter(owner=user)) + return payments - bills +def get_payment_method_for(user): + methods = PaymentMethod.objects.filter(owner=user) + for method in methods: + # Do we want to do something with non-primary method? + if method.primary: + return method + + return None diff --git a/uncloud/uncloud_pay/management/commands/charge-negative-balance.py b/uncloud/uncloud_pay/management/commands/charge-negative-balance.py index ae4c8dc..3667a03 100644 --- a/uncloud/uncloud_pay/management/commands/charge-negative-balance.py +++ b/uncloud/uncloud_pay/management/commands/charge-negative-balance.py @@ -1,7 +1,7 @@ from django.core.management.base import BaseCommand from uncloud_auth.models import User from uncloud_pay.models import Order, Bill -from uncloud_pay.helpers import get_balance_for +from uncloud_pay.helpers import get_balance_for, get_payment_method_for from datetime import timedelta from django.utils import timezone @@ -19,5 +19,14 @@ class Command(BaseCommand): balance = get_balance_for(user) if balance < 0: print("User {} has negative balance ({}), charging.".format(user.username, balance)) - # TODO: charge + payment_method = get_payment_method_for(user) + if payment_method != None: + amount_to_be_charged = abs(balance) + charge_ok = payment_method.charge(amount_to_be_charged) + if not charge_ok: + print("ERR: charging {} with method {} failed" + .format(user.username, payment_method.uuid) + ) + else: + print("ERR: no payment method registered for {}".format(user.username)) print("=> Done.")