Commit | Line | Data |
---|---|---|
31a99426 PN |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
31a99426 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
31a99426 PN |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
7cad285e JM |
29 | * This api exposes CiviCRM Order objects, an abstract entity |
30 | * comprised of contributions and related line items. | |
31a99426 PN |
31 | * |
32 | * @package CiviCRM_APIv3 | |
33 | */ | |
34 | ||
35 | /** | |
36 | * Retrieve a set of Order. | |
37 | * | |
38 | * @param array $params | |
39 | * Input parameters. | |
40 | * | |
41 | * @return array | |
42 | * Array of Order, if error an array with an error id and error message | |
43 | */ | |
44 | function civicrm_api3_order_get($params) { | |
cf8f0fff CW |
45 | $contributions = []; |
46 | $params['api.line_item.get'] = ['qty' => ['<>' => 0]]; | |
31a99426 | 47 | $isSequential = FALSE; |
de6c59ca | 48 | if (!empty($params['sequential'])) { |
31a99426 PN |
49 | $params['sequential'] = 0; |
50 | $isSequential = TRUE; | |
51 | } | |
52 | $result = civicrm_api3('Contribution', 'get', $params); | |
53 | if (!empty($result['values'])) { | |
54 | foreach ($result['values'] as $key => $contribution) { | |
55 | $contributions[$key] = $contribution; | |
56 | $contributions[$key]['line_items'] = $contribution['api.line_item.get']['values']; | |
57 | unset($contributions[$key]['api.line_item.get']); | |
58 | } | |
59 | } | |
60 | $params['sequential'] = $isSequential; | |
61 | return civicrm_api3_create_success($contributions, $params, 'Order', 'get'); | |
62 | } | |
b8644ae3 | 63 | |
785f03e2 | 64 | /** |
65 | * Adjust Metadata for Get action. | |
66 | * | |
67 | * The metadata is used for setting defaults, documentation & validation. | |
68 | * | |
69 | * @param array $params | |
70 | * Array of parameters determined by getfields. | |
71 | */ | |
72 | function _civicrm_api3_order_get_spec(&$params) { | |
73 | $params['id']['api.aliases'] = ['order_id']; | |
74 | $params['id']['title'] = ts('Contribution / Order ID'); | |
75 | } | |
76 | ||
b8644ae3 PN |
77 | /** |
78 | * Add or update a Order. | |
79 | * | |
80 | * @param array $params | |
81 | * Input parameters. | |
82 | * | |
b8644ae3 PN |
83 | * @return array |
84 | * Api result array | |
9c5edcd4 | 85 | * |
86 | * @throws \CiviCRM_API3_Exception | |
87 | * @throws API_Exception | |
b8644ae3 | 88 | */ |
1c31aa41 | 89 | function civicrm_api3_order_create($params) { |
9c5edcd4 | 90 | civicrm_api3_verify_one_mandatory($params, NULL, ['line_items', 'total_amount']); |
b8644ae3 | 91 | $entity = NULL; |
cf8f0fff | 92 | $entityIds = []; |
af595ac2 | 93 | $contributionStatus = CRM_Utils_Array::value('contribution_status_id', $params); |
94 | if ($contributionStatus !== 'Pending' && 'Pending' !== CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contributionStatus)) { | |
e2887a3c | 95 | 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 | 96 | } |
e2887a3c | 97 | |
de6c59ca | 98 | if (!empty($params['line_items']) && is_array($params['line_items'])) { |
b8644ae3 PN |
99 | $priceSetID = NULL; |
100 | CRM_Contribute_BAO_Contribution::checkLineItems($params); | |
101 | foreach ($params['line_items'] as $lineItems) { | |
cf8f0fff | 102 | $entityParams = CRM_Utils_Array::value('params', $lineItems, []); |
b8644ae3 PN |
103 | if (!empty($entityParams) && !empty($lineItems['line_item'])) { |
104 | $item = reset($lineItems['line_item']); | |
105 | $entity = str_replace('civicrm_', '', $item['entity_table']); | |
106 | } | |
107 | if ($entityParams) { | |
cf8f0fff | 108 | if (in_array($entity, ['participant', 'membership'])) { |
b8644ae3 | 109 | $entityParams['skipLineItem'] = TRUE; |
af595ac2 | 110 | if ($contributionStatus === 'Pending') { |
111 | $entityParams['status_id'] = ($entity === 'participant' ? 'Pending from incomplete transaction' : 'Pending'); | |
112 | } | |
b8644ae3 PN |
113 | $entityResult = civicrm_api3($entity, 'create', $entityParams); |
114 | $params['contribution_mode'] = $entity; | |
115 | $entityIds[] = $params[$entity . '_id'] = $entityResult['id']; | |
116 | foreach ($lineItems['line_item'] as &$items) { | |
117 | $items['entity_id'] = $entityResult['id']; | |
118 | } | |
119 | } | |
120 | else { | |
121 | // pledge payment | |
122 | } | |
123 | } | |
124 | if (empty($priceSetID)) { | |
125 | $item = reset($lineItems['line_item']); | |
cf8f0fff | 126 | $priceSetID = civicrm_api3('PriceField', 'getvalue', [ |
b8644ae3 PN |
127 | 'return' => 'price_set_id', |
128 | 'id' => $item['price_field_id'], | |
cf8f0fff CW |
129 | ]); |
130 | $params['line_item'][$priceSetID] = []; | |
b8644ae3 PN |
131 | } |
132 | $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']); | |
133 | } | |
134 | } | |
e2887a3c | 135 | $contributionParams = $params; |
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 | } | |
157 | $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams); | |
158 | } | |
159 | } | |
160 | return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create'); | |
161 | } | |
162 | ||
5f44f698 PN |
163 | /** |
164 | * Delete a Order. | |
165 | * | |
166 | * @param array $params | |
167 | * Input parameters. | |
5f44f698 | 168 | * @return array |
8089541a SL |
169 | * @throws API_Exception |
170 | * @throws CiviCRM_API3_Exception | |
5f44f698 PN |
171 | */ |
172 | function civicrm_api3_order_delete($params) { | |
cf8f0fff CW |
173 | $contribution = civicrm_api3('Contribution', 'get', [ |
174 | 'return' => ['is_test'], | |
95c4e89e | 175 | 'id' => $params['id'], |
cf8f0fff | 176 | ]); |
5f44f698 PN |
177 | if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) { |
178 | $result = civicrm_api3('Contribution', 'delete', $params); | |
179 | } | |
180 | else { | |
95c4e89e | 181 | throw new API_Exception('Only test orders can be deleted.'); |
5f44f698 | 182 | } |
95c4e89e | 183 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete'); |
5f44f698 PN |
184 | } |
185 | ||
65f7f9f6 | 186 | /** |
9d8e81c8 | 187 | * Cancel an Order. |
65f7f9f6 PN |
188 | * |
189 | * @param array $params | |
190 | * Input parameters. | |
191 | * | |
192 | * @return array | |
193 | */ | |
194 | function civicrm_api3_order_cancel($params) { | |
195 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
a5c7fd46 | 196 | $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses); |
65f7f9f6 | 197 | $result = civicrm_api3('Contribution', 'create', $params); |
9d8e81c8 PN |
198 | CRM_Contribute_BAO_Contribution::transitionComponents($params); |
199 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel'); | |
65f7f9f6 PN |
200 | } |
201 | ||
202 | /** | |
203 | * Adjust Metadata for Cancel action. | |
204 | * | |
205 | * The metadata is used for setting defaults, documentation & validation. | |
206 | * | |
207 | * @param array $params | |
208 | * Array of parameters determined by getfields. | |
209 | */ | |
210 | function _civicrm_api3_order_cancel_spec(&$params) { | |
cf8f0fff | 211 | $params['contribution_id'] = [ |
7c31ae57 | 212 | 'api.required' => 1, |
65f7f9f6 PN |
213 | 'title' => 'Contribution ID', |
214 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff | 215 | ]; |
65f7f9f6 PN |
216 | } |
217 | ||
b8644ae3 PN |
218 | /** |
219 | * Adjust Metadata for Create action. | |
220 | * | |
221 | * The metadata is used for setting defaults, documentation & validation. | |
222 | * | |
223 | * @param array $params | |
224 | * Array of parameters determined by getfields. | |
225 | */ | |
226 | function _civicrm_api3_order_create_spec(&$params) { | |
cf8f0fff | 227 | $params['contact_id'] = [ |
b8644ae3 PN |
228 | 'name' => 'contact_id', |
229 | 'title' => 'Contact ID', | |
230 | 'type' => CRM_Utils_Type::T_INT, | |
231 | 'api.required' => TRUE, | |
cf8f0fff CW |
232 | ]; |
233 | $params['total_amount'] = [ | |
b8644ae3 PN |
234 | 'name' => 'total_amount', |
235 | 'title' => 'Total Amount', | |
cf8f0fff | 236 | ]; |
785f03e2 | 237 | $params['financial_type_id'] = [ |
b8644ae3 PN |
238 | 'name' => 'financial_type_id', |
239 | 'title' => 'Financial Type', | |
240 | 'type' => CRM_Utils_Type::T_INT, | |
241 | 'api.required' => TRUE, | |
785f03e2 | 242 | 'table_name' => 'civicrm_contribution', |
243 | 'entity' => 'Contribution', | |
244 | 'bao' => 'CRM_Contribute_BAO_Contribution', | |
245 | 'pseudoconstant' => [ | |
246 | 'table' => 'civicrm_financial_type', | |
247 | 'keyColumn' => 'id', | |
248 | 'labelColumn' => 'name', | |
249 | ], | |
250 | ]; | |
b8644ae3 | 251 | } |
5f44f698 PN |
252 | |
253 | /** | |
254 | * Adjust Metadata for Delete action. | |
255 | * | |
256 | * The metadata is used for setting defaults, documentation & validation. | |
257 | * | |
258 | * @param array $params | |
259 | * Array of parameters determined by getfields. | |
260 | */ | |
261 | function _civicrm_api3_order_delete_spec(&$params) { | |
cf8f0fff | 262 | $params['contribution_id'] = [ |
5f44f698 PN |
263 | 'api.required' => TRUE, |
264 | 'title' => 'Contribution ID', | |
265 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff CW |
266 | ]; |
267 | $params['id']['api.aliases'] = ['contribution_id']; | |
5f44f698 | 268 | } |