Merge pull request #12003 from vinuvarshith/dev/core/69-fix-state-province-name-token...
[civicrm-core.git] / api / v3 / Payment.php
CommitLineData
d2545e26
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
d2545e26 5 +--------------------------------------------------------------------+
1f4ea726 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 }
135 // Get contribution
136 $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $params['contribution_id']));
fa7a1ebd
PN
137 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus($contribution['contribution_status_id'], 'name');
138 if ($contributionStatus != 'Partially paid'
139 && !($contributionStatus == 'Pending' && $contribution['is_pay_later'] == TRUE)
c60d3584
PN
140 ) {
141 throw new API_Exception('Please select a contribution which has a partial or pending payment');
d2545e26
PN
142 }
143 else {
c60d3584
PN
144 // Check if pending contribution
145 $fullyPaidPayLater = FALSE;
fa7a1ebd 146 if ($contributionStatus == 'Pending') {
c60d3584
PN
147 $cmp = bccomp($contribution['total_amount'], $params['total_amount'], 5);
148 // Total payment amount is the whole amount paid against pending contribution
149 if ($cmp == 0 || $cmp == -1) {
150 civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id']));
151 // Get the trxn
152 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
153 $ftParams = array('id' => $trxnId['financialTrxnId']);
154 $trxn = CRM_Core_BAO_FinancialTrxn::retrieve($ftParams, CRM_Core_DAO::$_nullArray);
155 $fullyPaidPayLater = TRUE;
156 }
157 else {
d5b39a17 158 civicrm_api3('Contribution', 'create',
c60d3584 159 array(
d5b39a17 160 'id' => $contribution['id'],
fa7a1ebd 161 'contribution_status_id' => 'Partially paid',
c60d3584
PN
162 )
163 );
164 }
d2545e26 165 }
c60d3584
PN
166 if (!$fullyPaidPayLater) {
167 $trxn = CRM_Core_BAO_FinancialTrxn::getPartialPaymentTrxn($contribution, $params);
168 if (CRM_Utils_Array::value('line_item', $params) && !empty($trxn)) {
169 foreach ($params['line_item'] as $values) {
170 foreach ($values as $id => $amount) {
171 $p = array('id' => $id);
172 $check = CRM_Price_BAO_LineItem::retrieve($p, $defaults);
173 if (empty($check)) {
174 throw new API_Exception('Please specify a valid Line Item.');
175 }
176 // get financial item
177 $sql = "SELECT fi.id
178 FROM civicrm_financial_item fi
fa7a1ebd 179 INNER JOIN civicrm_line_item li ON li.id = fi.entity_id and fi.entity_table = 'civicrm_line_item'
c60d3584
PN
180 WHERE li.contribution_id = %1 AND li.id = %2";
181 $sqlParams = array(
182 1 => array($params['contribution_id'], 'Integer'),
183 2 => array($id, 'Integer'),
184 );
185 $fid = CRM_Core_DAO::singleValueQuery($sql, $sqlParams);
186 // Record Entity Financial Trxn
187 $eftParams = array(
188 'entity_table' => 'civicrm_financial_item',
189 'financial_trxn_id' => $trxn->id,
190 'amount' => $amount,
191 'entity_id' => $fid,
192 );
193 civicrm_api3('EntityFinancialTrxn', 'create', $eftParams);
194 }
d2545e26 195 }
c60d3584
PN
196 }
197 elseif (!empty($trxn)) {
198 // Assign the lineitems proportionally
955ee56e 199 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution['total_amount']);
d2545e26
PN
200 }
201 }
202 }
d2545e26
PN
203 $values = array();
204 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]);
205 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
206}
207
208/**
209 * Adjust Metadata for Create action.
210 *
211 * The metadata is used for setting defaults, documentation & validation.
212 *
213 * @param array $params
214 * Array of parameters.
215 */
216function _civicrm_api3_payment_create_spec(&$params) {
217 $params = array(
218 'contribution_id' => array(
219 'api.required' => 1 ,
220 'title' => 'Contribution ID',
221 'type' => CRM_Utils_Type::T_INT,
222 ),
223 'total_amount' => array(
224 'api.required' => 1 ,
225 'title' => 'Total Payment Amount',
226 'type' => CRM_Utils_Type::T_FLOAT,
227 ),
228 'payment_processor_id' => array(
229 'title' => 'Payment Processor ID',
230 'type' => CRM_Utils_Type::T_INT,
231 'description' => ts('Payment processor ID - required for payment processor payments'),
232 ),
233 'id' => array(
234 'title' => 'Payment ID',
235 'type' => CRM_Utils_Type::T_INT,
236 'api.aliases' => array('payment_id'),
237 ),
238 );
239}
240
241/**
242 * Adjust Metadata for Get action.
243 *
244 * The metadata is used for setting defaults, documentation & validation.
245 *
246 * @param array $params
247 * Array of parameters determined by getfields.
248 */
249function _civicrm_api3_payment_get_spec(&$params) {
250 $params = array(
251 'contribution_id' => array(
252 'title' => 'Contribution ID',
253 'type' => CRM_Utils_Type::T_INT,
254 ),
255 'entity_table' => array(
256 'title' => 'Entity Table',
257 'api.default' => 'civicrm_contribution',
258 ),
259 'entity_id' => array(
260 'title' => 'Entity ID',
261 'type' => CRM_Utils_Type::T_INT,
262 'api.aliases' => array('contribution_id'),
263 ),
264 );
265}
266
267/**
268 * Adjust Metadata for Delete action.
269 *
270 * The metadata is used for setting defaults, documentation & validation.
271 *
272 * @param array $params
273 * Array of parameters.
274 */
275function _civicrm_api3_payment_delete_spec(&$params) {
276 $params = array(
277 'id' => array(
278 'api.required' => 1 ,
279 'title' => 'Payment ID',
280 'type' => CRM_Utils_Type::T_INT,
281 'api.aliases' => array('payment_id'),
282 ),
283 );
284}
285
286/**
287 * Adjust Metadata for Cancel action.
288 *
289 * The metadata is used for setting defaults, documentation & validation.
290 *
291 * @param array $params
292 * Array of parameters.
293 */
294function _civicrm_api3_payment_cancel_spec(&$params) {
295 $params = array(
296 'id' => array(
297 'api.required' => 1 ,
298 'title' => 'Payment ID',
299 'type' => CRM_Utils_Type::T_INT,
300 'api.aliases' => array('payment_id'),
301 ),
302 );
303}