Skip to content

Commit

Permalink
Merge pull request #69 from Affirm/store-level-api-keys
Browse files Browse the repository at this point in the history
Allows Affirm API credentials to be configured and used on the store level
  • Loading branch information
taehyunlim authored Oct 29, 2021
2 parents 5fd74f8 + 7c61472 commit da30295
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 48 deletions.
10 changes: 6 additions & 4 deletions Gateway/Helper/Request/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;

Expand Down
26 changes: 23 additions & 3 deletions Gateway/Http/AbstractTransferFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,6 +50,13 @@ abstract class AbstractTransferFactory implements TransferFactoryInterface
*/
protected $action;

/**
* Store manager
*
* @var \Magento\Store\App\Model\StoreManagerInterface
*/
protected $_storeManager;

/**
* Construct
*
Expand All @@ -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;
}

/**
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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();
}
}
10 changes: 6 additions & 4 deletions Gateway/Http/TransferFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,27 @@ 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();
}

/**
* 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);
}
}
12 changes: 11 additions & 1 deletion Gateway/Request/AbstractDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use Magento\Payment\Gateway\ConfigInterface;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class AbstractDataBuilder
Expand All @@ -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;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions Gateway/Request/CaptureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions Helper/Pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -52,6 +53,13 @@ class Pixel
*/
protected $scopeConfig;

/**
* Store manager
*
* @var \Magento\Store\App\Model\StoreManagerInterface
*/
protected $_storeManager;

/**
* Init
*
Expand All @@ -60,9 +68,11 @@ class Pixel
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
Config $configAffirm
) {
$this->scopeConfig = $scopeConfig;
$this->_storeManager = $storeManager;
$this->affirmPaymentConfig = $configAffirm;
}

Expand Down Expand Up @@ -121,4 +131,14 @@ public function getConfig($configPath, $scope = 'default')
$scope
);
}

/**
* Get store id
*
* @return int
*/
protected function getStoreId()
{
return $this->_storeManager->getStore()->getId();
}
}
45 changes: 38 additions & 7 deletions Model/Adminhtml/Observer/AfterShipmentSaveObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -60,6 +62,13 @@ class AfterShipmentSaveObserver implements ObserverInterface
*/
protected $scopeConfig;

/**
* Store manager
*
* @var \Magento\Store\App\Model\StoreManagerInterface
*/
protected $_storeManager;

/**
* Affirm logging instance
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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();
}
}
36 changes: 26 additions & 10 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit da30295

Please sign in to comment.