PAYONE-GmbH /
magento-2
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify |
||
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
||
| 6 | * the Free Software Foundation, either version 3 of the License, or |
||
| 7 | * (at your option) any later version. |
||
| 8 | * |
||
| 9 | * PAYONE Magento 2 Connector is distributed in the hope that it will be useful, |
||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 12 | * GNU Lesser General Public License for more details. |
||
| 13 | * |
||
| 14 | * You should have received a copy of the GNU Lesser General Public License |
||
| 15 | * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>. |
||
| 16 | * |
||
| 17 | * PHP version 5 |
||
| 18 | * |
||
| 19 | * @category Payone |
||
| 20 | * @package Payone_Magento2_Plugin |
||
| 21 | * @author FATCHIP GmbH <[email protected]> |
||
| 22 | * @copyright 2003 - 2018 Payone GmbH |
||
| 23 | * @license <http://www.gnu.org/licenses/> GNU Lesser General Public License |
||
| 24 | * @link http://www.payone.de |
||
| 25 | */ |
||
| 26 | |||
| 27 | namespace Payone\Core\Model\ResourceModel; |
||
| 28 | |||
| 29 | use Payone\Core\Model\PayoneConfig; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * SavedPaymentData resource model |
||
| 33 | */ |
||
| 34 | class SavedPaymentData extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb |
||
|
0 ignored issues
–
show
|
|||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Payment helper object |
||
| 38 | * |
||
| 39 | * @var \Payone\Core\Helper\Payment |
||
| 40 | */ |
||
| 41 | protected $paymentHelper; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Encryption method used |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $encryptionMethod = 'AES-128-ECB'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Class constructor |
||
| 52 | * |
||
| 53 | * @param \Magento\Framework\Model\ResourceModel\Db\Context $context |
||
| 54 | * @param \Payone\Core\Helper\Payment $paymentHelper |
||
| 55 | * @param string $connectionName |
||
| 56 | */ |
||
| 57 | public function __construct( |
||
| 58 | \Magento\Framework\Model\ResourceModel\Db\Context $context, |
||
|
0 ignored issues
–
show
The type
Magento\Framework\Model\ResourceModel\Db\Context was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 59 | \Payone\Core\Helper\Payment $paymentHelper, |
||
| 60 | $connectionName = null |
||
| 61 | ) { |
||
| 62 | parent::__construct($context, $connectionName); |
||
| 63 | $this->paymentHelper = $paymentHelper; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Initialize connection and table |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | protected function _construct() |
||
| 72 | { |
||
| 73 | $this->_init('payone_saved_payment_data', 'id'); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Check if customer has saved data for the given payment method |
||
| 78 | * |
||
| 79 | * @param $iCustomerId |
||
| 80 | * @param $sPaymentMethod |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | protected function hasSavedData($iCustomerId, $sPaymentMethod) |
||
| 84 | { |
||
| 85 | $aSavedPaymentData = $this->getSavedPaymentData($iCustomerId, $sPaymentMethod); |
||
| 86 | if (empty($aSavedPaymentData)) { |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | return true; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the encryption key |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | protected function getEncryptionKey() |
||
| 98 | { |
||
| 99 | return $this->paymentHelper->getConfigParam('mid'); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Encode and json_encode payment data for db |
||
| 104 | * |
||
| 105 | * @param array $aPaymentData |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function encryptPaymentData($aPaymentData) |
||
| 109 | { |
||
| 110 | $sPaymentData = json_encode($aPaymentData); |
||
| 111 | $sEncryptedData = openssl_encrypt($sPaymentData, $this->encryptionMethod, $this->getEncryptionKey()); |
||
| 112 | return $sEncryptedData; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Decrypt given and json_decode string |
||
| 117 | * |
||
| 118 | * @param string $sEncryptedPaymentData |
||
| 119 | * @return array|false |
||
| 120 | */ |
||
| 121 | protected function decryptPaymentData($sEncryptedPaymentData) |
||
| 122 | { |
||
| 123 | $sDecryptedData = openssl_decrypt($sEncryptedPaymentData, $this->encryptionMethod, $this->getEncryptionKey()); |
||
| 124 | $aPaymentData = false; |
||
| 125 | if (!empty($sDecryptedData)) { |
||
| 126 | $aPaymentData = json_decode($sDecryptedData, true); |
||
| 127 | } |
||
| 128 | return $aPaymentData; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Check if the given data already exists in the DB |
||
| 133 | * |
||
| 134 | * @param array $aData |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | protected function dataAlreadyExists($aData) |
||
| 138 | { |
||
| 139 | $oSelect = $this->getConnection()->select() |
||
| 140 | ->from($this->getMainTable(), ['id']) |
||
| 141 | ->where("customer_id = :customerId") |
||
| 142 | ->where("payment_method = :paymentMethod") |
||
| 143 | ->where("payment_data = :paymentData") |
||
| 144 | ->limit(1); |
||
| 145 | |||
| 146 | $aParams = [ |
||
| 147 | 'customerId' => $aData['customer_id'], |
||
| 148 | 'paymentMethod' => $aData['payment_method'], |
||
| 149 | 'paymentData' => $aData['payment_data'], |
||
| 150 | ]; |
||
| 151 | |||
| 152 | $iId = $this->getConnection()->fetchOne($oSelect, $aParams); |
||
| 153 | if (!empty($iId)) { |
||
| 154 | return true; |
||
| 155 | } |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Insert new line into payone_saved_payment_data table |
||
| 161 | * |
||
| 162 | * @param int $iCustomerId |
||
| 163 | * @param string $sPaymentMethod |
||
| 164 | * @param array $aPaymentData |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | public function addSavedPaymentData($iCustomerId, $sPaymentMethod, $aPaymentData) |
||
| 168 | { |
||
| 169 | $aData = [ |
||
| 170 | 'customer_id' => $iCustomerId, |
||
| 171 | 'payment_method' => $sPaymentMethod, |
||
| 172 | 'is_default' => $this->hasSavedData($iCustomerId, $sPaymentMethod) === true ? '0' : '1', |
||
| 173 | 'payment_data' => $this->encryptPaymentData($aPaymentData), |
||
| 174 | ]; |
||
| 175 | |||
| 176 | if ($this->dataAlreadyExists($aData) === false) { |
||
| 177 | $this->getConnection()->insert($this->getMainTable(), $aData); |
||
| 178 | } |
||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Format single data entry |
||
| 184 | * |
||
| 185 | * @param array $aData |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | protected function formatData(&$aData) |
||
| 189 | { |
||
| 190 | if (!empty($aData['payment_data'])) { |
||
| 191 | $aData['payment_data'] = $this->decryptPaymentData($aData['payment_data']); |
||
| 192 | if (!empty($aData['payment_data']['firstname']) && !empty($aData['payment_data']['lastname'])) { |
||
| 193 | $aData['payment_data']['cardholder'] = $aData['payment_data']['firstname'].' '.$aData['payment_data']['lastname']; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | return $aData; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get saved payment data for customer |
||
| 201 | * |
||
| 202 | * @param int $iCustomerId |
||
| 203 | * @param string|bool $sPaymentMethod |
||
| 204 | * @param bool $blFilterInactiveTypes |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function getSavedPaymentData($iCustomerId, $sPaymentMethod = false, $blFilterInactiveTypes = true) |
||
| 208 | { |
||
| 209 | if (!$iCustomerId) { |
||
| 210 | return []; |
||
| 211 | } |
||
| 212 | |||
| 213 | $oSelect = $this->getConnection()->select() |
||
| 214 | ->from($this->getMainTable()) |
||
| 215 | ->where("customer_id = :customerId") |
||
| 216 | ->order(['is_default DESC', 'created_at ASC']); |
||
| 217 | |||
| 218 | $aParams = ['customerId' => $iCustomerId]; |
||
| 219 | |||
| 220 | if ($sPaymentMethod !== false) { |
||
| 221 | $oSelect->where("payment_method = :paymentMethod"); |
||
| 222 | $aParams['paymentMethod'] = $sPaymentMethod; |
||
| 223 | } |
||
| 224 | |||
| 225 | $aReturn = []; |
||
| 226 | |||
| 227 | $aCreditcardTypes = []; |
||
| 228 | if ($sPaymentMethod == PayoneConfig::METHOD_CREDITCARD) { |
||
| 229 | $aCreditcardArray = $this->paymentHelper->getAvailableCreditcardTypes(); |
||
| 230 | foreach ($aCreditcardArray as $aTypeArray) { |
||
| 231 | $aCreditcardTypes[] = $aTypeArray['id']; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | $aResult = $this->getConnection()->fetchAll($oSelect, $aParams); |
||
| 236 | foreach ($aResult as $aRow) { |
||
| 237 | $aRow = $this->formatData($aRow); |
||
| 238 | if (!empty($aRow['payment_data']) && ($sPaymentMethod != PayoneConfig::METHOD_CREDITCARD || $blFilterInactiveTypes === false || in_array($aRow['payment_data']['cardtype'], $aCreditcardTypes))) { |
||
| 239 | $aReturn[] = $aRow; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | return $aReturn; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Delete the entity of the given id |
||
| 247 | * |
||
| 248 | * @param int $iId |
||
| 249 | * @param int $iCustomerId |
||
| 250 | * @param string|bool $sPaymentMethod |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | public function deletePaymentData($iId, $iCustomerId, $sPaymentMethod = false) |
||
| 254 | { |
||
| 255 | $this->getConnection()->delete($this->getMainTable(), ['id = ?' => $iId]); |
||
| 256 | |||
| 257 | $aAllRows = $this->getSavedPaymentData($iCustomerId, $sPaymentMethod); |
||
| 258 | if (!empty($aAllRows)) { |
||
| 259 | $aFirstRow = current($aAllRows); |
||
| 260 | $this->setDefault($aFirstRow['id'], $iCustomerId); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Set given id as default payment for the given customer |
||
| 266 | * |
||
| 267 | * @param int $iId |
||
| 268 | * @param int $iCustomerId |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | public function setDefault($iId, $iCustomerId) |
||
| 272 | { |
||
| 273 | $data = ['is_default' => 0]; |
||
| 274 | $where = ['customer_id = ?' => $iCustomerId]; |
||
| 275 | $this->getConnection()->update($this->getMainTable(), $data, $where); |
||
| 276 | |||
| 277 | $data = ['is_default' => 1]; |
||
| 278 | $where = ['id = ?' => $iId]; |
||
| 279 | $this->getConnection()->update($this->getMainTable(), $data, $where); |
||
| 280 | } |
||
| 281 | } |
||
| 282 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths