Merge pull request #21129 from civicrm/5.41
[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) {
d2aeabca
MW
30 $params['is_payment'] = TRUE;
31 $contributionID = $params['entity_id'] ?? NULL;
32
33 // In order to support contribution id we need to do an extra lookup.
34 if ($contributionID) {
35 $eftParams = [
36 'entity_id' => $contributionID,
37 'entity_table' => 'civicrm_contribution',
38 'options' => ['limit' => 0],
39 'financial_trxn_id.is_payment' => 1,
40 ];
41 $eft = civicrm_api3('EntityFinancialTrxn', 'get', $eftParams)['values'];
42 if (empty($eft)) {
43 return civicrm_api3_create_success([], $params, 'Payment', 'get');
44 }
45 foreach ($eft as $entityFinancialTrxn) {
46 $params['financial_trxn_id']['IN'][] = $entityFinancialTrxn['financial_trxn_id'];
47 }
c4204541 48 }
d2aeabca
MW
49
50 $financialTrxn = civicrm_api3('FinancialTrxn', 'get', array_merge($params, ['sequential' => FALSE]))['values'];
51 if ($contributionID) {
52 foreach ($financialTrxn as &$values) {
53 $values['contribution_id'] = $contributionID;
d2545e26 54 }
d2aeabca
MW
55 }
56 elseif (!empty($financialTrxn)) {
57 $entityFinancialTrxns = civicrm_api3('EntityFinancialTrxn', 'get', ['financial_trxn_id' => ['IN' => array_keys($financialTrxn)], 'entity_table' => 'civicrm_contribution', 'options' => ['limit' => 0]])['values'];
58 foreach ($entityFinancialTrxns as $entityFinancialTrxn) {
59 $financialTrxn[$entityFinancialTrxn['financial_trxn_id']]['contribution_id'] = $entityFinancialTrxn['entity_id'];
d2545e26
PN
60 }
61 }
d2aeabca
MW
62
63 return civicrm_api3_create_success($financialTrxn, $params, 'Payment', 'get');
d2545e26
PN
64}
65
66/**
67 * Delete a payment.
68 *
69 * @param array $params
70 * Input parameters.
71 *
d2545e26
PN
72 * @return array
73 * Api result array
f5269434 74 *
75 * @throws \CiviCRM_API3_Exception
d2545e26 76 */
1c31aa41 77function civicrm_api3_payment_delete($params) {
d2545e26
PN
78 return civicrm_api3('FinancialTrxn', 'delete', $params);
79}
80
81/**
82 * Cancel/Refund a payment for a Contribution.
83 *
84 * @param array $params
85 * Input parameters.
86 *
d2545e26
PN
87 * @return array
88 * Api result array
df29ccff 89 *
90 * @throws \CiviCRM_API3_Exception
91 * @throws API_Exception
d2545e26 92 */
1c31aa41 93function civicrm_api3_payment_cancel($params) {
cf8f0fff 94 $eftParams = [
d2545e26
PN
95 'entity_table' => 'civicrm_contribution',
96 'financial_trxn_id' => $params['id'],
d8a1b674 97 'return' => ['entity', 'amount', 'entity_id', 'financial_trxn_id.check_number'],
cf8f0fff 98 ];
d2545e26 99 $entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
d2545e26 100
2561fc11 101 $paymentParams = [
102 'total_amount' => -$entity['amount'],
103 'contribution_id' => $entity['entity_id'],
d2aeabca 104 'trxn_date' => $params['trxn_date'] ?? 'now',
df29ccff 105 'cancelled_payment_id' => $params['id'],
d8a1b674 106 'check_number' => $entity['financial_trxn_id.check_number'] ?? NULL,
2561fc11 107 ];
d2545e26 108
2561fc11 109 foreach (['trxn_id', 'payment_instrument_id'] as $permittedParam) {
110 if (isset($params[$permittedParam])) {
111 $paymentParams[$permittedParam] = $params[$permittedParam];
112 }
113 }
114 $result = civicrm_api3('Payment', 'create', $paymentParams);
115 return civicrm_api3_create_success($result['values'], $params, 'Payment', 'cancel');
d2545e26
PN
116}
117
118/**
119 * Add a payment for a Contribution.
120 *
121 * @param array $params
122 * Input parameters.
123 *
d2545e26
PN
124 * @return array
125 * Api result array
a494d7a3 126 *
a494d7a3 127 * @throws \CRM_Core_Exception
128 * @throws \CiviCRM_API3_Exception
d2545e26 129 */
1c31aa41 130function civicrm_api3_payment_create($params) {
b4c48831 131 if (empty($params['skipCleanMoney'])) {
132 foreach (['total_amount', 'net_amount', 'fee_amount'] as $field) {
133 if (isset($params[$field])) {
134 $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
135 }
136 }
137 }
d2545e26 138 // Check if it is an update
de6c59ca 139 if (!empty($params['id'])) {
d2545e26
PN
140 $amount = $params['total_amount'];
141 civicrm_api3('Payment', 'cancel', $params);
142 $params['total_amount'] = $amount;
143 }
0c9b306a 144 $trxn = CRM_Financial_BAO_Payment::create($params);
5625fdf0 145
cf8f0fff 146 $values = [];
d2545e26
PN
147 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]);
148 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
149}
150
151/**
152 * Adjust Metadata for Create action.
153 *
154 * The metadata is used for setting defaults, documentation & validation.
155 *
156 * @param array $params
157 * Array of parameters.
158 */
159function _civicrm_api3_payment_create_spec(&$params) {
cf8f0fff
CW
160 $params = [
161 'contribution_id' => [
7c31ae57 162 'api.required' => 1,
d1987324 163 'title' => ts('Contribution ID'),
d2545e26 164 'type' => CRM_Utils_Type::T_INT,
e2887a3c 165 // We accept order_id as an alias so that we can chain like
166 // civicrm_api3('Order', 'create', ['blah' => 'blah', 'contribution_status_id' => 'Pending', 'api.Payment.create => ['total_amount' => 5]]
167 'api.aliases' => ['order_id'],
cf8f0fff
CW
168 ],
169 'total_amount' => [
7c31ae57 170 'api.required' => 1,
d1987324 171 'title' => ts('Total Payment Amount'),
d2545e26 172 'type' => CRM_Utils_Type::T_FLOAT,
cf8f0fff 173 ],
f5269434 174 'fee_amount' => [
175 'title' => ts('Fee Amount'),
176 'type' => CRM_Utils_Type::T_FLOAT,
177 ],
cf8f0fff 178 'payment_processor_id' => [
a494d7a3 179 'name' => 'payment_processor_id',
d2545e26 180 'type' => CRM_Utils_Type::T_INT,
a494d7a3 181 'title' => ts('Payment Processor'),
182 'description' => ts('Payment Processor for this payment'),
183 'where' => 'civicrm_financial_trxn.payment_processor_id',
184 'table_name' => 'civicrm_financial_trxn',
185 'entity' => 'FinancialTrxn',
186 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
187 'localizable' => 0,
188 'FKClassName' => 'CRM_Financial_DAO_PaymentProcessor',
cf8f0fff
CW
189 ],
190 'id' => [
d1987324 191 'title' => ts('Payment ID'),
d2545e26 192 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
193 'api.aliases' => ['payment_id'],
194 ],
2561fc11 195 'trxn_date' => [
4c68d684 196 'title' => ts('Payment Date'),
2561fc11 197 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
4c68d684 198 'api.default' => 'now',
199 'api.required' => TRUE,
2561fc11 200 ],
bd981689 201 'is_send_contribution_notification' => [
202 'title' => ts('Send out notifications based on contribution status change?'),
203 'description' => ts('Most commonly this equates to emails relating to the contribution, event, etcwhen a payment completes a contribution'),
204 'type' => CRM_Utils_Type::T_BOOLEAN,
205 'api.default' => TRUE,
206 ],
a494d7a3 207 'payment_instrument_id' => [
208 'name' => 'payment_instrument_id',
209 'type' => CRM_Utils_Type::T_INT,
210 'title' => ts('Payment Method'),
211 'description' => ts('FK to payment_instrument option group values'),
212 'where' => 'civicrm_financial_trxn.payment_instrument_id',
213 'table_name' => 'civicrm_financial_trxn',
214 'entity' => 'FinancialTrxn',
215 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
216 'localizable' => 0,
217 'html' => [
218 'type' => 'Select',
219 ],
220 'pseudoconstant' => [
221 'optionGroupName' => 'payment_instrument',
222 'optionEditPath' => 'civicrm/admin/options/payment_instrument',
223 ],
224 ],
225 'card_type_id' => [
226 'name' => 'card_type_id',
227 'type' => CRM_Utils_Type::T_INT,
228 'title' => ts('Card Type ID'),
229 'description' => ts('FK to accept_creditcard option group values'),
230 'where' => 'civicrm_financial_trxn.card_type_id',
231 'table_name' => 'civicrm_financial_trxn',
232 'entity' => 'FinancialTrxn',
233 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
234 'localizable' => 0,
235 'html' => [
236 'type' => 'Select',
237 ],
238 'pseudoconstant' => [
239 'optionGroupName' => 'accept_creditcard',
240 'optionEditPath' => 'civicrm/admin/options/accept_creditcard',
241 ],
242 ],
243 'trxn_result_code' => [
244 'name' => 'trxn_result_code',
245 'type' => CRM_Utils_Type::T_STRING,
246 'title' => ts('Transaction Result Code'),
247 'description' => ts('processor result code'),
248 'maxlength' => 255,
249 'size' => CRM_Utils_Type::HUGE,
250 'where' => 'civicrm_financial_trxn.trxn_result_code',
251 'table_name' => 'civicrm_financial_trxn',
252 'entity' => 'FinancialTrxn',
253 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
254 'localizable' => 0,
255 ],
256 'trxn_id' => [
257 'name' => 'trxn_id',
258 'type' => CRM_Utils_Type::T_STRING,
259 'title' => ts('Transaction ID'),
260 'description' => ts('Transaction id supplied by external processor. This may not be unique.'),
261 'maxlength' => 255,
262 'size' => 10,
263 'where' => 'civicrm_financial_trxn.trxn_id',
264 'table_name' => 'civicrm_financial_trxn',
265 'entity' => 'FinancialTrxn',
266 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
267 'localizable' => 0,
268 'html' => [
269 'type' => 'Text',
270 ],
271 ],
9ed56e2d
MW
272 'order_reference' => [
273 'name' => 'order_reference',
274 'type' => CRM_Utils_Type::T_STRING,
275 'title' => 'Order Reference',
276 'description' => 'Payment Processor external order reference',
277 'maxlength' => 255,
278 'size' => 25,
279 'where' => 'civicrm_financial_trxn.order_reference',
280 'table_name' => 'civicrm_financial_trxn',
281 'entity' => 'FinancialTrxn',
282 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
283 'localizable' => 0,
284 'html' => [
285 'type' => 'Text',
286 ],
287 ],
a494d7a3 288 'check_number' => [
289 'name' => 'check_number',
290 'type' => CRM_Utils_Type::T_STRING,
291 'title' => ts('Check Number'),
292 'description' => ts('Check number'),
293 'maxlength' => 255,
294 'size' => 6,
295 'where' => 'civicrm_financial_trxn.check_number',
296 'table_name' => 'civicrm_financial_trxn',
297 'entity' => 'FinancialTrxn',
298 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
299 'localizable' => 0,
300 'html' => [
301 'type' => 'Text',
302 ],
303 ],
304 'pan_truncation' => [
305 'name' => 'pan_truncation',
306 'type' => CRM_Utils_Type::T_STRING,
d849e301 307 'title' => ts('PAN Truncation'),
a494d7a3 308 'description' => ts('Last 4 digits of credit card'),
309 'maxlength' => 4,
310 'size' => 4,
311 'where' => 'civicrm_financial_trxn.pan_truncation',
312 'table_name' => 'civicrm_financial_trxn',
313 'entity' => 'FinancialTrxn',
314 'bao' => 'CRM_Financial_DAO_FinancialTrxn',
315 'localizable' => 0,
316 'html' => [
317 'type' => 'Text',
318 ],
319 ],
cf8f0fff 320 ];
d2545e26
PN
321}
322
323/**
324 * Adjust Metadata for Get action.
325 *
326 * The metadata is used for setting defaults, documentation & validation.
327 *
328 * @param array $params
329 * Array of parameters determined by getfields.
330 */
331function _civicrm_api3_payment_get_spec(&$params) {
cf8f0fff
CW
332 $params = [
333 'contribution_id' => [
4c68d684 334 'title' => ts('Contribution ID'),
d2545e26 335 'type' => CRM_Utils_Type::T_INT,
cf8f0fff 336 ],
cf8f0fff 337 'entity_id' => [
4c68d684 338 'title' => ts('Entity ID'),
d2545e26 339 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
340 'api.aliases' => ['contribution_id'],
341 ],
c4204541 342 'trxn_id' => [
4c68d684 343 'title' => ts('Transaction ID'),
b6cb0554 344 'description' => ts('Transaction id supplied by external processor. This may not be unique.'),
c4204541 345 'type' => CRM_Utils_Type::T_STRING,
346 ],
6298cd65
MW
347 'order_reference' => [
348 'title' => ts('Order Reference'),
349 'description' => ts('Payment Processor external order reference'),
350 'type' => CRM_Utils_Type::T_STRING,
351 ],
4c68d684 352 'trxn_date' => [
353 'title' => ts('Payment Date'),
354 'type' => CRM_Utils_Type::T_TIMESTAMP,
355 ],
f5269434 356 'financial_trxn_id' => [
357 'title' => ts('Payment ID'),
b6cb0554 358 'description' => ts('The ID of the record in civicrm_financial_trxn'),
f5269434 359 'type' => CRM_Utils_Type::T_INT,
360 'api.aliases' => ['payment_id', 'id'],
361 ],
cf8f0fff 362 ];
d2545e26
PN
363}
364
365/**
366 * Adjust Metadata for Delete action.
367 *
368 * The metadata is used for setting defaults, documentation & validation.
369 *
370 * @param array $params
371 * Array of parameters.
372 */
373function _civicrm_api3_payment_delete_spec(&$params) {
cf8f0fff
CW
374 $params = [
375 'id' => [
7c31ae57 376 'api.required' => 1,
d2545e26
PN
377 'title' => 'Payment ID',
378 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
379 'api.aliases' => ['payment_id'],
380 ],
381 ];
d2545e26
PN
382}
383
384/**
385 * Adjust Metadata for Cancel action.
386 *
387 * The metadata is used for setting defaults, documentation & validation.
388 *
389 * @param array $params
390 * Array of parameters.
391 */
392function _civicrm_api3_payment_cancel_spec(&$params) {
cf8f0fff
CW
393 $params = [
394 'id' => [
7c31ae57 395 'api.required' => 1,
d2545e26
PN
396 'title' => 'Payment ID',
397 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
398 'api.aliases' => ['payment_id'],
399 ],
2561fc11 400 'trxn_date' => [
401 'title' => 'Cancel Date',
402 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
403 ],
cf8f0fff 404 ];
d2545e26 405}
a79d2ec2 406
407/**
408 * Send a payment confirmation.
409 *
410 * @param array $params
411 * Input parameters.
412 *
413 * @return array
414 * @throws Exception
415 */
416function civicrm_api3_payment_sendconfirmation($params) {
417 $allowedParams = [
44a2f017 418 'from',
a79d2ec2 419 'id',
44a2f017 420 'check_permissions',
a79d2ec2 421 ];
422 $input = array_intersect_key($params, array_flip($allowedParams));
423 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
424 $result = CRM_Financial_BAO_Payment::sendConfirmation($input);
425 return civicrm_api3_create_success([
426 $params['id'] => [
427 'is_sent' => $result[0],
428 'subject' => $result[1],
429 'message_txt' => $result[2],
430 'message_html' => $result[3],
7c31ae57
SL
431 ],
432 ]);
a79d2ec2 433}
434
435/**
436 * Adjust Metadata for sendconfirmation action.
437 *
438 * The metadata is used for setting defaults, documentation & validation.
439 *
440 * @param array $params
441 * Array of parameters determined by getfields.
442 */
443function _civicrm_api3_payment_sendconfirmation_spec(&$params) {
cf8f0fff 444 $params['id'] = [
a79d2ec2 445 'api.required' => 1,
446 'title' => ts('Payment ID'),
447 'type' => CRM_Utils_Type::T_INT,
cf8f0fff 448 ];
44a2f017 449 $params['from_email_address'] = [
450 'title' => ts('From email; an email string or the id of a valid email'),
451 'type' => CRM_Utils_Type::T_STRING,
452 ];
6ac768e7 453 $params['is_send_contribution_notification'] = [
454 'title' => ts('Send any event or contribution confirmations triggered by this payment'),
455 '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'),
456 'type' => CRM_Utils_Type::T_BOOLEAN,
457 'api.default' => 0,
458 ];
a79d2ec2 459}