1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* oledrion |
14
|
|
|
* |
15
|
|
|
* @copyright {@link http://xoops.org/ XOOPS Project} |
16
|
|
|
* @license {@link http://www.fsf.org/copyleft/gpl.html GNU public license} |
17
|
|
|
* @author Hervé Thouzard (http://www.herve-thouzard.com/) |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Paypal Gateway |
22
|
|
|
*/ |
23
|
|
|
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined'); |
|
|
|
|
24
|
|
|
|
25
|
|
|
class Oledrion_paypal extends Oledrion_gateway |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Oledrion_paypal constructor. |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Retourne des informations sur la passerelle de paiement |
37
|
|
|
* |
38
|
|
|
* @return array |
|
|
|
|
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function setGatewayInformation() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$gateway = array(); |
43
|
|
|
$gateway['name'] = 'Paypal'; |
44
|
|
|
$gateway['foldername'] = 'paypal'; |
45
|
|
|
$gateway['version'] = '1.1'; |
46
|
|
|
$gateway['description'] = 'PayPal is the safer, easier way to pay and get paid online'; |
47
|
|
|
$gateway['author'] = 'Instant Zero (http://www.herve-thouzard.com/)'; |
48
|
|
|
$gateway['credits'] = 'Hervé Thouzard'; |
49
|
|
|
$gateway['releaseDate'] = 20081215; |
50
|
|
|
$this->gatewayInformation = $gateway; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retourne le formulaire utilisé pour paramétrer la passerelle de paiement |
55
|
|
|
* |
56
|
|
|
* @param $postUrl |
57
|
|
|
* @return object de type XoopsThemeForm |
58
|
|
|
*/ |
59
|
|
|
public function getParametersForm($postUrl) |
60
|
|
|
{ |
61
|
|
|
require $this->getGatewayLanguageFile(); |
62
|
|
|
|
63
|
|
|
$sform = new XoopsThemeForm(_OLEDRION_PAYPAL_PARAMETERS . ' - ' . $this->gatewayInformation['name'], 'frmPaypal', $postUrl); |
64
|
|
|
// You must specify the gateway folder's name |
65
|
|
|
$sform->addElement(new XoopsFormHidden('gateway', $this->gatewayInformation['foldername'])); |
66
|
|
|
|
67
|
|
|
// Adresse email Paypal du compte marchand |
68
|
|
|
$paypal_email = new XoopsFormText(_OLEDRION_PAYPAL_EMAIL, 'paypal_email', 50, 255, $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($this->gatewayInformation['foldername'], 'paypal_email')); |
|
|
|
|
69
|
|
|
$paypal_email->setDescription(_OLEDRION_PAYPAL_EMAILDSC); |
70
|
|
|
$sform->addElement($paypal_email, true); |
71
|
|
|
|
72
|
|
|
// Libellé de la monnaie pour Paypal |
73
|
|
|
$paypal_money = new XoopsFormSelect(_OLEDRION_PAYPAL_MONEY_P, 'paypal_money', $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($this->gatewayInformation['foldername'], 'paypal_money')); |
|
|
|
|
74
|
|
|
$paypal_money->addOptionArray(array( |
75
|
|
|
'AUD' => 'Australian Dollar', |
76
|
|
|
'CAD' => 'Canadian Dollar', |
77
|
|
|
'CHF' => 'Swiss Franc', |
78
|
|
|
'CZK' => 'Czech Koruna', |
79
|
|
|
'DKK' => 'Danish Krone', |
80
|
|
|
'EUR' => 'Euro', |
81
|
|
|
'GBP' => 'Pound Sterling', |
82
|
|
|
'HKD' => 'Hong Kong Dollar', |
83
|
|
|
'HUF' => 'Hungarian Forint', |
84
|
|
|
'JPY' => 'Japanese Yen', |
85
|
|
|
'NOK' => 'Norwegian Krone', |
86
|
|
|
'NZD' => 'New Zealand Dollar', |
87
|
|
|
'PLN' => 'Polish Zloty', |
88
|
|
|
'SEK' => 'Swedish Krona', |
89
|
|
|
'SGD' => 'Singapore Dollar', |
90
|
|
|
'USD' => 'U.S. Dollar' |
91
|
|
|
)); |
92
|
|
|
$sform->addElement($paypal_money, true); |
93
|
|
|
|
94
|
|
|
// Paypal en mode test ? |
95
|
|
|
$paypal_test = new XoopsFormRadioYN(_OLEDRION_PAYPAL_TEST, 'paypal_test', $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($this->gatewayInformation['foldername'], 'paypal_test')); |
|
|
|
|
96
|
|
|
$sform->addElement($paypal_test, true); |
97
|
|
|
|
98
|
|
|
// Forcé à vrai ... |
99
|
|
|
$sform->addElement(new XoopsFormHidden('use_ipn', 1)); |
100
|
|
|
|
101
|
|
|
$button_tray = new XoopsFormElementTray('', ''); |
102
|
|
|
$submit_btn = new XoopsFormButton('', 'post', _AM_OLEDRION_GATEWAYS_UPDATE, 'submit'); |
103
|
|
|
$button_tray->addElement($submit_btn); |
104
|
|
|
$sform->addElement($button_tray); |
105
|
|
|
|
106
|
|
|
return $sform; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sauvegarde des paramètres de la passerelle de paiement |
111
|
|
|
* |
112
|
|
|
* @param array $data Les données du formulaire |
113
|
|
|
* @return boolean Le résultat de l'enregistrement des données |
114
|
|
|
*/ |
115
|
|
|
public function saveParametersForm($data) |
116
|
|
|
{ |
117
|
|
|
$parameters = array('paypal_email', 'paypal_money', 'paypal_test', 'use_ipn'); |
118
|
|
|
// On commence par supprimer les valeurs actuelles |
119
|
|
|
$gatewayName = $this->gatewayInformation['foldername']; |
120
|
|
|
$this->handlers->h_oledrion_gateways_options->deleteGatewayOptions($gatewayName); |
|
|
|
|
121
|
|
|
foreach ($parameters as $parameter) { |
122
|
|
|
if (!$this->handlers->h_oledrion_gateways_options->setGatewayOptionValue($gatewayName, $parameter, $data[$parameter])) { |
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Formate le montant au format Paypal |
132
|
|
|
* @param $amount |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
private function formatAmount($amount) |
136
|
|
|
{ |
137
|
|
|
return number_format($amount, 2, '.', ''); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Retourne l'url vers laquelle rediriger l'utilisateur pour le paiement en ligne |
142
|
|
|
* |
143
|
|
|
* @param $cmd_total |
144
|
|
|
* @param $cmd_id |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function getRedirectURL($cmd_total, $cmd_id) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$test_mode = (int)$this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($this->gatewayInformation['foldername'], 'paypal_test'); |
|
|
|
|
150
|
|
|
if ($test_mode == 1) { |
151
|
|
|
return 'https://www.sandbox.paypal.com/cgi-bin/webscr'; |
152
|
|
|
} else { |
153
|
|
|
return 'https://www.paypal.com/cgi-bin/webscr'; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Retourne les éléments à ajouter au formulaire en tant que zones cachées |
159
|
|
|
* |
160
|
|
|
* @param array $order La commande client |
161
|
|
|
* @param array |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getCheckoutFormContent($order) |
165
|
|
|
{ |
166
|
|
|
global $xoopsConfig; |
|
|
|
|
167
|
|
|
$gatewayName = $this->gatewayInformation['foldername']; |
168
|
|
|
$paypal_money = $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($gatewayName, 'paypal_money'); |
|
|
|
|
169
|
|
|
$paypal_email = $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($gatewayName, 'paypal_email'); |
|
|
|
|
170
|
|
|
$use_ipn = (int)$this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($gatewayName, 'use_ipn'); |
|
|
|
|
171
|
|
|
|
172
|
|
|
$ret = array(); |
173
|
|
|
$ret['cmd'] = '_xclick'; |
174
|
|
|
$ret['upload'] = '1'; |
175
|
|
|
$ret['currency_code'] = $paypal_money; |
176
|
|
|
$ret['business'] = $paypal_email; |
177
|
|
|
$ret['return'] = OLEDRION_URL . 'thankyou.php'; // Page (générique) de remerciement après paiement |
178
|
|
|
$ret['image_url'] = XOOPS_URL . '/images/logo.gif'; |
179
|
|
|
$ret['cpp_header_image'] = XOOPS_URL . '/images/logo.gif'; |
180
|
|
|
$ret['invoice'] = $order->getVar('cmd_id'); |
|
|
|
|
181
|
|
|
$ret['item_name'] = _OLEDRION_COMMAND . $order->getVar('cmd_id') . ' - ' . Oledrion_utils::makeHrefTitle($xoopsConfig['sitename']); |
|
|
|
|
182
|
|
|
$ret['item_number'] = $order->getVar('cmd_id'); |
|
|
|
|
183
|
|
|
$ret['tax'] = 0; // ajout 25/03/2008 |
|
|
|
|
184
|
|
|
$ret['amount'] = $this->formatAmount((float)$order->getVar('cmd_total', 'n')); |
|
|
|
|
185
|
|
|
$ret['custom'] = $order->getVar('cmd_id'); |
|
|
|
|
186
|
|
|
//$ret['rm'] = 2; // Renvoyer les données par POST (normalement) |
|
|
|
|
187
|
|
|
$ret['email'] = $order->getVar('cmd_email'); |
|
|
|
|
188
|
|
|
if (xoops_trim($order->getVar('cmd_cancel')) != '') { // URL à laquelle le navigateur du client est ramené si le paiement est annulé |
|
|
|
|
189
|
|
|
$ret['cancel_return'] = OLEDRION_URL . 'cancel-payment.php?id=' . $order->getVar('cmd_cancel'); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
if ($use_ipn == 1) { |
192
|
|
|
$ret['notify_url'] = OLEDRION_URL . 'gateway-notify.php'; // paypal-notify.php |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $ret; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Retourne la liste des pays à utiliser dans le formulaire de saisie des informations client (checkout.php) |
200
|
|
|
* |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function getCountriesList() |
204
|
|
|
{ |
205
|
|
|
require_once XOOPS_ROOT_PATH . '/class/xoopslists.php'; |
206
|
|
|
|
207
|
|
|
return XoopsLists::getCountryList(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Utilisée lors du dialog avec Paypal dans le cas de l'utilisation de l'IPN |
212
|
|
|
* Note : Spécifique Paypal |
213
|
|
|
* |
214
|
|
|
* @return string L'URL chez Paypal à appeler pour obtenir des informations |
215
|
|
|
*/ |
216
|
|
View Code Duplication |
private function getdialogURL() |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
$test_mode = (int)$this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($this->gatewayInformation['foldername'], 'paypal_test'); |
|
|
|
|
219
|
|
|
if ($test_mode == 1) { |
220
|
|
|
return 'www.sandbox.paypal.com'; |
221
|
|
|
} else { |
222
|
|
|
return 'www.paypal.com'; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Dialogue avec la passerelle de paiement pour indiquer l'état de la commande |
228
|
|
|
* L'appellant se charge de vérifier que le fichier log existe |
229
|
|
|
* |
230
|
|
|
* @param string $gatewaysLogPath Le chemin d'accès complet au fichier log |
231
|
|
|
* @return void |
232
|
|
|
*/ |
233
|
|
|
public function gatewayNotify($gatewaysLogPath) |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
error_reporting(0); |
236
|
|
|
@$xoopsLogger->activated = false; |
|
|
|
|
237
|
|
|
|
238
|
|
|
$log = ''; |
239
|
|
|
$req = 'cmd=_notify-validate'; |
240
|
|
|
$slashes = get_magic_quotes_gpc(); |
241
|
|
|
foreach ($_POST as $key => $value) { |
242
|
|
|
if ($slashes) { |
243
|
|
|
$log .= "$key=" . stripslashes($value) . "\n"; |
244
|
|
|
$value = urlencode(stripslashes($value)); |
245
|
|
|
} else { |
246
|
|
|
$log .= "$key=" . $value . "\n"; |
247
|
|
|
$value = urlencode($value); |
248
|
|
|
} |
249
|
|
|
$req .= "&$key=$value"; |
250
|
|
|
} |
251
|
|
|
$url = $this->getdialogURL(); |
252
|
|
|
$gatewayName = $this->gatewayInformation['foldername']; |
253
|
|
|
$paypal_email = $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($gatewayName, 'paypal_email'); |
|
|
|
|
254
|
|
|
$paypal_money = $this->handlers->h_oledrion_gateways_options->getGatewayOptionValue($gatewayName, 'paypal_money'); |
|
|
|
|
255
|
|
|
$header = ''; |
256
|
|
|
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; |
257
|
|
|
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
258
|
|
|
$header .= 'Content-Length: ' . strlen($req) . "\r\n\r\n"; |
259
|
|
|
$errno = 0; |
260
|
|
|
$errstr = ''; |
261
|
|
|
$fp = fsockopen($url, 80, $errno, $errstr, 30); |
262
|
|
|
if ($fp) { |
263
|
|
|
fwrite($fp, "$header$req"); |
264
|
|
|
while (!feof($fp)) { |
265
|
|
|
$res = fgets($fp, 1024); |
266
|
|
|
if (strcmp($res, 'VERIFIED') == 0) { |
267
|
|
|
$log .= "VERIFIED\t"; |
268
|
|
|
$paypalok = true; |
269
|
|
|
if (strtoupper($_POST['payment_status']) !== 'COMPLETED') { |
270
|
|
|
$paypalok = false; |
271
|
|
|
} |
272
|
|
|
if (strtoupper($_POST['receiver_email']) != strtoupper($paypal_email)) { |
273
|
|
|
$paypalok = false; |
274
|
|
|
} |
275
|
|
|
if (strtoupper($_POST['mc_currency']) != strtoupper($paypal_money)) { |
276
|
|
|
$paypalok = false; |
277
|
|
|
} |
278
|
|
|
if (!$_POST['custom']) { |
279
|
|
|
$paypalok = false; |
280
|
|
|
} |
281
|
|
|
$montant = $_POST['mc_gross']; |
282
|
|
|
if ($paypalok) { |
283
|
|
|
$ref = (int)$_POST['custom']; // Numéro de la commande |
284
|
|
|
$commande = null; |
|
|
|
|
285
|
|
|
$commande = $this->handlers->h_oledrion_commands->get($ref); |
|
|
|
|
286
|
|
|
if (is_object($commande)) { |
287
|
|
|
if ($montant == $commande->getVar('cmd_total')) { // Commande vérifiée |
288
|
|
|
$this->handlers->h_oledrion_commands->validateOrder($commande); // Validation de la commande et mise à jour des stocks |
|
|
|
|
289
|
|
|
} else { |
290
|
|
|
$this->handlers->h_oledrion_commands->setFraudulentOrder($commande); |
|
|
|
|
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} else { |
294
|
|
|
if (isset($_POST['custom'])) { |
295
|
|
|
$ref = (int)$_POST['custom']; |
296
|
|
|
$commande = null; |
|
|
|
|
297
|
|
|
$commande = $this->handlers->h_oledrion_commands->get($ref); |
|
|
|
|
298
|
|
|
if (is_object($commande)) { |
299
|
|
|
switch (strtoupper($_POST['payment_status'])) { |
300
|
|
|
case 'PENDING': |
301
|
|
|
$this->handlers->h_oledrion_commands->setOrderPending($commande); |
|
|
|
|
302
|
|
|
break; |
303
|
|
|
case 'FAILED': |
304
|
|
|
$this->handlers->h_oledrion_commands->setOrderFailed($commande); |
|
|
|
|
305
|
|
|
break; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} else { |
311
|
|
|
$log .= "$res\n"; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
fclose($fp); |
315
|
|
|
} else { |
316
|
|
|
$log .= "Error with the fsockopen function, unable to open communication ' : ($errno) $errstr\n"; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
// Ecriture dans le fichier log |
320
|
|
|
$fp = fopen($gatewaysLogPath, 'a'); |
321
|
|
|
if ($fp) { |
322
|
|
|
fwrite($fp, str_repeat('-', 120) . "\n"); |
323
|
|
|
fwrite($fp, date('d/m/Y H:i:s') . "\n"); |
324
|
|
|
if (isset($_POST['txn_id'])) { |
325
|
|
|
fwrite($fp, 'Transaction : ' . $_POST['txn_id'] . "\n"); |
326
|
|
|
} |
327
|
|
|
fwrite($fp, 'Result : ' . $log . "\n"); |
328
|
|
|
fclose($fp); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.