source: project/wiki/eggref/4/correios @ 30198

Last change on this file since 30198 was 30198, checked in by arthurmaciel, 8 years ago
File size: 7.7 KB
Line 
1[[tags: egg]]
2
3== correios
4
5[[toc:]]
6
7=== Description
8
9Correios 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]].
10
11=== Author
12
13[[/users/arthurmaciel|Arthur Maciel]]
14
15=== Repository
16
17The git repository for the correios source code is hosted by GitHub:
18[[https://github.com/arthurmaciel/correios|https://github.com/arthurmaciel/correios]].
19
20=== Requirements
21
22Requires the [[defstruct]], [[http-client]], [[ssax]] and [[uri-common]] eggs and the [[srfi-1]] unit.
23
24=== Documentation
25
26==== Data-structures
27
28<record>request</record>
29
30This 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]]:
31
32'''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.
33
34; 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)}}.
35; request-from-zip : Sender zip code. Should be a string with 8 digits (only numbers are allowed).
36; request-to-zip : Receiver zip code. Should be a string with 8 digits (only numbers are allowed).
37; request-company : Company code, if yours has one. Should be a string (default is {{""}}).
38; request-password : Company password, only if a company code is provided. Should be a string (default is {{""}}).
39; request-pkg-weight : Package weight in kilograms. Should be a number (default is {{0.3}}).
40; request-pkg-format : Package format. Should be a number: 1 for box/package, 2 for roller/prism and 3 for envelope (default is {{1}}).
41; request-pkg-length : Package length in centimeters, including packing size. Should be a number (default is {{16}}).
42; request-pkg-height : Package height in centimeters, including packing size. Should be a number (default is {{2}}).
43; request-pkg-breadth : Package breadth in centimeters, including packing size. Should be a number (default is {{11}}).
44; request-pkg-diameter : Package diameter in centimeters, including packing size. Should be a number (default is {{0}}).
45; 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}}).
46; 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).
47; 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).
48; request-return-url : The request return type - at the moment can only be "XML".
49
50<record>response</record>
51
52This 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):
53
54; response-service : symbol - ex.: {{'PAC}}, {{'SEDEX}} or {{SEDEX-10)}}.
55; response-cost : string - total delivery cost in Brazillian Reais - ex.: {{15,80}}.
56; response-delivery-time : number - representing days.
57; response-receiver-id-check-cost : string - cost of this service in Brazillian Reais - ex.: {{3,20}}
58; response-return-receipt-cost : string - cost of this service in Brazillian Reais - ex.: {{2,90}}
59; response-declared-value-cost : string - cost of this service in Brazillian Reais - ex.: {{32,50}}
60; response-home-delivery : boolean - indicates if home delivery is available (believe or not there are many inaccessible areas in Brazil!) - ex.: {{#f}}.
61; response-sunday-delivery : boolean - indicates if delivery on sundays is available - ex.: {{#t}}.
62; 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]]).
63; response-error-msg : returns {{#f}} if no error or a string with the error message otherwise.
64
65==== Procedures
66
67<procedure>(process-request request)</procedure>
68
69This 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).
70
71<procedure>(valid-response? response)</procedure>
72
73Check if a specific response has generated errors. Returns a boolean.
74
75===== Examples
76
77<enscript highlight="scheme">
78(use correios)
79
80;; Create a request
81(define ship-req (make-request
82                  service: (list 'SEDEX 'PAC)
83                  from-zip: "05412002"
84                  to-zip: "90035120"))
85
86;; Proccess it and iterate along the reponse list
87(let loop ((responses (process-request ship-req)))
88  (cond ((null? responses)
89         (printf "Finished processing responses.~N"))
90        (else
91         (let ((resp (car responses)))
92           (if (valid-response? resp)
93               (printf "Service ~A - Cost: R$~A  Estimated delivery (days): ~A~N"
94                 (response-service resp)
95                 (response-cost resp)
96                 (response-delivery-time resp))
97               (printf "Service ~A - Error:  ~A"
98                 (response-service resp)
99                 (response-error-msg resp))))
100         (loop (cdr responses)))))
101=>
102Service SEDEX - Cost: R$33,00  Estimated delivery (days): 1
103Service PAC - Cost: R$15,80  Estimated delivery (days): 4
104Finished processing responses.
105</enscript>
106
107=== Changelog
108
109* 0.1 Initial version
110
111=== License
112
113  Copyright (c) 2013, Arthur Maciel
114  All rights reserved.
115 
116  Redistribution and use in source and binary forms, with or without
117  modification, are permitted provided that the following conditions are
118  met:
119 
120  Redistributions of source code must retain the above copyright
121  notice, this list of conditions and the following disclaimer.
122 
123  Redistributions in binary form must reproduce the above copyright
124  notice, this list of conditions and the following disclaimer in the
125  documentation and/or other materials provided with the distribution.
126 
127  Neither the name of the author nor the names of its contributors may
128  be used to endorse or promote products derived from this software
129  without specific prior written permission.
130 
131  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
132  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
133  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
134  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
135  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
136  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
137  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
138  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
139  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
140  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
141  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
142  OF THE POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.