diff --git a/Gateway/Helper/Request/Action.php b/Gateway/Helper/Request/Action.php index bffb67ac..b673c366 100644 --- a/Gateway/Helper/Request/Action.php +++ b/Gateway/Helper/Request/Action.php @@ -52,8 +52,10 @@ class Action * @param string $action * @param ConfigInterface $config */ - public function __construct($action, ConfigInterface $config) - { + public function __construct( + $action, + ConfigInterface $config + ) { $this->action = $action; $this->config = $config; } @@ -64,9 +66,9 @@ public function __construct($action, ConfigInterface $config) * @param string $additionalPath * @return string */ - public function getUrl($additionalPath = '') + public function getUrl($additionalPath = '', $storeId = null) { - $gateway = $this->config->getValue('mode') == 'sandbox' + $gateway = $this->config->getValue('mode', $storeId) == 'sandbox' ? \Astound\Affirm\Model\Config::API_URL_SANDBOX : \Astound\Affirm\Model\Config::API_URL_PRODUCTION; diff --git a/Gateway/Http/AbstractTransferFactory.php b/Gateway/Http/AbstractTransferFactory.php index 3dd320de..a241feb8 100644 --- a/Gateway/Http/AbstractTransferFactory.php +++ b/Gateway/Http/AbstractTransferFactory.php @@ -22,6 +22,7 @@ use Magento\Payment\Gateway\ConfigInterface; use Magento\Payment\Gateway\Http\TransferBuilder; use Magento\Payment\Gateway\Http\TransferFactoryInterface; +use Magento\Store\Model\StoreManagerInterface; /** * Class AbstractTransferFactory @@ -49,6 +50,13 @@ abstract class AbstractTransferFactory implements TransferFactoryInterface */ protected $action; + /** + * Store manager + * + * @var \Magento\Store\App\Model\StoreManagerInterface + */ + protected $_storeManager; + /** * Construct * @@ -59,11 +67,13 @@ abstract class AbstractTransferFactory implements TransferFactoryInterface public function __construct( ConfigInterface $config, TransferBuilder $transferBuilder, - Action $action + Action $action, + StoreManagerInterface $storeManager ) { $this->config = $config; $this->transferBuilder = $transferBuilder; $this->action = $action; + $this->_storeManager = $storeManager; } /** @@ -75,7 +85,7 @@ public function __construct( protected function getPublicApiKey($storeId) { if(!empty($storeId)){ - return $this->config->getValue('mode') == 'sandbox' + return $this->config->getValue('mode', $storeId) == 'sandbox' ? $this->config->getValue('public_api_key_sandbox', $storeId) : $this->config->getValue('public_api_key_production', $storeId); } else { @@ -95,7 +105,7 @@ protected function getPublicApiKey($storeId) protected function getPrivateApiKey($storeId) { if(!empty($storeId)){ - return $this->config->getValue('mode') == 'sandbox' + return $this->config->getValue('mode', $storeId) == 'sandbox' ? $this->config->getValue('private_api_key_sandbox', $storeId) : $this->config->getValue('private_api_key_production', $storeId); } else { @@ -104,4 +114,14 @@ protected function getPrivateApiKey($storeId) : $this->config->getValue('private_api_key_production'); } } + + /** + * Get store id + * + * @return string + */ + protected function getStoreId() + { + return $this->_storeManager->getStore()->getId(); + } } diff --git a/Gateway/Http/TransferFactory.php b/Gateway/Http/TransferFactory.php index f290f96c..db345e19 100644 --- a/Gateway/Http/TransferFactory.php +++ b/Gateway/Http/TransferFactory.php @@ -35,14 +35,15 @@ class TransferFactory extends AbstractTransferFactory public function create(array $request) { $method = isset($request['method']) ? $request['method'] : ClientService::POST; - $storeId = isset($request['storeId']) ? $request['storeId'] : ''; + // Admin actions will include store id in the request + $storeId = isset($request['storeId']) ? $request['storeId'] : $this->getStoreId(); return $this->transferBuilder ->setMethod($method) ->setHeaders(['Content-Type' => 'application/json']) ->setBody($request['body']) ->setAuthUsername($this->getPublicApiKey($storeId)) ->setAuthPassword($this->getPrivateApiKey($storeId)) - ->setUri($this->getApiUrl($request['path'])) + ->setUri($this->getApiUrl($request['path'], $storeId)) ->build(); } @@ -50,10 +51,11 @@ public function create(array $request) * Get Api url * * @param string $additionalPath + * @param string $storeId * @return string */ - protected function getApiUrl($additionalPath) + protected function getApiUrl($additionalPath, $storeId) { - return $this->action->getUrl($additionalPath); + return $this->action->getUrl($additionalPath, $storeId); } } diff --git a/Gateway/Request/AbstractDataBuilder.php b/Gateway/Request/AbstractDataBuilder.php index 19d48e25..da8b9343 100644 --- a/Gateway/Request/AbstractDataBuilder.php +++ b/Gateway/Request/AbstractDataBuilder.php @@ -20,6 +20,7 @@ use Magento\Payment\Gateway\ConfigInterface; use Magento\Payment\Gateway\Request\BuilderInterface; +use Magento\Store\Model\StoreManagerInterface; /** * Class AbstractDataBuilder @@ -41,15 +42,24 @@ abstract class AbstractDataBuilder implements BuilderInterface */ private $config; + /** + * Store manager + * + * @var \Magento\Store\App\Model\StoreManagerInterface + */ + protected $_storeManager; + /** * Constructor * * @param ConfigInterface $config */ public function __construct( - ConfigInterface $config + ConfigInterface $config, + StoreManagerInterface $storeManager ) { $this->config = $config; + $this->_storeManager = $storeManager; } /** diff --git a/Gateway/Request/CaptureRequest.php b/Gateway/Request/CaptureRequest.php index fc64fc44..2990a187 100644 --- a/Gateway/Request/CaptureRequest.php +++ b/Gateway/Request/CaptureRequest.php @@ -46,9 +46,7 @@ public function build(array $buildSubject) $transactionId = $payment->getAdditionalInformation(self::TRANSACTION_ID) ?: $payment->getAdditionalInformation(self::CHARGE_ID); $order = $payment->getOrder(); - if($order) { - $storeId = $order->getStoreId(); - } + $storeId = isset($order) ? $order->getStoreId() : $this->_storeManager->getStore()->getId(); if (!$storeId) { $storeId = null; } diff --git a/Helper/Pixel.php b/Helper/Pixel.php index 06832606..5fd6f145 100644 --- a/Helper/Pixel.php +++ b/Helper/Pixel.php @@ -36,6 +36,7 @@ namespace Astound\Affirm\Helper; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Store\Model\StoreManagerInterface; use Astound\Affirm\Model\Config as Config; /** @@ -52,6 +53,13 @@ class Pixel */ protected $scopeConfig; + /** + * Store manager + * + * @var \Magento\Store\App\Model\StoreManagerInterface + */ + protected $_storeManager; + /** * Init * @@ -60,9 +68,11 @@ class Pixel */ public function __construct( ScopeConfigInterface $scopeConfig, + StoreManagerInterface $storeManager, Config $configAffirm ) { $this->scopeConfig = $scopeConfig; + $this->_storeManager = $storeManager; $this->affirmPaymentConfig = $configAffirm; } @@ -121,4 +131,14 @@ public function getConfig($configPath, $scope = 'default') $scope ); } + + /** + * Get store id + * + * @return int + */ + protected function getStoreId() + { + return $this->_storeManager->getStore()->getId(); + } } diff --git a/Model/Adminhtml/Observer/AfterShipmentSaveObserver.php b/Model/Adminhtml/Observer/AfterShipmentSaveObserver.php index e267f8a6..89a3bd37 100644 --- a/Model/Adminhtml/Observer/AfterShipmentSaveObserver.php +++ b/Model/Adminhtml/Observer/AfterShipmentSaveObserver.php @@ -24,6 +24,8 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Event\Observer; use Astound\Affirm\Model\Ui\ConfigProvider; +use Magento\Store\Model\StoreManagerInterface; +use Magento\Store\Model\ScopeInterface; use Astound\Affirm\Logger\Logger; /** @@ -60,6 +62,13 @@ class AfterShipmentSaveObserver implements ObserverInterface */ protected $scopeConfig; + /** + * Store manager + * + * @var \Magento\Store\App\Model\StoreManagerInterface + */ + protected $_storeManager; + /** * Affirm logging instance * @@ -79,11 +88,13 @@ public function __construct( OrderRepositoryInterface $orderRepository, ZendClientFactory $httpClientFactory, ScopeConfigInterface $scopeConfig, + StoreManagerInterface $storeManager, Logger $logger ) { $this->orderRepository = $orderRepository; $this->httpClientFactory = $httpClientFactory; $this->scopeConfig = $scopeConfig; + $this->_storeManager = $storeManager; $this->logger = $logger; } @@ -156,9 +167,9 @@ protected function isAffirmPaymentMethod($order) */ protected function getPrivateApiKey() { - return $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox' - ? $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_sandbox') - : $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_production'); + return $this->getIsSandboxMode() + ? $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_sandbox', ScopeInterface::SCOPE_STORE, $this->getStoreId()) + : $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_production', ScopeInterface::SCOPE_STORE, $this->getStoreId()); } /** @@ -168,9 +179,9 @@ protected function getPrivateApiKey() */ protected function getPublicApiKey() { - return $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox' - ? $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_sandbox') - : $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_production'); + return $this->getIsSandboxMode() + ? $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_sandbox', ScopeInterface::SCOPE_STORE, $this->getStoreId()) + : $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_production', ScopeInterface::SCOPE_STORE, $this->getStoreId()); } /** @@ -181,10 +192,30 @@ protected function getPublicApiKey() */ protected function getApiUrl($additionalPath) { - $gateway = $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox' + $gateway = $this->getIsSandboxMode() ? \Astound\Affirm\Model\Config::API_URL_SANDBOX : \Astound\Affirm\Model\Config::API_URL_PRODUCTION; return trim($gateway, '/') . sprintf('%s%s', self::API_TRANSACTIONS_PATH, $additionalPath); } + + /** + * Get is sandbox mode + * + * @return boolean + */ + protected function getIsSandboxMode() + { + return $this->scopeConfig->getValue('payment/affirm_gateway/mode', ScopeInterface::SCOPE_STORE, $this->getStoreId()) == 'sandbox'; + } + + /** + * Get store id + * + * @return int + */ + protected function getStoreId() + { + return $this->_storeManager->getStore()->getId(); + } } diff --git a/Model/Config.php b/Model/Config.php index 6f3763d5..b362ce66 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -250,13 +250,25 @@ public function setStoreId($storeId) } /** - * Get payment method mode + * Get current store id + * + * @return int + */ + protected function getCurrentStoreId() + { + return $this->storeManager->getStore()->getId(); + } + + + /** + * Get payment method environment mode * * @return mixed */ public function getMode() { - return $this->getValue('mode'); + $storeId = $this->getCurrentStoreId(); + return $this->getValue(self::KEY_MODE, $storeId); } /** @@ -266,9 +278,10 @@ public function getMode() */ public function getPrivateApiKey() { - return ($this->getValue('mode') == 'sandbox') ? - $this->getValue(self::KEY_PRIVATE_KEY_SANDBOX) : - $this->getValue(self::KEY_PRIVATE_KEY_PRODUCTION); + $storeId = $this->getCurrentStoreId(); + return ($this->getMode() == 'sandbox') ? + $this->getValue(self::KEY_PRIVATE_KEY_SANDBOX, $storeId) : + $this->getValue(self::KEY_PRIVATE_KEY_PRODUCTION, $storeId); } /** @@ -278,9 +291,10 @@ public function getPrivateApiKey() */ public function getPublicApiKey() { - return ($this->getValue('mode') == 'sandbox') ? - $this->getValue(self::KEY_PUBLIC_KEY_SANDBOX) : - $this->getValue(self::KEY_PUBLIC_KEY_PRODUCTION); + $storeId = $this->getCurrentStoreId(); + return ($this->getMode() == 'sandbox') ? + $this->getValue(self::KEY_PUBLIC_KEY_SANDBOX, $storeId) : + $this->getValue(self::KEY_PUBLIC_KEY_PRODUCTION, $storeId); } /** @@ -305,7 +319,7 @@ public function getScript() $apiUrl = $this->getApiUrl(); $prefix = "cdn1"; if ($apiUrl) { - if ($this->getValue('mode') == 'sandbox') { + if ($this->getMode() == 'sandbox') { $pattern = '~(http|https)://~'; $replacement = '-'; } else { @@ -524,10 +538,12 @@ public function getValue($key, $storeId = null) { $underscored = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $key)); $path = $this->_getSpecificConfigPath($underscored); + $storeScope = !empty($storeId) ? ScopeInterface::SCOPE_STORE : ScopeInterface::SCOPE_WEBSITE; if ($path !== null) { $value = $this->scopeConfig->getValue( $path, - ScopeInterface::SCOPE_WEBSITE + $storeScope, + $storeId ); return $value; } diff --git a/Model/Plugin/Order/AddressSave/Edit.php b/Model/Plugin/Order/AddressSave/Edit.php index 84d84921..1810d3f1 100644 --- a/Model/Plugin/Order/AddressSave/Edit.php +++ b/Model/Plugin/Order/AddressSave/Edit.php @@ -23,6 +23,8 @@ use Astound\Affirm\Model\Ui\ConfigProvider; use Magento\Framework\HTTP\ZendClientFactory; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Store\Model\StoreManagerInterface; +use Magento\Store\Model\ScopeInterface; use Astound\Affirm\Logger\Logger; /** @@ -62,6 +64,13 @@ class Edit */ protected $scopeConfig; + /** + * Store manager + * + * @var \Magento\Store\App\Model\StoreManagerInterface + */ + protected $_storeManager; + /** * Affirm logging instance * @@ -73,11 +82,13 @@ public function __construct( CollectionFactory $collectionFactory, ZendClientFactory $httpClientFactory, ScopeConfigInterface $scopeConfig, + StoreManagerInterface $storeManager, Logger $logger ) { $this->_collectionFactory = $collectionFactory; $this->httpClientFactory = $httpClientFactory; $this->scopeConfig = $scopeConfig; + $this->_storeManager = $storeManager; $this->logger = $logger; } @@ -147,7 +158,7 @@ public function afterExecute($controller , $method) protected function getApiUrl($additionalPath) { - $gateway = $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox' + $gateway = $this->getIsSandboxMode() ? \Astound\Affirm\Model\Config::API_URL_SANDBOX : \Astound\Affirm\Model\Config::API_URL_PRODUCTION; @@ -161,9 +172,9 @@ protected function isAffirmPaymentMethod($order) protected function getPrivateApiKey() { - return $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox' - ? $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_sandbox') - : $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_production'); + return $this->getIsSandboxMode() + ? $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_sandbox', ScopeInterface::SCOPE_STORE, $this->getStoreId()) + : $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_production', ScopeInterface::SCOPE_STORE, $this->getStoreId()); } /** @@ -173,8 +184,28 @@ protected function getPrivateApiKey() */ protected function getPublicApiKey() { - return $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox' - ? $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_sandbox') - : $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_production'); + return $this->getIsSandboxMode() + ? $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_sandbox', ScopeInterface::SCOPE_STORE, $this->getStoreId()) + : $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_production', ScopeInterface::SCOPE_STORE, $this->getStoreId()); + } + + /** + * Get is sandbox mode + * + * @return boolean + */ + protected function getIsSandboxMode() + { + return $this->scopeConfig->getValue('payment/affirm_gateway/mode', ScopeInterface::SCOPE_STORE, $this->getStoreId()) == 'sandbox'; + } + + /** + * Get store id + * + * @return string + */ + protected function getStoreId() + { + return $this->_storeManager->getStore()->getId(); } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index d3a2ffe1..40287162 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -44,31 +44,31 @@ Magento\Config\Model\Config\Source\Yesno - + Astound\Affirm\Model\Adminhtml\Source\ModeAction - + - sandbox + sandbox - + payment/affirm_gateway/private_api_key_sandbox Magento\Config\Model\Config\Backend\Encrypted 1 - sandbox + sandbox - + payment/affirm_gateway/public_api_key_production - production + production - + Magento\Config\Model\Config\Backend\Encrypted 1 - production + production