Quick Search:

View

Revision:

Diff

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