/** * Refunds specified amount * * @param InfoInterface $payment * @param float $amount * @return $this * @throws LocalizedException */ public function refund(InfoInterface $payment, $amount) { $transactionId = $this->braintreeHelper->clearTransactionId($payment->getRefundTransactionId()); try { $transaction = $this->braintreeTransaction->find($transactionId); $this->_debug([$payment->getCcTransId()]); $this->_debug($this->_convertObjToArray($transaction)); if ($transaction->status === \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { if ($transaction->amount != $amount) { $message = __('This refund is for a partial amount but the Transaction has not settled.')->getText(); $message .= ' '; $message .= __('Please wait 24 hours before trying to issue a partial refund.')->getText(); throw new LocalizedException(__($message)); } } $canVoid = $transaction->status === \Braintree_Transaction::AUTHORIZED || $transaction->status === \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT; $result = $canVoid ? $this->braintreeTransaction->void($transactionId) : $this->braintreeTransaction->refund($transactionId, $amount); $this->_debug($this->_convertObjToArray($result)); if ($result->success) { $payment->setIsTransactionClosed(true); } else { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } catch (\Exception $e) { $message = $e->getMessage(); throw new LocalizedException(__('There was an error refunding the transaction: %1.', $message)); } return $this; }