Skip to content

Commit

Permalink
Merge pull request #169 from bold-commerce/Q1-609
Browse files Browse the repository at this point in the history
Fix update payment store id for shop id validation.
  • Loading branch information
victorpetryk authored Oct 30, 2023
2 parents 70a41d2 + f961ba0 commit adf41fe
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Model/Order/UpdatePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function update(string $shopId, RequestInterface $payment): ResultInterfa
try {
$this->validateRequest($payment);
$order = $payment->getPayment()->getOrder();
$websiteId = (int)$order->getStore()->getWebsiteId();
$this->shopIdValidator->validate($shopId, $websiteId);
$storeId = (int)$order->getStoreId();
$this->shopIdValidator->validate($shopId, $storeId);
} catch (LocalizedException $e) {
return $this->getValidationErrorResponse($e->getMessage());
}
Expand Down
17 changes: 15 additions & 2 deletions Model/Quote/LoadAndValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Bold\Checkout\Model\Quote;

use Bold\Checkout\Model\Http\Client\Request\Validator\ShopIdValidator;
use Bold\Checkout\Model\Quote\LoadAndValidate\StoreIdResolver;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -60,6 +61,11 @@ class LoadAndValidate
*/
private $cartExtensionFactory;

/**
* @var StoreIdResolver $cartStoreIdResolver
*/
private $cartStoreIdResolver;

/**
* @param ShopIdValidator $shopIdValidator
* @param Cart $cart
Expand All @@ -69,6 +75,7 @@ class LoadAndValidate
* @param QuoteFactory $quoteFactory
* @param ShippingAssignmentProcessor $shippingAssignmentProcessor
* @param CartExtensionFactory $cartExtensionFactory
* @param StoreIdResolver $cartStoreIdResolver
*/
public function __construct(
ShopIdValidator $shopIdValidator,
Expand All @@ -78,7 +85,8 @@ public function __construct(
QuoteResource $quoteResource,
QuoteFactory $quoteFactory,
ShippingAssignmentProcessor $shippingAssignmentProcessor,
CartExtensionFactory $cartExtensionFactory
CartExtensionFactory $cartExtensionFactory,
StoreIdResolver $cartStoreIdResolver
) {
$this->shopIdValidator = $shopIdValidator;
$this->cart = $cart;
Expand All @@ -88,6 +96,7 @@ public function __construct(
$this->quoteFactory = $quoteFactory;
$this->shippingAssignmentProcessor = $shippingAssignmentProcessor;
$this->cartExtensionFactory = $cartExtensionFactory;
$this->cartStoreIdResolver = $cartStoreIdResolver;
}

/**
Expand All @@ -102,8 +111,12 @@ public function __construct(
public function load(string $shopId, int $cartId): CartInterface
{
$quote = $this->quoteFactory->create();
$storeId = $this->cartStoreIdResolver->resolve($cartId);
if ($storeId) {
$this->storeManager->setCurrentStore($storeId);
$quote->setStoreId($storeId);
}
$this->quoteResource->load($quote, $cartId);
$this->storeManager->setCurrentStore($quote->getStoreId());
$this->storeManager->getStore()->setCurrentCurrencyCode($quote->getQuoteCurrencyCode());
$this->checkoutSession->replaceQuote($quote);
$this->cart->setQuote($quote);
Expand Down
36 changes: 36 additions & 0 deletions Model/Quote/LoadAndValidate/StoreIdResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace Bold\Checkout\Model\Quote\LoadAndValidate;

use Bold\Checkout\Model\ResourceModel\Quote\GetStoreIdByCartId;

/**
* Resolve store id by cart id.
*/
class StoreIdResolver
{
/**
* @var GetStoreIdByCartId
*/
private $getStoreIdByCartId;

/**
* @param GetStoreIdByCartId $getStoreIdByCartId
*/
public function __construct(GetStoreIdByCartId $getStoreIdByCartId)
{
$this->getStoreIdByCartId = $getStoreIdByCartId;
}

/**
* Get store id by cart id.
*
* @param int $cartId
* @return int
*/
public function resolve(int $cartId): int
{
return $this->getStoreIdByCartId->getStoreId($cartId);
}
}
40 changes: 40 additions & 0 deletions Model/ResourceModel/Quote/GetStoreIdByCartId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

namespace Bold\Checkout\Model\ResourceModel\Quote;

use Magento\Framework\App\ResourceConnection;

/**
* Get store id by cart id resource model.
*/
class GetStoreIdByCartId
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @param ResourceConnection $resourceConnection
*/
public function __construct(ResourceConnection $resourceConnection)
{
$this->resourceConnection = $resourceConnection;
}

/**
* Retrieve store id by cart id.
*
* @param int $cartId
* @return int
*/
public function getStoreId(int $cartId): int
{
$select = $this->resourceConnection->getConnection()->select()->from(
$this->resourceConnection->getTableName('quote'),
['store_id']
)->where('entity_id = (?)', $cartId);
return (int)$this->resourceConnection->getConnection()->fetchOne($select);
}
}

0 comments on commit adf41fe

Please sign in to comment.