Commit | Line | Data |
---|---|---|
31a99426 PN |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
31a99426 | 5 | | | |
a30c801b TO |
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 | | |
31a99426 PN |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
7cad285e JM |
13 | * This api exposes CiviCRM Order objects, an abstract entity |
14 | * comprised of contributions and related line items. | |
31a99426 PN |
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 | */ | |
28 | function civicrm_api3_order_get($params) { | |
cf8f0fff CW |
29 | $contributions = []; |
30 | $params['api.line_item.get'] = ['qty' => ['<>' => 0]]; | |
31a99426 | 31 | $isSequential = FALSE; |
de6c59ca | 32 | if (!empty($params['sequential'])) { |
31a99426 PN |
33 | $params['sequential'] = 0; |
34 | $isSequential = TRUE; | |
35 | } | |
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']); | |
42 | } | |
43 | } | |
44 | $params['sequential'] = $isSequential; | |
45 | return civicrm_api3_create_success($contributions, $params, 'Order', 'get'); | |
46 | } | |
b8644ae3 | 47 | |
785f03e2 | 48 | /** |
49 | * Adjust Metadata for Get action. | |
50 | * | |
51 | * The metadata is used for setting defaults, documentation & validation. | |
52 | * | |
53 | * @param array $params | |
54 | * Array of parameters determined by getfields. | |
55 | */ | |
56 | function _civicrm_api3_order_get_spec(&$params) { | |
57 | $params['id']['api.aliases'] = ['order_id']; | |
58 | $params['id']['title'] = ts('Contribution / Order ID'); | |
59 | } | |
60 | ||
b8644ae3 PN |
61 | /** |
62 | * Add or update a Order. | |
63 | * | |
64 | * @param array $params | |
65 | * Input parameters. | |
66 | * | |
b8644ae3 PN |
67 | * @return array |
68 | * Api result array | |
9c5edcd4 | 69 | * |
70 | * @throws \CiviCRM_API3_Exception | |
71 | * @throws API_Exception | |
b8644ae3 | 72 | */ |
1c31aa41 | 73 | function civicrm_api3_order_create($params) { |
9c5edcd4 | 74 | civicrm_api3_verify_one_mandatory($params, NULL, ['line_items', 'total_amount']); |
b8644ae3 | 75 | $entity = NULL; |
cf8f0fff | 76 | $entityIds = []; |
5ad21509 | 77 | $params['contribution_status_id'] = 'Pending'; |
e2887a3c | 78 | |
de6c59ca | 79 | if (!empty($params['line_items']) && is_array($params['line_items'])) { |
b8644ae3 PN |
80 | $priceSetID = NULL; |
81 | CRM_Contribute_BAO_Contribution::checkLineItems($params); | |
82 | foreach ($params['line_items'] as $lineItems) { | |
dffc9c5f | 83 | $entityParams = $lineItems['params'] ?? []; |
b8644ae3 PN |
84 | if (!empty($entityParams) && !empty($lineItems['line_item'])) { |
85 | $item = reset($lineItems['line_item']); | |
86 | $entity = str_replace('civicrm_', '', $item['entity_table']); | |
87 | } | |
33f79146 | 88 | |
b8644ae3 | 89 | if ($entityParams) { |
33f79146 MW |
90 | $supportedEntity = TRUE; |
91 | switch ($entity) { | |
92 | case 'participant': | |
62afd697 MW |
93 | if (isset($entityParams['participant_status_id']) |
94 | && (!CRM_Event_BAO_ParticipantStatusType::getIsValidStatusForClass($entityParams['participant_status_id'], 'Pending'))) { | |
95 | throw new CiviCRM_API3_Exception('Creating a participant via the Order API with a non "pending" status is not supported'); | |
96 | } | |
97 | $entityParams['participant_status_id'] = $entityParams['participant_status_id'] ?? 'Pending from incomplete transaction'; | |
98 | $entityParams['status_id'] = $entityParams['participant_status_id']; | |
33f79146 MW |
99 | break; |
100 | ||
101 | case 'membership': | |
102 | $entityParams['status_id'] = 'Pending'; | |
103 | break; | |
104 | ||
105 | default: | |
106 | // Don't create any related entities. We might want to support eg. Pledge one day? | |
107 | $supportedEntity = FALSE; | |
108 | break; | |
109 | } | |
110 | if ($supportedEntity) { | |
b8644ae3 PN |
111 | $entityParams['skipLineItem'] = TRUE; |
112 | $entityResult = civicrm_api3($entity, 'create', $entityParams); | |
113 | $params['contribution_mode'] = $entity; | |
114 | $entityIds[] = $params[$entity . '_id'] = $entityResult['id']; | |
115 | foreach ($lineItems['line_item'] as &$items) { | |
116 | $items['entity_id'] = $entityResult['id']; | |
117 | } | |
118 | } | |
b8644ae3 | 119 | } |
33f79146 | 120 | |
b8644ae3 PN |
121 | if (empty($priceSetID)) { |
122 | $item = reset($lineItems['line_item']); | |
dffc9c5f | 123 | $priceSetID = (int) civicrm_api3('PriceField', 'getvalue', [ |
b8644ae3 PN |
124 | 'return' => 'price_set_id', |
125 | 'id' => $item['price_field_id'], | |
cf8f0fff CW |
126 | ]); |
127 | $params['line_item'][$priceSetID] = []; | |
b8644ae3 PN |
128 | } |
129 | $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']); | |
130 | } | |
131 | } | |
e2887a3c | 132 | $contributionParams = $params; |
0dea0c7c | 133 | // If this is nested we need to set sequential to 0 as sequential handling is done |
134 | // in create_success & id will be miscalculated... | |
135 | $contributionParams['sequential'] = 0; | |
e2887a3c | 136 | foreach ($contributionParams as $key => $value) { |
137 | // Unset chained keys so the code does not attempt to do this chaining twice. | |
138 | // e.g if calling 'api.Payment.create' We want to finish creating the order first. | |
139 | // it would probably be better to have a full whitelist of contributionParams | |
140 | if (substr($key, 0, 3) === 'api') { | |
141 | unset($contributionParams[$key]); | |
142 | } | |
143 | } | |
144 | ||
145 | $contribution = civicrm_api3('Contribution', 'create', $contributionParams); | |
b8644ae3 | 146 | // add payments |
de6c59ca | 147 | if ($entity && !empty($contribution['id'])) { |
b8644ae3 | 148 | foreach ($entityIds as $entityId) { |
cf8f0fff | 149 | $paymentParams = [ |
b8644ae3 PN |
150 | 'contribution_id' => $contribution['id'], |
151 | $entity . '_id' => $entityId, | |
cf8f0fff | 152 | ]; |
b8644ae3 PN |
153 | // if entity is pledge then build pledge param |
154 | if ($entity == 'pledge') { | |
155 | $paymentParams += $entityParams; | |
156 | } | |
675b7e61 AM |
157 | elseif ($entity == 'membership') { |
158 | $paymentParams['isSkipLineItem'] = TRUE; | |
159 | } | |
dffc9c5f | 160 | civicrm_api3($entity . '_payment', 'create', $paymentParams); |
b8644ae3 PN |
161 | } |
162 | } | |
dffc9c5f | 163 | return civicrm_api3_create_success($contribution['values'] ?? [], $params, 'Order', 'create'); |
b8644ae3 PN |
164 | } |
165 | ||
5f44f698 PN |
166 | /** |
167 | * Delete a Order. | |
168 | * | |
169 | * @param array $params | |
170 | * Input parameters. | |
5f44f698 | 171 | * @return array |
8089541a SL |
172 | * @throws API_Exception |
173 | * @throws CiviCRM_API3_Exception | |
5f44f698 PN |
174 | */ |
175 | function civicrm_api3_order_delete($params) { | |
cf8f0fff CW |
176 | $contribution = civicrm_api3('Contribution', 'get', [ |
177 | 'return' => ['is_test'], | |
95c4e89e | 178 | 'id' => $params['id'], |
cf8f0fff | 179 | ]); |
5f44f698 PN |
180 | if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) { |
181 | $result = civicrm_api3('Contribution', 'delete', $params); | |
182 | } | |
183 | else { | |
95c4e89e | 184 | throw new API_Exception('Only test orders can be deleted.'); |
5f44f698 | 185 | } |
95c4e89e | 186 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete'); |
5f44f698 PN |
187 | } |
188 | ||
65f7f9f6 | 189 | /** |
9d8e81c8 | 190 | * Cancel an Order. |
65f7f9f6 PN |
191 | * |
192 | * @param array $params | |
193 | * Input parameters. | |
194 | * | |
195 | * @return array | |
196 | */ | |
197 | function civicrm_api3_order_cancel($params) { | |
198 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
a5c7fd46 | 199 | $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses); |
65f7f9f6 | 200 | $result = civicrm_api3('Contribution', 'create', $params); |
9d8e81c8 | 201 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel'); |
65f7f9f6 PN |
202 | } |
203 | ||
204 | /** | |
205 | * Adjust Metadata for Cancel action. | |
206 | * | |
207 | * The metadata is used for setting defaults, documentation & validation. | |
208 | * | |
209 | * @param array $params | |
210 | * Array of parameters determined by getfields. | |
211 | */ | |
212 | function _civicrm_api3_order_cancel_spec(&$params) { | |
cf8f0fff | 213 | $params['contribution_id'] = [ |
7c31ae57 | 214 | 'api.required' => 1, |
65f7f9f6 PN |
215 | 'title' => 'Contribution ID', |
216 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff | 217 | ]; |
65f7f9f6 PN |
218 | } |
219 | ||
b8644ae3 PN |
220 | /** |
221 | * Adjust Metadata for Create action. | |
222 | * | |
223 | * The metadata is used for setting defaults, documentation & validation. | |
224 | * | |
225 | * @param array $params | |
226 | * Array of parameters determined by getfields. | |
227 | */ | |
228 | function _civicrm_api3_order_create_spec(&$params) { | |
cf8f0fff | 229 | $params['contact_id'] = [ |
b8644ae3 PN |
230 | 'name' => 'contact_id', |
231 | 'title' => 'Contact ID', | |
232 | 'type' => CRM_Utils_Type::T_INT, | |
233 | 'api.required' => TRUE, | |
cf8f0fff CW |
234 | ]; |
235 | $params['total_amount'] = [ | |
b8644ae3 PN |
236 | 'name' => 'total_amount', |
237 | 'title' => 'Total Amount', | |
cf8f0fff | 238 | ]; |
2c35902f | 239 | $params['skipCleanMoney'] = [ |
240 | 'api.default' => TRUE, | |
241 | 'title' => 'Do not attempt to convert money values', | |
242 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
243 | ]; | |
785f03e2 | 244 | $params['financial_type_id'] = [ |
b8644ae3 PN |
245 | 'name' => 'financial_type_id', |
246 | 'title' => 'Financial Type', | |
247 | 'type' => CRM_Utils_Type::T_INT, | |
248 | 'api.required' => TRUE, | |
785f03e2 | 249 | 'table_name' => 'civicrm_contribution', |
250 | 'entity' => 'Contribution', | |
251 | 'bao' => 'CRM_Contribute_BAO_Contribution', | |
252 | 'pseudoconstant' => [ | |
253 | 'table' => 'civicrm_financial_type', | |
254 | 'keyColumn' => 'id', | |
255 | 'labelColumn' => 'name', | |
256 | ], | |
257 | ]; | |
b8644ae3 | 258 | } |
5f44f698 PN |
259 | |
260 | /** | |
261 | * Adjust Metadata for Delete action. | |
262 | * | |
263 | * The metadata is used for setting defaults, documentation & validation. | |
264 | * | |
265 | * @param array $params | |
266 | * Array of parameters determined by getfields. | |
267 | */ | |
268 | function _civicrm_api3_order_delete_spec(&$params) { | |
cf8f0fff | 269 | $params['contribution_id'] = [ |
5f44f698 PN |
270 | 'api.required' => TRUE, |
271 | 'title' => 'Contribution ID', | |
272 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff CW |
273 | ]; |
274 | $params['id']['api.aliases'] = ['contribution_id']; | |
5f44f698 | 275 | } |