Skip to content

Commit

Permalink
code to handle financial records after successful refund
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Aug 11, 2021
1 parent bd2281e commit 6705570
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 58 deletions.
56 changes: 9 additions & 47 deletions CRM/Core/Payment/Faps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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;
Expand Down
54 changes: 43 additions & 11 deletions CRM/Iats/Form/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit 6705570

Please sign in to comment.