Beispiel #1
0
    def test_correios_get_shipping_rates_single_service_errors(self):
        correios = Correios()

        package = BoxPackage()
        package.add_item(1.0, 2.0, 3.0, 0.3)

        origin = '10000000'
        destination = '30000000'

        result = correios.get_shipping_rates(origin, destination, package,
                                             [Correios.SERVICE_PAC])

        self.assertTrue(result.has_errors())
Beispiel #2
0
    def test_correios_get_shipping_rates_multiple_service_success(self):
        correios = Correios()

        package = BoxPackage()
        package.add_item(1.0, 2.0, 3.0, 0.3)

        origin = '10000000'
        destination = '30000000'

        result = correios.get_shipping_rates(
            origin, destination, package,
            [Correios.SERVICE_PAC, Correios.SERVICE_SEDEX])

        self.assertFalse(result.has_errors())
        self.assertEqual(origin, result.origin)
        self.assertEqual(destination, result.destination)
Beispiel #3
0
    def test_correios_get_shipping_rates_single_service_success_with_contract(
            self):
        correios = Correios()
        correios.set_credentials('someuser', 'somepass')

        package = BoxPackage()
        package.add_item(1.0, 2.0, 3.0, 0.3)

        origin = '10000000'
        destination = '30000000'

        result = correios.get_shipping_rates(origin, destination, package,
                                             [Correios.SERVICE_PAC])

        self.assertFalse(result.has_errors())
        self.assertEqual(origin, result.origin)
        self.assertEqual(destination, result.destination)
def options():
    # getting current request body
    logger.info("Options got requested with the following body")
    logger.info(request.get_data(as_text=True))
    req = request.get_json()

    # initializing correios client
    correios = Correios()
    available_services = [
        config.OPTION_PAC_SERVICE, config.OPTION_SEDEX_SERVICE
    ]

    # getting cart specs for shipping costs calculation
    origin = req.get('origin').get('postal_code')
    destination = req.get('destination').get('postal_code')
    items = req.get('items')

    # getting base shipping costs
    # creating a correios package
    package = reduce(item_to_package_item, items, BoxPackage())
    logger.info('CORREIOS PACKAGE')
    logger.info(package.api_format())

    # requesting rates from webservice
    rates = correios.get_shipping_rates(origin=origin,
                                        destination=destination,
                                        package=package,
                                        services=available_services)

    # checking for errors
    if rates.has_errors():
        # logging errors and returning result early
        for service in rates.services:
            code = service.code
            error = service.error_code
            message = service.error_message
            logger.warn(
                'The service {} returned the error {} with message: {}'.format(
                    code, error, message))
        return abort(400)

    # checking for free shipping items in cart
    free_shipping_items_qty = reduce(
        lambda c, i: c + 1 if i.get('free_shipping') else 0, items, 0)
    free_shipping_cart = free_shipping_items_qty == len(items)

    if free_shipping_items_qty > 0 and not free_shipping_cart:
        # performing another shipping costs calculation in order to get the consumer prices
        non_free_shipping_items = [
            item for item in items if item.get('free_shipping') is not True
        ]
        non_free_shipping_package = reduce(item_to_package_item,
                                           non_free_shipping_items,
                                           BoxPackage())

        # requesting rates from webservice
        non_free_shipping_rates = correios.get_shipping_rates(
            origin=origin,
            destination=destination,
            package=non_free_shipping_package,
            services=available_services)

        if non_free_shipping_rates.has_errors():
            for service in non_free_shipping_rates.services:
                code = service.code
                error = service.error_code
                message = service.error_message
                logger.warn(
                    'The service {} returned the error {} with message: {}'.
                    format(code, error, message))
            non_free_shipping_rates = rates
    else:
        non_free_shipping_rates = rates

    # parsing service rates to the shipping option format
    options = list(
        map(lambda s: rate_to_shipping_option(s, free_shipping_cart),
            zip(rates.services, non_free_shipping_rates.services)))

    return json.jsonify({'rates': options})