user logged in after signup on payment page

This commit is contained in:
Arvind Tiwari 2017-09-09 02:10:25 +05:30
parent f2a04c20f2
commit 7a4573f7ad
3 changed files with 43 additions and 19 deletions

View file

@ -68,6 +68,11 @@
<h3><b>{%trans "Billing Address"%}</b></h3> <h3><b>{%trans "Billing Address"%}</b></h3>
{% endif %} {% endif %}
<hr class="top-hr"> <hr class="top-hr">
{% for message in messages %}
{% if 'duplicate_email' in message.tags %}
<p class="text-danger">{{message}}</p>
{% endif %}
{% endfor %}
<form role="form" id="billing-form" method="post" action="" novalidate> <form role="form" id="billing-form" method="post" action="" novalidate>
{% csrf_token %} {% csrf_token %}
{% for field in form %} {% for field in form %}
@ -146,10 +151,14 @@
{% endif %} {% endif %}
<div id='payment_error'> <div id='payment_error'>
{% for message in messages %} {% for message in messages %}
{% if 'failed_payment' or 'make_charge_error' in message.tags %} {% if 'failed_payment' in message.tags or 'make_charge_error' in message.tags %}
<ul class="list-unstyled"><li> <ul class="list-unstyled">
<p class="card-warning-content card-warning-error">{{ message|safe }}</p> <li><p class="card-warning-content card-warning-error">{{ message|safe }}</p></li>
</li></ul> </ul>
{% elif not form.non_field_errors %}
<p class="card-warning-content">
{% trans "You are not making any payment yet. After placing your order, you will be taken to the Submit Payment Page." %}
</p>
{% endif %} {% endif %}
{% endfor %} {% endfor %}

View file

@ -1,6 +1,7 @@
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth import login, authenticate
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
@ -484,30 +485,44 @@ class PaymentOrderView(FormView):
billing_address_data = form.cleaned_data billing_address_data = form.cleaned_data
token = form.cleaned_data.get('token') token = form.cleaned_data.get('token')
if request.user.is_authenticated(): if request.user.is_authenticated():
user = { this_user = {
'email': request.user.email, 'email': request.user.email,
'name': request.user.name 'name': request.user.name
} }
else: else:
user = { this_user = {
'email': form.cleaned_data.get('email'), 'email': form.cleaned_data.get('email'),
'name': form.cleaned_data.get('name') 'name': form.cleaned_data.get('name')
} }
request.session['user'] = user try:
try: custom_user = CustomUser.objects.get(
CustomUser.objects.get(email=user.get('email')) email=this_user.get('email'))
except CustomUser.DoesNotExist: except CustomUser.DoesNotExist:
password = CustomUser.get_random_password() password = CustomUser.get_random_password()
# Register the user, and do not send emails # Register the user, and do not send emails
CustomUser.register(user.get('name'), custom_user = CustomUser.register(
password, this_user.get('name'), password,
user.get('email'), this_user.get('email'),
app='dcl', app='dcl', base_url=None, send_email=False
base_url=None, send_email=False) )
new_user = authenticate(
username=custom_user.email,
password=password)
login(request, new_user)
else:
# new user used the email of existing user, fail
messages.error(
self.request,
_('Another user exists with that email!'),
extra_tags='duplicate_email'
)
return HttpResponseRedirect(
reverse('datacenterlight:payment'))
request.session['user'] = this_user
# Get or create stripe customer # Get or create stripe customer
customer = StripeCustomer.get_or_create( customer = StripeCustomer.get_or_create(
email=user.get('email'), email=this_user.get('email'),
token=token) token=token)
if not customer: if not customer:
form.add_error("__all__", "Invalid credit card") form.add_error("__all__", "Invalid credit card")

File diff suppressed because one or more lines are too long