[REF] Replace CRM_Utils_Array::value with ?? in variable assignments
[civicrm-core.git] / api / v3 / Payment.php
CommitLineData
d2545e26
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
d2545e26 5 | |
a30c801b
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
d2545e26
PN
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * This api exposes CiviCRM Contribution Payment records.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18/**
19 * Retrieve a set of financial transactions which are payments.
20 *
21 * @param array $params
22 * Input parameters.
23 *
24 * @return array
25 * Array of financial transactions which are payments, if error an array with an error id and error message
f5269434 26 *
27 * @throws \CiviCRM_API3_Exception
d2545e26
PN
28 */
29function civicrm_api3_payment_get($params) {
cf8f0fff 30 $financialTrxn = [];
cf33c6cb 31 $limit = '';
de6c59ca 32 if (isset($params['options']) && !empty($params['options']['limit'])) {
f748c073 33 $limit = $params['options']['limit'] ?? NULL;
cf33c6cb
E
34 }
35 $params['options']['limit'] = 0;
c4204541 36 if (isset($params['trxn_id'])) {
37 $params['financial_trxn_id.trxn_id'] = $params['trxn_id'];
38 }
f5269434 39 $eftParams = $params;
40 unset($eftParams['return']);
41 // @todo - why do we fetch EFT params at all?
42 $eft = civicrm_api3('EntityFinancialTrxn', 'get', $eftParams);
d2545e26 43 if (!empty($eft['values'])) {
cf8f0fff 44 $eftIds = [];
d2545e26 45 foreach ($eft['values'] as $efts) {
2ff2ff26
PN
46 if (empty($efts['financial_trxn_id'])) {
47 continue;
48 }
d2545e26
PN
49 $eftIds[] = $efts['financial_trxn_id'];
50 $map[$efts['financial_trxn_id']] = $efts['entity_id'];
51 }
2ff2ff26 52 if (!empty($eftIds)) {
cf8f0fff
CW
53 $ftParams = [
54 'id' => ['IN' => $eftIds],
2ff2ff26 55 'is_payment' => 1,
cf8f0fff 56 ];
2ff2ff26
PN
57 if ($limit) {
58 $ftParams['options']['limit'] = $limit;
59 }
60 $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
61 foreach ($financialTrxn['values'] as &$values) {
62 $values['contribution_id'] = $map[$values['id']];
63 }
d2545e26
PN
64 }
65 }
cf8f0fff 66 return civicrm_api3_create_success(CRM_Utils_Array::value('values', $financialTrxn, []), $params, 'Payment', 'get');
d2545e26
PN
67}
68
69/**
70 * Delete a payment.
71 *
72 * @param array $params
73 * Input parameters.
74 *
d2545e26
PN
75 * @return array
76 * Api result array
f5269434 77 *
78 * @throws \CiviCRM_API3_Exception
d2545e26 79 */
1c31aa41 80function civicrm_api3_payment_delete($params) {
d2545e26
PN
81 return civicrm_api3('FinancialTrxn', 'delete', $params);
82}
83
84/**
85 * Cancel/Refund a payment for a Contribution.
86 *
87 * @param array $params
88 * Input parameters.
89 *
d2545e26
PN
90 * @return array
91 * Api result array
df29ccff 92 *
93 * @throws \CiviCRM_API3_Exception
94 * @throws API_Exception
d2545e26 95 */
1c31aa41 96function civicrm_api3_payment_cancel($params) {
cf8f0fff 97 $eftParams = [
d2545e26
PN
98 'entity_table' => 'civicrm_contribution',
99 'financial_trxn_id' => $params['id'],
cf8f0fff 100 ];
d2545e26 101 $entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
d2545e26 102
2561fc11 103 $paymentParams = [
104 'total_amount' => -$entity['amount'],
105 'contribution_id' => $entity['entity_id'],
106 'trxn_date' => CRM_Utils_Array::value('trxn_date', $params, 'now'),
df29ccff 107 'cancelled_payment_id' => $params['id'],
2561fc11 108 ];
d2545e26 109
2561fc11 110 foreach (['trxn_id', 'payment_instrument_id'] as $permittedParam) {
111 if (isset($params[$permittedParam])) {
112 $paymentParams[$permittedParam] = $params[$permittedParam];
113 }
114 }
115 $result = civicrm_api3('Payment', 'create', $paymentParams);
116 return civicrm_api3_create_success($result['values'], $params, 'Payment', 'cancel');
d2545e26
PN
117}
118
119/**
120 * Add a payment for a Contribution.
121 *
122 * @param array $params
123 * Input parameters.
124 *
d2545e26
PN
125 * @return array
126 * Api result array
a494d7a3 127 *
a494d7a3 128 * @throws \CRM_Core_Exception
129 * @throws \CiviCRM_API3_Exception
d2545e26 130 */
1c31aa41 131function civicrm_api3_payment_create($params) {
b4c48831 132 if (empty($params['skipCleanMoney'])) {
133 foreach (['total_amount', 'net_amount', 'fee_amount'] as $field) {
134 if (isset($params[$field])) {
135 $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
136 }
137 }
138 }
6cc6cb8c 139 if (!empty($params['payment_processor'])) {
140 // I can't find evidence this is passed in - I was gonna just remove it but decided to deprecate as I see getToFinancialAccount
141 // also anticipates it.
142 CRM_Core_Error::deprecatedFunctionWarning('passing payment_processor is deprecated - use payment_processor_id');
143 $params['payment_processor_id'] = $params['payment_processor'];
144 }
d2545e26 145 // Check if it is an update
de6c59ca 146 if (!empty($params['id'])) {
d2545e26
PN
147 $amount = $params['total_amount'];
148 civicrm_api3('Payment', 'cancel', $params);
149 $params['total_amount'] = $amount;
150 }
0c9b306a 151 $trxn = CRM_Financial_BAO_Payment::create($params);
5625fdf0 152
cf8f0fff 153 $values = [];
d2545e26
PN
154 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]);
155 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
156}
157
158/**
159 * Adjust Metadata for Create action.
160 *
161 * The metadata is used for setting defaults, documentation & validation.
162 *
163 * @param array $params
164 * Array of parameters.
165 */
166function _civicrm_api3_payment_create_spec(&$params) {
cf8f0fff
CW
167 $params = [
168 'contribution_id' => [
7c31ae57 169 'api.required' => 1,
d1987324 170 'title' => ts('Contribution ID'),
d2545e26 171 'type' => CRM_Utils_Type::T_INT,
e2887a3c 172 // We accept order_id as an alias so that we can chain like
173 // civicrm_api3('Order', 'create', ['blah' => 'blah', 'contribution_status_id' => 'Pending', 'api.Payment.create => ['total_amount' => 5]]
174 'api.aliases' => ['order_id'],
cf8f0fff
CW
175 ],
176 'total_amount' => [
7c31ae57 177 'api.required' => 1,
d1987324 178 'title' => ts('Total Payment Amount'),
d2545e26 179 'type' => CRM_Utils_Type::T_FLOAT,
cf8f0fff 180 ],
f5269434 181 'fee_amount' => [
182 'title' => ts('Fee Amount'),
183 'type' => CRM_Utils_Type::T_FLOAT,
184 ],
cf8f0fff 185 'payment_processor_id' => [
a494d7a3 186 'name' => 'payment_processor_id',
d2545e26 187 'type' => CRM_Utils_Type::T_INT,
a494d7a3 188 'title' => ts('Payment Processor'),
189 'description' => ts('Payment Processor for this payment'),
190 'where' => 'civicrm_financial_trxn.payment_processor_id',
191 'table_name' => 'civicrm_financial_trxn',
192 'entity' => 'FinancialTrxn',
193 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
194 'localizable' => 0,
195 'FKClassName' => 'CRM_Financial_DAO_PaymentProcessor',
cf8f0fff
CW
196 ],
197 'id' => [
d1987324 198 'title' => ts('Payment ID'),
d2545e26 199 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
200 'api.aliases' => ['payment_id'],
201 ],
2561fc11 202 'trxn_date' => [
4c68d684 203 'title' => ts('Payment Date'),
2561fc11 204 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
4c68d684 205 'api.default' => 'now',
206 'api.required' => TRUE,
2561fc11 207 ],
bd981689 208 'is_send_contribution_notification' => [
209 'title' => ts('Send out notifications based on contribution status change?'),
210 'description' => ts('Most commonly this equates to emails relating to the contribution, event, etcwhen a payment completes a contribution'),
211 'type' => CRM_Utils_Type::T_BOOLEAN,
212 'api.default' => TRUE,
213 ],
a494d7a3 214 'payment_instrument_id' => [
215 'name' => 'payment_instrument_id',
216 'type' => CRM_Utils_Type::T_INT,
217 'title' => ts('Payment Method'),
218 'description' => ts('FK to payment_instrument option group values'),
219 'where' => 'civicrm_financial_trxn.payment_instrument_id',
220 'table_name' => 'civicrm_financial_trxn',
221 'entity' => 'FinancialTrxn',
222 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
223 'localizable' => 0,
224 'html' => [
225 'type' => 'Select',
226 ],
227 'pseudoconstant' => [
228 'optionGroupName' => 'payment_instrument',
229 'optionEditPath' => 'civicrm/admin/options/payment_instrument',
230 ],
231 ],
232 'card_type_id' => [
233 'name' => 'card_type_id',
234 'type' => CRM_Utils_Type::T_INT,
235 'title' => ts('Card Type ID'),
236 'description' => ts('FK to accept_creditcard option group values'),
237 'where' => 'civicrm_financial_trxn.card_type_id',
238 'table_name' => 'civicrm_financial_trxn',
239 'entity' => 'FinancialTrxn',
240 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
241 'localizable' => 0,
242 'html' => [
243 'type' => 'Select',
244 ],
245 'pseudoconstant' => [
246 'optionGroupName' => 'accept_creditcard',
247 'optionEditPath' => 'civicrm/admin/options/accept_creditcard',
248 ],
249 ],
250 'trxn_result_code' => [
251 'name' => 'trxn_result_code',
252 'type' => CRM_Utils_Type::T_STRING,
253 'title' => ts('Transaction Result Code'),
254 'description' => ts('processor result code'),
255 'maxlength' => 255,
256 'size' => CRM_Utils_Type::HUGE,
257 'where' => 'civicrm_financial_trxn.trxn_result_code',
258 'table_name' => 'civicrm_financial_trxn',
259 'entity' => 'FinancialTrxn',
260 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
261 'localizable' => 0,
262 ],
263 'trxn_id' => [
264 'name' => 'trxn_id',
265 'type' => CRM_Utils_Type::T_STRING,
266 'title' => ts('Transaction ID'),
267 'description' => ts('Transaction id supplied by external processor. This may not be unique.'),
268 'maxlength' => 255,
269 'size' => 10,
270 'where' => 'civicrm_financial_trxn.trxn_id',
271 'table_name' => 'civicrm_financial_trxn',
272 'entity' => 'FinancialTrxn',
273 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
274 'localizable' => 0,
275 'html' => [
276 'type' => 'Text',
277 ],
278 ],
279 'check_number' => [
280 'name' => 'check_number',
281 'type' => CRM_Utils_Type::T_STRING,
282 'title' => ts('Check Number'),
283 'description' => ts('Check number'),
284 'maxlength' => 255,
285 'size' => 6,
286 'where' => 'civicrm_financial_trxn.check_number',
287 'table_name' => 'civicrm_financial_trxn',
288 'entity' => 'FinancialTrxn',
289 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
290 'localizable' => 0,
291 'html' => [
292 'type' => 'Text',
293 ],
294 ],
295 'pan_truncation' => [
296 'name' => 'pan_truncation',
297 'type' => CRM_Utils_Type::T_STRING,
298 'title' => ts('Pan Truncation'),
299 'description' => ts('Last 4 digits of credit card'),
300 'maxlength' => 4,
301 'size' => 4,
302 'where' => 'civicrm_financial_trxn.pan_truncation',
303 'table_name' => 'civicrm_financial_trxn',
304 'entity' => 'FinancialTrxn',
305 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
306 'localizable' => 0,
307 'html' => [
308 'type' => 'Text',
309 ],
310 ],
cf8f0fff 311 ];
d2545e26
PN
312}
313
314/**
315 * Adjust Metadata for Get action.
316 *
317 * The metadata is used for setting defaults, documentation & validation.
318 *
319 * @param array $params
320 * Array of parameters determined by getfields.
321 */
322function _civicrm_api3_payment_get_spec(&$params) {
cf8f0fff
CW
323 $params = [
324 'contribution_id' => [
4c68d684 325 'title' => ts('Contribution ID'),
d2545e26 326 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
327 ],
328 'entity_table' => [
4c68d684 329 'title' => ts('Entity Table'),
d2545e26 330 'api.default' => 'civicrm_contribution',
cf8f0fff
CW
331 ],
332 'entity_id' => [
4c68d684 333 'title' => ts('Entity ID'),
d2545e26 334 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
335 'api.aliases' => ['contribution_id'],
336 ],
c4204541 337 'trxn_id' => [
4c68d684 338 'title' => ts('Transaction ID'),
b6cb0554 339 'description' => ts('Transaction id supplied by external processor. This may not be unique.'),
c4204541 340 'type' => CRM_Utils_Type::T_STRING,
341 ],
4c68d684 342 'trxn_date' => [
343 'title' => ts('Payment Date'),
344 'type' => CRM_Utils_Type::T_TIMESTAMP,
345 ],
f5269434 346 'financial_trxn_id' => [
347 'title' => ts('Payment ID'),
b6cb0554 348 'description' => ts('The ID of the record in civicrm_financial_trxn'),
f5269434 349 'type' => CRM_Utils_Type::T_INT,
350 'api.aliases' => ['payment_id', 'id'],
351 ],
cf8f0fff 352 ];
d2545e26
PN
353}
354
355/**
356 * Adjust Metadata for Delete action.
357 *
358 * The metadata is used for setting defaults, documentation & validation.
359 *
360 * @param array $params
361 * Array of parameters.
362 */
363function _civicrm_api3_payment_delete_spec(&$params) {
cf8f0fff
CW
364 $params = [
365 'id' => [
7c31ae57 366 'api.required' => 1,
d2545e26
PN
367 'title' => 'Payment ID',
368 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
369 'api.aliases' => ['payment_id'],
370 ],
371 ];
d2545e26
PN
372}
373
374/**
375 * Adjust Metadata for Cancel action.
376 *
377 * The metadata is used for setting defaults, documentation & validation.
378 *
379 * @param array $params
380 * Array of parameters.
381 */
382function _civicrm_api3_payment_cancel_spec(&$params) {
cf8f0fff
CW
383 $params = [
384 'id' => [
7c31ae57 385 'api.required' => 1,
d2545e26
PN
386 'title' => 'Payment ID',
387 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
388 'api.aliases' => ['payment_id'],
389 ],
2561fc11 390 'trxn_date' => [
391 'title' => 'Cancel Date',
392 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
393 ],
cf8f0fff 394 ];
d2545e26 395}
a79d2ec2 396
397/**
398 * Send a payment confirmation.
399 *
400 * @param array $params
401 * Input parameters.
402 *
403 * @return array
404 * @throws Exception
405 */
406function civicrm_api3_payment_sendconfirmation($params) {
407 $allowedParams = [
44a2f017 408 'from',
a79d2ec2 409 'id',
44a2f017 410 'check_permissions',
a79d2ec2 411 ];
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([
416 $params['id'] => [
417 'is_sent' => $result[0],
418 'subject' => $result[1],
419 'message_txt' => $result[2],
420 'message_html' => $result[3],
7c31ae57
SL
421 ],
422 ]);
a79d2ec2 423}
424
425/**
426 * Adjust Metadata for sendconfirmation action.
427 *
428 * The metadata is used for setting defaults, documentation & validation.
429 *
430 * @param array $params
431 * Array of parameters determined by getfields.
432 */
433function _civicrm_api3_payment_sendconfirmation_spec(&$params) {
cf8f0fff 434 $params['id'] = [
a79d2ec2 435 'api.required' => 1,
436 'title' => ts('Payment ID'),
437 'type' => CRM_Utils_Type::T_INT,
cf8f0fff 438 ];
44a2f017 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,
442 ];
6ac768e7 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,
447 'api.default' => 0,
448 ];
a79d2ec2 449}