3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 if (!empty($params['payment_processor'])) {
151 // I can't find evidence this is passed in - I was gonna just remove it but decided to deprecate as I see getToFinancialAccount
152 // also anticipates it.
153 CRM_Core_Error
::deprecatedFunctionWarning('passing payment_processor is deprecated - use payment_processor_id');
154 $params['payment_processor_id'] = $params['payment_processor'];
156 // Check if it is an update
157 if (!empty($params['id'])) {
158 $amount = $params['total_amount'];
159 civicrm_api3('Payment', 'cancel', $params);
160 $params['total_amount'] = $amount;
162 $trxn = CRM_Financial_BAO_Payment
::create($params);
165 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id
]);
166 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
170 * Adjust Metadata for Create action.
172 * The metadata is used for setting defaults, documentation & validation.
174 * @param array $params
175 * Array of parameters.
177 function _civicrm_api3_payment_create_spec(&$params) {
179 'contribution_id' => [
181 'title' => ts('Contribution ID'),
182 'type' => CRM_Utils_Type
::T_INT
,
183 // We accept order_id as an alias so that we can chain like
184 // civicrm_api3('Order', 'create', ['blah' => 'blah', 'contribution_status_id' => 'Pending', 'api.Payment.create => ['total_amount' => 5]]
185 'api.aliases' => ['order_id'],
189 'title' => ts('Total Payment Amount'),
190 'type' => CRM_Utils_Type
::T_FLOAT
,
192 'payment_processor_id' => [
193 'name' => 'payment_processor_id',
194 'type' => CRM_Utils_Type
::T_INT
,
195 'title' => ts('Payment Processor'),
196 'description' => ts('Payment Processor for this payment'),
197 'where' => 'civicrm_financial_trxn.payment_processor_id',
198 'table_name' => 'civicrm_financial_trxn',
199 'entity' => 'FinancialTrxn',
200 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
202 'FKClassName' => 'CRM_Financial_DAO_PaymentProcessor',
205 'title' => ts('Payment ID'),
206 'type' => CRM_Utils_Type
::T_INT
,
207 'api.aliases' => ['payment_id'],
210 'title' => ts('Payment Date'),
211 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
212 'api.default' => 'now',
213 'api.required' => TRUE,
215 'is_send_contribution_notification' => [
216 'title' => ts('Send out notifications based on contribution status change?'),
217 'description' => ts('Most commonly this equates to emails relating to the contribution, event, etcwhen a payment completes a contribution'),
218 'type' => CRM_Utils_Type
::T_BOOLEAN
,
219 'api.default' => TRUE,
221 'payment_instrument_id' => [
222 'name' => 'payment_instrument_id',
223 'type' => CRM_Utils_Type
::T_INT
,
224 'title' => ts('Payment Method'),
225 'description' => ts('FK to payment_instrument option group values'),
226 'where' => 'civicrm_financial_trxn.payment_instrument_id',
227 'table_name' => 'civicrm_financial_trxn',
228 'entity' => 'FinancialTrxn',
229 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
234 'pseudoconstant' => [
235 'optionGroupName' => 'payment_instrument',
236 'optionEditPath' => 'civicrm/admin/options/payment_instrument',
240 'name' => 'card_type_id',
241 'type' => CRM_Utils_Type
::T_INT
,
242 'title' => ts('Card Type ID'),
243 'description' => ts('FK to accept_creditcard option group values'),
244 'where' => 'civicrm_financial_trxn.card_type_id',
245 'table_name' => 'civicrm_financial_trxn',
246 'entity' => 'FinancialTrxn',
247 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
252 'pseudoconstant' => [
253 'optionGroupName' => 'accept_creditcard',
254 'optionEditPath' => 'civicrm/admin/options/accept_creditcard',
257 'trxn_result_code' => [
258 'name' => 'trxn_result_code',
259 'type' => CRM_Utils_Type
::T_STRING
,
260 'title' => ts('Transaction Result Code'),
261 'description' => ts('processor result code'),
263 'size' => CRM_Utils_Type
::HUGE
,
264 'where' => 'civicrm_financial_trxn.trxn_result_code',
265 'table_name' => 'civicrm_financial_trxn',
266 'entity' => 'FinancialTrxn',
267 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
272 'type' => CRM_Utils_Type
::T_STRING
,
273 'title' => ts('Transaction ID'),
274 'description' => ts('Transaction id supplied by external processor. This may not be unique.'),
277 'where' => 'civicrm_financial_trxn.trxn_id',
278 'table_name' => 'civicrm_financial_trxn',
279 'entity' => 'FinancialTrxn',
280 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
287 'name' => 'check_number',
288 'type' => CRM_Utils_Type
::T_STRING
,
289 'title' => ts('Check Number'),
290 'description' => ts('Check number'),
293 'where' => 'civicrm_financial_trxn.check_number',
294 'table_name' => 'civicrm_financial_trxn',
295 'entity' => 'FinancialTrxn',
296 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
302 'pan_truncation' => [
303 'name' => 'pan_truncation',
304 'type' => CRM_Utils_Type
::T_STRING
,
305 'title' => ts('Pan Truncation'),
306 'description' => ts('Last 4 digits of credit card'),
309 'where' => 'civicrm_financial_trxn.pan_truncation',
310 'table_name' => 'civicrm_financial_trxn',
311 'entity' => 'FinancialTrxn',
312 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
322 * Adjust Metadata for Get action.
324 * The metadata is used for setting defaults, documentation & validation.
326 * @param array $params
327 * Array of parameters determined by getfields.
329 function _civicrm_api3_payment_get_spec(&$params) {
331 'contribution_id' => [
332 'title' => ts('Contribution ID'),
333 'type' => CRM_Utils_Type
::T_INT
,
336 'title' => ts('Entity Table'),
337 'api.default' => 'civicrm_contribution',
340 'title' => ts('Entity ID'),
341 'type' => CRM_Utils_Type
::T_INT
,
342 'api.aliases' => ['contribution_id'],
345 'title' => ts('Transaction ID'),
346 'type' => CRM_Utils_Type
::T_STRING
,
349 'title' => ts('Payment Date'),
350 'type' => CRM_Utils_Type
::T_TIMESTAMP
,
356 * Adjust Metadata for Delete action.
358 * The metadata is used for setting defaults, documentation & validation.
360 * @param array $params
361 * Array of parameters.
363 function _civicrm_api3_payment_delete_spec(&$params) {
367 'title' => 'Payment ID',
368 'type' => CRM_Utils_Type
::T_INT
,
369 'api.aliases' => ['payment_id'],
375 * Adjust Metadata for Cancel action.
377 * The metadata is used for setting defaults, documentation & validation.
379 * @param array $params
380 * Array of parameters.
382 function _civicrm_api3_payment_cancel_spec(&$params) {
386 'title' => 'Payment ID',
387 'type' => CRM_Utils_Type
::T_INT
,
388 'api.aliases' => ['payment_id'],
391 'title' => 'Cancel Date',
392 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
398 * Send a payment confirmation.
400 * @param array $params
406 function civicrm_api3_payment_sendconfirmation($params) {
412 $input = array_intersect_key($params, array_flip($allowedParams));
413 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
414 $result = CRM_Financial_BAO_Payment
::sendConfirmation($input);
415 return civicrm_api3_create_success([
417 'is_sent' => $result[0],
418 'subject' => $result[1],
419 'message_txt' => $result[2],
420 'message_html' => $result[3],
426 * Adjust Metadata for sendconfirmation action.
428 * The metadata is used for setting defaults, documentation & validation.
430 * @param array $params
431 * Array of parameters determined by getfields.
433 function _civicrm_api3_payment_sendconfirmation_spec(&$params) {
436 'title' => ts('Payment ID'),
437 'type' => CRM_Utils_Type
::T_INT
,
439 $params['from_email_address'] = [
440 'title' => ts('From email; an email string or the id of a valid email'),
441 'type' => CRM_Utils_Type
::T_STRING
,
443 $params['is_send_contribution_notification'] = [
444 'title' => ts('Send any event or contribution confirmations triggered by this payment'),
445 'description' => ts('If this payment completes a contribution it may mean receipts will go out according to busines logic if thie is set to TRUE'),
446 'type' => CRM_Utils_Type
::T_BOOLEAN
,