You are looking at historical revision 30199 of this page. It may differ significantly from its current revision.
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.
The git repository for the correios source code is hosted by GitHub: https://github.com/arthurmaciel/correios.
This is a correios request object to be created and processed. It is a defstruct record and has a constructor make-request. It has many accessors that correspond to the official Correios web service specification:
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 the constructor should have a dot (ex.: 3.25) separating those parts.
- Available delivery services - PAC, SEDEX, SEDEX-10, SEDEX-HOJE and SEDEX-A-COBRAR. Should be a symbol or a list of symbols - ex.: 'PAC or '(PAC SEDEX SEDEX-10).
- Sender zip code. Should be a string with 8 digits (only numbers are allowed).
- Receiver zip code. Should be a string with 8 digits (only numbers are allowed).
- Company code, if yours has one. Should be a string (default is "").
- Company password, only if a company code is provided. Should be a string (default is "").
- Package weight in kilograms. Should be a number (default is 0.3).
- Package format. Should be a number: 1 for box/package, 2 for roller/prism and 3 for envelope (default is 1).
- Package length in centimeters, including packing size. Should be a number (default is 16).
- Package height in centimeters, including packing size. Should be a number (default is 2).
- Package breadth in centimeters, including packing size. Should be a number (default is 11).
- Package diameter in centimeters, including packing size. Should be a number (default is 0).
- If package should be delivered only to the receiver (Correios calls this service "Mão própria"). Should be a boolean (default is #f).
- Package declared value in Brazillian Reais, to be returned in case of miscarriage. Should be a number - ex.: 33.50 (default is 0, which means no use of service).
- If package should be delivered with additional service of a return receipt. Should be a boolean (default is #f, which means no use of service).
- The request return type - at the moment can only be "XML".
This is a correios shipping response object that is generated by process-request. It is a defstruct record and has a constructor make-response. It has many accessors that correspond to the official Correios web service specification (see the request record above for details):
- symbol - ex.: 'PAC, 'SEDEX or SEDEX-10).
- string - total delivery cost in Brazillian Reais - ex.: 15,80.
- number - representing days.
- string - cost of this service in Brazillian Reais - ex.: 3,20
- string - cost of this service in Brazillian Reais - ex.: 2,90
- string - cost of this service in Brazillian Reais - ex.: 32,50
- boolean - indicates if home delivery is available (believe or not there are many inaccessible areas in Brazil!) - ex.: #f.
- boolean - indicates if delivery on sundays is available - ex.: #t.
- string - indicates error number. Returns "0" if no error or other number if so (see pages 14 and 15 of the Correios web service specification).
- returns #f if no error or a string with the error message otherwise.
Procedures[procedure] (process-request request)
This is the core correios procedure for shipping cost calculation. It accepts a request as a parameter and returns a list of response. The list can be processed (see Examples below) and data can be extracted from each response using its accessors (see response details above).[procedure] (valid-response? response)
Check if a specific response has generated errors. Returns a boolean.
(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.
- 0.1 Initial version
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.