From 7212cb0081b6b7367d7be843ae31f71a45f5c4ac Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Thu, 12 May 2016 09:51:35 +0200 Subject: [PATCH] a bit more work on still unfinished shop --- shop/forms.py | 6 ++ .../shop/{order.html => checkout.html} | 0 .../shop/{detail.html => order_detail.html} | 0 shop/urls.py | 15 +--- shop/views.py | 70 ++++++++++++++++++- 5 files changed, 78 insertions(+), 13 deletions(-) rename shop/templates/shop/{order.html => checkout.html} (100%) rename shop/templates/shop/{detail.html => order_detail.html} (100%) diff --git a/shop/forms.py b/shop/forms.py index e79048bf..d3442c84 100644 --- a/shop/forms.py +++ b/shop/forms.py @@ -1 +1,7 @@ from django import forms + +class CheckoutForm(forms.ModelForm): + class Meta: + model = Order + fields = ['payment_method'] + diff --git a/shop/templates/shop/order.html b/shop/templates/shop/checkout.html similarity index 100% rename from shop/templates/shop/order.html rename to shop/templates/shop/checkout.html diff --git a/shop/templates/shop/detail.html b/shop/templates/shop/order_detail.html similarity index 100% rename from shop/templates/shop/detail.html rename to shop/templates/shop/order_detail.html diff --git a/shop/urls.py b/shop/urls.py index 5b77c68e..40e9526d 100644 --- a/shop/urls.py +++ b/shop/urls.py @@ -1,13 +1,7 @@ from django.conf.urls import url - -from .views import ( - ShopIndexView, - # EpayView, - # EpayCallbackView, -) +import .views urlpatterns = [ - #url(r'order/$', TicketOrderView.as_view(), name='order'), #url( #r'pay/credit_card/(?P[a-zA-Z0-9\-]+)/$', #EpayView.as_view(), @@ -18,10 +12,7 @@ urlpatterns = [ #EpayCallbackView, #name='epay_callback' #), - #url( - #r'detail/(?P[a-zA-Z0-9\-]+)/$', - #TicketDetailView.as_view(), - #name='detail' - #), url(r'$', ShopIndexView.as_view(), name='index'), + url(r'orders/(?P[0-9]+)/$', OrderDetailView.as_view(), name='order_detail'), + url(r'orders/(?P[0-9]+)/checkout/$', CheckoutView.as_view(), name='checkout'), ] diff --git a/shop/views.py b/shop/views.py index a0efa63d..8c977e99 100644 --- a/shop/views.py +++ b/shop/views.py @@ -1,7 +1,7 @@ import hashlib from django.http import HttpResponseRedirect, Http404 -from django.views.generic import CreateView, TemplateView, DetailView, View +from django.views.generic import CreateView, TemplateView, DetailView, View, FormView from django.core.urlresolvers import reverse_lazy from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin @@ -25,6 +25,74 @@ class ProductDetailView(LoginRequiredMixin, DetailView): context_object_name = 'product' +class CheckoutView(LoginRequiredMixin, DetailView): + """ + Shows the products contained in an order, and a button to go to the payment + """ + model = Order + template_name = 'shop/order_detail.html' + context_object_name = 'order' + + +class PaymentView(LoginRequiredMixin, FormView): + """ + One final chance to change payment method (in case another method failed), + and a submit button to intiate payment with selected metod + """ + template_name = 'shop/payment.html' + form_class = PaymentMethodForm + + def get(self, request, *args, **kwargs): + if self.instance.user != self.request.user: + raise Http404("Order not found") + + if self.instance.paid: + messages.error('This order is already paid for!') + return HttpResponseRedirect('shop:order_detail') + + if not self.instance.products: + messages.error('This order contains no products!') + return HttpResponseRedirect('shop:order_detail') + + return self.render_to_response(self.get_context_data()) + + def get_context_data(self, **kwargs): + order = Order.objects.get(pk=kwargs.get('order_id') + context = super(CheckoutView, self).get_context_data(**kwargs) + context['order'] = order + return context + + +class CoinifyView(TemplateView): + template_name = 'shop/coinify_form.html' + + def get_context_data(self, **kwargs): + order = Order.objects.get(pk=kwargs.get('order_id') + context = super(CoinifyView, self).get_context_data(**kwargs) + context['order'] = order + + coinifyapi = CoinifyAPI(settings.COINIFY_API_KEY, settings.COINIFY_API_SECRET) + + response = coinifyapi.invoice_create( + amount, + currency, + plugin_name='BornHack 2016 webshop', + plugin_version='1.0', + description='BornHack 2016 order id #%s' % order.id, + callback_url=reverse('shop:coinfy_callback', kwargs={'orderid': order.id}), + return_url=reverse('shop:order_paid', kwargs={'orderid': order.id}), + ) + + if not response['success']: + api_error = response['error'] + print "API error: %s (%s)" % (api_error['message'], api_error['code'] ) + + invoice = response['data'] + ### change this to pass only needed data when we get that far + context['invoice'] = invoice + return context + + # class EpayView(TemplateView): # template_name = 'tickets/epay_form.html' #