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
27 * @throws \CiviCRM_API3_Exception
29 function civicrm_api3_order_get(array $params): array {
31 $params['api.line_item.get'] = ['qty' => ['<>' => 0]];
32 $isSequential = FALSE;
33 if (!empty($params['sequential'])) {
34 $params['sequential'] = 0;
37 $result = civicrm_api3('Contribution', 'get', $params);
38 if (!empty($result['values'])) {
39 foreach ($result['values'] as $key => $contribution) {
40 $contributions[$key] = $contribution;
41 $contributions[$key]['line_items'] = $contribution['api.line_item.get']['values'];
42 unset($contributions[$key]['api.line_item.get']);
45 $params['sequential'] = $isSequential;
46 return civicrm_api3_create_success($contributions, $params, 'Order', 'get');
50 * Adjust Metadata for Get action.
52 * The metadata is used for setting defaults, documentation & validation.
54 * @param array $params
55 * Array of parameters determined by getfields.
57 function _civicrm_api3_order_get_spec(array &$params) {
58 $params['id']['api.aliases'] = ['order_id'];
59 $params['id']['title'] = ts('Contribution / Order ID');
63 * Add or update a Order.
65 * @param array $params
71 * @throws \CiviCRM_API3_Exception
72 * @throws API_Exception
74 function civicrm_api3_order_create(array $params): array {
75 civicrm_api3_verify_one_mandatory($params, NULL, ['line_items', 'total_amount']);
78 $params['contribution_status_id'] = 'Pending';
81 if (!empty($params['line_items']) && is_array($params['line_items'])) {
82 CRM_Contribute_BAO_Contribution
::checkLineItems($params);
83 foreach ($params['line_items'] as $lineItems) {
84 $entityParams = $lineItems['params'] ??
[];
85 if (!empty($entityParams) && !empty($lineItems['line_item'])) {
86 $item = reset($lineItems['line_item']);
87 if (!empty($item['membership_type_id'])) {
88 $entity = 'membership';
91 $entity = str_replace('civicrm_', '', $item['entity_table']);
96 $supportedEntity = TRUE;
99 if (isset($entityParams['participant_status_id'])
100 && (!CRM_Event_BAO_ParticipantStatusType
::getIsValidStatusForClass($entityParams['participant_status_id'], 'Pending'))) {
101 throw new CiviCRM_API3_Exception('Creating a participant via the Order API with a non "pending" status is not supported');
103 $entityParams['participant_status_id'] = $entityParams['participant_status_id'] ??
'Pending from incomplete transaction';
104 $entityParams['status_id'] = $entityParams['participant_status_id'];
108 $entityParams['status_id'] = 'Pending';
112 // Don't create any related entities. We might want to support eg. Pledge one day?
113 $supportedEntity = FALSE;
116 if ($supportedEntity) {
117 $entityParams['skipLineItem'] = TRUE;
118 $entityResult = civicrm_api3($entity, 'create', $entityParams);
119 $params['contribution_mode'] = $entity;
120 $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
121 foreach ($lineItems['line_item'] as &$items) {
122 $items['entity_id'] = $entityResult['id'];
127 if (empty($priceSetID)) {
128 $item = reset($lineItems['line_item']);
129 $priceSetID = (int) civicrm_api3('PriceField', 'getvalue', [
130 'return' => 'price_set_id',
131 'id' => $item['price_field_id'],
133 $params['line_item'][$priceSetID] = [];
135 $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
138 $contributionParams = $params;
139 // If this is nested we need to set sequential to 0 as sequential handling is done
140 // in create_success & id will be miscalculated...
141 $contributionParams['sequential'] = 0;
142 foreach ($contributionParams as $key => $value) {
143 // Unset chained keys so the code does not attempt to do this chaining twice.
144 // e.g if calling 'api.Payment.create' We want to finish creating the order first.
145 // it would probably be better to have a full whitelist of contributionParams
146 if (substr($key, 0, 3) === 'api') {
147 unset($contributionParams[$key]);
151 $contribution = civicrm_api3('Contribution', 'create', $contributionParams);
152 $contribution['values'][$contribution['id']]['line_item'] = $params['line_item'][$priceSetID] ??
[];
155 if ($entity && !empty($contribution['id'])) {
156 foreach ($entityIds as $entityId) {
158 'contribution_id' => $contribution['id'],
159 $entity . '_id' => $entityId,
161 // if entity is pledge then build pledge param
162 if ($entity === 'pledge') {
163 $paymentParams +
= $entityParams;
165 elseif ($entity === 'membership') {
166 $paymentParams['isSkipLineItem'] = TRUE;
168 civicrm_api3($entity . '_payment', 'create', $paymentParams);
171 return civicrm_api3_create_success($contribution['values'] ??
[], $params, 'Order', 'create');
177 * @param array $params
181 * @throws API_Exception
182 * @throws CiviCRM_API3_Exception
184 function civicrm_api3_order_delete(array $params): array {
185 $contribution = civicrm_api3('Contribution', 'get', [
186 'return' => ['is_test'],
187 'id' => $params['id'],
189 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
190 $result = civicrm_api3('Contribution', 'delete', $params);
193 throw new API_Exception('Only test orders can be deleted.');
195 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
201 * @param array $params
205 * @throws \CiviCRM_API3_Exception
207 function civicrm_api3_order_cancel(array $params) {
208 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
209 $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
210 $result = civicrm_api3('Contribution', 'create', $params);
211 return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel');
215 * Adjust Metadata for Cancel action.
217 * The metadata is used for setting defaults, documentation & validation.
219 * @param array $params
220 * Array of parameters determined by getfields.
222 function _civicrm_api3_order_cancel_spec(array &$params) {
223 $params['contribution_id'] = [
225 'title' => 'Contribution ID',
226 'type' => CRM_Utils_Type
::T_INT
,
231 * Adjust Metadata for Create action.
233 * The metadata is used for setting defaults, documentation & validation.
235 * @param array $params
236 * Array of parameters determined by getfields.
238 function _civicrm_api3_order_create_spec(array &$params) {
239 $params['contact_id'] = [
240 'name' => 'contact_id',
241 'title' => 'Contact ID',
242 'type' => CRM_Utils_Type
::T_INT
,
243 'api.required' => TRUE,
245 $params['total_amount'] = [
246 'name' => 'total_amount',
247 'title' => 'Total Amount',
249 $params['skipCleanMoney'] = [
250 'api.default' => TRUE,
251 'title' => 'Do not attempt to convert money values',
252 'type' => CRM_Utils_Type
::T_BOOLEAN
,
254 $params['financial_type_id'] = [
255 'name' => 'financial_type_id',
256 'title' => 'Financial Type',
257 'type' => CRM_Utils_Type
::T_INT
,
258 'api.required' => TRUE,
259 'table_name' => 'civicrm_contribution',
260 'entity' => 'Contribution',
261 'bao' => 'CRM_Contribute_BAO_Contribution',
262 'pseudoconstant' => [
263 'table' => 'civicrm_financial_type',
265 'labelColumn' => 'name',
271 * Adjust Metadata for Delete action.
273 * The metadata is used for setting defaults, documentation & validation.
275 * @param array $params
276 * Array of parameters determined by getfields.
278 function _civicrm_api3_order_delete_spec(array &$params) {
279 $params['contribution_id'] = [
280 'api.required' => TRUE,
281 'title' => 'Contribution ID',
282 'type' => CRM_Utils_Type
::T_INT
,
284 $params['id']['api.aliases'] = ['contribution_id'];