/**
  * @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;
 }
Example #2
0
 /**
  * Returns countries array
  *
  * @return array
  */
 public function getCountries()
 {
     if (!$this->countries) {
         $this->countries = $this->collectionFactory->create()->addFieldToFilter('country_id', ['nin' => $this->countryConfig->getExcludedCountries()])->loadData()->toOptionArray(false);
     }
     return $this->countries;
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * @return \Magento\Directory\Model\ResourceModel\Country\Collection
  */
 public function getCountryCollection()
 {
     $collection = $this->getData('country_collection');
     if ($collection === null) {
         $collection = $this->_countryCollectionFactory->create()->loadByStore();
         $this->setData('country_collection', $collection);
     }
     return $collection;
 }
 /**
  * Load directory countries
  *
  * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
  */
 protected function loadCountries()
 {
     if ($this->iso2Countries !== null && $this->iso3Countries !== null) {
         return $this;
     }
     $this->iso2Countries = [];
     $this->iso3Countries = [];
     /** @var $collection \Magento\Directory\Model\ResourceModel\Country\Collection */
     $collection = $this->_countryCollectionFactory->create();
     foreach ($collection->getData() as $row) {
         $this->iso2Countries[$row['iso2_code']] = $row['country_id'];
         $this->iso3Countries[$row['iso3_code']] = $row['country_id'];
     }
     return $this;
 }
 /**
  * @return Collection
  * @removeCandidate
  */
 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\\ResourceModel\\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());
     }
 }
 /**
  * {@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;
 }
Example #9
0
 /**
  * Return country collection
  *
  * @return \Magento\Directory\Model\ResourceModel\Country\Collection
  */
 public function getCountries()
 {
     return $this->countryCollectionFactory->create()->loadByStore();
 }
Example #10
0
 public function uploadAndImport(\Magento\Framework\DataObject $object)
 {
     $csvFile = $_FILES["groups"]["tmp_name"]["matrixrate"]["fields"]["import"]["value"];
     if (!empty($csvFile)) {
         $csv = trim(file_get_contents($csvFile));
         $table = $this->getMainTable();
         $websiteModel = $this->_storeManager->getWebsite($object->getScopeId());
         $websiteId = (int) $websiteModel->getId();
         $this->_importWebsiteId = (int) $websiteModel->getId();
         $this->_importUniqueHash = [];
         $this->_importErrors = [];
         $this->_importedRows = 0;
         /*
          getting condition name from post instead of the following commented logic
         */
         if ($object->getData('groups/matrixrate/fields/condition_name/inherit') == '1') {
             $conditionName = (string) $this->_coreConfig->getValue('carriers/matrixrate/condition_name', 'default');
         } else {
             $conditionName = $object->getData('groups/matrixrate/fields/condition_name/value');
         }
         $conditionFullName = $this->_getConditionFullName($conditionName);
         if (!empty($csv)) {
             $exceptions = array();
             $csvLines = explode("\n", $csv);
             $csvLine = array_shift($csvLines);
             $csvLine = $this->_getCsvValues($csvLine);
             if (count($csvLine) < 7) {
                 $exceptions[0] = __('Invalid Matrix Rates File Format');
             }
             $countryCodes = array();
             $regionCodes = array();
             foreach ($csvLines as $k => $csvLine) {
                 $csvLine = $this->_getCsvValues($csvLine);
                 if (count($csvLine) > 0 && count($csvLine) < 7) {
                     $exceptions[0] = __('Invalid Matrix Rates File Format');
                 } else {
                     $countryCodes[] = $csvLine[0];
                     $regionCodes[] = $csvLine[1];
                 }
             }
             if (empty($exceptions)) {
                 $data = array();
                 $countryCodesToIds = array();
                 $regionCodesToIds = array();
                 $countryCodesIso2 = array();
                 /** @var $collection \Magento\Directory\Model\ResourceModel\Country\Collection */
                 $collection = $this->_countryCollectionFactory->create();
                 $collection->addCountryCodeFilter($countryCodes);
                 foreach ($collection->getData() as $row) {
                     $countryCodesToIds[$row['iso2_code']] = $row['country_id'];
                     $countryCodesToIds[$row['iso3_code']] = $row['country_id'];
                     $countryCodesIso2[] = $row['iso2_code'];
                 }
                 /** @var $collection \Magento\Directory\Model\ResourceModel\Region\Collection */
                 $collection = $this->_regionCollectionFactory->create()->addRegionCodeFilter($regionCodes)->addCountryFilter($countryCodesIso2);
                 foreach ($collection->getData() as $row) {
                     $regionCodesToIds[$row['country_id']][$row['code']] = (int) $row['region_id'];
                 }
                 foreach ($csvLines as $k => $csvLine) {
                     $csvLine = $this->_getCsvValues($csvLine);
                     if (empty($countryCodesToIds) || !array_key_exists($csvLine[0], $countryCodesToIds)) {
                         $countryId = '0';
                         if ($csvLine[0] != '*' && $csvLine[0] != '') {
                             $exceptions[] = __('Invalid Country "%s" in the Row #%s', $csvLine[0], $k + 1);
                         }
                     } else {
                         $countryId = $countryCodesToIds[$csvLine[0]];
                     }
                     if (!isset($countryCodesToIds[$csvLine[0]]) || !isset($regionCodesToIds[$countryCodesToIds[$csvLine[0]]]) || !array_key_exists($csvLine[1], $regionCodesToIds[$countryCodesToIds[$csvLine[0]]])) {
                         $regionId = '0';
                         if ($csvLine[1] != '*' && $csvLine[1] != '') {
                             $exceptions[] = __('Invalid Region/State "%s" in the Row #%s', $csvLine[1], $k + 1);
                         }
                     } else {
                         $regionId = $regionCodesToIds[$countryCodesToIds[$csvLine[0]]][$csvLine[1]];
                     }
                     if (count($csvLine) == 9) {
                         // we are searching for postcodes in ranges & including cities
                         if ($csvLine[2] == '*' || $csvLine[2] == '') {
                             $city = '';
                         } else {
                             $city = $csvLine[2];
                         }
                         if ($csvLine[3] == '*' || $csvLine[3] == '') {
                             $zip = '';
                         } else {
                             $zip = $csvLine[3];
                         }
                         if ($csvLine[4] == '*' || $csvLine[4] == '') {
                             $zip_to = '';
                         } else {
                             $zip_to = $csvLine[4];
                         }
                         if (!$this->_isPositiveDecimalNumber($csvLine[5]) || $csvLine[5] == '*' || $csvLine[5] == '') {
                             $exceptions[] = __('Invalid %s From "%s" in the Row #%s', $conditionFullName, $csvLine[5], $k + 1);
                         } else {
                             $csvLine[5] = (double) $csvLine[5];
                         }
                         if (!$this->_isPositiveDecimalNumber($csvLine[6])) {
                             $exceptions[] = __('Invalid %s To "%s" in the Row #%s', $conditionFullName, $csvLine[6], $k + 1);
                         } else {
                             $csvLine[6] = (double) $csvLine[6];
                         }
                         $data[] = array('website_id' => $websiteId, 'dest_country_id' => $countryId, 'dest_region_id' => $regionId, 'dest_city' => $city, 'dest_zip' => $zip, 'dest_zip_to' => $zip_to, 'condition_name' => $conditionName, 'condition_from_value' => $csvLine[5], 'condition_to_value' => $csvLine[6], 'price' => $csvLine[7], 'cost' => 0, 'delivery_type' => $csvLine[8]);
                     } else {
                         if ($csvLine[2] == '*' || $csvLine[2] == '') {
                             $zip = '';
                         } else {
                             $zip = $csvLine[2] . "%";
                         }
                         $city = '';
                         $zip_to = '';
                         if (!$this->_isPositiveDecimalNumber($csvLine[3]) || $csvLine[3] == '*' || $csvLine[3] == '') {
                             $exceptions[] = __('Invalid %s From "%s" in the Row #%s', $conditionFullName, $csvLine[3], $k + 1);
                         } else {
                             $csvLine[3] = (double) $csvLine[3];
                         }
                         if (!$this->_isPositiveDecimalNumber($csvLine[4])) {
                             $exceptions[] = __('Invalid %s To "%s" in the Row #%s', $conditionFullName, $csvLine[4], $k + 1);
                         } else {
                             $csvLine[4] = (double) $csvLine[4];
                         }
                         $data[] = array('website_id' => $websiteId, 'dest_country_id' => $countryId, 'dest_region_id' => $regionId, 'dest_city' => $city, 'dest_zip' => $zip, 'dest_zip_to' => $zip_to, 'condition_name' => $conditionName, 'condition_from_value' => $csvLine[3], 'condition_to_value' => $csvLine[4], 'price' => $csvLine[5], 'cost' => 0, 'delivery_type' => $csvLine[6]);
                     }
                     $dataDetails[] = array('country' => $csvLine[0], 'region' => $csvLine[1]);
                 }
             }
             if (empty($exceptions)) {
                 $connection = $this->getConnection();
                 $condition = array($connection->quoteInto('website_id = ?', $websiteId), $connection->quoteInto('condition_name = ?', $conditionName));
                 $connection->delete($table, $condition);
                 //foreach ($data as $k => $dataLine) {
                 try {
                     foreach ($data as $v) {
                         $data2[] = array_values($v);
                     }
                     $this->_saveImportData($data2);
                 } catch (\Exception $e) {
                     //$connection->rollback();
                     $this->_logger->critical($e);
                     $exceptions[] = __($e->__toString());
                     //__('Duplicate Row #%s (Country "%s", Region/State "%s", City "%s", Zip From "%s", Zip To "%s", Delivery Type "%s", Value From "%s" and Value To "%s")', ($k + 1), $dataDetails[$k]['country'], $dataDetails[$k]['region'], $dataLine['dest_city'], $dataLine['dest_zip'], $dataLine['dest_zip_to'], $dataLine['delivery_type'], $dataLine['condition_from_value'], $dataLine['condition_to_value']);
                 }
                 //}
             }
             if (!empty($exceptions)) {
                 throw new \Magento\Framework\Exception\LocalizedException(__(implode("\n", $exceptions)));
             }
         }
     }
 }
Example #11
0
 /**
  * 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;
 }
Example #12
0
 /**
  * @return array
  */
 protected function _getOptions()
 {
     $options = $this->_directoriesFactory->create()->load()->toOptionArray(false);
     array_unshift($options, ['value' => '', 'label' => __('All Countries')]);
     return $options;
 }
Example #13
0
 /**
  * @return array
  */
 protected function _getOptions()
 {
     $options = $this->_collectionFactory->load()->toOptionArray();
     array_unshift($options, ['value' => '', 'label' => __('All countries')]);
     return $options;
 }
Example #14
0
 /**
  * @return \Magento\Directory\Model\ResourceModel\Country\Collection
  */
 protected function _createCountriesCollection()
 {
     return $this->_countriesFactory->create();
 }