From 650aade4dc1c4e8f8ae3629b9377101f7ea2ec56 Mon Sep 17 00:00:00 2001 From: kGablo Date: Mon, 2 Sep 2024 15:15:32 +0200 Subject: [PATCH] TpayService refactor --- Service/TpayService.php | 121 ++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 60 deletions(-) diff --git a/Service/TpayService.php b/Service/TpayService.php index eb49c37..6afc64e 100644 --- a/Service/TpayService.php +++ b/Service/TpayService.php @@ -51,8 +51,7 @@ public function __construct( ManagerInterface $transactionManager, EventManagerInterface $eventManager, InvoiceService $invoiceService - ) - { + ) { $this->orderRepository = $orderRepository; $this->orderPaymentRepository = $orderPaymentRepository; $this->invoiceRepository = $invoiceRepository; @@ -109,55 +108,10 @@ public function saveOrderPayment(OrderPaymentInterface $payment): OrderPaymentIn return $this->orderPaymentRepository->save($payment); } - /** @return bool|string */ - protected function getTransactionDesc(array $validParams) - { - if (false === $validParams) { - return false; - } - - $error = $validParams['tr_error']; - $paid = $validParams['tr_paid']; - $status = $validParams['tr_status']; - $transactionDesc = '' . $validParams['tr_id'] . ' '; - $transactionDesc .= 'none' === $error ? ' ' : ' Error: ' . strtoupper($error) . ' (' . $paid . ')'; - - if ('CHARGEBACK' === $status) { - $transactionDesc .= __('Transaction has been refunded'); - } - - if (array_key_exists('test_mode', $validParams)) { - $transactionDesc .= ' TEST '; - } - - return $transactionDesc; - } - - protected function getCardTransactionDesc($validParams) - { - if (empty($validParams)) { - return false; - } - - if (isset($validParams['err_desc'])) { - return 'Payment error: ' . $validParams['err_desc'] . ', error code: ' . $validParams['err_code']; - } - - $error = null; - - if ('declined' === $validParams['status']) { - $error = $validParams['reason']; - } - - $transactionDesc = (is_null($error)) ? ' ' : ' Reason: ' . $error . ''; - $transactionDesc .= (isset($validParams['test_mode'])) && 1 === (int)$validParams['test_mode'] ? ' TEST ' : ' '; - - return $transactionDesc; - } - /** - * @return bool|OrderInterface * @throws Exception + * + * @return bool|OrderInterface */ public function setOrderStatus(string $orderId, array $validParams, TpayConfigInterface $tpayConfig) { @@ -201,16 +155,62 @@ public function setCardOrderStatus($orderId, array $validParams, TpayConfigInter return $order; } + /** @return bool|string */ + protected function getTransactionDesc(array $validParams) + { + if (false === $validParams) { + return false; + } + + $error = $validParams['tr_error']; + $paid = $validParams['tr_paid']; + $status = $validParams['tr_status']; + $transactionDesc = ''.$validParams['tr_id'].' '; + $transactionDesc .= 'none' === $error ? ' ' : ' Error: '.strtoupper($error).' ('.$paid.')'; + + if ('CHARGEBACK' === $status) { + $transactionDesc .= __('Transaction has been refunded'); + } + + if (array_key_exists('test_mode', $validParams)) { + $transactionDesc .= ' TEST '; + } + + return $transactionDesc; + } + + protected function getCardTransactionDesc($validParams) + { + if (empty($validParams)) { + return false; + } + + if (isset($validParams['err_desc'])) { + return 'Payment error: '.$validParams['err_desc'].', error code: '.$validParams['err_code']; + } + + $error = null; + + if ('declined' === $validParams['status']) { + $error = $validParams['reason']; + } + + $transactionDesc = (is_null($error)) ? ' ' : ' Reason: '.$error.''; + $transactionDesc .= (isset($validParams['test_mode'])) && 1 === (int) $validParams['test_mode'] ? ' TEST ' : ' '; + + return $transactionDesc; + } + /** * @param float|string $amount - * @param bool|int $skipFraudDetection + * @param bool|int $skipFraudDetection */ private function registerCaptureNotificationTpay(OrderPaymentInterface $payment, $amount, array $validParams, bool $skipFraudDetection = false) { $this->setTransactionIdForPayment($payment); $order = $payment->getOrder(); - $amount = (float)$amount; + $amount = (float) $amount; $invoice = $this->getInvoiceForTransactionId($order, $payment->getTransactionId()); if (!$invoice && $payment->isCaptureFinal($amount)) { @@ -311,7 +311,7 @@ private function registerCardCaptureNotificationTpay(OrderPaymentInterface $paym $this->setTransactionIdForPayment($payment); $order = $payment->getOrder(); - $amount = (float)$amount; + $amount = (float) $amount; $invoice = $this->getInvoiceForTransactionId($order, $payment->getTransactionId()); $orderCurrencyCode = $order->getOrderCurrency()->getCode(); @@ -321,7 +321,7 @@ private function registerCardCaptureNotificationTpay(OrderPaymentInterface $paym $orderCurrency = array_search($orderCurrencyCode, CurrencyCodesDictionary::CODES); - if (!$invoice && $payment->isCaptureFinal($amount) && ($orderCurrency === (int)$validParams['currency'] || $orderCurrencyCode === $validParams['currency'])) { + if (!$invoice && $payment->isCaptureFinal($amount) && ($orderCurrency === (int) $validParams['currency'] || $orderCurrencyCode === $validParams['currency'])) { $invoice = $this->createAndRegisterInvoice($order, $payment, Order::STATE_PROCESSING); } else { $this->handlePotentialFraud($payment, $amount, $skipFraudDetection); @@ -344,10 +344,10 @@ private function updateTransactionId(Order $order, array $validParams): Order private function determineOrderStatus(OrderInterface $order, array $validParams, bool &$emailNotify): ?string { - $orderAmount = (float)number_format((float)$order->getBaseGrandTotal(), 2, '.', ''); + $orderAmount = (float) number_format((float) $order->getBaseGrandTotal(), 2, '.', ''); $trStatus = $validParams['tr_status']; - if ('TRUE' === $trStatus && ((float)number_format($validParams['tr_paid'], 2, '.', '') === $orderAmount)) { + if ('TRUE' === $trStatus && ((float) number_format($validParams['tr_paid'], 2, '.', '') === $orderAmount)) { if (Order::STATE_PROCESSING !== $order->getState()) { $emailNotify = true; } @@ -355,7 +355,8 @@ private function determineOrderStatus(OrderInterface $order, array $validParams, $this->registerCaptureNotificationTpay($order->getPayment(), $order->getBaseGrandTotal(), $validParams); return Order::STATE_PROCESSING; - } elseif ('CHARGEBACK' === $trStatus) { + } + if ('CHARGEBACK' === $trStatus) { $order->addCommentToStatusHistory($this->getTransactionDesc($validParams)); $this->orderRepository->save($order); @@ -366,7 +367,7 @@ private function determineOrderStatus(OrderInterface $order, array $validParams, $emailNotify = true; } - $comment = __('The order has been holded: ') . '
' . $this->getTransactionDesc($validParams); + $comment = __('The order has been holded: ').'
'.$this->getTransactionDesc($validParams); $status = Order::STATE_HOLDED; $order->addStatusToHistory($status, $comment, true); @@ -376,14 +377,14 @@ private function determineOrderStatus(OrderInterface $order, array $validParams, private function commentCardOrder(OrderInterface &$order, array $validParams, string $orderId): bool { $transactionDesc = $this->getCardTransactionDesc($validParams); - $orderAmount = (float)number_format((float)$order->getBaseGrandTotal(), 2, '.', ''); + $orderAmount = (float) number_format((float) $order->getBaseGrandTotal(), 2, '.', ''); $emailNotify = false; $order = $this->updateTransactionId($order, $validParams); - $amountCheck = (float)number_format((float)$validParams['amount'], 2, '.', '') !== $orderAmount; + $amountCheck = (float) number_format((float) $validParams['amount'], 2, '.', '') !== $orderAmount; if (!isset($validParams['status']) || 'correct' !== $validParams['status'] || $amountCheck) { - $comment = __('Payment has been declined. ') . '
' . $transactionDesc; + $comment = __('Payment has been declined. ').'
'.$transactionDesc; $this->addCommentToHistory($orderId, $comment); } else { if (Order::STATE_PROCESSING != $order->getState()) {