Skip to content

Commit

Permalink
TpayService refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Sep 2, 2024
1 parent 1cf4fc4 commit 650aade
Showing 1 changed file with 61 additions and 60 deletions.
121 changes: 61 additions & 60 deletions Service/TpayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public function __construct(
ManagerInterface $transactionManager,
EventManagerInterface $eventManager,
InvoiceService $invoiceService
)
{
) {
$this->orderRepository = $orderRepository;
$this->orderPaymentRepository = $orderPaymentRepository;
$this->invoiceRepository = $invoiceRepository;
Expand Down Expand Up @@ -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 = '<b>' . $validParams['tr_id'] . '</b> ';
$transactionDesc .= 'none' === $error ? ' ' : ' Error: <b>' . strtoupper($error) . '</b> (' . $paid . ')';

if ('CHARGEBACK' === $status) {
$transactionDesc .= __('Transaction has been refunded');
}

if (array_key_exists('test_mode', $validParams)) {
$transactionDesc .= '<b> TEST </b>';
}

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: <b>' . $error . '</b>';
$transactionDesc .= (isset($validParams['test_mode'])) && 1 === (int)$validParams['test_mode'] ? '<b> TEST </b>' : ' ';

return $transactionDesc;
}

/**
* @return bool|OrderInterface
* @throws Exception
*
* @return bool|OrderInterface
*/
public function setOrderStatus(string $orderId, array $validParams, TpayConfigInterface $tpayConfig)
{
Expand Down Expand Up @@ -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 = '<b>'.$validParams['tr_id'].'</b> ';
$transactionDesc .= 'none' === $error ? ' ' : ' Error: <b>'.strtoupper($error).'</b> ('.$paid.')';

if ('CHARGEBACK' === $status) {
$transactionDesc .= __('Transaction has been refunded');
}

if (array_key_exists('test_mode', $validParams)) {
$transactionDesc .= '<b> TEST </b>';
}

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: <b>'.$error.'</b>';
$transactionDesc .= (isset($validParams['test_mode'])) && 1 === (int) $validParams['test_mode'] ? '<b> TEST </b>' : ' ';

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)) {
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -344,18 +344,19 @@ 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;
}

$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);

Expand All @@ -366,7 +367,7 @@ private function determineOrderStatus(OrderInterface $order, array $validParams,
$emailNotify = true;
}

$comment = __('The order has been holded: ') . '</br>' . $this->getTransactionDesc($validParams);
$comment = __('The order has been holded: ').'</br>'.$this->getTransactionDesc($validParams);
$status = Order::STATE_HOLDED;
$order->addStatusToHistory($status, $comment, true);

Expand All @@ -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. ') . '</br>' . $transactionDesc;
$comment = __('Payment has been declined. ').'</br>'.$transactionDesc;
$this->addCommentToHistory($orderId, $comment);
} else {
if (Order::STATE_PROCESSING != $order->getState()) {
Expand Down

0 comments on commit 650aade

Please sign in to comment.