Quick Search:

View

Revision:

Diff

Diff from 503 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 503 2006-04-19 00:19:25Z 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
486
61     function sendTransactionToGateway($url, $parameters, $header='', $method='post') {
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
90       curl_setopt($curl, CURLOPT_HEADER, 0);
91       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
92       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
93       curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
94       curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
95       curl_setopt($curl, CURLOPT_POST, 1);
96       curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
97       $result = curl_exec($curl);
98       curl_close($curl);
99
100 /*
101       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)) : ''), $result);
102       $result = implode("\n", $result);
103 */
104 /*
105       if ($fp = @fsockopen(($server['scheme'] == 'https' ? 'ssl' : $server['scheme']) . '://' . $server['host'], $server['port'])) {
106         @fputs($fp, 'POST ' . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : '') . ' HTTP/1.1' . "\r\n" .
107                     'Host: ' . $server['host'] . "\r\n" .
108                     'Content-type: application/x-www-form-urlencoded' . "\r\n" .
109                     'Content-length: ' . strlen($parameters) . "\r\n" .
110                     (empty($header) === false ? implode("\r\n", $header) . "\r\n" : '') .
111                     'Connection: close' . "\r\n\r\n" .
112                     $parameters . "\r\n\r\n");
113
114         $result = @stream_get_contents($fp);
115
116         @fclose($fp);
117
118         $result = trim(substr($result, strpos($result, "\r\n\r\n", strpos(strtolower($result), 'content-length:'))));
119       }
120 */
121
122       return $result;
123     }
124
hpdl
432
125     function getCode() {
126       return $this->_code;
127     }
128
129     function getTitle() {
130       return $this->_title;
131     }
132
133     function getDescription() {
134       return $this->_description;
135     }
136
hpdl
486
137     function getMethodTitle() {
138       return $this->_method_title;
139     }
140
141     function isEnabled() {
hpdl
432
142       return $this->_status;
143     }
144
145     function getSortOrder() {
146       return $this->_sort_order;
147     }
148
hpdl
1
149 /* The following method is needed in the checkout_confirmation.php page
150    due to a chicken and egg problem with the payment class and order class.
151    The payment modules needs the order destination data for the dynamic status
152    feature, and the order class needs the payment module title.
153    The following method is a work-around to implementing the method in all
154    payment modules available which would break the modules in the contributions
155    section. This should be looked into again post 2.2.
156 */
157     function update_status() {
hpdl
434
158       if (is_array($this->_modules)) {
hpdl
1
159         if (isset($GLOBALS[$this->selected_module]) && is_object($GLOBALS[$this->selected_module])) {
160           if (method_exists($GLOBALS[$this->selected_module], 'update_status')) {
161             $GLOBALS[$this->selected_module]->update_status();
162           }
163         }
164       }
165     }
166
167     function javascript_validation() {
hpdl
387
168       global $osC_Language;
169
hpdl
1
170       $js = '';
hpdl
434
171       if (is_array($this->_modules)) {
hpdl
241
172         $js = '<script type="text/javascript"><!-- ' . "\n" .
hpdl
1
173               'function check_form() {' . "\n" .
174               '  var error = 0;' . "\n" .
hpdl
387
175               '  var error_message = "' . $osC_Language->get('js_error') . '";' . "\n" .
hpdl
1
176               '  var payment_value = null;' . "\n" .
hpdl
486
177               '  if (document.checkout_payment.payment_method.length) {' . "\n" .
178               '    for (var i=0; i<document.checkout_payment.payment_method.length; i++) {' . "\n" .
179               '      if (document.checkout_payment.payment_method[i].checked) {' . "\n" .
180               '        payment_value = document.checkout_payment.payment_method[i].value;' . "\n" .
hpdl
1
181               '      }' . "\n" .
182               '    }' . "\n" .
hpdl
486
183               '  } else if (document.checkout_payment.payment_method.checked) {' . "\n" .
184               '    payment_value = document.checkout_payment.payment_method.value;' . "\n" .
185               '  } else if (document.checkout_payment.payment_method.value) {' . "\n" .
186               '    payment_value = document.checkout_payment.payment_method.value;' . "\n" .
hpdl
1
187               '  }' . "\n\n";
188
hpdl
434
189         foreach ($this->_modules as $module) {
hpdl
486
190           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
191             $js .= $GLOBALS['osC_Payment_' . $module]->javascript_validation();
hpdl
1
192           }
193         }
194
195         $js .= "\n" . '  if (payment_value == null) {' . "\n" .
hpdl
390
196                '    error_message = error_message + "' . $osC_Language->get('js_no_payment_module_selected') . '\n";' . "\n" .
hpdl
1
197                '    error = 1;' . "\n" .
198                '  }' . "\n\n" .
199                '  if (error == 1) {' . "\n" .
200                '    alert(error_message);' . "\n" .
201                '    return false;' . "\n" .
202                '  } else {' . "\n" .
203                '    return true;' . "\n" .
204                '  }' . "\n" .
205                '}' . "\n" .
206                '//--></script>' . "\n";
207       }
208
209       return $js;
210     }
211
212     function selection() {
213       $selection_array = array();
214
hpdl
434
215       foreach ($this->_modules as $module) {
hpdl
486
216         if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
217           $selection = $GLOBALS['osC_Payment_' . $module]->selection();
218           if (is_array($selection)) $selection_array[] = $selection;
hpdl
1
219         }
220       }
221
222       return $selection_array;
223     }
224
225     function pre_confirmation_check() {
hpdl
434
226       if (is_array($this->_modules)) {
hpdl
486
227         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
228           $GLOBALS[$this->selected_module]->pre_confirmation_check();
229         }
230       }
231     }
232
233     function confirmation() {
hpdl
434
234       if (is_array($this->_modules)) {
hpdl
486
235         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
236           return $GLOBALS[$this->selected_module]->confirmation();
237         }
238       }
239     }
240
241     function process_button() {
hpdl
434
242       if (is_array($this->_modules)) {
hpdl
486
243         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
244           return $GLOBALS[$this->selected_module]->process_button();
245         }
246       }
247     }
248
249     function before_process() {
hpdl
434
250       if (is_array($this->_modules)) {
hpdl
486
251         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
252           return $GLOBALS[$this->selected_module]->before_process();
253         }
254       }
255     }
256
257     function after_process() {
hpdl
434
258       if (is_array($this->_modules)) {
hpdl
486
259         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
260           return $GLOBALS[$this->selected_module]->after_process();
261         }
262       }
263     }
264
265     function get_error() {
hpdl
434
266       if (is_array($this->_modules)) {
hpdl
486
267         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
1
268           return $GLOBALS[$this->selected_module]->get_error();
269         }
270       }
271     }
hpdl
431
272
hpdl
434
273     function hasActionURL() {
274       if (is_array($this->_modules)) {
hpdl
486
275         if (is_object($GLOBALS[$this->selected_module]) && $GLOBALS[$this->selected_module]->isEnabled()) {
hpdl
434
276           if (isset($GLOBALS[$this->selected_module]->form_action_url) && (empty($GLOBALS[$this->selected_module]->form_action_url) === false)) {
277             return true;
278           }
279         }
280       }
281
282       return false;
283     }
284
285     function getActionURL() {
286       return $GLOBALS[$this->selected_module]->form_action_url;
287     }
288
hpdl
431
289     function hasActive() {
290       static $has_active;
291
292       if (isset($has_active) === false) {
293         $has_active = false;
294
hpdl
434
295         foreach ($this->_modules as $module) {
hpdl
486
296           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
297             $has_active = true;
298             break;
299           }
300         }
301       }
302
303       return $has_active;
304     }
305
306     function numberOfActive() {
307       static $active;
308
309       if (isset($active) === false) {
310         $active = 0;
311
hpdl
434
312         foreach ($this->_modules as $module) {
hpdl
486
313           if ($GLOBALS['osC_Payment_' . $module]->isEnabled()) {
hpdl
431
314             $active++;
315           }
316         }
317       }
318
319       return $active;
320     }
321
322     function _usortModules($a, $b) {
hpdl
432
323       if ($GLOBALS['osC_Payment_' . $a]->getSortOrder() == $GLOBALS['osC_Payment_' . $b]->getSortOrder()) {
324         return strnatcasecmp($GLOBALS['osC_Payment_' . $a]->getTitle(), $GLOBALS['osC_Payment_' . $a]->getTitle());
hpdl
431
325       }
326
hpdl
432
327       return ($GLOBALS['osC_Payment_' . $a]->getSortOrder() < $GLOBALS['osC_Payment_' . $b]->getSortOrder()) ? -1 : 1;
hpdl
431
328     }
hpdl
1
329   }
330 ?>