/**
  * Returns customer credit cards if applicable
  * 
  * @return \Braintree_Customer|boolean
  */
 public function getLoggedInCustomerCards()
 {
     $applicableCards = [];
     $useVault = (bool) (int) $this->scopeConfig->getValue(self::CONFIG_PATH_VAULT, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $this->sessionQuote->getStoreId());
     if ($useVault) {
         $storedCards = false;
         if ($this->sessionQuote->getCustomerId()) {
             $customerId = $this->paymentHelper->generateCustomerId($this->sessionQuote->getCustomerId(), $this->sessionQuote->getQuote()->getCustomerEmail());
             try {
                 $storedCards = $this->braintreeCustomerAdapter->find($customerId)->creditCards;
             } catch (\Braintree_Exception $e) {
                 $this->_logger->critical($e);
             }
         }
         if ($storedCards) {
             $country = $this->sessionQuote->getQuote()->getBillingAddress()->getCountryId();
             $cardTypes = $this->paymentHelper->getCcAvailableCardTypes($country);
             $applicableCards = [];
             foreach ($storedCards as $card) {
                 if (isset($cardTypes[$this->paymentHelper->getCcTypeCodeByName($card->cardType)])) {
                     $applicableCards[] = $card;
                 }
             }
         }
     }
     return $applicableCards;
 }
 /**
  * @param array $data
  * @param boolean|array $expected
  * @dataProvider getLoggedInCustomerCardsDataProvider
  */
 public function testGetLoggedInCustomerCards($data, $expected)
 {
     $this->sessionQuote->expects($this->once())->method('getStoreId')->willReturn(1);
     $this->scopeConfig->expects($this->any())->method('getValue')->willReturn($data['vault']);
     if ($data['vault']) {
         $this->sessionQuote->expects($this->any())->method('getCustomerId')->willReturn(1);
         $this->paymentHelper->expects($this->any())->method('generateCustomerId')->willReturn(1);
         $this->paymentHelper->expects($this->any())->method('getCcAvailableCardTypes')->willReturn(['VI' => "VISA", 'MC' => "MasterCard"]);
         $this->paymentHelper->expects($this->any())->method('getCcTypeCodeByName')->willReturn(count($expected) ? 'VI' : false);
         $ccobj = json_decode(json_encode(['creditCards' => null]));
         $ccobj->creditCards[] = json_decode(json_encode(['cardType' => 'AE']));
         $ccobj->creditCards[] = json_decode(json_encode(['cardType' => 'VI']));
         $ccobj->creditCards[] = json_decode(json_encode(['cardType' => 'MC']));
         $this->braintreeCustomerAdapter->expects($this->any())->method('find')->willReturn($ccobj);
         $billing = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote\\Address')->disableOriginalConstructor()->setMethods(['getCountryId'])->getMock();
         $billing->expects($this->once())->method('getCountryId')->willReturn("US");
         $quote = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->setMethods(['getBillingAddress'])->getMock();
         $this->sessionQuote->expects($this->any())->method('getCustomerEmail')->willReturn("*****@*****.**");
         $quote->expects($this->any())->method('getBillingAddress')->willReturn($billing);
         $this->sessionQuote->expects($this->any())->method('getQuote')->willReturn($quote);
     }
     $result = $this->model->getLoggedInCustomerCards();
     $this->assertEquals($expected, $result);
 }
Exemple #3
0
 /**
  * @param array $creditCardsArray
  * @param boolean $useVault
  * @param stdClass $braintreeCustomerObject
  * @dataProvider dataProviderCurrentCustomerStoredCards
  */
 public function testCurrentCustomerStoredCards($creditCardsArray = [], $useVault = false, $braintreeCustomerObject = null)
 {
     $customerId = 1;
     $this->configMock->expects($this->once())->method('useVault')->willReturn($useVault);
     $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->willReturn(true);
     $this->customerFactoryMock->expects($this->any())->method('create')->willReturn($this->customerMock);
     $this->helperMock->expects($this->any())->method('generateCustomerId')->willReturn($customerId);
     $this->customerMock->expects($this->any())->method('load')->willReturn($this->customerMock);
     if ($braintreeCustomerObject != null) {
         $this->braintreeCustomerMock->expects($this->any())->method('find')->with($customerId)->willReturn($braintreeCustomerObject);
     } else {
         $this->braintreeCustomerMock->expects($this->any())->method('find')->with($customerId)->willThrowException(new \Braintree_Exception());
     }
     $result = $this->model->currentCustomerStoredCards();
     $this->assertEquals($creditCardsArray, $result);
 }
 /**
  * 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;
 }