Exemplo n.º 1
0
    def handle_notification(self, message, order_id, order, context):
        """
        Handle the rest of the messages.
        This Demostrates that it is not neccessary to override specific
        handlers -- everything could be done inside this one handler.
        """
        assert order is not None

        if message.__class__ == gmodel.authorization_amount_notification_t:
            order.authorized += message.authorization_amount.value

        elif message.__class__ == gmodel.risk_information_notification_t:
            # do nothing -- ignore the message (but process it)
            return gmodel.ok_t()

        elif message.__class__ == gmodel.refund_amount_notification_t:
            order.refunds += message.latest_refund_amount.value
            if order.refunds != message.total_refund_amount.value:
                raise AssertionError('Total refunded amount does not match!')

        elif message.__class__ == gmodel.chargeback_amount_notification_t:
            order.chargebacks += message.latest_chargeback_amount.value
            if order.chargebacks != message.total_chargeback_amount.value:
                raise AssertionError('Total chargeback amount does not match!')

        else:
            # unknown message - return None to indicate that we can't process it
            return None

        order.save()

        return gmodel.ok_t()
Exemplo n.º 2
0
    def handle_charge_amount(self, message, order_id, order, context):
        """
        GC has charged this order -- great! Save it.
        """
        assert order is not None

        order.charges = float(order.charges) + message.latest_charge_amount.value
        if order.charges != message.total_charge_amount.value:
            raise AssertionError, 'Total charged amount does not match!'
        order.save()
        if order.charges == order.total:
            if order.state == 'NEW':
                # process order
                order.state = 'PROCESSING'
                self.process_order(order_id)
                order.save()
                # deliver order
                order.state = 'DELIVERED'
                self.deliver_order(order.google_id,
                                   carrier=None,
                                   tracking_number=None,
                                   send_email=True)
                order.save()

        return gmodel.ok_t()
Exemplo n.º 3
0
 def handle_charge_amount(self, message, order_id, order, context):
     assert order is not None
     if order.state == 'NEW':
         order.state = 'PROCESSING'
         order.local_status = '0'
         order.is_active = None
         self.process_order(order_id)
         order.save()
     return gmodel.ok_t()
Exemplo n.º 4
0
 def handle_charge_amount(self, message, order_id, order, context):
     assert order is not None
     if order.state == 'NEW':
         order.state = 'PROCESSING'
         order.local_status = '0'
         order.is_active = None
         self.process_order(order_id)
         order.save()
     return gmodel.ok_t()
Exemplo n.º 5
0
    def handle_new_order(self, message, order_id, order, context):
        """
        Create a new Order instance in the database if everything is ok.
        """
        # First verify the currency
        if message.order_total.currency != settings.GCHECKOUT_CURRENCY:
            raise Exception("Currency mismatch! %s != %s" % (
                             message.order_total.currency,
                             settings.GCHECKOUT_CURRENCY
                             ))
        # Check if the order already exist in the database
        if order is not None:
            raise Exception("Order with google_id('%s') already exists" % (order_id,))

        #TODO
        from gchecky_common.models import ORDER_NATURE
        merchant_data = message.shopping_cart.merchant_private_data
        nature = 'unknown'
        for (ntag, ntitle) in ORDER_NATURE:
            if ntag == merchant_data:
                nature = ntag

        # Good to create a fresh new order in DB
        if message.buyer_billing_address.contact_name is not None:
            owner_id = message.buyer_billing_address.contact_name
        elif message.buyer_billing_address.company_name is not None:
            owner_id = message.buyer_billing_address.company_name
        else:
            owner_id = message.buyer_id
        order = Order(user_id   = owner_id,
                      nature    = nature,
                      google_id = order_id,
                      cart_xml  = message.toxml(),
                      state     = message.fulfillment_order_state,
                      payment   = message.financial_order_state,
                      currency  = message.order_total.currency,
                      total     = message.order_total.value)

        # parse cart items
        purchases = [Purchase(order       = order,
                              item_xml    = 'TODO',
                              title       = item.name,
                              brief       = item.description,
                              price       = item.unit_price.value,
                              currency    = item.unit_price.currency,
                              quantity    = item.quantity,
                              merchant_id = item.merchant_item_id,
                              merchant_data = str(item.merchant_private_item_data))
                 for item in message.shopping_cart.items]

        order.save()
        for purchase in purchases:
            purchase.order = order
            purchase.save()

        return gmodel.ok_t()
Exemplo n.º 6
0
 def handle_new_order(self, message, order_id, order, context):
     from store.models import Cart
     #XXX  set the Cart's cart_xml and google_id here, have to get that somehow
     cart = Cart.objects.get(pk = int(message.shopping_cart.merchant_private_data))
     cart.google_id = order_id,
     cart.cart_xml = message.toxml()
     cart.state = message.fulfillment_order_state
     cart.payment = message.financial_order_state
     cart.save()
     return gmodel.ok_t()
Exemplo n.º 7
0
 def handle_new_order(self, message, order_id, order, context):
     from store.models import Cart
     #XXX  set the Cart's cart_xml and google_id here, have to get that somehow
     cart = Cart.objects.get(
         pk=int(message.shopping_cart.merchant_private_data))
     cart.google_id = order_id,
     cart.cart_xml = message.toxml()
     cart.state = message.fulfillment_order_state
     cart.payment = message.financial_order_state
     cart.save()
     return gmodel.ok_t()
Exemplo n.º 8
0
    def handle_order_state_change(self, message, order_id, order, context):
        assert order is not None
        if message.new_fulfillment_order_state != message.previous_fulfillment_order_state:
            order.state = message.new_fulfillment_order_state
        if message.new_financial_order_state != message.previous_financial_order_state:
            order.state = message.new_financial_order_state
        order.save

        if order.state == 'NEW' and order.payment == 'CHARGEABLE':
            self.charge_order(order_id, order.get_total())

        return gmodel.ok_t()
Exemplo n.º 9
0
    def handle_order_state_change(self, message, order_id, order, context):
        assert order is not None
        if message.new_fulfillment_order_state != message.previous_fulfillment_order_state:
            order.state = message.new_fulfillment_order_state
        if message.new_financial_order_state != message.previous_financial_order_state:
            order.state = message.new_financial_order_state
        order.save

        if order.state == 'NEW' and order.payment == 'CHARGEABLE':
            self.charge_order(order_id, order.get_total())

        return gmodel.ok_t()
Exemplo n.º 10
0
    def handle_order_state_change(self, message, order_id, order, context):
        """
        React to the order state change.
        """
        assert order is not None
        if message.new_fulfillment_order_state != message.previous_financial_order_state:
            order.state = message.new_fulfillment_order_state
        if message.new_financial_order_state != message.previous_financial_order_state:
            order.payment = message.new_financial_order_state
        order.save()

        if order.state == 'NEW':
            if order.payment == 'CHARGEABLE':
                if order.total > order.charges_pending:
                    if self.automatically_charge:
                        # Only send charge command if your account does not
                        # tell google to do it automatically.
                        amount = order.total - (order.charges + order.charges_pending)
                        order.charges_pending += amount
                        self.charge_order(order_id, amount)
                        order.save()

        return gmodel.ok_t()