Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Manage payment in order details view
Marcin Gębala7b78a0640761
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 31ba9db5ae..33e9797d72 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -16,3 +16,7 @@ class OrderNoteForm(forms.ModelForm):widgets = {'content': forms.Textarea({'rows': 5, 'placeholder': _('Note')})}+++class ManagePaymentForm(forms.Form):+ amount = forms.DecimalField(min_value=0, decimal_places=2)diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex d753aa4c4a..2c6935f521 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -3,10 +3,12 @@ from django.utils.translation import ugettext_lazy as _from django.views.generic import ListView, DetailView, UpdateViewfrom django.core.urlresolvers import reversefrom django.shortcuts import get_object_or_404+from payments import PaymentError+from ...order.models import Order, Addressfrom ...userprofile.forms import AddressFormfrom ..views import StaffMemberOnlyMixin, FilterByStatusMixin-from .forms import OrderNoteForm+from .forms import OrderNoteForm, ManagePaymentFormclass OrderListView(StaffMemberOnlyMixin, FilterByStatusMixin, ListView):@@ -20,21 +22,29 @@ class OrderDetails(StaffMemberOnlyMixin, DetailView):model = Ordertemplate_name = 'dashboard/order/detail.html'context_object_name = 'order'- form_class = OrderNoteForm- form_instance = None+ payment_form_class = ManagePaymentForm+ note_form_class = OrderNoteFormdef get_queryset(self):qs = super(OrderDetails, self).get_queryset()qs = qs.prefetch_related('notes')-return qsdef get_context_data(self, **kwargs):ctx = super(OrderDetails, self).get_context_data(**kwargs)- if not self.form_instance:- self.form_instance = self.form_class()- ctx['notes_form'] = self.form_instance+ if 'payment_form' not in ctx:+ ctx['payment_form'] = self.payment_form_class(+ initial={'amount': self.object.get_total().gross})+ if 'note_form' not in ctx:+ ctx['note_form'] = self.note_form_class()++ last_payment_status = self.object.get_last_payment_status()+ ctx['can_capture'] = last_payment_status == 'preauth'+ ctx['can_release'] = last_payment_status == 'preauth'+ ctx['can_refund'] = last_payment_status == 'confirmed'+ctx['notes'] = self.object.notes.all()+ ctx['payment'] = self.object.payments.last()return ctxdef get_delivery_info(self):@@ -43,8 +53,46 @@ class OrderDetails(StaffMemberOnlyMixin, DetailView):except self.model.DoesNotExist:return None- def post(self, *args, **kwargs):- form = self.form_class(self.request.POST)+ def post(self, request, *args, **kwargs):+ if 'payment_form' in request.POST:+ form = self.payment_form_class(request.POST)+ self.handle_payment_form(form, request.POST['payment_form'])+ else:+ form = self.note_form_class(request.POST)+ self.handle_note_form(form)+ return self.get(request, *args, **kwargs)++ def handle_payment_form(self, form, action):+ payment = self.get_object().payments.last()+ error_msg = None+ if form.is_valid():+ if action == 'capture' and payment.status == 'preauth':+ try:+ payment.capture(amount=form.cleaned_data['amount'])+ except PaymentError, e:+ error_msg = _('Payment gateway error: ') + e.message+ else:+ messages.success(self.request, _('Funds captured'))++ elif action == 'refund' and payment.status == 'confirmed':+ try:+ payment.refund(amount=payment.captured_amount)+ except PaymentError, e:+ error_msg = _('Payment gateway error: ') + e.message+ else:+ messages.success(self.request, _('Refund successful'))++ elif action == 'release' and payment.status == 'preauth':+ try:+ payment.release()+ except PaymentError, e:+ error_msg = _('Payment gateway error: ') + e.message+ else:+ messages.success(self.request, _('Release successful'))+ if error_msg:+ messages.error(self.request, error_msg)++ def handle_note_form(self, form):if form.is_valid():note = form.save(commit=False)note.order = self.get_object()@@ -53,8 +101,6 @@ class OrderDetails(StaffMemberOnlyMixin, DetailView):messages.success(self.request, _('Note saved'))else:messages.error(self.request, _('Form has errors'))- self.form_instance = form- return self.get(*args, **kwargs)class AddressView(StaffMemberOnlyMixin, UpdateView):diff --git a/saleor/dashboard/templates/dashboard/order/detail.html b/saleor/dashboard/templates/dashboard/order/detail.htmlindex 966fdef379..440309170a 100644--- a/saleor/dashboard/templates/dashboard/order/detail.html+++ b/saleor/dashboard/templates/dashboard/order/detail.html@@ -57,6 +57,39 @@</div></div>+ <div class="panel panel-default">+ <div class="panel-heading">+ <span class="panel-title">{% trans "Manage payment" %}</span>+ </div>+ <div class="panel-body">+ <div>+ {% if payment_form %}+ <form method="post">+ {% csrf_token %}+ {{ payment_form|as_vertical_form }}+ <div class="form-group">+ <input type="submit" class="btn btn-default" name='payment_form' value="capture" {% if not can_capture %} disabled {% endif %}/>+ <input type="submit" class="btn btn-default" name='payment_form' value="release" {% if not can_release %} disabled {% endif %}/>+ <input type="submit" class="btn btn-default" name='payment_form' value="refund" {% if not can_refund %} disabled {% endif %}/>+ </div>+ </form>+ {% endif %}+ </div>+ <div>+ <h4>Payment history</h4>+ <ul class="list-unstyled">+ {% for payment in order.payments.all %}+ <li>+ <span>{{ payment.created }}</span>+ {% include 'dashboard/includes/_status_label.html' with status=payment.status status_display=payment.get_status_display %}+ <a href="{% url "dashboard:payment-details" pk=payment.pk %}">Details</a>+ </li>+ {% endfor %}+ </ul>+ </div>+ </div>+ </div>+<div class="panel panel-default" id="order-notes"><div class="panel-heading"><span class="panel-title">Order notes</span>@@ -78,31 +111,15 @@<div id="add-order-note" class="collapse"><form class="form-horizontal" role="form" method="post">{% csrf_token %}- {{ notes_form|as_vertical_form }}- <div class="form-group">- <button type="submit" class="btn btn-default">Save</button>- </div>+ {{ note_form|as_vertical_form }}+ <div class="form-group">+ <button name='note_form' type="submit" class="btn btn-default">Save</button>+ </div></form></div></div></div>- <div class="panel panel-default">- <div class="panel-heading">- <span class="panel-title">{% trans "Payment history" %}</span>- </div>- <div class="panel-body">- <ul class="list-unstyled">- {% for payment in order.payments.all %}- <li>- <span>{{ payment.created }}</span>- {% include 'dashboard/includes/_status_label.html' with status=payment.status status_display=payment.get_status_display %}- <a href="{% url "dashboard:payment-details" pk=payment.pk %}">Details</a>- </li>- {% endfor %}- </ul>- </div>- </div></div><div class="col-lg-4">