update dg payment

This commit is contained in:
Tomislav R 2016-05-01 14:13:12 +02:00
parent e0fc542673
commit 45730e4eb1
10 changed files with 69 additions and 60 deletions

View file

@ -28,7 +28,7 @@
rel="stylesheet" type="text/css">
<link href="//fonts.googleapis.com/css?family=Josefin+Slab:100,300,400,600,700,100italic,300italic,400italic,600italic,700italic"
rel="stylesheet" type="text/css">
<link href="{% static 'digitalglarus/css/font-awesome.min.css' %}"
<link href="{% static 'digitalglarus/bower_components/font-awesome/css/font-awesome.min.css' %}"
rel="stylesheet" type="text/css">
{% render_block "css" postprocessor "compressor.contrib.sekizai.compress" %}
@ -65,7 +65,7 @@
<div class="pull-right u_P user">
{% if request.user.is_authenticated %}
<div class="user_loggedin">
<h4>{{ request.user.name }}
<h4><a href="{% url 'membership' %}">{{ request.user.name }}</a>
<a href="{% url 'logout_glarus' %}"><i class="fa fa-1x fa-sign-out"
aria-hidden="true"></i></a>
</h4>

View file

@ -1,17 +1,17 @@
from django.conf.urls import url
from django.utils.translation import ugettext_lazy as _
from . import views
from .views import ContactView, IndexView, AboutView
urlpatterns = [
# url(r'^$', IndexView.as_view(), name='home'),
url(_(r'home/?$'), IndexView.as_view(), name='home'),
url(_(r'about/?$'), AboutView.as_view(), name='about'),
# url(r'^$', IndexView.as_view(), name='home'),
# url(_(r'home/?$'), IndexView.as_view(), name='home'),
# url(_(r'about/?$'), AboutView.as_view(), name='about'),
url(_(r'contact/?$'), ContactView.as_view(), name='contact'),
url(_(r'supporters/?$'), views.supporters, name='supporters'),
url(r'calendar_api/(?P<month>\d+)/(?P<year>\d+)?$',views.CalendarApi.as_view()),
url(r'calendar_api/',views.CalendarApi.as_view()),
url(r'calendar_api/(?P<month>\d+)/(?P<year>\d+)?$', views.CalendarApi.as_view()),
url(r'calendar_api/', views.CalendarApi.as_view()),
url(_(r'support-us/?$'), views.support, name='support'),
url(r'^blog/(?P<slug>\w[-\w]*)/$', views.blog_detail, name='blog-detail'),
url(r'blog/$', views.blog, name='blog'),

View file

@ -446,8 +446,8 @@ AUTH_USER_MODEL = 'membership.CustomUser'
# PAYMENT
STRIPE_API_PUBLIC_KEY = 'pk_test_QqBZ50Am8KOxaAlOxbcm9Psl' # used in frontend to call from user browser
STRIPE_API_PRIVATE_KEY = 'sk_test_dqAmbKAij12QCGfkYZ3poGt2' # used in backend payment
STRIPE_API_PUBLIC_KEY = 'pk_test_ZRg6P8g5ybiHE6l2RW5pSaYV' # used in frontend to call from user browser
STRIPE_API_PRIVATE_KEY = 'sk_test_uIPMdgXoRGydrcD7fkwcn7dj' # used in backend payment
STRIPE_DESCRIPTION_ON_PAYMENT = "Payment for ungleich GmbH services"
# EMAIL MESSAGES

View file

@ -23,13 +23,13 @@ urlpatterns += i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^digitalglarus/login/', include(membership_urls)),
url(r'^digitalglarus/', include('digitalglarus.urls',
namespace="digitalglarus"),
namespace="digitalglarus")),
url(r'^blog/', include('ungleich.urls', namespace='ungleich')),
url(r'^ungleich_page/',
include('ungleich_page.urls', namespace='ungleich_page'),
name='ungleich_page'),
url(r'^blog/',include('ungleich.urls',namespace='ungleich')),
url(r'^', include('cms.urls')),
url(r'^', include('cms.urls'))
)
if settings.DEBUG:

View file

@ -4,7 +4,7 @@ from .views import DjangoHostingView, RailsHostingView, PaymentVMView, \
NodeJSHostingView, LoginView, SignupView, IndexView
urlpatterns = [
url(r'pricing/?$', VMPricingView.as_view(), name='pricing'),
# url(r'pricing/?$', VMPricingView.as_view(), name='pricing'),
url(r'index/?$', IndexView.as_view(), name='index'),
url(r'django/?$', DjangoHostingView.as_view(), name='djangohosting'),
url(r'nodejs/?$', NodeJSHostingView.as_view(), name='nodejshosting'),

View file

@ -8,6 +8,7 @@ from django.core.mail import send_mail
from django.core.validators import RegexValidator
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from utils.stripe_utils import StripeUtils
REGISTRATION_MESSAGE = {'subject': "Validation mail",
@ -132,8 +133,12 @@ class StripeCustomer(models.Model):
Check if there is a registered stripe customer with that email
or create a new one
"""
try:
stripe_utils = StripeUtils()
stripe_customer = cls.objects.get(user__email=email)
#check if user is not in stripe but in database
stripe_utils.check_customer(stripe_customer.stripe_id,stripe_customer.user,token)
return stripe_customer
except StripeCustomer.DoesNotExist:
@ -142,7 +147,7 @@ class StripeCustomer(models.Model):
stripe_utils = StripeUtils()
stripe_data = stripe_utils.create_customer(token, email)
stripe_customer = StripeCustomer.objects.\
stripe_customer = StripeCustomer.objects. \
create(user=user, stripe_id=stripe_data.get('id'))
return stripe_customer
@ -157,6 +162,10 @@ class CreditCards(models.Model):
ccv = models.CharField(max_length=4, validators=[RegexValidator(r'\d{3,4}', _('Wrong CCV number.'))])
payment_type = models.CharField(max_length=5, default='N')
def save(self, *args, **kwargs):
# override saving to database
pass
class Calendar(models.Model):
datebooked = models.DateField()
@ -170,10 +179,9 @@ class Calendar(models.Model):
super(Calendar, self).__init__(*args, **kwargs)
@classmethod
def add_dates(cls,dates,user):
def add_dates(cls, dates, user):
old_dates = Calendar.objects.filter(user_id=user.id)
if old_dates:
old_dates.delete()
for date in dates:
Calendar.objects.create(datebooked=date,user=user)
Calendar.objects.create(datebooked=date, user=user)

View file

@ -125,7 +125,7 @@
for coworking. It is a perfect try-out package!
<br>
</p>
<a class="btn btn-primary" href="/login/buy/month/">Buy Now</a>
<a class="btn btn-primary" href="{% url 'payment' time='month' %}">Buy Now</a>
</div>
</div>
</div>
@ -146,7 +146,7 @@
membership.
<br>
</p>
<a class="btn btn-primary" href="/login/buy/year/">Buy Now</a>
<a class="btn btn-primary" href="{% url 'payment' time='year' %}">Buy Now</a>
</div>
<div class="col-md-6">
<img src="https://unsplash.imgix.net/photo-1413834932717-29e7d4714192?w=1024&amp;q=50&amp;fm=jpg&amp;s=d9d6cbc32dd4968d0c21e331436a6af6"

View file

@ -1,17 +1,18 @@
{% load static %}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="{% static 'digitalglarus/bower_components/jquery/dist/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'digitalglarus/bower_components/bootstrap/dist/js/bootstrap.min.js' %}"></script>
<link href="{% static 'digitalglarus/css/font-awesome.min.css' %}" rel="stylesheet" type="text/css">
<link href="{% static 'digitalglarus/bower_components/font-awesome/css/font-awesome.min.css' %}" rel="stylesheet" type="text/css">
<link href="{% static 'digitalglarus/bower_components/bootstrap/dist/css/bootstrap.min.css' %}" rel="stylesheet"
type="text/css">
{% if request.session.next == None %}
<script>
type = "text/javascript" >
setTimeout(function () {
window.location.href = "/";
window.location.href = "{% url 'membership' %}";
}, 5000);
</script>
{% endif %}
@ -25,7 +26,7 @@
{% if msg == 'succeeded' %}
Thank You for Your payment!<br> <p>redirecting...</p>
{% else %}
Your payment was not processed correctly.<br> Reason: {{ msg }}
Your payment was not processed correctly. Please contact us <a href="{% url 'digitalglarus:contact' %}">here</a>!
{% endif %}
</h3>
</div>

View file

@ -8,9 +8,9 @@ from django.contrib.auth import logout
from django.http import HttpResponseRedirect
from django.contrib.auth import login
from .models import CustomUser
from .models import CustomUser,StripeCustomer
from .forms import (LoginForm, RegisterForm, PaymentForm)
from .payment import StripePayment
from utils.stripe_utils import StripeUtils
def validate_email(request, validate_slug):
@ -49,10 +49,12 @@ class CreditCardView(View):
template = 'templates/creditcard.html'
request.session['next'] +=1
elif next == 2:
msg = StripePayment.make_payment(request.user, request.session['amount'],
request.session['token'],request.session['time'])
customer = StripeCustomer.get_or_create(email=request.user.email,token=request.session['token'])
stripe_utils = StripeUtils()
charge = stripe_utils.make_charge(request.session['amount'],customer=customer.stripe_id)
template = 'templates/validated.html'
context['msg'] = msg
import ipdb;ipdb.set_trace()
context['msg'] = charge.get('status')
request.session['next'] = None
return render(request, template, context)
@ -63,7 +65,6 @@ class CreditCardView(View):
if form.is_valid():
ret = form.save(request.user)
amount = 35 if time == 'month' else 360
amount = amount * 100 # payments are in 'cents'
request.session['token'] = stripe_token
request.session['amount'] = amount
request.session['next'] +=1

View file

@ -1,58 +1,49 @@
import stripe
from django.conf import settings
from django.db import models
stripe.api_key = settings.STRIPE_API_PRIVATE_KEY
class StripeUtils(object):
CURRENCY = 'chf'
INTERVAL = 'month'
SUCCEEDED_STATUS = 'succeeded'
def __init__(self):
self.stripe = stripe
self.stripe.api_key = settings.STRIPE_API_PRIVATE_KEY
def check_customer(self, id,user,token):
customers = self.stripe.Customer.all()
if not customers.get('data'):
customer = self.create_customer(token,user.email)
else:
try:
customer = stripe.Customer.retrieve(id)
except stripe.InvalidRequestError:
customer = self.create_customer(token,user.email)
user.stripecustomer.stripe_id=customer.get('id')
user.stripecustomer.save()
return customer
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
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,
interval=self.INTERVAL,
name=name,
currency=self.CURRENCY,
id=id)
def make_payment(self, user, amount, token):
import ipdb;ipdb.set_trace()
try:
# Use Stripe's library to make requests...
charge = self.stripe.Charge.create(
amount=amount,
amount=amount, # in cents
currency=self.CURRENCY,
source=token,
description=settings.STRIPE_DESCRIPTION_ON_PAYMENT
customer=customer
)
if charge.get('status') == self.SUCCEEDED_STATUS:
# do something
pass
return charge['status']
return charge
except self.stripe.error.CardError as e:
# Since it's a decline, stripe.error.CardError will be caught
body = e.json_body
@ -74,3 +65,11 @@ class StripeUtils(object):
except Exception as e:
# maybe send email
return "Currently its not possible to make payments."
def create_plan(self, amount, name, id):
self.stripe.Plan.create(
amount=amount,
interval=self.INTERVAL,
name=name,
currency=self.CURRENCY,
id=id)