ff5c432af7e9e082560f75f7638cd11f309a49bb
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_PUBLIC
),
81 'payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC
),
82 'admin/payments' => $this->make_hook('admin', AUTH_MDP
, 'admin'),
86 function handler_payment(&$page, $ref = -1)
90 require_once 'profil.func.inc.php' ;
91 require_once dirname(__FILE__
).'/payment/money.inc.php' ;
93 $page->changeTpl('payment/index.tpl');
94 $page->assign('xorg_title','Polytechnique.org - Télépaiements');
97 $op = Env
::v('op', 'select');
98 $meth = new PayMethod(Env
::i('methode', -1));
99 $pay = new Payment($ref);
101 if($pay->flags
->hasflag('old')){
102 $page->trig("La transaction selectionnée est périmée.");
103 $pay = new Payment();
105 $val = Env
::v('montant') != 0 ? Env
::v('montant') : $pay->montant_def
;
107 if (($e = $pay->check($val)) !== true
) {
112 $pay->init($val, $meth);
113 $pay->prepareform($pay);
115 $res = XDB
::iterator("SELECT timestamp, montant
116 FROM paiement.transactions
117 WHERE uid = {?} AND ref = {?}
118 ORDER BY timestamp DESC",
119 S
::v('uid', -1), $ref);
121 if ($res->total()) $page->assign('transactions', $res);
124 $val = floor($val).".".substr(floor(($val - floor($val))*100+
100),1);
125 $page->assign('montant',$val);
127 $page->assign('meth', $meth);
128 $page->assign('pay', $pay);
129 $page->assign('evtlink', $pay->event());
131 $page->assign('prefix', $globals->money
->mpay_tprefix
);
134 function handler_cyber_return(&$page, $uid = null
)
136 require_once 'diogenes/diogenes.hermes.inc.php';
138 /* reference banque (numero de transaction) */
139 $champ901 = clean_request('CHAMP901');
140 /* cle d'acceptation */
141 $champ905 = clean_request('CHAMP905');
143 $champ906 = clean_request('CHAMP906');
144 /* email renvoye par la banque */
145 $champ104 = clean_request('CHAMP104');
146 /* reference complete de la commande */
147 $champ200 = clean_request('CHAMP200');
148 /* montant de la transaction */
149 $champ201 = clean_request('CHAMP201');
151 $champ202 = clean_request('CHAMP202');
152 $montant = "$champ201 $champ202";
154 /* on extrait les informations sur l'utilisateur */
156 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
157 FROM auth_user_md5 AS a
158 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
159 WHERE a.user_id={?}", $uid);
160 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
161 cb_erreur("uid invalide");
165 /* on extrait la reference de la commande */
166 if (!ereg('-xorg-([0-9]+)$',$champ200,$matches)) {
167 cb_erreur("référence de commande invalide");
170 echo ($ref = $matches[1]);
171 $res = XDB
::query("SELECT mail,text,confirmation
172 FROM paiement.paiements WHERE id={?}", $ref);
173 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
174 cb_erreur("référence de commande inconnue");
177 /* on extrait le code de retour */
178 if ($champ906 != "0000") {
179 $res = XDB
::query("SELECT rcb.text,c.id,c.text
180 FROM paiement.codeRCB AS rcb
181 LEFT JOIN paiement.codeC AS c ON rcb.codeC=c.id
182 WHERE rcb.id='$champ906'");
183 if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) {
184 cb_erreur("erreur lors du paiement : $c_text ($c_id)");
186 cb_erreur("erreur inconnue lors du paiement");
190 /* on fait l'insertion en base de donnees */
191 XDB
::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle)
192 VALUES ({?},{?},{?},{?},{?},{?})",
193 $champ901, $uid, $ref, $champ200, $montant, $champ905);
195 /* on genere le mail de confirmation */
196 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
197 $conf_text = str_replace("<nom>",$nom,$conf_text);
198 $conf_text = str_replace("<promo>",$promo,$conf_text);
199 $conf_text = str_replace("<montant>",$montant,$conf_text);
200 $conf_text = str_replace("<salutation>",$femme ?
"Chère" : "Cher",$conf_text);
201 $conf_text = str_replace("<cher>",$femme ?
"Chère" : "Cher",$conf_text);
203 $mymail = new HermesMailer();
204 $mymail->setFrom($conf_mail);
205 $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
206 $mymail->addCc($conf_mail);
207 $mymail->setSubject($conf_title);
208 $mymail->setTxtBody($conf_text);
211 /* on envoie les details de la transaction à telepaiement@ */
212 $mymail = new HermesMailer();
213 $mymail->setFrom("webmaster@polytechnique.org");
214 $mymail->addTo("telepaiement@polytechnique.org");
215 $mymail->setSubject($conf_title);
216 $msg = "utilisateur : $prenom $nom ($uid)\n".
217 "mail : $forlife@polytechnique.org\n\n".
218 "paiement : $conf_title ($conf_mail)\n".
219 "reference : $champ200\n".
220 "montant : $montant\n\n".
221 "dump de REQUEST:\n".
222 var_export($_REQUEST,true
);
223 $mymail->setTxtBody($msg);
228 function handler_paypal_return(&$page, $uid = null
)
230 $page->changeTpl('payment/retour_paypal.tpl');
231 require_once 'diogenes/diogenes.hermes.inc.php';
233 /* reference banque (numero de transaction) */
234 $no_transaction = clean_request('tx');
235 /* token a renvoyer pour avoir plus d'information */
236 $clef = clean_request('sig');
238 $status = clean_request('st');
240 $reason = ($status == 'Pending')?
clean_request('pending_reason'):clean_request('reason_code');
241 /* reference complete de la commande */
242 $fullref = clean_request('cm');
243 /* montant de la transaction */
244 $montant_nb = clean_request('amt');
246 $montant_dev = clean_request('cc');
247 $montant = "$montant_nb $montant_dev";
249 /* on extrait le code de retour */
250 if ($status != "Completed") {
252 paypal_erreur("erreur lors du paiement : $status - $reason");
254 paypal_erreur("Paiement annulé", false
);
257 /* on extrait les informations sur l'utilisateur */
259 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
260 FROM auth_user_md5 AS a
261 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
262 WHERE a.user_id={?}", $uid);
263 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
264 paypal_erreur("uid invalide");
267 /* on extrait la reference de la commande */
268 if (!ereg('-xorg-([0-9]+)$',$fullref,$matches)) {
269 paypal_erreur("référence de commande invalide");
273 $res = XDB
::query("SELECT mail,text,confirmation
274 FROM paiement.paiements WHERE id={?}", $ref);
275 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
276 paypal_erreur("référence de commande inconnue");
279 /* on fait l'insertion en base de donnees */
280 XDB
::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle)
281 VALUES ({?},{?},{?},{?},{?},{?})",
282 $no_transaction, $uid, $ref, $fullref, $montant, $clef);
284 /* on genere le mail de confirmation */
285 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
286 $conf_text = str_replace("<nom>",$nom,$conf_text);
287 $conf_text = str_replace("<promo>",$promo,$conf_text);
288 $conf_text = str_replace("<montant>",$montant,$conf_text);
289 $conf_text = str_replace("<salutation>",$femme ?
"Chère" : "Cher",$conf_text);
290 $conf_text = str_replace("<cher>",$femme ?
"Chère" : "Cher",$conf_text);
292 $mymail = new HermesMailer();
293 $mymail->setFrom($conf_mail);
294 $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
295 $mymail->addCc($conf_mail);
296 $mymail->setSubject($conf_title);
297 $mymail->setTxtBody($conf_text);
300 /* on envoie les details de la transaction à telepaiement@ */
301 $mymail = new HermesMailer();
302 $mymail->setFrom("webmaster@polytechnique.org");
303 $mymail->addTo("telepaiement@polytechnique.org");
304 $mymail->setSubject($conf_title);
305 $msg = "utilisateur : $prenom $nom ($uid)\n".
306 "mail : $forlife@polytechnique.org\n\n".
307 "paiement : $conf_title ($conf_mail)\n".
308 "reference : $no_transaction\n".
309 "montant : $montant\n\n".
310 "dump de REQUEST:\n".
311 var_export($_REQUEST,true
);
312 $mymail->setTxtBody($msg);
315 $page->assign('texte', $conf_text);
316 $page->assign('erreur', $erreur);
318 function handler_admin(&$page, $action = 'list', $id = null
) {
319 $page->assign('xorg_title','Polytechnique.org - Administration - Paiements');
320 $page->assign('title', 'Gestion des télépaiements');
321 $table_editor = new PLTableEditor('admin/payments','paiement.paiements','id');
322 $table_editor->add_join_table('paiement.transactions','ref',true
);
323 $table_editor->add_sort_field('flags');
324 $table_editor->add_sort_field('id', true
, true
);
325 $table_editor->on_delete("UPDATE paiement.paiements SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
326 $table_editor->describe('text','intitulé',true
);
327 $table_editor->describe('url','site web',false
);
328 $table_editor->describe('montant_def','montant par défaut',false
);
329 $table_editor->describe('montant_min','montant minimum',false
);
330 $table_editor->describe('montant_max','montant maximum',false
);
331 $table_editor->describe('mail','email contact',true
);
332 $table_editor->describe('confirmation','message confirmation',false
);
333 $table_editor->apply($page, $action, $id);