From e51edab2f5fc8f107db0860c28337d9c668b1347 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 26 Dec 2020 11:22:51 +0100 Subject: [PATCH] cleanup/in between commit --- uncloud/settings.py | 4 + uncloud/tasks.py | 3 + uncloud/urls.py | 5 +- uncloud_pay/stripe.py | 26 ++++++- uncloud_pay/tasks.py | 11 +++ uncloud_pay/templates/error.html.j2 | 18 ----- uncloud_pay/templates/stripe-payment.html.j2 | 76 ------------------- .../templates/{ => uncloud_pay}/bill.html.j2 | 0 .../templates/uncloud_pay/list_stripe.html | 30 ++++++++ .../uncloud_pay/register_stripe.html | 3 + uncloud_pay/views.py | 30 ++++++++ 11 files changed, 107 insertions(+), 99 deletions(-) create mode 100644 uncloud_pay/tasks.py delete mode 100644 uncloud_pay/templates/error.html.j2 delete mode 100644 uncloud_pay/templates/stripe-payment.html.j2 rename uncloud_pay/templates/{ => uncloud_pay}/bill.html.j2 (100%) create mode 100644 uncloud_pay/templates/uncloud_pay/list_stripe.html diff --git a/uncloud/settings.py b/uncloud/settings.py index ae734dc..afc6d65 100644 --- a/uncloud/settings.py +++ b/uncloud/settings.py @@ -228,6 +228,10 @@ CELERY_BEAT_SCHEDULE = { 'cleanup_tasks': { 'task': 'uncloud.tasks.cleanup_tasks', 'schedule': 10 + }, + 'check_balance': { + 'task': 'uncloud_pay.tasks.check_balance', + 'schedule': 15 } } diff --git a/uncloud/tasks.py b/uncloud/tasks.py index 5a13ec5..a42f359 100644 --- a/uncloud/tasks.py +++ b/uncloud/tasks.py @@ -14,6 +14,9 @@ def cleanup_tasks(self): continue res = AsyncResult(id=str(task.task_id)) + print(f"Task {task}: {res.state}") if res.ready(): print(res.get()) task.delete() + + res.forget() diff --git a/uncloud/urls.py b/uncloud/urls.py index 9097b4c..343a83c 100644 --- a/uncloud/urls.py +++ b/uncloud/urls.py @@ -90,7 +90,10 @@ urlpatterns = [ path('login/', authviews.LoginView.as_view(), name="login"), path('logout/', authviews.LogoutView.as_view(), name="logout"), path('admin/', admin.site.urls), + path('cc/reg/', payviews.RegisterCard.as_view(), name="cc_register"), - path('cc/submit/', payviews.RegisterCard.as_view(), name="cc_register"), + path('cc/list/', payviews.ListCards.as_view(), name="cc_list"), + path('cc/delete/', payviews.DeleteCard.as_view(), name="cc_delete"), + path('', uncloudviews.UncloudIndex.as_view(), name="uncloudindex"), ] diff --git a/uncloud_pay/stripe.py b/uncloud_pay/stripe.py index f4c467a..a3dcb23 100644 --- a/uncloud_pay/stripe.py +++ b/uncloud_pay/stripe.py @@ -47,10 +47,6 @@ def handle_stripe_error(f): # XXX: maybe send email logging.error(str(e)) raise Exception(common_message) - except Exception as e: - # maybe send email - logging.error(str(e)) - raise Exception(common_message) return handle_problems @@ -103,3 +99,25 @@ def create_customer(name, email): @handle_stripe_error def get_customer(customer_id): return stripe.Customer.retrieve(customer_id) + +@handle_stripe_error +def get_customer_cards(customer_id): + print(f"getting cards for: {customer_id}") + + cards = [] + stripe_cards = stripe.PaymentMethod.list( + customer=customer_id, + type="card", + ) + + for stripe_card in stripe_cards["data"]: + card = {} + card['brand'] = stripe_card["card"]["brand"] + card['last4'] = stripe_card["card"]["last4"] + card['month'] = stripe_card["card"]["exp_month"] + card['year'] = stripe_card["card"]["exp_year"] + card['id'] = stripe_card["card"]["id"] + + cards.append(card) + + return cards diff --git a/uncloud_pay/tasks.py b/uncloud_pay/tasks.py new file mode 100644 index 0000000..b88f494 --- /dev/null +++ b/uncloud_pay/tasks.py @@ -0,0 +1,11 @@ +from celery import shared_task +from .models import * +import uuid + +from uncloud.models import UncloudTask + +@shared_task(bind=True) +def check_balance(self): + UncloudTask.objects.create(task_id=self.id) + print("for each user res is 50") + return 50 diff --git a/uncloud_pay/templates/error.html.j2 b/uncloud_pay/templates/error.html.j2 deleted file mode 100644 index ba9209c..0000000 --- a/uncloud_pay/templates/error.html.j2 +++ /dev/null @@ -1,18 +0,0 @@ - - - - Error - - - -
-

Error

-

{{ error }}

-
- - diff --git a/uncloud_pay/templates/stripe-payment.html.j2 b/uncloud_pay/templates/stripe-payment.html.j2 deleted file mode 100644 index 6c59740..0000000 --- a/uncloud_pay/templates/stripe-payment.html.j2 +++ /dev/null @@ -1,76 +0,0 @@ - - - - Stripe Card Registration - - - - - - - - -
-

Registering Stripe Credit Card

- - - -
-
- -
- - -
-
- - - - - - - - diff --git a/uncloud_pay/templates/bill.html.j2 b/uncloud_pay/templates/uncloud_pay/bill.html.j2 similarity index 100% rename from uncloud_pay/templates/bill.html.j2 rename to uncloud_pay/templates/uncloud_pay/bill.html.j2 diff --git a/uncloud_pay/templates/uncloud_pay/list_stripe.html b/uncloud_pay/templates/uncloud_pay/list_stripe.html new file mode 100644 index 0000000..b5cba17 --- /dev/null +++ b/uncloud_pay/templates/uncloud_pay/list_stripe.html @@ -0,0 +1,30 @@ +{% extends 'uncloud/base.html' %} + +{% block header %} + +{% endblock %} + +{% block body %} +
+

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 %} + +
+

+
+ +{% endblock %} diff --git a/uncloud_pay/templates/uncloud_pay/register_stripe.html b/uncloud_pay/templates/uncloud_pay/register_stripe.html index 9fd82ae..82aca74 100644 --- a/uncloud_pay/templates/uncloud_pay/register_stripe.html +++ b/uncloud_pay/templates/uncloud_pay/register_stripe.html @@ -45,6 +45,9 @@ var clientSecret = '{{ client_secret }}'; cardButton.addEventListener('click', function(ev) { + document.getElementById("ungleichmessage").innerHTML + = "Registering card with Stripe, please wait ..." + stripe.confirmCardSetup( clientSecret, { diff --git a/uncloud_pay/views.py b/uncloud_pay/views.py index 99d176e..2f4ba8d 100644 --- a/uncloud_pay/views.py +++ b/uncloud_pay/views.py @@ -65,6 +65,36 @@ class RegisterCard(LoginRequiredMixin, TemplateView): context['stripe_pk'] = uncloud_stripe.public_api_key return context +class ListCards(LoginRequiredMixin, TemplateView): + login_url = '/login/' + + template_name = "uncloud_pay/list_stripe.html" + + 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 + +class DeleteCard(LoginRequiredMixin, TemplateView): + login_url = '/login/' + + template_name = "uncloud_pay/delete_stripe_card.html" + + 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 + class PaymentMethodViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated]