chickadee » correios

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

correios

Description

Correios is a Chicken Scheme library that implements shipping calculation (tax and estimated delivery time) for a Brazillian post office service with the same name. It is implemented according to the official Correios web service specification.

Author

Arthur Maciel

Requirements

Requires the defstruct, http-client, ssax and uri-common eggs and the srfi-1 unit.

Documentation

Requests

make-request #!key service from-zip to-zip (company ) (password ) (pkg-weight 0.3) (pkg-format 1) (pkg-length 16) (pkg-height 2) (pkg-breadth 11) (pkg-diameter 0) (receiver-id-check #f) (declared-value 0) (return-receipt #f) (return-url XML)procedure

Create a correios request object (a defstruct record) to be processed. It has many attributes that correspond to the official Correios web service specification:

SERVICE
available delivery services: 'PAC, 'SEDEX, 'SEDEX-10, 'SEDEX-HOJE and 'SEDEX-A-COBRAR. It can also be a list combining one or more of these symbols (ie. '(PAC SEDEX SEDEX-10)).
FROM-ZIP
sender's zip code and should be a string with 8 digits (only numbers allowed).
TO-ZIP
receiver's zip code and should be a string with 8 digits (only numbers allowed).
COMPANY
company code, if yours has one. It should be a string.
PASSWORD
company password, only if a company code is provided. It should be a string.
PKG-WEIGHT
package weight in kilograms. Should be a number.
PKG-FORMAT
package format and should be a number: 1 for box/package, 2 for roller/prism and 3 for envelope.
PKG-LENGTH
package length in centimeters (including packing size) and should be a number.
PKG-HEIGHT
package height in centimeters (including packing size) and should be a number.
PKG-BREADTH
package breadth in centimeters (including packing size) and should be a number.
PKG-DIAMETER
package diameter in centimeters (including packing size) and should be a number.
RECEIVER-ID-CHECK
a boolean and determines whether the package should be delivered only to the receiver's hand (Correios calls this service "Mão própria").
DECLARED-VALUE
package declared value in Brazillian Reais, to be returned in case of miscarriage. It should be a number - ex.: 33.50 (default is 0, which means no use of service).
RETURN-RECEIPT
a boolean and determines whether the package should be delivered with additional service of a return receipt (#f means no use of the service).
RETURN-URL
request return URL or type - at the moment can only be "XML".

Note: although all values returned use Brazillian decimal notation with a comma separating cents from integers (ex.: 3,25), all the decimal values provided to make-request should have a dot (ex.: 3.25) separating those parts.

update-request old-request #!key service from-zip to-zip company password pkg-weight pkg-format pkg-length pkg-height pkg-breadth pkg-diameter receiver-id-check declared-value return-receipt return-urlprocedure

Like make-request, except this takes an old-request object as a template for values which are missing from the parameter list, thereby providing a way to do a purely functional update of that object.

process-request REQUESTprocedure

This is the core procedure for shipping cost calculation. It accepts a request object as a parameter and returns a list of response objects. The list can be processed (see Examples below) and data can be extracted from each response using its accessors (see response details below).

For each request attribute there is a respective accessor procedure:

request-service REQUESTprocedure
request-from-zip REQUESTprocedure
request-to-zip REQUESTprocedure
request-company REQUESTprocedure
request-password REQUESTprocedure
request-pkg-weight REQUESTprocedure
request-pkg-format REQUESTprocedure
request-pkg-length REQUESTprocedure
request-pkg-height REQUESTprocedure
request-pkg-breadth REQUESTprocedure
request-pkg-diameter REQUESTprocedure
request-receiver-id-check REQUESTprocedure
request-declared-value REQUESTprocedure
request-return-receipt REQUESTprocedure
request-return-url REQUESTprocedure

Responses

responserecord

This is a correios shipping response object that is generated by process-request. It is a defstruct record and has a constructor make-response (used only internally). It has 10 attributes that can be retrieved using the respective accessor procedure:

response-service RESPONSEprocedure

Returns the delivery service type as a symbol - ie. 'PAC, 'SEDEX or SEDEX-10).

response-cost RESPONSEprocedure

Returns the total delivery cost in Brazillian Reais as a string - ie. "15,80".

response-delivery-time RESPONSEprocedure

Returns the delivery time in days as a number.

response-receiver-id-check-cost RESPONSEprocedure

Returns the cost of the receiver-id-check service (if used) in Brazillian Reais as a string - ie. "3,20".

response-return-receipt-cost RESPONSEprocedure

Returns the cost of the return-receipt-cost service (if used) in Brazillian Reais as a string - ie. "2,90".

response-declared-value-cost RESPONSEprocedure

Returns the cost of declared-value service in Brazillian Reais as a string - ie. "32,50".

response-home-delivery RESPONSEprocedure

Returns a boolean indicating whether or not the home-delivery service is available (believe it or not there are still many inaccessible areas in Brazil!).

response-saturday-delivery RESPONSEprocedure

Returns a boolean indicating whether or not the saturday-delivery service is available.

response-error RESPONSEprocedure

Returns "0" if there were no errors or other number (in a string format) if so (see pages 14 and 15 of the Correios web service specification for a complete error code list).

response-error-msg RESPONSEprocedure

Returns a #f if there were no errors or a string with the error message.

There is also a procedure to check if there response generated by process-request is valid:

valid-response? RESPONSEprocedure

Check if a specific response has generated errors. Returns a boolean.

Examples

(use correios)

;; Create a request
(define ship-req (make-request 
		  service: (list 'SEDEX 'PAC)
		  from-zip: "05412002" 
		  to-zip: "90035120"))

;; Proccess it and iterate along the reponse list
(let loop ((responses (process-request ship-req)))
  (cond ((null? responses) 
	 (printf "Finished processing responses.~N"))
	(else
	 (let ((resp (car responses)))
	   (if (valid-response? resp)
	       (printf "Service ~A - Cost: R$~A  Estimated delivery (days): ~A~N" 
		 (response-service resp)
		 (response-cost resp)
		 (response-delivery-time resp))
	       (printf "Service ~A - Error:  ~A" 
		 (response-service resp)
		 (response-error-msg resp))))
	 (loop (cdr responses)))))
=> 
Service SEDEX - Cost: R$33,00  Estimated delivery (days): 1
Service PAC - Cost: R$15,80  Estimated delivery (days): 4
Finished processing responses.

Changelog

License

 Copyright (c) 2013, Arthur Maciel
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
 
 Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 
 Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 
 Neither the name of the author nor the names of its contributors may
 be used to endorse or promote products derived from this software
 without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

Contents »