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