Fixed membership payment view issues, Membership creation after a stripe charge, Membership order creation after stripe charge, Added membership activated view, Added membership activated html, Fixing membership cost function, Added function to format membership date ranges, Added membership calculated dates for first month into membership_payment html and membership_activated html

This commit is contained in:
Levi 2016-08-22 02:52:29 -05:00
parent fbaf439ebc
commit 1152e41b6e
6 changed files with 212 additions and 29 deletions

View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-08-20 19:59
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', '0006_auto_20160526_0445'),
('digitalglarus', '0007_auto_20160820_0408'),
]
operations = [
migrations.AddField(
model_name='membershiporder',
name='billing_address',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='utils.BillingAddress'),
preserve_default=False,
),
migrations.AddField(
model_name='membershiporder',
name='customer',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='membership.StripeCustomer'),
preserve_default=False,
),
]

View file

@ -1,7 +1,14 @@
import calendar
from datetime import datetime, date, timedelta
from django.db import models from django.db import models
from cms.models import CMSPlugin from cms.models import CMSPlugin
from filer.fields.image import FilerImageField from filer.fields.image import FilerImageField
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.functional import cached_property
from membership.models import StripeCustomer
from utils.models import BillingAddress
class MembershipType(models.Model): class MembershipType(models.Model):
@ -13,25 +20,69 @@ class MembershipType(models.Model):
name = models.CharField(choices=MEMBERSHIP_TYPES, max_length=20) name = models.CharField(choices=MEMBERSHIP_TYPES, max_length=20)
price = models.FloatField() price = models.FloatField()
@cached_property
def days_left(self):
current_date = date.today()
_, days_in_month = calendar.monthrange(current_date.year, current_date.month)
pass_days = current_date.day
days_left = days_in_month - pass_days + 1
return days_left
@cached_property
def first_month_price(self):
current_date = date.today()
_, days_in_month = calendar.monthrange(current_date.year, current_date.month)
pass_days = current_date.day
days_left = days_in_month - pass_days + 1
percentage = days_left / days_in_month
membership_price = self.price
final_price = membership_price * percentage
return final_price
@cached_property
def first_month_range(self):
current_date = date.today()
_, days_in_month = calendar.monthrange(current_date.year, current_date.month)
pass_days = current_date.day
days_left = days_in_month - pass_days
end_date = current_date + timedelta(days=days_left)
return current_date, end_date
@cached_property
def first_month_formated_range(self):
start_date, end_date = self.first_month_range
return "{} - {}".format(datetime.strftime(start_date, "%b, %d %Y"),
datetime.strftime(end_date, "%b, %d %Y"))
class Membership(models.Model): class Membership(models.Model):
type = models.ForeignKey(MembershipType) type = models.ForeignKey(MembershipType)
@classmethod @classmethod
def create(cls, data, user): def create(cls, data):
instance = cls.objects.create(**data) instance = cls.objects.create(**data)
instance.assign_permissions(user)
return instance return instance
class MembershipOrder(models.Model): class MembershipOrder(models.Model):
membership = models.ForeignKey(Membership) membership = models.ForeignKey(Membership)
customer = models.ForeignKey(StripeCustomer)
billing_address = models.ForeignKey(BillingAddress)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
approved = models.BooleanField(default=False) approved = models.BooleanField(default=False)
last4 = models.CharField(max_length=4) last4 = models.CharField(max_length=4)
cc_brand = models.CharField(max_length=10) cc_brand = models.CharField(max_length=10)
stripe_charge_id = models.CharField(max_length=100, null=True) stripe_charge_id = models.CharField(max_length=100, null=True)
@classmethod
def create(cls, data):
stripe_charge = data.pop('stripe_charge', None)
instance = cls.objects.create(**data)
instance.stripe_charge_id = stripe_charge.id
instance.last4 = stripe_charge.source.last4
instance.cc_brand = stripe_charge.source.brand
return instance
class Supporter(models.Model): class Supporter(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
@ -44,9 +95,6 @@ class Supporter(models.Model):
return reverse('dgSupporters_view', args=[self.pk]) return reverse('dgSupporters_view', args=[self.pk])
class DGGallery(models.Model): class DGGallery(models.Model):
parent = models.ForeignKey('self', blank=True, null=True) parent = models.ForeignKey('self', blank=True, null=True)
name = models.CharField(max_length=30) name = models.CharField(max_length=30)

View file

@ -0,0 +1,57 @@
{% extends "new_base_glarus.html" %}
{% load staticfiles cms_tags bootstrap3%}
{% block title %}crowdfunding{% endblock %}
{% block content %}
<section id="price">
<div class="signup-container">
<div class="col-xs-12 col-sm-3 col-lg-4 text-center wow fadeInDown"> </div>
<div class="col-xs-12 col-sm-6 col-lg-4 text-center wow fadeInDown">
<!-- <span class="glyphicon glyphicon-user"></span> -->
<div class="payment-box">
<h2 class="billing-head">Membership Activated</h2>
<hr class="greyline-long">
<h2 class="membership-lead">Your Digital Glarus membership is successfully activated for the following date.</h2>
<div class="date-box">
<h2 class="date-oneline">{{membership_dates}}</h2>
<h2 class="activation-lead">Now you can book your next coworking!</h2>
</div>
<!--<hr class="primary">-->
<div class="signup-form form-group row">
<div class="button-booking-box form-inline row">
<button type="submit" class="btn btn-primary btn-blue">Go to Booking</button>
</div>
<div class="notice-box text-left">
<p class="order-bottom-text">Your membership will be automatically renewed each month. For deactivating go to<a href=#>my page</a></p>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-3 col-lg-4 text-center wow fadeInDown"> </div>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="contact">
<div class="fill">
<div class="row" class="wow fadeInDown">
<div class="col-lg-12 text-center wow fadeInDown">
<div class="col-md-4 map-title">
Digital Glarus<br>
<span class="map-caption">In der Au 7 Schwanden 8762 Switzerland
<br>info@digitalglarus.ch
<br>
(044) 534-66-22
<p>&nbsp;</p>
</span>
</div>
<p>&nbsp;</p>
</div>
</div>
</div>
</section>
{% endblock %}

View file

@ -8,6 +8,16 @@
padding: 0 !important; padding: 0 !important;
margin: 0 !important; margin: 0 !important;
} }
.form-control#id_country{
-webkit-appearance: none;
-moz-appearance: none;
background-position: right 50%;
background-repeat: no-repeat;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAMCAYAAABSgIzaAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNv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ZD0iciI/PuGsgwQAAAA5SURBVHjaYvz//z8DOYCJgUxAf42MQIzTk0D/M+KzkRGPoQSdykiKJrBGpOhgJFYTWNEIiEeAAAMAzNENEOH+do8AAAAASUVORK5CYII=);
padding: .5em;
padding-right: 1.5em
}
</style> </style>
<section id="price"> <section id="price">
@ -23,9 +33,13 @@
Additional day costs Additional day costs
15CHF per day. More than 17 days a month it 15CHF per day. More than 17 days a month it
will charge only 290CHF/month.</h2> will charge only 290CHF/month.</h2>
<h2 class="membership-lead">
You will be charged on the first of the month until you
cancel your subscription. Previous charges won't be refunded.
</h2>
<hr class="greyline-long"> <hr class="greyline-long">
<h2 class="billing-head">Member Name</h2> <h2 class="billing-head">Member Name</h2>
<h2 class="member-name">Nico Schottelius</h2> <h2 class="member-name">{{request.user.name}}</h2>
<hr class="greyline-long"> <hr class="greyline-long">
<h2 class="billing-head">Billing Adress</h2> <h2 class="billing-head">Billing Adress</h2>
<div class="signup-form form-group row"> <div class="signup-form form-group row">
@ -44,20 +58,14 @@
<div class="row"> <div class="row">
<div class="col-xs-9 col-md-12"> <div class="col-xs-9 col-md-12">
<div class="form-group"> <div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="cardName" placeholder="Name on card" required autofocus data-stripe="name" /> <input type="text" class="form-control" name="cardName" placeholder="Name on card" required autofocus data-stripe="name" />
<span class="input-group-addon"><i class="fa fa-user" aria-hidden="true"></i></span>
</div>
</div> </div>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-xs-9 col-md-12"> <div class="col-xs-9 col-md-12">
<div class="form-group"> <div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="cardNumber" placeholder="Valid Card Number" required data-stripe="number" /> <input type="text" class="form-control" name="cardNumber" placeholder="Valid Card Number" required data-stripe="number" />
<span class="input-group-addon"><i class="fa fa-credit-card"></i></span>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -75,7 +83,7 @@
</div> </div>
</div> </div>
</div> </div>
<div class="col-xs-4 col-md-6 pull-right nopadding"> <div class="col-xs-4 col-md-6 pull-right">
<div class="form-group"> <div class="form-group">
<label for="cvCode">CV CODE</label> <label for="cvCode">CV CODE</label>
<input type="password" class="form-control" name="cvCode" placeholder="CV" required data-stripe="cvc" /> <input type="password" class="form-control" name="cvCode" placeholder="CV" required data-stripe="cvc" />
@ -84,7 +92,7 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col-xs-12"> <div class="col-xs-12">
<button class="btn btn-success btn-lg btn-block" type="submit">Purchase membership</button> <button class="btn btn-primary btn-lg btn-blck " type="submit">Purchase membership</button>
</div> </div>
</div> </div>
<div class="row" style="display:none;"> <div class="row" style="display:none;">
@ -115,25 +123,25 @@
<div class="order-box"> <div class="order-box">
<h2 class="col-xs-6 order-item">Digital Glarus Membership</h2> <h2 class="col-xs-6 order-item">Digital Glarus Membership</h2>
<br> <br>
<h2 class="col-xs-6 order-duration">valid 2016.09.08 - 2016.10.08</h2> <h2 class="col-xs-6 order-duration">valid {{membership_type.first_month_formated_range}}</h2>
<br/>
<h2 class="order-person">1 person</h2> <h2 class="order-person">1 person</h2>
<h2 class="col-xs-6 payment-total">Today's Total</h2> <h2 class="col-xs-6 payment-total">Today's Total for {{membership_type.days_left}} days </h2>
<h2 class="order-sum">0.00CHF</h2> <h2 class="order-sum">{{membership_type.first_month_price|floatformat}}CHF</h2>
<hr class="greyline"> <hr class="greyline">
<h2 class="col-xs-6 payment-total">Total</h2> <h2 class="col-xs-6 payment-total">Total</h2>
<h2 class="order-result">35CHF</h2> <h2 class="order-result">{{membership_type.first_month_price|floatformat}}CHF</h2>
<div class="text-center"> <div class="text-center">
<label class="custom-control custom-checkbox"> <label class="custom-control custom-checkbox">
<br/>
<input type="checkbox" class="custom-control-input"> <input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span> <span class="custom-control-indicator"></span>
<span class="custom-control-description">I accept the Digital Glarus <a href=#>Terms and Conditions</a>, <a href=#>Community Guidelines</a> and <a href=#>Privacy Policy</a></span> <span class="custom-control-description">I accept the Digital Glarus <a href=#>Terms and Conditions</a>, <a href=#>Community Guidelines</a> and <a href=#>Privacy Policy</a></span>
</label> </label>
<div class="button-box"> <div class="button-box">
<button type="submit" class="btn btn-primary">Continue to Review</button>
</div> </div>
<div class="button-box"> <div class="button-box">
<p class="order-bottom-text">You can checkout on the next page</p>
</div> </div>
</div> </div>
</div> </div>

View file

@ -3,7 +3,7 @@ from django.utils.translation import ugettext_lazy as _
from . import views from . import views
from .views import ContactView, IndexView, AboutView, HistoryView, LoginView, SignupView,\ from .views import ContactView, IndexView, AboutView, HistoryView, LoginView, SignupView,\
PasswordResetView, PasswordResetConfirmView, MembershipPaymentView PasswordResetView, PasswordResetConfirmView, MembershipPaymentView, MembershipActivatedView
# from membership.views import LoginRegistrationView # from membership.views import LoginRegistrationView
urlpatterns = [ urlpatterns = [
@ -15,7 +15,9 @@ urlpatterns = [
url(r'reset-password-confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', url(r'reset-password-confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
PasswordResetConfirmView.as_view(), name='reset_password_confirm'), PasswordResetConfirmView.as_view(), name='reset_password_confirm'),
url(_(r'history/?$'), HistoryView.as_view(), name='history'), url(_(r'history/?$'), HistoryView.as_view(), name='history'),
url(_(r'membership/payment?$'), MembershipPaymentView.as_view(), name='membership_payment'), url(_(r'membership/payment/?$'), MembershipPaymentView.as_view(), name='membership_payment'),
url(_(r'membership/activated/?$'), MembershipActivatedView.as_view(),
name='membership_activated'),
url(_(r'supporters/?$'), views.supporters, name='supporters'), url(_(r'supporters/?$'), views.supporters, name='supporters'),
url(r'calendar_api/(?P<month>\d+)/(?P<year>\d+)?$', views.CalendarApi.as_view(),name='calendar_api_1'), url(r'calendar_api/(?P<month>\d+)/(?P<year>\d+)?$', views.CalendarApi.as_view(),name='calendar_api_1'),
url(r'calendar_api/', views.CalendarApi.as_view(),name='calendar_api'), url(r'calendar_api/', views.CalendarApi.as_view(),name='calendar_api'),

View file

@ -13,7 +13,7 @@ from django.utils.translation import get_language
from djangocms_blog.models import Post from djangocms_blog.models import Post
from django.contrib import messages from django.contrib import messages
from django.http import JsonResponse from django.http import JsonResponse
from django.views.generic import View from django.views.generic import View, DetailView
from .models import Supporter from .models import Supporter
from utils.forms import ContactUsForm from utils.forms import ContactUsForm
@ -29,7 +29,7 @@ from utils.stripe_utils import StripeUtils
from .forms import LoginForm, SignupForm, MembershipBillingForm from .forms import LoginForm, SignupForm, MembershipBillingForm
from .models import MembershipType from .models import MembershipType, Membership, MembershipOrder
class IndexView(TemplateView): class IndexView(TemplateView):
@ -78,22 +78,24 @@ class MembershipPaymentView(LoginRequiredMixin, FormView):
form_class = MembershipBillingForm form_class = MembershipBillingForm
def get_form_kwargs(self): def get_form_kwargs(self):
membership_type = MembershipType.objects.get(name='standard') self.membership_type = MembershipType.objects.get(name='standard')
form_kwargs = super(MembershipPaymentView, self).get_form_kwargs() form_kwargs = super(MembershipPaymentView, self).get_form_kwargs()
form_kwargs.update({ form_kwargs.update({
'initial': {'membership_type': membership_type.id} 'initial': {
'membership_type': self.membership_type.id
}
}) })
return form_kwargs return form_kwargs
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(MembershipPaymentView, self).get_context_data(**kwargs) context = super(MembershipPaymentView, self).get_context_data(**kwargs)
context.update({ context.update({
'stripe_key': settings.STRIPE_API_PUBLIC_KEY 'stripe_key': settings.STRIPE_API_PUBLIC_KEY,
'membership_type': self.membership_type
}) })
return context return context
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
import pdb;pdb.set_trace()
form = self.get_form() form = self.get_form()
if form.is_valid(): if form.is_valid():
@ -111,7 +113,7 @@ class MembershipPaymentView(LoginRequiredMixin, FormView):
# Make stripe charge to a customer # Make stripe charge to a customer
stripe_utils = StripeUtils() stripe_utils = StripeUtils()
charge_response = stripe_utils.make_charge(amount=membership_type.price, charge_response = stripe_utils.make_charge(amount=membership_type.first_month_price,
customer=customer.stripe_id) customer=customer.stripe_id)
charge = charge_response.get('response_object') charge = charge_response.get('response_object')
@ -124,10 +126,46 @@ class MembershipPaymentView(LoginRequiredMixin, FormView):
return render(request, self.template_name, context) return render(request, self.template_name, context)
charge = charge_response.get('response_object') charge = charge_response.get('response_object')
# Create Billing Address
billing_address = form.save()
# Create membership plan
membership_data = {'type': membership_type}
membership = Membership.create(membership_data)
# Create membership order
order_data = {
'membership': membership,
'customer': customer,
'billing_address': billing_address,
'stripe_charge': charge
}
MembershipOrder.create(order_data)
request.session.update({
'membership_price': membership.type.first_month_price,
'membership_dates': membership.type.first_month_formated_range
})
return HttpResponseRedirect(reverse('digitalglarus:membership_activated'))
else: else:
return self.form_invalid(form) return self.form_invalid(form)
class MembershipActivatedView(TemplateView):
template_name = "digitalglarus/membership_activated.html"
def get_context_data(self, **kwargs):
context = super(MembershipActivatedView, self).get_context_data(**kwargs)
membership_price = self.request.session.get('membership_price')
membership_dates = self.request.session.get('membership_dates')
context.update({
'membership_price': membership_price,
'membership_dates': membership_dates,
})
return context
############## OLD VIEWS ############## OLD VIEWS
class CalendarApi(View): class CalendarApi(View):