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,30 +20,50 @@
 | 
				
			||||||
    <div class="row">
 | 
					    <div class="row">
 | 
				
			||||||
        <div class="col-xs-12 col-md-8 col-md-offset-2">
 | 
					        <div class="col-xs-12 col-md-8 col-md-offset-2">
 | 
				
			||||||
    		<div class="invoice-title">
 | 
					    		<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>
 | 
					    		</div>
 | 
				
			||||||
    		<hr>
 | 
					    		<hr>
 | 
				
			||||||
    		<div class="row">
 | 
					    		<div class="row">
 | 
				
			||||||
				<div class="col-xs-12 col-md-6 pull-right order-confirm-date">
 | 
									<div class="col-xs-12 col-md-6 pull-right order-confirm-date">
 | 
				
			||||||
                    <address>
 | 
					                    <address>
 | 
				
			||||||
                        <strong>{% trans "Date"%}:</strong><br>
 | 
					                        <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>
 | 
					                        <strong>{% trans "Status:"%}</strong><br>
 | 
				
			||||||
                        {% if order.status == 'Approved' %}
 | 
					                            {% if order.status == 'Approved' %}
 | 
				
			||||||
                            <strong class="text-success">{% trans "Approved" %}</strong>
 | 
					                                <strong class="text-success">{% trans "Approved" %}</strong>
 | 
				
			||||||
                        {% else %}
 | 
					                            {% else %}
 | 
				
			||||||
                            <strong class="text-danger">{% trans "Declined" %}</strong>
 | 
					                                <strong class="text-danger">{% trans "Declined" %}</strong>
 | 
				
			||||||
                        {% endif %}
 | 
					                            {% endif %}
 | 
				
			||||||
                        <br><br>
 | 
					                        <br><br>
 | 
				
			||||||
 | 
					                        {% endif %}
 | 
				
			||||||
                    </address>
 | 
					                    </address>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                </div>
 | 
					                </div>
 | 
				
			||||||
    			<div class="col-xs-12 col-md-6">
 | 
					    			<div class="col-xs-12 col-md-6">
 | 
				
			||||||
    				<address>
 | 
					    				<address>
 | 
				
			||||||
                    <h3><b>{% trans "Billed To:"%}</b></h3>
 | 
					                    <h3><b>{% trans "Billed To:"%}</b></h3>
 | 
				
			||||||
 | 
					                        {% if order %}
 | 
				
			||||||
    					{{user.name}}<br>
 | 
					    					{{user.name}}<br>
 | 
				
			||||||
                        {{order.billing_address.street_address}},{{order.billing_address.postal_code}}<br>
 | 
					                        {{order.billing_address.street_address}},{{order.billing_address.postal_code}}<br>
 | 
				
			||||||
                        {{order.billing_address.city}}, {{order.billing_address.country}}.
 | 
					                        {{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>
 | 
					    				</address>
 | 
				
			||||||
    			</div>
 | 
					    			</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -52,8 +72,13 @@
 | 
				
			||||||
    			<div class="col-xs-6">
 | 
					    			<div class="col-xs-6">
 | 
				
			||||||
    				<address>
 | 
					    				<address>
 | 
				
			||||||
    					<strong>{% trans "Payment Method:"%}</strong><br>
 | 
					    					<strong>{% trans "Payment Method:"%}</strong><br>
 | 
				
			||||||
 | 
					                        {% if order %}
 | 
				
			||||||
    					{{order.cc_brand}} {% trans "ending in" %} **** {{order.last4}}<br>
 | 
					    					{{order.cc_brand}} {% trans "ending in" %} **** {{order.last4}}<br>
 | 
				
			||||||
    					{{user.email}}
 | 
					    					{{user.email}}
 | 
				
			||||||
 | 
					                        {% else %}
 | 
				
			||||||
 | 
					                                    {{cc_brand}} {% trans "ending" %} **** {{cc_last4}}<br>
 | 
				
			||||||
 | 
					                                    {{request.session.user.email}}
 | 
				
			||||||
 | 
					                        {% endif %}
 | 
				
			||||||
    				</address>
 | 
					    				</address>
 | 
				
			||||||
    			</div>
 | 
					    			</div>
 | 
				
			||||||
    		</div>
 | 
					    		</div>
 | 
				
			||||||
| 
						 | 
					@ -65,20 +90,48 @@
 | 
				
			||||||
            <h3><b>{% trans "Order summary"%}</b></h3>
 | 
					            <h3><b>{% trans "Order summary"%}</b></h3>
 | 
				
			||||||
            <hr>
 | 
					            <hr>
 | 
				
			||||||
            <div class="content">
 | 
					            <div class="content">
 | 
				
			||||||
                <p><b>{% trans "Cores"%}</b> <span class="pull-right">{{vm.cores}}</span></p>
 | 
					                {% if request.session.specs %}
 | 
				
			||||||
                <hr>
 | 
					                    {% with request.session.specs as vm %}
 | 
				
			||||||
                <p><b>{% trans "Memory"%}</b> <span class="pull-right">{{vm.memory}} GB</span></p>
 | 
					                        <p><b>{% trans "Cores"%}</b> <span class="pull-right">{{vm.cpu}}</span></p>
 | 
				
			||||||
                <hr>
 | 
					                        <hr>
 | 
				
			||||||
                <p><b>{% trans "Disk space"%}</b> <span class="pull-right">{{vm.disk_size}} GB</span></p>
 | 
					                        <p><b>{% trans "Memory"%}</b> <span class="pull-right">{{vm.memory}} GB</span></p>
 | 
				
			||||||
                <hr>
 | 
					                        <hr>
 | 
				
			||||||
                                <h4>{% trans "Total"%}<p class="pull-right"><b>{{vm.price}} CHF</b></p></h4>
 | 
					                        <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>
 | 
				
			||||||
 | 
					                    <hr>
 | 
				
			||||||
 | 
					                    <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>
 | 
					            </div>
 | 
				
			||||||
            <br/>
 | 
					            <br/>
 | 
				
			||||||
            {% url 'hosting:payment' as payment_url %}
 | 
					            {% if not order %}
 | 
				
			||||||
            {% if payment_url in request.META.HTTP_REFERER  %}
 | 
					            <form method="post">
 | 
				
			||||||
            <div class=" content pull-right">
 | 
					                {% csrf_token %}
 | 
				
			||||||
                <a href="{% url 'hosting:virtual_machines'%}" ><button class="btn btn-info">{% trans "Finish Configuration"%}</button></a>
 | 
					                <div class="row">
 | 
				
			||||||
            </div>
 | 
					                    <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 %}
 | 
					            {% endif %}
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -569,100 +569,17 @@ class PaymentVMView(LoginRequiredMixin, FormView):
 | 
				
			||||||
            customer = StripeCustomer.get_or_create(email=owner.email,
 | 
					            customer = StripeCustomer.get_or_create(email=owner.email,
 | 
				
			||||||
                                                    token=token)
 | 
					                                                    token=token)
 | 
				
			||||||
            if not customer:
 | 
					            if not customer:
 | 
				
			||||||
                msg = _("Invalid credit card")
 | 
					                form.add_error("__all__", _("Invalid credit card"))
 | 
				
			||||||
                messages.add_message(
 | 
					                return self.render_to_response(
 | 
				
			||||||
                    self.request, messages.ERROR, msg, extra_tags='make_charge_error')
 | 
					                    self.get_context_data(form=form))
 | 
				
			||||||
                return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Create Billing Address
 | 
					            # Create Billing Address
 | 
				
			||||||
            billing_address = form.save()
 | 
					            billing_address = form.save()
 | 
				
			||||||
 | 
					            request.session['billing_address_data'] = billing_address_data
 | 
				
			||||||
            # Make stripe charge to a customer
 | 
					            request.session['billing_address'] = billing_address.id
 | 
				
			||||||
            stripe_utils = StripeUtils()
 | 
					            request.session['token'] = token
 | 
				
			||||||
            charge_response = stripe_utils.make_charge(amount=final_price,
 | 
					            request.session['customer'] = customer.id
 | 
				
			||||||
                                                       customer=customer.stripe_id)
 | 
					            return HttpResponseRedirect(reverse('hosting:orders'))
 | 
				
			||||||
 | 
					 | 
				
			||||||
            # 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'))
 | 
					 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            return self.form_invalid(form)
 | 
					            return self.form_invalid(form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue