3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM Contribution Payment records.
31 * @package CiviCRM_APIv3
35 * Retrieve a set of financial transactions which are payments.
37 * @param array $params
41 * Array of financial transactions which are payments, if error an array with an error id and error message
43 function civicrm_api3_payment_get($params) {
44 $financialTrxn = array();
46 if (isset($params['options']) && CRM_Utils_Array
::value('limit', $params['options'])) {
47 $limit = CRM_Utils_Array
::value('limit', $params['options']);
49 $params['options']['limit'] = 0;
50 $eft = civicrm_api3('EntityFinancialTrxn', 'get', $params);
51 if (!empty($eft['values'])) {
53 foreach ($eft['values'] as $efts) {
54 if (empty($efts['financial_trxn_id'])) {
57 $eftIds[] = $efts['financial_trxn_id'];
58 $map[$efts['financial_trxn_id']] = $efts['entity_id'];
60 if (!empty($eftIds)) {
62 'id' => array('IN' => $eftIds),
66 $ftParams['options']['limit'] = $limit;
68 $financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
69 foreach ($financialTrxn['values'] as &$values) {
70 $values['contribution_id'] = $map[$values['id']];
74 return civicrm_api3_create_success(CRM_Utils_Array
::value('values', $financialTrxn, array()), $params, 'Payment', 'get');
80 * @param array $params
83 * @throws API_Exception
87 function civicrm_api3_payment_delete(&$params) {
88 return civicrm_api3('FinancialTrxn', 'delete', $params);
92 * Cancel/Refund a payment for a Contribution.
94 * @param array $params
97 * @throws API_Exception
101 function civicrm_api3_payment_cancel(&$params) {
103 'entity_table' => 'civicrm_contribution',
104 'financial_trxn_id' => $params['id'],
106 $entity = civicrm_api3('EntityFinancialTrxn', 'getsingle', $eftParams);
107 $contributionId = $entity['entity_id'];
108 $params['total_amount'] = $entity['amount'];
109 unset($params['id']);
111 $trxn = CRM_Contribute_BAO_Contribution
::recordAdditionalPayment($contributionId, $params, 'refund', NULL, FALSE);
114 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id
]);
115 return civicrm_api3_create_success($values, $params, 'Payment', 'cancel', $trxn);
119 * Add a payment for a Contribution.
121 * @param array $params
124 * @throws API_Exception
128 function civicrm_api3_payment_create(&$params) {
129 // Check if it is an update
130 if (CRM_Utils_Array
::value('id', $params)) {
131 $amount = $params['total_amount'];
132 civicrm_api3('Payment', 'cancel', $params);
133 $params['total_amount'] = $amount;
136 $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $params['contribution_id']));
137 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus($contribution['contribution_status_id'], 'name');
138 if ($contributionStatus != 'Partially paid'
139 && !($contributionStatus == 'Pending' && $contribution['is_pay_later'] == TRUE)
141 throw new API_Exception('Please select a contribution which has a partial or pending payment');
144 // Check if pending contribution
145 $fullyPaidPayLater = FALSE;
146 if ($contributionStatus == 'Pending') {
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']));
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;
158 civicrm_api3('Contribution', 'create',
160 'id' => $contribution['id'],
161 'contribution_status_id' => 'Partially paid',
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);
174 throw new API_Exception('Please specify a valid Line Item.');
176 // get financial item
178 FROM civicrm_financial_item fi
179 INNER JOIN civicrm_line_item li ON li.id = fi.entity_id and fi.entity_table = 'civicrm_line_item'
180 WHERE li.contribution_id = %1 AND li.id = %2";
182 1 => array($params['contribution_id'], 'Integer'),
183 2 => array($id, 'Integer'),
185 $fid = CRM_Core_DAO
::singleValueQuery($sql, $sqlParams);
186 // Record Entity Financial Trxn
188 'entity_table' => 'civicrm_financial_item',
189 'financial_trxn_id' => $trxn->id
,
193 civicrm_api3('EntityFinancialTrxn', 'create', $eftParams);
197 elseif (!empty($trxn)) {
198 // Assign the lineitems proportionally
199 CRM_Contribute_BAO_Contribution
::assignProportionalLineItems($params, $trxn->id
, $contribution['total_amount']);
204 _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id
]);
205 return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn);
209 * Adjust Metadata for Create action.
211 * The metadata is used for setting defaults, documentation & validation.
213 * @param array $params
214 * Array of parameters.
216 function _civicrm_api3_payment_create_spec(&$params) {
218 'contribution_id' => array(
219 'api.required' => 1 ,
220 'title' => 'Contribution ID',
221 'type' => CRM_Utils_Type
::T_INT
,
223 'total_amount' => array(
224 'api.required' => 1 ,
225 'title' => 'Total Payment Amount',
226 'type' => CRM_Utils_Type
::T_FLOAT
,
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'),
234 'title' => 'Payment ID',
235 'type' => CRM_Utils_Type
::T_INT
,
236 'api.aliases' => array('payment_id'),
242 * Adjust Metadata for Get action.
244 * The metadata is used for setting defaults, documentation & validation.
246 * @param array $params
247 * Array of parameters determined by getfields.
249 function _civicrm_api3_payment_get_spec(&$params) {
251 'contribution_id' => array(
252 'title' => 'Contribution ID',
253 'type' => CRM_Utils_Type
::T_INT
,
255 'entity_table' => array(
256 'title' => 'Entity Table',
257 'api.default' => 'civicrm_contribution',
259 'entity_id' => array(
260 'title' => 'Entity ID',
261 'type' => CRM_Utils_Type
::T_INT
,
262 'api.aliases' => array('contribution_id'),
268 * Adjust Metadata for Delete action.
270 * The metadata is used for setting defaults, documentation & validation.
272 * @param array $params
273 * Array of parameters.
275 function _civicrm_api3_payment_delete_spec(&$params) {
278 'api.required' => 1 ,
279 'title' => 'Payment ID',
280 'type' => CRM_Utils_Type
::T_INT
,
281 'api.aliases' => array('payment_id'),
287 * Adjust Metadata for Cancel action.
289 * The metadata is used for setting defaults, documentation & validation.
291 * @param array $params
292 * Array of parameters.
294 function _civicrm_api3_payment_cancel_spec(&$params) {
297 'api.required' => 1 ,
298 'title' => 'Payment ID',
299 'type' => CRM_Utils_Type
::T_INT
,
300 'api.aliases' => array('payment_id'),