migrate payments.
[platal.git] / modules / payment.php
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
96 $op = Env::get('op', 'select');
97 $meth = new PayMethod(Env::getInt('methode', -1));
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 }
104 $val = Env::get('montant') != 0 ? Env::get('montant') : $pay->montant_def;
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 {
114 $res = $globals->xdb->iterator("SELECT timestamp, montant
115 FROM paiement.transactions
116 WHERE uid = {?} AND ref = {?}
117 ORDER BY timestamp DESC",
118 Session::getInt('uid', -1), $ref);
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
130 $page->assign('prefix',$globals->money->mpay_tprefix);
131
132 return PL_OK;
133 }
134
135 function handler_cyber_return(&$page, $uid = null)
136 {
137 require_once 'diogenes/diogenes.hermes.inc.php';
138
139 /* reference banque (numero de transaction) */
140 $champ901 = clean_request('CHAMP901');
141 /* cle d'acceptation */
142 $champ905 = clean_request('CHAMP905');
143 /* code retour */
144 $champ906 = clean_request('CHAMP906');
145 /* email renvoye par la banque */
146 $champ104 = clean_request('CHAMP104');
147 /* reference complete de la commande */
148 $champ200 = clean_request('CHAMP200');
149 /* montant de la transaction */
150 $champ201 = clean_request('CHAMP201');
151 /* devise */
152 $champ202 = clean_request('CHAMP202');
153 $montant = "$champ201 $champ202";
154
155 /* on extrait les informations sur l'utilisateur */
156 $res = $globals->xdb->query("
157 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
158 FROM auth_user_md5 AS a
159 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
160 WHERE a.user_id={?}", $uid);
161 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
162 cb_erreur("uid invalide");
163 }
164
165
166 /* on extrait la reference de la commande */
167 if (!ereg('-xorg-([0-9]+)$',$champ200,$matches)) {
168 cb_erreur("référence de commande invalide");
169 }
170
171 echo ($ref = $matches[1]);
172 $res = $globals->xdb->query("SELECT mail,text,confirmation
173 FROM paiement.paiements WHERE id={?}", $ref);
174 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
175 cb_erreur("référence de commande inconnue");
176 }
177
178 /* on extrait le code de retour */
179 if ($champ906 != "0000") {
180 $res = $globals->xdb->query("SELECT rcb.text,c.id,c.text
181 FROM paiement.codeRCB AS rcb
182 LEFT JOIN paiement.codeC AS c ON rcb.codeC=c.id
183 WHERE rcb.id='$champ906'");
184 if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) {
185 cb_erreur("erreur lors du paiement : $c_text ($c_id)");
186 } else{
187 cb_erreur("erreur inconnue lors du paiement");
188 }
189 }
190
191 /* on fait l'insertion en base de donnees */
192 $globals->xdb->execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle)
193 VALUES ({?},{?},{?},{?},{?},{?})",
194 $champ901, $uid, $ref, $champ200, $montant, $champ905);
195
196 /* on genere le mail de confirmation */
197 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
198 $conf_text = str_replace("<nom>",$nom,$conf_text);
199 $conf_text = str_replace("<promo>",$promo,$conf_text);
200 $conf_text = str_replace("<montant>",$montant,$conf_text);
201 $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
202 $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
203
204 $mymail = new HermesMailer();
205 $mymail->setFrom($conf_mail);
206 $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
207 $mymail->addCc($conf_mail);
208 $mymail->setSubject($conf_title);
209 $mymail->setTxtBody($conf_text);
210 $mymail->send();
211
212 /* on envoie les details de la transaction à telepaiement@ */
213 $mymail = new HermesMailer();
214 $mymail->setFrom("webmaster@polytechnique.org");
215 $mymail->addTo("telepaiement@polytechnique.org");
216 $mymail->setSubject($conf_title);
217 $msg = "utilisateur : $prenom $nom ($uid)\n".
218 "mail : $forlife@polytechnique.org\n\n".
219 "paiement : $conf_title ($conf_mail)\n".
220 "reference : $champ200\n".
221 "montant : $montant\n\n".
222 "dump de REQUEST:\n".
223 var_export($_REQUEST,true);
224 $mymail->setTxtBody($msg);
225 $mymail->send();
226 exit;
227 }
228
229 function handler_paypal_return(&$page, $uid = null)
230 {
231 global $globals;
232
233 $page->changeTpl('payment/retour_paypal.tpl');
234 require_once 'diogenes/diogenes.hermes.inc.php';
235
236 /* reference banque (numero de transaction) */
237 $no_transaction = clean_request('tx');
238 /* token a renvoyer pour avoir plus d'information */
239 $clef = clean_request('sig');
240 /* code retour */
241 $status = clean_request('st');
242 /* raison */
243 $reason = ($status == 'Pending')?clean_request('pending_reason'):clean_request('reason_code');
244 /* reference complete de la commande */
245 $fullref = clean_request('cm');
246 /* montant de la transaction */
247 $montant_nb = clean_request('amt');
248 /* devise */
249 $montant_dev = clean_request('cc');
250 $montant = "$montant_nb $montant_dev";
251
252 /* on extrait le code de retour */
253 if ($status != "Completed") {
254 if ($status)
255 paypal_erreur("erreur lors du paiement : $status - $reason");
256 else
257 paypal_erreur("Paiement annulé", false);
258 }
259
260 /* on extrait les informations sur l'utilisateur */
261 $res = $globals->xdb->query("
262 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
263 FROM auth_user_md5 AS a
264 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
265 WHERE a.user_id={?}", $uid);
266 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
267 paypal_erreur("uid invalide");
268 }
269
270 /* on extrait la reference de la commande */
271 if (!ereg('-xorg-([0-9]+)$',$fullref,$matches)) {
272 paypal_erreur("référence de commande invalide");
273 }
274
275 $ref = $matches[1];
276 $res = $globals->xdb->query("SELECT mail,text,confirmation
277 FROM paiement.paiements WHERE id={?}", $ref);
278 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
279 paypal_erreur("référence de commande inconnue");
280 }
281
282 /* on fait l'insertion en base de donnees */
283 $globals->xdb->execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle)
284 VALUES ({?},{?},{?},{?},{?},{?})",
285 $no_transaction, $uid, $ref, $fullref, $montant, $clef);
286
287 /* on genere le mail de confirmation */
288 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
289 $conf_text = str_replace("<nom>",$nom,$conf_text);
290 $conf_text = str_replace("<promo>",$promo,$conf_text);
291 $conf_text = str_replace("<montant>",$montant,$conf_text);
292 $conf_text = str_replace("<salutation>",$femme ? "Chère" : "Cher",$conf_text);
293 $conf_text = str_replace("<cher>",$femme ? "Chère" : "Cher",$conf_text);
294
295 $mymail = new HermesMailer();
296 $mymail->setFrom($conf_mail);
297 $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
298 $mymail->addCc($conf_mail);
299 $mymail->setSubject($conf_title);
300 $mymail->setTxtBody($conf_text);
301 $mymail->send();
302
303 /* on envoie les details de la transaction à telepaiement@ */
304 $mymail = new HermesMailer();
305 $mymail->setFrom("webmaster@polytechnique.org");
306 $mymail->addTo("telepaiement@polytechnique.org");
307 $mymail->setSubject($conf_title);
308 $msg = "utilisateur : $prenom $nom ($uid)\n".
309 "mail : $forlife@polytechnique.org\n\n".
310 "paiement : $conf_title ($conf_mail)\n".
311 "reference : $no_transaction\n".
312 "montant : $montant\n\n".
313 "dump de REQUEST:\n".
314 var_export($_REQUEST,true);
315 $mymail->setTxtBody($msg);
316 $mymail->send();
317
318 $page->assign('texte', $conf_text);
319 $page->assign('erreur', $erreur);
320
321 return PL_OK;
322 }
323 }
324
325 ?>