3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM Contribution Payment records.
31 * @package CiviCRM_APIv3
35 * Retrieve a set of financial transactions which are payments.
37 * @param array $params
41 * Array of financial transactions which are payments, if error an array with an error id and error message
43 function civicrm_api3_payment_get($params) {
46 if (isset($params['options']) && CRM_Utils_Array
::value('limit', $params['options'])) {
47 $limit = CRM_Utils_Array
::value('limit', $params['options']);
49 $params['options']['limit'] = 0;
50 $eft = civicrm_api3('EntityFinancialTrxn', 'get', $params);
51 if (!empty($eft['values'])) {
53 foreach ($eft['values'] as $efts) {
54 if (empty($efts['financial_trxn_id'])) {
57 $eftIds[] = $efts['financial_trxn_id'];
58 $map[$efts['financial_trxn_id']] = $efts['entity_id'];
60 if (!empty($eftIds)) {
62 'id' => ['IN' => $eftIds],
66 $ftParams['options']['limit'] = $limit;
68 $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
69 foreach ($financialTrxn['values'] as &$values) {
70 $values['contribution_id'] = $map[$values['id']];
74 return civicrm_api3_create_success(CRM_Utils_Array
::value('values', $financialTrxn, []), $params, 'Payment', 'get');
80 * @param array $params
83 * @throws API_Exception
87 function civicrm_api3_payment_delete($params) {
88 return civicrm_api3('FinancialTrxn', 'delete', $params);
92 * Cancel/Refund a payment for a Contribution.
94 * @param array $params
97 * @throws API_Exception
101 function civicrm_api3_payment_cancel($params) {
103 'entity_table' => 'civicrm_contribution',
104 'financial_trxn_id' => $params['id'],
106 $entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
109 'total_amount' => -$entity['amount'],
110 'contribution_id' => $entity['entity_id'],
111 'trxn_date' => CRM_Utils_Array
::value('trxn_date', $params, 'now'),
114 foreach (['trxn_id', 'payment_instrument_id'] as $permittedParam) {
115 if (isset($params[$permittedParam])) {
116 $paymentParams[$permittedParam] = $params[$permittedParam];
119 $result = civicrm_api3('Payment', 'create', $paymentParams);
120 return civicrm_api3_create_success($result['values'], $params, 'Payment', 'cancel');
124 * Add a payment for a Contribution.
126 * @param array $params
129 * @throws API_Exception
133 function civicrm_api3_payment_create($params) {
134 // Check if it is an update
135 if (CRM_Utils_Array
::value('id', $params)) {
136 $amount = $params['total_amount'];
137 civicrm_api3('Payment', 'cancel', $params);
138 $params['total_amount'] = $amount;
140 $trxn = CRM_Financial_BAO_Payment
::create($params);
143 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id
]);
144 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
148 * Adjust Metadata for Create action.
150 * The metadata is used for setting defaults, documentation & validation.
152 * @param array $params
153 * Array of parameters.
155 function _civicrm_api3_payment_create_spec(&$params) {
157 'contribution_id' => [
159 'title' => ts('Contribution ID'),
160 'type' => CRM_Utils_Type
::T_INT
,
164 'title' => ts('Total Payment Amount'),
165 'type' => CRM_Utils_Type
::T_FLOAT
,
167 'payment_processor_id' => [
168 'title' => ts('Payment Processor ID'),
169 'type' => CRM_Utils_Type
::T_INT
,
170 'description' => ts('Payment processor ID - required for payment processor payments'),
173 'title' => ts('Payment ID'),
174 'type' => CRM_Utils_Type
::T_INT
,
175 'api.aliases' => ['payment_id'],
178 'title' => ts('Cancel Date'),
179 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
185 * Adjust Metadata for Get action.
187 * The metadata is used for setting defaults, documentation & validation.
189 * @param array $params
190 * Array of parameters determined by getfields.
192 function _civicrm_api3_payment_get_spec(&$params) {
194 'contribution_id' => [
195 'title' => 'Contribution ID',
196 'type' => CRM_Utils_Type
::T_INT
,
199 'title' => 'Entity Table',
200 'api.default' => 'civicrm_contribution',
203 'title' => 'Entity ID',
204 'type' => CRM_Utils_Type
::T_INT
,
205 'api.aliases' => ['contribution_id'],
211 * Adjust Metadata for Delete action.
213 * The metadata is used for setting defaults, documentation & validation.
215 * @param array $params
216 * Array of parameters.
218 function _civicrm_api3_payment_delete_spec(&$params) {
222 'title' => 'Payment ID',
223 'type' => CRM_Utils_Type
::T_INT
,
224 'api.aliases' => ['payment_id'],
230 * Adjust Metadata for Cancel action.
232 * The metadata is used for setting defaults, documentation & validation.
234 * @param array $params
235 * Array of parameters.
237 function _civicrm_api3_payment_cancel_spec(&$params) {
241 'title' => 'Payment ID',
242 'type' => CRM_Utils_Type
::T_INT
,
243 'api.aliases' => ['payment_id'],
246 'title' => 'Cancel Date',
247 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
253 * Send a payment confirmation.
255 * @param array $params
261 function civicrm_api3_payment_sendconfirmation($params) {
263 'receipt_from_email',
270 $input = array_intersect_key($params, array_flip($allowedParams));
271 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
272 $result = CRM_Financial_BAO_Payment
::sendConfirmation($input);
273 return civicrm_api3_create_success([
275 'is_sent' => $result[0],
276 'subject' => $result[1],
277 'message_txt' => $result[2],
278 'message_html' => $result[3],
284 * Adjust Metadata for sendconfirmation action.
286 * The metadata is used for setting defaults, documentation & validation.
288 * @param array $params
289 * Array of parameters determined by getfields.
291 function _civicrm_api3_payment_sendconfirmation_spec(&$params) {
294 'title' => ts('Payment ID'),
295 'type' => CRM_Utils_Type
::T_INT
,