cleanup/in between commit

This commit is contained in:
Nico Schottelius 2020-12-26 11:22:51 +01:00
parent f7c68b5ca5
commit e51edab2f5
11 changed files with 107 additions and 99 deletions

View File

@ -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
}
}

View File

@ -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()

View File

@ -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/<payment_method_id>', payviews.DeleteCard.as_view(), name="cc_delete"),
path('', uncloudviews.UncloudIndex.as_view(), name="uncloudindex"),
]

View File

@ -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

11
uncloud_pay/tasks.py Normal file
View File

@ -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

View File

@ -1,18 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
#content {
width: 400px;
margin: auto;
}
</style>
</head>
<body>
<div id="content">
<h1>Error</h1>
<p>{{ error }}</p>
</div>
</body>
</html>

View File

@ -1,76 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Stripe Card Registration</title>
<!-- https://stripe.com/docs/js/appendix/viewport_meta_requirements -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://js.stripe.com/v3/"></script>
<style>
#content {
width: 400px;
margin: auto;
}
#callback-form {
display: none;
}
</style>
</head>
<body>
<div id="content">
<h1>Registering Stripe Credit Card</h1>
<!-- Stripe form and messages -->
<span id="message"></span>
<form id="setup-form">
<div id="card-element"></div>
<button type='button' id="card-button">
Save
</button>
</form>
<!-- Dirty hack used for callback to API -->
<form id="callback-form" action="{{ callback }}" method="post"></form>
</div>
<!-- Enable Stripe from UI elements -->
<script>
var stripe = Stripe('{{ stripe_pk }}');
var elements = stripe.elements();
var cardElement = elements.create('card');
cardElement.mount('#card-element');
</script>
<!-- Handle card submission -->
<script>
var cardButton = document.getElementById('card-button');
var messageContainer = document.getElementById('message');
var clientSecret = '{{ client_secret }}';
cardButton.addEventListener('click', function(ev) {
stripe.confirmCardSetup(
clientSecret,
{
payment_method: {
card: cardElement,
billing_details: {
},
},
}
).then(function(result) {
if (result.error) {
var message = document.createTextNode('Error:' + result.error.message);
messageContainer.appendChild(message);
} else {
// Return to API on success.
document.getElementById("callback-form").submit();
}
});
});
</script>
</body>
</html>

View File

@ -0,0 +1,30 @@
{% extends 'uncloud/base.html' %}
{% block header %}
<style>
#content {
width: 400px;
margin: auto;
}
</style>
{% endblock %}
{% block body %}
<div id="content">
<h1>Your credit cards registered with Stripe</h1>
<!-- Stripe form and messages -->
<span id="message"></span>
<p>List of stripe credit cards:
<ul>
{% for card in cards %}
<li>{{ card.brand }} ending in {{ card.last4 }} expiring
{{ card.year }}-{{ card.month }}
{% endfor %}
</ul>
</p>
</div>
{% endblock %}

View File

@ -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,
{

View File

@ -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]