The unit test to test & ensure this was failing to pick up that the records were not being created because it iterated
through the created rows & checked they were right but there was no check to ensure the rows were created :-(
This still leaves a gap I think for refunds not tied to a specific payment but that can be a later step
}
elseif ($params['total_amount'] < 0) {
$trxn = self::recordRefundPayment($params['contribution_id'], $params, FALSE);
+ if (!empty($params['cancelled_payment_id'])) {
+ // Do a direct reversal of any entity_financial_trxn records being cancelled.
+ $entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', [
+ 'entity_table' => 'civicrm_financial_item',
+ 'options' => ['limit' => 0],
+ 'financial_trxn_id.id' => $params['cancelled_payment_id'],
+ ])['values'];
+ foreach ($entityFinancialTrxns as $entityFinancialTrxn) {
+ civicrm_api3('EntityFinancialTrxn', 'create', [
+ 'entity_table' => 'civicrm_financial_item',
+ 'entity_id' => $entityFinancialTrxn['entity_id'],
+ 'amount' => -$entityFinancialTrxn['amount'],
+ 'financial_trxn_id' => $trxn->id,
+ ]);
+ }
+ }
}
if ($isPaymentCompletesContribution) {
* - deprecate this param
*
* @return CRM_Financial_DAO_FinancialTrxn
+ * @throws \CiviCRM_API3_Exception
*/
protected static function recordRefundPayment($contributionId, $trxnData, $updateStatus) {
list($contributionDAO, $params) = self::getContributionAndParamsInFormatForRecordFinancialTransaction($contributionId);
* @param array $params
* Input parameters.
*
- * @throws API_Exception
* @return array
* Api result array
+ *
+ * @throws \CiviCRM_API3_Exception
+ * @throws API_Exception
*/
function civicrm_api3_payment_cancel($params) {
$eftParams = [
'total_amount' => -$entity['amount'],
'contribution_id' => $entity['entity_id'],
'trxn_date' => CRM_Utils_Array::value('trxn_date', $params, 'now'),
+ 'cancelled_payment_id' => $params['id'],
];
foreach (['trxn_id', 'payment_instrument_id'] as $permittedParam) {
'financial_trxn_id' => $payment['id'] - 1,
];
- $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams);
+ $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $minParams)['values'];
+ $this->assertCount(2, $eft);
$amounts = [-33.33, -16.67];
- foreach ($eft['values'] as $value) {
+ foreach ($eft as $value) {
$this->assertEquals($value['amount'], array_pop($amounts));
}
'entity_table' => 'civicrm_financial_item',
'financial_trxn_id' => $payment['id'],
];
- $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
+ $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params)['values'];
$amounts = [66.67, 33.33];
- foreach ($eft['values'] as $value) {
+ foreach ($eft as $value) {
$this->assertEquals($value['amount'], array_pop($amounts));
}
$items = $this->callAPISuccess('FinancialItem', 'get', [])['values'];