You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. import six
  2. import logging
  3. from django import http
  4. from django.contrib import messages
  5. from django.contrib.auth import login
  6. from django.core.urlresolvers import reverse, reverse_lazy
  7. from django.utils.translation import ugettext as _
  8. from django.views import generic
  9. from oscar.core.loading import get_model
  10. from oscar.apps.shipping.methods import NoShippingRequired
  11. from oscar.core.loading import get_class, get_classes
  12. from . import signals
  13. ShippingAddressForm, GatewayForm \
  14. = get_classes('checkout.forms', ['ShippingAddressForm', 'GatewayForm'])
  15. OrderCreator = get_class('order.utils', 'OrderCreator')
  16. UserAddressForm = get_class('address.forms', 'UserAddressForm')
  17. Repository = get_class('shipping.repository', 'Repository')
  18. AccountAuthView = get_class('customer.views', 'AccountAuthView')
  19. RedirectRequired, UnableToTakePayment, PaymentError \
  20. = get_classes('payment.exceptions', ['RedirectRequired',
  21. 'UnableToTakePayment',
  22. 'PaymentError'])
  23. UnableToPlaceOrder = get_class('order.exceptions', 'UnableToPlaceOrder')
  24. OrderPlacementMixin = get_class('checkout.mixins', 'OrderPlacementMixin')
  25. CheckoutSessionMixin = get_class('checkout.session', 'CheckoutSessionMixin')
  26. Order = get_model('order', 'Order')
  27. ShippingAddress = get_model('order', 'ShippingAddress')
  28. CommunicationEvent = get_model('order', 'CommunicationEvent')
  29. PaymentEventType = get_model('order', 'PaymentEventType')
  30. PaymentEvent = get_model('order', 'PaymentEvent')
  31. UserAddress = get_model('address', 'UserAddress')
  32. Basket = get_model('basket', 'Basket')
  33. Email = get_model('customer', 'Email')
  34. CommunicationEventType = get_model('customer', 'CommunicationEventType')
  35. # Standard logger for checkout events
  36. logger = logging.getLogger('oscar.checkout')
  37. class IndexView(CheckoutSessionMixin, generic.FormView):
  38. """
  39. First page of the checkout. We prompt user to either sign in, or
  40. to proceed as a guest (where we still collect their email address).
  41. """
  42. template_name = 'checkout/gateway.html'
  43. form_class = GatewayForm
  44. success_url = reverse_lazy('checkout:shipping-address')
  45. pre_conditions = (
  46. 'check_basket_is_not_empty',
  47. 'check_basket_is_valid')
  48. def get(self, request, *args, **kwargs):
  49. # We redirect immediately to shipping address stage if the user is
  50. # signed in.
  51. if request.user.is_authenticated():
  52. # We raise a signal to indicate that the user has entered the
  53. # checkout process so analytics tools can track this event.
  54. signals.start_checkout.send_robust(
  55. sender=self, request=request)
  56. return self.get_success_response()
  57. return super(IndexView, self).get(request, *args, **kwargs)
  58. def get_form_kwargs(self):
  59. kwargs = super(IndexView, self).get_form_kwargs()
  60. email = self.checkout_session.get_guest_email()
  61. if email:
  62. kwargs['initial'] = {
  63. 'username': email,
  64. }
  65. return kwargs
  66. def form_valid(self, form):
  67. if form.is_guest_checkout() or form.is_new_account_checkout():
  68. email = form.cleaned_data['username']
  69. self.checkout_session.set_guest_email(email)
  70. # We raise a signal to indicate that the user has entered the
  71. # checkout process by specifying an email address.
  72. signals.start_checkout.send_robust(
  73. sender=self, request=self.request, email=email)
  74. if form.is_new_account_checkout():
  75. messages.info(
  76. self.request,
  77. _("Create your account and then you will be redirected "
  78. "back to the checkout process"))
  79. self.success_url = "%s?next=%s&email=%s" % (
  80. reverse('customer:register'),
  81. reverse('checkout:shipping-address'),
  82. email
  83. )
  84. else:
  85. user = form.get_user()
  86. login(self.request, user)
  87. # We raise a signal to indicate that the user has entered the
  88. # checkout process.
  89. signals.start_checkout.send_robust(
  90. sender=self, request=self.request)
  91. return self.get_success_response()
  92. def get_success_response(self):
  93. return http.HttpResponseRedirect(self.get_success_url())
  94. # ================
  95. # SHIPPING ADDRESS
  96. # ================
  97. class ShippingAddressView(CheckoutSessionMixin, generic.FormView):
  98. """
  99. Determine the shipping address for the order.
  100. The default behaviour is to display a list of addresses from the users's
  101. address book, from which the user can choose one to be their shipping
  102. address. They can add/edit/delete these USER addresses. This address will
  103. be automatically converted into a SHIPPING address when the user checks
  104. out.
  105. Alternatively, the user can enter a SHIPPING address directly which will be
  106. saved in the session and later saved as ShippingAddress model when the
  107. order is sucessfully submitted.
  108. """
  109. template_name = 'checkout/shipping_address.html'
  110. form_class = ShippingAddressForm
  111. success_url = reverse_lazy('checkout:shipping-method')
  112. pre_conditions = ('check_basket_is_not_empty',
  113. 'check_basket_is_valid',
  114. 'check_user_email_is_captured',
  115. 'check_basket_requires_shipping')
  116. def get_initial(self):
  117. return self.checkout_session.new_shipping_address_fields()
  118. def get_context_data(self, **kwargs):
  119. ctx = super(ShippingAddressView, self).get_context_data(**kwargs)
  120. if self.request.user.is_authenticated():
  121. # Look up address book data
  122. ctx['addresses'] = self.get_available_addresses()
  123. return ctx
  124. def get_available_addresses(self):
  125. # Include only addresses where the country is flagged as valid for
  126. # shipping. Also, use ordering to ensure the default address comes
  127. # first.
  128. return self.request.user.addresses.filter(
  129. country__is_shipping_country=True).order_by(
  130. '-is_default_for_shipping')
  131. def post(self, request, *args, **kwargs):
  132. # Check if a shipping address was selected directly (eg no form was
  133. # filled in)
  134. if self.request.user.is_authenticated() \
  135. and 'address_id' in self.request.POST:
  136. address = UserAddress._default_manager.get(
  137. pk=self.request.POST['address_id'], user=self.request.user)
  138. action = self.request.POST.get('action', None)
  139. if action == 'ship_to':
  140. # User has selected a previous address to ship to
  141. self.checkout_session.ship_to_user_address(address)
  142. return self.get_success_response()
  143. elif action == 'delete':
  144. # Delete the selected address
  145. address.delete()
  146. messages.info(self.request, _("Address deleted from your"
  147. " address book"))
  148. return self.get_success_response()
  149. else:
  150. return http.HttpResponseBadRequest()
  151. else:
  152. return super(ShippingAddressView, self).post(
  153. request, *args, **kwargs)
  154. def form_valid(self, form):
  155. # Store the address details in the session and redirect to next step
  156. address_fields = dict(
  157. (k, v) for (k, v) in form.instance.__dict__.items()
  158. if not k.startswith('_'))
  159. self.checkout_session.ship_to_new_address(address_fields)
  160. return super(ShippingAddressView, self).form_valid(form)
  161. def get_success_response(self):
  162. return http.HttpResponseRedirect(self.get_success_url())
  163. class UserAddressUpdateView(CheckoutSessionMixin, generic.UpdateView):
  164. """
  165. Update a user address
  166. """
  167. template_name = 'checkout/user_address_form.html'
  168. form_class = UserAddressForm
  169. def get_queryset(self):
  170. return self.request.user.addresses.all()
  171. def get_form_kwargs(self):
  172. kwargs = super(UserAddressUpdateView, self).get_form_kwargs()
  173. kwargs['user'] = self.request.user
  174. return kwargs
  175. def get_success_url(self):
  176. messages.info(self.request, _("Address saved"))
  177. return reverse('checkout:shipping-address')
  178. class UserAddressDeleteView(CheckoutSessionMixin, generic.DeleteView):
  179. """
  180. Delete an address from a user's addressbook.
  181. """
  182. template_name = 'checkout/user_address_delete.html'
  183. def get_queryset(self):
  184. return self.request.user.addresses.all()
  185. def get_success_url(self):
  186. messages.info(self.request, _("Address deleted"))
  187. return reverse('checkout:shipping-address')
  188. # ===============
  189. # Shipping method
  190. # ===============
  191. class ShippingMethodView(CheckoutSessionMixin, generic.TemplateView):
  192. """
  193. View for allowing a user to choose a shipping method.
  194. Shipping methods are largely domain-specific and so this view
  195. will commonly need to be subclassed and customised.
  196. The default behaviour is to load all the available shipping methods
  197. using the shipping Repository. If there is only 1, then it is
  198. automatically selected. Otherwise, a page is rendered where
  199. the user can choose the appropriate one.
  200. """
  201. template_name = 'checkout/shipping_methods.html'
  202. pre_conditions = ('check_basket_is_not_empty',
  203. 'check_basket_is_valid',
  204. 'check_user_email_is_captured', )
  205. def get(self, request, *args, **kwargs):
  206. # These pre-conditions can't easily be factored out into the normal
  207. # pre-conditions as they do more than run a test and then raise an
  208. # exception if it fails.
  209. # Check that shipping is required at all
  210. if not request.basket.is_shipping_required():
  211. # No shipping required - we store a special code to indicate so.
  212. self.checkout_session.use_shipping_method(
  213. NoShippingRequired().code)
  214. return self.get_success_response()
  215. # Check that shipping address has been completed
  216. if not self.checkout_session.is_shipping_address_set():
  217. messages.error(request, _("Please choose a shipping address"))
  218. return http.HttpResponseRedirect(
  219. reverse('checkout:shipping-address'))
  220. # Save shipping methods as instance var as we need them both here
  221. # and when setting the context vars.
  222. self._methods = self.get_available_shipping_methods()
  223. if len(self._methods) == 0:
  224. # No shipping methods available for given address
  225. messages.warning(request, _(
  226. "Shipping is unavailable for your chosen address - please "
  227. "choose another"))
  228. return http.HttpResponseRedirect(
  229. reverse('checkout:shipping-address'))
  230. elif len(self._methods) == 1:
  231. # Only one shipping method - set this and redirect onto the next
  232. # step
  233. self.checkout_session.use_shipping_method(self._methods[0].code)
  234. return self.get_success_response()
  235. # Must be more than one available shipping method, we present them to
  236. # the user to make a choice.
  237. return super(ShippingMethodView, self).get(request, *args, **kwargs)
  238. def get_context_data(self, **kwargs):
  239. kwargs = super(ShippingMethodView, self).get_context_data(**kwargs)
  240. kwargs['methods'] = self._methods
  241. return kwargs
  242. def get_available_shipping_methods(self):
  243. """
  244. Returns all applicable shipping method objects
  245. for a given basket.
  246. """
  247. # Shipping methods can depend on the user, the contents of the basket
  248. # and the shipping address. I haven't come across a scenario that
  249. # doesn't fit this system.
  250. return Repository().get_shipping_methods(
  251. user=self.request.user, basket=self.request.basket,
  252. shipping_addr=self.get_shipping_address(self.request.basket),
  253. request=self.request)
  254. def post(self, request, *args, **kwargs):
  255. # Need to check that this code is valid for this user
  256. method_code = request.POST.get('method_code', None)
  257. is_valid = False
  258. for method in self.get_available_shipping_methods():
  259. if method.code == method_code:
  260. is_valid = True
  261. if not is_valid:
  262. messages.error(request, _("Your submitted shipping method is not"
  263. " permitted"))
  264. return http.HttpResponseRedirect(
  265. reverse('checkout:shipping-method'))
  266. # Save the code for the chosen shipping method in the session
  267. # and continue to the next step.
  268. self.checkout_session.use_shipping_method(method_code)
  269. return self.get_success_response()
  270. def get_success_response(self):
  271. return http.HttpResponseRedirect(reverse('checkout:payment-method'))
  272. # ==============
  273. # Payment method
  274. # ==============
  275. class PaymentMethodView(CheckoutSessionMixin, generic.TemplateView):
  276. """
  277. View for a user to choose which payment method(s) they want to use.
  278. This would include setting allocations if payment is to be split
  279. between multiple sources. It's not the place for entering sensitive details
  280. like bankcard numbers though - that belongs on the payment details view.
  281. """
  282. pre_conditions = (
  283. 'check_basket_is_not_empty',
  284. 'check_basket_is_valid',
  285. 'check_user_email_is_captured',
  286. 'check_shipping_data_is_captured')
  287. def get(self, request, *args, **kwargs):
  288. # By default we redirect straight onto the payment details view. Shops
  289. # that require a choice of payment method may want to override this
  290. # method to implement their specific logic.
  291. return self.get_success_response()
  292. def get_success_response(self):
  293. return http.HttpResponseRedirect(reverse('checkout:payment-details'))
  294. # ================
  295. # Order submission
  296. # ================
  297. class PaymentDetailsView(OrderPlacementMixin, generic.TemplateView):
  298. """
  299. For taking the details of payment and creating the order.
  300. This view class is used by two separate URLs: 'payment-details' and
  301. 'preview'. The `preview` class attribute is used to distinguish which is
  302. being used.
  303. If sensitive details are required (eg a bankcard), then the payment details
  304. view should submit to the preview URL and a custom implementation of
  305. `validate_payment_submission` should be provided.
  306. - If the form data is valid, then the preview template can be rendered with
  307. the payment-details forms re-rendered within a hidden div so they can be
  308. re-submitted when the 'place order' button is clicked. This avoids having
  309. to write sensitive data to disk anywhere during the process. This can be
  310. done by calling `render_preview`, passing in the extra template context
  311. vars.
  312. - If the form data is invalid, then the payment details templates needs to
  313. be re-rendered with the relevant error messages. This can be done by
  314. calling `render_payment_details`, passing in the form instances to pass
  315. to the templates.
  316. The class is deliberately split into fine-grained methods, responsible for
  317. only one thing. This is to make it easier to subclass and override just
  318. one component of functionality.
  319. All projects will need to subclass and customise this class as no payment
  320. is taken by default.
  321. """
  322. template_name = 'checkout/payment_details.html'
  323. template_name_preview = 'checkout/preview.html'
  324. pre_conditions = (
  325. 'check_basket_is_not_empty',
  326. 'check_basket_is_valid',
  327. 'check_user_email_is_captured',
  328. 'check_shipping_data_is_captured')
  329. # If preview=True, then we render a preview template that shows all order
  330. # details ready for submission.
  331. preview = False
  332. def get_preconditions(self, request):
  333. if not self.preview:
  334. return self.pre_conditions
  335. # The preview view needs to ensure payment information has been
  336. # correctly captured.
  337. return self.pre_conditions + (
  338. 'check_payment_data_is_captured',)
  339. def get_template_names(self):
  340. return [self.template_name_preview] if self.preview else [
  341. self.template_name]
  342. def post(self, request, *args, **kwargs):
  343. # Posting to payment-details isn't the right thing to do. Form
  344. # submissions should use the preview URL.
  345. if not self.preview:
  346. return http.HttpBadRequest()
  347. # We use a custom parameter to indicate if this is an attempt to place
  348. # an order (normally from the preview page). Without this, we assume a
  349. # payment form is being submitted from the payment details view. In
  350. # this case, the form needs validating and the order preview shown.
  351. if request.POST.get('action', '') == 'place_order':
  352. return self.handle_place_order_submission(request)
  353. return self.handle_payment_details_submission(request)
  354. def handle_place_order_submission(self, request):
  355. """
  356. Handle a request to place an order.
  357. This method is normally called after the customer has clicked "place
  358. order" on the preview page. It's responsible for (re-)validating any
  359. form information then building the submission dict to pass to the
  360. `submit` method.
  361. If forms are submitted on your payment details view, you should
  362. override this method to ensure they are valid before extracting their
  363. data into the submission dict and passing it onto `submit`.
  364. """
  365. return self.submit(**self.build_submission())
  366. def handle_payment_details_submission(self, request):
  367. """
  368. Handle a request to submit payment details.
  369. This method will need to be overridden by projects that require forms
  370. to be submitted on the payment details view. The new version of this
  371. method should validate the submitted form data and:
  372. - If the form data is valid, show the preview view with the forms
  373. re-rendered in the page
  374. - If the form data is invalid, show the payment details view with
  375. the form errors showing.
  376. """
  377. # No form data to validate by default, so we simply render the preview
  378. # page. If validating form data and it's invalid, then call the
  379. # render_payment_details view.
  380. return self.render_preview(request)
  381. def render_preview(self, request, **kwargs):
  382. """
  383. Show a preview of the order.
  384. If sensitive data was submitted on the payment details page, you will
  385. need to pass it back to the view here so it can be stored in hidden
  386. form inputs. This avoids ever writing the sensitive data to disk.
  387. """
  388. self.preview = True
  389. ctx = self.get_context_data(**kwargs)
  390. return self.render_to_response(ctx)
  391. def render_payment_details(self, request, **kwargs):
  392. """
  393. Show the payment details page
  394. This method is useful if the submission from the payment details view
  395. is invalid and needs to be re-rendered with form errors showing.
  396. """
  397. self.preview = False
  398. ctx = self.get_context_data(**kwargs)
  399. return self.render_to_response(ctx)
  400. def get_default_billing_address(self):
  401. """
  402. Return default billing address for user
  403. This is useful when the payment details view includes a billing address
  404. form - you can use this helper method to prepopulate the form.
  405. Note, this isn't used in core oscar as there is no billing address form
  406. by default.
  407. """
  408. if not self.request.user.is_authenticated():
  409. return None
  410. try:
  411. return self.request.user.addresses.get(is_default_for_billing=True)
  412. except UserAddress.DoesNotExist:
  413. return None
  414. def submit(self, user, basket, shipping_address, shipping_method, # noqa (too complex (10))
  415. order_total, payment_kwargs=None, order_kwargs=None):
  416. """
  417. Submit a basket for order placement.
  418. The process runs as follows:
  419. * Generate an order number
  420. * Freeze the basket so it cannot be modified any more (important when
  421. redirecting the user to another site for payment as it prevents the
  422. basket being manipulated during the payment process).
  423. * Attempt to take payment for the order
  424. - If payment is successful, place the order
  425. - If a redirect is required (eg PayPal, 3DSecure), redirect
  426. - If payment is unsuccessful, show an appropriate error message
  427. :basket: The basket to submit.
  428. :payment_kwargs: Additional kwargs to pass to the handle_payment method
  429. :order_kwargs: Additional kwargs to pass to the place_order method
  430. """
  431. if payment_kwargs is None:
  432. payment_kwargs = {}
  433. if order_kwargs is None:
  434. order_kwargs = {}
  435. # Taxes must be known at this point
  436. assert basket.is_tax_known, (
  437. "Basket tax must be set before a user can place an order")
  438. assert shipping_method.is_tax_known, (
  439. "Shipping method tax must be set before a user can place an order")
  440. # We generate the order number first as this will be used
  441. # in payment requests (ie before the order model has been
  442. # created). We also save it in the session for multi-stage
  443. # checkouts (eg where we redirect to a 3rd party site and place
  444. # the order on a different request).
  445. order_number = self.generate_order_number(basket)
  446. self.checkout_session.set_order_number(order_number)
  447. logger.info("Order #%s: beginning submission process for basket #%d",
  448. order_number, basket.id)
  449. # Freeze the basket so it cannot be manipulated while the customer is
  450. # completing payment on a 3rd party site. Also, store a reference to
  451. # the basket in the session so that we know which basket to thaw if we
  452. # get an unsuccessful payment response when redirecting to a 3rd party
  453. # site.
  454. self.freeze_basket(basket)
  455. self.checkout_session.set_submitted_basket(basket)
  456. # We define a general error message for when an unanticipated payment
  457. # error occurs.
  458. error_msg = _("A problem occurred while processing payment for this "
  459. "order - no payment has been taken. Please "
  460. "contact customer services if this problem persists")
  461. signals.pre_payment.send_robust(sender=self, view=self)
  462. try:
  463. self.handle_payment(order_number, order_total, **payment_kwargs)
  464. except RedirectRequired as e:
  465. # Redirect required (eg PayPal, 3DS)
  466. logger.info("Order #%s: redirecting to %s", order_number, e.url)
  467. return http.HttpResponseRedirect(e.url)
  468. except UnableToTakePayment as e:
  469. # Something went wrong with payment but in an anticipated way. Eg
  470. # their bankcard has expired, wrong card number - that kind of
  471. # thing. This type of exception is supposed to set a friendly error
  472. # message that makes sense to the customer.
  473. msg = six.text_type(e)
  474. logger.warning(
  475. "Order #%s: unable to take payment (%s) - restoring basket",
  476. order_number, msg)
  477. self.restore_frozen_basket()
  478. # We assume that the details submitted on the payment details view
  479. # were invalid (eg expired bankcard).
  480. return self.render_payment_details(
  481. self.request, error=msg, **payment_kwargs)
  482. except PaymentError as e:
  483. # A general payment error - Something went wrong which wasn't
  484. # anticipated. Eg, the payment gateway is down (it happens), your
  485. # credentials are wrong - that king of thing.
  486. # It makes sense to configure the checkout logger to
  487. # mail admins on an error as this issue warrants some further
  488. # investigation.
  489. msg = six.text_type(e)
  490. logger.error("Order #%s: payment error (%s)", order_number, msg,
  491. exc_info=True)
  492. self.restore_frozen_basket()
  493. return self.render_preview(
  494. self.request, error=error_msg, **payment_kwargs)
  495. except Exception as e:
  496. # Unhandled exception - hopefully, you will only ever see this in
  497. # development...
  498. logger.error(
  499. "Order #%s: unhandled exception while taking payment (%s)",
  500. order_number, e, exc_info=True)
  501. self.restore_frozen_basket()
  502. return self.render_preview(
  503. self.request, error=error_msg, **payment_kwargs)
  504. signals.post_payment.send_robust(sender=self, view=self)
  505. # If all is ok with payment, try and place order
  506. logger.info("Order #%s: payment successful, placing order",
  507. order_number)
  508. try:
  509. return self.handle_order_placement(
  510. order_number, user, basket, shipping_address, shipping_method,
  511. order_total, **order_kwargs)
  512. except UnableToPlaceOrder as e:
  513. # It's possible that something will go wrong while trying to
  514. # actually place an order. Not a good situation to be in as a
  515. # payment transaction may already have taken place, but needs
  516. # to be handled gracefully.
  517. msg = six.text_type(e)
  518. logger.error("Order #%s: unable to place order - %s",
  519. order_number, msg, exc_info=True)
  520. self.restore_frozen_basket()
  521. return self.render_preview(
  522. self.request, error=msg, **payment_kwargs)
  523. # =========
  524. # Thank you
  525. # =========
  526. class ThankYouView(generic.DetailView):
  527. """
  528. Displays the 'thank you' page which summarises the order just submitted.
  529. """
  530. template_name = 'checkout/thank_you.html'
  531. context_object_name = 'order'
  532. def get_object(self):
  533. # We allow superusers to force an order thank-you page for testing
  534. order = None
  535. if self.request.user.is_superuser:
  536. if 'order_number' in self.request.GET:
  537. order = Order._default_manager.get(
  538. number=self.request.GET['order_number'])
  539. elif 'order_id' in self.request.GET:
  540. order = Order._default_manager.get(
  541. id=self.request.GET['order_id'])
  542. if not order:
  543. if 'checkout_order_id' in self.request.session:
  544. order = Order._default_manager.get(
  545. pk=self.request.session['checkout_order_id'])
  546. else:
  547. raise http.Http404(_("No order found"))
  548. return order