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 PN |
47 | $isSequential = FALSE; |
48 | if (CRM_Utils_Array::value('sequential', $params)) { | |
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 | * | |
83 | * @throws API_Exception | |
84 | * @return array | |
85 | * Api result array | |
86 | */ | |
87 | function civicrm_api3_order_create(&$params) { | |
785f03e2 | 88 | |
b8644ae3 | 89 | $entity = NULL; |
cf8f0fff | 90 | $entityIds = []; |
b8644ae3 PN |
91 | if (CRM_Utils_Array::value('line_items', $params) && is_array($params['line_items'])) { |
92 | $priceSetID = NULL; | |
93 | CRM_Contribute_BAO_Contribution::checkLineItems($params); | |
94 | foreach ($params['line_items'] as $lineItems) { | |
cf8f0fff | 95 | $entityParams = CRM_Utils_Array::value('params', $lineItems, []); |
b8644ae3 PN |
96 | if (!empty($entityParams) && !empty($lineItems['line_item'])) { |
97 | $item = reset($lineItems['line_item']); | |
98 | $entity = str_replace('civicrm_', '', $item['entity_table']); | |
99 | } | |
100 | if ($entityParams) { | |
cf8f0fff | 101 | if (in_array($entity, ['participant', 'membership'])) { |
b8644ae3 PN |
102 | $entityParams['skipLineItem'] = TRUE; |
103 | $entityResult = civicrm_api3($entity, 'create', $entityParams); | |
104 | $params['contribution_mode'] = $entity; | |
105 | $entityIds[] = $params[$entity . '_id'] = $entityResult['id']; | |
106 | foreach ($lineItems['line_item'] as &$items) { | |
107 | $items['entity_id'] = $entityResult['id']; | |
108 | } | |
109 | } | |
110 | else { | |
111 | // pledge payment | |
112 | } | |
113 | } | |
114 | if (empty($priceSetID)) { | |
115 | $item = reset($lineItems['line_item']); | |
cf8f0fff | 116 | $priceSetID = civicrm_api3('PriceField', 'getvalue', [ |
b8644ae3 PN |
117 | 'return' => 'price_set_id', |
118 | 'id' => $item['price_field_id'], | |
cf8f0fff CW |
119 | ]); |
120 | $params['line_item'][$priceSetID] = []; | |
b8644ae3 PN |
121 | } |
122 | $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']); | |
123 | } | |
124 | } | |
125 | $contribution = civicrm_api3('Contribution', 'create', $params); | |
126 | // add payments | |
127 | if ($entity && CRM_Utils_Array::value('id', $contribution)) { | |
128 | foreach ($entityIds as $entityId) { | |
cf8f0fff | 129 | $paymentParams = [ |
b8644ae3 PN |
130 | 'contribution_id' => $contribution['id'], |
131 | $entity . '_id' => $entityId, | |
cf8f0fff | 132 | ]; |
b8644ae3 PN |
133 | // if entity is pledge then build pledge param |
134 | if ($entity == 'pledge') { | |
135 | $paymentParams += $entityParams; | |
136 | } | |
137 | $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams); | |
138 | } | |
139 | } | |
140 | return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create'); | |
141 | } | |
142 | ||
5f44f698 PN |
143 | /** |
144 | * Delete a Order. | |
145 | * | |
146 | * @param array $params | |
147 | * Input parameters. | |
5f44f698 | 148 | * @return array |
8089541a SL |
149 | * @throws API_Exception |
150 | * @throws CiviCRM_API3_Exception | |
5f44f698 PN |
151 | */ |
152 | function civicrm_api3_order_delete($params) { | |
cf8f0fff CW |
153 | $contribution = civicrm_api3('Contribution', 'get', [ |
154 | 'return' => ['is_test'], | |
95c4e89e | 155 | 'id' => $params['id'], |
cf8f0fff | 156 | ]); |
5f44f698 PN |
157 | if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) { |
158 | $result = civicrm_api3('Contribution', 'delete', $params); | |
159 | } | |
160 | else { | |
95c4e89e | 161 | throw new API_Exception('Only test orders can be deleted.'); |
5f44f698 | 162 | } |
95c4e89e | 163 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete'); |
5f44f698 PN |
164 | } |
165 | ||
65f7f9f6 | 166 | /** |
9d8e81c8 | 167 | * Cancel an Order. |
65f7f9f6 PN |
168 | * |
169 | * @param array $params | |
170 | * Input parameters. | |
171 | * | |
172 | * @return array | |
173 | */ | |
174 | function civicrm_api3_order_cancel($params) { | |
175 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
a5c7fd46 | 176 | $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses); |
65f7f9f6 | 177 | $result = civicrm_api3('Contribution', 'create', $params); |
9d8e81c8 PN |
178 | CRM_Contribute_BAO_Contribution::transitionComponents($params); |
179 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel'); | |
65f7f9f6 PN |
180 | } |
181 | ||
182 | /** | |
183 | * Adjust Metadata for Cancel action. | |
184 | * | |
185 | * The metadata is used for setting defaults, documentation & validation. | |
186 | * | |
187 | * @param array $params | |
188 | * Array of parameters determined by getfields. | |
189 | */ | |
190 | function _civicrm_api3_order_cancel_spec(&$params) { | |
cf8f0fff | 191 | $params['contribution_id'] = [ |
7c31ae57 | 192 | 'api.required' => 1, |
65f7f9f6 PN |
193 | 'title' => 'Contribution ID', |
194 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff | 195 | ]; |
65f7f9f6 PN |
196 | } |
197 | ||
b8644ae3 PN |
198 | /** |
199 | * Adjust Metadata for Create action. | |
200 | * | |
201 | * The metadata is used for setting defaults, documentation & validation. | |
202 | * | |
203 | * @param array $params | |
204 | * Array of parameters determined by getfields. | |
205 | */ | |
206 | function _civicrm_api3_order_create_spec(&$params) { | |
cf8f0fff | 207 | $params['contact_id'] = [ |
b8644ae3 PN |
208 | 'name' => 'contact_id', |
209 | 'title' => 'Contact ID', | |
210 | 'type' => CRM_Utils_Type::T_INT, | |
211 | 'api.required' => TRUE, | |
cf8f0fff CW |
212 | ]; |
213 | $params['total_amount'] = [ | |
b8644ae3 PN |
214 | 'name' => 'total_amount', |
215 | 'title' => 'Total Amount', | |
216 | 'api.required' => TRUE, | |
cf8f0fff | 217 | ]; |
785f03e2 | 218 | $params['financial_type_id'] = [ |
b8644ae3 PN |
219 | 'name' => 'financial_type_id', |
220 | 'title' => 'Financial Type', | |
221 | 'type' => CRM_Utils_Type::T_INT, | |
222 | 'api.required' => TRUE, | |
785f03e2 | 223 | 'table_name' => 'civicrm_contribution', |
224 | 'entity' => 'Contribution', | |
225 | 'bao' => 'CRM_Contribute_BAO_Contribution', | |
226 | 'pseudoconstant' => [ | |
227 | 'table' => 'civicrm_financial_type', | |
228 | 'keyColumn' => 'id', | |
229 | 'labelColumn' => 'name', | |
230 | ], | |
231 | ]; | |
b8644ae3 | 232 | } |
5f44f698 PN |
233 | |
234 | /** | |
235 | * Adjust Metadata for Delete action. | |
236 | * | |
237 | * The metadata is used for setting defaults, documentation & validation. | |
238 | * | |
239 | * @param array $params | |
240 | * Array of parameters determined by getfields. | |
241 | */ | |
242 | function _civicrm_api3_order_delete_spec(&$params) { | |
cf8f0fff | 243 | $params['contribution_id'] = [ |
5f44f698 PN |
244 | 'api.required' => TRUE, |
245 | 'title' => 'Contribution ID', | |
246 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff CW |
247 | ]; |
248 | $params['id']['api.aliases'] = ['contribution_id']; | |
5f44f698 | 249 | } |