Merge pull request #15697 from artfulrobot/payment-property-bag
[civicrm-core.git] / api / v3 / Order.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM Order objects, an abstract entity
14 * comprised of contributions and related line items.
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) {
29 $contributions = [];
30 $params['api.line_item.get'] = ['qty' => ['<>' => 0]];
31 $isSequential = FALSE;
32 if (!empty($params['sequential'])) {
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 }
47
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
61 /**
62 * Add or update a Order.
63 *
64 * @param array $params
65 * Input parameters.
66 *
67 * @return array
68 * Api result array
69 *
70 * @throws \CiviCRM_API3_Exception
71 * @throws API_Exception
72 */
73 function civicrm_api3_order_create($params) {
74 civicrm_api3_verify_one_mandatory($params, NULL, ['line_items', 'total_amount']);
75 $entity = NULL;
76 $entityIds = [];
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)) {
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]]");
80 }
81
82 if (!empty($params['line_items']) && is_array($params['line_items'])) {
83 $priceSetID = NULL;
84 CRM_Contribute_BAO_Contribution::checkLineItems($params);
85 foreach ($params['line_items'] as $lineItems) {
86 $entityParams = CRM_Utils_Array::value('params', $lineItems, []);
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) {
92 if (in_array($entity, ['participant', 'membership'])) {
93 $entityParams['skipLineItem'] = TRUE;
94 if ($contributionStatus === 'Pending') {
95 $entityParams['status_id'] = ($entity === 'participant' ? 'Pending from incomplete transaction' : 'Pending');
96 }
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']);
110 $priceSetID = civicrm_api3('PriceField', 'getvalue', [
111 'return' => 'price_set_id',
112 'id' => $item['price_field_id'],
113 ]);
114 $params['line_item'][$priceSetID] = [];
115 }
116 $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
117 }
118 }
119 $contributionParams = $params;
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;
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);
133 // add payments
134 if ($entity && !empty($contribution['id'])) {
135 foreach ($entityIds as $entityId) {
136 $paymentParams = [
137 'contribution_id' => $contribution['id'],
138 $entity . '_id' => $entityId,
139 ];
140 // if entity is pledge then build pledge param
141 if ($entity == 'pledge') {
142 $paymentParams += $entityParams;
143 }
144 $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams);
145 }
146 }
147 return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create');
148 }
149
150 /**
151 * Delete a Order.
152 *
153 * @param array $params
154 * Input parameters.
155 * @return array
156 * @throws API_Exception
157 * @throws CiviCRM_API3_Exception
158 */
159 function civicrm_api3_order_delete($params) {
160 $contribution = civicrm_api3('Contribution', 'get', [
161 'return' => ['is_test'],
162 'id' => $params['id'],
163 ]);
164 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
165 $result = civicrm_api3('Contribution', 'delete', $params);
166 }
167 else {
168 throw new API_Exception('Only test orders can be deleted.');
169 }
170 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
171 }
172
173 /**
174 * Cancel an Order.
175 *
176 * @param array $params
177 * Input parameters.
178 *
179 * @return array
180 */
181 function civicrm_api3_order_cancel($params) {
182 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
183 $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
184 $result = civicrm_api3('Contribution', 'create', $params);
185 CRM_Contribute_BAO_Contribution::transitionComponents($params);
186 return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel');
187 }
188
189 /**
190 * Adjust Metadata for Cancel action.
191 *
192 * The metadata is used for setting defaults, documentation & validation.
193 *
194 * @param array $params
195 * Array of parameters determined by getfields.
196 */
197 function _civicrm_api3_order_cancel_spec(&$params) {
198 $params['contribution_id'] = [
199 'api.required' => 1,
200 'title' => 'Contribution ID',
201 'type' => CRM_Utils_Type::T_INT,
202 ];
203 }
204
205 /**
206 * Adjust Metadata for Create action.
207 *
208 * The metadata is used for setting defaults, documentation & validation.
209 *
210 * @param array $params
211 * Array of parameters determined by getfields.
212 */
213 function _civicrm_api3_order_create_spec(&$params) {
214 $params['contact_id'] = [
215 'name' => 'contact_id',
216 'title' => 'Contact ID',
217 'type' => CRM_Utils_Type::T_INT,
218 'api.required' => TRUE,
219 ];
220 $params['total_amount'] = [
221 'name' => 'total_amount',
222 'title' => 'Total Amount',
223 ];
224 $params['financial_type_id'] = [
225 'name' => 'financial_type_id',
226 'title' => 'Financial Type',
227 'type' => CRM_Utils_Type::T_INT,
228 'api.required' => TRUE,
229 'table_name' => 'civicrm_contribution',
230 'entity' => 'Contribution',
231 'bao' => 'CRM_Contribute_BAO_Contribution',
232 'pseudoconstant' => [
233 'table' => 'civicrm_financial_type',
234 'keyColumn' => 'id',
235 'labelColumn' => 'name',
236 ],
237 ];
238 }
239
240 /**
241 * Adjust Metadata for Delete action.
242 *
243 * The metadata is used for setting defaults, documentation & validation.
244 *
245 * @param array $params
246 * Array of parameters determined by getfields.
247 */
248 function _civicrm_api3_order_delete_spec(&$params) {
249 $params['contribution_id'] = [
250 'api.required' => TRUE,
251 'title' => 'Contribution ID',
252 'type' => CRM_Utils_Type::T_INT,
253 ];
254 $params['id']['api.aliases'] = ['contribution_id'];
255 }