views.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 = {"amount": item.unit_price,
  22. "name": item.name,
  23. "description": item.description,
  24. "currency": "USD",
  25. "id": item.sku,
  26. "quantity": item.quantity}
  27. if item.product.tax_free:
  28. item_dict['tax-table-selector'] = 'TAX_EXEMPT'
  29. items.append(item_dict)
  30. shipping_costs = [
  31. {'shipping_type': 'flat-rate-shipping',
  32. 'name': "USPS",
  33. 'currency': "USD",
  34. 'price': shopping_cart.cart_shipping_total}
  35. ]
  36. gc_taxes = {'default-tax-table': {
  37. 'tax-rules': [
  38. {'shipping-taxed': False,
  39. 'rate': 0.055,
  40. 'tax-area': {'us-state-area': ['ME']}}
  41. ]},
  42. 'alternate-tax-tables': [
  43. {'name': 'TAX_EXEMPT',
  44. 'standalone': True}
  45. ]
  46. }
  47. private_data = order.pk
  48. url_scheme = "http"
  49. if request.is_secure():
  50. url_scheme = "https"
  51. gc_return_url = "%s://%s%s" % (url_scheme,
  52. RequestSite(request).domain,
  53. reverse("checkout_receipt"))
  54. gc.add_fields({'items': items,
  55. "return_url": gc_return_url,
  56. 'shipping-methods': shipping_costs,
  57. 'private_data': private_data,
  58. 'tax-tables': gc_taxes})
  59. # caller_reference = shopping_cart.cart_id
  60. # print("callerReference = {0}".format(caller_reference))
  61. # fields = {
  62. # "itemTotal": shopping_cart.cart_subtotal,
  63. # "transactionAmount": shopping_cart.cart_total,
  64. # "pipelineName": "SingleUse",
  65. # "shipping": shopping_cart.cart_shipping_total,
  66. # "callerReference": caller_reference,
  67. # 'collectShippingAddress': True,
  68. # "paymentReason": shopping_cart.purchase_description,
  69. # "paymentPage": request.build_absolute_uri(),
  70. # "returnURL": "%s://%s%s" % (url_scheme,
  71. # RequestSite(request).domain,
  72. # reverse("fps_return_url"))
  73. # }
  74. # print("Fields = {0}".format(fields))
  75. # Save the fps.fields["callerReference"] in the db along with
  76. # the amount to be charged or use the user's unique id as
  77. # the callerReference so that the amount to be charged is known
  78. # Or save the callerReference in the session and send the user
  79. # to FPS and then use the session value when the user is back.
  80. # fps.add_fields(fields)
  81. context = {'gc': gc,
  82. 'shopping_cart': shopping_cart,
  83. #'fps': fps,
  84. 'order': order}
  85. #print("View session_key = {0}".format(request.session.session_key))
  86. return render_to_response(template_name, context,
  87. context_instance=RequestContext(request))
  88. def receipt(request, template_name='checkout/receipt.html'):
  89. """ page displayed with order information after an order
  90. has been placed successfully """
  91. shopping_cart = cart.get_cart(request)
  92. try:
  93. order = Order.objects.get(cart_id=shopping_cart.cart_id)
  94. except Order.DoesNotExist:
  95. return HttpResponseRedirect(reverse('shopping_cart'))
  96. context = {
  97. 'shopping_cart': shopping_cart,
  98. 'order': order,
  99. }
  100. # flush the session so that we can't get back
  101. request.session.flush()
  102. return render_to_response(template_name, context,
  103. context_instance=RequestContext(request))