Ejemplo n.º 1
0
 /**
  * @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;
 }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 /**
  * @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;
 }
Ejemplo n.º 4
0
 /**
  * @return Collection
  */
 public function getCountryCollection()
 {
     if (!$this->_countryCollection) {
         $this->_countryCollection = $this->_countryCollectionFactory->create()->loadByStore();
     }
     return $this->_countryCollection;
 }
Ejemplo n.º 5
0
 /**
  * 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;
 }
Ejemplo n.º 6
0
 /**
  * {@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;
 }
Ejemplo n.º 7
0
 /**
  * @return \Magento\Directory\Model\Resource\Country\Collection
  */
 protected function _createCountriesCollection()
 {
     return $this->_countriesFactory->create();
 }
Ejemplo n.º 8
0
 /**
  * @return array
  */
 protected function _getOptions()
 {
     $options = $this->_directoriesFactory->create()->load()->toOptionArray(false);
     array_unshift($options, ['value' => '', 'label' => __('All Countries')]);
     return $options;
 }
Ejemplo n.º 9
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;
 }