gc_integration.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """ Google checkout integration for django-merchant """
  2. import logging
  3. from billing.integrations.google_checkout_integration import (
  4. GoogleCheckoutIntegration as Integration)
  5. from billing.models import GCNewOrderNotification
  6. from billing import signals
  7. from store_order.models import Order, OrderPayment
  8. LOGGER = logging.getLogger('gc_integration')
  9. BUTTON_SANDBOX_URL_SSL = 'https://sandbox.google.com/checkout/buttons/checkout.gif?merchant_id=%(merchant_id)s&w=%(width)s&h=%(height)s&style=white&variant=text&loc=en_US'
  10. BUTTON_URL_SSL = 'https://checkout.google.com/checkout/buttons/checkout.gif?merchant_id=%(merchant_id)s&w=%(width)s&h=%(height)s&style=white&variant=text&loc=en_US'
  11. class GcIntegration(Integration):
  12. # remove once this is fixed in django-merchant
  13. def button_image_url(self):
  14. params = {"merchant_id": self.merchant_id,
  15. "width": self.button_width,
  16. "height": self.button_height}
  17. if self.test_mode:
  18. return BUTTON_SANDBOX_URL_SSL % params
  19. return BUTTON_URL_SSL % params
  20. def gc_order_state_change_notification(self, request):
  21. """ used when a the GC order changed state """
  22. LOGGER.debug("[gc_order_state_change_notification] new update")
  23. post_data = request.POST.copy()
  24. #TODO, clean this up, only for testing.
  25. LOGGER.debug("[gc_order_state_change_notification] data = {0}".format(
  26. post_data))
  27. try:
  28. order = GCNewOrderNotification.objects.get(
  29. google_order_number=post_data['google-order-number'])
  30. except GCNewOrderNotification.DoesNotExist:
  31. LOGGER.info("[gc_order_state_change_notification] object not found!")
  32. return
  33. order.financial_order_state = post_data['new-financial-order-state']
  34. try:
  35. order_payments = order.order_payment.all()
  36. if order_payments:
  37. order_payment = order_payments[0]
  38. orig_order = order_payment.order
  39. else:
  40. orig_order = None
  41. except Exception as exp:
  42. LOGGER.error("[gc_order_state_change_notification] Error = {0}".format(exp))
  43. orig_order = None
  44. # Send success signal when order state is charged
  45. if order.financial_order_state == 'CHARGED':
  46. signals.transaction_was_successful.send(sender=self.__class__,
  47. type="purchase",
  48. response=order)
  49. if orig_order:
  50. LOGGER.debug("[gc_order_state_change_notification] status changed to processed")
  51. orig_order.status = Order.PROCESSED
  52. orig_order.save()
  53. # Ideally we'd call this in a signal receiver above, but
  54. # I wasn't able to get the signal to work.
  55. from storefront.subscriptions import send_subscription_email
  56. send_subscription_email(orig_order)
  57. elif order.financial_order_state in ['CANCELLED', 'CANCELLED_BY_GOOGLE']:
  58. if orig_order:
  59. LOGGER.debug("[gc_order_state_change_notification] status changed to cancelled")
  60. orig_order.status = Order.CANCELLED
  61. orig_order.save()
  62. order.fulfillment_order_state = post_data['new-fulfillment-order-state']
  63. if order.fulfillment_order_state == 'DELIVERED':
  64. if orig_order:
  65. LOGGER.debug("[gc_order_state_change_notification] status changed to shipped")
  66. orig_order.status = Order.SHIPPED
  67. orig_order.save()
  68. order.save()
  69. def gc_new_order_notification(self, request):
  70. """ new GC order coming in"""
  71. LOGGER.debug("New GC Order")
  72. post_data = request.POST.copy()
  73. data = {}
  74. resp_fields = {
  75. "_type": "notify_type",
  76. "serial-number": "serial_number",
  77. "google-order-number": "google_order_number",
  78. "buyer-id": "buyer_id",
  79. "buyer-shipping-address.contact-name": "shipping_contact_name",
  80. "buyer-shipping-address.address1": "shipping_address1",
  81. "buyer-shipping-address.address2": "shipping_address2",
  82. "buyer-shipping-address.city": "shipping_city",
  83. "buyer-shipping-address.postal-code": "shipping_postal_code",
  84. "buyer-shipping-address.region": "shipping_region",
  85. "buyer-shipping-address.country-code": "shipping_country_code",
  86. "buyer-shipping-address.email": "shipping_email",
  87. "buyer-shipping-address.company-name": "shipping_company_name",
  88. "buyer-shipping-address.fax": "shipping_fax",
  89. "buyer-shipping-address.phone": "shipping_phone",
  90. "buyer-billing-address.contact-name": "billing_contact_name",
  91. "buyer-billing-address.address1": "billing_address1",
  92. "buyer-billing-address.address2": "billing_address2",
  93. "buyer-billing-address.city": "billing_city",
  94. "buyer-billing-address.postal-code": "billing_postal_code",
  95. "buyer-billing-address.region": "billing_region",
  96. "buyer-billing-address.country-code": "billing_country_code",
  97. "buyer-billing-address.email": "billing_email",
  98. "buyer-billing-address.company-name": "billing_company_name",
  99. "buyer-billing-address.fax": "billing_fax",
  100. "buyer-billing-address.phone": "billing_phone",
  101. "buyer-marketing-preferences.email-allowed": "marketing_email_allowed",
  102. "order-adjustment.total-tax": "total_tax",
  103. "order-adjustment.total-tax.currency": "total_tax_currency",
  104. "order-adjustment.adjustment-total": "adjustment_total",
  105. "order-adjustment.adjustment-total.currency": "adjustment_total_currency",
  106. "order-total": "order_total",
  107. "order-total.currency": "order_total_currency",
  108. "financial-order-state": "financial_order_state",
  109. "fulfillment-order-state": "fulfillment_order_state",
  110. "timestamp": "timestamp",
  111. "shopping-cart.merchant-private-data": "private_data",
  112. }
  113. for (key, val) in resp_fields.iteritems():
  114. data[val] = post_data.get(key, '')
  115. data['num_cart_items'] = len(post_data.getlist('shopping-cart.items'))
  116. data['cart_items'] = self.gc_cart_items_blob(post_data)
  117. resp = GCNewOrderNotification.objects.create(**data)
  118. try:
  119. order_id = int(resp.private_data)
  120. order = Order.objects.get(pk=order_id)
  121. order_payment = OrderPayment()
  122. order_payment.order = order
  123. order_payment.google_payment = resp
  124. order_payment.save()
  125. #order.orderpayment = order_payment
  126. order.status = Order.SUBMITTED
  127. #TODO: Make sure status is set correctly.
  128. order.save()
  129. LOGGER.debug("New GC Order saved!")
  130. except Exception as exc:
  131. #TODO log error, send email.
  132. LOGGER.error("[gc_new_order_notification] Error = {0}".format(exc))