PaymentGateways.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ Gg0ILE2Bm/0VpUF9mfUZo2CSC1jiaQ0JwoamoEXklqY=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.2
  11. */
  12. /**
  13. * payment gateways and gateways settings tables service class
  14. */
  15. namespace Ppb\Service\Table;
  16. use Ppb\Db\Table,
  17. Ppb\Service\PaymentGatewaysSettings,
  18. Cube\Db\Expr;
  19. class PaymentGateways extends AbstractServiceTable
  20. {
  21. /**
  22. *
  23. * gateways settings service
  24. *
  25. * @var \Ppb\Service\PaymentGatewaysSettings
  26. */
  27. protected $_paymentGatewaysSettings;
  28. /**
  29. *
  30. * class constructor
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. $this->setTable(
  36. new Table\PaymentGateways());
  37. $this->setPaymentGatewaysSettings();
  38. }
  39. /**
  40. *
  41. * get payment gateways settings service
  42. *
  43. * @return \Ppb\Service\PaymentGatewaysSettings
  44. */
  45. public function getPaymentGatewaysSettings()
  46. {
  47. return $this->_paymentGatewaysSettings;
  48. }
  49. /**
  50. *
  51. * set payment gateways settings service
  52. *
  53. * @param \Ppb\Service\PaymentGatewaysSettings $paymentGatewaysSettings
  54. *
  55. * @return \Ppb\Service\Table\PaymentGateways
  56. */
  57. public function setPaymentGatewaysSettings(PaymentGatewaysSettings $paymentGatewaysSettings = null)
  58. {
  59. if (!$paymentGatewaysSettings instanceof PaymentGatewaysSettings) {
  60. $paymentGatewaysSettings = new PaymentGatewaysSettings();
  61. }
  62. $this->_paymentGatewaysSettings = $paymentGatewaysSettings;
  63. return $this;
  64. }
  65. /**
  66. *
  67. * get gateways data from the 'payment_gateways' and 'payment_gateways_settings' tables
  68. *
  69. * @param int|bool $userId user id (when fetching direct payment gateways)
  70. * @param int $gatewayIds gateway ids (to fetch data for specific gateways)
  71. * @param bool $activeOnly show only active gateways
  72. * @param bool $singleRow whether to display as single row result if only one row matches the query
  73. *
  74. * @return array
  75. */
  76. public function getData($userId = null, $gatewayIds = null, $activeOnly = false, $singleRow = false)
  77. {
  78. $select = $this->_table->select();
  79. if ($gatewayIds !== null) {
  80. $select->where('id IN (?)', new Expr(implode(', ', (array)$gatewayIds)));
  81. }
  82. if ($activeOnly) {
  83. if ($userId !== null || $userId === true) {
  84. $select->where('direct_payment = ?', 1);
  85. }
  86. else {
  87. $select->where('site_fees = ?', 1);
  88. }
  89. }
  90. $select->order('order_id ASC');
  91. $gateways = $this->fetchAll($select)->toArray();
  92. foreach ($gateways as $key => $gateway) {
  93. $select = $this->_paymentGatewaysSettings->getTable()->select()
  94. ->where('gateway_id = ?', $gateway['id']);
  95. if ($userId !== null) {
  96. $select->where('user_id = ?', $userId);
  97. }
  98. else {
  99. $select->where('user_id IS NULL');
  100. }
  101. $gatewayParams = $this->_paymentGatewaysSettings->fetchAll($select);
  102. foreach ($gatewayParams as $param) {
  103. $gateways[$key][$param['name']] = $param['value'];
  104. }
  105. }
  106. return (count($gateways) == 1 && $singleRow === true) ? $gateways[0] : $gateways;
  107. }
  108. /**
  109. *
  110. * get all active gateways
  111. * if user id is provided, get all enabled direct payment gateways
  112. * >> we will always return all available direct payment gateways, even if the seller hasnt set them up,
  113. * so that he can set them up later
  114. *
  115. * @param int $userId
  116. *
  117. * @return array
  118. */
  119. public function getMultiOptions($userId = null)
  120. {
  121. $data = array();
  122. $translate = $this->getTranslate();
  123. $select = $this->_table->select()
  124. ->order(array('order_id ASC', 'name ASC'));
  125. if ($userId === null) {
  126. $select->where('site_fees = ?', 1);
  127. }
  128. else {
  129. $select->where('direct_payment = ?', 1);
  130. }
  131. $rows = $this->_table->fetchAll($select);
  132. foreach ($rows as $row) {
  133. $data[(string)$row['id']] = $translate->_($row['name']);
  134. }
  135. return $data;
  136. }
  137. /**
  138. *
  139. * save data in the tables (for the 'payment_gateways_settings' table, insert if key doesnt exist or update if it does)
  140. *
  141. * @param array $data
  142. * @param int $userId
  143. *
  144. * @throws \InvalidArgumentException
  145. * @return \Ppb\Service\Table\PaymentGateways
  146. */
  147. public function save(array $data, $userId = null)
  148. {
  149. if (!isset($data['id'])) {
  150. throw new \InvalidArgumentException("The form must use an element with the name 'id'.");
  151. }
  152. $columns = array('id', 'site_fees', 'direct_payment');
  153. $params = $data;
  154. foreach ($data as $key => $value) {
  155. if (!in_array($key, $columns)) {
  156. unset($data[$key]);
  157. }
  158. else {
  159. unset($params[$key]);
  160. }
  161. }
  162. foreach ($params as $key => $param) {
  163. $gatewayId = key($param);
  164. $value = $param[$gatewayId];
  165. $select = $this->_paymentGatewaysSettings->getTable()->select()
  166. ->where('name = ?', $key)
  167. ->where('gateway_id = ?', $gatewayId);
  168. if ($userId !== null) {
  169. $select->where('user_id = ?', $userId);
  170. }
  171. else {
  172. $select->where('user_id IS NULL');
  173. }
  174. $row = $this->_paymentGatewaysSettings->getTable()->fetchRow($select);
  175. $input = array(
  176. 'name' => $key,
  177. 'value' => $value,
  178. 'gateway_id' => $gatewayId,
  179. );
  180. if ($userId !== null) {
  181. $input['user_id'] = intval($userId);
  182. }
  183. if (count($row) > 0) {
  184. $input['id'] = $row['id'];
  185. }
  186. $this->_paymentGatewaysSettings->save($input);
  187. }
  188. parent::save($data);
  189. return $this;
  190. }
  191. public function getDirectPaymentFields()
  192. {
  193. $gateways = $this->getData(true, null, true);
  194. $gatewayFields = array();
  195. foreach ($gateways as $gateway) {
  196. $className = '\\Ppb\\Model\\PaymentGateway\\' . $gateway['name'];
  197. if (class_exists($className)) {
  198. /* @var \Ppb\Model\PaymentGateway\AbstractPaymentGateway $gatewayModel */
  199. $gatewayModel = new $className();
  200. $elements = $gatewayModel->getElements();
  201. foreach ($elements as $element) {
  202. $gatewayFields[] = array(
  203. 'gateway_id' => $gateway['id'],
  204. 'name' => $element['id'],
  205. );
  206. }
  207. }
  208. }
  209. return $gatewayFields;
  210. }
  211. public function getColumns()
  212. {
  213. }
  214. public function getElements()
  215. {
  216. }
  217. }