123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- /**
- *
- * PHP Pro Bid $Id$ Gg0ILE2Bm/0VpUF9mfUZo2CSC1jiaQ0JwoamoEXklqY=
- *
- * @link http://www.phpprobid.com
- * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
- * @license http://www.phpprobid.com/license Commercial License
- *
- * @version 7.2
- */
- /**
- * payment gateways and gateways settings tables service class
- */
- namespace Ppb\Service\Table;
- use Ppb\Db\Table,
- Ppb\Service\PaymentGatewaysSettings,
- Cube\Db\Expr;
- class PaymentGateways extends AbstractServiceTable
- {
- /**
- *
- * gateways settings service
- *
- * @var \Ppb\Service\PaymentGatewaysSettings
- */
- protected $_paymentGatewaysSettings;
- /**
- *
- * class constructor
- */
- public function __construct()
- {
- parent::__construct();
- $this->setTable(
- new Table\PaymentGateways());
- $this->setPaymentGatewaysSettings();
- }
- /**
- *
- * get payment gateways settings service
- *
- * @return \Ppb\Service\PaymentGatewaysSettings
- */
- public function getPaymentGatewaysSettings()
- {
- return $this->_paymentGatewaysSettings;
- }
- /**
- *
- * set payment gateways settings service
- *
- * @param \Ppb\Service\PaymentGatewaysSettings $paymentGatewaysSettings
- *
- * @return \Ppb\Service\Table\PaymentGateways
- */
- public function setPaymentGatewaysSettings(PaymentGatewaysSettings $paymentGatewaysSettings = null)
- {
- if (!$paymentGatewaysSettings instanceof PaymentGatewaysSettings) {
- $paymentGatewaysSettings = new PaymentGatewaysSettings();
- }
- $this->_paymentGatewaysSettings = $paymentGatewaysSettings;
- return $this;
- }
- /**
- *
- * get gateways data from the 'payment_gateways' and 'payment_gateways_settings' tables
- *
- * @param int|bool $userId user id (when fetching direct payment gateways)
- * @param int $gatewayIds gateway ids (to fetch data for specific gateways)
- * @param bool $activeOnly show only active gateways
- * @param bool $singleRow whether to display as single row result if only one row matches the query
- *
- * @return array
- */
- public function getData($userId = null, $gatewayIds = null, $activeOnly = false, $singleRow = false)
- {
- $select = $this->_table->select();
- if ($gatewayIds !== null) {
- $select->where('id IN (?)', new Expr(implode(', ', (array)$gatewayIds)));
- }
- if ($activeOnly) {
- if ($userId !== null || $userId === true) {
- $select->where('direct_payment = ?', 1);
- }
- else {
- $select->where('site_fees = ?', 1);
- }
- }
- $select->order('order_id ASC');
- $gateways = $this->fetchAll($select)->toArray();
- foreach ($gateways as $key => $gateway) {
- $select = $this->_paymentGatewaysSettings->getTable()->select()
- ->where('gateway_id = ?', $gateway['id']);
- if ($userId !== null) {
- $select->where('user_id = ?', $userId);
- }
- else {
- $select->where('user_id IS NULL');
- }
- $gatewayParams = $this->_paymentGatewaysSettings->fetchAll($select);
- foreach ($gatewayParams as $param) {
- $gateways[$key][$param['name']] = $param['value'];
- }
- }
- return (count($gateways) == 1 && $singleRow === true) ? $gateways[0] : $gateways;
- }
- /**
- *
- * get all active gateways
- * if user id is provided, get all enabled direct payment gateways
- * >> we will always return all available direct payment gateways, even if the seller hasnt set them up,
- * so that he can set them up later
- *
- * @param int $userId
- *
- * @return array
- */
- public function getMultiOptions($userId = null)
- {
- $data = array();
- $translate = $this->getTranslate();
- $select = $this->_table->select()
- ->order(array('order_id ASC', 'name ASC'));
- if ($userId === null) {
- $select->where('site_fees = ?', 1);
- }
- else {
- $select->where('direct_payment = ?', 1);
- }
- $rows = $this->_table->fetchAll($select);
- foreach ($rows as $row) {
- $data[(string)$row['id']] = $translate->_($row['name']);
- }
- return $data;
- }
- /**
- *
- * save data in the tables (for the 'payment_gateways_settings' table, insert if key doesnt exist or update if it does)
- *
- * @param array $data
- * @param int $userId
- *
- * @throws \InvalidArgumentException
- * @return \Ppb\Service\Table\PaymentGateways
- */
- public function save(array $data, $userId = null)
- {
- if (!isset($data['id'])) {
- throw new \InvalidArgumentException("The form must use an element with the name 'id'.");
- }
- $columns = array('id', 'site_fees', 'direct_payment');
- $params = $data;
- foreach ($data as $key => $value) {
- if (!in_array($key, $columns)) {
- unset($data[$key]);
- }
- else {
- unset($params[$key]);
- }
- }
- foreach ($params as $key => $param) {
- $gatewayId = key($param);
- $value = $param[$gatewayId];
- $select = $this->_paymentGatewaysSettings->getTable()->select()
- ->where('name = ?', $key)
- ->where('gateway_id = ?', $gatewayId);
- if ($userId !== null) {
- $select->where('user_id = ?', $userId);
- }
- else {
- $select->where('user_id IS NULL');
- }
- $row = $this->_paymentGatewaysSettings->getTable()->fetchRow($select);
- $input = array(
- 'name' => $key,
- 'value' => $value,
- 'gateway_id' => $gatewayId,
- );
- if ($userId !== null) {
- $input['user_id'] = intval($userId);
- }
- if (count($row) > 0) {
- $input['id'] = $row['id'];
- }
- $this->_paymentGatewaysSettings->save($input);
- }
- parent::save($data);
- return $this;
- }
- public function getDirectPaymentFields()
- {
- $gateways = $this->getData(true, null, true);
- $gatewayFields = array();
- foreach ($gateways as $gateway) {
- $className = '\\Ppb\\Model\\PaymentGateway\\' . $gateway['name'];
- if (class_exists($className)) {
- /* @var \Ppb\Model\PaymentGateway\AbstractPaymentGateway $gatewayModel */
- $gatewayModel = new $className();
- $elements = $gatewayModel->getElements();
- foreach ($elements as $element) {
- $gatewayFields[] = array(
- 'gateway_id' => $gateway['id'],
- 'name' => $element['id'],
- );
- }
- }
- }
- return $gatewayFields;
- }
- public function getColumns()
- {
- }
- public function getElements()
- {
- }
- }
|