diff --git a/CRM/Core/Payment/Faps.php b/CRM/Core/Payment/Faps.php index 5c6d3fdd..d2aaa39d 100644 --- a/CRM/Core/Payment/Faps.php +++ b/CRM/Core/Payment/Faps.php @@ -433,18 +433,9 @@ public function supportsRefund() { // might become a supported core function but for now just create our own function name public function doRefund($params = []) { - // find the token for this contribution - try { - $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $params['contribution_id'])); - } - catch (CiviCRM_API3_Exception $e) { - // FIXME: display an error message or something ? - throw new \Civi\Payment\Exception\PaymentProcessorException($e->getMessage()); - } - - $params = [ - 'refNumber' => $contribution['trxn_id'], - 'transactionAmount' => sprintf('%01.2f', CRM_Utils_Rule::cleanMoney($contribution['total_amount'])), + $request = [ + 'refNumber' => $params['trxn_id'], + 'transactionAmount' => sprintf('%01.2f', CRM_Utils_Rule::cleanMoney($params['total_amount'])), ]; $options = [ @@ -462,42 +453,13 @@ public function doRefund($params = []) { $this->error($result); if (!empty($result['authResponse'] == 'ACCEPTED')) { - $payments = civicrm_api3('Payment', 'get', ['entity_id' => $params['contribution_id']]); - if (!empty($payments['count']) && $payments['count'] == 1) { - $paymentId = $payments['id']; - } - else { - $paymentId = FALSE; - } - $trxnsData = $params; - $trxnsData['is_send_contribution_notification'] = FALSE; - $trxnsData['total_amount'] = -$contribution['total_amount']; - $trxnsData['trxn_date'] = date('Y-m-d H:i:s'); - $trxnsData['trxn_id'] = $result['referenceNumber']; - $trxnsData['payment_processor_id'] = $this->_paymentProcessor['id']; - if (!empty($paymentId)) { - $trxnsData['id'] = $paymentId; - civicrm_api3('Payment', 'cancel', $trxnsData); - } - else { - civicrm_api3('Payment', 'create', $trxnsData); - } - - // If the Contribution total is now 0 set the status to be refunded. - if (((float) CRM_Core_BAO_FinancialTrxn::getTotalPayments($params['contribution_id'], TRUE) === 0.0)) { - civicrm_api3('Contribution', 'create', array( - 'id' => $params['contribution_id'], - 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refund'), // refund - 'is_post_payment_create' => TRUE, - 'cancel_date' => date('Y-m-d H:i:s'), - )); - } - - $params = array_merge($params, [ + $refundParams = [ 'refund_trxn_id' => $result['referenceNumber'], - 'refund_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refund'), - 'processor_result' => json_encode($result), - ]); + 'refund_status_id' => $refundStatus, + 'refund_status_name' => $refundStatusName, + 'processor_result' => $refund->jsonSerialize(), + ]; + return $refundParams; } return $params; diff --git a/CRM/Iats/Form/Refund.php b/CRM/Iats/Form/Refund.php index cf0d48b8..e406ed81 100644 --- a/CRM/Iats/Form/Refund.php +++ b/CRM/Iats/Form/Refund.php @@ -64,21 +64,53 @@ public function buildQuickForm() { ), ) ); - return; } public function postProcess() { - // FIXME: doesn't work for multiple iATS Processors - $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID); - if (!$processor->supportsRefund()) { - throw new API_Exception('Payment Processor does not support refund'); + // find the token for this contribution + try { + $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $this->_id)); + } + catch (CiviCRM_API3_Exception $e) { + // FIXME: display an error message or something ? + throw new \Civi\Payment\Exception\PaymentProcessorException($e->getMessage()); + } + + try { + $refundParams = [ + 'payment_processor_id' => $this->_paymentProcessorID, + 'amount' => $contribution['total_amount'], + 'currency' => $contribution['currency'], + 'trxn_id' => $contribution['trxn_id'], + ]; + $refund = civicrm_api3('PaymentProcessor', 'Refund', $refundParams)['values']; + if ($refund['refund_status_name'] === 'Completed') { + $payments = civicrm_api3('Payment', 'get', ['entity_id' => $params['contribution_id']]); + if (!empty($payments['count']) && !empty($payments['values'])) { + foreach ($payments['values'] as $payment) { + civicrm_api3('Payment', 'cancel', [ + 'id' => $payment['id'], + 'trxn_date' = date('Y-m-d H:i:s'), + ]); + } + } + } + $refundPaymentParams = [ + 'contribution_id' => $this->_id, + 'trxn_id' => $refund['refund_trxn_id'], + 'total_amount' => (-$contribution['total_amount']), + 'payment_processor_id' => $this->_paymentProcessorID, + ]; + $trxn = CRM_Financial_BAO_Payment::create($refundPaymentParams); + + CRM_Core_Session::setStatus(E::ts('Refund was processed successfully.'), 'Refund processed', 'success'); + + CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', + "reset=1&cid={$this->_contactID}&selectedChild=contribute" + )); + } catch (Exception $e) { + CRM_Core_Error::statusBounce($e->getMessage(), NULL, 'Refund failed'); } - $params = ['contribution_id' => $this->_id]; - $result = $processor->doRefund($params); - CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', - "reset=1&cid={$this->_contactID}&selectedChild=contribute" - )); - return; } }