[[tags: egg]] == correios [[toc:]] === 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 [[http://www.correios.com.br/webServices/PDF/SCPP_manual_implementacao_calculo_remoto_de_precos_e_prazos.pdf|Correios web service specification]]. === Author [[/users/arthurmaciel|Arthur Maciel]] === Requirements Requires the [[defstruct]], [[http-client]], [[ssax]] and [[uri-common]] eggs and the [[srfi-1]] unit. === Documentation ==== Data-structures request 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 [[http://www.correios.com.br/webServices/PDF/SCPP_manual_implementacao_calculo_remoto_de_precos_e_prazos.pdf|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. ; request-service : 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)}}. ; request-from-zip : Sender zip code. Should be a string with 8 digits (only numbers are allowed). ; request-to-zip : Receiver zip code. Should be a string with 8 digits (only numbers are allowed). ; request-company : Company code, if yours has one. Should be a string (default is {{""}}). ; request-password : Company password, only if a company code is provided. Should be a string (default is {{""}}). ; request-pkg-weight : Package weight in kilograms. Should be a number (default is {{0.3}}). ; request-pkg-format : Package format. Should be a number: 1 for box/package, 2 for roller/prism and 3 for envelope (default is {{1}}). ; request-pkg-length : Package length in centimeters, including packing size. Should be a number (default is {{16}}). ; request-pkg-height : Package height in centimeters, including packing size. Should be a number (default is {{2}}). ; request-pkg-breadth : Package breadth in centimeters, including packing size. Should be a number (default is {{11}}). ; request-pkg-diameter : Package diameter in centimeters, including packing size. Should be a number (default is {{0}}). ; request-receiver-id-check : If package should be delivered only to the receiver (Correios calls this service "Mão própria"). Should be a boolean (default is {{#f}}). ; request-declared-value : 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). ; request-return-receipt : 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). ; request-return-url : The request return type - at the moment can only be "XML". response 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 [[http://www.correios.com.br/webServices/PDF/SCPP_manual_implementacao_calculo_remoto_de_precos_e_prazos.pdf|Correios web service specification]] (see the {{request}} record above for details): ; response-service : symbol - ex.: {{'PAC}}, {{'SEDEX}} or {{SEDEX-10)}}. ; response-cost : string - total delivery cost in Brazillian Reais - ex.: {{15,80}}. ; response-delivery-time : number - representing days. ; response-receiver-id-check-cost : string - cost of this service in Brazillian Reais - ex.: {{3,20}} ; response-return-receipt-cost : string - cost of this service in Brazillian Reais - ex.: {{2,90}} ; response-declared-value-cost : string - cost of this service in Brazillian Reais - ex.: {{32,50}} ; response-home-delivery : boolean - indicates if home delivery is available (believe or not there are many inaccessible areas in Brazil!) - ex.: {{#f}}. ; response-sunday-delivery : boolean - indicates if delivery on sundays is available - ex.: {{#t}}. ; response-error : string - indicates error number. Returns {{"0"}} if no error or other number if so (see pages 14 and 15 of the [[http://www.correios.com.br/webServices/PDF/SCPP_manual_implementacao_calculo_remoto_de_precos_e_prazos.pdf|Correios web service specification]]). ; response-error-msg : returns {{#f}} if no error or a string with the error message otherwise. ==== Procedures (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). (valid-response? response) 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 * 0.1 Initial version === 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.