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"> |                         <div class="col-xs-12 col-sm-6"> | ||||||
|                             <address> |                             <address> | ||||||
|                             <h3><b>{% trans "Billed To:"%}</b></h3> |                             <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.cardholder_name}}<br> {{billing_address.street_address}}, {{billing_address.postal_code}}<br> | ||||||
|                                 {{billing_address.city}}, {{billing_address.country}}. |                                 {{billing_address.city}}, {{billing_address.country}}. | ||||||
|                             {% endwith %} |                             {% endwith %} | ||||||
|  |  | ||||||
|  | @ -424,55 +424,60 @@ class PaymentOrderView(FormView): | ||||||
|                     'name': request.user.name |                     'name': request.user.name | ||||||
|                 } |                 } | ||||||
|                 custom_user = request.user |                 custom_user = request.user | ||||||
|  |                 customer = StripeCustomer.get_or_create( | ||||||
|  |                     email=this_user.get('email'), | ||||||
|  |                     token=token) | ||||||
|             else: |             else: | ||||||
|                 this_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') | ||||||
|                 } |                 } | ||||||
|                 try: |                 customer = StripeCustomer.create_stripe_customer( | ||||||
|                     custom_user = CustomUser.objects.get( |                     email=this_user.get('email'), | ||||||
|                         email=this_user.get('email')) |                     token=token, | ||||||
|                 except CustomUser.DoesNotExist: |                     customer_name=form.cleaned_data.get('name')) | ||||||
|                     password = CustomUser.get_random_password() |                 #try: | ||||||
|                     # Register the user, and do not send emails |                 #    custom_user = CustomUser.objects.get( | ||||||
|                     custom_user = CustomUser.register( |                 #        email=this_user.get('email')) | ||||||
|                         this_user.get('name'), password, |                 #except CustomUser.DoesNotExist: | ||||||
|                         this_user.get('email'), |                 #    password = CustomUser.get_random_password() | ||||||
|                         app='dcl', base_url=None, send_email=False |                 #    # Register the user, and do not send emails | ||||||
|                     ) |                 #    custom_user = CustomUser.register( | ||||||
|                     new_user = authenticate( |                 #        this_user.get('name'), password, | ||||||
|                         username=custom_user.email, |                 #        this_user.get('email'), | ||||||
|                         password=password) |                 #        app='dcl', base_url=None, send_email=False | ||||||
|                     login(request, new_user) |                 #    ) | ||||||
|                 else: |                 #    new_user = authenticate( | ||||||
|                     # new user used the email of existing user, fail |                 #        username=custom_user.email, | ||||||
|                     messages.error( |                 #        password=password) | ||||||
|                         self.request, |                 #    login(request, new_user) | ||||||
|                         _('Another user exists with that email!'), |                 #else: | ||||||
|                         extra_tags='duplicate_email' |                 #    # new user used the email of existing user, fail | ||||||
|                     ) |                 #    messages.error( | ||||||
|                     return HttpResponseRedirect( |                 #        self.request, | ||||||
|                         reverse('datacenterlight:payment')) |                 #        _('Another user exists with that email!'), | ||||||
|             billing_address_data = form.cleaned_data |                 #        extra_tags='duplicate_email' | ||||||
|             billing_address_data.update({ |                 #    ) | ||||||
|                 'user': custom_user.id |                 #    return HttpResponseRedirect( | ||||||
|             }) |                 #        reverse('datacenterlight:payment')) | ||||||
|             billing_address_user_form = UserBillingAddressForm( |             #billing_address_data = form.cleaned_data | ||||||
|                 instance=custom_user.billing_addresses.first(), |             #billing_address_data.update({ | ||||||
|                 data=billing_address_data) |             #    'user': custom_user.id | ||||||
|             billing_address_user_form.save() |             #}) | ||||||
|  |             #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 |             request.session['user'] = this_user | ||||||
|             # Get or create stripe customer |             # Get or create stripe customer | ||||||
|             customer = StripeCustomer.get_or_create( |  | ||||||
|                 email=this_user.get('email'), |  | ||||||
|                 token=token) |  | ||||||
|             if not customer: |             if not customer: | ||||||
|                 form.add_error("__all__", "Invalid credit card") |                 form.add_error("__all__", "Invalid credit card") | ||||||
|                 return self.render_to_response( |                 return self.render_to_response( | ||||||
|                     self.get_context_data(form=form)) |                     self.get_context_data(form=form)) | ||||||
| 
 |  | ||||||
|             request.session['token'] = token |             request.session['token'] = token | ||||||
|             request.session['customer'] = customer.id |             request.session['customer'] = customer.id if request.user.is_authenticated() else customer | ||||||
|             return HttpResponseRedirect( |             return HttpResponseRedirect( | ||||||
|                 reverse('datacenterlight:order_confirmation')) |                 reverse('datacenterlight:order_confirmation')) | ||||||
|         else: |         else: | ||||||
|  | @ -492,9 +497,14 @@ class OrderConfirmationView(DetailView): | ||||||
|         if 'token' not in request.session: |         if 'token' not in request.session: | ||||||
|             return HttpResponseRedirect(reverse('datacenterlight:payment')) |             return HttpResponseRedirect(reverse('datacenterlight:payment')) | ||||||
|         stripe_customer_id = request.session.get('customer') |         stripe_customer_id = request.session.get('customer') | ||||||
|  |         if request.user.is_authenticated(): | ||||||
|             customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() |             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() |         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( |                                                      request.session.get( | ||||||
|                                                          'token')) |                                                          'token')) | ||||||
|         if not card_details.get('response_object'): |         if not card_details.get('response_object'): | ||||||
|  | @ -506,7 +516,8 @@ class OrderConfirmationView(DetailView): | ||||||
|         context = { |         context = { | ||||||
|             'site_url': reverse('datacenterlight:index'), |             'site_url': reverse('datacenterlight:index'), | ||||||
|             'cc_last4': card_details.get('response_object').get('last4'), |             '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) |         return render(request, self.template_name, context) | ||||||
| 
 | 
 | ||||||
|  | @ -514,15 +525,23 @@ class OrderConfirmationView(DetailView): | ||||||
|         template = request.session.get('template') |         template = request.session.get('template') | ||||||
|         specs = request.session.get('specs') |         specs = request.session.get('specs') | ||||||
|         user = request.session.get('user') |         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') |         stripe_customer_id = request.session.get('customer') | ||||||
|  |         if request.user.is_authenticated(): | ||||||
|             customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() |             customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() | ||||||
|         billing_address_data = {} |             stripe_api_cus_id = customer.stripe_id | ||||||
|         billing_address_id = request.user.billing_addresses.first().id |         else: | ||||||
|  |             stripe_api_cus_id = stripe_customer_id | ||||||
|  |          | ||||||
|         vm_template_id = template.get('id', 1) |         vm_template_id = template.get('id', 1) | ||||||
| 
 | 
 | ||||||
|         # Make stripe charge to a customer |         # Make stripe charge to a customer | ||||||
|         stripe_utils = StripeUtils() |         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( |                                                      request.session.get( | ||||||
|                                                          'token')) |                                                          'token')) | ||||||
|         if not card_details.get('response_object'): |         if not card_details.get('response_object'): | ||||||
|  | @ -563,6 +582,49 @@ class OrderConfirmationView(DetailView): | ||||||
|                                  extra_tags='failed_payment') |                                  extra_tags='failed_payment') | ||||||
|             return HttpResponseRedirect( |             return HttpResponseRedirect( | ||||||
|                 reverse('datacenterlight:payment') + '#payment_error') |                 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, |         create_vm_task.delay(vm_template_id, user, specs, template, | ||||||
|                              stripe_customer_id, billing_address_data, |                              stripe_customer_id, billing_address_data, | ||||||
|                              billing_address_id, |                              billing_address_id, | ||||||
|  |  | ||||||
|  | @ -368,7 +368,7 @@ MIGRATION_MODULES = { | ||||||
|     'cmsplugin_filer_teaser': 'cmsplugin_filer_teaser.migrations_django', |     'cmsplugin_filer_teaser': 'cmsplugin_filer_teaser.migrations_django', | ||||||
|     'cmsplugin_filer_utils': 'cmsplugin_filer_utils.migrations_django', |     'cmsplugin_filer_utils': 'cmsplugin_filer_utils.migrations_django', | ||||||
|     'cmsplugin_filer_video': 'cmsplugin_filer_video.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 = ( | STATICFILES_FINDERS = ( | ||||||
|  |  | ||||||
|  | @ -176,6 +176,25 @@ class StripeCustomer(models.Model): | ||||||
|     def __str__(self): |     def __str__(self): | ||||||
|         return "%s - %s" % (self.stripe_id, self.user.email) |         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 |     @classmethod | ||||||
|     def get_or_create(cls, email=None, token=None): |     def get_or_create(cls, email=None, token=None): | ||||||
|         """ |         """ | ||||||
|  | @ -195,7 +214,6 @@ class StripeCustomer(models.Model): | ||||||
| 
 | 
 | ||||||
|         except StripeCustomer.DoesNotExist: |         except StripeCustomer.DoesNotExist: | ||||||
|             user = CustomUser.objects.get(email=email) |             user = CustomUser.objects.get(email=email) | ||||||
| 
 |  | ||||||
|             stripe_utils = StripeUtils() |             stripe_utils = StripeUtils() | ||||||
|             stripe_data = stripe_utils.create_customer(token, email, user.name) |             stripe_data = stripe_utils.create_customer(token, email, user.name) | ||||||
|             if stripe_data.get('response_object'): |             if stripe_data.get('response_object'): | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue