gc_integration.py 7.1 KB

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