Merge commit 'origin/master' into fusionax
[platal.git] / modules / payment / money / paypal.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 class PayPal
23 {
24 var $val_number;
25 var $urlform;
26 var $nomsite = "PayPal";
27 var $text;
28
29 var $infos;
30
31 function PayPal($val)
32 {
33 $this->val_number = $val;
34 }
35
36 function prepareform(&$pay)
37 {
38 // Documentation:
39 // https://www.paypal.com/developer
40 // Warning: the automatic return only works if we force the
41 // users to create a paypal account. We do not use it; thus
42 // the user must come back on the site.
43 global $globals, $platal;
44
45 $this->urlform = 'https://' . $globals->money->paypal_site . '/cgi-bin/webscr';
46 $req = XDB::query("SELECT IF(nom_usage!='', nom_usage, nom) AS nom
47 FROM auth_user_md5
48 WHERE user_id = {?}", S::v('uid'));
49 $name = $req->fetchOneCell();
50
51 $roboturl = str_replace("https://","http://",$globals->baseurl)
52 . '/' . $platal->ns . "payment/paypal_return/" . S::v('uid')
53 . "?comment=" . urlencode(Env::v('comment'));
54
55 $this->infos = array();
56
57 $this->infos['commercant'] = array(
58 'business' => $globals->money->paypal_compte,
59 'rm' => 2,
60 'return' => $roboturl,
61 'cn' => 'Commentaires',
62 'no_shipping' => 1,
63 'cbt' => empty($GLOBALS['IS_XNET_SITE']) ?
64 'Revenir sur polytechnique.org.' :
65 'Revenir sur polytechnique.net.'
66 );
67
68 $info_client = array(
69 'first_name' => S::v('prenom'),
70 'last_name' => $name,
71 'email' => S::user()->bestEmail()
72 );
73
74 $res = XDB::query(
75 "SELECT a.text, l.name AS city, a.postalCode AS zip, a.countryiId AS country,
76 IF(t1.display_tel != '', t1.display_tel, t2.display_tel) AS night_phone_b
77 FROM auth_user_quick AS q
78 LEFT JOIN profile_addresses AS a ON (q.user_id = a.pid AND FIND_IN_SET('current', a.flags))
79 LEFT JOIN profile_phones AS t1 ON (t1.uid = a.uid AND t1.link_type = 'address'
80 AND t1.link_id = a.adrid)
81 LEFT JOIN profile_phones AS t2 ON (t2.uid = a.uid AND t2.link_type = 'user'
82 AND t2.link_id = 0)
83 LEFT JOIN geoloc_localities AS l ON (l.id = a.localityId)
84 WHERE q.user_id = {?}
85 LIMIT 1",
86 S::v('uid'));
87 $this->infos['client'] = array_map('replace_accent', array_merge($info_client, $res->fetchOneAssoc()));
88 list($this->infos['client']['address1'], $this->infos['client']['address2']) =
89 explode("\n", Geocoder::getFirstLines($this->infos['client']['text'],
90 $this->infos['client']['zip'], 2));
91 unset($this->infos['client']['text']);
92
93 // We build the transaction's reference
94 $prefix = ($pay->flags->hasflag('unique')) ? str_pad("", 15, "0") : rand_url_id();
95 $fullref = substr("$prefix-xorg-{$pay->id}", -15);
96
97 $this->infos['commande'] = array(
98 'item_name' => replace_accent($pay->text),
99 'amount' => $this->val_number,
100 'currency_code' => 'EUR',
101 'custom' => $fullref
102 );
103
104 $this->infos['divers'] = array('cmd' => '_xclick');
105 }
106 }
107
108 $api = 'PayPal';
109
110 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
111 ?>