a2558f2b |
1 | <?php |
2 | /*************************************************************************** |
3 | * Copyright (C) 2003-2006 Polytechnique.org * |
4 | * http://opensource.polytechnique.org/ * |
5 | * * |
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. * |
10 | * * |
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. * |
15 | * * |
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 * |
18 | * Foundation, Inc., * |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
20 | ***************************************************************************/ |
21 | |
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)); |
29 | $mymail->send(); |
30 | exit; |
31 | } |
32 | |
33 | /* sort en affichant une erreur */ |
34 | function paypal_erreur($text, $send=true) { |
35 | global $page, $erreur; |
36 | if ($erreur) return; |
37 | $erreur = $text; |
38 | if (!$send) return; |
39 | |
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)); |
45 | $mymail->send(); |
46 | |
47 | $page->trig($text); |
48 | } |
49 | |
50 | /* http://fr.wikipedia.org/wiki/Formule_de_Luhn */ |
51 | function luhn($nombre) { |
52 | $s = strrev($nombre); |
53 | $sum = 0; |
54 | for ($i = 0; $i < strlen($s); $i++) { |
55 | $dgt = $s{$i}; |
56 | $sum += ($i % 2) ? (2*$dgt) % 9 : $dgt; |
57 | } |
58 | return $sum % 10; |
59 | } |
60 | |
61 | /* calcule la clé d'acceptation a partir de 5 champs */ |
62 | function cle_accept($d1,$d2,$d3,$d4,$d5) |
63 | { |
64 | $m1 = luhn($d1.$d5); |
65 | $m2 = luhn($d2.$d5); |
66 | $m3 = luhn($d3.$d5); |
67 | $m4 = luhn($d4.$d5); |
68 | $n = $m1 + $m2 + $m3 + $m4; |
69 | $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
70 | return $alpha{$n-1}.$m1.$m2.$m3.$m4; |
71 | } |
72 | |
73 | |
74 | class PaymentModule extends PLModule |
75 | { |
76 | function handlers() |
77 | { |
78 | return array( |
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), |
82 | ); |
83 | } |
84 | |
85 | function handler_payment(&$page, $ref = -1) |
86 | { |
87 | global $globals; |
88 | |
89 | require_once 'profil.func.inc.php' ; |
90 | require_once 'money.inc.php' ; |
91 | |
92 | $page->changeTpl('payment/index.tpl'); |
93 | $page->assign('xorg_title','Polytechnique.org - Télépaiements'); |
94 | |
95 | // initialisation |
5e2307dc |
96 | $op = Env::v('op', 'select'); |
97 | $meth = new PayMethod(Env::i('methode', -1)); |
a2558f2b |
98 | $pay = new Payment($ref); |
99 | |
100 | if($pay->flags->hasflag('old')){ |
101 | $page->trig("La transaction selectionnée est périmée."); |
102 | $pay = new Payment(); |
103 | } |
5e2307dc |
104 | $val = Env::v('montant') != 0 ? Env::v('montant') : $pay->montant_def; |
a2558f2b |
105 | |
106 | if (($e = $pay->check($val)) !== true) { |
107 | $page->trig($e); |
108 | } |
109 | |
110 | if ($op=='submit') { |
111 | $pay->init($val, $meth); |
112 | $pay->prepareform($pay); |
113 | } else { |
08cce2ff |
114 | $res = XDB::iterator("SELECT timestamp, montant |
a2558f2b |
115 | FROM paiement.transactions |
116 | WHERE uid = {?} AND ref = {?} |
117 | ORDER BY timestamp DESC", |
cab08090 |
118 | S::v('uid', -1), $ref); |
a2558f2b |
119 | |
120 | if ($res->total()) $page->assign('transactions', $res); |
121 | } |
122 | |
123 | $val = floor($val).".".substr(floor(($val - floor($val))*100+100),1); |
124 | $page->assign('montant',$val); |
125 | |
126 | $page->assign('meth', $meth); |
127 | $page->assign('pay', $pay); |
128 | $page->assign('evtlink', $pay->event()); |
129 | |
a3a049fc |
130 | $page->assign('prefix', $globals->money->mpay_tprefix); |
a2558f2b |
131 | } |
132 | |
133 | function handler_cyber_return(&$page, $uid = null) |
134 | { |
135 | require_once 'diogenes/diogenes.hermes.inc.php'; |
136 | |
137 | /* reference banque (numero de transaction) */ |
138 | $champ901 = clean_request('CHAMP901'); |
139 | /* cle d'acceptation */ |
140 | $champ905 = clean_request('CHAMP905'); |
141 | /* code retour */ |
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'); |
149 | /* devise */ |
150 | $champ202 = clean_request('CHAMP202'); |
151 | $montant = "$champ201 $champ202"; |
152 | |
153 | /* on extrait les informations sur l'utilisateur */ |
08cce2ff |
154 | $res = XDB::query(" |
a2558f2b |
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"); |
161 | } |
162 | |
163 | |
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"); |
167 | } |
168 | |
169 | echo ($ref = $matches[1]); |
08cce2ff |
170 | $res = XDB::query("SELECT mail,text,confirmation |
a2558f2b |
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"); |
174 | } |
175 | |
176 | /* on extrait le code de retour */ |
177 | if ($champ906 != "0000") { |
08cce2ff |
178 | $res = XDB::query("SELECT rcb.text,c.id,c.text |
a2558f2b |
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)"); |
184 | } else{ |
185 | cb_erreur("erreur inconnue lors du paiement"); |
186 | } |
187 | } |
188 | |
189 | /* on fait l'insertion en base de donnees */ |
08cce2ff |
190 | XDB::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle) |
a2558f2b |
191 | VALUES ({?},{?},{?},{?},{?},{?})", |
192 | $champ901, $uid, $ref, $champ200, $montant, $champ905); |
193 | |
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); |
201 | |
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); |
208 | $mymail->send(); |
209 | |
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); |
223 | $mymail->send(); |
224 | exit; |
225 | } |
226 | |
227 | function handler_paypal_return(&$page, $uid = null) |
228 | { |
a2558f2b |
229 | $page->changeTpl('payment/retour_paypal.tpl'); |
230 | require_once 'diogenes/diogenes.hermes.inc.php'; |
231 | |
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'); |
236 | /* code retour */ |
237 | $status = clean_request('st'); |
238 | /* raison */ |
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'); |
244 | /* devise */ |
245 | $montant_dev = clean_request('cc'); |
246 | $montant = "$montant_nb $montant_dev"; |
247 | |
248 | /* on extrait le code de retour */ |
249 | if ($status != "Completed") { |
250 | if ($status) |
251 | paypal_erreur("erreur lors du paiement : $status - $reason"); |
252 | else |
253 | paypal_erreur("Paiement annulé", false); |
254 | } |
255 | |
256 | /* on extrait les informations sur l'utilisateur */ |
08cce2ff |
257 | $res = XDB::query(" |
a2558f2b |
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"); |
264 | } |
265 | |
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"); |
269 | } |
270 | |
271 | $ref = $matches[1]; |
08cce2ff |
272 | $res = XDB::query("SELECT mail,text,confirmation |
a2558f2b |
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"); |
276 | } |
277 | |
278 | /* on fait l'insertion en base de donnees */ |
08cce2ff |
279 | XDB::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle) |
a2558f2b |
280 | VALUES ({?},{?},{?},{?},{?},{?})", |
281 | $no_transaction, $uid, $ref, $fullref, $montant, $clef); |
282 | |
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); |
290 | |
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); |
297 | $mymail->send(); |
298 | |
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); |
312 | $mymail->send(); |
313 | |
314 | $page->assign('texte', $conf_text); |
315 | $page->assign('erreur', $erreur); |
a2558f2b |
316 | } |
317 | } |
318 | |
319 | ?> |