PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/classes/payment.php

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