Ejemplo n.º 1
0
 /**
  * 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;
 }