/** * @param bool $isMultiselect * @return array */ public function toOptionArray($isMultiselect = false) { if (!$this->_options) { $countriesArray = $this->_countryCollectionFactory->create()->load()->toOptionArray(false); $this->_countries = []; foreach ($countriesArray as $a) { $this->_countries[$a['value']] = $a['label']; } $countryRegions = []; $regionsCollection = $this->_regionCollectionFactory->create()->load(); foreach ($regionsCollection as $region) { $countryRegions[$region->getCountryId()][$region->getId()] = $region->getDefaultName(); } uksort($countryRegions, [$this, 'sortRegionCountries']); $this->_options = []; foreach ($countryRegions as $countryId => $regions) { $regionOptions = []; foreach ($regions as $regionId => $regionName) { $regionOptions[] = ['label' => $regionName, 'value' => $regionId]; } $this->_options[] = ['label' => $this->_countries[$countryId], 'value' => $regionOptions]; } } $options = $this->_options; if (!$isMultiselect) { array_unshift($options, ['value' => '', 'label' => '']); } return $options; }
/** * Returns countries array * * @return array */ protected function _getCountries() { if (!$this->countries) { $restrictedCountries = $this->countrySource->getRestrictedCountries(); $this->countries = $this->countryCollectionFactory->create()->addFieldToFilter('country_id', ['nin' => $restrictedCountries])->loadData()->toOptionArray(false); } return $this->countries; }
/** * @return \Magento\Directory\Model\Resource\Country\Collection */ public function getCountryCollection() { $collection = $this->getData('country_collection'); if (is_null($collection)) { $collection = $this->_countryCollectionFactory->create()->loadByStore(); $this->setData('country_collection', $collection); } return $collection; }
/** * @return Collection */ public function getCountryCollection() { if (!$this->_countryCollection) { $this->_countryCollection = $this->_countryCollectionFactory->create()->loadByStore(); } return $this->_countryCollection; }
/** * @param string $params * @dataProvider dataProcessNonce */ public function testProcessNonceException($params = null, $exceptionMessage = null) { $this->customerSessionMock->expects($this->any())->method('getCustomerId')->willReturn($params['customerId']); $countryCollectionMock = $this->getMockBuilder('\\Magento\\Directory\\Model\\Resource\\Country\\Collection')->disableOriginalConstructor()->getMock(); $countryCollectionMock->expects($this->any())->method('addCountryCodeFilter')->willReturn($countryCollectionMock); $this->countryFactoryMock->expects($this->any())->method('create')->willReturn($countryCollectionMock); $this->configMock->expects($this->any())->method('canUseForCountry')->willReturn($params['canUseForCountry']); $this->configMock->expects($this->any())->method('canUseCcTypeForCountry')->willReturn($params['canUseCcTypeForCountry']); $this->helperMock->expects($this->any())->method('generateCustomerId')->willReturn($params['customerId']); $this->customerMock->expects($this->any())->method('load')->willReturn($this->customerMock); $this->customerFactoryMock->expects($this->any())->method('create')->willReturn($this->customerMock); if (is_object($params['paymentMethodObj'])) { if (!$params['optionsArray']['update']) { $this->braintreePaymentMethodMock->expects($this->once())->method('create')->willReturn($params['paymentMethodObj']); } else { $this->braintreePaymentMethodMock->expects($this->once())->method('update')->willReturn($params['paymentMethodObj']); } if (!$params['paymentMethodObj']->success) { $this->errorHelperMock->expects($this->once())->method('parseBraintreeError')->willReturn(new \Magento\Framework\Phrase($exceptionMessage)); } else { $this->errorHelperMock->expects($this->never())->method('parseBraintreeError'); } } try { $this->model->processNonce($params['nonce'], $params['optionsArray'], $params['billingAddress']); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->assertEquals($exceptionMessage, $e->getMessage()); } }
/** * Load directory countries * * @return \Magento\OfflineShipping\Model\Resource\Carrier\Tablerate */ protected function _loadDirectoryCountries() { if (!is_null($this->_importIso2Countries) && !is_null($this->_importIso3Countries)) { return $this; } $this->_importIso2Countries = array(); $this->_importIso3Countries = array(); /** @var $collection \Magento\Directory\Model\Resource\Country\Collection */ $collection = $this->_countryCollectionFactory->create(); foreach ($collection->getData() as $row) { $this->_importIso2Countries[$row['iso2_code']] = $row['country_id']; $this->_importIso3Countries[$row['iso3_code']] = $row['country_id']; } return $this; }
/** * {@inheritdoc} */ public function toOptionArray($isMultiselect = false) { $supported = $this->_configFactory->create()->getSupportedBuyerCountryCodes(); $options = $this->_countryCollectionFactory->create()->addCountryCodeFilter($supported, 'iso2')->loadData()->toOptionArray($isMultiselect ? false : __('--Please Select--')); return $options; }
/** * @return \Magento\Directory\Model\Resource\Country\Collection */ protected function _createCountriesCollection() { return $this->_countriesFactory->create(); }
/** * @return array */ protected function _getOptions() { $options = $this->_directoriesFactory->create()->load()->toOptionArray(false); array_unshift($options, ['value' => '', 'label' => __('All Countries')]); return $options; }
/** * Gets response from braintree api using the nonce * * @param string|null $nonce * @param array|null $options * @param array|null $billingAddress * @return $this * @throws LocalizedException * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function processNonce($nonce = null, $options = null, $billingAddress = null) { $customerId = $this->customerSession->getCustomerId(); if (!$customerId) { throw new LocalizedException(__('Invalid Customer ID provided')); } $billingCountry = null; if (is_array($billingAddress)) { $collection = $this->countryFactory->create()->addCountryCodeFilter($billingAddress['countryCodeAlpha2']); if ($collection->getSize()) { $billingCountry = $collection->getFirstItem()->getId(); } if (!$this->config->canUseForCountry($billingCountry)) { throw new LocalizedException(__('Selected payment type is not allowed for billing country.')); } } $ccType = null; if ($options) { $ccType = $options['ccType']; } if ($ccType) { $error = $this->config->canUseCcTypeForCountry($billingCountry, $ccType); if ($error) { throw new LocalizedException($error); } } $customer = $this->customerFactory->create()->load($customerId); $customerId = $this->braintreeHelper->generateCustomerId($customerId, $customer->getEmail()); if (!$this->exists($customerId)) { $customerRequest = ['id' => $customerId, 'firstName' => $billingAddress['firstName'], 'lastName' => $billingAddress['lastName'], 'email' => $this->customerSession->getCustomerDataObject()->getEmail()]; if (strlen($billingAddress['company'])) { $customerRequest['company'] = $billingAddress['company']; } $result = $this->braintreeCustomer->create($customerRequest); if (!$result->success) { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } //check if customerId is created on braintree $requestArray = ['customerId' => $customerId, 'paymentMethodNonce' => $nonce, 'options' => ['makeDefault' => $options['default'] == 'true' ? true : false, 'failOnDuplicatePaymentMethod' => $this->config->allowDuplicateCards() == '1' ? false : true, 'verifyCard' => $this->config->useCvv() == '1' ? true : false]]; if ($this->config->isFraudDetectionEnabled() && strlen($options['device_data']) > 0) { $requestArray['deviceData'] = $options['device_data']; } if ($options['update'] == 'true') { $token = $options['token']; unset($requestArray['customerId']); unset($requestArray['options']['failOnDuplicatePaymentMethod']); $requestArray['billingAddress'] = $billingAddress; $result = $this->braintreePaymentMethod->update($token, $requestArray); $this->debug($requestArray); $this->debug($result); } else { $result = $this->braintreePaymentMethod->create($requestArray); $this->debug($requestArray); $this->debug($result); } if (!$result->success) { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } return $this; }
/** * @return array */ protected function _getOptions() { $options = $this->_collectionFactory->load()->toOptionArray(); array_unshift($options, array('value' => '', 'label' => __('All countries'))); return $options; }