| 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 | 'admin/payments' => $this->make_hook('admin', AUTH_MDP, 'admin'), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | function handler_payment(&$page, $ref = -1) |
| 87 | { |
| 88 | global $globals; |
| 89 | |
| 90 | require_once 'profil.func.inc.php' ; |
| 91 | require_once dirname(__FILE__).'/payment/money.inc.php' ; |
| 92 | |
| 93 | $page->changeTpl('payment/index.tpl'); |
| 94 | $page->assign('xorg_title','Polytechnique.org - Télépaiements'); |
| 95 | |
| 96 | // initialisation |
| 97 | $op = Env::v('op', 'select'); |
| 98 | $meth = new PayMethod(Env::i('methode', -1)); |
| 99 | $pay = new Payment($ref); |
| 100 | |
| 101 | if($pay->flags->hasflag('old')){ |
| 102 | $page->trig("La transaction selectionnée est périmée."); |
| 103 | $pay = new Payment(); |
| 104 | } |
| 105 | $val = Env::v('montant') != 0 ? Env::v('montant') : $pay->montant_def; |
| 106 | |
| 107 | if (($e = $pay->check($val)) !== true) { |
| 108 | $page->trig($e); |
| 109 | } |
| 110 | |
| 111 | if ($op=='submit') { |
| 112 | $pay->init($val, $meth); |
| 113 | $pay->prepareform($pay); |
| 114 | } else { |
| 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); |
| 120 | |
| 121 | if ($res->total()) $page->assign('transactions', $res); |
| 122 | } |
| 123 | |
| 124 | $val = floor($val).".".substr(floor(($val - floor($val))*100+100),1); |
| 125 | $page->assign('montant',$val); |
| 126 | |
| 127 | $page->assign('meth', $meth); |
| 128 | $page->assign('pay', $pay); |
| 129 | $page->assign('evtlink', $pay->event()); |
| 130 | |
| 131 | $page->assign('prefix', $globals->money->mpay_tprefix); |
| 132 | } |
| 133 | |
| 134 | function handler_cyber_return(&$page, $uid = null) |
| 135 | { |
| 136 | require_once 'diogenes/diogenes.hermes.inc.php'; |
| 137 | |
| 138 | /* reference banque (numero de transaction) */ |
| 139 | $champ901 = clean_request('CHAMP901'); |
| 140 | /* cle d'acceptation */ |
| 141 | $champ905 = clean_request('CHAMP905'); |
| 142 | /* code retour */ |
| 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'); |
| 150 | /* devise */ |
| 151 | $champ202 = clean_request('CHAMP202'); |
| 152 | $montant = "$champ201 $champ202"; |
| 153 | |
| 154 | /* on extrait les informations sur l'utilisateur */ |
| 155 | $res = XDB::query(" |
| 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"); |
| 162 | } |
| 163 | |
| 164 | |
| 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"); |
| 168 | } |
| 169 | |
| 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"); |
| 175 | } |
| 176 | |
| 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)"); |
| 185 | } else{ |
| 186 | cb_erreur("erreur inconnue lors du paiement"); |
| 187 | } |
| 188 | } |
| 189 | |
| 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); |
| 194 | |
| 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); |
| 202 | |
| 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); |
| 209 | $mymail->send(); |
| 210 | |
| 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); |
| 224 | $mymail->send(); |
| 225 | exit; |
| 226 | } |
| 227 | |
| 228 | function handler_paypal_return(&$page, $uid = null) |
| 229 | { |
| 230 | $page->changeTpl('payment/retour_paypal.tpl'); |
| 231 | require_once 'diogenes/diogenes.hermes.inc.php'; |
| 232 | |
| 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'); |
| 237 | /* code retour */ |
| 238 | $status = clean_request('st'); |
| 239 | /* raison */ |
| 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'); |
| 245 | /* devise */ |
| 246 | $montant_dev = clean_request('cc'); |
| 247 | $montant = "$montant_nb $montant_dev"; |
| 248 | |
| 249 | /* on extrait le code de retour */ |
| 250 | if ($status != "Completed") { |
| 251 | if ($status) |
| 252 | paypal_erreur("erreur lors du paiement : $status - $reason"); |
| 253 | else |
| 254 | paypal_erreur("Paiement annulé", false); |
| 255 | } |
| 256 | |
| 257 | /* on extrait les informations sur l'utilisateur */ |
| 258 | $res = XDB::query(" |
| 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"); |
| 265 | } |
| 266 | |
| 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"); |
| 270 | } |
| 271 | |
| 272 | $ref = $matches[1]; |
| 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"); |
| 277 | } |
| 278 | |
| 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); |
| 283 | |
| 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); |
| 291 | |
| 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); |
| 298 | $mymail->send(); |
| 299 | |
| 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); |
| 313 | $mymail->send(); |
| 314 | |
| 315 | $page->assign('texte', $conf_text); |
| 316 | $page->assign('erreur', $erreur); |
| 317 | } |
| 318 | function handler_admin(&$page, $action = 'list', $id = null) { |
| 319 | require_once('../classes/PLTableEditor.php'); |
| 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->describe('text','intitulé',true); |
| 325 | $table_editor->describe('url','site web',false); |
| 326 | $table_editor->describe('montant_def','montant par défaut',false); |
| 327 | $table_editor->describe('montant_min','montant minimum',false); |
| 328 | $table_editor->describe('montant_max','montant maximum',false); |
| 329 | $table_editor->describe('mail','email contact',true); |
| 330 | $table_editor->describe('confirmation','message confirmation',false); |
| 331 | $table_editor->apply($page, $action, $id); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | ?> |