Merge pull request #13778 from eileenmcnaughton/paymetn_next
[civicrm-core.git] / api / v3 / Payment.php
CommitLineData
d2545e26
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
d2545e26 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
d2545e26
PN
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 * This api exposes CiviCRM Contribution Payment records.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34/**
35 * Retrieve a set of financial transactions which are payments.
36 *
37 * @param array $params
38 * Input parameters.
39 *
40 * @return array
41 * Array of financial transactions which are payments, if error an array with an error id and error message
42 */
43function civicrm_api3_payment_get($params) {
44 $financialTrxn = array();
cf33c6cb
E
45 $limit = '';
46 if (isset($params['options']) && CRM_Utils_Array::value('limit', $params['options'])) {
47 $limit = CRM_Utils_Array::value('limit', $params['options']);
48 }
49 $params['options']['limit'] = 0;
d2545e26
PN
50 $eft = civicrm_api3('EntityFinancialTrxn', 'get', $params);
51 if (!empty($eft['values'])) {
2ff2ff26 52 $eftIds = array();
d2545e26 53 foreach ($eft['values'] as $efts) {
2ff2ff26
PN
54 if (empty($efts['financial_trxn_id'])) {
55 continue;
56 }
d2545e26
PN
57 $eftIds[] = $efts['financial_trxn_id'];
58 $map[$efts['financial_trxn_id']] = $efts['entity_id'];
59 }
2ff2ff26
PN
60 if (!empty($eftIds)) {
61 $ftParams = array(
62 'id' => array('IN' => $eftIds),
63 'is_payment' => 1,
64 );
65 if ($limit) {
66 $ftParams['options']['limit'] = $limit;
67 }
68 $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
69 foreach ($financialTrxn['values'] as &$values) {
70 $values['contribution_id'] = $map[$values['id']];
71 }
d2545e26
PN
72 }
73 }
2ff2ff26 74 return civicrm_api3_create_success(CRM_Utils_Array::value('values', $financialTrxn, array()), $params, 'Payment', 'get');
d2545e26
PN
75}
76
77/**
78 * Delete a payment.
79 *
80 * @param array $params
81 * Input parameters.
82 *
83 * @throws API_Exception
84 * @return array
85 * Api result array
86 */
87function civicrm_api3_payment_delete(&$params) {
88 return civicrm_api3('FinancialTrxn', 'delete', $params);
89}
90
91/**
92 * Cancel/Refund a payment for a Contribution.
93 *
94 * @param array $params
95 * Input parameters.
96 *
97 * @throws API_Exception
98 * @return array
99 * Api result array
100 */
101function civicrm_api3_payment_cancel(&$params) {
102 $eftParams = array(
103 'entity_table' => 'civicrm_contribution',
104 'financial_trxn_id' => $params['id'],
105 );
106 $entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
107 $contributionId = $entity['entity_id'];
108 $params['total_amount'] = $entity['amount'];
109 unset($params['id']);
110
111 $trxn = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($contributionId, $params, 'refund', NULL, FALSE);
112
113 $values = array();
114 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]);
115 return civicrm_api3_create_success($values, $params, 'Payment', 'cancel', $trxn);
116}
117
118/**
119 * Add a payment for a Contribution.
120 *
121 * @param array $params
122 * Input parameters.
123 *
124 * @throws API_Exception
125 * @return array
126 * Api result array
127 */
128function civicrm_api3_payment_create(&$params) {
129 // Check if it is an update
2fabb298 130 if (CRM_Utils_Array::value('id', $params)) {
d2545e26
PN
131 $amount = $params['total_amount'];
132 civicrm_api3('Payment', 'cancel', $params);
133 $params['total_amount'] = $amount;
134 }
0c9b306a 135 $trxn = CRM_Financial_BAO_Payment::create($params);
5625fdf0 136
d2545e26
PN
137 $values = array();
138 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]);
139 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
140}
141
142/**
143 * Adjust Metadata for Create action.
144 *
145 * The metadata is used for setting defaults, documentation & validation.
146 *
147 * @param array $params
148 * Array of parameters.
149 */
150function _civicrm_api3_payment_create_spec(&$params) {
151 $params = array(
152 'contribution_id' => array(
153 'api.required' => 1 ,
154 'title' => 'Contribution ID',
155 'type' => CRM_Utils_Type::T_INT,
156 ),
157 'total_amount' => array(
158 'api.required' => 1 ,
159 'title' => 'Total Payment Amount',
160 'type' => CRM_Utils_Type::T_FLOAT,
161 ),
162 'payment_processor_id' => array(
163 'title' => 'Payment Processor ID',
164 'type' => CRM_Utils_Type::T_INT,
165 'description' => ts('Payment processor ID - required for payment processor payments'),
166 ),
167 'id' => array(
168 'title' => 'Payment ID',
169 'type' => CRM_Utils_Type::T_INT,
170 'api.aliases' => array('payment_id'),
171 ),
172 );
173}
174
175/**
176 * Adjust Metadata for Get action.
177 *
178 * The metadata is used for setting defaults, documentation & validation.
179 *
180 * @param array $params
181 * Array of parameters determined by getfields.
182 */
183function _civicrm_api3_payment_get_spec(&$params) {
184 $params = array(
185 'contribution_id' => array(
186 'title' => 'Contribution ID',
187 'type' => CRM_Utils_Type::T_INT,
188 ),
189 'entity_table' => array(
190 'title' => 'Entity Table',
191 'api.default' => 'civicrm_contribution',
192 ),
193 'entity_id' => array(
194 'title' => 'Entity ID',
195 'type' => CRM_Utils_Type::T_INT,
196 'api.aliases' => array('contribution_id'),
197 ),
198 );
199}
200
201/**
202 * Adjust Metadata for Delete action.
203 *
204 * The metadata is used for setting defaults, documentation & validation.
205 *
206 * @param array $params
207 * Array of parameters.
208 */
209function _civicrm_api3_payment_delete_spec(&$params) {
210 $params = array(
211 'id' => array(
212 'api.required' => 1 ,
213 'title' => 'Payment ID',
214 'type' => CRM_Utils_Type::T_INT,
215 'api.aliases' => array('payment_id'),
216 ),
217 );
218}
219
220/**
221 * Adjust Metadata for Cancel action.
222 *
223 * The metadata is used for setting defaults, documentation & validation.
224 *
225 * @param array $params
226 * Array of parameters.
227 */
228function _civicrm_api3_payment_cancel_spec(&$params) {
229 $params = array(
230 'id' => array(
231 'api.required' => 1 ,
232 'title' => 'Payment ID',
233 'type' => CRM_Utils_Type::T_INT,
234 'api.aliases' => array('payment_id'),
235 ),
236 );
237}
a79d2ec2 238
239/**
240 * Send a payment confirmation.
241 *
242 * @param array $params
243 * Input parameters.
244 *
245 * @return array
246 * @throws Exception
247 */
248function civicrm_api3_payment_sendconfirmation($params) {
249 $allowedParams = [
250 'receipt_from_email',
251 'receipt_from_name',
252 'cc_receipt',
253 'bcc_receipt',
254 'receipt_text',
255 'id',
256 ];
257 $input = array_intersect_key($params, array_flip($allowedParams));
258 // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not
259 $result = CRM_Financial_BAO_Payment::sendConfirmation($input);
260 return civicrm_api3_create_success([
261 $params['id'] => [
262 'is_sent' => $result[0],
263 'subject' => $result[1],
264 'message_txt' => $result[2],
265 'message_html' => $result[3],
266 ]]);
267}
268
269/**
270 * Adjust Metadata for sendconfirmation action.
271 *
272 * The metadata is used for setting defaults, documentation & validation.
273 *
274 * @param array $params
275 * Array of parameters determined by getfields.
276 */
277function _civicrm_api3_payment_sendconfirmation_spec(&$params) {
278 $params['id'] = array(
279 'api.required' => 1,
280 'title' => ts('Payment ID'),
281 'type' => CRM_Utils_Type::T_INT,
282 );
283}