/** * Loads country collection * * @return \Magento\Directory\Model\ResourceModel\Country\Collection */ public function getCountryCollection() { $collection = $this->getData('country_collection'); if ($collection == null) { $restrictedCountriesByBraintree = $this->countrySource->getRestrictedCountries(); $collection = $this->_countryCollectionFactory->create()->addFieldToFilter('country_id', ['nin' => $restrictedCountriesByBraintree])->loadByStore(); foreach ($collection as $item) { $countryCode = $item->getData('country_id'); if (!$this->config->canUseForCountry($countryCode)) { $restrictedCountriesByBraintree[] = $item->getData('country_id'); } } $collection = $this->_countryCollectionFactory->create()->addFieldToFilter('country_id', ['nin' => $restrictedCountriesByBraintree])->loadByStore(); $this->setData('country_collection', $collection); } return $collection; }
/** * Validate data * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function validate() { $info = $this->getInfoInstance(); if ($info instanceof \Magento\Sales\Model\Order\Payment) { $billingCountry = $info->getOrder()->getBillingAddress()->getCountryId(); } else { $billingCountry = $info->getQuote()->getBillingAddress()->getCountryId(); } if (!$this->config->canUseForCountry($billingCountry)) { throw new LocalizedException(__('Selected payment type is not allowed for billing country.')); } $ccType = $info->getCcType(); if (!$ccType) { $token = $this->getInfoInstance()->getAdditionalInformation('cc_token'); if ($token) { $ccType = $this->vault->getSavedCardType($token); } } if ($ccType) { $error = $this->config->canUseCcTypeForCountry($billingCountry, $ccType); if ($error) { throw new LocalizedException($error); } } return $this; }
/** * 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; }