/** * 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 string $token * @return bool|string */ public function getSavedCardType($token) { $ccType = false; $useCache = $this->config->useVault(); $cachedValues = $useCache ? $this->cache->load(self::CACHE_KEY_CREDIT_CARDS) : false; if ($cachedValues) { try { $cachedValues = unserialize($cachedValues); } catch (\Exception $e) { $cachedValues = []; } if (array_key_exists($token, $cachedValues)) { return $cachedValues[$token]; } } try { $creditCard = $this->braintreeCreditCard->find($token); $this->debug($token); $this->debug($creditCard); $ccType = $this->braintreeHelper->getCcTypeCodeByName($creditCard->cardType); if (!empty($cachedValues)) { $cachedValues = array_merge($cachedValues, [$token => $ccType]); } else { $cachedValues = [$token => $ccType]; } if ($useCache) { $this->cache->save(serialize($cachedValues), self::CACHE_KEY_CREDIT_CARDS); } } catch (\Exception $e) { $this->logger->critical($e); } return $ccType; }
/** * @return array|void */ public function getConfig() { if (!$this->config->isActive()) { return []; } $config = parent::getConfig(); $clientToken = $this->config->getClientToken(); $useVault = $this->config->useVault(); $selectedCardToken = null; $storedCardOptions = []; if ($useVault) { $storedCards = $this->getStoredCards(); if (count($storedCards) == 0) { $useVault = false; } else { foreach ($storedCards as $creditCard) { $storedCardOptions[] = ['token' => $creditCard->token, 'maskedNumber' => $creditCard->maskedNumber . ' - ' . $creditCard->cardType, 'selected' => $creditCard->default, 'type' => $this->dataHelper->getCcTypeCodeByName($creditCard->cardType)]; if ($creditCard->default) { $selectedCardToken = $creditCard->token; } } } } $config = array_merge_recursive($config, ['payment' => ['braintree' => ['clientToken' => $clientToken, 'useVault' => $useVault, 'canSaveCard' => $this->canSaveCard(), 'show3dSecure' => $this->show3dSecure(), 'storedCards' => $storedCardOptions, 'selectedCardToken' => $selectedCardToken, 'creditCardExpMonth' => (int) $this->dataHelper->getTodayMonth(), 'creditCardExpYear' => (int) $this->dataHelper->getTodayYear(), 'countrySpecificCardTypes' => $this->config->getCountrySpecificCardTypeConfig(), 'isFraudDetectionEnabled' => $this->config->isFraudDetectionEnabled(), 'isCcDetectionEnabled' => $this->config->isCcDetectionEnabled(), 'availableCardTypes' => $this->getCcAvailableCcTypes(), 'braintreeDataJs' => $this->config->getBraintreeDataJs(), 'ajaxGenerateNonceUrl' => $this->getAjaxGenerateNonceUrl()]]]); return $config; }
/** * Returns applicable stored cards * * @return array */ public function getStoredCards() { $storedCards = $this->vault->currentCustomerStoredCards(); $country = $this->checkoutSession->getQuote()->getBillingAddress()->getCountryId(); $cardTypes = $this->config->getApplicableCardTypes($country); $applicableCards = []; foreach ($storedCards as $card) { if (in_array($this->dataHelper->getCcTypeCodeByName($card->cardType), $cardTypes)) { $applicableCards[] = $card; } } return $applicableCards; }