Config.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Gateway\Config;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * Class Config
  11. */
  12. class Config extends \Magento\Payment\Gateway\Config\Config
  13. {
  14. const KEY_ENVIRONMENT = 'environment';
  15. const KEY_ACTIVE = 'active';
  16. const KEY_MERCHANT_ID = 'merchant_id';
  17. const KEY_MERCHANT_ACCOUNT_ID = 'merchant_account_id';
  18. const KEY_PUBLIC_KEY = 'public_key';
  19. const KEY_PRIVATE_KEY = 'private_key';
  20. const KEY_COUNTRY_CREDIT_CARD = 'countrycreditcard';
  21. const KEY_CC_TYPES = 'cctypes';
  22. const KEY_CC_TYPES_BRAINTREE_MAPPER = 'cctypes_braintree_mapper';
  23. const KEY_SDK_URL = 'sdk_url';
  24. const KEY_USE_CVV = 'useccv';
  25. const KEY_VERIFY_3DSECURE = 'verify_3dsecure';
  26. const KEY_THRESHOLD_AMOUNT = 'threshold_amount';
  27. const KEY_VERIFY_ALLOW_SPECIFIC = 'verify_all_countries';
  28. const KEY_VERIFY_SPECIFIC = 'verify_specific_countries';
  29. const VALUE_3DSECURE_ALL = 0;
  30. const CODE_3DSECURE = 'three_d_secure';
  31. const KEY_KOUNT_MERCHANT_ID = 'kount_id';
  32. const FRAUD_PROTECTION = 'fraudprotection';
  33. /**
  34. * @var \Magento\Framework\Serialize\Serializer\Json
  35. */
  36. private $serializer;
  37. /**
  38. * Braintree config constructor
  39. *
  40. * @param ScopeConfigInterface $scopeConfig
  41. * @param null|string $methodCode
  42. * @param string $pathPattern
  43. * @param Json|null $serializer
  44. */
  45. public function __construct(
  46. ScopeConfigInterface $scopeConfig,
  47. $methodCode = null,
  48. $pathPattern = self::DEFAULT_PATH_PATTERN,
  49. Json $serializer = null
  50. ) {
  51. parent::__construct($scopeConfig, $methodCode, $pathPattern);
  52. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  53. ->get(Json::class);
  54. }
  55. /**
  56. * Get list of available dynamic descriptors keys
  57. * @var array
  58. */
  59. private static $dynamicDescriptorKeys = [
  60. 'name', 'phone', 'url'
  61. ];
  62. /**
  63. * Return the country specific card type config
  64. *
  65. * @param int|null $storeId
  66. * @return array
  67. */
  68. public function getCountrySpecificCardTypeConfig($storeId = null)
  69. {
  70. $countryCardTypes = $this->getValue(self::KEY_COUNTRY_CREDIT_CARD, $storeId);
  71. if (!$countryCardTypes) {
  72. return [];
  73. }
  74. $countryCardTypes = $this->serializer->unserialize($countryCardTypes);
  75. return is_array($countryCardTypes) ? $countryCardTypes : [];
  76. }
  77. /**
  78. * Retrieve available credit card types
  79. *
  80. * @param int|null $storeId
  81. * @return array
  82. */
  83. public function getAvailableCardTypes($storeId = null)
  84. {
  85. $ccTypes = $this->getValue(self::KEY_CC_TYPES, $storeId);
  86. return !empty($ccTypes) ? explode(',', $ccTypes) : [];
  87. }
  88. /**
  89. * Retrieve mapper between Magento and Braintree card types
  90. *
  91. * @return array
  92. */
  93. public function getCcTypesMapper()
  94. {
  95. $result = json_decode(
  96. $this->getValue(self::KEY_CC_TYPES_BRAINTREE_MAPPER),
  97. true
  98. );
  99. return is_array($result) ? $result : [];
  100. }
  101. /**
  102. * Gets list of card types available for country.
  103. *
  104. * @param string $country
  105. * @param int|null $storeId
  106. * @return array
  107. */
  108. public function getCountryAvailableCardTypes($country, $storeId = null)
  109. {
  110. $types = $this->getCountrySpecificCardTypeConfig($storeId);
  111. return (!empty($types[$country])) ? $types[$country] : [];
  112. }
  113. /**
  114. * Checks if cvv field is enabled.
  115. *
  116. * @param int|null $storeId
  117. * @return bool
  118. */
  119. public function isCvvEnabled($storeId = null)
  120. {
  121. return (bool) $this->getValue(self::KEY_USE_CVV, $storeId);
  122. }
  123. /**
  124. * Checks if 3d secure verification enabled.
  125. *
  126. * @param int|null $storeId
  127. * @return bool
  128. */
  129. public function isVerify3DSecure($storeId = null)
  130. {
  131. return (bool) $this->getValue(self::KEY_VERIFY_3DSECURE, $storeId);
  132. }
  133. /**
  134. * Gets threshold amount for 3d secure.
  135. *
  136. * @param int|null $storeId
  137. * @return float
  138. */
  139. public function getThresholdAmount($storeId = null)
  140. {
  141. return (double) $this->getValue(self::KEY_THRESHOLD_AMOUNT, $storeId);
  142. }
  143. /**
  144. * Gets list of specific countries for 3d secure.
  145. *
  146. * @param int|null $storeId
  147. * @return array
  148. */
  149. public function get3DSecureSpecificCountries($storeId = null)
  150. {
  151. if ((int) $this->getValue(self::KEY_VERIFY_ALLOW_SPECIFIC, $storeId) == self::VALUE_3DSECURE_ALL) {
  152. return [];
  153. }
  154. return explode(',', $this->getValue(self::KEY_VERIFY_SPECIFIC, $storeId));
  155. }
  156. /**
  157. * Gets value of configured environment.
  158. * Possible values: production or sandbox.
  159. *
  160. * @param int|null $storeId
  161. * @return string
  162. */
  163. public function getEnvironment($storeId = null)
  164. {
  165. return $this->getValue(Config::KEY_ENVIRONMENT, $storeId);
  166. }
  167. /**
  168. * Gets Kount merchant ID.
  169. *
  170. * @param int|null $storeId
  171. * @return string
  172. */
  173. public function getKountMerchantId($storeId = null)
  174. {
  175. return $this->getValue(Config::KEY_KOUNT_MERCHANT_ID, $storeId);
  176. }
  177. /**
  178. * Gets merchant ID.
  179. *
  180. * @param int|null $storeId
  181. * @return string
  182. */
  183. public function getMerchantId($storeId = null)
  184. {
  185. return $this->getValue(Config::KEY_MERCHANT_ID, $storeId);
  186. }
  187. /**
  188. * Gets Merchant account ID.
  189. *
  190. * @param int|null $storeId
  191. * @return string
  192. */
  193. public function getMerchantAccountId($storeId = null)
  194. {
  195. return $this->getValue(self::KEY_MERCHANT_ACCOUNT_ID, $storeId);
  196. }
  197. /**
  198. * @return string
  199. */
  200. public function getSdkUrl()
  201. {
  202. return $this->getValue(Config::KEY_SDK_URL);
  203. }
  204. /**
  205. * Checks if fraud protection is enabled.
  206. *
  207. * @param int|null $storeId
  208. * @return bool
  209. */
  210. public function hasFraudProtection($storeId = null)
  211. {
  212. return (bool) $this->getValue(Config::FRAUD_PROTECTION, $storeId);
  213. }
  214. /**
  215. * Gets Payment configuration status.
  216. *
  217. * @param int|null $storeId
  218. * @return bool
  219. */
  220. public function isActive($storeId = null)
  221. {
  222. return (bool) $this->getValue(self::KEY_ACTIVE, $storeId);
  223. }
  224. /**
  225. * Gets list of configured dynamic descriptors.
  226. *
  227. * @param int|null $storeId
  228. * @return array
  229. */
  230. public function getDynamicDescriptors($storeId = null)
  231. {
  232. $values = [];
  233. foreach (self::$dynamicDescriptorKeys as $key) {
  234. $value = $this->getValue('descriptor_' . $key, $storeId);
  235. if (!empty($value)) {
  236. $values[$key] = $value;
  237. }
  238. }
  239. return $values;
  240. }
  241. }