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']) && !empty($params['options']['limit'])) {
47 $limit = CRM_Utils_Array
::value('limit', $params['options']);
49 $params['options']['limit'] = 0;
50 if (isset($params['trxn_id'])) {
51 $params['financial_trxn_id.trxn_id'] = $params['trxn_id'];
53 $eft = civicrm_api3('EntityFinancialTrxn', 'get', $params);
54 if (!empty($eft['values'])) {
56 foreach ($eft['values'] as $efts) {
57 if (empty($efts['financial_trxn_id'])) {
60 $eftIds[] = $efts['financial_trxn_id'];
61 $map[$efts['financial_trxn_id']] = $efts['entity_id'];
63 if (!empty($eftIds)) {
65 'id' => ['IN' => $eftIds],
69 $ftParams['options']['limit'] = $limit;
71 $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
72 foreach ($financialTrxn['values'] as &$values) {
73 $values['contribution_id'] = $map[$values['id']];
77 return civicrm_api3_create_success(CRM_Utils_Array
::value('values', $financialTrxn, []), $params, 'Payment', 'get');
83 * @param array $params
86 * @throws API_Exception
90 function civicrm_api3_payment_delete($params) {
91 return civicrm_api3('FinancialTrxn', 'delete', $params);
95 * Cancel/Refund a payment for a Contribution.
97 * @param array $params
103 * @throws \CiviCRM_API3_Exception
104 * @throws API_Exception
106 function civicrm_api3_payment_cancel($params) {
108 'entity_table' => 'civicrm_contribution',
109 'financial_trxn_id' => $params['id'],
111 $entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
114 'total_amount' => -$entity['amount'],
115 'contribution_id' => $entity['entity_id'],
116 'trxn_date' => CRM_Utils_Array
::value('trxn_date', $params, 'now'),
117 'cancelled_payment_id' => $params['id'],
120 foreach (['trxn_id', 'payment_instrument_id'] as $permittedParam) {
121 if (isset($params[$permittedParam])) {
122 $paymentParams[$permittedParam] = $params[$permittedParam];
125 $result = civicrm_api3('Payment', 'create', $paymentParams);
126 return civicrm_api3_create_success($result['values'], $params, 'Payment', 'cancel');
130 * Add a payment for a Contribution.
132 * @param array $params
138 * @throws \API_Exception
139 * @throws \CRM_Core_Exception
140 * @throws \CiviCRM_API3_Exception
142 function civicrm_api3_payment_create($params) {
143 if (empty($params['skipCleanMoney'])) {
144 foreach (['total_amount', 'net_amount', 'fee_amount'] as $field) {
145 if (isset($params[$field])) {
146 $params[$field] = CRM_Utils_Rule
::cleanMoney($params[$field]);
150 // Check if it is an update
151 if (!empty($params['id'])) {
152 $amount = $params['total_amount'];
153 civicrm_api3('Payment', 'cancel', $params);
154 $params['total_amount'] = $amount;
156 $trxn = CRM_Financial_BAO_Payment
::create($params);
159 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id
]);
160 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
164 * Adjust Metadata for Create action.
166 * The metadata is used for setting defaults, documentation & validation.
168 * @param array $params
169 * Array of parameters.
171 function _civicrm_api3_payment_create_spec(&$params) {
173 'contribution_id' => [
175 'title' => ts('Contribution ID'),
176 'type' => CRM_Utils_Type
::T_INT
,
177 // We accept order_id as an alias so that we can chain like
178 // civicrm_api3('Order', 'create', ['blah' => 'blah', 'contribution_status_id' => 'Pending', 'api.Payment.create => ['total_amount' => 5]]
179 'api.aliases' => ['order_id'],
183 'title' => ts('Total Payment Amount'),
184 'type' => CRM_Utils_Type
::T_FLOAT
,
186 'payment_processor_id' => [
187 'name' => 'payment_processor_id',
188 'type' => CRM_Utils_Type
::T_INT
,
189 'title' => ts('Payment Processor'),
190 'description' => ts('Payment Processor for this payment'),
191 'where' => 'civicrm_financial_trxn.payment_processor_id',
192 'table_name' => 'civicrm_financial_trxn',
193 'entity' => 'FinancialTrxn',
194 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
196 'FKClassName' => 'CRM_Financial_DAO_PaymentProcessor',
199 'title' => ts('Payment ID'),
200 'type' => CRM_Utils_Type
::T_INT
,
201 'api.aliases' => ['payment_id'],
204 'title' => ts('Cancel Date'),
205 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
207 'is_send_contribution_notification' => [
208 'title' => ts('Send out notifications based on contribution status change?'),
209 'description' => ts('Most commonly this equates to emails relating to the contribution, event, etcwhen a payment completes a contribution'),
210 'type' => CRM_Utils_Type
::T_BOOLEAN
,
211 'api.default' => TRUE,
213 'payment_instrument_id' => [
214 'name' => 'payment_instrument_id',
215 'type' => CRM_Utils_Type
::T_INT
,
216 'title' => ts('Payment Method'),
217 'description' => ts('FK to payment_instrument option group values'),
218 'where' => 'civicrm_financial_trxn.payment_instrument_id',
219 'table_name' => 'civicrm_financial_trxn',
220 'entity' => 'FinancialTrxn',
221 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
226 'pseudoconstant' => [
227 'optionGroupName' => 'payment_instrument',
228 'optionEditPath' => 'civicrm/admin/options/payment_instrument',
232 'name' => 'card_type_id',
233 'type' => CRM_Utils_Type
::T_INT
,
234 'title' => ts('Card Type ID'),
235 'description' => ts('FK to accept_creditcard option group values'),
236 'where' => 'civicrm_financial_trxn.card_type_id',
237 'table_name' => 'civicrm_financial_trxn',
238 'entity' => 'FinancialTrxn',
239 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
244 'pseudoconstant' => [
245 'optionGroupName' => 'accept_creditcard',
246 'optionEditPath' => 'civicrm/admin/options/accept_creditcard',
249 'trxn_result_code' => [
250 'name' => 'trxn_result_code',
251 'type' => CRM_Utils_Type
::T_STRING
,
252 'title' => ts('Transaction Result Code'),
253 'description' => ts('processor result code'),
255 'size' => CRM_Utils_Type
::HUGE
,
256 'where' => 'civicrm_financial_trxn.trxn_result_code',
257 'table_name' => 'civicrm_financial_trxn',
258 'entity' => 'FinancialTrxn',
259 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
264 'type' => CRM_Utils_Type
::T_STRING
,
265 'title' => ts('Transaction ID'),
266 'description' => ts('Transaction id supplied by external processor. This may not be unique.'),
269 'where' => 'civicrm_financial_trxn.trxn_id',
270 'table_name' => 'civicrm_financial_trxn',
271 'entity' => 'FinancialTrxn',
272 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
279 'name' => 'check_number',
280 'type' => CRM_Utils_Type
::T_STRING
,
281 'title' => ts('Check Number'),
282 'description' => ts('Check number'),
285 'where' => 'civicrm_financial_trxn.check_number',
286 'table_name' => 'civicrm_financial_trxn',
287 'entity' => 'FinancialTrxn',
288 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
294 'pan_truncation' => [
295 'name' => 'pan_truncation',
296 'type' => CRM_Utils_Type
::T_STRING
,
297 'title' => ts('Pan Truncation'),
298 'description' => ts('Last 4 digits of credit card'),
301 'where' => 'civicrm_financial_trxn.pan_truncation',
302 'table_name' => 'civicrm_financial_trxn',
303 'entity' => 'FinancialTrxn',
304 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
314 * Adjust Metadata for Get action.
316 * The metadata is used for setting defaults, documentation & validation.
318 * @param array $params
319 * Array of parameters determined by getfields.
321 function _civicrm_api3_payment_get_spec(&$params) {
323 'contribution_id' => [
324 'title' => 'Contribution ID',
325 'type' => CRM_Utils_Type
::T_INT
,
328 'title' => 'Entity Table',
329 'api.default' => 'civicrm_contribution',
332 'title' => 'Entity ID',
333 'type' => CRM_Utils_Type
::T_INT
,
334 'api.aliases' => ['contribution_id'],
337 'title' => 'Transaction ID',
338 'type' => CRM_Utils_Type
::T_STRING
,
344 * Adjust Metadata for Delete action.
346 * The metadata is used for setting defaults, documentation & validation.
348 * @param array $params
349 * Array of parameters.
351 function _civicrm_api3_payment_delete_spec(&$params) {
355 'title' => 'Payment ID',
356 'type' => CRM_Utils_Type
::T_INT
,
357 'api.aliases' => ['payment_id'],
363 * Adjust Metadata for Cancel action.
365 * The metadata is used for setting defaults, documentation & validation.
367 * @param array $params
368 * Array of parameters.
370 function _civicrm_api3_payment_cancel_spec(&$params) {
374 'title' => 'Payment ID',
375 'type' => CRM_Utils_Type
::T_INT
,
376 'api.aliases' => ['payment_id'],
379 'title' => 'Cancel Date',
380 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
386 * Send a payment confirmation.
388 * @param array $params
394 function civicrm_api3_payment_sendconfirmation($params) {
400 $input = array_intersect_key($params, array_flip($allowedParams));
401 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
402 $result = CRM_Financial_BAO_Payment
::sendConfirmation($input);
403 return civicrm_api3_create_success([
405 'is_sent' => $result[0],
406 'subject' => $result[1],
407 'message_txt' => $result[2],
408 'message_html' => $result[3],
414 * Adjust Metadata for sendconfirmation action.
416 * The metadata is used for setting defaults, documentation & validation.
418 * @param array $params
419 * Array of parameters determined by getfields.
421 function _civicrm_api3_payment_sendconfirmation_spec(&$params) {
424 'title' => ts('Payment ID'),
425 'type' => CRM_Utils_Type
::T_INT
,
427 $params['from_email_address'] = [
428 'title' => ts('From email; an email string or the id of a valid email'),
429 'type' => CRM_Utils_Type
::T_STRING
,