[laposte]

Description

Link: [laposte]
Author: Dominique Guardiola
Category: Custom Tag
Version: 8.5.x
License: Public Domain
Posted: Feb. 18, 2008
Updated: Jan. 01, 0001
More by this author...

This ctag will be only useful for french e-commerce sites.
It simply grabs shipping costs from several applications from La Poste website and gives you the shipping cost for a parcel sent from France based upon weight and others options.

At the moment it handles the following services :

  • Colissimo France 48h
  • Colissimo R1
  • Colissimo R2
  • Colissimo R3
  • Colissimo Outre-Mer
  • Colissimo Outre-Mer R1
  • Colissimo Outre-Mer R2
  • Colissimo Outre-Mer R3
  • Colissimo Outre-Mer économique
  • Colissimo international
  • Colissimo international économique (à vérifier)

Ce ctag vous permet de calculer automatiquement le coût d'un envoi Colissimo à partie de la France métropolitaine, en fonction de son poids, de sa destination et de quelques options. Pour une liste des codes pays, inspirez vous du menu déroulant de cette page.

It uses js_flattenparams to allow you to create params for the custom type as an array, see usage.

Parameters

-weight decimal, required in kilograms, like in 1.230 or 0.020 for 20 grams
-cc string, required country code in uppercase, standard except for France : 'F'
-rec string, optional Recommandé : use 'R1', 'R2' or 'R3'
-eco string, optional no value needed - Ecnomical rate, available for overseas (DOM-TOM) and some foreign countries
-cr string, optional no value needed, "contre-remboursement" option

Sample Usage

It's a custom type : you need to create a type,
then use the ->price method to get the price

//nlle calédonie, tarif économique
var('moncolis') = laposte(-weight='3.23',-cc='NC',-eco); 
$monenvoi->price;

//france recommandé R2
var('monenvoi') = laposte(-weight='3.23',-cc='F',-rec='R2'); 
$monenvoi->price;

//using js_flattenparams facility
var('colis') = array(  -weight = client_param('poids'),
                           -cc = client_param('pays'));
client_param('eco') == 'yes'? $ colis->(insert:-eco='');
laposte($colis)->price;


Country codes used are standard, except 'F' for France.
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
define_type(
	'laposte',
	-description='Tarifs d\'expédition de la poste'
);

	// flatten arrays passed as params 
    local: 'flattenedparams' = js_flattenparams: params; 
    // replace the current params with the flattened params 
    params = #flattenedparams -> params; 
    // autocreate locals from params 
    locals = #flattenedparams -> locals; 

	local(
		'cookiejar' = map,
		'debug' = string,
		'price' = decimal,
	);	
	
	define_tag(
		'oncreate',
		-req='weight',
		-req='cc',
		-optional='rec',
		-optional='eco',
		-optional='cr'
	);
		

		local('jsessionid') = string;	
		local('category') = string;
		local('om') = array('GP','GF','MQ','YT','NC','PF','RE','PM','WF');		
		local('rf') = #om;
		#rf->(insert:'F');
			
		select(true);
		
			case(#cc == 'F'); 
				#category = string('8N');  //Colissimo normal
			case(#om >> #cc); 
				#category = string('8Y'); //Colissimo OM
			case;
				#category = string('CC'); // Colissimo international
			
		/select;
		
		//si -eco et international, c'est un international economique
		local_defined('eco') && #category == 'CC' ? local('category') = string('CE');
		
		//si -eco et OM, c'est un Colissimo OM economique
		local_defined('eco') && #om >> #cc ? local('category') = string('XE');
		
		
		//recommandé France
		local_defined('rec') && #cc == 'F' ? local('category') = string('8U');
		
		//recommandé Outre-Mer
		local_defined('rec') && #om >> #cc ? local('category') = string('8Z');
				
		fail_if(local_defined('rec')&&(#rf!>>#cc), -1, 'Erreur : Pas de recommande pour l\'international'); 		
		
		local('pp') = array(
			'forward'				=	'success',
			'codeOffre'				=	#category,
			'codePaysDestination'	=	#cc,
			'poids'					=	#weight,
			'page'					=	'tarificationParticulier'
		);
		
		local_defined('rec')? #pp->(insert: 'choixTauxOptionObligatoire' = #rec);

///// fin config		

		protect;
			local('page') = include_url(
				'http://www.coliposte.fr/cotation/gp/tarificationParticulier.do',
				-getparams = array('codeOffre'=#category),
				-SendMIMEHeaders=(array:'User-Agent'='Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'),
				-retrievemimeheaders='hdr',
				-timeout=15
			);
		/protect;
			
	
		local('debug_cookie_sent') = ((self->cookiejar)->values->join:'<br/');
		
		//get the special cookie code we need in step 2
		iterate($hdr->find('Set-Cookie'), local('i'));
			#i->value >> 'JSESSIONID'? #jsessionid = #i->value;
		/iterate;
		#jsessionid->(Replace: 'JSESSIONID=','');
		#jsessionid->(Replace: 'path=/','');
		
		
		//global routine for getting and updating cookiemap, should be ctagged or something
		iterate($hdr->find('Set-Cookie'), local('i'));
			local('debug_cookie_rcvd') += (#i->value)+'<br/>';
			//we are given an array of cookies statements		
			local('ivalue') = (#i->value)->(split:';');
			// this way for each of each cookie statement , we get an array like this:
			// array ((NAME=VALUE),(Domain=.example.com),(/Path=/),(Expires:....))
			
			local('valuename') = ((#ivalue->Get:1)->(split:'='))->First;		
			
			(self->cookiejar)->(insert: #valuename = #i->value);
			//we update the cookiejar map, and the key is the value of the cookie
			//limitations is that we don't care about domains so this is only for a single-domain transaction				

		/iterate;


		self->debug += '<h4>1. Calling first page to get a cookie</h4><p>'
		'<p><b>Cookie sent : <br/></b>'#debug_cookie_sent'</p>'
		'<p><b>Cookie received : <br/></b>'#debug_cookie_rcvd'</p>'
		'<p><b>Cookie stored : <br/></b>'((self->cookiejar)->values->join:'<br/>')'</p>'
		//'<p><b>HTML : <br/></b><pre>'#page'</pre></p>'
		;
		

////////////step (2)		
	
		
		protect;
			local('page2') = include_url('http://www.coliposte.fr/cotation/gp/tarificationParticulier.do;jsessionid='+#jsessionid,
				-postparams = #pp,
				-SendMIMEHeaders=(array:'User-Agent'='Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11', 'Cookie'=((self->cookiejar)->values->join:';')),
				-retrievemimeheaders='hdr',
				-timeout=15
			);
		/protect;

		local('debug_cookie_sent') = ((self->cookiejar)->values->join:'<br/>');		
			
		iterate($hdr->find('Set-Cookie'), local('i'));		
			local('debug_cookie_rcvd') += (#i->value)+'<br/>';
			local('ivalue') = (#i->value)->(split:';');
			local('valuename') = ((#ivalue->Get:1)->(split:'='))->First;		
			(self->cookiejar)->(insert: #valuename = #i->value);		
		/iterate;
		
		
		#page2->(replace:'\r\n', '');
		#page2->(replace:'\t', '');
		#page2->(replace:' ', '');
		local('data') = #page2->(ExportString: 'iso-8859-1');

		
		local('computed_price')=(String_FindRegExp: #data, -Find='resest</TD><TDalign="left"class="bordgris2">:(.*?)€*', -IgnoreCase);

		
		self->debug += '<hr><h4>2. POST results</h4>'
		'<p><b>POST params passed : </b>'#pp'</p>'
		'<p><b>Cookie sent : <br/></b>'#debug_cookie_sent'</p>'
		'<p><b>Cookie received : <br/></b>'#debug_cookie_rcvd'</p>'
		'<p><b>Cookie stored : <br/></b>'((self->cookiejar)->values->join:'<br/>')'</p>'	
		//'<p><b>HTML : </b><pre>'encode_html(#data)'</pre></p>'
		'<p><b>Prix : </b><pre>'#computed_price'</pre></p>'
		;
		
		
		local('final_price') = decimal(#computed_price->Get:2);
		decimal(#final_price);
		local_defined('cr')? #final_price = (#final_price + 11.72);
		
		
		self->price = #final_price;
	
	/define_tag;	
	
/define_type;

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular