source: project/wiki/eggref/4/correios

Last change on this file was 39572, checked in by sjamaan, 6 months ago

Add "outdated egg" notes to all the CHICKEN 4 eggs

File size: 10.1 KB
Line 
1== Outdated egg!
2
3This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for [[/eggref/5/correios|the CHICKEN 5 version of this egg]], if it exists.
4
5If it does not exist, there may be equivalent functionality provided by another egg; have a look at the [[https://wiki.call-cc.org/chicken-projects/egg-index-5.html|egg index]]. Otherwise, please consider porting this egg to the current version of CHICKEN.
6
7[[tags: egg]]
8
9== correios
10
11[[toc:]]
12
13=== Description
14
15Correios 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]].
16
17=== Author
18
19[[/users/arthurmaciel|Arthur Maciel]]
20
21=== Requirements
22
23Requires the [[defstruct]], [[http-client]], [[ssax]] and [[uri-common]] eggs and the [[srfi-1]] unit.
24
25=== Documentation
26
27==== Requests
28
29<procedure>(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>
30Create a correios {{request}} object (a [[defstruct]] record) to be processed. It has many attributes 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]]:
31
32; 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)}}).
33; FROM-ZIP: sender's zip code and should be a string with 8 digits (only numbers allowed).
34; TO-ZIP: receiver's zip code and should be a string with 8 digits (only numbers allowed).
35; COMPANY: company code, if yours has one. It should be a string.
36; PASSWORD: company password, only if a company code is provided. It should be a string.
37; PKG-WEIGHT: package weight in kilograms. Should be a number.
38; PKG-FORMAT: package format and should be a number: 1 for box/package, 2 for roller/prism and 3 for envelope.
39; PKG-LENGTH: package length in centimeters (including packing size) and should be a number.
40; PKG-HEIGHT: package height in centimeters (including packing size) and should be a number.
41; PKG-BREADTH: package breadth in centimeters (including packing size) and should be a number.
42; PKG-DIAMETER: package diameter in centimeters (including packing size) and should be a number.
43; 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").
44; 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).
45; 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).
46; RETURN-URL: request return URL or type - at the moment can only be "XML".
47
48'''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.
49
50<procedure>(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-url)</procedure>
51Like 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.
52
53<procedure>(process-request REQUEST)</procedure>
54This 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|Examples]] below) and data can be extracted from each response using its accessors (see [[#responses|{{response}}]] details below).
55
56For each {{request}} attribute there is a respective accessor procedure:
57
58<procedure>(request-service REQUEST) => SERVICE</procedure>
59<procedure>(request-from-zip REQUEST) => FROM-ZIP</procedure>
60<procedure>(request-to-zip REQUEST) => TO-ZIP</procedure>
61<procedure>(request-company REQUEST) => COMPANY</procedure>
62<procedure>(request-password REQUEST) => PASSWORD</procedure>
63<procedure>(request-pkg-weight REQUEST) => PKG-WEIGHT</procedure>
64<procedure>(request-pkg-format REQUEST) => PKG-FORMAT</procedure>
65<procedure>(request-pkg-length REQUEST) => PKG-LENGTH</procedure>
66<procedure>(request-pkg-height REQUEST) => PKG-HEIGHT</procedure>
67<procedure>(request-pkg-breadth REQUEST) => PKG-BREADTH</procedure>
68<procedure>(request-pkg-diameter REQUEST) => PKG-DIAMETER</procedure>
69<procedure>(request-receiver-id-check REQUEST) => RECEIVER-ID-CHECK</procedure>
70<procedure>(request-declared-value REQUEST) => DECLARED-VALUE</procedure>
71<procedure>(request-return-receipt REQUEST) => RETURN-RECEIPT</procedure>
72<procedure>(request-return-url REQUEST) => RETURN-URL</procedure>
73
74==== Responses
75
76<record>response</record>
77
78This 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:
79
80<procedure>(response-service RESPONSE) => SERVICE</procedure>
81Returns the delivery service type as a symbol - ie. {{'PAC}}, {{'SEDEX}} or {{SEDEX-10)}}.
82
83<procedure>(response-cost RESPONSE) => COST</procedure>
84Returns the total delivery cost in Brazillian Reais as a string - ie. {{"15,80"}}.
85
86<procedure>(response-delivery-time RESPONSE) => DELIVERY-TIME</procedure>
87Returns the delivery time in days as a number.
88
89<procedure>(response-receiver-id-check-cost RESPONSE) => RECEIVER-ID-CHECK-COST</procedure>
90Returns the cost of the {{receiver-id-check}} service (if used) in Brazillian Reais as a string - ie. {{"3,20"}}.
91
92<procedure>(response-return-receipt-cost RESPONSE) => RETURN-RECEIPT-COST</procedure>
93Returns the cost of the {{return-receipt-cost}} service (if used) in Brazillian Reais as a string - ie. {{"2,90"}}.
94
95<procedure>(response-declared-value-cost RESPONSE) => DECLARED-VALUE-COST</procedure>
96Returns the cost of {{declared-value}} service in Brazillian Reais as a string - ie. {{"32,50"}}.
97
98<procedure>(response-home-delivery RESPONSE) => HOME-DELIVERY</procedure>
99Returns a boolean indicating whether or not the {{home-delivery}} service is available (believe it or not there are still many inaccessible areas in Brazil!).
100
101<procedure>(response-saturday-delivery RESPONSE) => SATURDAY-DELIVERY</procedure>
102Returns a boolean indicating whether or not the {{saturday-delivery}} service is available.
103
104<procedure>(response-error RESPONSE) => ERROR</procedure>
105Returns {{"0"}} if there were no errors or other number (in a string format) 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]] for a complete error code list).
106
107<procedure>(response-error-msg RESPONSE) => ERROR-MSG</procedure>
108Returns a {{#f}} if there were no errors or a string with the error message.
109
110There is also a procedure to check if there {{response}} generated by {{process-request}} is valid:
111
112<procedure>(valid-response? RESPONSE)</procedure>
113Check if a specific response has generated errors. Returns a boolean.
114
115=== Examples
116<enscript highlight="scheme">
117(use correios)
118
119;; Create a request
120(define ship-req (make-request
121                  service: (list 'SEDEX 'PAC)
122                  from-zip: "05412002"
123                  to-zip: "90035120"))
124
125;; Proccess it and iterate along the reponse list
126(let loop ((responses (process-request ship-req)))
127  (cond ((null? responses)
128         (printf "Finished processing responses.~N"))
129        (else
130         (let ((resp (car responses)))
131           (if (valid-response? resp)
132               (printf "Service ~A - Cost: R$~A  Estimated delivery (days): ~A~N"
133                 (response-service resp)
134                 (response-cost resp)
135                 (response-delivery-time resp))
136               (printf "Service ~A - Error:  ~A"
137                 (response-service resp)
138                 (response-error-msg resp))))
139         (loop (cdr responses)))))
140=>
141Service SEDEX - Cost: R$33,00  Estimated delivery (days): 1
142Service PAC - Cost: R$15,80  Estimated delivery (days): 4
143Finished processing responses.
144</enscript>
145
146=== Changelog
147
148* 0.1 Initial version
149
150=== License
151
152  Copyright (c) 2013, Arthur Maciel
153  All rights reserved.
154 
155  Redistribution and use in source and binary forms, with or without
156  modification, are permitted provided that the following conditions are
157  met:
158 
159  Redistributions of source code must retain the above copyright
160  notice, this list of conditions and the following disclaimer.
161 
162  Redistributions in binary form must reproduce the above copyright
163  notice, this list of conditions and the following disclaimer in the
164  documentation and/or other materials provided with the distribution.
165 
166  Neither the name of the author nor the names of its contributors may
167  be used to endorse or promote products derived from this software
168  without specific prior written permission.
169 
170  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
172  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
173  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
174  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
175  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
176  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
177  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
178  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
179  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
180  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
181  OF THE POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.