Skip to content

Commit

Permalink
Notification scope update
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jul 18, 2024
1 parent 0386022 commit 15da066
Showing 1 changed file with 24 additions and 65 deletions.
89 changes: 24 additions & 65 deletions Controller/Tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Service\TpayService;
Expand All @@ -35,9 +33,6 @@ class Notification implements CsrfAwareActionInterface
/** @var TpayTokensService */
private $tokensService;

/** @var StoreManagerInterface */
private $storeManager;

/** @var ResponseInterface */
private $response;

Expand All @@ -46,55 +41,27 @@ public function __construct(
TpayConfigInterface $tpayConfig,
TpayService $tpayService,
TpayTokensService $tokensService,
StoreManagerInterface $storeManager,
ResponseInterface $response
) {
$this->tpay = $tpayModel;
$this->tpayConfig = $tpayConfig;
$this->tpayService = $tpayService;
$this->tokensService = $tokensService;
$this->storeManager = $storeManager;
$this->response = $response;
Util::$loggingEnabled = false;
}

public function execute(): ?Response
{
if (isset($_POST['card'])) {
return $this->executeCardNotification();
}

return $this->executeNotification();
}

public function executeNotification(): ?Response
{
$response = null;

foreach ($this->storeManager->getStores() as $store) {
$response = $this->extractNotification($store);

if (Response::STATUS_CODE_200 === $response->getStatusCode()) {
break;
}
}

return $response;
}

public function executeCardNotification(): ?Response
{
$response = null;

foreach ($this->storeManager->getStores() as $store) {
$response = $this->extractCardNotification($store);
$orderId = base64_decode($_POST['tr_crc']);
$order = $this->tpayService->getOrderById($orderId);
$storeId = $order->getStoreId() ? (int)$order->getStoreId() : null;

if (Response::STATUS_CODE_200 === $response->getStatusCode()) {
break;
}
if (isset($_POST['card'])) {
return $this->extractCardNotification($storeId);
}

return $response;
return $this->extractNotification($storeId);
}

public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
Expand Down Expand Up @@ -160,10 +127,8 @@ private function saveOriginCard(array $notification, string $orderId)
}
}

private function extractNotification(StoreInterface $store): Response
private function extractNotification(?int $storeId = null): Response
{
$storeId = (int) $store->getStoreId();

try {
$notification = (new JWSVerifiedPaymentNotification(
$this->tpayConfig->getSecurityCode($storeId),
Expand All @@ -184,25 +149,14 @@ private function extractNotification(StoreInterface $store): Response

return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE');
} catch (Exception $e) {
Util::log(
'Notification exception',
sprintf(
'%s in file %s line: %d \n\n %s',
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
)
);
$this->handleException($e);

return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE');
}
}

private function extractCardNotification(StoreInterface $store): ?Response
private function extractCardNotification(?int $storeId = null): ?Response
{
$storeId = (int) $store->getStoreId();

try {
$notification = (new OriginJWSVerifiedPaymentNotification(
$this->tpayConfig->getSecurityCode($storeId),
Expand All @@ -216,18 +170,23 @@ private function extractCardNotification(StoreInterface $store): ?Response

return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE');
} catch (Exception $e) {
Util::log(
'Notification exception',
sprintf(
'%s in file %s line: %d \n\n %s',
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
)
);
$this->handleException($e);

return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE');
}
}

private function handleException(Exception $e)
{
Util::log(
'Notification exception',
sprintf(
'%s in file %s line: %d \n\n %s',
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
)
);
}
}

0 comments on commit 15da066

Please sign in to comment.