Merge pull request #20681 from eileenmcnaughton/order_easy
[civicrm-core.git] / api / v3 / Order.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM Order objects, an abstract entity
14 * comprised of contributions and related line items.
15 *
16 * @package CiviCRM_APIv3
17 */
18
19 /**
20 * Retrieve a set of Order.
21 *
22 * @param array $params
23 * Input parameters.
24 *
25 * @return array
26 * Array of Order, if error an array with an error id and error message
27 * @throws \CiviCRM_API3_Exception
28 */
29 function civicrm_api3_order_get(array $params): array {
30 $contributions = [];
31 $params['api.line_item.get'] = ['qty' => ['<>' => 0]];
32 $isSequential = FALSE;
33 if (!empty($params['sequential'])) {
34 $params['sequential'] = 0;
35 $isSequential = TRUE;
36 }
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']);
43 }
44 }
45 $params['sequential'] = $isSequential;
46 return civicrm_api3_create_success($contributions, $params, 'Order', 'get');
47 }
48
49 /**
50 * Adjust Metadata for Get action.
51 *
52 * The metadata is used for setting defaults, documentation & validation.
53 *
54 * @param array $params
55 * Array of parameters determined by getfields.
56 */
57 function _civicrm_api3_order_get_spec(array &$params) {
58 $params['id']['api.aliases'] = ['order_id'];
59 $params['id']['title'] = ts('Contribution / Order ID');
60 }
61
62 /**
63 * Add or update a Order.
64 *
65 * @param array $params
66 * Input parameters.
67 *
68 * @return array
69 * Api result array
70 *
71 * @throws \CiviCRM_API3_Exception
72 * @throws API_Exception
73 */
74 function civicrm_api3_order_create(array $params): array {
75 civicrm_api3_verify_one_mandatory($params, NULL, ['line_items', 'total_amount']);
76 $entity = NULL;
77 $entityIds = [];
78 $params['contribution_status_id'] = 'Pending';
79 $order = new CRM_Financial_BAO_Order();
80 $order->setDefaultFinancialTypeID($params['financial_type_id'] ?? NULL);
81
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);
88 }
89
90 $entityParams = $lineItems['params'] ?? [];
91 $entity = $order->getLineItemEntity($lineIndex);
92
93 if ($entityParams) {
94 $supportedEntity = TRUE;
95 switch ($entity) {
96 case 'participant':
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');
100 }
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';
104 break;
105
106 case 'membership':
107 $entityParams['status_id'] = 'Pending';
108 break;
109
110 default:
111 // Don't create any related entities. We might want to support eg. Pledge one day?
112 $supportedEntity = FALSE;
113 break;
114 }
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);
122 }
123 }
124 }
125 }
126 $priceSetID = $order->getPriceSetID();
127 $params['line_item'][$priceSetID] = $order->getLineItems();
128 }
129 else {
130 $order->setPriceSetToDefault('contribution');
131 }
132
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]);
143 }
144 }
145
146 $contribution = civicrm_api3('Contribution', 'create', $contributionParams);
147 $contribution['values'][$contribution['id']]['line_item'] = $order->getLineItems();
148
149 // add payments
150 if ($entity && !empty($contribution['id'])) {
151 foreach ($entityIds as $entityId) {
152 $paymentParams = [
153 'contribution_id' => $contribution['id'],
154 $entity . '_id' => $entityId,
155 ];
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);
162 }
163 if ($entity === 'participant') {
164 civicrm_api3('ParticipantPayment', 'create', $paymentParams);
165 }
166
167 }
168 }
169 return civicrm_api3_create_success($contribution['values'] ?? [], $params, 'Order', 'create');
170 }
171
172 /**
173 * Delete a Order.
174 *
175 * @param array $params
176 * Input parameters.
177 *
178 * @return array
179 * @throws API_Exception
180 * @throws CiviCRM_API3_Exception
181 */
182 function civicrm_api3_order_delete(array $params): array {
183 $contribution = civicrm_api3('Contribution', 'get', [
184 'return' => ['is_test'],
185 'id' => $params['id'],
186 ]);
187 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
188 $result = civicrm_api3('Contribution', 'delete', $params);
189 }
190 else {
191 throw new API_Exception('Only test orders can be deleted.');
192 }
193 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
194 }
195
196 /**
197 * Cancel an Order.
198 *
199 * @param array $params
200 * Input parameters.
201 *
202 * @return array
203 * @throws \CiviCRM_API3_Exception
204 */
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');
210 }
211
212 /**
213 * Adjust Metadata for Cancel action.
214 *
215 * The metadata is used for setting defaults, documentation & validation.
216 *
217 * @param array $params
218 * Array of parameters determined by getfields.
219 */
220 function _civicrm_api3_order_cancel_spec(array &$params) {
221 $params['contribution_id'] = [
222 'api.required' => 1,
223 'title' => 'Contribution ID',
224 'type' => CRM_Utils_Type::T_INT,
225 ];
226 }
227
228 /**
229 * Adjust Metadata for Create action.
230 *
231 * The metadata is used for setting defaults, documentation & validation.
232 *
233 * @param array $params
234 * Array of parameters determined by getfields.
235 */
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,
242 ];
243 $params['total_amount'] = [
244 'name' => 'total_amount',
245 'title' => 'Total Amount',
246 ];
247 $params['skipCleanMoney'] = [
248 'api.default' => TRUE,
249 'title' => 'Do not attempt to convert money values',
250 'type' => CRM_Utils_Type::T_BOOLEAN,
251 ];
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',
262 'keyColumn' => 'id',
263 'labelColumn' => 'name',
264 ],
265 ];
266 }
267
268 /**
269 * Adjust Metadata for Delete action.
270 *
271 * The metadata is used for setting defaults, documentation & validation.
272 *
273 * @param array $params
274 * Array of parameters determined by getfields.
275 */
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,
281 ];
282 $params['id']['api.aliases'] = ['contribution_id'];
283 }