combo/combo/apps/lingo/views.py

110 lines
3.7 KiB
Python
Raw Normal View History

# lingo - basket and payment system
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from decimal import Decimal
import json
from django.contrib.auth.models import User
2015-03-05 17:02:52 +01:00
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View, ListView
2015-03-05 17:02:52 +01:00
import eopayment
from .models import Regie, BasketItem, Transaction
class RegiesApiView(ListView):
model = Regie
def get(self, request, *args, **kwargs):
response = HttpResponse(content_type='application/json')
data = {'data': [x.as_api_dict() for x in self.get_queryset()]}
json_str = json.dumps(data)
if 'jsonpCallback' in request.GET:
json_str = '%s(%s);' % (request.GET['jsonpCallback'], json_str)
response.write(json_str)
return response
class AddBasketItemApiView(View):
http_method_names = ['post', 'options']
@csrf_exempt
def dispatch(self, *args, **kwargs):
return super(AddBasketItemApiView, self).dispatch(*args, **kwargs)
def post(self, request, *args, **kwargs):
# XXX: check request signature
request_body = json.loads(self.request.body)
item = BasketItem()
item.amount = Decimal(request.GET.get('amount'))
try:
if request.GET.get('NameId'):
user = User.objects.get(username=request.GET.get('NameId'))
elif request.GET.get('email'):
user = User.objects.get(email=request.GET.get('email'))
else:
raise Exception('no user specified')
except User.DoesNotExist:
raise Exception('unknown user')
item.user = user
if request.GET.get('regie_id'):
item.regie = Regie.objects.get(id=request.GET.get('regie_id'))
else:
# if there's no regie specified, use the first one we get from the
# database...
item.regie = Regie.objects.all()[0]
item.subject = request_body.get('display_name')
item.source_url = request_body.get('url')
item.save()
response = HttpResponse(content_type='application/json')
response.write(json.dumps({'result': 'success'}))
return response
2015-03-05 17:02:52 +01:00
class PayView(View):
def post(self, request, *args, **kwargs):
items = BasketItem.objects.filter(id__in=request.POST.getlist('item'))
# XXX: check all items are going to the same regie
transaction = Transaction()
transaction.save()
transaction.items = items
transaction.save()
total_amount = sum([x.amount for x in items])
regie = items[0].regie
payment = eopayment.Payment(regie.service, regie.service_options)
(order_id, kind, data) = payment.request(total_amount,
next_url=request.build_absolute_uri('/'))
transaction.order_id = order_id
transaction.save()
# XXX: mark basket items as being processed (?)
if kind == eopayment.URL:
return HttpResponseRedirect(data)
raise NotImplementedError()