/**
  * 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();
 }
 /**
  * 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($this->_convertObjToArray($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(true);
         }
     }
     return $this;
 }