/** * @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; }
/** * @param InfoInterface $payment * @param string $amount * @return $this * @throws LocalizedException */ protected function partialCapture($payment, $amount) { $collection = $this->salesTransactionCollectionFactory->create()->addPaymentIdFilter($payment->getId())->addTxnTypeFilter(PaymentTransaction::TYPE_AUTH)->setOrder('created_at', \Magento\Framework\Data\Collection::SORT_ORDER_DESC)->setOrder('transaction_id', \Magento\Framework\Data\Collection::SORT_ORDER_DESC)->setPageSize(1)->setCurPage(1); $authTransaction = $collection->getFirstItem(); if (!$authTransaction->getId()) { throw new LocalizedException(__('Can not find original authorization transaction for partial capture')); } if ($token = $authTransaction->getAdditionalInformation('token')) { //order was placed using saved card or card was saved during checkout token $found = true; try { $this->braintreeCreditCard->find($token); } catch (\Exception $e) { $found = false; } if ($found) { $this->config->initEnvironment($payment->getOrder()->getStoreId()); $this->braintreeAuthorize($payment, $amount, true, $token); } else { // case if payment token is no more applicable. attempt to clone transaction $result = $this->cloneTransaction($amount, $authTransaction->getTxnId()); if ($result && $result->success) { $this->processSuccessResult($payment, $result, $amount); } else { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } } else { // order was placed without saved card and card wasn't saved during checkout $result = $this->cloneTransaction($amount, $authTransaction->getTxnId()); if ($result->success) { $this->processSuccessResult($payment, $result, $amount); } else { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } return $this; }