diff --git a/api/v3/Payment.php b/api/v3/Payment.php index a4292a5b6b71..584405df5abc 100644 --- a/api/v3/Payment.php +++ b/api/v3/Payment.php @@ -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); } /** @@ -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, + ], + ]; } /** @@ -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, + ], + ]; } /**