/** * Magento will consider a transaction for voiding only if it is an authorization * Braintree allows voiding captures too * * Lookup an authorization transaction using parent transaction id, if set * * @param Payment $subject * @param callable $proceed * * @return \Magento\Sales\Model\Order\Payment\Transaction|false * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function aroundGetAuthorizationTransaction(Payment $subject, \Closure $proceed) { if ($subject->getMethodInstance()->getCode() != PaymentMethod::METHOD_CODE) { return $proceed(); } $invoice = $this->registry->registry('current_invoice'); if ($invoice && $invoice->getId()) { $transactionId = $this->paymentHelper->clearTransactionId($invoice->getTransactionId()); $collection = $this->salesTransactionCollectionFactory->create()->addFieldToFilter('txn_id', ['eq' => $transactionId]); if ($collection->getSize() < 1) { return $proceed(); } else { return $collection->getFirstItem(); } } return $proceed(); }
/** * Find entities by criteria * * @param \Magento\Framework\Api\SearchCriteria $criteria * @return Transaction[] */ public function find(\Magento\Framework\Api\SearchCriteria $criteria) { /** @var TransactionResource\Collection $collection */ $collection = $this->transactionCollectionFactory->create(); foreach ($criteria->getFilterGroups() as $filterGroup) { foreach ($filterGroup->getFilters() as $filter) { $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq'; $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]); } } $collection->setCurPage($criteria->getCurrentPage()); $collection->setPageSize($criteria->getPageSize()); $collection->addPaymentInformation(['method']); $collection->addOrderInformation(['increment_id']); foreach ($collection as $object) { $this->register($object); } $objectIds = $collection->getAllIds(); return array_intersect_key($this->registry, array_flip($objectIds)); }
/** * Find one transaction by ID or type * * @param string $txnId * @param bool|string $txnType * @return Transaction|false */ protected function _lookupTransaction($txnId, $txnType = false) { if (!$txnId) { if ($txnType && $this->getId()) { $collection = $this->_transactionCollectionFactory->create()->setOrderFilter($this->getOrder())->addPaymentIdFilter($this->getId())->addTxnTypeFilter($txnType)->setOrder('created_at', \Magento\Framework\Data\Collection::SORT_ORDER_DESC)->setOrder('transaction_id', \Magento\Framework\Data\Collection::SORT_ORDER_DESC); foreach ($collection as $txn) { $txn->setOrderPaymentObject($this); $this->_transactionsLookup[$txn->getTxnId()] = $txn; return $txn; } } return false; } if (isset($this->_transactionsLookup[$txnId])) { return $this->_transactionsLookup[$txnId]; } $txn = $this->_transactionFactory->create()->setOrderPaymentObject($this)->loadByTxnId($txnId); if ($txn->getId()) { $this->_transactionsLookup[$txnId] = $txn; } else { $this->_transactionsLookup[$txnId] = false; } return $this->_transactionsLookup[$txnId]; }
/** * Voids transaction * * @param InfoInterface $payment * @throws LocalizedException * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function void(InfoInterface $payment) { $transactionIds = $this->getTransactionsToVoid($payment); $message = false; foreach ($transactionIds as $transactionId) { $transaction = $this->braintreeTransaction->find($transactionId); if ($transaction->status !== \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT && $transaction->status !== \Braintree_Transaction::AUTHORIZED) { throw new LocalizedException(__('Some transactions are already settled or voided and cannot be voided.')); } if ($transaction->status === \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { $message = __('Voided capture.'); } } $errors = ''; foreach ($transactionIds as $transactionId) { $this->_debug('void-' . $transactionId); $result = $this->braintreeTransaction->void($transactionId); $this->_debug($result); if (!$result->success) { $errors .= ' ' . $this->errorHelper->parseBraintreeError($result)->getText(); } elseif ($message) { $payment->setMessage($message); } } if ($errors) { throw new LocalizedException(__('There was an error voiding the transaction: %1.', $errors)); } else { $match = true; foreach ($transactionIds as $transactionId) { $collection = $this->salesTransactionCollectionFactory->create()->addFieldToFilter('parent_txn_id', ['eq' => $transactionId])->addFieldToFilter('txn_type', PaymentTransaction::TYPE_VOID); if ($collection->getSize() < 1) { $match = false; } } if ($match) { $payment->setIsTransactionClosed(1); } } return $this; }