3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM Order objects, an abstract entity
14 * comprised of contributions and related line items.
16 * @package CiviCRM_APIv3
20 * Retrieve a set of Order.
22 * @param array $params
26 * Array of Order, if error an array with an error id and error message
28 function civicrm_api3_order_get($params) {
30 $params['api.line_item.get'] = ['qty' => ['<>' => 0]];
31 $isSequential = FALSE;
32 if (!empty($params['sequential'])) {
33 $params['sequential'] = 0;
36 $result = civicrm_api3('Contribution', 'get', $params);
37 if (!empty($result['values'])) {
38 foreach ($result['values'] as $key => $contribution) {
39 $contributions[$key] = $contribution;
40 $contributions[$key]['line_items'] = $contribution['api.line_item.get']['values'];
41 unset($contributions[$key]['api.line_item.get']);
44 $params['sequential'] = $isSequential;
45 return civicrm_api3_create_success($contributions, $params, 'Order', 'get');
49 * Adjust Metadata for Get action.
51 * The metadata is used for setting defaults, documentation & validation.
53 * @param array $params
54 * Array of parameters determined by getfields.
56 function _civicrm_api3_order_get_spec(&$params) {
57 $params['id']['api.aliases'] = ['order_id'];
58 $params['id']['title'] = ts('Contribution / Order ID');
62 * Add or update a Order.
64 * @param array $params
70 * @throws \CiviCRM_API3_Exception
71 * @throws API_Exception
73 function civicrm_api3_order_create($params) {
74 civicrm_api3_verify_one_mandatory($params, NULL, ['line_items', 'total_amount']);
77 $contributionStatus = CRM_Utils_Array
::value('contribution_status_id', $params);
78 if ($contributionStatus !== 'Pending' && 'Pending' !== CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contributionStatus)) {
79 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. You can chain payment creation e.g civicrm_api3('Order', 'create', ['blah' => 'blah', 'contribution_status_id' => 'Pending', 'api.Payment.create => ['total_amount' => 5]]");
82 if (!empty($params['line_items']) && is_array($params['line_items'])) {
84 CRM_Contribute_BAO_Contribution
::checkLineItems($params);
85 foreach ($params['line_items'] as $lineItems) {
86 $entityParams = CRM_Utils_Array
::value('params', $lineItems, []);
87 if (!empty($entityParams) && !empty($lineItems['line_item'])) {
88 $item = reset($lineItems['line_item']);
89 $entity = str_replace('civicrm_', '', $item['entity_table']);
92 if (in_array($entity, ['participant', 'membership'])) {
93 $entityParams['skipLineItem'] = TRUE;
94 if ($contributionStatus === 'Pending') {
95 $entityParams['status_id'] = ($entity === 'participant' ?
'Pending from incomplete transaction' : 'Pending');
97 $entityResult = civicrm_api3($entity, 'create', $entityParams);
98 $params['contribution_mode'] = $entity;
99 $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
100 foreach ($lineItems['line_item'] as &$items) {
101 $items['entity_id'] = $entityResult['id'];
108 if (empty($priceSetID)) {
109 $item = reset($lineItems['line_item']);
110 $priceSetID = civicrm_api3('PriceField', 'getvalue', [
111 'return' => 'price_set_id',
112 'id' => $item['price_field_id'],
114 $params['line_item'][$priceSetID] = [];
116 $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
119 $contributionParams = $params;
120 foreach ($contributionParams as $key => $value) {
121 // Unset chained keys so the code does not attempt to do this chaining twice.
122 // e.g if calling 'api.Payment.create' We want to finish creating the order first.
123 // it would probably be better to have a full whitelist of contributionParams
124 if (substr($key, 0, 3) === 'api') {
125 unset($contributionParams[$key]);
129 $contribution = civicrm_api3('Contribution', 'create', $contributionParams);
131 if ($entity && !empty($contribution['id'])) {
132 foreach ($entityIds as $entityId) {
134 'contribution_id' => $contribution['id'],
135 $entity . '_id' => $entityId,
137 // if entity is pledge then build pledge param
138 if ($entity == 'pledge') {
139 $paymentParams +
= $entityParams;
141 $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams);
144 return civicrm_api3_create_success(CRM_Utils_Array
::value('values', $contribution), $params, 'Order', 'create');
150 * @param array $params
153 * @throws API_Exception
154 * @throws CiviCRM_API3_Exception
156 function civicrm_api3_order_delete($params) {
157 $contribution = civicrm_api3('Contribution', 'get', [
158 'return' => ['is_test'],
159 'id' => $params['id'],
161 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
162 $result = civicrm_api3('Contribution', 'delete', $params);
165 throw new API_Exception('Only test orders can be deleted.');
167 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
173 * @param array $params
178 function civicrm_api3_order_cancel($params) {
179 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
180 $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
181 $result = civicrm_api3('Contribution', 'create', $params);
182 CRM_Contribute_BAO_Contribution
::transitionComponents($params);
183 return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel');
187 * Adjust Metadata for Cancel action.
189 * The metadata is used for setting defaults, documentation & validation.
191 * @param array $params
192 * Array of parameters determined by getfields.
194 function _civicrm_api3_order_cancel_spec(&$params) {
195 $params['contribution_id'] = [
197 'title' => 'Contribution ID',
198 'type' => CRM_Utils_Type
::T_INT
,
203 * Adjust Metadata for Create action.
205 * The metadata is used for setting defaults, documentation & validation.
207 * @param array $params
208 * Array of parameters determined by getfields.
210 function _civicrm_api3_order_create_spec(&$params) {
211 $params['contact_id'] = [
212 'name' => 'contact_id',
213 'title' => 'Contact ID',
214 'type' => CRM_Utils_Type
::T_INT
,
215 'api.required' => TRUE,
217 $params['total_amount'] = [
218 'name' => 'total_amount',
219 'title' => 'Total Amount',
221 $params['financial_type_id'] = [
222 'name' => 'financial_type_id',
223 'title' => 'Financial Type',
224 'type' => CRM_Utils_Type
::T_INT
,
225 'api.required' => TRUE,
226 'table_name' => 'civicrm_contribution',
227 'entity' => 'Contribution',
228 'bao' => 'CRM_Contribute_BAO_Contribution',
229 'pseudoconstant' => [
230 'table' => 'civicrm_financial_type',
232 'labelColumn' => 'name',
238 * Adjust Metadata for Delete action.
240 * The metadata is used for setting defaults, documentation & validation.
242 * @param array $params
243 * Array of parameters determined by getfields.
245 function _civicrm_api3_order_delete_spec(&$params) {
246 $params['contribution_id'] = [
247 'api.required' => TRUE,
248 'title' => 'Contribution ID',
249 'type' => CRM_Utils_Type
::T_INT
,
251 $params['id']['api.aliases'] = ['contribution_id'];