Added Hosting Order model, Created Billing Address Model , Method to create a customer using Stripe API , Created Customer Stripe profile to store for further charges , Method in order to charge an amount to a customer
This commit is contained in:
parent
f5978a7da9
commit
bf334a38d4
17 changed files with 281 additions and 42 deletions
44
hosting/migrations/0009_auto_20160426_0444.py
Normal file
44
hosting/migrations/0009_auto_20160426_0444.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.4 on 2016-04-26 04:44
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('utils', '0002_billingaddress'),
|
||||
('membership', '0004_stripecustomer'),
|
||||
('hosting', '0008_virtualmachineplan'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='HostingOrder',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
],
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='virtualmachineplan',
|
||||
name='client',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='hostingorder',
|
||||
name='VMPlan',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='hosting.VirtualMachinePlan'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='hostingorder',
|
||||
name='billing_address',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='utils.BillingAddress'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='hostingorder',
|
||||
name='customer',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='membership.StripeCustomer'),
|
||||
),
|
||||
]
|
||||
25
hosting/migrations/0010_auto_20160426_0530.py
Normal file
25
hosting/migrations/0010_auto_20160426_0530.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.4 on 2016-04-26 05:30
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('hosting', '0009_auto_20160426_0444'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='hostingorder',
|
||||
name='approved',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='hostingorder',
|
||||
name='stripe_charge_id',
|
||||
field=models.CharField(max_length=100, null=True),
|
||||
),
|
||||
]
|
||||
21
hosting/migrations/0011_auto_20160426_0555.py
Normal file
21
hosting/migrations/0011_auto_20160426_0555.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.4 on 2016-04-26 05:55
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('hosting', '0010_auto_20160426_0530'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='hostingorder',
|
||||
name='VMPlan',
|
||||
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='hosting.VirtualMachinePlan'),
|
||||
),
|
||||
]
|
||||
|
|
@ -3,7 +3,8 @@ import json
|
|||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core import serializers
|
||||
from membership.models import CustomUser
|
||||
from membership.models import StripeCustomer
|
||||
from utils.models import BillingAddress
|
||||
|
||||
|
||||
class RailsBetaUser(models.Model):
|
||||
|
|
@ -78,13 +79,36 @@ class VirtualMachinePlan(models.Model):
|
|||
memory = models.IntegerField()
|
||||
disk_size = models.IntegerField()
|
||||
vm_type = models.ForeignKey(VirtualMachineType)
|
||||
client = models.ManyToManyField(CustomUser)
|
||||
price = models.FloatField()
|
||||
|
||||
@classmethod
|
||||
def create(cls, data, user):
|
||||
instance = cls.objects.create(**data)
|
||||
instance.client.add(user)
|
||||
return instance
|
||||
|
||||
|
||||
class HostingOrder(models.Model):
|
||||
VMPlan = models.OneToOneField(VirtualMachinePlan)
|
||||
customer = models.ForeignKey(StripeCustomer)
|
||||
billing_address = models.ForeignKey(BillingAddress)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
approved = models.BooleanField(default=False)
|
||||
stripe_charge_id = models.CharField(max_length=100, null=True)
|
||||
|
||||
@classmethod
|
||||
def create(cls, VMPlan=None, customer=None, billing_address=None):
|
||||
instance = cls.objects.create(VMPlan=VMPlan, customer=customer,
|
||||
billing_address=billing_address)
|
||||
return instance
|
||||
|
||||
def set_approved(self):
|
||||
self.approved = True
|
||||
self.save()
|
||||
|
||||
def set_stripe_charge(self, stripe_charge):
|
||||
self.stripe_charge_id = stripe_charge.id
|
||||
self.save()
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ $( document ).ready(function() {
|
|||
/* Visual feedback */
|
||||
$form.find('[type=submit]').html('Validating <i class="fa fa-spinner fa-pulse"></i>');
|
||||
|
||||
var PublishableKey = 'pk_test_6pRNASCoBOKtIshFeQd4XMUh'; // Replace with your API publishable key
|
||||
var PublishableKey = window.stripeKey;
|
||||
Stripe.setPublishableKey(PublishableKey);
|
||||
Stripe.card.createToken($form, function stripeResponseHandler(status, response) {
|
||||
if (response.error) {
|
||||
|
|
@ -52,27 +52,11 @@ $( document ).ready(function() {
|
|||
$form.find('.payment-errors').text("");
|
||||
// response contains id and card, which contains additional card details
|
||||
var token = response.id;
|
||||
console.log(token);
|
||||
// AJAX
|
||||
|
||||
//set token on a hidden input
|
||||
$('#id_token').val(token);
|
||||
$('#billing-form').submit();
|
||||
|
||||
// $.post('/hosting/payment/', {
|
||||
// token: token,
|
||||
// })
|
||||
// // Assign handlers immediately after making the request,
|
||||
// .done(function(data, textStatus, jqXHR) {
|
||||
|
||||
// $form.find('[type=submit]').html('Payment successful <i class="fa fa-check"></i>').prop('disabled', true);
|
||||
// })
|
||||
// .fail(function(jqXHR, textStatus, errorThrown) {
|
||||
// $form.find('[type=submit]').html('There was a problem').removeClass('success').addClass('error');
|
||||
// /* Show Stripe errors on the form */
|
||||
// $form.find('.payment-errors').text('Try refreshing the page and trying again.');
|
||||
// $form.find('.payment-errors').closest('.row').show();
|
||||
// });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,8 +96,15 @@
|
|||
</div>
|
||||
|
||||
</div>
|
||||
<!-- stripe key data -->
|
||||
{% if stripe_key %}
|
||||
<script type="text/javascript">
|
||||
(function () {window.stripeKey = "{{stripe_key}}";})();
|
||||
</script>
|
||||
{%endif%}
|
||||
|
||||
{%endblock%}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -9,13 +9,15 @@ from django.contrib.auth import authenticate, login
|
|||
from django.conf import settings
|
||||
|
||||
from membership.forms import PaymentForm
|
||||
from membership.models import CustomUser
|
||||
from membership.models import CustomUser, StripeCustomer
|
||||
from utils.stripe_utils import StripeUtils
|
||||
from utils.forms import BillingAddressForm
|
||||
from .models import VirtualMachineType, VirtualMachinePlan
|
||||
from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder
|
||||
from .forms import HostingUserSignupForm, HostingUserLoginForm
|
||||
from .mixins import ProcessVMSelectionMixin
|
||||
|
||||
|
||||
|
||||
class DjangoHostingView(ProcessVMSelectionMixin, View):
|
||||
template_name = "hosting/django.html"
|
||||
|
||||
|
|
@ -157,21 +159,46 @@ class PaymentVMView(FormView):
|
|||
specifications = request.session.get('vm_specs')
|
||||
vm_type = specifications.get('hosting_company')
|
||||
vm = VirtualMachineType.objects.get(hosting_company=vm_type)
|
||||
final_price = vm.calculate_price(specifications)
|
||||
|
||||
plan_data = {
|
||||
'vm_type': vm,
|
||||
'cores': specifications.get('cores'),
|
||||
'memory': specifications.get('memory'),
|
||||
'disk_size': specifications.get('disk_size'),
|
||||
'price': vm.calculate_price(specifications)
|
||||
'price': final_price
|
||||
}
|
||||
token = form.cleaned_data.get('token')
|
||||
|
||||
# Stripe payment goes here
|
||||
# Get or create stripe customer
|
||||
customer = StripeCustomer.get_or_create(email=self.request.user.email,
|
||||
token=token)
|
||||
# Create Virtual Machine Plan
|
||||
plan = VirtualMachinePlan.create(plan_data, request.user)
|
||||
|
||||
# Create Billing Address
|
||||
billing_address = form.save()
|
||||
|
||||
# Create a Hosting Order
|
||||
order = HostingOrder.create(VMPlan=plan, customer=customer,
|
||||
billing_address=billing_address)
|
||||
|
||||
# Make stripe charge to a customer
|
||||
stripe_utils = StripeUtils()
|
||||
charge = stripe_utils.make_charge(amount=final_price,
|
||||
customer=customer.stripe_id)
|
||||
order.set_stripe_charge(charge)
|
||||
|
||||
if not charge.paid:
|
||||
# raise an error
|
||||
pass
|
||||
|
||||
# If the Stripe payment was successed, set order status approved
|
||||
order.set_approved()
|
||||
# order.charge =
|
||||
|
||||
# Billing Address should be store here
|
||||
|
||||
VirtualMachinePlan.create(plan_data, request.user)
|
||||
|
||||
return HttpResponseRedirect(reverse('hosting:payment'))
|
||||
else:
|
||||
return self.form_invalid(form)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue