/** * @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; }
public function testDeleteException() { $exception = new \Braintree_Exception(); $cardToken = 1; $this->braintreeCreditCardMock->expects($this->once())->method('delete')->with($cardToken)->willThrowException($exception); $this->loggerMock->expects($this->once())->method('critical')->with($exception); $return = $this->model->deleteCard($cardToken); $this->assertEquals(false, $return); }
/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage There was an error capturing the transaction: error. */ public function testPartialCaptureCloneTransactionError() { $amount = self::AUTH_AMOUNT; $paymentId = 1005; $authTransactionId = 1006; $braintreeTransactionId = '4fg7hj'; $paymentObject = $this->setupPaymentObjectForCapture($paymentId); $this->setupSalesTransaction($paymentId, 1); //one existing capture transaction $authTransactionMock = $this->getMockBuilder('\\Magento\\Sales\\Model\\Order\\Payment\\Transaction')->disableOriginalConstructor()->getMock(); $authTransactionMock->expects($this->any())->method('getId')->willReturn($authTransactionId); $authTransactionMock->expects($this->once())->method('getAdditionalInformation')->with('token')->willReturn(self::CC_TOKEN); $authTransactionMock->expects($this->once())->method('getTxnId')->willReturn($braintreeTransactionId); $this->setupAuthTransaction($paymentId, $authTransactionMock); $this->braintreeCreditCardMock->expects($this->once())->method('find')->with(self::CC_TOKEN)->willThrowException(new \Exception('not found')); $resultError = $this->getMockBuilder('\\Braintree_Result_Error')->disableOriginalConstructor()->getMock(); $this->braintreeTransactionMock->expects($this->once())->method('cloneTransaction')->with($braintreeTransactionId, ['amount' => $amount, 'options' => ['submitForSettlement' => true]])->willReturn($resultError); $this->errorHelperMock->expects($this->once())->method('parseBraintreeError')->with($resultError)->willReturn(new \Magento\Framework\Phrase('error')); $this->model->capture($paymentObject, $amount); }
/** * @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; }