Form.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Block;
  7. use Magento\Backend\Model\Session\Quote;
  8. use Magento\Braintree\Gateway\Config\Config as GatewayConfig;
  9. use Magento\Braintree\Model\Adminhtml\Source\CcType;
  10. use Magento\Braintree\Model\Ui\ConfigProvider;
  11. use Magento\Framework\View\Element\Template\Context;
  12. use Magento\Payment\Block\Form\Cc;
  13. use Magento\Payment\Helper\Data;
  14. use Magento\Payment\Model\Config;
  15. use Magento\Vault\Model\VaultPaymentInterface;
  16. /**
  17. * Class Form
  18. */
  19. class Form extends Cc
  20. {
  21. /**
  22. * @var Quote
  23. */
  24. protected $sessionQuote;
  25. /**
  26. * @var Config
  27. */
  28. protected $gatewayConfig;
  29. /**
  30. * @var CcType
  31. */
  32. protected $ccType;
  33. /**
  34. * @var Data
  35. */
  36. private $paymentDataHelper;
  37. /**
  38. * @param Context $context
  39. * @param Config $paymentConfig
  40. * @param Quote $sessionQuote
  41. * @param GatewayConfig $gatewayConfig
  42. * @param CcType $ccType
  43. * @param Data $paymentDataHelper
  44. * @param array $data
  45. */
  46. public function __construct(
  47. Context $context,
  48. Config $paymentConfig,
  49. Quote $sessionQuote,
  50. GatewayConfig $gatewayConfig,
  51. CcType $ccType,
  52. Data $paymentDataHelper,
  53. array $data = []
  54. ) {
  55. parent::__construct($context, $paymentConfig, $data);
  56. $this->sessionQuote = $sessionQuote;
  57. $this->gatewayConfig = $gatewayConfig;
  58. $this->ccType = $ccType;
  59. $this->paymentDataHelper = $paymentDataHelper;
  60. }
  61. /**
  62. * Get list of available card types of order billing address country
  63. * @return array
  64. */
  65. public function getCcAvailableTypes()
  66. {
  67. $configuredCardTypes = $this->getConfiguredCardTypes();
  68. $countryId = $this->sessionQuote->getQuote()->getBillingAddress()->getCountryId();
  69. return $this->filterCardTypesForCountry($configuredCardTypes, $countryId);
  70. }
  71. /**
  72. * Check if cvv validation is available
  73. * @return boolean
  74. */
  75. public function useCvv()
  76. {
  77. return $this->gatewayConfig->isCvvEnabled($this->sessionQuote->getStoreId());
  78. }
  79. /**
  80. * Check if vault enabled
  81. * @return bool
  82. */
  83. public function isVaultEnabled()
  84. {
  85. $vaultPayment = $this->getVaultPayment();
  86. return $vaultPayment->isActive($this->sessionQuote->getStoreId());
  87. }
  88. /**
  89. * Get card types available for Braintree
  90. * @return array
  91. */
  92. private function getConfiguredCardTypes()
  93. {
  94. $types = $this->ccType->getCcTypeLabelMap();
  95. $configCardTypes = array_fill_keys(
  96. $this->gatewayConfig->getAvailableCardTypes($this->sessionQuote->getStoreId()),
  97. ''
  98. );
  99. return array_intersect_key($types, $configCardTypes);
  100. }
  101. /**
  102. * Filter card types for specific country
  103. * @param array $configCardTypes
  104. * @param string $countryId
  105. * @return array
  106. */
  107. private function filterCardTypesForCountry(array $configCardTypes, $countryId)
  108. {
  109. $filtered = $configCardTypes;
  110. $countryCardTypes = $this->gatewayConfig->getCountryAvailableCardTypes(
  111. $countryId,
  112. $this->sessionQuote->getStoreId()
  113. );
  114. // filter card types only if specific card types are set for country
  115. if (!empty($countryCardTypes)) {
  116. $availableTypes = array_fill_keys($countryCardTypes, '');
  117. $filtered = array_intersect_key($filtered, $availableTypes);
  118. }
  119. return $filtered;
  120. }
  121. /**
  122. * Get configured vault payment for Braintree
  123. * @return VaultPaymentInterface
  124. */
  125. private function getVaultPayment()
  126. {
  127. return $this->paymentDataHelper->getMethodInstance(ConfigProvider::CC_VAULT_CODE);
  128. }
  129. }