Skip to content

Commit

Permalink
normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jan 5, 2024
1 parent 4341c50 commit 21d0158
Show file tree
Hide file tree
Showing 23 changed files with 233 additions and 370 deletions.
1 change: 0 additions & 1 deletion Api/TpayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function useSandboxMode(): bool;
/** Check if checkout amount is in range of installments payment channel */
public function getInstallmentsAmountValid(): bool;


// KARTY
public function getCardApiKey(): ?string;

Expand Down
40 changes: 13 additions & 27 deletions Controller/tpay/Create.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php
/**
*
* @category payment gateway
* @package Tpaycom_Magento2.3
* @author Tpay.com
* @copyright (https://tpay.com)
*/

namespace tpaycom\magento2basic\Controller\tpay;

Expand All @@ -18,10 +11,6 @@
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionOriginApi;
use tpaycom\magento2basic\Service\TpayService;

/**
* Class Create
* @package tpaycom\magento2basic\Controller\tpay
*/
class Create extends Action
{
/** @var TpayService */
Expand All @@ -36,11 +25,6 @@ class Create extends Action
/** @var TransactionApiFacade */
private $transaction;

/**
* {@inheritdoc}
* @param TpayInterface $tpayModel
* @param TpayService $tpayService
*/
public function __construct(Context $context, TpayInterface $tpayModel, TpayService $tpayService, Session $checkoutSession)
{
$this->tpay = $tpayModel;
Expand All @@ -51,7 +35,6 @@ public function __construct(Context $context, TpayInterface $tpayModel, TpayServ
parent::__construct($context);
}

/** {@inheritdoc} */
public function execute()
{
$orderId = $this->checkoutSession->getLastRealOrderId();
Expand All @@ -69,27 +52,29 @@ public function execute()

$this->handleOpenApiTrId($paymentData, $transaction);

$this->tpayService->addCommentToHistory($orderId, 'Transaction title ' . $transaction['title']);
$this->tpayService->addCommentToHistory($orderId, 'Transaction title '.$transaction['title']);
$transactionUrl = $transaction['url'];

if ($this->tpay->redirectToChannel() === true) {
if (true === $this->tpay->redirectToChannel()) {
$transactionUrl = str_replace('gtitle', 'title', $transactionUrl);
}

$this->tpayService->addCommentToHistory($orderId, 'Transaction link ' . $transactionUrl);
$this->tpayService->addCommentToHistory($orderId, 'Transaction link '.$transactionUrl);
$paymentData['additional_information']['transaction_url'] = $transactionUrl;
$payment->setData($paymentData)->save();

if (strlen($additionalPaymentInformation['blik_code']) === 6 && $this->tpay->checkBlikLevel0Settings()) {
if (6 === strlen($additionalPaymentInformation['blik_code']) && $this->tpay->checkBlikLevel0Settings()) {
if (true === $this->transaction->isOpenApiUse()) {
return $this->_redirect('magento2basic/tpay/success');
}
$result = $this->blikPay($transaction['title'], $additionalPaymentInformation['blik_code']);
$this->checkoutSession->unsQuoteId();

if (!$result) {
$this->tpayService->addCommentToHistory($orderId,
'User has typed wrong blik code and has been redirected to transaction panel in order to finish payment');
$this->tpayService->addCommentToHistory(
$orderId,
'User has typed wrong blik code and has been redirected to transaction panel in order to finish payment'
);

return $this->_redirect($transactionUrl);
}
Expand All @@ -103,25 +88,26 @@ public function execute()

/**
* Send BLIK code for transaction id
*
* @param string $blikTransactionId
* @param string $blikCode
*/
protected function blikPay($blikTransactionId, $blikCode): bool
{
$apiResult = $this->transaction->blik($blikTransactionId, $blikCode);

return isset($apiResult['result']) && $apiResult['result'] === 1;
return isset($apiResult['result']) && 1 === $apiResult['result'];
}

private function prepareTransaction($orderId, array $additionalPaymentInformation)
{
$data = $this->tpay->getTpayFormData($orderId);

if (strlen($additionalPaymentInformation['blik_code']) === 6) {
if (6 === strlen($additionalPaymentInformation['blik_code'])) {
$data['group'] = TransactionOriginApi::BLIK_CHANNEL;
$this->handleBlikData($data, $additionalPaymentInformation['blik_code']);
} else {
$data['group'] = (int)$additionalPaymentInformation['group'];
$data['group'] = (int) $additionalPaymentInformation['group'];

if ($this->tpay->redirectToChannel()) {
$data['direct'] = 1;
Expand All @@ -135,7 +121,7 @@ private function handleBlikData(array &$data, string $blikCode)
{
if ($this->transaction->isOpenApiUse() && $this->tpay->checkBlikLevel0Settings()) {
$data['blikPaymentData'] = [
'blikToken' => $blikCode
'blikToken' => $blikCode,
];
}
}
Expand Down
14 changes: 8 additions & 6 deletions Controller/tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use Magento\Framework\App\Response\Http;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Sales\Model\Order;
use Tpay\OriginApi\Utilities\Util;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Service\TpayService;
use Tpay\OriginApi\Utilities\Util;
use tpaycom\magento2basic\Service\TpayTokensService;
use tpaySDK\Webhook\JWSVerifiedPaymentNotification;

Expand All @@ -30,11 +30,11 @@ class Notification extends Action implements CsrfAwareActionInterface
/** @var TpayService */
protected $tpayService;

protected $request;

/** @var TpayTokensService */
private $tokensService;

protected $request;

public function __construct(Context $context, RemoteAddress $remoteAddress, TpayInterface $tpayModel, TpayService $tpayService, TpayTokensService $tokensService)
{
$this->tpay = $tpayModel;
Expand Down Expand Up @@ -90,8 +90,10 @@ public function validateForCsrf(RequestInterface $request): ?bool

/**
* Check if the order has been canceled and get response to Tpay server.
* @return string response for Tpay server
*
* @throws Exception
*
* @return string response for Tpay server
*/
protected function getPaidTransactionResponse(string $orderId): string
{
Expand All @@ -113,9 +115,9 @@ private function saveCard(array $notification, string $orderId)
$order = $this->tpayService->getOrderById($orderId);

if (isset($notification['card_token']) && !$this->tpay->isCustomerGuest($orderId)) {
$token = $this->tokensService->getWithoutAuthCustomerTokens((int)$order->getCustomerId(), $notification['tr_crc']);
$token = $this->tokensService->getWithoutAuthCustomerTokens((int) $order->getCustomerId(), $notification['tr_crc']);
if (!empty($token)) {
$this->tokensService->updateTokenById((int)$token['tokenId'], $notification['card_token']);
$this->tokensService->updateTokenById((int) $token['tokenId'], $notification['card_token']);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Controller/tpay/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function execute()
return $this->_redirect('magento2basic/tpay/CardPayment');
}

if ((!array_key_exists(TpayInterface::CHANNEL, $additionalPaymentInfo) || (int)$additionalPaymentInfo[TpayInterface::CHANNEL] < 1) && (!array_key_exists(TpayInterface::BLIK_CODE, $additionalPaymentInfo) || 6 !== strlen($additionalPaymentInfo[TpayInterface::BLIK_CODE]))) {
if ((!array_key_exists(TpayInterface::CHANNEL, $additionalPaymentInfo) || (int) $additionalPaymentInfo[TpayInterface::CHANNEL] < 1) && (!array_key_exists(TpayInterface::BLIK_CODE, $additionalPaymentInfo) || 6 !== strlen($additionalPaymentInfo[TpayInterface::BLIK_CODE]))) {
return $this->_redirect('checkout/cart');
}
$this->tpayService->setOrderStatePendingPayment($orderId, true);
Expand Down
11 changes: 0 additions & 11 deletions Model/ApiFacade/CardTransaction/CardApiFacade.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php
/**
*
* @category payment gateway
* @package Tpaycom_Magento2.3
* @author Tpay.com
* @copyright (https://tpay.com)
*/

namespace tpaycom\magento2basic\Model\ApiFacade\CardTransaction;

Expand All @@ -14,10 +7,6 @@
use tpaycom\magento2basic\Service\TpayService;
use tpaycom\magento2basic\Service\TpayTokensService;

/**
* Class CardApiFacade
* @package tpaycom\magento2basic\Model\ApiFacade\CardTransaction
*/
class CardApiFacade
{
/** @var CardOrigin */
Expand Down
33 changes: 11 additions & 22 deletions Model/ApiFacade/CardTransaction/CardOpen.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* @category payment gateway
* @package Tpaycom_Magento2.3
* @author Tpay.com
* @copyright (https://tpay.com)
*/

namespace tpaycom\magento2basic\Model\ApiFacade\CardTransaction;

Expand All @@ -14,10 +8,6 @@
use tpaycom\magento2basic\Service\TpayTokensService;
use tpaySDK\Api\TpayApi;

/**
* Class CardOpen
* @package tpaycom\magento2basic\Model\ApiFacade\CardTransaction
*/
class CardOpen
{
/** @var TpayInterface */
Expand Down Expand Up @@ -53,7 +43,7 @@ public function makeCardTransaction(string $orderId): string
$this->tpayPaymentConfig = $this->tpay->getTpayFormData($orderId);

if (isset($additionalPaymentInformation['card_id']) && false !== $additionalPaymentInformation['card_id'] && $this->tpay->getCardSaveEnabled()) {
$cardId = (int)$additionalPaymentInformation['card_id'];
$cardId = (int) $additionalPaymentInformation['card_id'];

return $this->processSavedCardPayment($orderId, $cardId);
}
Expand Down Expand Up @@ -87,9 +77,9 @@ private function processSavedCardPayment(string $orderId, int $cardId): string
}

if (isset($paymentResult['status']) && 'declined' === $paymentResult['status']) {
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, Elavon rejection code: ' . $paymentResult['reason']);
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, Elavon rejection code: '.$paymentResult['reason']);
} else {
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, error: ' . $paymentResult['err_desc']);
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, error: '.$paymentResult['err_desc']);
}
} catch (Exception $e) {
return 'magento2basic/tpay/error';
Expand All @@ -111,7 +101,7 @@ private function addToPaymentData(string $orderId, string $key, $value)

private function processNewCardPayment(string $orderId, array $additionalPaymentInformation): string
{
$saveCard = isset($additionalPaymentInformation['card_save']) && $this->tpay->getCardSaveEnabled() ? (bool)$additionalPaymentInformation['card_save'] : false;
$saveCard = isset($additionalPaymentInformation['card_save']) && $this->tpay->getCardSaveEnabled() ? (bool) $additionalPaymentInformation['card_save'] : false;
try {
$transaction = $this->tpayApi->Transactions->createTransaction($this->handleDataStructure());
$request = [
Expand All @@ -124,14 +114,13 @@ private function processNewCardPayment(string $orderId, array $additionalPayment
];
$result = $this->tpayApi->Transactions->createPaymentByTransactionId($request, $transaction['transactionId']);
$this->tpayService->setCardOrderStatus($orderId, $this->handleValidParams($result), $this->tpay);

} catch (Exception $e) {
return 'magento2basic/tpay/error';
}

if (isset($result['transactionPaymentUrl']) && $result['payments']['status'] === 'pending') {
if (isset($result['transactionPaymentUrl']) && 'pending' === $result['payments']['status']) {
$url3ds = $result['transactionPaymentUrl'];
$this->tpayService->addCommentToHistory($orderId, '3DS Transaction link ' . $url3ds);
$this->tpayService->addCommentToHistory($orderId, '3DS Transaction link '.$url3ds);
$this->addToPaymentData($orderId, 'transaction_url', $url3ds);
$this->saveCard($orderId, $saveCard, $additionalPaymentInformation);

Expand All @@ -157,7 +146,7 @@ private function saveCard(string $orderId, bool $saveCard, array $additionalPaym
private function handleDataStructure(): array
{
return [
'amount' => (float)$this->tpayPaymentConfig['amount'],
'amount' => (float) $this->tpayPaymentConfig['amount'],
'description' => $this->tpayPaymentConfig['description'],
'hiddenDescription' => $this->tpayPaymentConfig['crc'],
'payer' => [
Expand All @@ -175,13 +164,13 @@ private function handleDataStructure(): array
],
'callbacks' => [
'notification' => [
"url" => $this->tpayPaymentConfig['result_url'],
'url' => $this->tpayPaymentConfig['result_url'],
],
'payerUrls' => [
"success" => $this->tpayPaymentConfig['return_url'],
"error" => $this->tpayPaymentConfig['return_error_url']
'success' => $this->tpayPaymentConfig['return_url'],
'error' => $this->tpayPaymentConfig['return_error_url'],
],
]
],
];
}

Expand Down
Loading

0 comments on commit 21d0158

Please sign in to comment.