123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Paypal\Model;
- use Magento\Framework\App\ProductMetadataInterface;
- use Magento\Payment\Model\Method\ConfigInterface;
- use Magento\Payment\Model\MethodInterface;
- use Magento\Store\Model\ScopeInterface;
- use Magento\Framework\App\ObjectManager;
- /**
- * Class AbstractConfig
- */
- abstract class AbstractConfig implements ConfigInterface
- {
- /**#@+
- * Payment actions
- */
- const PAYMENT_ACTION_SALE = 'Sale';
- const PAYMENT_ACTION_AUTH = 'Authorization';
- const PAYMENT_ACTION_ORDER = 'Order';
- /**#@-*/
- /**
- * PayPal Website Payments Pro - Express Checkout
- */
- const METHOD_WPP_EXPRESS = 'paypal_express';
- /**
- * Current payment method code
- *
- * @var string
- */
- protected $_methodCode;
- /**
- * Current store id
- *
- * @var int
- */
- protected $_storeId;
- /**
- * @var string
- */
- protected $pathPattern;
- /**
- * @var ProductMetadataInterface
- */
- protected $productMetadata;
- /**
- * @var string
- */
- private static $bnCode = 'Magento_Cart_%s';
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $_scopeConfig;
- /**
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- */
- public function __construct(
- \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- ) {
- $this->_scopeConfig = $scopeConfig;
- }
- /**
- * @var MethodInterface
- */
- protected $methodInstance;
- /**
- * Sets method instance used for retrieving method specific data
- *
- * @param MethodInterface $method
- * @return $this
- */
- public function setMethodInstance($method)
- {
- $this->methodInstance = $method;
- return $this;
- }
- /**
- * Method code setter
- *
- * @param string|MethodInterface $method
- * @return $this
- */
- public function setMethod($method)
- {
- if ($method instanceof MethodInterface) {
- $this->_methodCode = $method->getCode();
- } elseif (is_string($method)) {
- $this->_methodCode = $method;
- }
- return $this;
- }
- /**
- * Payment method instance code getter
- *
- * @return string
- */
- public function getMethodCode()
- {
- return $this->_methodCode;
- }
- /**
- * Store ID setter
- *
- * @param int $storeId
- * @return $this
- */
- public function setStoreId($storeId)
- {
- $this->_storeId = (int)$storeId;
- return $this;
- }
- /**
- * Returns payment configuration value
- *
- * @param string $key
- * @param null|int $storeId
- * @return null|string
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function getValue($key, $storeId = null)
- {
- switch ($key) {
- case 'getDebugReplacePrivateDataKeys':
- return $this->methodInstance->getDebugReplacePrivateDataKeys();
- default:
- $underscored = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $key));
- $path = $this->_getSpecificConfigPath($underscored);
- if ($path !== null) {
- $value = $this->_scopeConfig->getValue(
- $path,
- ScopeInterface::SCOPE_STORE,
- $this->_storeId
- );
- $value = $this->_prepareValue($underscored, $value);
- return $value;
- }
- }
- return null;
- }
- /**
- * Sets method code
- *
- * @param string $methodCode
- * @return void
- */
- public function setMethodCode($methodCode)
- {
- $this->_methodCode = $methodCode;
- }
- /**
- * Sets path pattern
- *
- * @param string $pathPattern
- * @return void
- */
- public function setPathPattern($pathPattern)
- {
- $this->pathPattern = $pathPattern;
- }
- /**
- * Map any supported payment method into a config path by specified field name
- *
- * @param string $fieldName
- * @return string|null
- */
- protected function _getSpecificConfigPath($fieldName)
- {
- if ($this->pathPattern) {
- return sprintf($this->pathPattern, $this->_methodCode, $fieldName);
- }
- return "payment/{$this->_methodCode}/{$fieldName}";
- }
- /**
- * Perform additional config value preparation and return new value if needed
- *
- * @param string $key Underscored key
- * @param string $value Old value
- * @return string Modified value or old value
- */
- protected function _prepareValue($key, $value)
- {
- // Always set payment action as "Sale" for Unilateral payments in EC
- if ($key == 'payment_action' &&
- $value != self::PAYMENT_ACTION_SALE &&
- $this->_methodCode == self::METHOD_WPP_EXPRESS &&
- $this->shouldUseUnilateralPayments()
- ) {
- return self::PAYMENT_ACTION_SALE;
- }
- return $value;
- }
- /**
- * Check whether only Unilateral payments (Accelerated Boarding) possible for Express method or not
- *
- * @return bool
- */
- public function shouldUseUnilateralPayments()
- {
- return $this->getValue('business_account') && !$this->isWppApiAvailable();
- }
- /**
- * Check whether WPP API credentials are available for this method
- *
- * @deprecated 100.3.1
- * @return bool
- */
- public function isWppApiAvailabe()
- {
- return $this->isWppApiAvailable();
- }
- /**
- * Check whether WPP API credentials are available for this method
- *
- * @return bool
- */
- public function isWppApiAvailable()
- {
- return $this->getValue('api_username')
- && $this->getValue('api_password')
- && ($this->getValue('api_signature')
- || $this->getValue('api_cert'));
- }
- /**
- * Check whether method available for checkout or not
- *
- * @param null|string $methodCode
- *
- * @return bool
- */
- public function isMethodAvailable($methodCode = null)
- {
- $methodCode = $methodCode ?: $this->_methodCode;
- return $this->isMethodActive($methodCode);
- }
- /**
- * Check whether method active in configuration and supported for merchant country or not
- *
- * @param string $method Method code
- * @return bool
- *
- * @todo: refactor this
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function isMethodActive($method)
- {
- switch ($method) {
- case Config::METHOD_WPS_EXPRESS:
- case Config::METHOD_WPP_EXPRESS:
- $isEnabled = $this->_scopeConfig->isSetFlag(
- 'payment/' . Config::METHOD_WPS_EXPRESS .'/active',
- ScopeInterface::SCOPE_STORE,
- $this->_storeId
- )
- || $this->_scopeConfig->isSetFlag(
- 'payment/' . Config::METHOD_WPP_EXPRESS .'/active',
- ScopeInterface::SCOPE_STORE,
- $this->_storeId
- );
- $method = Config::METHOD_WPP_EXPRESS;
- break;
- case Config::METHOD_WPS_BML:
- case Config::METHOD_WPP_BML:
- $disabledFunding = $this->_scopeConfig->getValue(
- 'payment/paypal_express/disable_funding_options',
- ScopeInterface::SCOPE_STORE,
- $this->_storeId
- );
- $isExpressCreditEnabled = $disabledFunding
- ? strpos($disabledFunding, 'CREDIT') === false
- : true;
- $isEnabled = $isExpressCreditEnabled
- || $this->_scopeConfig->isSetFlag(
- 'payment/' . Config::METHOD_WPP_BML .'/active',
- ScopeInterface::SCOPE_STORE,
- $this->_storeId
- );
- $method = Config::METHOD_WPP_BML;
- break;
- case Config::METHOD_PAYMENT_PRO:
- case Config::METHOD_PAYFLOWPRO:
- $isEnabled = $this->_scopeConfig->isSetFlag(
- 'payment/' . Config::METHOD_PAYMENT_PRO .'/active',
- ScopeInterface::SCOPE_STORE,
- $this->_storeId
- )
- || $this->_scopeConfig->isSetFlag(
- 'payment/' . Config::METHOD_PAYFLOWPRO .'/active',
- ScopeInterface::SCOPE_STORE,
- $this->_storeId
- );
- $method = Config::METHOD_PAYFLOWPRO;
- break;
- default:
- $isEnabled = $this->_scopeConfig->isSetFlag(
- "payment/{$method}/active",
- ScopeInterface::SCOPE_STORE,
- $this->_storeId
- );
- }
- return $this->isMethodSupportedForCountry($method) && $isEnabled;
- }
- /**
- * Check whether method supported for specified country or not
- *
- * @param string|null $method
- * @param string|null $countryCode
- * @return bool
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function isMethodSupportedForCountry($method = null, $countryCode = null)
- {
- return true;
- }
- /**
- * BN code getter
- *
- * @return string
- */
- public function getBuildNotationCode()
- {
- $notationCode = $this->_scopeConfig->getValue('paypal/notation_code', ScopeInterface::SCOPE_STORES);
- return $notationCode ?: sprintf(self::$bnCode, $this->getProductMetadata()->getEdition());
- }
- /**
- * The getter function to get the ProductMetadata
- *
- * @return ProductMetadataInterface
- * @deprecated 100.1.0
- */
- protected function getProductMetadata()
- {
- if ($this->productMetadata === null) {
- $this->productMetadata = ObjectManager::getInstance()->get(ProductMetadataInterface::class);
- }
- return $this->productMetadata;
- }
- }
|