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 Mar 8, 2019
1 parent 7aa9abe commit d529efb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
5 changes: 4 additions & 1 deletion 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
59 changes: 36 additions & 23 deletions api/v3/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,26 @@ function civicrm_api3_payment_delete(&$params) {
* @return array
* Api result array
*/
function civicrm_api3_payment_cancel(&$params) {
function civicrm_api3_payment_cancel($params) {
$eftParams = array(
'entity_table' => 'civicrm_contribution',
'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 = array();
_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 @@ -148,28 +153,32 @@ function civicrm_api3_payment_create(&$params) {
* Array of parameters.
*/
function _civicrm_api3_payment_create_spec(&$params) {
$params = array(
'contribution_id' => array(
$params = [
'contribution_id' => [
'api.required' => 1 ,
'title' => 'Contribution ID',
'type' => CRM_Utils_Type::T_INT,
),
'total_amount' => array(
],
'total_amount' => [
'api.required' => 1 ,
'title' => 'Total Payment Amount',
'type' => CRM_Utils_Type::T_FLOAT,
),
'payment_processor_id' => array(
],
'payment_processor_id' => [
'title' => 'Payment Processor ID',
'type' => CRM_Utils_Type::T_INT,
'description' => ts('Payment processor ID - required for payment processor payments'),
),
'id' => array(
],
'id' => [
'title' => 'Payment ID',
'type' => CRM_Utils_Type::T_INT,
'api.aliases' => array('payment_id'),
),
);
],
'trxn_date' => [
'title' => 'Cancel Date',
'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
],
];
}

/**
Expand Down Expand Up @@ -226,14 +235,18 @@ function _civicrm_api3_payment_delete_spec(&$params) {
* Array of parameters.
*/
function _civicrm_api3_payment_cancel_spec(&$params) {
$params = array(
'id' => array(
$params = [
'id' => [
'api.required' => 1 ,
'title' => 'Payment ID',
'type' => CRM_Utils_Type::T_INT,
'api.aliases' => array('payment_id'),
),
);
'api.aliases' => ['payment_id'],
],
'trxn_date' => [
'title' => 'Cancel Date',
'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
],
];
}

/**
Expand Down

0 comments on commit d529efb

Please sign in to comment.