Quick Search:

View

Revision:

Diff

Diff from 533 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 533 2006-04-26 07:32:54Z 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
1
179 /* The following method is needed in the checkout_confirmation.php page
180    due to a chicken and egg problem with the payment class and order class.
181    The payment modules needs the order destination data for the dynamic status
182    feature, and the order class needs the payment module title.
183    The following method is a work-around to implementing the method in all
184    payment modules available which would break the modules in the contributions
185    section. This should be looked into again post 2.2.
186 */
187     function update_status() {
hpdl
434
188       if (is_array($this->_modules)) {
hpdl
1
189         if (isset($GLOBALS[$this->selected_module]) && is_object($GLOBALS[$this->selected_module])) {
190           if (method_exists($GLOBALS[$this->selected_module], 'update_status')) {
191             $GLOBALS[$this->selected_module]->update_status();
192           }
193         }
194       }
195     }
196
197     function javascript_validation() {
hpdl
387
198       global $osC_Language;
199
hpdl
1
200       $js = '';
hpdl
434
201       if (is_array($this->_modules)) {
hpdl
241
202         $js = '<script type="text/javascript"><!-- ' . "\n" .
hpdl
1
203               'function check_form() {' . "\n" .
204               '  var error = 0;' . "\n" .
hpdl
387
205               '  var error_message = "' . $osC_Language->get('js_error') . '";' . "\n" .
hpdl
1
206               '  var payment_value = null;' . "\n" .
hpdl
486
207               '  if (document.checkout_payment.payment_method.length) {' . "\n" .
208               '    for (var i=0; i<document.checkout_payment.payment_method.length; i++) {' . "\n" .
209               '      if (document.checkout_payment.payment_method[i].checked) {' . "\n" .
210               '        payment_value = document.checkout_payment.payment_method[i].value;' . "\n" .
hpdl
1
211               '      }' . "\n" .
212               '    }' . "\n" .
hpdl
486
213               '  } else if (document.checkout_payment.payment_method.checked) {' . "\n" .
214               '    payment_value = document.checkout_payment.payment_method.value;' . "\n" .
215               '  } else if (document.checkout_payment.payment_method.value) {' . "\n" .
216               '    payment_value = document.checkout_payment.payment_method.value;' . "\n" .
hpdl
1
217               '  }' . "\n\n";
218
hpdl
434
219         foreach ($this->_modules as $module) {
hpdl
486
220           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
221             $js .= $GLOBALS['osC_Payment_' . $module]->javascript_validation();
hpdl
1
222           }
223         }
224
225         $js .= "\n" . '  if (payment_value == null) {' . "\n" .
hpdl
390
226                '    error_message = error_message + "' . $osC_Language->get('js_no_payment_module_selected') . '\n";' . "\n" .
hpdl
1
227                '    error = 1;' . "\n" .
228                '  }' . "\n\n" .
229                '  if (error == 1) {' . "\n" .
230                '    alert(error_message);' . "\n" .
231                '    return false;' . "\n" .
232                '  } else {' . "\n" .
233                '    return true;' . "\n" .
234                '  }' . "\n" .
235                '}' . "\n" .
236                '//--></script>' . "\n";
237       }
238
239       return $js;
240     }
241
242     function selection() {
243       $selection_array = array();
244
hpdl
434
245       foreach ($this->_modules as $module) {
hpdl
486
246         if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
247           $selection = $GLOBALS['osC_Payment_' . $module]->selection();
248           if (is_array($selection)) $selection_array[] = $selection;
hpdl
1
249         }
250       }
251
252       return $selection_array;
253     }
254
255     function pre_confirmation_check() {
hpdl
434
256       if (is_array($this->_modules)) {
hpdl
486
257         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
258           $GLOBALS[$this->selected_module]->pre_confirmation_check();
259         }
260       }
261     }
262
263     function confirmation() {
hpdl
434
264       if (is_array($this->_modules)) {
hpdl
486
265         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
266           return $GLOBALS[$this->selected_module]->confirmation();
267         }
268       }
269     }
270
271     function process_button() {
hpdl
434
272       if (is_array($this->_modules)) {
hpdl
486
273         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
274           return $GLOBALS[$this->selected_module]->process_button();
275         }
276       }
277     }
278
hpdl
523
279     function process() {
hpdl
434
280       if (is_array($this->_modules)) {
hpdl
486
281         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
523
282           return $GLOBALS[$this->selected_module]->process();
hpdl
1
283         }
284       }
285     }
286
287     function get_error() {
hpdl
434
288       if (is_array($this->_modules)) {
hpdl
486
289         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
290           return $GLOBALS[$this->selected_module]->get_error();
291         }
292       }
293     }
hpdl
431
294
hpdl
434
295     function hasActionURL() {
296       if (is_array($this->_modules)) {
hpdl
486
297         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
434
298           if (isset($GLOBALS[$this->selected_module]->form_action_url) && (empty($GLOBALS[$this->selected_module]->form_action_url) === false)) {
299             return true;
300           }
301         }
302       }
303
304       return false;
305     }
306
307     function getActionURL() {
308       return $GLOBALS[$this->selected_module]->form_action_url;
309     }
310
hpdl
431
311     function hasActive() {
312       static $has_active;
313
314       if (isset($has_active) === false) {
315         $has_active = false;
316
hpdl
434
317         foreach ($this->_modules as $module) {
hpdl
486
318           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
319             $has_active = true;
320             break;
321           }
322         }
323       }
324
325       return $has_active;
326     }
327
328     function numberOfActive() {
329       static $active;
330
331       if (isset($active) === false) {
332         $active = 0;
333
hpdl
434
334         foreach ($this->_modules as $module) {
hpdl
486
335           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
336             $active++;
337           }
338         }
339       }
340
341       return $active;
342     }
343
344     function _usortModules($a, $b) {
hpdl
432
345       if ($GLOBALS['osC_Payment_' . $a]->getSortOrder() == $GLOBALS['osC_Payment_' . $b]->getSortOrder()) {
346         return strnatcasecmp($GLOBALS['osC_Payment_' . $a]->getTitle(), $GLOBALS['osC_Payment_' . $a]->getTitle());
hpdl
431
347       }
348
hpdl
432
349       return ($GLOBALS['osC_Payment_' . $a]->getSortOrder() < $GLOBALS['osC_Payment_' . $b]->getSortOrder()) ? -1 : 1;
hpdl
431
350     }
hpdl
1
351   }
352 ?>