-
+
{% 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)