hosting: Moving the stripe charge to order confirmation page (still incomplete)
This commit is contained in:
		
					parent
					
						
							
								cad5e209ed
							
						
					
				
			
			
				commit
				
					
						2a9b208719
					
				
			
		
					 2 changed files with 80 additions and 110 deletions
				
			
		|  | @ -20,14 +20,26 @@ | |||
|     <div class="row"> | ||||
|         <div class="col-xs-12 col-md-8 col-md-offset-2"> | ||||
|     		<div class="invoice-title"> | ||||
|     			<h2>{{page_header_text}}</h2><h3 class="pull-right">{% trans "Order #"%} {{order.id}}</h3> | ||||
|     			<h2>{{page_header_text}}</h2> | ||||
|                 <h3 class="pull-right"> | ||||
|                 {% if order %} | ||||
|                     {% trans "Order #"%} {{order.id}} | ||||
|                 {% endif %} | ||||
|                 </h3> | ||||
|     		</div> | ||||
|     		<hr> | ||||
|     		<div class="row"> | ||||
| 				<div class="col-xs-12 col-md-6 pull-right order-confirm-date"> | ||||
|                     <address> | ||||
|                         <strong>{% trans "Date"%}:</strong><br> | ||||
|                         <span id="order-created_at">{{order.created_at|date:'Y-m-d H:i'}}</span><br><br> | ||||
|                             <span id="order-created_at"> | ||||
|                                 {% if order %} | ||||
|                                     {{order.created_at|date:'Y-m-d H:i'}} | ||||
|                                 {% else %} | ||||
|                                     {% now "Y-m-d H:i" %} | ||||
|                                 {% endif %} | ||||
|                             </span><br><br> | ||||
|                         {% if order %} | ||||
|                         <strong>{% trans "Status:"%}</strong><br> | ||||
|                             {% if order.status == 'Approved' %} | ||||
|                                 <strong class="text-success">{% trans "Approved" %}</strong> | ||||
|  | @ -35,15 +47,23 @@ | |||
|                                 <strong class="text-danger">{% trans "Declined" %}</strong> | ||||
|                             {% endif %} | ||||
|                         <br><br> | ||||
|                         {% endif %} | ||||
|                     </address> | ||||
| 
 | ||||
|                 </div> | ||||
|     			<div class="col-xs-12 col-md-6"> | ||||
|     				<address> | ||||
|                     <h3><b>{% trans "Billed To:"%}</b></h3> | ||||
|                         {% if order %} | ||||
|     					{{user.name}}<br> | ||||
|                         {{order.billing_address.street_address}},{{order.billing_address.postal_code}}<br> | ||||
|                         {{order.billing_address.city}}, {{order.billing_address.country}}. | ||||
|                         {% else %} | ||||
|                             {% with request.session.billing_address_data as billing_address %} | ||||
|                                 {{billing_address|get_value_from_dict:'cardholder_name'}}<br> {{billing_address|get_value_from_dict:'street_address'}}, {{billing_address|get_value_from_dict:'postal_code'}}<br> | ||||
|                                 {{billing_address|get_value_from_dict:'city'}}, {{billing_address|get_value_from_dict:'country'}}. | ||||
|                             {% endwith %} | ||||
|                         {% endif %} | ||||
|     				</address> | ||||
|     			</div> | ||||
| 
 | ||||
|  | @ -52,8 +72,13 @@ | |||
|     			<div class="col-xs-6"> | ||||
|     				<address> | ||||
|     					<strong>{% trans "Payment Method:"%}</strong><br> | ||||
|                         {% if order %} | ||||
|     					{{order.cc_brand}} {% trans "ending in" %} **** {{order.last4}}<br> | ||||
|     					{{user.email}} | ||||
|                         {% else %} | ||||
|                                     {{cc_brand}} {% trans "ending" %} **** {{cc_last4}}<br> | ||||
|                                     {{request.session.user.email}} | ||||
|                         {% endif %} | ||||
|     				</address> | ||||
|     			</div> | ||||
|     		</div> | ||||
|  | @ -65,6 +90,19 @@ | |||
|             <h3><b>{% trans "Order summary"%}</b></h3> | ||||
|             <hr> | ||||
|             <div class="content"> | ||||
|                 {% if request.session.specs %} | ||||
|                     {% with request.session.specs as vm %} | ||||
|                         <p><b>{% trans "Cores"%}</b> <span class="pull-right">{{vm.cpu}}</span></p> | ||||
|                         <hr> | ||||
|                         <p><b>{% trans "Memory"%}</b> <span class="pull-right">{{vm.memory}} GB</span></p> | ||||
|                         <hr> | ||||
|                         <p><b>{% trans "Disk space"%}</b> <span class="pull-right">{{vm.disk_size}} GB</span></p> | ||||
|                         <hr> | ||||
|                         <p><b>{% trans "Configuration"%}</b> <span class="pull-right">{{request.session.template.name}}</span></p> | ||||
|                         <hr> | ||||
|                         <h4>{% trans "Total"%}<p class="pull-right"><b>{{vm.price}} CHF</b><span class="dcl-price-month"> /{% trans "Month" %}</span></p></h4> | ||||
|                     {% endwith %} | ||||
|                 {% else %} | ||||
|                     <p><b>{% trans "Cores"%}</b> <span class="pull-right">{{vm.cores}}</span></p> | ||||
|                     <hr> | ||||
|                     <p><b>{% trans "Memory"%}</b> <span class="pull-right">{{vm.memory}} GB</span></p> | ||||
|  | @ -72,14 +110,29 @@ | |||
|                     <p><b>{% trans "Disk space"%}</b> <span class="pull-right">{{vm.disk_size}} GB</span></p> | ||||
|                     <hr> | ||||
|                                     <h4>{% trans "Total"%}<p class="pull-right"><b>{{vm.price}} CHF</b></p></h4> | ||||
|                 {% endif %} | ||||
|             </div> | ||||
|             <br/> | ||||
|             {% if not order %} | ||||
|             <form method="post"> | ||||
|                 {% csrf_token %} | ||||
|                 <div class="row"> | ||||
|                     <div class="col-sm-8"> | ||||
|                         <p class="dcl-place-order-text">{% blocktrans with vm_price=request.session.specs.price %}By clicking "Place order" this plan will charge your credit card account with the fee of {{ vm_price }}CHF/month{% endblocktrans %}.</p> | ||||
|                     </div> | ||||
|                     <div class="col-sm-4 content"> | ||||
|                         <a href="{{next_url}}" ><button class="btn btn-info pull-right">{% trans "Place order"%}</button></a> | ||||
|                     </div> | ||||
|                 </div> | ||||
|             </form> | ||||
|             {% else %} | ||||
|                 {% url 'hosting:payment' as payment_url %} | ||||
|                 {% if payment_url in request.META.HTTP_REFERER  %} | ||||
|                 <div class=" content pull-right"> | ||||
|                     <a href="{% url 'hosting:virtual_machines'%}" ><button class="btn btn-info">{% trans "Finish Configuration"%}</button></a> | ||||
|                 </div> | ||||
|                 {% endif %} | ||||
|             {% endif %} | ||||
|         </div> | ||||
|     </div> | ||||
|     {% endif %} | ||||
|  |  | |||
|  | @ -569,100 +569,17 @@ class PaymentVMView(LoginRequiredMixin, FormView): | |||
|             customer = StripeCustomer.get_or_create(email=owner.email, | ||||
|                                                     token=token) | ||||
|             if not customer: | ||||
|                 msg = _("Invalid credit card") | ||||
|                 messages.add_message( | ||||
|                     self.request, messages.ERROR, msg, extra_tags='make_charge_error') | ||||
|                 return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') | ||||
|                 form.add_error("__all__", _("Invalid credit card")) | ||||
|                 return self.render_to_response( | ||||
|                     self.get_context_data(form=form)) | ||||
| 
 | ||||
|             # Create Billing Address | ||||
|             billing_address = form.save() | ||||
| 
 | ||||
|             # Make stripe charge to a customer | ||||
|             stripe_utils = StripeUtils() | ||||
|             charge_response = stripe_utils.make_charge(amount=final_price, | ||||
|                                                        customer=customer.stripe_id) | ||||
| 
 | ||||
|             # Check if the payment was approved | ||||
|             if not charge_response.get('response_object'): | ||||
|                 msg = charge_response.get('error') | ||||
|                 messages.add_message( | ||||
|                     self.request, messages.ERROR, msg, extra_tags='make_charge_error') | ||||
|                 return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') | ||||
| 
 | ||||
|             charge = charge_response.get('response_object') | ||||
| 
 | ||||
|             # Create OpenNebulaManager | ||||
|             manager = OpenNebulaManager(email=owner.email, | ||||
|                                         password=owner.password) | ||||
|             # Get user ssh key | ||||
|             if not UserHostingKey.objects.filter(user=self.request.user).exists(): | ||||
|                 context.update({ | ||||
|                     'sshError': 'error', | ||||
|                     'form': form | ||||
|                 }) | ||||
|                 return render(request, self.template_name, context) | ||||
|             # For now just get first one | ||||
|             user_key = UserHostingKey.objects.filter( | ||||
|                 user=self.request.user).first() | ||||
| 
 | ||||
|             # Create a vm using logged user | ||||
|             vm_id = manager.create_vm( | ||||
|                 template_id=vm_template_id, | ||||
|                 # XXX: Confi | ||||
|                 specs=specs, | ||||
|                 ssh_key=user_key.public_key, | ||||
|             ) | ||||
| 
 | ||||
|             # Create a Hosting Order | ||||
|             order = HostingOrder.create( | ||||
|                 price=final_price, | ||||
|                 vm_id=vm_id, | ||||
|                 customer=customer, | ||||
|                 billing_address=billing_address | ||||
|             ) | ||||
| 
 | ||||
|             # Create a Hosting Bill | ||||
|             HostingBill.create( | ||||
|                 customer=customer, billing_address=billing_address) | ||||
| 
 | ||||
|             # Create Billing Address for User if he does not have one | ||||
|             if not customer.user.billing_addresses.count(): | ||||
|                 billing_address_data.update({ | ||||
|                     'user': customer.user.id | ||||
|                 }) | ||||
|                 billing_address_user_form = UserBillingAddressForm( | ||||
|                     billing_address_data) | ||||
|                 billing_address_user_form.is_valid() | ||||
|                 billing_address_user_form.save() | ||||
| 
 | ||||
|             # Associate an order with a stripe payment | ||||
|             order.set_stripe_charge(charge) | ||||
| 
 | ||||
|             # If the Stripe payment was successed, set order status approved | ||||
|             order.set_approved() | ||||
| 
 | ||||
|             vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data | ||||
| 
 | ||||
|             # Send notification to ungleich as soon as VM has been booked | ||||
|             context = { | ||||
|                 'vm': vm, | ||||
|                 'order': order, | ||||
|                 'base_url': "{0}://{1}".format(request.scheme, request.get_host()) | ||||
| 
 | ||||
|             } | ||||
|             email_data = { | ||||
|                 'subject': 'New VM request', | ||||
|                 'to': request.user.email, | ||||
|                 'context': context, | ||||
|                 'template_name': 'new_booked_vm', | ||||
|                 'template_path': 'hosting/emails/' | ||||
|             } | ||||
|             email = BaseEmail(**email_data) | ||||
|             email.send() | ||||
| 
 | ||||
|             return HttpResponseRedirect( | ||||
|                 "{url}?{query_params}".format(url=reverse('hosting:orders', kwargs={'pk': order.id}), | ||||
|                                               query_params='page=payment')) | ||||
|             request.session['billing_address_data'] = billing_address_data | ||||
|             request.session['billing_address'] = billing_address.id | ||||
|             request.session['token'] = token | ||||
|             request.session['customer'] = customer.id | ||||
|             return HttpResponseRedirect(reverse('hosting:orders')) | ||||
|         else: | ||||
|             return self.form_invalid(form) | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue