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
					
						
							
								981be2dcf4
							
						
					
				
			
			
				commit
				
					
						fdf8722c8f
					
				
			
		
					 17 changed files with 281 additions and 42 deletions
				
			
		| 
						 | 
				
			
			@ -1,4 +1,5 @@
 | 
			
		|||
from django.utils.translation import ugettext as _
 | 
			
		||||
from django.db import models
 | 
			
		||||
from django import forms
 | 
			
		||||
 | 
			
		||||
# http://xml.coverpages.org/country3166.html
 | 
			
		||||
| 
						 | 
				
			
			@ -245,10 +246,11 @@ COUNTRIES = (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CountryField(forms.ChoiceField):
 | 
			
		||||
class CountryField(models.CharField):
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        kwargs.setdefault('choices', COUNTRIES)
 | 
			
		||||
        kwargs.setdefault('initial', 'CH')
 | 
			
		||||
        kwargs.setdefault('default', 'CH')
 | 
			
		||||
        kwargs.setdefault('max_length', 2)
 | 
			
		||||
 | 
			
		||||
        super(CountryField, self).__init__(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,19 +1,17 @@
 | 
			
		|||
from django import forms
 | 
			
		||||
from .models import ContactMessage
 | 
			
		||||
from .models import ContactMessage, BillingAddress
 | 
			
		||||
from django.template.loader import render_to_string
 | 
			
		||||
from django.core.mail import EmailMultiAlternatives
 | 
			
		||||
from django.utils.translation import ugettext_lazy as _
 | 
			
		||||
from utils.fields import CountryField
 | 
			
		||||
# from utils.fields import CountryField
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BillingAddressForm(forms.Form):
 | 
			
		||||
    street_address = forms.CharField()
 | 
			
		||||
    city = forms.CharField()
 | 
			
		||||
    postal_code = forms.CharField()
 | 
			
		||||
    country = CountryField()
 | 
			
		||||
class BillingAddressForm(forms.ModelForm):
 | 
			
		||||
    token = forms.CharField(widget=forms.HiddenInput())
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = BillingAddress
 | 
			
		||||
        fields = ['street_address', 'city', 'postal_code', 'country']
 | 
			
		||||
        labels = {
 | 
			
		||||
            'street_address': _('Street Address'),
 | 
			
		||||
            'city': _('City'),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										26
									
								
								utils/migrations/0002_billingaddress.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								utils/migrations/0002_billingaddress.py
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
						 | 
				
			
			@ -1,8 +1,18 @@
 | 
			
		|||
from django.db import models
 | 
			
		||||
 | 
			
		||||
from .fields import CountryField
 | 
			
		||||
 | 
			
		||||
# Create your models here.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BillingAddress(models.Model):
 | 
			
		||||
    street_address = models.CharField(max_length=100)
 | 
			
		||||
    city = models.CharField(max_length=50)
 | 
			
		||||
    postal_code = models.CharField(max_length=50)
 | 
			
		||||
    country = CountryField()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ContactMessage(models.Model):
 | 
			
		||||
    name = models.CharField(max_length=200)
 | 
			
		||||
    email = models.EmailField()
 | 
			
		||||
| 
						 | 
				
			
			@ -11,4 +21,4 @@ class ContactMessage(models.Model):
 | 
			
		|||
    received_date = models.DateTimeField(auto_now_add=True)
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return "%s - %s - %s" % (self.name, self.email, self.received_date)
 | 
			
		||||
        return "%s - %s - %s" % (self.name, self.email, self.received_date)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,6 +12,25 @@ class StripeUtils(object):
 | 
			
		|||
        self.stripe = stripe
 | 
			
		||||
        self.stripe.api_key = settings.STRIPE_API_PRIVATE_KEY
 | 
			
		||||
 | 
			
		||||
    def create_customer(self, token, email):
 | 
			
		||||
        stripe.api_key = settings.STRIPE_API_PRIVATE_KEY
 | 
			
		||||
        customer = stripe.Customer.create(
 | 
			
		||||
              source=token,
 | 
			
		||||
              description='description for testing',
 | 
			
		||||
              email=email
 | 
			
		||||
        )
 | 
			
		||||
        return customer
 | 
			
		||||
 | 
			
		||||
    def make_charge(self, amount=None, customer=None):
 | 
			
		||||
        amount = int(amount * 100)  # stripe amount unit, in cents
 | 
			
		||||
 | 
			
		||||
        charge = self.stripe.Charge.create(
 | 
			
		||||
          amount=amount,  # in cents
 | 
			
		||||
          currency=self.CURRENCY,
 | 
			
		||||
          customer=customer
 | 
			
		||||
        )
 | 
			
		||||
        return charge
 | 
			
		||||
 | 
			
		||||
    def create_plan(self, amount, name, id):
 | 
			
		||||
        self.stripe.Plan.create(
 | 
			
		||||
          amount=amount,
 | 
			
		||||
| 
						 | 
				
			
			@ -20,7 +39,7 @@ class StripeUtils(object):
 | 
			
		|||
          currency=self.CURRENCY,
 | 
			
		||||
          id=id)
 | 
			
		||||
 | 
			
		||||
    def make_payment(self, user, amount, token, time):
 | 
			
		||||
    def make_payment(self, user, amount, token):
 | 
			
		||||
        try:
 | 
			
		||||
            # Use Stripe's library to make requests...
 | 
			
		||||
            charge = self.stripe.Charge.create(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue