Quick Search:

View

Revision:

Diff

Diff from 1498 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/payment.php

Annotated File View

hpdl
1
1 <?php
2 /*
hpdl
146
3   $Id: payment.php 1498 2007-03-29 14:04:50Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
410
8   Copyright (c) 2006 osCommerce
hpdl
1
9
hpdl
1498
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
1
13 */
14
hpdl
554
15   include(dirname(__FILE__) . '/credit_card.php');
16
hpdl
443
17   class osC_Payment {
18     var $selected_module;
hpdl
1
19
hpdl
443
20     var $_modules = array(),
21         $_group = 'payment';
22
hpdl
1
23 // class constructor
hpdl
443
24     function osC_Payment($module = '') {
25       global $osC_Database, $osC_Language;
hpdl
383
26
hpdl
443
27       $Qmodules = $osC_Database->query('select code from :table_templates_boxes where modules_group = "payment"');
28       $Qmodules->bindTable(':table_templates_boxes', TABLE_TEMPLATES_BOXES);
29       $Qmodules->setCache('modules-payment');
30       $Qmodules->execute();
hpdl
1
31
hpdl
443
32       while ($Qmodules->next()) {
33         $this->_modules[] = $Qmodules->value('code');
34       }
hpdl
1
35
hpdl
443
36       $Qmodules->freeResult();
hpdl
1
37
hpdl
443
38       if (empty($this->_modules) === false) {
39         if ((empty($module) === false) && in_array($module, $this->_modules)) {
40           $this->_modules = array($module);
41           $this->selected_module = 'osC_Payment_' . $module;
hpdl
1
42         }
43
hpdl
410
44         $osC_Language->load('modules-payment');
45
hpdl
443
46         foreach ($this->_modules as $modules) {
47           include('includes/modules/payment/' . $modules . '.' . substr(basename(__FILE__), (strrpos(basename(__FILE__), '.')+1)));
hpdl
1
48
hpdl
443
49           $module_class = 'osC_Payment_' . $modules;
hpdl
1
50
hpdl
443
51           $GLOBALS[$module_class] = new $module_class();
hpdl
1
52         }
53
hpdl
443
54         usort($this->_modules, array('osC_Payment', '_usortModules'));
55
hpdl
757
56         if ( (!empty($module)) && (in_array($module, $this->_modules)) && (isset($GLOBALS['osC_Payment_' . $module]->form_action_url)) ) {
hpdl
443
57           $this->form_action_url = $GLOBALS['osC_Payment_' . $module]->form_action_url;
hpdl
1
58         }
59       }
60     }
61
62 // class methods
hpdl
554
63     function sendTransactionToGateway($url, $parameters, $header = '', $method = 'post', $certificate = '') {
hpdl
610
64       if (empty($header) || !is_array($header)) {
hpdl
554
65         $header = array();
66       }
67
68       $server = parse_url($url);
69
70       if (isset($server['port']) === false) {
71         $server['port'] = ($server['scheme'] == 'https') ? 443 : 80;
72       }
73
74       if (isset($server['path']) === false) {
75         $server['path'] = '/';
76       }
77
78       if (isset($server['user']) && isset($server['pass'])) {
79         $header[] = 'Authorization: Basic ' . base64_encode($server['user'] . ':' . $server['pass']);
80       }
81
hpdl
610
82       $connection_method = 0;
hpdl
554
83
hpdl
610
84       if (function_exists('curl_init')) {
85         $connection_method = 1;
86       } elseif ( ($server['scheme'] == 'http') || (($server['scheme'] == 'https') && extension_loaded('openssl')) ) {
87         if (function_exists('stream_context_create')) {
88           $connection_method = 3;
89         } else {
90           $connection_method = 2;
91         }
hpdl
554
92       }
93
hpdl
610
94       $result = '';
hpdl
554
95
hpdl
610
96       switch ($connection_method) {
97         case 1:
98           $curl = curl_init($server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : ''));
99           curl_setopt($curl, CURLOPT_PORT, $server['port']);
hpdl
554
100
hpdl
610
101           if (!empty($header)) {
102             curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
103           }
hpdl
554
104
hpdl
610
105           if (!empty($certificate)) {
106             curl_setopt($curl, CURLOPT_SSLCERT, $certificate);
107           }
hpdl
554
108
hpdl
610
109           curl_setopt($curl, CURLOPT_HEADER, 0);
110           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
111           curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
112           curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
113           curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
114           curl_setopt($curl, CURLOPT_POST, 1);
115           curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
hpdl
554
116
hpdl
610
117           $result = curl_exec($curl);
hpdl
554
118
hpdl
610
119           curl_close($curl);
hpdl
554
120
hpdl
610
121           break;
hpdl
554
122
hpdl
610
123         case 2:
124           if ($fp = @fsockopen(($server['scheme'] == 'https' ? 'ssl' : $server['scheme']) . '://' . $server['host'], $server['port'])) {
125             @fputs($fp, 'POST ' . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : '') . ' HTTP/1.1' . "\r\n" .
126                         'Host: ' . $server['host'] . "\r\n" .
127                         'Content-type: application/x-www-form-urlencoded' . "\r\n" .
128                         'Content-length: ' . strlen($parameters) . "\r\n" .
129                         (!empty($header) ? implode("\r\n", $header) . "\r\n" : '') .
130                         'Connection: close' . "\r\n\r\n" .
131                         $parameters . "\r\n\r\n");
hpdl
554
132
hpdl
610
133             $result = @stream_get_contents($fp);
hpdl
554
134
hpdl
610
135             @fclose($fp);
hpdl
554
136
hpdl
610
137             $result = trim(substr($result, strpos($result, "\r\n\r\n", strpos(strtolower($result), 'content-length:'))));
138           }
139
140           break;
141
142         case 3:
143           $options = array('http' => array('method' => 'POST',
144                                            'header' => 'Host: ' . $server['host'] . "\r\n" .
145                                                        'Content-type: application/x-www-form-urlencoded' . "\r\n" .
146                                                        'Content-length: ' . strlen($parameters) . "\r\n" .
147                                                        (!empty($header) ? implode("\r\n", $header) . "\r\n" : '') .
148                                                        'Connection: close',
149                                            'content' => $parameters));
150
151           if (!empty($certificate)) {
152             $options['ssl'] = array('local_cert' => $certificate);
153           }
154
155           $context = stream_context_create($options);
156
157           if ($fp = fopen($url, 'r', false, $context)) {
158             $result = '';
159
160             while (!feof($fp)) {
161               $result .= fgets($fp, 4096);
162             }
163
164             fclose($fp);
165           }
166
167           break;
168
169         default:
170           exec(escapeshellarg(CFG_APP_CURL) . ' -d ' . escapeshellarg($parameters) . ' "' . $server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : '') . '" -P ' . $server['port'] . ' -k' . (!empty($header) ? ' -H ' . escapeshellarg(implode("\r\n", $header)) : '') . (!empty($certificate) ? ' -E ' . escapeshellarg($certificate) : ''), $result);
171           $result = implode("\n", $result);
hpdl
554
172       }
173
174       return $result;
175     }
176
hpdl
443
177     function getCode() {
178       return $this->_code;
179     }
180
181     function getTitle() {
182       return $this->_title;
183     }
184
185     function getDescription() {
186       return $this->_description;
187     }
188
hpdl
554
189     function getMethodTitle() {
190       return $this->_method_title;
191     }
192
193     function isEnabled() {
hpdl
443
194       return $this->_status;
195     }
196
197     function getSortOrder() {
198       return $this->_sort_order;
199     }
200
hpdl
554
201     function getJavascriptBlock() {
hpdl
1
202     }
203
hpdl
554
204     function getJavascriptBlocks() {
hpdl
410
205       global $osC_Language;
206
hpdl
1
207       $js = '';
hpdl
443
208       if (is_array($this->_modules)) {
hpdl
368
209         $js = '<script type="text/javascript"><!-- ' . "\n" .
hpdl
1
210               'function check_form() {' . "\n" .
211               '  var error = 0;' . "\n" .
hpdl
410
212               '  var error_message = "' . $osC_Language->get('js_error') . '";' . "\n" .
hpdl
1
213               '  var payment_value = null;' . "\n" .
hpdl
554
214               '  if (document.checkout_payment.payment_method.length) {' . "\n" .
215               '    for (var i=0; i<document.checkout_payment.payment_method.length; i++) {' . "\n" .
216               '      if (document.checkout_payment.payment_method[i].checked) {' . "\n" .
217               '        payment_value = document.checkout_payment.payment_method[i].value;' . "\n" .
hpdl
1
218               '      }' . "\n" .
219               '    }' . "\n" .
hpdl
554
220               '  } else if (document.checkout_payment.payment_method.checked) {' . "\n" .
221               '    payment_value = document.checkout_payment.payment_method.value;' . "\n" .
222               '  } else if (document.checkout_payment.payment_method.value) {' . "\n" .
223               '    payment_value = document.checkout_payment.payment_method.value;' . "\n" .
hpdl
1
224               '  }' . "\n\n";
225
hpdl
443
226         foreach ($this->_modules as $module) {
hpdl
554
227           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
228             $js .= $GLOBALS['osC_Payment_' . $module]->getJavascriptBlock();
hpdl
1
229           }
230         }
231
232         $js .= "\n" . '  if (payment_value == null) {' . "\n" .
hpdl
410
233                '    error_message = error_message + "' . $osC_Language->get('js_no_payment_module_selected') . '\n";' . "\n" .
hpdl
1
234                '    error = 1;' . "\n" .
235                '  }' . "\n\n" .
236                '  if (error == 1) {' . "\n" .
237                '    alert(error_message);' . "\n" .
238                '    return false;' . "\n" .
239                '  } else {' . "\n" .
240                '    return true;' . "\n" .
241                '  }' . "\n" .
242                '}' . "\n" .
243                '//--></script>' . "\n";
244       }
245
246       return $js;
247     }
248
249     function selection() {
250       $selection_array = array();
251
hpdl
443
252       foreach ($this->_modules as $module) {
hpdl
554
253         if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
443
254           $selection = $GLOBALS['osC_Payment_' . $module]->selection();
255           if (is_array($selection)) $selection_array[] = $selection;
hpdl
1
256         }
257       }
258
259       return $selection_array;
260     }
261
262     function pre_confirmation_check() {
hpdl
443
263       if (is_array($this->_modules)) {
hpdl
831
264         if (isset($GLOBALS[$this->selected_module]) && is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
265           $GLOBALS[$this->selected_module]->pre_confirmation_check();
266         }
267       }
268     }
269
270     function confirmation() {
hpdl
443
271       if (is_array($this->_modules)) {
hpdl
831
272         if (isset($GLOBALS[$this->selected_module]) && is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
273           return $GLOBALS[$this->selected_module]->confirmation();
274         }
275       }
276     }
277
278     function process_button() {
hpdl
443
279       if (is_array($this->_modules)) {
hpdl
831
280         if (isset($GLOBALS[$this->selected_module]) && is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
281           return $GLOBALS[$this->selected_module]->process_button();
282         }
283       }
284     }
285
hpdl
554
286     function process() {
hpdl
443
287       if (is_array($this->_modules)) {
hpdl
831
288         if (isset($GLOBALS[$this->selected_module]) && is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
554
289           return $GLOBALS[$this->selected_module]->process();
hpdl
1
290         }
291       }
292     }
293
294     function get_error() {
hpdl
443
295       if (is_array($this->_modules)) {
hpdl
831
296         if (isset($GLOBALS[$this->selected_module]) && is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
297           return $GLOBALS[$this->selected_module]->get_error();
298         }
299       }
300     }
hpdl
443
301
302     function hasActionURL() {
303       if (is_array($this->_modules)) {
hpdl
831
304         if (isset($GLOBALS[$this->selected_module]) && is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
443
305           if (isset($GLOBALS[$this->selected_module]->form_action_url) && (empty($GLOBALS[$this->selected_module]->form_action_url) === false)) {
306             return true;
307           }
308         }
309       }
310
311       return false;
312     }
313
314     function getActionURL() {
315       return $GLOBALS[$this->selected_module]->form_action_url;
316     }
317
318     function hasActive() {
319       static $has_active;
320
321       if (isset($has_active) === false) {
322         $has_active = false;
323
324         foreach ($this->_modules as $module) {
hpdl
554
325           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
443
326             $has_active = true;
327             break;
328           }
329         }
330       }
331
332       return $has_active;
333     }
334
335     function numberOfActive() {
336       static $active;
337
338       if (isset($active) === false) {
339         $active = 0;
340
341         foreach ($this->_modules as $module) {
hpdl
554
342           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
443
343             $active++;
344           }
345         }
346       }
347
348       return $active;
349     }
350
351     function _usortModules($a, $b) {
352       if ($GLOBALS['osC_Payment_' . $a]->getSortOrder() == $GLOBALS['osC_Payment_' . $b]->getSortOrder()) {
353         return strnatcasecmp($GLOBALS['osC_Payment_' . $a]->getTitle(), $GLOBALS['osC_Payment_' . $a]->getTitle());
354       }
355
356       return ($GLOBALS['osC_Payment_' . $a]->getSortOrder() < $GLOBALS['osC_Payment_' . $b]->getSortOrder()) ? -1 : 1;
357     }
hpdl
1
358   }
359 ?>