def get_endicia_shipping_cost(self):
        """Returns the calculated shipping cost as sent by endicia

        :returns: The shipping cost in USD
        """
        endicia_credentials = self.company.get_endicia_credentials()

        if not self.endicia_mailclass:
            self.raise_user_error('mailclass_missing')

        calculate_postage_request = CalculatingPostageAPI(
            mailclass=self.endicia_mailclass.value,
            weightoz=sum(
                map(lambda move: move.get_weight_for_endicia(),
                    self.outgoing_moves)),
            from_postal_code=self.warehouse.address.zip,
            to_postal_code=self.delivery_address.zip,
            to_country_code=self.delivery_address.country.code,
            accountid=endicia_credentials.account_id,
            requesterid=endicia_credentials.requester_id,
            passphrase=endicia_credentials.passphrase,
            test=endicia_credentials.usps_test,
        )

        response = calculate_postage_request.send_request()

        return Decimal(
            objectify_response(response).PostagePrice.get('TotalAmount'))
    def get_endicia_shipping_cost(self, mailclass=None):
        """Returns the calculated shipping cost as sent by endicia

        :param mailclass: endicia mailclass for which cost to be fetched

        :returns: The shipping cost in USD
        """
        Carrier = Pool().get('carrier')
        EndiciaConfiguration = Pool().get('endicia.configuration')

        endicia_credentials = EndiciaConfiguration(1).get_endicia_credentials()
        carrier, = Carrier.search(['carrier_cost_method', '=', 'endicia'])

        if not mailclass and not self.endicia_mailclass:
            self.raise_user_error('mailclass_missing')

        from_address = self._get_ship_from_address()
        to_address = self.shipment_address
        to_zip = to_address.zip

        if to_address.country and to_address.country.code == 'US':
            # Domestic
            to_zip = to_zip and to_zip[:5]
        else:
            # International
            to_zip = to_zip and to_zip[:15]

        # Endicia only support 1 decimal place in weight
        weight_oz = "%.1f" % self.package_weight
        calculate_postage_request = CalculatingPostageAPI(
            mailclass=mailclass or self.endicia_mailclass.value,
            MailpieceShape=self.endicia_mailpiece_shape,
            weightoz=weight_oz,
            from_postal_code=from_address.zip and from_address.zip[:5],
            to_postal_code=to_zip,
            to_country_code=to_address.country and to_address.country.code,
            accountid=endicia_credentials.account_id,
            requesterid=endicia_credentials.requester_id,
            passphrase=endicia_credentials.passphrase,
            test=endicia_credentials.is_test,
        )

        # Logging.
        logger.debug('Making Postage Request for shipping cost of'
                     'Sale ID: {0} and Carrier ID: {1}'.format(
                         self.id, carrier.id))
        logger.debug('--------POSTAGE REQUEST--------')
        logger.debug(str(calculate_postage_request.to_xml()))
        logger.debug('--------END REQUEST--------')

        try:
            response = calculate_postage_request.send_request()
        except RequestError, e:
            self.raise_user_error(unicode(e))
Пример #3
0
    def get_endicia_shipping_cost(self):
        """Returns the calculated shipping cost as sent by endicia

        :returns: The shipping cost in USD
        """
        Carrier = Pool().get('carrier')

        carrier, = Carrier.search(['carrier_cost_method', '=', 'endicia'])

        if not self.endicia_mailclass:
            self.raise_user_error('mailclass_missing')

        from_address = self._get_ship_from_address()
        to_address = self.delivery_address
        to_zip = to_address.zip

        if to_address.country and to_address.country.code == 'US':
            # Domestic
            to_zip = to_zip and to_zip[:5]
        else:
            # International
            to_zip = to_zip and to_zip[:15]

        # Endicia only support 1 decimal place in weight
        weight_oz = "%.1f" % self.weight
        calculate_postage_request = CalculatingPostageAPI(
            mailclass=self.endicia_mailclass.value,
            weightoz=weight_oz,
            from_postal_code=from_address.zip and from_address.zip[:5],
            to_postal_code=to_zip,
            to_country_code=to_address.country and to_address.country.code,
            accountid=carrier.endicia_account_id,
            requesterid=carrier.endicia_requester_id,
            passphrase=carrier.endicia_passphrase,
            test=carrier.endicia_is_test,
        )
        calculate_postage_request.mailpieceshape = self.endicia_mailpiece_shape

        # Logging.
        logger.debug('Making Postage Request for'
                     'Shipment ID: {0} and Carrier ID: {1}'.format(
                         self.id, carrier.id))
        logger.debug('--------POSTAGE REQUEST--------')
        logger.debug(str(calculate_postage_request.to_xml()))
        logger.debug('--------END REQUEST--------')

        try:
            response = calculate_postage_request.send_request()
        except RequestError, error:
            self.raise_user_error('error_label', error_args=(error, ))
Пример #4
0
 def test0030_calculating_postage_request(self):
     calculate_postage_request = CalculatingPostageAPI(
         mailclass='First',
         weightoz=10.00,
         from_postal_code="83702",
         to_postal_code="84301",
         to_country_code="US",
         requesterid=REQUESTER_ID,
         accountid=ACCOUNT_ID,
         passphrase=PASSPHRASE,
         test=True,
     )
     print calculate_postage_request.to_xml()
     response = calculate_postage_request.send_request()
     print objectify_response(response)