3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 Order objects, an abstract entity
30 * comprised of contributions and related line items.
32 * @package CiviCRM_APIv3
36 * Retrieve a set of Order.
38 * @param array $params
42 * Array of Order, if error an array with an error id and error message
44 function civicrm_api3_order_get($params) {
46 $params['api.line_item.get'] = ['qty' => ['<>' => 0]];
47 $isSequential = FALSE;
48 if (!empty($params['sequential'])) {
49 $params['sequential'] = 0;
52 $result = civicrm_api3('Contribution', 'get', $params);
53 if (!empty($result['values'])) {
54 foreach ($result['values'] as $key => $contribution) {
55 $contributions[$key] = $contribution;
56 $contributions[$key]['line_items'] = $contribution['api.line_item.get']['values'];
57 unset($contributions[$key]['api.line_item.get']);
60 $params['sequential'] = $isSequential;
61 return civicrm_api3_create_success($contributions, $params, 'Order', 'get');
65 * Adjust Metadata for Get action.
67 * The metadata is used for setting defaults, documentation & validation.
69 * @param array $params
70 * Array of parameters determined by getfields.
72 function _civicrm_api3_order_get_spec(&$params) {
73 $params['id']['api.aliases'] = ['order_id'];
74 $params['id']['title'] = ts('Contribution / Order ID');
78 * Add or update a Order.
80 * @param array $params
86 * @throws \CiviCRM_API3_Exception
87 * @throws API_Exception
89 function civicrm_api3_order_create($params) {
90 civicrm_api3_verify_one_mandatory($params, NULL, ['line_items', 'total_amount']);
93 $contributionStatus = CRM_Utils_Array
::value('contribution_status_id', $params);
94 if ($contributionStatus !== 'Pending' && 'Pending' !== CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contributionStatus)) {
95 CRM_Core_Error
::deprecatedFunctionWarning('Creating a Order with a status other than pending is deprecated. Currently empty defaults to "Completed" so as a transition not passing in "Pending" is deprecated');
97 if (!empty($params['line_items']) && is_array($params['line_items'])) {
99 CRM_Contribute_BAO_Contribution
::checkLineItems($params);
100 foreach ($params['line_items'] as $lineItems) {
101 $entityParams = CRM_Utils_Array
::value('params', $lineItems, []);
102 if (!empty($entityParams) && !empty($lineItems['line_item'])) {
103 $item = reset($lineItems['line_item']);
104 $entity = str_replace('civicrm_', '', $item['entity_table']);
107 if (in_array($entity, ['participant', 'membership'])) {
108 $entityParams['skipLineItem'] = TRUE;
109 if ($contributionStatus === 'Pending') {
110 $entityParams['status_id'] = ($entity === 'participant' ?
'Pending from incomplete transaction' : 'Pending');
112 $entityResult = civicrm_api3($entity, 'create', $entityParams);
113 $params['contribution_mode'] = $entity;
114 $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
115 foreach ($lineItems['line_item'] as &$items) {
116 $items['entity_id'] = $entityResult['id'];
123 if (empty($priceSetID)) {
124 $item = reset($lineItems['line_item']);
125 $priceSetID = civicrm_api3('PriceField', 'getvalue', [
126 'return' => 'price_set_id',
127 'id' => $item['price_field_id'],
129 $params['line_item'][$priceSetID] = [];
131 $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
134 $contribution = civicrm_api3('Contribution', 'create', $params);
136 if ($entity && !empty($contribution['id'])) {
137 foreach ($entityIds as $entityId) {
139 'contribution_id' => $contribution['id'],
140 $entity . '_id' => $entityId,
142 // if entity is pledge then build pledge param
143 if ($entity == 'pledge') {
144 $paymentParams +
= $entityParams;
146 $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams);
149 return civicrm_api3_create_success(CRM_Utils_Array
::value('values', $contribution), $params, 'Order', 'create');
155 * @param array $params
158 * @throws API_Exception
159 * @throws CiviCRM_API3_Exception
161 function civicrm_api3_order_delete($params) {
162 $contribution = civicrm_api3('Contribution', 'get', [
163 'return' => ['is_test'],
164 'id' => $params['id'],
166 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
167 $result = civicrm_api3('Contribution', 'delete', $params);
170 throw new API_Exception('Only test orders can be deleted.');
172 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
178 * @param array $params
183 function civicrm_api3_order_cancel($params) {
184 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
185 $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
186 $result = civicrm_api3('Contribution', 'create', $params);
187 CRM_Contribute_BAO_Contribution
::transitionComponents($params);
188 return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel');
192 * Adjust Metadata for Cancel action.
194 * The metadata is used for setting defaults, documentation & validation.
196 * @param array $params
197 * Array of parameters determined by getfields.
199 function _civicrm_api3_order_cancel_spec(&$params) {
200 $params['contribution_id'] = [
202 'title' => 'Contribution ID',
203 'type' => CRM_Utils_Type
::T_INT
,
208 * Adjust Metadata for Create action.
210 * The metadata is used for setting defaults, documentation & validation.
212 * @param array $params
213 * Array of parameters determined by getfields.
215 function _civicrm_api3_order_create_spec(&$params) {
216 $params['contact_id'] = [
217 'name' => 'contact_id',
218 'title' => 'Contact ID',
219 'type' => CRM_Utils_Type
::T_INT
,
220 'api.required' => TRUE,
222 $params['total_amount'] = [
223 'name' => 'total_amount',
224 'title' => 'Total Amount',
226 $params['financial_type_id'] = [
227 'name' => 'financial_type_id',
228 'title' => 'Financial Type',
229 'type' => CRM_Utils_Type
::T_INT
,
230 'api.required' => TRUE,
231 'table_name' => 'civicrm_contribution',
232 'entity' => 'Contribution',
233 'bao' => 'CRM_Contribute_BAO_Contribution',
234 'pseudoconstant' => [
235 'table' => 'civicrm_financial_type',
237 'labelColumn' => 'name',
243 * Adjust Metadata for Delete action.
245 * The metadata is used for setting defaults, documentation & validation.
247 * @param array $params
248 * Array of parameters determined by getfields.
250 function _civicrm_api3_order_delete_spec(&$params) {
251 $params['contribution_id'] = [
252 'api.required' => TRUE,
253 'title' => 'Contribution ID',
254 'type' => CRM_Utils_Type
::T_INT
,
256 $params['id']['api.aliases'] = ['contribution_id'];