From 5e42b54da59e6e781208340a07f557205ac9f99b Mon Sep 17 00:00:00 2001 From: Nuryagdy Mustapayev Date: Fri, 26 Apr 2024 10:26:34 +0200 Subject: [PATCH] run rector php --- src/Crypt/AbstractCrypt.php | 4 ++-- src/Crypt/AkbankPosCrypt.php | 1 + .../RequestDataMapper/AbstractRequestDataMapper.php | 1 + .../RequestDataMapper/AkbankPosRequestDataMapper.php | 1 + .../PosNetV1PosRequestDataMapper.php | 2 +- .../AbstractResponseDataMapper.php | 1 + .../AkbankPosResponseDataMapper.php | 12 +++++++++--- .../KuveytPosResponseDataMapper.php | 12 ++---------- src/Gateways/AkbankPos.php | 7 ++----- src/Gateways/KuveytPos.php | 1 + src/Serializer/AkbankPosSerializer.php | 6 +++++- tests/Functional/KuveytPosTest.php | 3 ++- tests/Functional/PaymentTestTrait.php | 3 +-- .../KuveytPosRequestDataMapperTest.php | 3 +++ .../KuveytPosResponseDataMapperTest.php | 1 + 15 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/Crypt/AbstractCrypt.php b/src/Crypt/AbstractCrypt.php index 80efadc5..56890914 100644 --- a/src/Crypt/AbstractCrypt.php +++ b/src/Crypt/AbstractCrypt.php @@ -36,8 +36,8 @@ public function generateRandomString(int $length = 24): string $charactersLength = \strlen($characters); $randomString = ''; - for ($i = 0; $i < $length; $i++) { - $randomString .= $characters[\rand(0, $charactersLength - 1)]; + for ($i = 0; $i < $length; ++$i) { + $randomString .= $characters[random_int(0, $charactersLength - 1)]; } return $randomString; diff --git a/src/Crypt/AkbankPosCrypt.php b/src/Crypt/AkbankPosCrypt.php index 1757bd41..82bbdb53 100644 --- a/src/Crypt/AkbankPosCrypt.php +++ b/src/Crypt/AkbankPosCrypt.php @@ -107,6 +107,7 @@ public function hashString(string $str, ?string $encryptionKey = null): string if (null === $encryptionKey) { throw new \LogicException('Encryption key zorunlu!'); } + $str = \hash_hmac(static::HASH_ALGORITHM, $str, $encryptionKey, true); return \base64_encode($str); diff --git a/src/DataMapper/RequestDataMapper/AbstractRequestDataMapper.php b/src/DataMapper/RequestDataMapper/AbstractRequestDataMapper.php index 8a8ea7c4..044b255c 100644 --- a/src/DataMapper/RequestDataMapper/AbstractRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/AbstractRequestDataMapper.php @@ -173,6 +173,7 @@ public function isSupportedTxType(string $txType, ?string $paymentModel = null): if (!isset($this->txTypeMappings[$txType])) { return false; } + if (\is_array($this->txTypeMappings[$txType])) { if (null === $paymentModel) { return false; diff --git a/src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php index e6a88345..8ee56068 100644 --- a/src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php @@ -377,6 +377,7 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s if (null !== $posAccount->getSubMerchantId()) { $inputs['subMerchantId'] = $posAccount->getSubMerchantId(); } + if ($creditCard instanceof CreditCardInterface) { $inputs['creditCard'] = $creditCard->getNumber(); $inputs['expiredDate'] = $creditCard->getExpirationDate(self::CREDIT_CARD_EXP_DATE_FORMAT); diff --git a/src/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapper.php index c8f4b473..35a48e6d 100644 --- a/src/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapper.php @@ -356,7 +356,7 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s 'Amount' => (string) $this->formatAmount($order['amount']), 'CurrencyCode' => (string) $this->mapCurrency($order['currency']), 'MerchantReturnURL' => (string) $order['success_url'], - 'InstallmentCount' => (string) $this->mapInstallment($order['installment']), + 'InstallmentCount' => $this->mapInstallment($order['installment']), 'Language' => $this->getLang($posAccount, $order), 'TxnState' => 'INITIAL', 'OpenNewWindow' => '0', diff --git a/src/DataMapper/ResponseDataMapper/AbstractResponseDataMapper.php b/src/DataMapper/ResponseDataMapper/AbstractResponseDataMapper.php index 73bdc854..ba6f8031 100644 --- a/src/DataMapper/ResponseDataMapper/AbstractResponseDataMapper.php +++ b/src/DataMapper/ResponseDataMapper/AbstractResponseDataMapper.php @@ -57,6 +57,7 @@ public function mapTxType($txType): ?string if (\is_array($mapping) && \in_array($txType, $mapping, true)) { return $mappedTxType; } + if ($mapping === $txType) { return $mappedTxType; } diff --git a/src/DataMapper/ResponseDataMapper/AkbankPosResponseDataMapper.php b/src/DataMapper/ResponseDataMapper/AkbankPosResponseDataMapper.php index aa9a79eb..6828e8ea 100644 --- a/src/DataMapper/ResponseDataMapper/AkbankPosResponseDataMapper.php +++ b/src/DataMapper/ResponseDataMapper/AkbankPosResponseDataMapper.php @@ -289,21 +289,26 @@ public function mapOrderHistoryResponse(array $rawResponseData): array } else { $transactions[] = $this->mapSingleOrderHistoryTransaction($rawTx); } - $orderId = $orderId ?? $rawTx['orgOrderId'] ?? $rawTx['orderId'] ?? null; + + $orderId ??= $rawTx['orgOrderId'] ?? $rawTx['orderId'] ?? null; } } if (!$isRecurringOrder) { - \usort($transactions, function (array $tx1, array $tx2) { + \usort($transactions, static function (array $tx1, array $tx2) { if (null !== $tx1['transaction_time'] && null === $tx2['transaction_time']) { return 1; } + if (null === $tx1['transaction_time'] && null !== $tx2['transaction_time']) { return -1; } + if ($tx1['transaction_time'] == $tx2['transaction_time']) { return 0; - } elseif ($tx1['transaction_time'] < $tx2['transaction_time']) { + } + + if ($tx1['transaction_time'] < $tx2['transaction_time']) { return -1; } @@ -520,6 +525,7 @@ private function mapSingleRecurringOrderHistoryTransaction(array $rawTx): array if (PosInterface::PAYMENT_STATUS_PAYMENT_PENDING !== $transaction['order_status']) { $transaction['transaction_time'] = new \DateTimeImmutable($rawTx['txnDateTime']); } + if (PosInterface::PAYMENT_STATUS_PAYMENT_COMPLETED === $transaction['order_status']) { $transaction['batch_num'] = $rawTx['batchNumber']; $transaction['ref_ret_num'] = $rawTx['rrn']; diff --git a/src/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapper.php b/src/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapper.php index fe48cd7f..60460e0d 100644 --- a/src/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapper.php +++ b/src/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapper.php @@ -230,11 +230,7 @@ public function mapRefundResponse(array $rawResponseData): array $responseResults = $drawbackResult['Results']; if (self::TX_APPROVED !== $status && isset($responseResults['Result']) && [] !== $responseResults['Result']) { - if (isset($responseResults['Result'][0])) { - $responseResult = $responseResults['Result'][0]; - } else { - $responseResult = $responseResults['Result']; - } + $responseResult = $responseResults['Result'][0] ?? $responseResults['Result']; $result['proc_return_code'] = $procReturnCode; $result['error_code'] = $responseResult['ErrorCode'] ?? $procReturnCode; $result['error_message'] = $responseResult['ErrorMessage']; @@ -292,11 +288,7 @@ public function mapCancelResponse(array $rawResponseData): array $responseResults = $rawResponseData['SaleReversalResult']['Results']; if (self::TX_APPROVED !== $status && isset($responseResults['Result']) && [] !== $responseResults['Result']) { - if (isset($responseResults['Result'][0])) { - $responseResult = $responseResults['Result'][0]; - } else { - $responseResult = $responseResults['Result']; - } + $responseResult = $responseResults['Result'][0] ?? $responseResults['Result']; $result['proc_return_code'] = $procReturnCode; $result['error_code'] = $responseResult['ErrorCode'] ?? $procReturnCode; $result['error_message'] = $responseResult['ErrorMessage']; diff --git a/src/Gateways/AkbankPos.php b/src/Gateways/AkbankPos.php index 4e5c522b..b4cb318e 100644 --- a/src/Gateways/AkbankPos.php +++ b/src/Gateways/AkbankPos.php @@ -181,6 +181,7 @@ protected function send($contents, string $txType, string $paymentModel, string if (!\is_string($contents)) { throw new \InvalidArgumentException(\sprintf('Argument type must be string, %s provided.', \gettype($contents))); } + $hash = $this->requestDataMapper->getCrypt()->hashString($contents, $this->account->getStoreKey()); $response = $this->client->post($url, [ @@ -217,10 +218,6 @@ private function getRequestURIByTransactionType(string $txType): string PosInterface::TX_TYPE_HISTORY => 'portal/report/transaction', ]; - if (isset($arr[$txType])) { - return $arr[$txType]; - } - - return 'transaction/process'; + return $arr[$txType] ?? 'transaction/process'; } } diff --git a/src/Gateways/KuveytPos.php b/src/Gateways/KuveytPos.php index 08a93f59..fb0d1fb4 100644 --- a/src/Gateways/KuveytPos.php +++ b/src/Gateways/KuveytPos.php @@ -79,6 +79,7 @@ public function getApiURL(string $txType = null, string $paymentModel = null, ?s )) { return $this->getQueryAPIUrl(); } + if (null !== $txType && null !== $paymentModel) { return parent::getApiURL().'/'.$this->getRequestURIByTransactionType($txType, $paymentModel); } diff --git a/src/Serializer/AkbankPosSerializer.php b/src/Serializer/AkbankPosSerializer.php index 8ba9c202..f9d2cab3 100644 --- a/src/Serializer/AkbankPosSerializer.php +++ b/src/Serializer/AkbankPosSerializer.php @@ -43,6 +43,7 @@ public function decode(string $data, ?string $txType = null): array if ('' === $data) { return []; } + $decodedData = $this->serializer->decode($data, JsonEncoder::FORMAT); if (PosInterface::TX_TYPE_HISTORY === $txType && isset($decodedData['data'])) { @@ -66,15 +67,18 @@ private function decompress(string $data): string if (!$gzipStream) { return ''; } + $decompressedData = ''; $i = 0; while (!gzeof($gzipStream)) { - $i++; + ++$i; if ($i > 1000000) { throw new \RuntimeException('Invalid history data'); } + $decompressedData .= gzread($gzipStream, 1024); } + gzclose($gzipStream); return $decompressedData; diff --git a/tests/Functional/KuveytPosTest.php b/tests/Functional/KuveytPosTest.php index 215b70cf..7846545f 100644 --- a/tests/Functional/KuveytPosTest.php +++ b/tests/Functional/KuveytPosTest.php @@ -123,6 +123,7 @@ function (RequestDataPreparedEvent $requestDataPreparedEvent) use (&$eventIsThro ]; $requestData = $requestDataPreparedEvent->getRequestData(); $requestData = array_merge($requestData, $additionalRequestDataForKuveyt); + $requestDataPreparedEvent->setRequestData($requestData); $this->assertSame(PosInterface::TX_TYPE_PAY_AUTH, $requestDataPreparedEvent->getTxType()); }); @@ -228,7 +229,7 @@ function (RequestDataPreparedEvent $requestDataPreparedEvent) use (&$eventIsThro */ public function testRefundFail(array $lastResponse): array { - $refundOrder = $this->createRefundOrder(\get_class($this->pos), $lastResponse); + $refundOrder = $this->createRefundOrder(\get_class($this->pos), $lastResponse); $refundOrder['amount'] = 1.0; $eventIsThrown = false; diff --git a/tests/Functional/PaymentTestTrait.php b/tests/Functional/PaymentTestTrait.php index 12527e41..9c454f20 100644 --- a/tests/Functional/PaymentTestTrait.php +++ b/tests/Functional/PaymentTestTrait.php @@ -5,7 +5,6 @@ namespace Mews\Pos\Tests\Functional; -use Mews\Pos\Gateways\AkbankPos; use Mews\Pos\Gateways\EstPos; use Mews\Pos\Gateways\EstV3Pos; use Mews\Pos\Gateways\GarantiPos; @@ -23,7 +22,7 @@ private function createPaymentOrder( bool $tekrarlanan = false ): array { - if ($tekrarlanan && get_class($this->pos) === AkbankPos::class) { + if ($tekrarlanan && $this->pos instanceof \Mews\Pos\Gateways\AkbankPos) { // AkbankPos'ta recurring odemede orderTrackId/orderId en az 36 karakter olmasi gerekiyor $orderId = date('Ymd').strtoupper(substr(uniqid(sha1(time())), 0, 28)); } else { diff --git a/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php index 62f23375..717995b3 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php @@ -165,6 +165,7 @@ public function testCreateCancelRequestData(array $order, array $expected): void ksort($item); } } + foreach ($expected as &$item) { if (is_array($item)) { ksort($item); @@ -193,6 +194,7 @@ public function testCreateRefundRequestData(array $order, array $expected): void ksort($item); } } + foreach ($expected as &$item) { if (is_array($item)) { ksort($item); @@ -221,6 +223,7 @@ public function testCreateStatusRequestData(array $order, array $expected): void ksort($item); } } + foreach ($expected as &$item) { if (is_array($item)) { ksort($item); diff --git a/tests/Unit/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapperTest.php b/tests/Unit/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapperTest.php index 4883abb9..15d7e926 100644 --- a/tests/Unit/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapperTest.php +++ b/tests/Unit/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapperTest.php @@ -92,6 +92,7 @@ public function testMapPaymentResponse(string $txType, array $responseData, arra $this->assertIsArray($actualData['all']); $this->assertNotEmpty($actualData['all']); } + unset($actualData['all']); \ksort($expectedData); \ksort($actualData);