/** * @dataProvider getApplicableCardTypesDataProvider */ public function testGetApplicableCardTypes($country, $countryCardType, $ccTypes, $expectedResult) { $prefix = 'payment/braintree/'; $valueMap = [[$prefix . Cc::KEY_COUNTRY_CREDIT_CARD, ScopeInterface::SCOPE_STORE, null, $countryCardType], [$prefix . Cc::KEY_CC_TYPES, ScopeInterface::SCOPE_STORE, null, $ccTypes]]; $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturnMap($valueMap); $this->assertEquals($expectedResult, $this->model->getApplicableCardTypes($country)); }
/** * Retrieve availables credit card types * * @return array */ public function getCcAvailableTypes() { if ($this->_appState->getAreaCode() === \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) { $country = $this->sessionQuote->getQuote()->getBillingAddress()->getCountryId(); } else { $country = $this->checkoutSession->getQuote()->getBillingAddress()->getCountryId(); } $applicableTypes = $this->config->getApplicableCardTypes($country); $types = $this->_paymentConfig->getCcTypes(); foreach (array_keys($types) as $code) { if (!in_array($code, $applicableTypes)) { unset($types[$code]); } } return $types; }
/** * Retrieve available credit card types as associative array code & title * * @param string $country * @return array * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getCcAvailableCardTypes($country = null) { $types = array_flip(explode(',', $this->braintreeCcConfig->getConfigData('cctypes'))); $mergedArray = []; if (is_array($types)) { foreach (array_keys($types) as $type) { $types[$type] = $this->getCcTypeNameByCode($type); } } //merge options then filter by a specific country $countrySpecificTypes = $this->braintreeCcConfig->getCountrySpecificCardTypeConfig(); //include all country specific in types if (is_array($countrySpecificTypes)) { foreach ($countrySpecificTypes as $countryArray) { foreach ($countryArray as $ccType) { $types[$ccType] = $this->getCcTypeNameByCode($ccType); } } } //preserve the same credit card order $allTypes = $this->getCcTypes(); if (is_array($allTypes)) { foreach ($allTypes as $ccTypeCode => $ccTypeName) { if (array_key_exists($ccTypeCode, $types)) { $mergedArray[$ccTypeCode] = $ccTypeName; } } } if ($country) { $countrySpecificTypesApplicable = $this->braintreeCcConfig->getApplicableCardTypes($country); if (!empty($countrySpecificTypesApplicable)) { foreach (array_keys($mergedArray) as $code) { if (!in_array($code, $countrySpecificTypesApplicable)) { unset($mergedArray[$code]); } } } } return $mergedArray; }
/** * Check whether payment method is applicable to quote * Purposed to allow use in controllers some logic that was implemented in blocks only before * * @param \Magento\Quote\Api\Data\CartInterface|null $quote * @return bool */ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) { if (parent::isAvailable($quote)) { if ($quote != null) { $availableCcTypes = $this->config->getApplicableCardTypes($quote->getBillingAddress()->getCountryId()); if (!$availableCcTypes) { return false; } } } else { return false; } return true; }
/** * Retrieve applicable credit card types * * @return array */ public function getCcApplicableTypes() { return $this->config->getApplicableCardTypes(null); }