From 45730e4eb13d1dbe34d6a1f2fa49eb26eafd928f Mon Sep 17 00:00:00 2001 From: Tomislav R Date: Sun, 1 May 2016 14:13:12 +0200 Subject: [PATCH] update dg payment --- digitalglarus/templates/base_glarus.html | 4 +- digitalglarus/urls.py | 12 ++--- dynamicweb/settings/base.py | 4 +- dynamicweb/urls.py | 4 +- hosting/urls.py | 2 +- membership/models.py | 16 ++++-- membership/templates/membership.html | 4 +- membership/templates/validated.html | 7 +-- membership/views.py | 13 ++--- utils/stripe_utils.py | 63 ++++++++++++------------ 10 files changed, 69 insertions(+), 60 deletions(-) diff --git a/digitalglarus/templates/base_glarus.html b/digitalglarus/templates/base_glarus.html index fb2358da..a87e157e 100644 --- a/digitalglarus/templates/base_glarus.html +++ b/digitalglarus/templates/base_glarus.html @@ -28,7 +28,7 @@ rel="stylesheet" type="text/css"> - {% render_block "css" postprocessor "compressor.contrib.sekizai.compress" %} @@ -65,7 +65,7 @@
{% if request.user.is_authenticated %}
-

{{ request.user.name }} +

{{ request.user.name }}

diff --git a/digitalglarus/urls.py b/digitalglarus/urls.py index 4270fe2b..51c397e0 100644 --- a/digitalglarus/urls.py +++ b/digitalglarus/urls.py @@ -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\d+)/(?P\d+)?$',views.CalendarApi.as_view()), - url(r'calendar_api/',views.CalendarApi.as_view()), + url(r'calendar_api/(?P\d+)/(?P\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\w[-\w]*)/$', views.blog_detail, name='blog-detail'), url(r'blog/$', views.blog, name='blog'), diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 9d64dc60..69e708d2 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -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 diff --git a/dynamicweb/urls.py b/dynamicweb/urls.py index d813ab31..d6fb4a52 100644 --- a/dynamicweb/urls.py +++ b/dynamicweb/urls.py @@ -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: diff --git a/hosting/urls.py b/hosting/urls.py index dd27a76b..0cff09ec 100644 --- a/hosting/urls.py +++ b/hosting/urls.py @@ -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'), diff --git a/membership/models.py b/membership/models.py index 50b3edf4..3632e56a 100644 --- a/membership/models.py +++ b/membership/models.py @@ -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) diff --git a/membership/templates/membership.html b/membership/templates/membership.html index 578aae1f..f2b8cb59 100644 --- a/membership/templates/membership.html +++ b/membership/templates/membership.html @@ -125,7 +125,7 @@ for coworking. It is a perfect try-out package!

- Buy Now + Buy Now
@@ -146,7 +146,7 @@ membership.

- Buy Now + Buy Now
- + {% if request.session.next == None %} {% endif %} @@ -25,7 +26,7 @@ {% if msg == 'succeeded' %} Thank You for Your payment!

redirecting...

{% else %} - Your payment was not processed correctly.
Reason: {{ msg }} + Your payment was not processed correctly. Please contact us here! {% endif %}
diff --git a/membership/views.py b/membership/views.py index 65541210..5440167b 100644 --- a/membership/views.py +++ b/membership/views.py @@ -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 diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index cfe1e63a..5217711e 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -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)