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';
79 $order = new CRM_Financial_BAO_Order();
80 $order->setDefaultFinancialTypeID($params['financial_type_id'] ??
NULL);
82 if (!empty($params['line_items']) && is_array($params['line_items'])) {
83 CRM_Contribute_BAO_Contribution
::checkLineItems($params);
84 foreach ($params['line_items'] as $index => $lineItems) {
85 foreach ($lineItems['line_item'] as $innerIndex => $lineItem) {
86 $lineIndex = $index . '+' . $innerIndex;
87 $order->setLineItem($lineItem, $lineIndex);
90 $entityParams = $lineItems['params'] ??
[];
91 $entity = $order->getLineItemEntity($lineIndex);
94 $supportedEntity = TRUE;
97 if (isset($entityParams['participant_status_id'])
98 && (!CRM_Event_BAO_ParticipantStatusType
::getIsValidStatusForClass($entityParams['participant_status_id'], 'Pending'))) {
99 throw new CiviCRM_API3_Exception('Creating a participant via the Order API with a non "pending" status is not supported');
101 $entityParams['participant_status_id'] = $entityParams['participant_status_id'] ??
'Pending from incomplete transaction';
102 $entityParams['status_id'] = $entityParams['participant_status_id'];
103 $params['contribution_mode'] = 'participant';
107 $entityParams['status_id'] = 'Pending';
111 // Don't create any related entities. We might want to support eg. Pledge one day?
112 $supportedEntity = FALSE;
115 if ($supportedEntity) {
116 $entityParams['skipLineItem'] = TRUE;
117 $entityResult = civicrm_api3($entity, 'create', $entityParams);
118 $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
119 foreach ($lineItems['line_item'] as $innerIndex => $lineItem) {
120 $lineIndex = $index . '+' . $innerIndex;
121 $order->setLineItemValue('entity_id', $entityResult['id'], $lineIndex);
126 $priceSetID = $order->getPriceSetID();
127 $params['line_item'][$priceSetID] = $order->getLineItems();
130 $order->setPriceSetToDefault('contribution');
133 $contributionParams = $params;
134 // If this is nested we need to set sequential to 0 as sequential handling is done
135 // in create_success & id will be miscalculated...
136 $contributionParams['sequential'] = 0;
137 foreach ($contributionParams as $key => $value) {
138 // Unset chained keys so the code does not attempt to do this chaining twice.
139 // e.g if calling 'api.Payment.create' We want to finish creating the order first.
140 // it would probably be better to have a full whitelist of contributionParams
141 if (substr($key, 0, 3) === 'api') {
142 unset($contributionParams[$key]);
146 $contribution = civicrm_api3('Contribution', 'create', $contributionParams);
147 $contribution['values'][$contribution['id']]['line_item'] = $order->getLineItems();
150 if ($entity && !empty($contribution['id'])) {
151 foreach ($entityIds as $entityId) {
153 'contribution_id' => $contribution['id'],
154 $entity . '_id' => $entityId,
156 // if entity is pledge then build pledge param
157 if ($entity === 'pledge') {
158 $paymentParams +
= $entityParams;
159 // Pledges are not stored as entity_id in the line_item table.
160 CRM_Core_Error
::deprecatedWarning('This should be unreachable & tests show it is never tested.');
161 civicrm_api3('PledgePayment', 'create', $paymentParams);
163 if ($entity === 'participant') {
164 civicrm_api3('ParticipantPayment', 'create', $paymentParams);
169 return civicrm_api3_create_success($contribution['values'] ??
[], $params, 'Order', 'create');
175 * @param array $params
179 * @throws API_Exception
180 * @throws CiviCRM_API3_Exception
182 function civicrm_api3_order_delete(array $params): array {
183 $contribution = civicrm_api3('Contribution', 'get', [
184 'return' => ['is_test'],
185 'id' => $params['id'],
187 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
188 $result = civicrm_api3('Contribution', 'delete', $params);
191 throw new API_Exception('Only test orders can be deleted.');
193 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
199 * @param array $params
203 * @throws \CiviCRM_API3_Exception
205 function civicrm_api3_order_cancel(array $params) {
206 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
207 $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
208 $result = civicrm_api3('Contribution', 'create', $params);
209 return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel');
213 * Adjust Metadata for Cancel action.
215 * The metadata is used for setting defaults, documentation & validation.
217 * @param array $params
218 * Array of parameters determined by getfields.
220 function _civicrm_api3_order_cancel_spec(array &$params) {
221 $params['contribution_id'] = [
223 'title' => 'Contribution ID',
224 'type' => CRM_Utils_Type
::T_INT
,
229 * Adjust Metadata for Create action.
231 * The metadata is used for setting defaults, documentation & validation.
233 * @param array $params
234 * Array of parameters determined by getfields.
236 function _civicrm_api3_order_create_spec(array &$params) {
237 $params['contact_id'] = [
238 'name' => 'contact_id',
239 'title' => 'Contact ID',
240 'type' => CRM_Utils_Type
::T_INT
,
241 'api.required' => TRUE,
243 $params['total_amount'] = [
244 'name' => 'total_amount',
245 'title' => 'Total Amount',
247 $params['skipCleanMoney'] = [
248 'api.default' => TRUE,
249 'title' => 'Do not attempt to convert money values',
250 'type' => CRM_Utils_Type
::T_BOOLEAN
,
252 $params['financial_type_id'] = [
253 'name' => 'financial_type_id',
254 'title' => 'Financial Type',
255 'type' => CRM_Utils_Type
::T_INT
,
256 'api.required' => TRUE,
257 'table_name' => 'civicrm_contribution',
258 'entity' => 'Contribution',
259 'bao' => 'CRM_Contribute_BAO_Contribution',
260 'pseudoconstant' => [
261 'table' => 'civicrm_financial_type',
263 'labelColumn' => 'name',
269 * Adjust Metadata for Delete action.
271 * The metadata is used for setting defaults, documentation & validation.
273 * @param array $params
274 * Array of parameters determined by getfields.
276 function _civicrm_api3_order_delete_spec(array &$params) {
277 $params['contribution_id'] = [
278 'api.required' => TRUE,
279 'title' => 'Contribution ID',
280 'type' => CRM_Utils_Type
::T_INT
,
282 $params['id']['api.aliases'] = ['contribution_id'];