def post(self, request, *args, **kwargs): if 'product' in request.POST: # query for the supplied product product = None try: product = GenericProduct.objects.get( id=request.POST['generic_payment_form-product_name']) except GenericProduct.DoesNotExist as dne: logger.error( "The requested product '{}' does not exist".format( request.POST['generic_payment_form-product_name'])) except GenericProduct.MultipleObjectsReturned as mpe: logger.error( "There seem to be more than one product with " "the name {}".format( request.POST['generic_payment_form-product_name'])) product = GenericProduct.objects.all( product_name=request. POST['generic_payment_form-product_name']).first() if product is None: return JsonResponse({}) else: return JsonResponse({ 'amount': product.get_actual_price(), 'isSubscription': product.product_is_subscription }) if 'login_form' in request.POST: login_form = HostingUserLoginForm(data=request.POST, prefix='login_form') if login_form.is_valid(): email = login_form.cleaned_data.get('email') password = login_form.cleaned_data.get('password') auth_user = authenticate(email=email, password=password) if auth_user: login(self.request, auth_user) if 'product_slug' in kwargs: return HttpResponseRedirect( reverse('show_product', kwargs={ 'product_slug': kwargs['product_slug'] })) return HttpResponseRedirect( reverse('datacenterlight:payment')) else: context = self.get_context_data() context['login_form'] = login_form return self.render_to_response(context) if request.user.is_authenticated(): address_form = BillingAddressForm(data=request.POST, ) else: address_form = BillingAddressFormSignup(data=request.POST, ) if address_form.is_valid(): # Check if we are in a generic payment case and handle the generic # payment details form before we go on to verify payment if ('generic_payment_type' in request.session and self.request.session['generic_payment_type'] == 'generic'): if 'product_id' in request.session: generic_payment_form = ProductPaymentForm( data=request.POST, prefix='generic_payment_form', product_id=request.session['product_id']) else: generic_payment_form = GenericPaymentForm( data=request.POST, prefix='generic_payment_form') if generic_payment_form.is_valid(): logger.debug("Generic payment form is valid.") if 'product_id' in request.session: product = generic_payment_form.product else: product = generic_payment_form.cleaned_data.get( 'product_name') gp_details = { "product_name": product.product_name, "amount": generic_payment_form.cleaned_data.get('amount'), "recurring": generic_payment_form.cleaned_data.get('recurring'), "description": generic_payment_form.cleaned_data.get('description'), "product_id": product.id, "product_slug": product.product_slug } request.session["generic_payment_details"] = (gp_details) else: logger.debug("Generic payment form invalid") context = self.get_context_data() context['generic_payment_form'] = generic_payment_form context['billing_address_form'] = address_form return self.render_to_response(context) token = address_form.cleaned_data.get('token') if token is '': card_id = address_form.cleaned_data.get('card') try: user_card_detail = UserCardDetail.objects.get(id=card_id) if not request.user.has_perm('view_usercarddetail', user_card_detail): raise UserCardDetail.DoesNotExist( _("{user} does not have permission to access the " "card").format(user=request.user.email)) except UserCardDetail.DoesNotExist as e: ex = str(e) logger.error("Card Id: {card_id}, Exception: {ex}".format( card_id=card_id, ex=ex)) msg = _("An error occurred. Details: {}".format(ex)) messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') return HttpResponseRedirect( reverse('datacenterlight:payment') + '#payment_error') request.session['card_id'] = user_card_detail.id else: request.session['token'] = token if request.user.is_authenticated(): this_user = { 'email': request.user.email, 'name': request.user.name } customer = StripeCustomer.get_or_create( email=this_user.get('email'), token=token) else: user_email = address_form.cleaned_data.get('email') user_name = address_form.cleaned_data.get('name') this_user = {'email': user_email, 'name': user_name} try: custom_user = CustomUser.objects.get(email=user_email) customer = StripeCustomer.objects.filter( user_id=custom_user.id).first() if customer is None: logger.debug( ("User {email} is already registered with us." "But, StripeCustomer does not exist for {email}." "Hence, creating a new StripeCustomer.").format( email=user_email)) customer = StripeCustomer.create_stripe_api_customer( email=user_email, token=token, customer_name=user_name) except CustomUser.DoesNotExist: logger.debug( ("StripeCustomer does not exist for {email}." "Hence, creating a new StripeCustomer.").format( email=user_email)) customer = StripeCustomer.create_stripe_api_customer( email=user_email, token=token, customer_name=user_name) request.session['billing_address_data'] = address_form.cleaned_data request.session['user'] = this_user # Get or create stripe customer if not customer: address_form.add_error("__all__", "Invalid credit card") return self.render_to_response( self.get_context_data(billing_address_form=address_form)) if type(customer) is StripeCustomer: request.session['customer'] = customer.stripe_id else: request.session['customer'] = customer return HttpResponseRedirect( reverse('datacenterlight:order_confirmation')) else: context = self.get_context_data() context['billing_address_form'] = address_form return self.render_to_response(context)
def post(self, request, *args, **kwargs): if 'login_form' in request.POST: login_form = HostingUserLoginForm( data=request.POST, prefix='login_form' ) if login_form.is_valid(): email = login_form.cleaned_data.get('email') password = login_form.cleaned_data.get('password') auth_user = authenticate(email=email, password=password) if auth_user: login(self.request, auth_user) return HttpResponseRedirect( reverse('datacenterlight:payment') ) else: context = self.get_context_data() context['login_form'] = login_form return self.render_to_response(context) if request.user.is_authenticated(): address_form = BillingAddressForm( data=request.POST, ) else: address_form = BillingAddressFormSignup( data=request.POST, ) if address_form.is_valid(): token = address_form.cleaned_data.get('token') if token is '': card_id = address_form.cleaned_data.get('card') try: user_card_detail = UserCardDetail.objects.get(id=card_id) if not request.user.has_perm( 'view_usercarddetail', user_card_detail ): raise UserCardDetail.DoesNotExist( _("{user} does not have permission to access the " "card").format(user=request.user.email) ) except UserCardDetail.DoesNotExist as e: ex = str(e) logger.error("Card Id: {card_id}, Exception: {ex}".format( card_id=card_id, ex=ex ) ) msg = _("An error occurred. Details: {}".format(ex)) messages.add_message( self.request, messages.ERROR, msg, extra_tags='make_charge_error' ) return HttpResponseRedirect( reverse('datacenterlight:payment') + '#payment_error' ) request.session['card_id'] = user_card_detail.id else: request.session['token'] = token if request.user.is_authenticated(): this_user = { 'email': request.user.email, 'name': request.user.name } customer = StripeCustomer.get_or_create( email=this_user.get('email'), token=token ) else: user_email = address_form.cleaned_data.get('email') user_name = address_form.cleaned_data.get('name') this_user = { 'email': user_email, 'name': user_name } try: custom_user = CustomUser.objects.get(email=user_email) customer = StripeCustomer.objects.filter( user_id=custom_user.id).first() if customer is None: logger.debug( ("User {email} is already registered with us." "But, StripeCustomer does not exist for {email}." "Hence, creating a new StripeCustomer.").format( email=user_email ) ) customer = StripeCustomer.create_stripe_api_customer( email=user_email, token=token, customer_name=user_name) except CustomUser.DoesNotExist: logger.debug( ("StripeCustomer does not exist for {email}." "Hence, creating a new StripeCustomer.").format( email=user_email ) ) customer = StripeCustomer.create_stripe_api_customer( email=user_email, token=token, customer_name=user_name) request.session['billing_address_data'] = address_form.cleaned_data request.session['user'] = this_user # Get or create stripe customer if not customer: address_form.add_error( "__all__", "Invalid credit card" ) return self.render_to_response( self.get_context_data( billing_address_form=address_form ) ) if type(customer) is StripeCustomer: request.session['customer'] = customer.stripe_id else: request.session['customer'] = customer return HttpResponseRedirect( reverse('datacenterlight:order_confirmation')) else: context = self.get_context_data() context['billing_address_form'] = address_form return self.render_to_response(context)