Skip to content

Commit

Permalink
Update demo checkout for 0.6
Browse files Browse the repository at this point in the history
Fix issue with submit() method and a few other small issues.

Fixes #913
  • Loading branch information
codeinthehole committed Nov 4, 2013
1 parent a4b5396 commit c800715
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
40 changes: 23 additions & 17 deletions sites/demo/apps/checkout/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from django.contrib import messages
from django import http
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from datacash.facade import Facade

from oscar.apps.checkout import views
from oscar.apps.payment.forms import BankcardForm
from oscar.apps.payment.models import SourceType, Source
from oscar.apps.payment.models import SourceType


# Customise the core PaymentDetailsView to integrate Datacash
class PaymentDetailsView(views.PaymentDetailsView):

def get_context_data(self, **kwargs):
# Add bankcard form to the template context
ctx = super(PaymentDetailsView, self).get_context_data(**kwargs)
ctx['bankcard_form'] = kwargs.get('bankcard_form', BankcardForm())
if 'bankcard_form' not in kwargs:
kwargs['bankcard_form'] = BankcardForm()
ctx = super(PaymentDetailsView, self).get_context_data(**kwargs)
return ctx

def post(self, request, *args, **kwargs):
Expand All @@ -41,35 +41,41 @@ def do_place_order(self, request):
# Double-check the bankcard data is still valid
bankcard_form = BankcardForm(request.POST)
if not bankcard_form.is_valid():
# Must be tampering - we don't need to be that friendly with our
# error message.
messages.error(request, _("Invalid submission"))
return http.HttpResponseRedirect(
reverse('checkout:payment-details'))

# Call oscar's submit method, passing through the bankcard object so it
# gets passed to the 'handle_payment' method and can be used for the
# submission to Datacash.
bankcard = bankcard_form.bankcard
return self.submit(request.basket,
payment_kwargs={'bankcard': bankcard})
submission = self.build_submission(bankcard_form)
return self.submit(**submission)

def handle_payment(self, order_number, total_incl_tax, **kwargs):
def build_submission(self, bankcard_form, **kwargs):
# Modify the default submission dict with the bankcard instance
submission = super(PaymentDetailsView, self).build_submission()
if bankcard_form.is_valid():
submission['payment_kwargs']['bankcard'] = bankcard_form.bankcard
return submission

def handle_payment(self, order_number, total, **kwargs):
# Make request to DataCash - if there any problems (eg bankcard
# not valid / request refused by bank) then an exception would be
# raised and handled by the parent PaymentDetail view)
facade = Facade()
datacash_ref = facade.pre_authorise(
order_number, total_incl_tax, kwargs['bankcard'])
order_number, total.incl_tax, kwargs['bankcard'])

# Request was successful - record the "payment source". As this
# request was a 'pre-auth', we set the 'amount_allocated' - if we had
# performed an 'auth' request, then we would set 'amount_debited'.
source_type, _ = SourceType.objects.get_or_create(name='Datacash')
source = Source(source_type=source_type,
currency=settings.DATACASH_CURRENCY,
amount_allocated=total_incl_tax,
reference=datacash_ref)
source = source_type.sources.model(
source_type=source_type,
currency=total.currency,
amount_allocated=total.incl_tax,
reference=datacash_ref)
self.add_payment_source(source)

# Also record payment event
self.add_payment_event(
'pre-auth', total_incl_tax, reference=datacash_ref)
'pre-auth', total.incl_tax, reference=datacash_ref)
3 changes: 3 additions & 0 deletions sites/demo/apps/shipping/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Standard(base.Base):
code = 'standard'
name = 'Standard shipping'
is_tax_known = True

charge_per_item = D('0.99')
threshold = D('12.00')
Expand Down Expand Up @@ -37,6 +38,8 @@ def charge_excl_tax(self):
class Express(base.Base):
code = 'express'
name = 'Express shipping'
is_tax_known = True

charge_per_item = D('1.50')
description = render_to_string(
'shipping/express.html', {'charge_per_item': charge_per_item})
Expand Down
2 changes: 1 addition & 1 deletion sites/demo/templates/basket/partials/basket_totals.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ <h4>{% trans "Shipping" %}</h4>
</tr>
<tr class="success">
<td><h4>{% trans "Order total" %}</h4></td>
<td class="align-right"><h3 class="price_color">{{ order_total_incl_tax|currency }}</h3></td>
<td class="align-right"><h3 class="price_color">{{ order_total.incl_tax|currency }}</h3></td>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion sites/demo/templates/checkout/preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="span6">
<h3>{% trans "Payment" %}</h3>
<div class="well well-success" data-behaviours="match-height">
<p>{% blocktrans with amount=order_total_incl_tax|currency %}{{ amount }} will be debited from your bankcard.{% endblocktrans %}</p>
<p>{% blocktrans with amount=order_total.incl_tax|currency %}{{ amount }} will be debited from your bankcard.{% endblocktrans %}</p>
<div class="alert-actions">
<a href="{% url 'checkout:payment-details' %}" class="btn">{% trans "Change payment details" %}</a>
</div>
Expand Down

0 comments on commit c800715

Please sign in to comment.