2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 /* sort en affichant une erreur */
23 function cb_erreur($text) {
24 $mymail = new HermesMailer();
25 $mymail->addTo("telepaiement@polytechnique.org");
26 $mymail->setFrom("webmaster@polytechnique.org");
27 $mymail->setSubject("erreur lors d'un télépaiement (CyberPaiement)");
28 $mymail->setTxtBody("\n\n".var_export($_REQUEST,true
));
33 /* sort en affichant une erreur */
34 function paypal_erreur($text, $send=true
) {
35 global $page, $erreur;
40 $mymail = new HermesMailer();
41 $mymail->addTo("telepaiement@polytechnique.org");
42 $mymail->setFrom("webmaster@polytechnique.org");
43 $mymail->setSubject("erreur lors d'un télépaiement (PayPal)");
44 $mymail->setTxtBody("\n\n".var_export($_REQUEST,true
));
50 /* http://fr.wikipedia.org/wiki/Formule_de_Luhn */
51 function luhn($nombre) {
54 for ($i = 0; $i < strlen($s); $i++
) {
56 $sum +
= ($i %
2) ?
(2*$dgt) %
9 : $dgt;
61 /* calcule la clé d'acceptation a partir de 5 champs */
62 function cle_accept($d1,$d2,$d3,$d4,$d5)
68 $n = $m1 +
$m2 +
$m3 +
$m4;
69 $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
70 return $alpha{$n-1}.$m1.$m2.$m3.$m4;
74 class PaymentModule
extends PLModule
79 'payment' => $this->make_hook('payment', AUTH_MDP
),
80 'payment/cyber_return' => $this->make_hook('cyber_return', AUTH_PUB
),
81 'payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUB
),
85 function handler_payment(&$page, $ref = -1)
89 require_once 'profil.func.inc.php' ;
90 require_once 'money.inc.php' ;
92 $page->changeTpl('payment/index.tpl');
93 $page->assign('xorg_title','Polytechnique.org - Télépaiements');
96 $op = Env
::v('op', 'select');
97 $meth = new PayMethod(Env
::i('methode', -1));
98 $pay = new Payment($ref);
100 if($pay->flags
->hasflag('old')){
101 $page->trig("La transaction selectionnée est périmée.");
102 $pay = new Payment();
104 $val = Env
::v('montant') != 0 ? Env
::v('montant') : $pay->montant_def
;
106 if (($e = $pay->check($val)) !== true
) {
111 $pay->init($val, $meth);
112 $pay->prepareform($pay);
114 $res = XDB
::iterator("SELECT timestamp, montant
115 FROM paiement.transactions
116 WHERE uid = {?} AND ref = {?}
117 ORDER BY timestamp DESC",
118 S
::v('uid', -1), $ref);
120 if ($res->total()) $page->assign('transactions', $res);
123 $val = floor($val).".".substr(floor(($val - floor($val))*100+
100),1);
124 $page->assign('montant',$val);
126 $page->assign('meth', $meth);
127 $page->assign('pay', $pay);
128 $page->assign('evtlink', $pay->event());
130 $page->assign('prefix', $globals->money
->mpay_tprefix
);
133 function handler_cyber_return(&$page, $uid = null
)
135 require_once 'diogenes/diogenes.hermes.inc.php';
137 /* reference banque (numero de transaction) */
138 $champ901 = clean_request('CHAMP901');
139 /* cle d'acceptation */
140 $champ905 = clean_request('CHAMP905');
142 $champ906 = clean_request('CHAMP906');
143 /* email renvoye par la banque */
144 $champ104 = clean_request('CHAMP104');
145 /* reference complete de la commande */
146 $champ200 = clean_request('CHAMP200');
147 /* montant de la transaction */
148 $champ201 = clean_request('CHAMP201');
150 $champ202 = clean_request('CHAMP202');
151 $montant = "$champ201 $champ202";
153 /* on extrait les informations sur l'utilisateur */
155 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
156 FROM auth_user_md5 AS a
157 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
158 WHERE a.user_id={?}", $uid);
159 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
160 cb_erreur("uid invalide");
164 /* on extrait la reference de la commande */
165 if (!ereg('-xorg-([0-9]+)$',$champ200,$matches)) {
166 cb_erreur("référence de commande invalide");
169 echo ($ref = $matches[1]);
170 $res = XDB
::query("SELECT mail,text,confirmation
171 FROM paiement.paiements WHERE id={?}", $ref);
172 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
173 cb_erreur("référence de commande inconnue");
176 /* on extrait le code de retour */
177 if ($champ906 != "0000") {
178 $res = XDB
::query("SELECT rcb.text,c.id,c.text
179 FROM paiement.codeRCB AS rcb
180 LEFT JOIN paiement.codeC AS c ON rcb.codeC=c.id
181 WHERE rcb.id='$champ906'");
182 if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) {
183 cb_erreur("erreur lors du paiement : $c_text ($c_id)");
185 cb_erreur("erreur inconnue lors du paiement");
189 /* on fait l'insertion en base de donnees */
190 XDB
::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle)
191 VALUES ({?},{?},{?},{?},{?},{?})",
192 $champ901, $uid, $ref, $champ200, $montant, $champ905);
194 /* on genere le mail de confirmation */
195 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
196 $conf_text = str_replace("<nom>",$nom,$conf_text);
197 $conf_text = str_replace("<promo>",$promo,$conf_text);
198 $conf_text = str_replace("<montant>",$montant,$conf_text);
199 $conf_text = str_replace("<salutation>",$femme ?
"Chère" : "Cher",$conf_text);
200 $conf_text = str_replace("<cher>",$femme ?
"Chère" : "Cher",$conf_text);
202 $mymail = new HermesMailer();
203 $mymail->setFrom($conf_mail);
204 $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
205 $mymail->addCc($conf_mail);
206 $mymail->setSubject($conf_title);
207 $mymail->setTxtBody($conf_text);
210 /* on envoie les details de la transaction à telepaiement@ */
211 $mymail = new HermesMailer();
212 $mymail->setFrom("webmaster@polytechnique.org");
213 $mymail->addTo("telepaiement@polytechnique.org");
214 $mymail->setSubject($conf_title);
215 $msg = "utilisateur : $prenom $nom ($uid)\n".
216 "mail : $forlife@polytechnique.org\n\n".
217 "paiement : $conf_title ($conf_mail)\n".
218 "reference : $champ200\n".
219 "montant : $montant\n\n".
220 "dump de REQUEST:\n".
221 var_export($_REQUEST,true
);
222 $mymail->setTxtBody($msg);
227 function handler_paypal_return(&$page, $uid = null
)
229 $page->changeTpl('payment/retour_paypal.tpl');
230 require_once 'diogenes/diogenes.hermes.inc.php';
232 /* reference banque (numero de transaction) */
233 $no_transaction = clean_request('tx');
234 /* token a renvoyer pour avoir plus d'information */
235 $clef = clean_request('sig');
237 $status = clean_request('st');
239 $reason = ($status == 'Pending')?
clean_request('pending_reason'):clean_request('reason_code');
240 /* reference complete de la commande */
241 $fullref = clean_request('cm');
242 /* montant de la transaction */
243 $montant_nb = clean_request('amt');
245 $montant_dev = clean_request('cc');
246 $montant = "$montant_nb $montant_dev";
248 /* on extrait le code de retour */
249 if ($status != "Completed") {
251 paypal_erreur("erreur lors du paiement : $status - $reason");
253 paypal_erreur("Paiement annulé", false
);
256 /* on extrait les informations sur l'utilisateur */
258 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
259 FROM auth_user_md5 AS a
260 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
261 WHERE a.user_id={?}", $uid);
262 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
263 paypal_erreur("uid invalide");
266 /* on extrait la reference de la commande */
267 if (!ereg('-xorg-([0-9]+)$',$fullref,$matches)) {
268 paypal_erreur("référence de commande invalide");
272 $res = XDB
::query("SELECT mail,text,confirmation
273 FROM paiement.paiements WHERE id={?}", $ref);
274 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
275 paypal_erreur("référence de commande inconnue");
278 /* on fait l'insertion en base de donnees */
279 XDB
::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle)
280 VALUES ({?},{?},{?},{?},{?},{?})",
281 $no_transaction, $uid, $ref, $fullref, $montant, $clef);
283 /* on genere le mail de confirmation */
284 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
285 $conf_text = str_replace("<nom>",$nom,$conf_text);
286 $conf_text = str_replace("<promo>",$promo,$conf_text);
287 $conf_text = str_replace("<montant>",$montant,$conf_text);
288 $conf_text = str_replace("<salutation>",$femme ?
"Chère" : "Cher",$conf_text);
289 $conf_text = str_replace("<cher>",$femme ?
"Chère" : "Cher",$conf_text);
291 $mymail = new HermesMailer();
292 $mymail->setFrom($conf_mail);
293 $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
294 $mymail->addCc($conf_mail);
295 $mymail->setSubject($conf_title);
296 $mymail->setTxtBody($conf_text);
299 /* on envoie les details de la transaction à telepaiement@ */
300 $mymail = new HermesMailer();
301 $mymail->setFrom("webmaster@polytechnique.org");
302 $mymail->addTo("telepaiement@polytechnique.org");
303 $mymail->setSubject($conf_title);
304 $msg = "utilisateur : $prenom $nom ($uid)\n".
305 "mail : $forlife@polytechnique.org\n\n".
306 "paiement : $conf_title ($conf_mail)\n".
307 "reference : $no_transaction\n".
308 "montant : $montant\n\n".
309 "dump de REQUEST:\n".
310 var_export($_REQUEST,true
);
311 $mymail->setTxtBody($msg);
314 $page->assign('texte', $conf_text);
315 $page->assign('erreur', $erreur);