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 Feb 25, 2019
1 parent 4bbf7b9 commit 1c61226
Showing 1 changed file with 36 additions and 23 deletions.
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', $trxn);
}

/**
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 1c61226

Please sign in to comment.