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) {
45 $contributions = array();
46 $params['api.line_item.get'] = array('qty' => array('<>' => 0));
47 $isSequential = FALSE;
48 if (CRM_Utils_Array
::value('sequential', $params)) {
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
83 * @throws API_Exception
87 function civicrm_api3_order_create(&$params) {
91 if (CRM_Utils_Array
::value('line_items', $params) && is_array($params['line_items'])) {
93 CRM_Contribute_BAO_Contribution
::checkLineItems($params);
94 foreach ($params['line_items'] as $lineItems) {
95 $entityParams = CRM_Utils_Array
::value('params', $lineItems, array());
96 if (!empty($entityParams) && !empty($lineItems['line_item'])) {
97 $item = reset($lineItems['line_item']);
98 $entity = str_replace('civicrm_', '', $item['entity_table']);
101 if (in_array($entity, array('participant', 'membership'))) {
102 $entityParams['skipLineItem'] = TRUE;
103 $entityResult = civicrm_api3($entity, 'create', $entityParams);
104 $params['contribution_mode'] = $entity;
105 $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
106 foreach ($lineItems['line_item'] as &$items) {
107 $items['entity_id'] = $entityResult['id'];
114 if (empty($priceSetID)) {
115 $item = reset($lineItems['line_item']);
116 $priceSetID = civicrm_api3('PriceField', 'getvalue', array(
117 'return' => 'price_set_id',
118 'id' => $item['price_field_id'],
120 $params['line_item'][$priceSetID] = array();
122 $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
125 $contribution = civicrm_api3('Contribution', 'create', $params);
127 if ($entity && CRM_Utils_Array
::value('id', $contribution)) {
128 foreach ($entityIds as $entityId) {
129 $paymentParams = array(
130 'contribution_id' => $contribution['id'],
131 $entity . '_id' => $entityId,
133 // if entity is pledge then build pledge param
134 if ($entity == 'pledge') {
135 $paymentParams +
= $entityParams;
137 $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams);
140 return civicrm_api3_create_success(CRM_Utils_Array
::value('values', $contribution), $params, 'Order', 'create');
146 * @param array $params
149 * @throws API_Exception
150 * @throws CiviCRM_API3_Exception
152 function civicrm_api3_order_delete($params) {
153 $contribution = civicrm_api3('Contribution', 'get', array(
154 'return' => array('is_test'),
155 'id' => $params['id'],
157 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
158 $result = civicrm_api3('Contribution', 'delete', $params);
161 throw new API_Exception('Only test orders can be deleted.');
163 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
169 * @param array $params
174 function civicrm_api3_order_cancel($params) {
175 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
176 $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
177 $result = civicrm_api3('Contribution', 'create', $params);
178 CRM_Contribute_BAO_Contribution
::transitionComponents($params);
179 return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel');
183 * Adjust Metadata for Cancel action.
185 * The metadata is used for setting defaults, documentation & validation.
187 * @param array $params
188 * Array of parameters determined by getfields.
190 function _civicrm_api3_order_cancel_spec(&$params) {
191 $params['contribution_id'] = array(
192 'api.required' => 1 ,
193 'title' => 'Contribution ID',
194 'type' => CRM_Utils_Type
::T_INT
,
199 * Adjust Metadata for Create action.
201 * The metadata is used for setting defaults, documentation & validation.
203 * @param array $params
204 * Array of parameters determined by getfields.
206 function _civicrm_api3_order_create_spec(&$params) {
207 $params['contact_id'] = array(
208 'name' => 'contact_id',
209 'title' => 'Contact ID',
210 'type' => CRM_Utils_Type
::T_INT
,
211 'api.required' => TRUE,
213 $params['total_amount'] = array(
214 'name' => 'total_amount',
215 'title' => 'Total Amount',
216 'api.required' => TRUE,
218 $params['financial_type_id'] = [
219 'name' => 'financial_type_id',
220 'title' => 'Financial Type',
221 'type' => CRM_Utils_Type
::T_INT
,
222 'api.required' => TRUE,
223 'table_name' => 'civicrm_contribution',
224 'entity' => 'Contribution',
225 'bao' => 'CRM_Contribute_BAO_Contribution',
226 'pseudoconstant' => [
227 'table' => 'civicrm_financial_type',
229 'labelColumn' => 'name',
235 * Adjust Metadata for Delete action.
237 * The metadata is used for setting defaults, documentation & validation.
239 * @param array $params
240 * Array of parameters determined by getfields.
242 function _civicrm_api3_order_delete_spec(&$params) {
243 $params['contribution_id'] = array(
244 'api.required' => TRUE,
245 'title' => 'Contribution ID',
246 'type' => CRM_Utils_Type
::T_INT
,
248 $params['id']['api.aliases'] = array('contribution_id');