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 = []; |
af595ac2 | 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)) { | |
e2887a3c | 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]]"); |
af595ac2 | 80 | } |
e2887a3c | 81 | |
de6c59ca | 82 | if (!empty($params['line_items']) && is_array($params['line_items'])) { |
b8644ae3 PN |
83 | $priceSetID = NULL; |
84 | CRM_Contribute_BAO_Contribution::checkLineItems($params); | |
85 | foreach ($params['line_items'] as $lineItems) { | |
cf8f0fff | 86 | $entityParams = CRM_Utils_Array::value('params', $lineItems, []); |
b8644ae3 PN |
87 | if (!empty($entityParams) && !empty($lineItems['line_item'])) { |
88 | $item = reset($lineItems['line_item']); | |
89 | $entity = str_replace('civicrm_', '', $item['entity_table']); | |
90 | } | |
91 | if ($entityParams) { | |
cf8f0fff | 92 | if (in_array($entity, ['participant', 'membership'])) { |
b8644ae3 | 93 | $entityParams['skipLineItem'] = TRUE; |
af595ac2 | 94 | if ($contributionStatus === 'Pending') { |
95 | $entityParams['status_id'] = ($entity === 'participant' ? 'Pending from incomplete transaction' : 'Pending'); | |
96 | } | |
b8644ae3 PN |
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']; | |
102 | } | |
103 | } | |
104 | else { | |
105 | // pledge payment | |
106 | } | |
107 | } | |
108 | if (empty($priceSetID)) { | |
109 | $item = reset($lineItems['line_item']); | |
cf8f0fff | 110 | $priceSetID = civicrm_api3('PriceField', 'getvalue', [ |
b8644ae3 PN |
111 | 'return' => 'price_set_id', |
112 | 'id' => $item['price_field_id'], | |
cf8f0fff CW |
113 | ]); |
114 | $params['line_item'][$priceSetID] = []; | |
b8644ae3 PN |
115 | } |
116 | $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']); | |
117 | } | |
118 | } | |
e2887a3c | 119 | $contributionParams = $params; |
0dea0c7c | 120 | // If this is nested we need to set sequential to 0 as sequential handling is done |
121 | // in create_success & id will be miscalculated... | |
122 | $contributionParams['sequential'] = 0; | |
e2887a3c | 123 | foreach ($contributionParams as $key => $value) { |
124 | // Unset chained keys so the code does not attempt to do this chaining twice. | |
125 | // e.g if calling 'api.Payment.create' We want to finish creating the order first. | |
126 | // it would probably be better to have a full whitelist of contributionParams | |
127 | if (substr($key, 0, 3) === 'api') { | |
128 | unset($contributionParams[$key]); | |
129 | } | |
130 | } | |
131 | ||
132 | $contribution = civicrm_api3('Contribution', 'create', $contributionParams); | |
b8644ae3 | 133 | // add payments |
de6c59ca | 134 | if ($entity && !empty($contribution['id'])) { |
b8644ae3 | 135 | foreach ($entityIds as $entityId) { |
cf8f0fff | 136 | $paymentParams = [ |
b8644ae3 PN |
137 | 'contribution_id' => $contribution['id'], |
138 | $entity . '_id' => $entityId, | |
cf8f0fff | 139 | ]; |
b8644ae3 PN |
140 | // if entity is pledge then build pledge param |
141 | if ($entity == 'pledge') { | |
142 | $paymentParams += $entityParams; | |
143 | } | |
675b7e61 AM |
144 | elseif ($entity == 'membership') { |
145 | $paymentParams['isSkipLineItem'] = TRUE; | |
146 | } | |
b8644ae3 PN |
147 | $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams); |
148 | } | |
149 | } | |
150 | return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create'); | |
151 | } | |
152 | ||
5f44f698 PN |
153 | /** |
154 | * Delete a Order. | |
155 | * | |
156 | * @param array $params | |
157 | * Input parameters. | |
5f44f698 | 158 | * @return array |
8089541a SL |
159 | * @throws API_Exception |
160 | * @throws CiviCRM_API3_Exception | |
5f44f698 PN |
161 | */ |
162 | function civicrm_api3_order_delete($params) { | |
cf8f0fff CW |
163 | $contribution = civicrm_api3('Contribution', 'get', [ |
164 | 'return' => ['is_test'], | |
95c4e89e | 165 | 'id' => $params['id'], |
cf8f0fff | 166 | ]); |
5f44f698 PN |
167 | if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) { |
168 | $result = civicrm_api3('Contribution', 'delete', $params); | |
169 | } | |
170 | else { | |
95c4e89e | 171 | throw new API_Exception('Only test orders can be deleted.'); |
5f44f698 | 172 | } |
95c4e89e | 173 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete'); |
5f44f698 PN |
174 | } |
175 | ||
65f7f9f6 | 176 | /** |
9d8e81c8 | 177 | * Cancel an Order. |
65f7f9f6 PN |
178 | * |
179 | * @param array $params | |
180 | * Input parameters. | |
181 | * | |
182 | * @return array | |
183 | */ | |
184 | function civicrm_api3_order_cancel($params) { | |
185 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
a5c7fd46 | 186 | $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses); |
65f7f9f6 | 187 | $result = civicrm_api3('Contribution', 'create', $params); |
9d8e81c8 PN |
188 | CRM_Contribute_BAO_Contribution::transitionComponents($params); |
189 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel'); | |
65f7f9f6 PN |
190 | } |
191 | ||
192 | /** | |
193 | * Adjust Metadata for Cancel action. | |
194 | * | |
195 | * The metadata is used for setting defaults, documentation & validation. | |
196 | * | |
197 | * @param array $params | |
198 | * Array of parameters determined by getfields. | |
199 | */ | |
200 | function _civicrm_api3_order_cancel_spec(&$params) { | |
cf8f0fff | 201 | $params['contribution_id'] = [ |
7c31ae57 | 202 | 'api.required' => 1, |
65f7f9f6 PN |
203 | 'title' => 'Contribution ID', |
204 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff | 205 | ]; |
65f7f9f6 PN |
206 | } |
207 | ||
b8644ae3 PN |
208 | /** |
209 | * Adjust Metadata for Create action. | |
210 | * | |
211 | * The metadata is used for setting defaults, documentation & validation. | |
212 | * | |
213 | * @param array $params | |
214 | * Array of parameters determined by getfields. | |
215 | */ | |
216 | function _civicrm_api3_order_create_spec(&$params) { | |
cf8f0fff | 217 | $params['contact_id'] = [ |
b8644ae3 PN |
218 | 'name' => 'contact_id', |
219 | 'title' => 'Contact ID', | |
220 | 'type' => CRM_Utils_Type::T_INT, | |
221 | 'api.required' => TRUE, | |
cf8f0fff CW |
222 | ]; |
223 | $params['total_amount'] = [ | |
b8644ae3 PN |
224 | 'name' => 'total_amount', |
225 | 'title' => 'Total Amount', | |
cf8f0fff | 226 | ]; |
785f03e2 | 227 | $params['financial_type_id'] = [ |
b8644ae3 PN |
228 | 'name' => 'financial_type_id', |
229 | 'title' => 'Financial Type', | |
230 | 'type' => CRM_Utils_Type::T_INT, | |
231 | 'api.required' => TRUE, | |
785f03e2 | 232 | 'table_name' => 'civicrm_contribution', |
233 | 'entity' => 'Contribution', | |
234 | 'bao' => 'CRM_Contribute_BAO_Contribution', | |
235 | 'pseudoconstant' => [ | |
236 | 'table' => 'civicrm_financial_type', | |
237 | 'keyColumn' => 'id', | |
238 | 'labelColumn' => 'name', | |
239 | ], | |
240 | ]; | |
b8644ae3 | 241 | } |
5f44f698 PN |
242 | |
243 | /** | |
244 | * Adjust Metadata for Delete action. | |
245 | * | |
246 | * The metadata is used for setting defaults, documentation & validation. | |
247 | * | |
248 | * @param array $params | |
249 | * Array of parameters determined by getfields. | |
250 | */ | |
251 | function _civicrm_api3_order_delete_spec(&$params) { | |
cf8f0fff | 252 | $params['contribution_id'] = [ |
5f44f698 PN |
253 | 'api.required' => TRUE, |
254 | 'title' => 'Contribution ID', | |
255 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff CW |
256 | ]; |
257 | $params['id']['api.aliases'] = ['contribution_id']; | |
5f44f698 | 258 | } |