Skip to content

Commit

Permalink
Call Payment.create from payment.cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Apr 1, 2019
1 parent 46e0ade commit 2561fc1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -3339,6 +3339,9 @@ public static function recordFinancialAccounts(&$params, $financialTrxnValues =
'net_amount' => CRM_Utils_Array::value('net_amount', $params, $totalAmount),
'currency' => $params['contribution']->currency,
'trxn_id' => $params['contribution']->trxn_id,
// @todo - this is getting the status id from the contribution - that is BAD - ie the contribution could be partially
// paid but each payment is completed. The work around is to pass in the status_id in the trxn_params but
// this should really default to completed (after discussion).
'status_id' => $statusId,
'payment_instrument_id' => CRM_Utils_Array::value('payment_instrument_id', $params, $params['contribution']->payment_instrument_id),
'check_number' => CRM_Utils_Array::value('check_number', $params),
Expand Down Expand Up @@ -3893,6 +3896,7 @@ public static function recordAdditionalPayment($contributionId, $trxnsData, $pay
$financialTrxn = CRM_Financial_BAO_Payment::recordPayment($contributionId, $trxnsData, $participantId);
}
elseif ($paymentType == 'refund') {
$trxnsData['total_amount'] = -$trxnsData['total_amount'];
$financialTrxn = CRM_Financial_BAO_Payment::recordRefundPayment($contributionId, $trxnsData, $updateStatus);
if ($participantId) {
// update participant status
Expand Down
7 changes: 5 additions & 2 deletions CRM/Financial/BAO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function create($params) {
// should be handled through Payment.create.
$isSkipRecordingPaymentHereForLegacyHandlingReasons = ($contributionStatus == 'Pending' && $isPaymentCompletesContribution);

if (!$isSkipRecordingPaymentHereForLegacyHandlingReasons) {
if (!$isSkipRecordingPaymentHereForLegacyHandlingReasons && $params['total_amount'] > 0) {
$trxn = CRM_Contribute_BAO_Contribution::recordPartialPayment($contribution, $params);

if (CRM_Utils_Array::value('line_item', $params) && !empty($trxn)) {
Expand Down Expand Up @@ -99,6 +99,9 @@ public static function create($params) {
CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution['total_amount']);
}
}
elseif ($params['total_amount'] < 0) {
$trxn = self::recordRefundPayment($params['contribution_id'], $params, FALSE);
}

if ($isPaymentCompletesContribution) {
civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id']));
Expand Down Expand Up @@ -310,7 +313,7 @@ public static function recordRefundPayment($contributionId, $trxnData, $updateSt
$arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is');
$completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');

$trxnData['total_amount'] = $trxnData['net_amount'] = -$trxnData['total_amount'];
$trxnData['total_amount'] = $trxnData['net_amount'] = $trxnData['total_amount'];
$trxnData['from_financial_account_id'] = $arAccountId;
$trxnData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');
// record the entry
Expand Down
27 changes: 20 additions & 7 deletions api/v3/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,20 @@ function civicrm_api3_payment_cancel(&$params) {
'financial_trxn_id' => $params['id'],
];
$entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
$contributionId = $entity['entity_id'];
$params['total_amount'] = $entity['amount'];
unset($params['id']);

$trxn = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionId, $params, 'refund', NULL, FALSE);
$paymentParams = [
'total_amount' => -$entity['amount'],
'contribution_id' => $entity['entity_id'],
'trxn_date' => CRM_Utils_Array::value('trxn_date', $params, 'now'),
];

$values = [];
_civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]);
return civicrm_api3_create_success($values, $params, 'Payment', 'cancel', $trxn);
foreach (['trxn_id', 'payment_instrument_id'] as $permittedParam) {
if (isset($params[$permittedParam])) {
$paymentParams[$permittedParam] = $params[$permittedParam];
}
}
$result = civicrm_api3('Payment', 'create', $paymentParams);
return civicrm_api3_create_success($result['values'], $params, 'Payment', 'cancel');
}

/**
Expand Down Expand Up @@ -169,6 +174,10 @@ function _civicrm_api3_payment_create_spec(&$params) {
'type' => CRM_Utils_Type::T_INT,
'api.aliases' => ['payment_id'],
],
'trxn_date' => [
'title' => 'Cancel Date',
'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
],
];
}

Expand Down Expand Up @@ -233,6 +242,10 @@ function _civicrm_api3_payment_cancel_spec(&$params) {
'type' => CRM_Utils_Type::T_INT,
'api.aliases' => ['payment_id'],
],
'trxn_date' => [
'title' => 'Cancel Date',
'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
],
];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/api/v3/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function testRefundEmailReceipt($thousandSeparator) {
'from_financial_account_id' => 7,
'to_financial_account_id' => 6,
'total_amount' => -30,
'status_id' => 1,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'status_id', 'Refunded'),
'is_payment' => 1,
];
foreach ($expected as $key => $value) {
Expand Down Expand Up @@ -317,7 +317,7 @@ public function testCreatePaymentNoLineItems() {
*/
public function checkPaymentResult($payment, $expectedResult) {
foreach ($expectedResult[$payment['id']] as $key => $value) {
$this->assertEquals($payment['values'][$payment['id']][$key], $value);
$this->assertEquals($payment['values'][$payment['id']][$key], $value, 'mismatch on ' . $key);
}
}

Expand Down

0 comments on commit 2561fc1

Please sign in to comment.