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);
126 $page->assign('comment',Env
::v('comment'));
128 $page->assign('meth', $meth);
129 $page->assign('pay', $pay);
130 $page->assign('evtlink', $pay->event());
132 $page->assign('prefix', $globals->money
->mpay_tprefix
);
135 function handler_cyber_return(&$page, $uid = null
)
137 require_once 'diogenes/diogenes.hermes.inc.php';
139 /* reference banque (numero de transaction) */
140 $champ901 = clean_request('CHAMP901');
141 /* cle d'acceptation */
142 $champ905 = clean_request('CHAMP905');
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');
152 $champ202 = clean_request('CHAMP202');
153 $montant = "$champ201 $champ202";
155 /* on extrait les informations sur l'utilisateur */
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");
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");
171 echo ($ref = $matches[1]);
172 $res = 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");
178 /* on extrait le code de retour */
179 if ($champ906 != "0000") {
180 $res = 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)");
187 cb_erreur("erreur inconnue lors du paiement");
191 /* on fait l'insertion en base de donnees */
192 XDB
::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle,comment)
193 VALUES ({?},{?},{?},{?},{?},{?},{?})",
194 $champ901, $uid, $ref, $champ200, $montant, $champ905,Env
::v('comment'));
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);
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);
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);
229 function handler_paypal_return(&$page, $uid = null
)
231 $page->changeTpl('payment/retour_paypal.tpl');
232 require_once 'diogenes/diogenes.hermes.inc.php';
234 /* reference banque (numero de transaction) */
235 $no_transaction = clean_request('tx');
236 /* token a renvoyer pour avoir plus d'information */
237 $clef = clean_request('sig');
239 $status = clean_request('st');
241 $reason = ($status == 'Pending')?
clean_request('pending_reason'):clean_request('reason_code');
242 /* reference complete de la commande */
243 $fullref = clean_request('cm');
244 /* montant de la transaction */
245 $montant_nb = clean_request('amt');
247 $montant_dev = clean_request('cc');
248 $montant = "$montant_nb $montant_dev";
250 /* on extrait le code de retour */
251 if ($status != "Completed") {
253 paypal_erreur("erreur lors du paiement : $status - $reason");
255 paypal_erreur("Paiement annulé", false
);
258 /* on extrait les informations sur l'utilisateur */
260 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET(a.flags,'femme')
261 FROM auth_user_md5 AS a
262 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
263 WHERE a.user_id={?}", $uid);
264 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
265 paypal_erreur("uid invalide");
268 /* on extrait la reference de la commande */
269 if (!ereg('-xorg-([0-9]+)$',$fullref,$matches)) {
270 paypal_erreur("référence de commande invalide");
274 $res = XDB
::query("SELECT mail,text,confirmation
275 FROM paiement.paiements WHERE id={?}", $ref);
276 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
277 paypal_erreur("référence de commande inconnue");
280 /* on fait l'insertion en base de donnees */
281 XDB
::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle,comment)
282 VALUES ({?},{?},{?},{?},{?},{?},{?})",
283 $no_transaction, $uid, $ref, $fullref, $montant, $clef, Env
::v('comment'));
285 /* on genere le mail de confirmation */
286 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
287 $conf_text = str_replace("<nom>",$nom,$conf_text);
288 $conf_text = str_replace("<promo>",$promo,$conf_text);
289 $conf_text = str_replace("<montant>",$montant,$conf_text);
290 $conf_text = str_replace("<salutation>",$femme ?
"Chère" : "Cher",$conf_text);
291 $conf_text = str_replace("<cher>",$femme ?
"Chère" : "Cher",$conf_text);
293 $mymail = new HermesMailer();
294 $mymail->setFrom($conf_mail);
295 $mymail->addTo("\"$prenom $nom\" <$forlife@polytechnique.org>");
296 $mymail->addCc($conf_mail);
297 $mymail->setSubject($conf_title);
298 $mymail->setTxtBody($conf_text);
301 /* on envoie les details de la transaction à telepaiement@ */
302 $mymail = new HermesMailer();
303 $mymail->setFrom("webmaster@polytechnique.org");
304 $mymail->addTo("telepaiement@polytechnique.org");
305 $mymail->setSubject($conf_title);
306 $msg = "utilisateur : $prenom $nom ($uid)\n".
307 "mail : $forlife@polytechnique.org\n\n".
308 "paiement : $conf_title ($conf_mail)\n".
309 "reference : $no_transaction\n".
310 "montant : $montant\n\n".
311 "dump de REQUEST:\n".
312 var_export($_REQUEST,true
);
313 $mymail->setTxtBody($msg);
316 $page->assign('texte', $conf_text);
317 $page->assign('erreur', $erreur);
319 function handler_admin(&$page, $action = 'list', $id = null
) {
320 $page->assign('xorg_title','Polytechnique.org - Administration - Paiements');
321 $page->assign('title', 'Gestion des télépaiements');
322 $table_editor = new PLTableEditor('admin/payments','paiement.paiements','id');
323 $table_editor->add_join_table('paiement.transactions','ref',true
);
324 $table_editor->add_sort_field('flags');
325 $table_editor->add_sort_field('id', true
, true
);
326 $table_editor->on_delete("UPDATE paiement.paiements SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
327 $table_editor->describe('text','intitulé',true
);
328 $table_editor->describe('url','site web',false
);
329 $table_editor->describe('montant_def','montant par défaut',false
);
330 $table_editor->describe('montant_min','montant minimum',false
);
331 $table_editor->describe('montant_max','montant maximum',false
);
332 $table_editor->describe('mail','email contact',true
);
333 $table_editor->describe('confirmation','message confirmation',false
);
334 $table_editor->apply($page, $action, $id);