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