first attempt at refatoring the user creating to order confirmaton page
This commit is contained in:
parent
143f40bd2d
commit
e2cb70a697
4 changed files with 127 additions and 47 deletions
|
@ -39,7 +39,7 @@
|
|||
<div class="col-xs-12 col-sm-6">
|
||||
<address>
|
||||
<h3><b>{% trans "Billed To:"%}</b></h3>
|
||||
{% with request.user.billing_addresses.first as billing_address %}
|
||||
{% with billing_address_data as billing_address %}
|
||||
{{billing_address.cardholder_name}}<br> {{billing_address.street_address}}, {{billing_address.postal_code}}<br>
|
||||
{{billing_address.city}}, {{billing_address.country}}.
|
||||
{% endwith %}
|
||||
|
|
|
@ -424,55 +424,60 @@ class PaymentOrderView(FormView):
|
|||
'name': request.user.name
|
||||
}
|
||||
custom_user = request.user
|
||||
customer = StripeCustomer.get_or_create(
|
||||
email=this_user.get('email'),
|
||||
token=token)
|
||||
else:
|
||||
this_user = {
|
||||
'email': form.cleaned_data.get('email'),
|
||||
'name': form.cleaned_data.get('name')
|
||||
}
|
||||
try:
|
||||
custom_user = CustomUser.objects.get(
|
||||
email=this_user.get('email'))
|
||||
except CustomUser.DoesNotExist:
|
||||
password = CustomUser.get_random_password()
|
||||
# Register the user, and do not send emails
|
||||
custom_user = CustomUser.register(
|
||||
this_user.get('name'), password,
|
||||
this_user.get('email'),
|
||||
app='dcl', 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'))
|
||||
billing_address_data = form.cleaned_data
|
||||
billing_address_data.update({
|
||||
'user': custom_user.id
|
||||
})
|
||||
billing_address_user_form = UserBillingAddressForm(
|
||||
instance=custom_user.billing_addresses.first(),
|
||||
data=billing_address_data)
|
||||
billing_address_user_form.save()
|
||||
customer = StripeCustomer.create_stripe_customer(
|
||||
email=this_user.get('email'),
|
||||
token=token,
|
||||
customer_name=form.cleaned_data.get('name'))
|
||||
#try:
|
||||
# custom_user = CustomUser.objects.get(
|
||||
# email=this_user.get('email'))
|
||||
#except CustomUser.DoesNotExist:
|
||||
# password = CustomUser.get_random_password()
|
||||
# # Register the user, and do not send emails
|
||||
# custom_user = CustomUser.register(
|
||||
# this_user.get('name'), password,
|
||||
# this_user.get('email'),
|
||||
# app='dcl', 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'))
|
||||
#billing_address_data = form.cleaned_data
|
||||
#billing_address_data.update({
|
||||
# 'user': custom_user.id
|
||||
#})
|
||||
#billing_address_user_form = UserBillingAddressForm(
|
||||
# instance=custom_user.billing_addresses.first(),
|
||||
# data=billing_address_data)
|
||||
#billing_address_user_form.save()
|
||||
#for k, v in form.cleaned_data.iteritems():
|
||||
request.session['billing_address_data'] = form.cleaned_data
|
||||
request.session['user'] = this_user
|
||||
# Get or create stripe customer
|
||||
customer = StripeCustomer.get_or_create(
|
||||
email=this_user.get('email'),
|
||||
token=token)
|
||||
if not customer:
|
||||
form.add_error("__all__", "Invalid credit card")
|
||||
return self.render_to_response(
|
||||
self.get_context_data(form=form))
|
||||
|
||||
request.session['token'] = token
|
||||
request.session['customer'] = customer.id
|
||||
request.session['customer'] = customer.id if request.user.is_authenticated() else customer
|
||||
return HttpResponseRedirect(
|
||||
reverse('datacenterlight:order_confirmation'))
|
||||
else:
|
||||
|
@ -492,9 +497,14 @@ class OrderConfirmationView(DetailView):
|
|||
if 'token' not in request.session:
|
||||
return HttpResponseRedirect(reverse('datacenterlight:payment'))
|
||||
stripe_customer_id = request.session.get('customer')
|
||||
if request.user.is_authenticated():
|
||||
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
|
||||
stripe_api_cus_id = customer.stripe_id
|
||||
else:
|
||||
stripe_api_cus_id = stripe_customer_id
|
||||
|
||||
stripe_utils = StripeUtils()
|
||||
card_details = stripe_utils.get_card_details(customer.stripe_id,
|
||||
card_details = stripe_utils.get_card_details(stripe_api_cus_id,
|
||||
request.session.get(
|
||||
'token'))
|
||||
if not card_details.get('response_object'):
|
||||
|
@ -506,7 +516,8 @@ class OrderConfirmationView(DetailView):
|
|||
context = {
|
||||
'site_url': reverse('datacenterlight:index'),
|
||||
'cc_last4': card_details.get('response_object').get('last4'),
|
||||
'cc_brand': card_details.get('response_object').get('brand')
|
||||
'cc_brand': card_details.get('response_object').get('brand'),
|
||||
'billing_address_data': request.session.get('billing_address_data')
|
||||
}
|
||||
return render(request, self.template_name, context)
|
||||
|
||||
|
@ -514,15 +525,23 @@ class OrderConfirmationView(DetailView):
|
|||
template = request.session.get('template')
|
||||
specs = request.session.get('specs')
|
||||
user = request.session.get('user')
|
||||
|
||||
|
||||
#stripe_customer_id = request.session.get('customer')
|
||||
#customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
|
||||
|
||||
stripe_customer_id = request.session.get('customer')
|
||||
if request.user.is_authenticated():
|
||||
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
|
||||
billing_address_data = {}
|
||||
billing_address_id = request.user.billing_addresses.first().id
|
||||
stripe_api_cus_id = customer.stripe_id
|
||||
else:
|
||||
stripe_api_cus_id = stripe_customer_id
|
||||
|
||||
vm_template_id = template.get('id', 1)
|
||||
|
||||
# Make stripe charge to a customer
|
||||
stripe_utils = StripeUtils()
|
||||
card_details = stripe_utils.get_card_details(customer.stripe_id,
|
||||
card_details = stripe_utils.get_card_details(stripe_api_cus_id,
|
||||
request.session.get(
|
||||
'token'))
|
||||
if not card_details.get('response_object'):
|
||||
|
@ -563,6 +582,49 @@ class OrderConfirmationView(DetailView):
|
|||
extra_tags='failed_payment')
|
||||
return HttpResponseRedirect(
|
||||
reverse('datacenterlight:payment') + '#payment_error')
|
||||
|
||||
# Create user if the user is not logged in
|
||||
if not request.user.is_authenticated():
|
||||
try:
|
||||
custom_user = CustomUser.objects.get(
|
||||
email=user.get('email'))
|
||||
except CustomUser.DoesNotExist:
|
||||
logger.debug("Customer {} does not exist.".format(user.get('email')))
|
||||
password = CustomUser.get_random_password()
|
||||
# Register the user, and do not send emails
|
||||
custom_user = CustomUser.register(
|
||||
this_user.get('name'), password,
|
||||
this_user.get('email'),
|
||||
app='dcl', base_url=None, send_email=False
|
||||
)
|
||||
logger.debug("Created user {}.".format(user.get('email')))
|
||||
#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'))
|
||||
|
||||
# Save billing address
|
||||
billing_address_data = request.session.get('billing_address_data')
|
||||
logger.debug('billing_address_data is {}'.format(billing_address_data))
|
||||
billing_address_data.update({
|
||||
'user': custom_user.id
|
||||
})
|
||||
billing_address_user_form = UserBillingAddressForm(
|
||||
instance=custom_user.billing_addresses.first(),
|
||||
data=billing_address_data)
|
||||
billing_address_user_form.save()
|
||||
billing_address_id = billing_address_user_form.id
|
||||
logger.debug("billking address id = {}".format(billing_address_id))
|
||||
|
||||
create_vm_task.delay(vm_template_id, user, specs, template,
|
||||
stripe_customer_id, billing_address_data,
|
||||
billing_address_id,
|
||||
|
|
|
@ -368,7 +368,7 @@ MIGRATION_MODULES = {
|
|||
'cmsplugin_filer_teaser': 'cmsplugin_filer_teaser.migrations_django',
|
||||
'cmsplugin_filer_utils': 'cmsplugin_filer_utils.migrations_django',
|
||||
'cmsplugin_filer_video': 'cmsplugin_filer_video.migrations_django',
|
||||
'djangocms_text_ckeditor': 'djangocms_text_ckeditor.migrations',
|
||||
'djangocms_text_ckeditor': 'djangocms_text_ckeditor.migrations_django',
|
||||
}
|
||||
|
||||
STATICFILES_FINDERS = (
|
||||
|
|
|
@ -176,6 +176,25 @@ class StripeCustomer(models.Model):
|
|||
def __str__(self):
|
||||
return "%s - %s" % (self.stripe_id, self.user.email)
|
||||
|
||||
@classmethod
|
||||
def create_stripe_api_customer(cls, email=None, token=None, customer_name=None):
|
||||
"""
|
||||
This method creates a Stripe API customer with the given
|
||||
email, token and customer_name. This is different from
|
||||
get_or_create method below in that it does not create a
|
||||
CustomUser and associate the customer created in stripe
|
||||
with it, while get_or_create does that before creating the
|
||||
stripe user.
|
||||
"""
|
||||
stripe_utils = StripeUtils()
|
||||
stripe_data = stripe_utils.create_customer(token, email, customer_name)
|
||||
if stripe_data.get('response_object'):
|
||||
stripe_cus_id = stripe_data.get('response_object').get('id')
|
||||
return stripe_cus_id
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_or_create(cls, email=None, token=None):
|
||||
"""
|
||||
|
@ -195,7 +214,6 @@ class StripeCustomer(models.Model):
|
|||
|
||||
except StripeCustomer.DoesNotExist:
|
||||
user = CustomUser.objects.get(email=email)
|
||||
|
||||
stripe_utils = StripeUtils()
|
||||
stripe_data = stripe_utils.create_customer(token, email, user.name)
|
||||
if stripe_data.get('response_object'):
|
||||
|
|
Loading…
Reference in a new issue