forked from iATSPayments/com.iatspayments.civicrm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support refund for iATS 1stPay CC and iATS EFT/ACH
- Loading branch information
Showing
8 changed files
with
273 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
use CRM_Iats_ExtensionUtil as E; | ||
|
||
/** | ||
* Form controller class | ||
* | ||
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference | ||
*/ | ||
class CRM_Iats_Form_Refund extends CRM_Core_Form { | ||
|
||
/** | ||
* contact ID | ||
* @var object | ||
*/ | ||
protected $_contactID; | ||
|
||
/** | ||
* Test or live mode | ||
* @var object | ||
*/ | ||
protected $_isTest; | ||
|
||
protected $_paymentProcessorID; | ||
|
||
|
||
/** | ||
* Set variables up before form is built. | ||
*/ | ||
public function preProcess() { | ||
// Check permission for action. | ||
if (!CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) { | ||
// @todo replace with throw new CRM_Core_Exception(). | ||
CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); | ||
} | ||
|
||
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); | ||
$this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); | ||
|
||
$this->_paymentProcessorID = E::getPaymentProcessorByContributionID($this->_id); | ||
if (!$this->_paymentProcessorID) { | ||
CRM_Core_Error::statusBounce(ts('Payment processor not found')); | ||
} | ||
parent::preProcess(); | ||
|
||
$this->_isTest = 0; | ||
if ($this->_action & CRM_Core_Action::PREVIEW) { | ||
$this->_isTest = 1; | ||
} | ||
} | ||
|
||
public function buildQuickForm() { | ||
$this->addButtons( | ||
array( | ||
array( | ||
'type' => 'next', | ||
'name' => ts('Refund'), | ||
'spacing' => ' ', | ||
'isDefault' => TRUE, | ||
), | ||
array( | ||
'type' => 'cancel', | ||
'name' => ts('Cancel'), | ||
), | ||
) | ||
); | ||
} | ||
|
||
public function postProcess() { | ||
// 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' => (-1 * $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'); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
<div class="crm-block crm-form-block crm-contribution-form-block"> | ||
<div class="messages status no-popup"> | ||
<div class="icon inform-icon"></div> | ||
{ts}You are about to refund the total amount of this contribution using iATS.{/ts} {ts}Do you want to continue?{/ts} | ||
</div> | ||
</div> | ||
|
||
<div class="crm-submit-buttons"> | ||
{include file="CRM/common/formButtons.tpl"} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters