From 6eb5565537686d6f19f9a5fe6cedeae0f15f515a Mon Sep 17 00:00:00 2001 From: Tomislav R Date: Sat, 21 May 2016 00:03:17 +0200 Subject: [PATCH] https://redmine.ungleich.ch/issues/2290 --- hosting/views.py | 8 ++++++-- membership/models.py | 11 +++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index fbeb36ea..8262517d 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1,5 +1,5 @@ -from django.shortcuts import get_object_or_404, render +from django.shortcuts import get_object_or_404, render,render_to_response from django.core.urlresolvers import reverse_lazy, reverse from django.contrib.auth.mixins import LoginRequiredMixin @@ -7,6 +7,7 @@ from django.views.generic import View, CreateView, FormView, ListView, DetailVie from django.http import HttpResponseRedirect from django.contrib.auth import authenticate, login from django.conf import settings +from django.contrib import messages from membership.models import CustomUser, StripeCustomer from utils.stripe_utils import StripeUtils @@ -133,7 +134,6 @@ class SignupView(CreateView): return next_url def form_valid(self, form): - name = form.cleaned_data.get('name') email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') @@ -179,6 +179,10 @@ class PaymentVMView(LoginRequiredMixin, FormView): # Get or create stripe customer customer = StripeCustomer.get_or_create(email=self.request.user.email, token=token) + if not customer: + form.add_error("__all__","Invalid credit card") + return self.render_to_response(self.get_context_data(form=form)) + # Create Virtual Machine Plan plan = VirtualMachinePlan.create(plan_data, request.user) diff --git a/membership/models.py b/membership/models.py index d9861166..97d3ee06 100644 --- a/membership/models.py +++ b/membership/models.py @@ -143,12 +143,15 @@ class StripeCustomer(models.Model): stripe_utils = StripeUtils() stripe_data = stripe_utils.create_customer(token, email) - stripe_cus_id = stripe_data.get('response_object').get('id') + if stripe_data.get('response_object'): + stripe_cus_id = stripe_data.get('response_object').get('id') - stripe_customer = StripeCustomer.objects.\ - create(user=user, stripe_id=stripe_cus_id) + stripe_customer = StripeCustomer.objects.\ + create(user=user, stripe_id=stripe_cus_id) - return stripe_customer + return stripe_customer + else: + return None class CreditCards(models.Model):