Fixes return url for paypal payments.
[platal.git] / modules / payment / money / paypal.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 $user = S::user();
47
48 $roboturl = str_replace("https://","http://",$globals->baseurl)
49 . '/' . $platal->ns . "payment/paypal_return/" . S::v('uid')
50 . "?comment=" . urlencode(Env::v('comment')) . '&display=' . Post::i('display');
51
52 $this->infos = array(
53 'commercant' => array(
54 'business' => $globals->money->paypal_compte,
55 'rm' => 2,
56 'return' => $roboturl,
57 'cn' => 'Commentaires',
58 'no_shipping' => 1,
59 'cbt' => empty($GLOBALS['IS_XNET_SITE']) ? 'Revenir sur polytechnique.org.' : 'Revenir sur polytechnique.net.'
60 )
61 );
62
63 $info_client = array(
64 'first_name' => $user->firstName(),
65 'last_name' => $user->lastName(),
66 'email' => $user->bestEmail()
67 );
68
69 if ($user->hasProfile()) {
70 $res = XDB::query("SELECT pa.text, GROUP_CONCAT(pace2.short_name) AS city,
71 GROUP_CONCAT(pace3.short_name) AS zip, GROUP_CONCAT(pace1.short_name) AS country,
72 IF(pp1.display_tel != '', pp1.display_tel, pp2.display_tel) AS night_phone_b
73 FROM profile_addresses AS pa
74 LEFT JOIN profile_phones AS pp1 ON (pp1.pid = pa.pid AND pp1.link_type = 'address' AND pp1.link_id = pa.id)
75 LEFT JOIN profile_phones AS pp2 ON (pp2.pid = pa.pid AND pp2.link_type = 'user' AND pp2.link_id = 0)
76 LEFT JOIN profile_addresses_components AS pc ON (pa.pid = pc.pid AND pa.jobid = pc.jobid AND pa.groupid = pc.groupid
77 AND pa.type = pc.type AND pa.id = pc.id)
78 LEFT JOIN profile_addresses_components_enum AS pace1 ON (FIND_IN_SET('country', pace1.types) AND pace1.id = pc.component_id)
79 LEFT JOIN profile_addresses_components_enum AS pace2 ON (FIND_IN_SET('locality', pace2.types) AND pace2.id = pc.component_id)
80 LEFT JOIN profile_addresses_components_enum AS pace3 ON (FIND_IN_SET('postal_code', pace3.types) AND pace3.id = pc.component_id)
81 WHERE pa.pid = {?} AND FIND_IN_SET('current', pa.flags)
82 GROUP BY pa.pid, pa.jobid, pa.groupid, pa.id, pa.type
83 LIMIT 1",
84 $user->profile()->id());
85 $this->infos['client'] = array_map('replace_accent', array_merge($info_client, $res->fetchOneAssoc()));
86 list($this->infos['client']['address1'], $this->infos['client']['address2']) =
87 explode("\n", Geocoder::getFirstLines($this->infos['client']['text'],
88 $this->infos['client']['zip'], 2));
89 unset($this->infos['client']['text']);
90 } else {
91 $this->infos['client'] = replace_accent($info_client);
92 }
93
94 // We build the transaction's reference
95 $prefix = ($pay->flags->hasflag('unique')) ? str_pad("", 15, "0") : rand_url_id();
96 $fullref = substr("$prefix-xorg-{$pay->id}", -15);
97
98 $this->infos['commande'] = array(
99 'item_name' => replace_accent($pay->text),
100 'amount' => $this->val_number,
101 'currency_code' => 'EUR',
102 'custom' => $fullref
103 );
104
105 $this->infos['divers'] = array('cmd' => '_xclick');
106 }
107 }
108
109 $api = 'PayPal';
110
111 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
112 ?>