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