AbstractConfig.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model;
  7. use Magento\Framework\App\ProductMetadataInterface;
  8. use Magento\Payment\Model\Method\ConfigInterface;
  9. use Magento\Payment\Model\MethodInterface;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Framework\App\ObjectManager;
  12. /**
  13. * Class AbstractConfig
  14. */
  15. abstract class AbstractConfig implements ConfigInterface
  16. {
  17. /**#@+
  18. * Payment actions
  19. */
  20. const PAYMENT_ACTION_SALE = 'Sale';
  21. const PAYMENT_ACTION_AUTH = 'Authorization';
  22. const PAYMENT_ACTION_ORDER = 'Order';
  23. /**#@-*/
  24. /**
  25. * PayPal Website Payments Pro - Express Checkout
  26. */
  27. const METHOD_WPP_EXPRESS = 'paypal_express';
  28. /**
  29. * Current payment method code
  30. *
  31. * @var string
  32. */
  33. protected $_methodCode;
  34. /**
  35. * Current store id
  36. *
  37. * @var int
  38. */
  39. protected $_storeId;
  40. /**
  41. * @var string
  42. */
  43. protected $pathPattern;
  44. /**
  45. * @var ProductMetadataInterface
  46. */
  47. protected $productMetadata;
  48. /**
  49. * @var string
  50. */
  51. private static $bnCode = 'Magento_Cart_%s';
  52. /**
  53. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  54. */
  55. protected $_scopeConfig;
  56. /**
  57. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  58. */
  59. public function __construct(
  60. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  61. ) {
  62. $this->_scopeConfig = $scopeConfig;
  63. }
  64. /**
  65. * @var MethodInterface
  66. */
  67. protected $methodInstance;
  68. /**
  69. * Sets method instance used for retrieving method specific data
  70. *
  71. * @param MethodInterface $method
  72. * @return $this
  73. */
  74. public function setMethodInstance($method)
  75. {
  76. $this->methodInstance = $method;
  77. return $this;
  78. }
  79. /**
  80. * Method code setter
  81. *
  82. * @param string|MethodInterface $method
  83. * @return $this
  84. */
  85. public function setMethod($method)
  86. {
  87. if ($method instanceof MethodInterface) {
  88. $this->_methodCode = $method->getCode();
  89. } elseif (is_string($method)) {
  90. $this->_methodCode = $method;
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Payment method instance code getter
  96. *
  97. * @return string
  98. */
  99. public function getMethodCode()
  100. {
  101. return $this->_methodCode;
  102. }
  103. /**
  104. * Store ID setter
  105. *
  106. * @param int $storeId
  107. * @return $this
  108. */
  109. public function setStoreId($storeId)
  110. {
  111. $this->_storeId = (int)$storeId;
  112. return $this;
  113. }
  114. /**
  115. * Returns payment configuration value
  116. *
  117. * @param string $key
  118. * @param null|int $storeId
  119. * @return null|string
  120. *
  121. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  122. */
  123. public function getValue($key, $storeId = null)
  124. {
  125. switch ($key) {
  126. case 'getDebugReplacePrivateDataKeys':
  127. return $this->methodInstance->getDebugReplacePrivateDataKeys();
  128. default:
  129. $underscored = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $key));
  130. $path = $this->_getSpecificConfigPath($underscored);
  131. if ($path !== null) {
  132. $value = $this->_scopeConfig->getValue(
  133. $path,
  134. ScopeInterface::SCOPE_STORE,
  135. $this->_storeId
  136. );
  137. $value = $this->_prepareValue($underscored, $value);
  138. return $value;
  139. }
  140. }
  141. return null;
  142. }
  143. /**
  144. * Sets method code
  145. *
  146. * @param string $methodCode
  147. * @return void
  148. */
  149. public function setMethodCode($methodCode)
  150. {
  151. $this->_methodCode = $methodCode;
  152. }
  153. /**
  154. * Sets path pattern
  155. *
  156. * @param string $pathPattern
  157. * @return void
  158. */
  159. public function setPathPattern($pathPattern)
  160. {
  161. $this->pathPattern = $pathPattern;
  162. }
  163. /**
  164. * Map any supported payment method into a config path by specified field name
  165. *
  166. * @param string $fieldName
  167. * @return string|null
  168. */
  169. protected function _getSpecificConfigPath($fieldName)
  170. {
  171. if ($this->pathPattern) {
  172. return sprintf($this->pathPattern, $this->_methodCode, $fieldName);
  173. }
  174. return "payment/{$this->_methodCode}/{$fieldName}";
  175. }
  176. /**
  177. * Perform additional config value preparation and return new value if needed
  178. *
  179. * @param string $key Underscored key
  180. * @param string $value Old value
  181. * @return string Modified value or old value
  182. */
  183. protected function _prepareValue($key, $value)
  184. {
  185. // Always set payment action as "Sale" for Unilateral payments in EC
  186. if ($key == 'payment_action' &&
  187. $value != self::PAYMENT_ACTION_SALE &&
  188. $this->_methodCode == self::METHOD_WPP_EXPRESS &&
  189. $this->shouldUseUnilateralPayments()
  190. ) {
  191. return self::PAYMENT_ACTION_SALE;
  192. }
  193. return $value;
  194. }
  195. /**
  196. * Check whether only Unilateral payments (Accelerated Boarding) possible for Express method or not
  197. *
  198. * @return bool
  199. */
  200. public function shouldUseUnilateralPayments()
  201. {
  202. return $this->getValue('business_account') && !$this->isWppApiAvailable();
  203. }
  204. /**
  205. * Check whether WPP API credentials are available for this method
  206. *
  207. * @deprecated 100.3.1
  208. * @return bool
  209. */
  210. public function isWppApiAvailabe()
  211. {
  212. return $this->isWppApiAvailable();
  213. }
  214. /**
  215. * Check whether WPP API credentials are available for this method
  216. *
  217. * @return bool
  218. */
  219. public function isWppApiAvailable()
  220. {
  221. return $this->getValue('api_username')
  222. && $this->getValue('api_password')
  223. && ($this->getValue('api_signature')
  224. || $this->getValue('api_cert'));
  225. }
  226. /**
  227. * Check whether method available for checkout or not
  228. *
  229. * @param null|string $methodCode
  230. *
  231. * @return bool
  232. */
  233. public function isMethodAvailable($methodCode = null)
  234. {
  235. $methodCode = $methodCode ?: $this->_methodCode;
  236. return $this->isMethodActive($methodCode);
  237. }
  238. /**
  239. * Check whether method active in configuration and supported for merchant country or not
  240. *
  241. * @param string $method Method code
  242. * @return bool
  243. *
  244. * @todo: refactor this
  245. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  246. */
  247. public function isMethodActive($method)
  248. {
  249. switch ($method) {
  250. case Config::METHOD_WPS_EXPRESS:
  251. case Config::METHOD_WPP_EXPRESS:
  252. $isEnabled = $this->_scopeConfig->isSetFlag(
  253. 'payment/' . Config::METHOD_WPS_EXPRESS .'/active',
  254. ScopeInterface::SCOPE_STORE,
  255. $this->_storeId
  256. )
  257. || $this->_scopeConfig->isSetFlag(
  258. 'payment/' . Config::METHOD_WPP_EXPRESS .'/active',
  259. ScopeInterface::SCOPE_STORE,
  260. $this->_storeId
  261. );
  262. $method = Config::METHOD_WPP_EXPRESS;
  263. break;
  264. case Config::METHOD_WPS_BML:
  265. case Config::METHOD_WPP_BML:
  266. $disabledFunding = $this->_scopeConfig->getValue(
  267. 'payment/paypal_express/disable_funding_options',
  268. ScopeInterface::SCOPE_STORE,
  269. $this->_storeId
  270. );
  271. $isExpressCreditEnabled = $disabledFunding
  272. ? strpos($disabledFunding, 'CREDIT') === false
  273. : true;
  274. $isEnabled = $isExpressCreditEnabled
  275. || $this->_scopeConfig->isSetFlag(
  276. 'payment/' . Config::METHOD_WPP_BML .'/active',
  277. ScopeInterface::SCOPE_STORE,
  278. $this->_storeId
  279. );
  280. $method = Config::METHOD_WPP_BML;
  281. break;
  282. case Config::METHOD_PAYMENT_PRO:
  283. case Config::METHOD_PAYFLOWPRO:
  284. $isEnabled = $this->_scopeConfig->isSetFlag(
  285. 'payment/' . Config::METHOD_PAYMENT_PRO .'/active',
  286. ScopeInterface::SCOPE_STORE,
  287. $this->_storeId
  288. )
  289. || $this->_scopeConfig->isSetFlag(
  290. 'payment/' . Config::METHOD_PAYFLOWPRO .'/active',
  291. ScopeInterface::SCOPE_STORE,
  292. $this->_storeId
  293. );
  294. $method = Config::METHOD_PAYFLOWPRO;
  295. break;
  296. default:
  297. $isEnabled = $this->_scopeConfig->isSetFlag(
  298. "payment/{$method}/active",
  299. ScopeInterface::SCOPE_STORE,
  300. $this->_storeId
  301. );
  302. }
  303. return $this->isMethodSupportedForCountry($method) && $isEnabled;
  304. }
  305. /**
  306. * Check whether method supported for specified country or not
  307. *
  308. * @param string|null $method
  309. * @param string|null $countryCode
  310. * @return bool
  311. *
  312. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  313. */
  314. public function isMethodSupportedForCountry($method = null, $countryCode = null)
  315. {
  316. return true;
  317. }
  318. /**
  319. * BN code getter
  320. *
  321. * @return string
  322. */
  323. public function getBuildNotationCode()
  324. {
  325. $notationCode = $this->_scopeConfig->getValue('paypal/notation_code', ScopeInterface::SCOPE_STORES);
  326. return $notationCode ?: sprintf(self::$bnCode, $this->getProductMetadata()->getEdition());
  327. }
  328. /**
  329. * The getter function to get the ProductMetadata
  330. *
  331. * @return ProductMetadataInterface
  332. * @deprecated 100.1.0
  333. */
  334. protected function getProductMetadata()
  335. {
  336. if ($this->productMetadata === null) {
  337. $this->productMetadata = ObjectManager::getInstance()->get(ProductMetadataInterface::class);
  338. }
  339. return $this->productMetadata;
  340. }
  341. }