PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_virtuemart/plugins/vmpsplugin.php

https://github.com/srgg6701/auction-ruseasons
PHP | 1235 lines | 681 code | 193 blank | 361 comment | 98 complexity | 07ac90850381124d94f0693ff298798c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, JSON
  1. <?php
  2. defined ('_JEXEC') or die('Restricted access');
  3. /**
  4. * abstract class for payment/shipment plugins
  5. *
  6. * @package VirtueMart
  7. * @subpackage Plugins
  8. * @author Max Milbers
  9. * @author Valérie Isaksen
  10. * @link http://www.virtuemart.net
  11. * @copyright Copyright (c) 2004 - 2011 VirtueMart Team. All rights reserved.
  12. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  13. * VirtueMart is free software. This version may have been modified pursuant
  14. * to the GNU General Public License, and as distributed it includes or
  15. * is derivative of works licensed under the GNU General Public License or
  16. * other free or open source software licenses.
  17. * @version $Id$
  18. */
  19. if (!class_exists ('vmPlugin')) {
  20. require(JPATH_VM_PLUGINS . DS . 'vmplugin.php');
  21. }
  22. abstract class vmPSPlugin extends vmPlugin {
  23. function __construct (& $subject, $config) {
  24. parent::__construct ($subject, $config);
  25. $this->_tablepkey = 'id'; //virtuemart_order_id';
  26. $this->_idName = 'virtuemart_' . $this->_psType . 'method_id';
  27. $this->_configTable = '#__virtuemart_' . $this->_psType . 'methods';
  28. $this->_configTableFieldName = $this->_psType . '_params';
  29. $this->_configTableFileName = $this->_psType . 'methods';
  30. $this->_configTableClassName = 'Table' . ucfirst ($this->_psType) . 'methods'; //TablePaymentmethods
  31. // $this->_configTableIdName = $this->_psType.'_jplugin_id';
  32. $this->_loggable = TRUE;
  33. $this->_tableChecked = TRUE;
  34. }
  35. public function getVarsToPush () {
  36. $black_list = array('spacer');
  37. $data = array();
  38. if (JVM_VERSION === 2) {
  39. $filename = JPATH_SITE . '/plugins/' . $this->_type . '/' . $this->_name . '/' . $this->_name . '.xml';
  40. } else {
  41. $filename = JPATH_SITE . '/plugins/' . $this->_type . '/' . $this->_name . '.xml';
  42. }
  43. // Check of the xml file exists
  44. $filePath = JPath::clean ($filename);
  45. if (is_file ($filePath)) {
  46. $xml = JFactory::getXMLParser ('simple');
  47. $result = $xml->loadFile ($filename);
  48. if ($result) {
  49. if ($params = $xml->document->params) {
  50. foreach ($params as $param) {
  51. if ($param->_name = "params") {
  52. if ($children = $param->_children) {
  53. foreach ($children as $child) {
  54. if (isset($child->_attributes['name'])) {
  55. $data[$child->_attributes['name']] = array('', 'char');
  56. $result = TRUE;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. return $data;
  66. }
  67. /**
  68. * check if it is the correct type
  69. *
  70. * @param string $psType either payment or shipment
  71. * @return boolean
  72. */
  73. public function selectedThisType ($psType) {
  74. if ($this->_psType <> $psType) {
  75. return FALSE;
  76. } else {
  77. return TRUE;
  78. }
  79. }
  80. /**
  81. * Create the table for this plugin if it does not yet exist.
  82. * This functions checks if the called plugin is active one.
  83. * When yes it is calling the standard method to create the tables
  84. *
  85. * @author Valérie Isaksen
  86. *
  87. */
  88. protected function onStoreInstallPluginTable ($jplugin_id, $name = FALSE) {
  89. if ($res = $this->selectedThisByJPluginId ($jplugin_id)) {
  90. parent::onStoreInstallPluginTable ($this->_psType);
  91. }
  92. return $res;
  93. }
  94. /**
  95. * This event is fired after the payment method has been selected. It can be used to store
  96. * additional payment info in the cart.
  97. *
  98. * @author Max Milbers
  99. * @author Valérie isaksen
  100. *
  101. * @param VirtueMartCart $cart: the actual cart
  102. * @return null if the payment was not selected, true if the data is valid, error message if the data is not vlaid
  103. *
  104. */
  105. public function onSelectCheck (VirtueMartCart $cart) {
  106. $idName = $this->_idName; //vmdebug('OnSelectCheck',$idName);
  107. if (!$this->selectedThisByMethodId ($cart->$idName)) {
  108. return NULL; // Another method was selected, do nothing
  109. }
  110. return TRUE; // this method was selected , and the data is valid by default
  111. }
  112. /**
  113. * displayListFE
  114. * This event is fired to display the pluginmethods in the cart (edit shipment/payment) for example
  115. *
  116. * @param object $cart Cart object
  117. * @param integer $selected ID of the method selected
  118. * @return boolean True on success, false on failures, null when this plugin was not selected.
  119. * On errors, JError::raiseWarning (or JError::raiseError) must be used to set a message.
  120. *
  121. * @author Valerie Isaksen
  122. * @author Max Milbers
  123. */
  124. public function displayListFE (VirtueMartCart $cart, $selected = 0, &$htmlIn) {
  125. if ($this->getPluginMethods ($cart->vendorId) === 0) {
  126. if (empty($this->_name)) {
  127. vmAdminInfo ('displayListFE cartVendorId=' . $cart->vendorId);
  128. $app = JFactory::getApplication ();
  129. $app->enqueueMessage (JText::_ ('COM_VIRTUEMART_CART_NO_' . strtoupper ($this->_psType)));
  130. return FALSE;
  131. } else {
  132. return FALSE;
  133. }
  134. }
  135. $html = array();
  136. $method_name = $this->_psType . '_name';
  137. foreach ($this->methods as $method) {
  138. if ($this->checkConditions ($cart, $method, $cart->pricesUnformatted)) {
  139. $methodSalesPrice = $this->calculateSalesPrice ($cart, $method, $cart->pricesUnformatted);
  140. $method->$method_name = $this->renderPluginName ($method);
  141. $html [] = $this->getPluginHtml ($method, $selected, $methodSalesPrice);
  142. }
  143. }
  144. if (!empty($html)) {
  145. $htmlIn[] = $html;
  146. return TRUE;
  147. }
  148. return FALSE;
  149. }
  150. /*
  151. * onSelectedCalculatePrice
  152. * Calculate the price (value, tax_id) of the selected method
  153. * It is called by the calculator
  154. * This function does NOT to be reimplemented. If not reimplemented, then the default values from this function are taken.
  155. * @author Valerie Isaksen
  156. * @cart: VirtueMartCart the current cart
  157. * @cart_prices: array the new cart prices
  158. * @return null if the method was not selected, false if the shipping rate is not valid any more, true otherwise
  159. *
  160. */
  161. public function onSelectedCalculatePrice (VirtueMartCart $cart, array &$cart_prices, &$cart_prices_name) {
  162. $id = $this->_idName;
  163. if (!($method = $this->selectedThisByMethodId ($cart->$id))) {
  164. return NULL; // Another method was selected, do nothing
  165. }
  166. if (!($method = $this->getVmPluginMethod ($cart->$id))) {
  167. return NULL;
  168. }
  169. $cart_prices_name = '';
  170. //$cart_prices[$this->_psType . '_tax_id'] = 0;
  171. $cart_prices['cost'] = 0;
  172. if (!$this->checkConditions ($cart, $method, $cart_prices)) {
  173. return FALSE;
  174. }
  175. $paramsName = $this->_psType . '_params';
  176. $cart_prices_name = $this->renderPluginName ($method);
  177. $this->setCartPrices ($cart, $cart_prices, $method);
  178. return TRUE;
  179. }
  180. /**
  181. * onCheckAutomaticSelected
  182. * Checks how many plugins are available. If only one, the user will not have the choice. Enter edit_xxx page
  183. * The plugin must check first if it is the correct type
  184. *
  185. * @author Valerie Isaksen
  186. * @param VirtueMartCart cart: the cart object
  187. * @return null if no plugin was found, 0 if more then one plugin was found, virtuemart_xxx_id if only one plugin is found
  188. *
  189. */
  190. function onCheckAutomaticSelected (VirtueMartCart $cart, array $cart_prices = array(), &$methodCounter = 0) {
  191. $nbPlugin = 0;
  192. $virtuemart_pluginmethod_id = 0;
  193. $nbMethod = $this->getSelectable ($cart, $virtuemart_pluginmethod_id, $cart_prices);
  194. $methodCounter += $nbMethod;
  195. if ($nbMethod == NULL) {
  196. return NULL;
  197. } else {
  198. if ($nbMethod == 1) {
  199. return $virtuemart_pluginmethod_id;
  200. } else {
  201. return 0;
  202. }
  203. }
  204. }
  205. /**
  206. * This method is fired when showing the order details in the frontend.
  207. * It displays the method-specific data.
  208. *
  209. * @param integer $order_id The order ID
  210. * @return mixed Null for methods that aren't active, text (HTML) otherwise
  211. * @author Max Milbers
  212. * @author Valerie Isaksen
  213. */
  214. protected function onShowOrderFE ($virtuemart_order_id, $virtuemart_method_id, &$method_info) {
  215. if (!($this->selectedThisByMethodId ($virtuemart_method_id))) {
  216. return NULL;
  217. }
  218. $method_info = $this->getOrderMethodNamebyOrderId ($virtuemart_order_id);
  219. }
  220. /**
  221. *
  222. * @author Valerie Isaksen
  223. * @author Max Milbers
  224. * @param int $virtuemart_order_id
  225. * @return string pluginName from the plugin table
  226. */
  227. private function getOrderMethodNamebyOrderId ($virtuemart_order_id) {
  228. $db = JFactory::getDBO ();
  229. $q = 'SELECT * FROM `' . $this->_tablename . '` '
  230. . 'WHERE `virtuemart_order_id` = ' . $virtuemart_order_id;
  231. $db->setQuery ($q);
  232. if (!($pluginInfo = $db->loadObject ())) {
  233. vmWarn ('Attention, ' . $this->_tablename . ' has not any entry for the order ' . $db->getErrorMsg ());
  234. return NULL;
  235. }
  236. $idName = $this->_psType . '_name';
  237. return $pluginInfo->$idName;
  238. }
  239. /**
  240. *
  241. * @author Valerie Isaksen
  242. * @author Max Milbers
  243. * @param int $virtuemart_order_id
  244. * @return string pluginName from the plugin table
  245. */
  246. private function getOrderPluginNamebyOrderId ($virtuemart_order_id) {
  247. $db = JFactory::getDBO ();
  248. $q = 'SELECT * FROM `' . $this->_tablename . '` '
  249. . 'WHERE `virtuemart_order_id` = ' . $virtuemart_order_id;
  250. $db->setQuery ($q);
  251. if (!($pluginInfo = $db->loadObject ())) {
  252. vmWarn (500, $q . " getOrderPluginNamebyOrderId " . $db->getErrorMsg ());
  253. return NULL;
  254. }
  255. $idName = $this->_idName;
  256. return $pluginInfo->$idName;
  257. }
  258. /**
  259. * check if it is the correct element
  260. *
  261. * @param string $element either standard or paypal
  262. * @return boolean
  263. */
  264. public function selectedThisElement ($element) {
  265. if ($this->_name <> $element) {
  266. return FALSE;
  267. } else {
  268. return TRUE;
  269. }
  270. }
  271. /**
  272. * This method is fired when showing the order details in the backend.
  273. * It displays the the payment method-specific data.
  274. * All plugins *must* reimplement this method.
  275. *
  276. * @param integer $_virtuemart_order_id The order ID
  277. * @param integer $_paymethod_id Payment method used for this order
  278. * @return mixed Null when for payment methods that were not selected, text (HTML) otherwise
  279. * @author Max Milbers
  280. * @author Valerie Isaksen
  281. */
  282. function onShowOrderBE ($_virtuemart_order_id, $_method_id) {
  283. return NULL;
  284. }
  285. /**
  286. * This method is fired when showing when priting an Order
  287. * It displays the the payment method-specific data.
  288. *
  289. * @param integer $_virtuemart_order_id The order ID
  290. * @param integer $method_id method used for this order
  291. * @return mixed Null when for payment methods that were not selected, text (HTML) otherwise
  292. * @author Valerie Isaksen
  293. */
  294. function onShowOrderPrint ($order_number, $method_id) {
  295. if (!$this->selectedThisByMethodId ($method_id)) {
  296. return NULL; // Another method was selected, do nothing
  297. }
  298. if (!($order_name = $this->getOrderPluginName ($order_number, $method_id))) {
  299. return NULL;
  300. }
  301. JFactory::getLanguage ()->load ('com_virtuemart');
  302. $html = '<table class="admintable">' . "\n"
  303. . ' <thead>' . "\n"
  304. . ' <tr>' . "\n"
  305. . ' <td class="key" style="text-align: center;" colspan="2">' . JText::_ ('COM_VIRTUEMART_ORDER_PRINT_' . $this->_type . '_LBL') . '</td>' . "\n"
  306. . ' </tr>' . "\n"
  307. . ' </thead>' . "\n"
  308. . ' <tr>' . "\n"
  309. . ' <td class="key">' . JText::_ ('COM_VIRTUEMART_ORDER_PRINT_' . $this->_type . '_LBL') . ': </td>' . "\n"
  310. . ' <td align="left">' . $order_name . '</td>' . "\n"
  311. . ' </tr>' . "\n";
  312. $html .= '</table>' . "\n";
  313. return $html;
  314. }
  315. private function getOrderPluginName ($order_number, $pluginmethod_id) {
  316. $db = JFactory::getDBO ();
  317. $q = 'SELECT * FROM `' . $this->_tablename . '` WHERE `order_number` = "' . $order_number . '"
  318. AND `' . $this->_idName . '` =' . $pluginmethod_id;
  319. $db->setQuery ($q);
  320. if (!($order = $db->loadObject ())) {
  321. return NULL;
  322. }
  323. $plugin_name = $this->_psType . '_name';
  324. return $order->$plugin_name;
  325. }
  326. /**
  327. * Save updated order data to the method specific table
  328. *
  329. * @param array $_formData Form data
  330. * @return mixed, True on success, false on failures (the rest of the save-process will be
  331. * skipped!), or null when this method is not actived.
  332. * @author Oscar van Eijk
  333. */
  334. public function onUpdateOrder ($formData) {
  335. return NULL;
  336. }
  337. /**
  338. * Save updated orderline data to the method specific table
  339. *
  340. * @param array $_formData Form data
  341. * @return mixed, True on success, false on failures (the rest of the save-process will be
  342. * skipped!), or null when this method is not actived.
  343. * @author Oscar van Eijk
  344. */
  345. public function onUpdateOrderLine ($formData) {
  346. return NULL;
  347. }
  348. /**
  349. * OnEditOrderLineBE
  350. * This method is fired when editing the order line details in the backend.
  351. * It can be used to add line specific package codes
  352. *
  353. * @param integer $_orderId The order ID
  354. * @param integer $_lineId
  355. * @return mixed Null for method that aren't active, text (HTML) otherwise
  356. * @author Oscar van Eijk
  357. */
  358. public function onEditOrderLineBE ($orderId, $lineId) {
  359. return NULL;
  360. }
  361. /**
  362. * This method is fired when showing the order details in the frontend, for every orderline.
  363. * It can be used to display line specific package codes, e.g. with a link to external tracking and
  364. * tracing systems
  365. *
  366. * @param integer $_orderId The order ID
  367. * @param integer $_lineId
  368. * @return mixed Null for method that aren't active, text (HTML) otherwise
  369. * @author Oscar van Eijk
  370. */
  371. public function onShowOrderLineFE ($orderId, $lineId) {
  372. return NULL;
  373. }
  374. /**
  375. * This event is fired when the method notifies you when an event occurs that affects the order.
  376. * Typically, the events represents for payment authorizations, Fraud Management Filter actions and other actions,
  377. * such as refunds, disputes, and chargebacks.
  378. *
  379. * NOTE for Plugin developers:
  380. * If the plugin is NOT actually executed (not the selected payment method), this method must return NULL
  381. *
  382. * @param $return_context: it was given and sent in the payment form. The notification should return it back.
  383. * Used to know which cart should be emptied, in case it is still in the session.
  384. * @param int $virtuemart_order_id : payment order id
  385. * @param char $new_status : new_status for this order id.
  386. * @return mixed Null when this method was not selected, otherwise the true or false
  387. *
  388. * @author Valerie Isaksen
  389. *
  390. */
  391. public function onNotification () {
  392. return NULL;
  393. }
  394. /**
  395. * OnResponseReceived
  396. * This event is fired when the method returns to the shop after the transaction
  397. *
  398. * the method itself should send in the URL the parameters needed
  399. * NOTE for Plugin developers:
  400. * If the plugin is NOT actually executed (not the selected payment method), this method must return NULL
  401. *
  402. * @param int $virtuemart_order_id : should return the virtuemart_order_id
  403. * @param text $html: the html to display
  404. * @return mixed Null when this method was not selected, otherwise the true or false
  405. *
  406. * @author Valerie Isaksen
  407. *
  408. */
  409. function onResponseReceived (&$virtuemart_order_id, &$html) {
  410. return NULL;
  411. }
  412. function getDebug () {
  413. return $this->_debug;
  414. }
  415. function setDebug ($params) {
  416. return $this->_debug = $params->get ('debug', 0);
  417. }
  418. /**
  419. * Get Plugin Data for a go given plugin ID
  420. *
  421. * @author Valérie Isaksen
  422. * @param int $pluginmethod_id The method ID
  423. * @return method data
  424. */
  425. final protected function getPluginMethod ($method_id) {
  426. if (!$this->selectedThisByMethodId ($method_id)) {
  427. return FALSE;
  428. }
  429. return $this->getVmPluginMethod ($method_id);
  430. }
  431. /**
  432. * Fill the array with all plugins found with this plugin for the current vendor
  433. *
  434. * @return True when plugins(s) was (were) found for this vendor, false otherwise
  435. * @author Oscar van Eijk
  436. * @author max Milbers
  437. * @author valerie Isaksen
  438. */
  439. protected function getPluginMethods ($vendorId) {
  440. if (!class_exists ('VirtueMartModelUser')) {
  441. require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'user.php');
  442. }
  443. $usermodel = VmModel::getModel ('user');
  444. $user = $usermodel->getUser ();
  445. $user->shopper_groups = (array)$user->shopper_groups;
  446. $db = JFactory::getDBO ();
  447. $select = 'SELECT l.*, v.*, ';
  448. if (JVM_VERSION === 1) {
  449. $extPlgTable = '#__plugins';
  450. $extField1 = 'id';
  451. $extField2 = 'element';
  452. $select .= 'j.`' . $extField1 . '`, j.`name`, j.`element`, j.`folder`, j.`client_id`, j.`access`,
  453. j.`params`, j.`checked_out`, j.`checked_out_time`, s.virtuemart_shoppergroup_id ';
  454. } else {
  455. $extPlgTable = '#__extensions';
  456. $extField1 = 'extension_id';
  457. $extField2 = 'element';
  458. $select .= 'j.`' . $extField1 . '`,j.`name`, j.`type`, j.`element`, j.`folder`, j.`client_id`, j.`enabled`, j.`access`, j.`protected`, j.`manifest_cache`,
  459. j.`params`, j.`custom_data`, j.`system_data`, j.`checked_out`, j.`checked_out_time`, j.`state`, s.virtuemart_shoppergroup_id ';
  460. }
  461. $q = $select . ' FROM `#__virtuemart_' . $this->_psType . 'methods_' . VMLANG . '` as l ';
  462. $q .= ' JOIN `#__virtuemart_' . $this->_psType . 'methods` AS v USING (`virtuemart_' . $this->_psType . 'method_id`) ';
  463. $q .= ' LEFT JOIN `' . $extPlgTable . '` as j ON j.`' . $extField1 . '` = v.`' . $this->_psType . '_jplugin_id` ';
  464. $q .= ' LEFT OUTER JOIN `#__virtuemart_' . $this->_psType . 'method_shoppergroups` AS s ON v.`virtuemart_' . $this->_psType . 'method_id` = s.`virtuemart_' . $this->_psType . 'method_id` ';
  465. $q .= ' WHERE v.`published` = "1" AND j.`' . $extField2 . '` = "' . $this->_name . '"
  466. AND (v.`virtuemart_vendor_id` = "' . $vendorId . '" OR v.`virtuemart_vendor_id` = "0")
  467. AND (';
  468. foreach ($user->shopper_groups as $groups) {
  469. $q .= ' s.`virtuemart_shoppergroup_id`= "' . (int)$groups . '" OR';
  470. }
  471. $q .= ' (s.`virtuemart_shoppergroup_id`) IS NULL ) ORDER BY v.`ordering`';
  472. $db->setQuery ($q);
  473. $this->methods = $db->loadObjectList ();
  474. $err = $db->getErrorMsg ();
  475. if (!empty($err)) {
  476. vmError ('Error reading getPluginMethods ' . $err);
  477. }
  478. if ($this->methods) {
  479. foreach ($this->methods as $method) {
  480. VmTable::bindParameterable ($method, $this->_xParams, $this->_varsToPushParam);
  481. }
  482. }
  483. // vmdebug('getPluginMethods',$this->methods);
  484. return count ($this->methods);
  485. }
  486. /**
  487. * Get Method Data for a given Payment ID
  488. *
  489. * @author Valérie Isaksen
  490. * @param int $virtuemart_order_id The order ID
  491. * @return $methodData
  492. */
  493. final protected function getDataByOrderId ($virtuemart_order_id) {
  494. $db = JFactory::getDBO ();
  495. $q = 'SELECT * FROM `' . $this->_tablename . '` '
  496. . 'WHERE `virtuemart_order_id` = ' . $virtuemart_order_id;
  497. $db->setQuery ($q);
  498. $methodData = $db->loadObject ();
  499. return $methodData;
  500. }
  501. /**
  502. * Get Method Datas for a given Payment ID
  503. *
  504. * @author Valérie Isaksen
  505. * @param int $virtuemart_order_id The order ID
  506. * @return $methodData
  507. */
  508. final protected function getDatasByOrderId ($virtuemart_order_id) {
  509. $db = JFactory::getDBO ();
  510. $q = 'SELECT * FROM `' . $this->_tablename . '` '
  511. . 'WHERE `virtuemart_order_id` = ' . $virtuemart_order_id;
  512. $db->setQuery ($q);
  513. $methodData = $db->loadObjectList ();
  514. return $methodData;
  515. }
  516. /**
  517. * Get the total weight for the order, based on which the proper shipping rate
  518. * can be selected.
  519. *
  520. * @param object $cart Cart object
  521. * @return float Total weight for the order
  522. * @author Oscar van Eijk
  523. */
  524. protected function getOrderWeight (VirtueMartCart $cart, $to_weight_unit) {
  525. static $weight = 0.0;
  526. if(count($cart->products)>0 and empty($weight)){
  527. foreach ($cart->products as $product) {
  528. $weight += (ShopFunctions::convertWeigthUnit ($product->product_weight, $product->product_weight_uom, $to_weight_unit) * $product->quantity);
  529. }
  530. }
  531. return $weight;
  532. }
  533. /**
  534. * getThisName
  535. * Get the name of the method
  536. *
  537. * @param int $id The method ID
  538. * @author Valérie Isaksen
  539. * @return string Shipment name
  540. */
  541. final protected function getThisName ($virtuemart_method_id) {
  542. $db = JFactory::getDBO ();
  543. $q = 'SELECT `' . $this->_psType . '_name` '
  544. . 'FROM #__virtuemart_' . $this->_psType . 'methods '
  545. . 'WHERE ' . $this->_idName . ' = "' . $virtuemart_method_id . '" ';
  546. $db->setQuery ($q);
  547. return $db->loadResult (); // TODO Error check
  548. }
  549. /**
  550. * Extends the standard function in vmplugin. Extendst the input data by virtuemart_order_id
  551. * Calls the parent to execute the write operation
  552. *
  553. * @author Max Milbers
  554. * @param array $_values
  555. * @param string $_table
  556. */
  557. protected function storePSPluginInternalData ($values, $primaryKey = 0, $preload = FALSE) {
  558. if (!class_exists ('VirtueMartModelOrders')) {
  559. require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'orders.php');
  560. }
  561. if (!isset($values['virtuemart_order_id'])) {
  562. $values['virtuemart_order_id'] = VirtueMartModelOrders::getOrderIdByOrderNumber ($values['order_number']);
  563. }
  564. $this->storePluginInternalData ($values, $primaryKey, 0, $preload);
  565. }
  566. /**
  567. * Something went wrong, Send notification to all administrators
  568. *
  569. * @param string subject of the mail
  570. * @param string message
  571. */
  572. protected function sendEmailToVendorAndAdmins ($subject, $message) {
  573. // recipient is vendor and admin
  574. $vendorId = 1;
  575. $vendorModel = VmModel::getModel ('vendor');
  576. $vendorEmail = $vendorModel->getVendorEmail ($vendorId);
  577. $vendorName = $vendorModel->getVendorName ($vendorId);
  578. JUtility::sendMail ($vendorEmail, $vendorName, $vendorEmail, $subject, $message);
  579. if (JVM_VERSION === 1) {
  580. //get all super administrator
  581. $query = 'SELECT name, email, sendEmail' .
  582. ' FROM #__users' .
  583. ' WHERE LOWER( usertype ) = "super administrator"';
  584. } else {
  585. $query = 'SELECT name, email, sendEmail' .
  586. ' FROM #__users' .
  587. ' WHERE sendEmail=1';
  588. }
  589. $db = JFactory::getDBO ();
  590. $db->setQuery ($query);
  591. $rows = $db->loadObjectList ();
  592. $subject = html_entity_decode ($subject, ENT_QUOTES);
  593. // get superadministrators id
  594. foreach ($rows as $row) {
  595. if ($row->sendEmail) {
  596. $message = html_entity_decode ($message, ENT_QUOTES);
  597. JUtility::sendMail ($vendorEmail, $vendorName, $row->email, $subject, $message);
  598. }
  599. }
  600. }
  601. /**
  602. * displays the logos of a VirtueMart plugin
  603. *
  604. * @author Valerie Isaksen
  605. * @author Max Milbers
  606. * @param array $logo_list
  607. * @return html with logos
  608. */
  609. protected function displayLogos ($logo_list) {
  610. $img = "";
  611. if (!(empty($logo_list))) {
  612. $url = JURI::root () . 'images/stories/virtuemart/' . $this->_psType . '/';
  613. if (!is_array ($logo_list)) {
  614. $logo_list = (array)$logo_list;
  615. }
  616. foreach ($logo_list as $logo) {
  617. $alt_text = substr ($logo, 0, strpos ($logo, '.'));
  618. $img .= '<span class="vmCartPaymentLogo" ><img align="middle" src="' . $url . $logo . '" alt="' . $alt_text . '" /></span> ';
  619. }
  620. }
  621. return $img;
  622. }
  623. /**
  624. * @param $plugin plugin
  625. */
  626. protected function renderPluginName ($plugin) {
  627. $return = '';
  628. $plugin_name = $this->_psType . '_name';
  629. $plugin_desc = $this->_psType . '_desc';
  630. $description = '';
  631. // $params = new JParameter($plugin->$plugin_params);
  632. // $logo = $params->get($this->_psType . '_logos');
  633. $logosFieldName = $this->_psType . '_logos';
  634. $logos = $plugin->$logosFieldName;
  635. if (!empty($logos)) {
  636. $return = $this->displayLogos ($logos) . ' ';
  637. }
  638. if (!empty($plugin->$plugin_desc)) {
  639. $description = '<span class="' . $this->_type . '_description">' . $plugin->$plugin_desc . '</span>';
  640. }
  641. $pluginName = $return . '<span class="' . $this->_type . '_name">' . $plugin->$plugin_name . '</span>' . $description;
  642. return $pluginName;
  643. }
  644. protected function getPluginHtml ($plugin, $selectedPlugin, $pluginSalesPrice) {
  645. $pluginmethod_id = $this->_idName;
  646. $pluginName = $this->_psType . '_name';
  647. if ($selectedPlugin == $plugin->$pluginmethod_id) {
  648. $checked = 'checked="checked"';
  649. } else {
  650. $checked = '';
  651. }
  652. if (!class_exists ('CurrencyDisplay')) {
  653. require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'currencydisplay.php');
  654. }
  655. $currency = CurrencyDisplay::getInstance ();
  656. $costDisplay = "";
  657. if ($pluginSalesPrice) {
  658. $costDisplay = $currency->priceDisplay ($pluginSalesPrice);
  659. $costDisplay = '<span class="' . $this->_type . '_cost"> (' . JText::_ ('COM_VIRTUEMART_PLUGIN_COST_DISPLAY') . $costDisplay . ")</span>";
  660. }
  661. $html = '<input type="radio" name="' . $pluginmethod_id . '" id="' . $this->_psType . '_id_' . $plugin->$pluginmethod_id . '" value="' . $plugin->$pluginmethod_id . '" ' . $checked . ">\n"
  662. . '<label for="' . $this->_psType . '_id_' . $plugin->$pluginmethod_id . '">' . '<span class="' . $this->_type . '">' . $plugin->$pluginName . $costDisplay . "</span></label>\n";
  663. return $html;
  664. }
  665. /**
  666. *
  667. */
  668. protected function getHtmlHeaderBE () {
  669. $class = "class='key'";
  670. $html = ' <thead>' . "\n"
  671. . ' <tr>' . "\n"
  672. . ' <th ' . $class . ' style="text-align: center;" colspan="2">' . JText::_ ('COM_VIRTUEMART_ORDER_PRINT_' . $this->_psType . '_LBL') . '</th>' . "\n"
  673. . ' </tr>' . "\n"
  674. . ' </thead>' . "\n";
  675. return $html;
  676. }
  677. /**
  678. *
  679. */
  680. protected function getHtmlRow ($key, $value, $class = '') {
  681. $lang = JFactory::getLanguage ();
  682. $key_text = '';
  683. $complete_key = strtoupper ($this->_type . '_' . $key);
  684. // vmdebug('getHtmlRow',$key,$complete_key);
  685. //if ($lang->hasKey($complete_key)) {
  686. $key_text = JText::_ ($complete_key);
  687. //}
  688. $more_key = $complete_key . '_' . $value;
  689. if ($lang->hasKey ($more_key)) {
  690. $value .= " (" . JText::_ ($more_key) . ")";
  691. }
  692. $html = "<tr>\n<td " . $class . ">" . $key_text . "</td>\n <td align='left'>" . $value . "</td>\n</tr>\n";
  693. return $html;
  694. }
  695. protected function getHtmlRowBE ($key, $value) {
  696. return $this->getHtmlRow ($key, $value, "class='key'");
  697. }
  698. /**
  699. * getSelectable
  700. * This method returns the number of valid methods
  701. *
  702. * @param VirtueMartCart cart: the cart object
  703. * @param $method_id eg $virtuemart_shipmentmethod_id
  704. *
  705. */
  706. function getSelectable (VirtueMartCart $cart, &$method_id, $cart_prices) {
  707. $nbMethod = 0;
  708. if ($this->getPluginMethods ($cart->vendorId) === 0) {
  709. return FALSE;
  710. }
  711. foreach ($this->methods as $method) {
  712. if ($nb = (int)$this->checkConditions ($cart, $method, $cart_prices)) {
  713. $nbMethod = $nbMethod + $nb;
  714. $idName = $this->_idName;
  715. $method_id = $method->$idName;
  716. }
  717. }
  718. return $nbMethod;
  719. }
  720. /**
  721. *
  722. * Enter description here ...
  723. *
  724. * @author Valerie Isaksen
  725. * @author Max Milbers
  726. * @param VirtueMartCart $cart
  727. * @param int $method
  728. * @param array $cart_prices
  729. */
  730. protected function checkConditions ($cart, $method, $cart_prices) {
  731. vmAdminInfo ('vmPsPlugin function checkConditions not overriden, gives always back FALSE');
  732. return FALSE;
  733. }
  734. function getCosts (VirtueMartCart $cart, $method, $cart_prices) {
  735. return 0;
  736. }
  737. function getPaymentCurrency (&$method, $getCurrency = FALSE) {
  738. if (!isset($method->payment_currency) or empty($method->payment_currency) or !$method->payment_currency or $getCurrency) {
  739. // if (!class_exists('VirtueMartModelVendor')) require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'vendor.php');
  740. $vendorId = 1; //VirtueMartModelVendor::getLoggedVendor();
  741. $db = JFactory::getDBO ();
  742. $q = 'SELECT `vendor_currency` FROM `#__virtuemart_vendors` WHERE `virtuemart_vendor_id`=' . $vendorId;
  743. $db->setQuery ($q);
  744. $method->payment_currency = $db->loadResult ();
  745. }
  746. }
  747. function getEmailCurrency (&$method) {
  748. if (!isset($method->email_currency) or $method->email_currency=='vendor') {
  749. // if (!class_exists('VirtueMartModelVendor')) require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'vendor.php');
  750. $vendorId = 1; //VirtueMartModelVendor::getLoggedVendor();
  751. $db = JFactory::getDBO ();
  752. $q = 'SELECT `vendor_currency` FROM `#__virtuemart_vendors` WHERE `virtuemart_vendor_id`=' . $vendorId;
  753. $db->setQuery ($q);
  754. return $db->loadResult ();
  755. } else {
  756. return $method->payment_currency; // either the vendor currency, either same currency as payment
  757. }
  758. }
  759. /**
  760. * displayTaxRule
  761. *
  762. * @param int $tax_id
  763. * @return string $html:
  764. */
  765. function displayTaxRule ($tax_id) {
  766. $html = '';
  767. $db = JFactory::getDBO ();
  768. if (!empty($tax_id)) {
  769. $q = 'SELECT * FROM #__virtuemart_calcs WHERE `virtuemart_calc_id`="' . $tax_id . '" ';
  770. $db->setQuery ($q);
  771. $taxrule = $db->loadObject ();
  772. $html = $taxrule->calc_name . '(' . $taxrule->calc_kind . ':' . $taxrule->calc_value_mathop . $taxrule->calc_value . ')';
  773. }
  774. return $html;
  775. }
  776. /**
  777. * update the plugin cart_prices
  778. *
  779. * @author Valérie Isaksen
  780. *
  781. * @param $cart_prices: $cart_prices['salesPricePayment'] and $cart_prices['paymentTax'] updated. Displayed in the cart.
  782. * @param $value : fee
  783. * @param $tax_id : tax id
  784. */
  785. function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method) {
  786. if (!class_exists ('calculationHelper')) {
  787. require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
  788. }
  789. $calculator = calculationHelper::getInstance ();
  790. $value = $calculator->roundInternal ($this->getCosts ($cart, $method, $cart_prices), 'salesPrice');
  791. $_psType = ucfirst ($this->_psType);
  792. $cart_prices[$this->_psType . 'Value'] = $value;
  793. $taxrules = array();
  794. if (!empty($method->tax_id)) {
  795. $cart_prices[$this->_psType . '_calc_id'] = $method->tax_id;
  796. $db = JFactory::getDBO ();
  797. $q = 'SELECT * FROM #__virtuemart_calcs WHERE `virtuemart_calc_id`="' . $method->tax_id . '" ';
  798. $db->setQuery ($q);
  799. $taxrules = $db->loadAssocList ();
  800. }
  801. if (count ($taxrules) > 0) {
  802. $cart_prices['salesPrice' . $_psType] = $calculator->roundInternal ($calculator->executeCalculation ($taxrules, $cart_prices[$this->_psType . 'Value']), 'salesPrice');
  803. $cart_prices[$this->_psType . 'Tax'] = $calculator->roundInternal (($cart_prices['salesPrice' . $_psType] - $cart_prices[$this->_psType . 'Value']), 'salesPrice');
  804. $cart_prices[$this->_psType . '_calc_id'] = $taxrules[0]['virtuemart_calc_id'];
  805. } else {
  806. $cart_prices['salesPrice' . $_psType] = $value;
  807. $cart_prices[$this->_psType . 'Tax'] = 0;
  808. $cart_prices[$this->_psType . '_calc_id'] = 0;
  809. }
  810. }
  811. /**
  812. * calculateSalesPrice
  813. *
  814. * @param $value
  815. * @param $tax_id: tax id
  816. * @return $salesPrice
  817. */
  818. protected function calculateSalesPrice ($cart, $method, $cart_prices) {
  819. $value = $this->getCosts ($cart, $method, $cart_prices);
  820. $tax_id = @$method->tax_id;
  821. if (!class_exists ('calculationHelper')) {
  822. require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
  823. }
  824. if (!class_exists ('CurrencyDisplay')) {
  825. require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'currencydisplay.php');
  826. }
  827. if (!class_exists ('VirtueMartModelVendor')) {
  828. require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'vendor.php');
  829. }
  830. $vendor_id = 1;
  831. $vendor_currency = VirtueMartModelVendor::getVendorCurrency ($vendor_id);
  832. $db = JFactory::getDBO ();
  833. $calculator = calculationHelper::getInstance ();
  834. $currency = CurrencyDisplay::getInstance ();
  835. $value = $currency->convertCurrencyTo ($vendor_currency->virtuemart_currency_id, $value);
  836. $taxrules = array();
  837. if (!empty($tax_id)) {
  838. $q = 'SELECT * FROM #__virtuemart_calcs WHERE `virtuemart_calc_id`="' . $tax_id . '" ';
  839. $db->setQuery ($q);
  840. $taxrules = $db->loadAssocList ();
  841. }
  842. if (count ($taxrules) > 0) {
  843. $salesPrice = $calculator->roundInternal ($calculator->executeCalculation ($taxrules, $value));
  844. } else {
  845. $salesPrice = $value;
  846. }
  847. return $salesPrice;
  848. }
  849. /**
  850. * logPaymentInfo
  851. * to help debugging Payment notification for example
  852. */
  853. protected function logInfo ($text, $type = 'message') {
  854. if ($this->_debug) {
  855. $file = JPATH_ROOT . "/logs/" . $this->_name . ".log";
  856. $date = JFactory::getDate ();
  857. $fp = fopen ($file, 'a');
  858. fwrite ($fp, "\n\n" . $date->toFormat ('%Y-%m-%d %H:%M:%S'));
  859. fwrite ($fp, "\n" . $type . ': ' . $text);
  860. fclose ($fp);
  861. }
  862. }
  863. public function processConfirmedOrderPaymentResponse ($returnValue, $cart, $order, $html, $payment_name, $new_status = '') {
  864. if ($returnValue == 1) {
  865. //We delete the old stuff
  866. // send the email only if payment has been accepted
  867. // update status
  868. $modelOrder = VmModel::getModel ('orders');
  869. $order['order_status'] = $new_status;
  870. $order['customer_notified'] = 1;
  871. $order['comments'] = '';
  872. $modelOrder->updateStatusForOneOrder ($order['details']['BT']->virtuemart_order_id, $order, TRUE);
  873. $order['paymentName'] = $payment_name;
  874. //if(!class_exists('shopFunctionsF')) require(JPATH_VM_SITE.DS.'helpers'.DS.'shopfunctionsf.php');
  875. //shopFunctionsF::sentOrderConfirmedEmail($order);
  876. //We delete the old stuff
  877. $cart->emptyCart ();
  878. JRequest::setVar ('html', $html);
  879. // payment echos form, but cart should not be emptied, data is valid
  880. } elseif ($returnValue == 2) {
  881. $cart->_confirmDone = FALSE;
  882. $cart->_dataValidated = FALSE;
  883. $cart->setCartIntoSession ();
  884. JRequest::setVar ('html', $html);
  885. } elseif ($returnValue == 0) {
  886. // error while processing the payment
  887. $mainframe = JFactory::getApplication ();
  888. $mainframe->enqueueMessage ($html);
  889. $mainframe->redirect (JRoute::_ ('index.php?option=com_virtuemart&view=cart'), JText::_ ('COM_VIRTUEMART_CART_ORDERDONE_DATA_NOT_VALID'));
  890. }
  891. }
  892. function emptyCart ($session_id = NULL, $order_number = NULL) {
  893. if (!class_exists ('VirtueMartCart')) {
  894. require(JPATH_VM_SITE . DS . 'helpers' . DS . 'cart.php');
  895. }
  896. $this->logInfo ('Notification: emptyCart ' . $session_id, 'message');
  897. if ($session_id != NULL and $order_number != NULL) {
  898. // Recover session from the storage session in wich the payment is done
  899. $this->emptyCartFromStorageSession ($session_id, $order_number);
  900. } else {
  901. $cart = VirtueMartCart::getCart ();
  902. $cart->emptyCart ();
  903. }
  904. return TRUE;
  905. }
  906. /*
  907. * recovers the session from Storage, and only empty the cart if it has not been done already
  908. */
  909. function emptyCartFromStorageSession ($session_id, $order_number) {
  910. $conf = JFactory::getConfig ();
  911. $handler = $conf->get ('session_handler', 'none');
  912. $config['session_name'] = 'site';
  913. $name = Japplication::getHash ($config['session_name']);
  914. $options['name'] = $name;
  915. $sessionStorage = JSessionStorage::getInstance ($handler, $options);
  916. // The session store MUST be registered.
  917. $sessionStorage->register ();
  918. // reads directly the session from the storage
  919. $sessionStored = $sessionStorage->read ($session_id);
  920. if (empty($sessionStored)) {
  921. return;
  922. }
  923. $sessionStorageDecoded = self::session_decode ($sessionStored);
  924. $vm_namespace = '__vm';
  925. $cart_name = 'vmcart';
  926. if (array_key_exists ($vm_namespace, $sessionStorageDecoded)) { // vm session is there
  927. $vm_sessionStorage = $sessionStorageDecoded[$vm_namespace];
  928. if (array_key_exists ($cart_name, $vm_sessionStorage)) { // vm cart session is there
  929. $sessionStorageCart = unserialize ($vm_sessionStorage[$cart_name]);
  930. // only empty the cart if the order number is still there. If not there, it means that the cart has already been emptied.
  931. if ($sessionStorageCart->order_number == $order_number) {
  932. if (!class_exists ('VirtueMartCart')) {
  933. require(JPATH_VM_SITE . DS . 'helpers' . DS . 'cart.php');
  934. }
  935. VirtueMartCart::emptyCartValues ($sessionStorageCart);
  936. $sessionStorageDecoded[$vm_namespace][$cart_name] = serialize ($sessionStorageCart);
  937. $sessionStorageEncoded = self::session_encode ($sessionStorageDecoded);
  938. $sessionStorage->write ($session_id, $sessionStorageEncoded);
  939. }
  940. }
  941. }
  942. }
  943. private static function session_decode ($session_data) {
  944. $decoded_session = array();
  945. $offset = 0;
  946. while ($offset < strlen ($session_data)) {
  947. if (!strstr (substr ($session_data, $offset), "|")) {
  948. return array();
  949. }
  950. $pos = strpos ($session_data, "|", $offset);
  951. $num = $pos - $offset;
  952. $varname = substr ($session_data, $offset, $num);
  953. $offset += $num + 1;
  954. $data = unserialize (substr ($session_data, $offset));
  955. $decoded_session[$varname] = $data;
  956. $offset += strlen (serialize ($data));
  957. }
  958. return $decoded_session;
  959. }
  960. private static function session_encode ($session_data_array) {
  961. $encoded_session = "";
  962. foreach ($session_data_array as $key => $session_data) {
  963. $encoded_session .= $key . "|" . serialize ($session_data);
  964. }
  965. return $encoded_session;
  966. }
  967. /**
  968. * get_passkey
  969. * Retrieve the payment method-specific encryption key
  970. *
  971. * @author Oscar van Eijk
  972. * @author Valerie Isaksen
  973. * @return mixed
  974. * @deprecated
  975. */
  976. function get_passkey () {
  977. return TRUE;
  978. $_db = JFactory::getDBO ();
  979. $_q = 'SELECT ' . VM_DECRYPT_FUNCTION . "(secret_key, '" . ENCODE_KEY . "') as passkey "
  980. . 'FROM #__virtuemart_paymentmethods '
  981. . "WHERE virtuemart_paymentmethod_id='" . (int)$this->_virtuemart_paymentmethod_id . "'";
  982. $_db->setQuery ($_q);
  983. $_r = $_db->loadAssoc (); // TODO Error check
  984. return $_r['passkey'];
  985. }
  986. /**
  987. * validateVendor
  988. * Check if this plugin has methods for the current vendor.
  989. *
  990. * @author Oscar van Eijk
  991. * @param integer $_vendorId The vendor ID taken from the cart.
  992. * @return True when a id was found for this vendor, false otherwise
  993. *
  994. * @deprecated ????
  995. */
  996. protected function validateVendor ($_vendorId) {
  997. if (!$_vendorId) {
  998. $_vendorId = 1;
  999. }
  1000. $_db = JFactory::getDBO ();
  1001. if (JVM_VERSION === 1) {
  1002. $_q = 'SELECT 1 '
  1003. . 'FROM #__virtuemart_' . $this->_psType . 'methods v '
  1004. . ', #__plugins j '
  1005. . 'WHERE j.`element` = "' . $this->_name . '" '
  1006. . 'AND v.`' . $this->_psType . '_jplugin_id` = j.`id` '
  1007. . 'AND v.`virtuemart_vendor_id` = "' . $_vendorId . '" '
  1008. . 'AND v.`published` = 1 ';
  1009. } else {
  1010. $_q = 'SELECT 1 '
  1011. . 'FROM #__virtuemart_' . $this->_psType . 'methods AS v '
  1012. . ', #__extensions AS j '
  1013. . 'WHERE j.`folder` = "' . $this->_type . '" '
  1014. . 'AND j.`element` = "' . $this->_name . '" '
  1015. . 'AND v.`' . $this->_psType . '_jplugin_id` = j.`extension_id` '
  1016. . 'AND v.`virtuemart_vendor_id` = "' . $_vendorId . '" '
  1017. . 'AND v.`published` = 1 ';
  1018. }
  1019. $_db->setQuery ($_q);
  1020. $_r = $_db->loadAssoc ();
  1021. if ($_r) {
  1022. return TRUE;
  1023. } else {
  1024. return FALSE;
  1025. }
  1026. }
  1027. function handlePaymentUserCancel ($virtuemart_order_id) {
  1028. if ($virtuemart_order_id) {
  1029. // set the order to cancel , to handle the stock correctly
  1030. if (!class_exists ('VirtueMartModelOrders')) {
  1031. require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'orders.php');
  1032. }
  1033. $modelOrder = VmModel::getModel ('orders');
  1034. $order['order_status'] = 'X';
  1035. $order['virtuemart_order_id'] = $virtuemart_order_id;
  1036. $order['customer_notified'] = 0;
  1037. $order['comments'] = JText::_ ('COM_VIRTUEMART_PAYMENT_CANCELLED_BY_SHOPPER');
  1038. $modelOrder->updateStatusForOneOrder ($virtuemart_order_id, $order, TRUE);
  1039. //$modelOrder->remove (array('virtuemart_order_id' => $virtuemart_order_id));
  1040. }
  1041. }
  1042. }