Skip to content

Commit

Permalink
Multistore notification fix
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jan 19, 2024
1 parent e965f34 commit f5d6a40
Showing 1 changed file with 52 additions and 21 deletions.
73 changes: 52 additions & 21 deletions Controller/tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Magento\Framework\App\Response\Http;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Sales\Model\Order;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\OriginApi\Utilities\Util;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Service\TpayService;
Expand All @@ -35,39 +37,24 @@ class Notification extends Action implements CsrfAwareActionInterface
/** @var TpayTokensService */
private $tokensService;

public function __construct(Context $context, RemoteAddress $remoteAddress, TpayInterface $tpayModel, TpayService $tpayService, TpayTokensService $tokensService)
/** @var StoreManagerInterface */
private $storeManager;

public function __construct(Context $context, RemoteAddress $remoteAddress, TpayInterface $tpayModel, TpayService $tpayService, TpayTokensService $tokensService, StoreManagerInterface $storeManager)
{
$this->tpay = $tpayModel;
$this->remoteAddress = $remoteAddress;
$this->tpayService = $tpayService;
$this->tokensService = $tokensService;
$this->storeManager = $storeManager;
Util::$loggingEnabled = false;

parent::__construct($context);
}

public function execute()
{
try {
$notification = (new JWSVerifiedPaymentNotification($this->tpay->getSecurityCode(), !$this->tpay->useSandboxMode()))->getNotification();
$notification = $notification->getNotificationAssociative();
$orderId = base64_decode($notification['tr_crc']);

if ('PAID' === $notification['tr_status']) {
$response = $this->getPaidTransactionResponse($orderId);

return $this->getResponse()->setStatusCode(Http::STATUS_CODE_200)->setContent($response);
}

$this->saveCard($notification, $orderId);
$this->tpayService->SetOrderStatus($orderId, $notification, $this->tpay);

return $this->getResponse()->setStatusCode(Http::STATUS_CODE_200)->setContent('TRUE');
} catch (Exception $e) {
Util::log('Notification exception', "{$e->getMessage()} in file {$e->getFile()} line: {$e->getLine()} \n\n {$e->getTraceAsString()}");

return $this->getResponse()->setStatusCode(Http::STATUS_CODE_500)->setContent('FALSE');
}
return $this->getNotification();
}

/**
Expand Down Expand Up @@ -121,4 +108,48 @@ private function saveCard(array $notification, string $orderId)
}
}
}

public function getNotification()
{
$returnData = null;
foreach ($this->storeManager->getStores() as $store) {
[$returnData, $isPassed] = $this->extractNotification($store);
if ($isPassed) {
break;
}
}

return $returnData;
}

public function extractNotification(StoreInterface $store): array
{
$storeId = (int) $store->getStoreId();
try {
$notification = (new JWSVerifiedPaymentNotification($this->tpay->getSecurityCode($storeId), !$this->tpay->useSandboxMode($storeId)))->getNotification();
$notification = $notification->getNotificationAssociative();
$orderId = base64_decode($notification['tr_crc']);

if ('PAID' === $notification['tr_status']) {
$response = $this->getPaidTransactionResponse($orderId);

$returnData = $this->getResponse()->setStatusCode(Http::STATUS_CODE_200)->setContent($response);

return [$returnData, true];
}

$this->saveCard($notification, $orderId);
$this->tpayService->SetOrderStatus($orderId, $notification, $this->tpay);

$returnData = $this->getResponse()->setStatusCode(Http::STATUS_CODE_200)->setContent('TRUE');
$isPassed = true;
} catch (Exception $e) {
Util::log('Notification exception', "{$e->getMessage()} in file {$e->getFile()} line: {$e->getLine()} \n\n {$e->getTraceAsString()}");

$returnData = $this->getResponse()->setStatusCode(Http::STATUS_CODE_500)->setContent('FALSE');
$isPassed = false;
}

return [$returnData, $isPassed];
}
}

0 comments on commit f5d6a40

Please sign in to comment.