in between commit to update for cc tests

This commit is contained in:
Nico Schottelius 2020-12-25 10:31:42 +01:00
parent 8dd4b712fb
commit df4c0c3060
6 changed files with 108 additions and 7 deletions

View file

@ -13,6 +13,7 @@ from rest_framework import routers
from rest_framework.schemas import get_schema_view from rest_framework.schemas import get_schema_view
#from opennebula import views as oneviews #from opennebula import views as oneviews
from uncloud import views as uncloudviews
from uncloud_auth import views as authviews from uncloud_auth import views as authviews
from uncloud_net import views as netviews from uncloud_net import views as netviews
from uncloud_pay import views as payviews from uncloud_pay import views as payviews
@ -87,6 +88,7 @@ urlpatterns = [
# path('web/vpn/create/', netviews.WireGuardVPNCreateView.as_view(), name="vpncreate"), # path('web/vpn/create/', netviews.WireGuardVPNCreateView.as_view(), name="vpncreate"),
path('login/', authviews.LoginView.as_view(), name="login"), path('login/', authviews.LoginView.as_view(), name="login"),
path('logout/', authviews.LogoutView.as_view(), name="logout"), path('logout/', authviews.LogoutView.as_view(), name="logout"),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('cc/reg/', payviews.RegisterCard.as_view(), name="cc_register"),
path('', uncloudviews.UncloudIndex.as_view(), name="uncloudindex"),
] ]

View file

@ -14,11 +14,6 @@ from .services import *
from .forms import * from .forms import *
from .tasks import * from .tasks import *
# class VPNPoolViewSet(viewsets.ModelViewSet):
# serializer_class = VPNPoolSerializer
# permission_classes = [permissions.IsAdminUser]
# queryset = VPNPool.objects.all()
class WireGuardVPNViewSet(viewsets.ModelViewSet): class WireGuardVPNViewSet(viewsets.ModelViewSet):
serializer_class = WireGuardVPNSerializer serializer_class = WireGuardVPNSerializer
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
@ -66,3 +61,10 @@ class WireGuardVPNSizes(viewsets.ViewSet):
print(sizes) print(sizes)
return Response(WireGuardVPNSizesSerializer(sizes, many=True).data) return Response(WireGuardVPNSizesSerializer(sizes, many=True).data)
# class VPNPoolViewSet(viewsets.ModelViewSet):
# serializer_class = VPNPoolSerializer
# permission_classes = [permissions.IsAdminUser]
# queryset = VPNPool.objects.all()

View file

@ -88,5 +88,5 @@ admin.site.register(Bill, BillAdmin)
admin.site.register(ProductToRecurringPeriod) admin.site.register(ProductToRecurringPeriod)
admin.site.register(Product, ProductAdmin) admin.site.register(Product, ProductAdmin)
for m in [ Order, BillRecord, BillingAddress, RecurringPeriod, VATRate ]: for m in [ Order, BillRecord, BillingAddress, RecurringPeriod, VATRate, StripeCustomer ]:
admin.site.register(m) admin.site.register(m)

View file

@ -89,6 +89,8 @@ class StripeCustomer(models.Model):
on_delete=models.CASCADE) on_delete=models.CASCADE)
stripe_id = models.CharField(max_length=32) stripe_id = models.CharField(max_length=32)
def __str__(self):
return self.owner.username
### ###
# Payments and Payment Methods. # Payments and Payment Methods.

View file

@ -0,0 +1,72 @@
{% extends 'uncloud/base.html' %}
{% block header %}
<script src="https://js.stripe.com/v3/"></script>
<style>
#content {
width: 400px;
margin: auto;
}
#callback-form {
display: none;
}
</style>
{% endblock %}
{% block 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>
{% endblock %}

View file

@ -1,3 +1,7 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.base import TemplateView
from django.shortcuts import render from django.shortcuts import render
from django.db import transaction from django.db import transaction
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
@ -43,6 +47,25 @@ class OrderViewSet(viewsets.ReadOnlyModelViewSet):
def get_queryset(self): def get_queryset(self):
return Order.objects.filter(owner=self.request.user) return Order.objects.filter(owner=self.request.user)
class RegisterCard(LoginRequiredMixin, TemplateView):
login_url = '/login/'
# This is not supposed to be "static" --
# the idea is to be able to switch the provider when needed
template_name = "uncloud_pay/stripe.html"
def get_context_data(self, **kwargs):
customer_id = uncloud_stripe.get_customer_id_for(self.request.user)
setup_intent = uncloud_stripe.create_setup_intent(customer_id)
context = super().get_context_data(**kwargs)
context['client_secret'] = setup_intent.client_secret
context['username'] = self.request.user
context['stripe_pk'] = uncloud_stripe.public_api_key
return context
class PaymentMethodViewSet(viewsets.ModelViewSet): class PaymentMethodViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]