Quick Search:

View

Revision:

Diff

Diff from 607 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 607 2006-07-05 10:17:57Z 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
607
62       if (empty($header) || !is_array($header)) {
hpdl
486
63         $header = array();
64       }
65
66       $server = parse_url($url);
67
68       if (isset($server['port']) === false) {
69         $server['port'] = ($server['scheme'] == 'https') ? 443 : 80;
70       }
71
72       if (isset($server['path']) === false) {
73         $server['path'] = '/';
74       }
75
76       if (isset($server['user']) && isset($server['pass'])) {
77         $header[] = 'Authorization: Basic ' . base64_encode($server['user'] . ':' . $server['pass']);
78       }
79
hpdl
607
80       $connection_method = 0;
hpdl
486
81
hpdl
607
82       if (function_exists('curl_init')) {
83         $connection_method = 1;
84       } elseif ( ($server['scheme'] == 'http') || (($server['scheme'] == 'https') && extension_loaded('openssl')) ) {
85         if (function_exists('stream_context_create')) {
86           $connection_method = 3;
87         } else {
88           $connection_method = 2;
89         }
hpdl
486
90       }
91
hpdl
607
92       $result = '';
hpdl
523
93
hpdl
607
94       switch ($connection_method) {
95         case 1:
96           $curl = curl_init($server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : ''));
97           curl_setopt($curl, CURLOPT_PORT, $server['port']);
hpdl
486
98
hpdl
607
99           if (!empty($header)) {
100             curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
101           }
hpdl
533
102
hpdl
607
103           if (!empty($certificate)) {
104             curl_setopt($curl, CURLOPT_SSLCERT, $certificate);
105           }
hpdl
486
106
hpdl
607
107           curl_setopt($curl, CURLOPT_HEADER, 0);
108           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
109           curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
110           curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
111           curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
112           curl_setopt($curl, CURLOPT_POST, 1);
113           curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
hpdl
486
114
hpdl
607
115           $result = curl_exec($curl);
hpdl
486
116
hpdl
607
117           curl_close($curl);
hpdl
486
118
hpdl
607
119           break;
hpdl
533
120
hpdl
607
121         case 2:
122           if ($fp = @fsockopen(($server['scheme'] == 'https' ? 'ssl' : $server['scheme']) . '://' . $server['host'], $server['port'])) {
123             @fputs($fp, 'POST ' . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : '') . ' HTTP/1.1' . "\r\n" .
124                         'Host: ' . $server['host'] . "\r\n" .
125                         'Content-type: application/x-www-form-urlencoded' . "\r\n" .
126                         'Content-length: ' . strlen($parameters) . "\r\n" .
127                         (!empty($header) ? implode("\r\n", $header) . "\r\n" : '') .
128                         'Connection: close' . "\r\n\r\n" .
129                         $parameters . "\r\n\r\n");
hpdl
533
130
hpdl
607
131             $result = @stream_get_contents($fp);
hpdl
533
132
hpdl
607
133             @fclose($fp);
hpdl
533
134
hpdl
607
135             $result = trim(substr($result, strpos($result, "\r\n\r\n", strpos(strtolower($result), 'content-length:'))));
136           }
137
138           break;
139
140         case 3:
141           $options = array('http' => array('method' => 'POST',
142                                            'header' => 'Host: ' . $server['host'] . "\r\n" .
143                                                        'Content-type: application/x-www-form-urlencoded' . "\r\n" .
144                                                        'Content-length: ' . strlen($parameters) . "\r\n" .
145                                                        (!empty($header) ? implode("\r\n", $header) . "\r\n" : '') .
146                                                        'Connection: close',
147                                            'content' => $parameters));
148
149           if (!empty($certificate)) {
150             $options['ssl'] = array('local_cert' => $certificate);
151           }
152
153           $context = stream_context_create($options);
154
155           if ($fp = fopen($url, 'r', false, $context)) {
156             $result = '';
157
158             while (!feof($fp)) {
159               $result .= fgets($fp, 4096);
160             }
161
162             fclose($fp);
163           }
164
165           break;
166
167         default:
168           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);
169           $result = implode("\n", $result);
hpdl
533
170       }
171
hpdl
486
172       return $result;
173     }
174
hpdl
432
175     function getCode() {
176       return $this->_code;
177     }
178
179     function getTitle() {
180       return $this->_title;
181     }
182
183     function getDescription() {
184       return $this->_description;
185     }
186
hpdl
486
187     function getMethodTitle() {
188       return $this->_method_title;
189     }
190
191     function isEnabled() {
hpdl
432
192       return $this->_status;
193     }
194
195     function getSortOrder() {
196       return $this->_sort_order;
197     }
198
hpdl
547
199     function getJavascriptBlock() {
hpdl
1
200     }
201
hpdl
547
202     function getJavascriptBlocks() {
hpdl
387
203       global $osC_Language;
204
hpdl
1
205       $js = '';
hpdl
434
206       if (is_array($this->_modules)) {
hpdl
241
207         $js = '<script type="text/javascript"><!-- ' . "\n" .
hpdl
1
208               'function check_form() {' . "\n" .
209               '  var error = 0;' . "\n" .
hpdl
387
210               '  var error_message = "' . $osC_Language->get('js_error') . '";' . "\n" .
hpdl
1
211               '  var payment_value = null;' . "\n" .
hpdl
486
212               '  if (document.checkout_payment.payment_method.length) {' . "\n" .
213               '    for (var i=0; i<document.checkout_payment.payment_method.length; i++) {' . "\n" .
214               '      if (document.checkout_payment.payment_method[i].checked) {' . "\n" .
215               '        payment_value = document.checkout_payment.payment_method[i].value;' . "\n" .
hpdl
1
216               '      }' . "\n" .
217               '    }' . "\n" .
hpdl
486
218               '  } else if (document.checkout_payment.payment_method.checked) {' . "\n" .
219               '    payment_value = document.checkout_payment.payment_method.value;' . "\n" .
220               '  } else if (document.checkout_payment.payment_method.value) {' . "\n" .
221               '    payment_value = document.checkout_payment.payment_method.value;' . "\n" .
hpdl
1
222               '  }' . "\n\n";
223
hpdl
434
224         foreach ($this->_modules as $module) {
hpdl
486
225           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
547
226             $js .= $GLOBALS['osC_Payment_' . $module]->getJavascriptBlock();
hpdl
1
227           }
228         }
229
230         $js .= "\n" . '  if (payment_value == null) {' . "\n" .
hpdl
390
231                '    error_message = error_message + "' . $osC_Language->get('js_no_payment_module_selected') . '\n";' . "\n" .
hpdl
1
232                '    error = 1;' . "\n" .
233                '  }' . "\n\n" .
234                '  if (error == 1) {' . "\n" .
235                '    alert(error_message);' . "\n" .
236                '    return false;' . "\n" .
237                '  } else {' . "\n" .
238                '    return true;' . "\n" .
239                '  }' . "\n" .
240                '}' . "\n" .
241                '//--></script>' . "\n";
242       }
243
244       return $js;
245     }
246
247     function selection() {
248       $selection_array = array();
249
hpdl
434
250       foreach ($this->_modules as $module) {
hpdl
486
251         if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
252           $selection = $GLOBALS['osC_Payment_' . $module]->selection();
253           if (is_array($selection)) $selection_array[] = $selection;
hpdl
1
254         }
255       }
256
257       return $selection_array;
258     }
259
260     function pre_confirmation_check() {
hpdl
434
261       if (is_array($this->_modules)) {
hpdl
486
262         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
263           $GLOBALS[$this->selected_module]->pre_confirmation_check();
264         }
265       }
266     }
267
268     function confirmation() {
hpdl
434
269       if (is_array($this->_modules)) {
hpdl
486
270         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
271           return $GLOBALS[$this->selected_module]->confirmation();
272         }
273       }
274     }
275
276     function process_button() {
hpdl
434
277       if (is_array($this->_modules)) {
hpdl
486
278         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
279           return $GLOBALS[$this->selected_module]->process_button();
280         }
281       }
282     }
283
hpdl
523
284     function process() {
hpdl
434
285       if (is_array($this->_modules)) {
hpdl
486
286         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
523
287           return $GLOBALS[$this->selected_module]->process();
hpdl
1
288         }
289       }
290     }
291
292     function get_error() {
hpdl
434
293       if (is_array($this->_modules)) {
hpdl
486
294         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
295           return $GLOBALS[$this->selected_module]->get_error();
296         }
297       }
298     }
hpdl
431
299
hpdl
434
300     function hasActionURL() {
301       if (is_array($this->_modules)) {
hpdl
486
302         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
434
303           if (isset($GLOBALS[$this->selected_module]->form_action_url) && (empty($GLOBALS[$this->selected_module]->form_action_url) === false)) {
304             return true;
305           }
306         }
307       }
308
309       return false;
310     }
311
312     function getActionURL() {
313       return $GLOBALS[$this->selected_module]->form_action_url;
314     }
315
hpdl
431
316     function hasActive() {
317       static $has_active;
318
319       if (isset($has_active) === false) {
320         $has_active = false;
321
hpdl
434
322         foreach ($this->_modules as $module) {
hpdl
486
323           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
324             $has_active = true;
325             break;
326           }
327         }
328       }
329
330       return $has_active;
331     }
332
333     function numberOfActive() {
334       static $active;
335
336       if (isset($active) === false) {
337         $active = 0;
338
hpdl
434
339         foreach ($this->_modules as $module) {
hpdl
486
340           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
341             $active++;
342           }
343         }
344       }
345
346       return $active;
347     }
348
349     function _usortModules($a, $b) {
hpdl
432
350       if ($GLOBALS['osC_Payment_' . $a]->getSortOrder() == $GLOBALS['osC_Payment_' . $b]->getSortOrder()) {
351         return strnatcasecmp($GLOBALS['osC_Payment_' . $a]->getTitle(), $GLOBALS['osC_Payment_' . $a]->getTitle());
hpdl
431
352       }
353
hpdl
432
354       return ($GLOBALS['osC_Payment_' . $a]->getSortOrder() < $GLOBALS['osC_Payment_' . $b]->getSortOrder()) ? -1 : 1;
hpdl
431
355     }
hpdl
1
356   }
357 ?>