Skip to content

Commit

Permalink
PLUG-105: Emulate environment for correct store on webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
lighe committed May 13, 2024
1 parent c796c20 commit 9dca79e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
56 changes: 39 additions & 17 deletions Model/Webapi/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

namespace TrueLayer\Connect\Model\Webapi;

use Magento\Framework\App\Area;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Store\Model\App\Emulation;
use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository;
use TrueLayer\Connect\Api\Log\RepositoryInterface as LogRepository;
use TrueLayer\Connect\Api\Transaction\RepositoryInterface as TransactionRepository;
Expand Down Expand Up @@ -54,6 +56,14 @@ class Webhook implements WebhookInterface
* @var CartRepositoryInterface
*/
private $quoteRepository;
/**
* @var Emulation
*/
private $appEmulation;
/**
* @var int|null
*/
private $storeId = null;

/**
* Webhook constructor.
Expand All @@ -65,6 +75,7 @@ class Webhook implements WebhookInterface
* @param File $file
* @param TransactionRepository $transactionRepository
* @param CartRepositoryInterface $quoteRepository
* @param Emulation $appEmulation
*/
public function __construct(
LogRepository $logRepository,
Expand All @@ -73,7 +84,8 @@ public function __construct(
JsonSerializer $jsonSerializer,
File $file,
TransactionRepository $transactionRepository,
CartRepositoryInterface $quoteRepository
CartRepositoryInterface $quoteRepository,
Emulation $appEmulation
) {
$this->logRepository = $logRepository;
$this->processWebhook = $processWebhook;
Expand All @@ -82,6 +94,7 @@ public function __construct(
$this->file = $file;
$this->transactionRepository = $transactionRepository;
$this->quoteRepository = $quoteRepository;
$this->appEmulation = $appEmulation;
}

/**
Expand All @@ -98,10 +111,13 @@ public function processTransfer()
$this->logRepository->addDebugLog('Webhook', $event->getBody());
})->handler(function (TrueLayerWebhookInterface\PaymentSettledEventInterface $event) {
try {
$this->appEmulation->startEnvironmentEmulation($this->getStoreId(), Area::AREA_FRONTEND, true);
$this->processWebhook->execute($event->getBody()['payment_id'], $event->getBody()['user_id']);
} catch (\Exception $exception) {
$this->logRepository->addErrorLog('Webhook processTransfer', $exception->getMessage());
throw new LocalizedException(__($exception->getMessage()));
} finally {
$this->appEmulation->stopEnvironmentEmulation();
}
});
try {
Expand All @@ -116,24 +132,30 @@ public function processTransfer()
*/
private function getStoreId(): int
{
try {
$post = $this->file->fileGetContents('php://input');
$postArray = $this->jsonSerializer->unserialize($post);
if (!isset($postArray['payment_id']) || !$this->isValidUuid((string)$postArray['payment_id'])) {
return 0;
}
if ($this->storeId === null) {
try {
$post = $this->file->fileGetContents('php://input');
$postArray = $this->jsonSerializer->unserialize($post);
if (!isset($postArray['payment_id']) || !$this->isValidUuid((string)$postArray['payment_id'])) {
$this->storeId = 0;
return $this->storeId;
}

$transaction = $this->transactionRepository->getByUuid($postArray['payment_id']);
if (!$quoteId = $transaction->getQuoteId()) {
return 0;
}
$transaction = $this->transactionRepository->getByUuid($postArray['payment_id']);
if (!$quoteId = $transaction->getQuoteId()) {
$this->storeId = 0;
return $this->storeId;
}

$quote = $this->quoteRepository->get($quoteId);
return $quote->getStoreId();
} catch (\Exception $exception) {
$this->logRepository->addErrorLog('Webhook processTransfer postData', $exception->getMessage());
return 0;
$quote = $this->quoteRepository->get($quoteId);
$this->storeId = $quote->getStoreId();
} catch (\Exception $exception) {
$this->logRepository->addErrorLog('Webhook processTransfer postData', $exception->getMessage());
$this->storeId = 0;
return $this->storeId;
}
}
return $this->storeId;
}

/**
Expand All @@ -147,4 +169,4 @@ private function isValidUuid(string $paymentId): bool
$pattern = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i';
return preg_match($pattern, $paymentId) === 1;
}
}
}
1 change: 0 additions & 1 deletion Service/Order/ProcessWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ private function prepareQuote(CartInterface $quote, string $userId): CartInterfa

$quote->setCustomerIsGuest($quote->getCustomerId() == null);
$quote->setIsActive(true);
$quote->getShippingAddress()->setCollectShippingRates(false);
$this->quoteRepository->save($quote);
return $quote;
}
Expand Down

0 comments on commit 9dca79e

Please sign in to comment.