views.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """ Checkout views """
  2. from django.shortcuts import render_to_response
  3. from django.template import RequestContext
  4. from django.core.urlresolvers import reverse
  5. from django.http import HttpResponseRedirect
  6. from django.contrib.sites.models import RequestSite
  7. from store_order.models import Order
  8. from cart import cart
  9. from .checkout import create_order
  10. from billing import get_integration
  11. gc = get_integration("google_checkout")
  12. # fps = get_integration("fps")
  13. def checkout(request, template_name="checkout/confirm.html"):
  14. """ page displayed with order information before order is placed. """
  15. shopping_cart = cart.get_cart(request)
  16. if cart.is_empty(request):
  17. return HttpResponseRedirect(reverse("shopping_cart"))
  18. order = create_order(request)
  19. items = []
  20. for item in shopping_cart.cart_items:
  21. item_dict = {
  22. "amount": item.unit_price,
  23. "name": item.name,
  24. "description": item.description,
  25. "currency": "USD",
  26. "id": item.sku,
  27. "quantity": item.quantity,
  28. }
  29. if item.product.tax_free:
  30. item_dict["tax-table-selector"] = "TAX_EXEMPT"
  31. items.append(item_dict)
  32. shipping_costs = [
  33. {
  34. "shipping_type": "flat-rate-shipping",
  35. "name": "USPS",
  36. "currency": "USD",
  37. "price": shopping_cart.cart_shipping_total,
  38. }
  39. ]
  40. gc_taxes = {
  41. "default-tax-table": {
  42. "tax-rules": [
  43. {
  44. "shipping-taxed": False,
  45. "rate": 0.055,
  46. "tax-area": {"us-state-area": ["ME"]},
  47. }
  48. ]
  49. },
  50. "alternate-tax-tables": [{"name": "TAX_EXEMPT", "standalone": True}],
  51. }
  52. private_data = order.pk
  53. url_scheme = "http"
  54. if request.is_secure():
  55. url_scheme = "https"
  56. gc_return_url = "%s://%s%s" % (
  57. url_scheme,
  58. RequestSite(request).domain,
  59. reverse("checkout_receipt"),
  60. )
  61. gc.add_fields(
  62. {
  63. "items": items,
  64. "return_url": gc_return_url,
  65. "shipping-methods": shipping_costs,
  66. "private_data": private_data,
  67. "tax-tables": gc_taxes,
  68. }
  69. )
  70. # caller_reference = shopping_cart.cart_id
  71. # print("callerReference = {0}".format(caller_reference))
  72. # fields = {
  73. # "itemTotal": shopping_cart.cart_subtotal,
  74. # "transactionAmount": shopping_cart.cart_total,
  75. # "pipelineName": "SingleUse",
  76. # "shipping": shopping_cart.cart_shipping_total,
  77. # "callerReference": caller_reference,
  78. # 'collectShippingAddress': True,
  79. # "paymentReason": shopping_cart.purchase_description,
  80. # "paymentPage": request.build_absolute_uri(),
  81. # "returnURL": "%s://%s%s" % (url_scheme,
  82. # RequestSite(request).domain,
  83. # reverse("fps_return_url"))
  84. # }
  85. # print("Fields = {0}".format(fields))
  86. # Save the fps.fields["callerReference"] in the db along with
  87. # the amount to be charged or use the user's unique id as
  88. # the callerReference so that the amount to be charged is known
  89. # Or save the callerReference in the session and send the user
  90. # to FPS and then use the session value when the user is back.
  91. # fps.add_fields(fields)
  92. context = {
  93. "gc": gc,
  94. "shopping_cart": shopping_cart,
  95. #'fps': fps,
  96. "order": order,
  97. }
  98. # print("View session_key = {0}".format(request.session.session_key))
  99. return render_to_response(
  100. template_name, context, context_instance=RequestContext(request)
  101. )
  102. def receipt(request, template_name="checkout/receipt.html"):
  103. """ page displayed with order information after an order
  104. has been placed successfully """
  105. shopping_cart = cart.get_cart(request)
  106. try:
  107. order = Order.objects.get(cart_id=shopping_cart.cart_id)
  108. except Order.DoesNotExist:
  109. return HttpResponseRedirect(reverse("shopping_cart"))
  110. context = {
  111. "shopping_cart": shopping_cart,
  112. "order": order,
  113. }
  114. # flush the session so that we can't get back
  115. request.session.flush()
  116. return render_to_response(
  117. template_name, context, context_instance=RequestContext(request)
  118. )