Commit | Line | Data |
---|---|---|
73c0e107 PN |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
73c0e107 | 5 | | | |
7d61e75f 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 | | |
73c0e107 PN |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
73c0e107 PN |
12 | /** |
13 | * Test APIv3 civicrm_contribute_* functions | |
14 | * | |
15 | * @package CiviCRM_APIv3 | |
16 | * @subpackage API_Contribution | |
acb109b7 | 17 | * @group headless |
73c0e107 PN |
18 | */ |
19 | class api_v3_OrderTest extends CiviUnitTestCase { | |
20 | ||
73c0e107 PN |
21 | protected $_individualId; |
22 | protected $_financialTypeId = 1; | |
73c0e107 PN |
23 | public $debug = 0; |
24 | ||
25 | /** | |
26 | * Setup function. | |
9c5edcd4 | 27 | * |
28 | * @throws \CRM_Core_Exception | |
73c0e107 PN |
29 | */ |
30 | public function setUp() { | |
31 | parent::setUp(); | |
32 | ||
33 | $this->_apiversion = 3; | |
34 | $this->_individualId = $this->individualCreate(); | |
35 | } | |
36 | ||
37 | /** | |
38 | * Clean up after each test. | |
9c5edcd4 | 39 | * |
40 | * @throws \CRM_Core_Exception | |
73c0e107 PN |
41 | */ |
42 | public function tearDown() { | |
43 | $this->quickCleanUpFinancialEntities(); | |
9099cab3 | 44 | $this->quickCleanup(['civicrm_uf_match']); |
73c0e107 PN |
45 | } |
46 | ||
47 | /** | |
c39e27f2 | 48 | * Test Get order api. |
73c0e107 PN |
49 | */ |
50 | public function testGetOrder() { | |
51 | $contribution = $this->addOrder(FALSE, 100); | |
52 | ||
9099cab3 | 53 | $params = [ |
73c0e107 | 54 | 'contribution_id' => $contribution['id'], |
9099cab3 | 55 | ]; |
73c0e107 | 56 | |
54a54e15 | 57 | $order = $this->callAPIAndDocument('Order', 'get', $params, __FUNCTION__, __FILE__); |
73c0e107 PN |
58 | |
59 | $this->assertEquals(1, $order['count']); | |
9099cab3 CW |
60 | $expectedResult = [ |
61 | $contribution['id'] => [ | |
c5e2b8c1 PN |
62 | 'total_amount' => 100, |
63 | 'contribution_id' => $contribution['id'], | |
64 | 'contribution_status' => 'Completed', | |
65 | 'net_amount' => 100, | |
9099cab3 CW |
66 | ], |
67 | ]; | |
68 | $lineItems[] = [ | |
73c0e107 PN |
69 | 'entity_table' => 'civicrm_contribution', |
70 | 'entity_id' => $contribution['id'], | |
71 | 'contribution_id' => $contribution['id'], | |
72 | 'unit_price' => 100, | |
73 | 'line_total' => 100, | |
74 | 'financial_type_id' => 1, | |
9099cab3 | 75 | ]; |
73c0e107 | 76 | $this->checkPaymentResult($order, $expectedResult, $lineItems); |
9099cab3 | 77 | $this->callAPISuccess('Contribution', 'Delete', [ |
73c0e107 | 78 | 'id' => $contribution['id'], |
9099cab3 | 79 | ]); |
73c0e107 PN |
80 | } |
81 | ||
c39e27f2 PN |
82 | /** |
83 | * Test Get Order api for participant contribution. | |
f3e6da5e | 84 | * |
85 | * @throws \CRM_Core_Exception | |
c39e27f2 PN |
86 | */ |
87 | public function testGetOrderParticipant() { | |
e4649817 | 88 | $this->addOrder(FALSE, 100); |
5266bd48 | 89 | $contribution = $this->createPartiallyPaidParticipantOrder(); |
c39e27f2 | 90 | |
9099cab3 | 91 | $params = [ |
c39e27f2 | 92 | 'contribution_id' => $contribution['id'], |
9099cab3 | 93 | ]; |
c39e27f2 PN |
94 | |
95 | $order = $this->callAPISuccess('Order', 'get', $params); | |
96 | ||
f3e6da5e | 97 | $this->assertCount(2, $order['values'][$contribution['id']]['line_items']); |
9099cab3 | 98 | $this->callAPISuccess('Contribution', 'Delete', [ |
c39e27f2 | 99 | 'id' => $contribution['id'], |
9099cab3 | 100 | ]); |
c39e27f2 PN |
101 | } |
102 | ||
73c0e107 | 103 | /** |
e4649817 | 104 | * Function to assert db values. |
73c0e107 PN |
105 | */ |
106 | public function checkPaymentResult($results, $expectedResult, $lineItems = NULL) { | |
107 | foreach ($expectedResult[$results['id']] as $key => $value) { | |
108 | $this->assertEquals($results['values'][$results['id']][$key], $value); | |
109 | } | |
110 | ||
111 | if ($lineItems) { | |
112 | foreach ($lineItems as $key => $items) { | |
113 | foreach ($items as $k => $item) { | |
114 | $this->assertEquals($results['values'][$results['id']]['line_items'][$key][$k], $item); | |
115 | } | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | /** | |
e4649817 | 121 | * Add order. |
73c0e107 PN |
122 | * |
123 | * @param bool $isPriceSet | |
124 | * @param float $amount | |
125 | * @param array $extraParams | |
126 | * | |
127 | * @return array | |
128 | */ | |
9099cab3 CW |
129 | public function addOrder($isPriceSet, $amount = 300.00, $extraParams = []) { |
130 | $p = [ | |
73c0e107 PN |
131 | 'contact_id' => $this->_individualId, |
132 | 'receive_date' => '2010-01-20', | |
133 | 'total_amount' => $amount, | |
134 | 'financial_type_id' => $this->_financialTypeId, | |
135 | 'contribution_status_id' => 1, | |
9099cab3 | 136 | ]; |
73c0e107 PN |
137 | |
138 | if ($isPriceSet) { | |
139 | $priceFields = $this->createPriceSet(); | |
140 | foreach ($priceFields['values'] as $key => $priceField) { | |
9099cab3 | 141 | $lineItems[1][$key] = [ |
73c0e107 PN |
142 | 'price_field_id' => $priceField['price_field_id'], |
143 | 'price_field_value_id' => $priceField['id'], | |
144 | 'label' => $priceField['label'], | |
145 | 'field_title' => $priceField['label'], | |
146 | 'qty' => 1, | |
147 | 'unit_price' => $priceField['amount'], | |
148 | 'line_total' => $priceField['amount'], | |
149 | 'financial_type_id' => $priceField['financial_type_id'], | |
9099cab3 | 150 | ]; |
73c0e107 PN |
151 | } |
152 | $p['line_item'] = $lineItems; | |
153 | } | |
154 | $p = array_merge($extraParams, $p); | |
155 | return $this->callAPISuccess('Contribution', 'create', $p); | |
156 | } | |
157 | ||
b8644ae3 PN |
158 | /** |
159 | * Test create order api | |
160 | */ | |
161 | public function testAddOrder() { | |
162 | $order = $this->addOrder(FALSE, 100); | |
9099cab3 | 163 | $params = [ |
b8644ae3 | 164 | 'contribution_id' => $order['id'], |
9099cab3 | 165 | ]; |
b8644ae3 | 166 | $order = $this->callAPISuccess('order', 'get', $params); |
9099cab3 CW |
167 | $expectedResult = [ |
168 | $order['id'] => [ | |
b8644ae3 PN |
169 | 'total_amount' => 100, |
170 | 'contribution_id' => $order['id'], | |
171 | 'contribution_status' => 'Completed', | |
172 | 'net_amount' => 100, | |
9099cab3 CW |
173 | ], |
174 | ]; | |
175 | $lineItems[] = [ | |
b8644ae3 PN |
176 | 'entity_table' => 'civicrm_contribution', |
177 | 'entity_id' => $order['id'], | |
178 | 'contribution_id' => $order['id'], | |
179 | 'unit_price' => 100, | |
180 | 'line_total' => 100, | |
181 | 'financial_type_id' => 1, | |
9099cab3 | 182 | ]; |
b8644ae3 | 183 | $this->checkPaymentResult($order, $expectedResult, $lineItems); |
9099cab3 | 184 | $this->callAPISuccess('Contribution', 'Delete', [ |
b8644ae3 | 185 | 'id' => $order['id'], |
9099cab3 | 186 | ]); |
b8644ae3 PN |
187 | } |
188 | ||
189 | /** | |
190 | * Test create order api for membership | |
9c5edcd4 | 191 | * |
192 | * @throws \CRM_Core_Exception | |
b8644ae3 PN |
193 | */ |
194 | public function testAddOrderForMembership() { | |
e4649817 | 195 | $membershipType = $this->membershipTypeCreate(); |
196 | $membershipType1 = $this->membershipTypeCreate(); | |
9099cab3 CW |
197 | $membershipType = $membershipTypes = [$membershipType, $membershipType1]; |
198 | $p = [ | |
b8644ae3 PN |
199 | 'contact_id' => $this->_individualId, |
200 | 'receive_date' => '2010-01-20', | |
785f03e2 | 201 | 'financial_type_id' => 'Event Fee', |
af595ac2 | 202 | 'contribution_status_id' => 'Pending', |
9099cab3 | 203 | ]; |
b8644ae3 PN |
204 | $priceFields = $this->createPriceSet(); |
205 | foreach ($priceFields['values'] as $key => $priceField) { | |
9099cab3 | 206 | $lineItems[$key] = [ |
b8644ae3 PN |
207 | 'price_field_id' => $priceField['price_field_id'], |
208 | 'price_field_value_id' => $priceField['id'], | |
209 | 'label' => $priceField['label'], | |
210 | 'field_title' => $priceField['label'], | |
211 | 'qty' => 1, | |
212 | 'unit_price' => $priceField['amount'], | |
213 | 'line_total' => $priceField['amount'], | |
214 | 'financial_type_id' => $priceField['financial_type_id'], | |
215 | 'entity_table' => 'civicrm_membership', | |
216 | 'membership_type_id' => array_pop($membershipType), | |
9099cab3 | 217 | ]; |
b8644ae3 | 218 | } |
9099cab3 CW |
219 | $p['line_items'][] = [ |
220 | 'line_item' => [array_pop($lineItems)], | |
221 | 'params' => [ | |
b8644ae3 PN |
222 | 'contact_id' => $this->_individualId, |
223 | 'membership_type_id' => array_pop($membershipTypes), | |
224 | 'join_date' => '2006-01-21', | |
225 | 'start_date' => '2006-01-21', | |
226 | 'end_date' => '2006-12-21', | |
227 | 'source' => 'Payment', | |
228 | 'is_override' => 1, | |
9099cab3 CW |
229 | ], |
230 | ]; | |
7aeb7f06 | 231 | $order = $this->callAPIAndDocument('Order', 'create', $p, __FUNCTION__, __FILE__); |
9099cab3 | 232 | $params = [ |
b8644ae3 | 233 | 'contribution_id' => $order['id'], |
9099cab3 | 234 | ]; |
b8644ae3 | 235 | $order = $this->callAPISuccess('order', 'get', $params); |
9099cab3 CW |
236 | $expectedResult = [ |
237 | $order['id'] => [ | |
b8644ae3 PN |
238 | 'total_amount' => 200, |
239 | 'contribution_id' => $order['id'], | |
af595ac2 | 240 | 'contribution_status' => 'Pending Label**', |
b8644ae3 | 241 | 'net_amount' => 200, |
9099cab3 CW |
242 | ], |
243 | ]; | |
b8644ae3 | 244 | $this->checkPaymentResult($order, $expectedResult); |
7aeb7f06 | 245 | $membershipPayment = $this->callAPISuccessGetSingle('MembershipPayment', $params); |
246 | ||
247 | $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipPayment['id']]); | |
9099cab3 | 248 | $this->callAPISuccess('Contribution', 'Delete', [ |
b8644ae3 | 249 | 'id' => $order['id'], |
9099cab3 CW |
250 | ]); |
251 | $p['line_items'][] = [ | |
252 | 'line_item' => [array_pop($lineItems)], | |
253 | 'params' => [ | |
b8644ae3 PN |
254 | 'contact_id' => $this->_individualId, |
255 | 'membership_type_id' => array_pop($membershipTypes), | |
256 | 'join_date' => '2006-01-21', | |
257 | 'start_date' => '2006-01-21', | |
258 | 'end_date' => '2006-12-21', | |
259 | 'source' => 'Payment', | |
260 | 'is_override' => 1, | |
af595ac2 | 261 | 'status_id' => 'Pending', |
9099cab3 CW |
262 | ], |
263 | ]; | |
b8644ae3 PN |
264 | $p['total_amount'] = 300; |
265 | $order = $this->callAPISuccess('order', 'create', $p); | |
9099cab3 CW |
266 | $expectedResult = [ |
267 | $order['id'] => [ | |
b8644ae3 | 268 | 'total_amount' => 300, |
af595ac2 | 269 | 'contribution_status' => 'Pending Label**', |
b8644ae3 | 270 | 'net_amount' => 300, |
9099cab3 CW |
271 | ], |
272 | ]; | |
273 | $paymentMembership = [ | |
b8644ae3 | 274 | 'contribution_id' => $order['id'], |
9099cab3 | 275 | ]; |
b8644ae3 PN |
276 | $order = $this->callAPISuccess('order', 'get', $paymentMembership); |
277 | $this->checkPaymentResult($order, $expectedResult); | |
278 | $this->callAPISuccessGetCount('MembershipPayment', $paymentMembership, 2); | |
9099cab3 | 279 | $this->callAPISuccess('Contribution', 'Delete', [ |
b8644ae3 | 280 | 'id' => $order['id'], |
9099cab3 | 281 | ]); |
b8644ae3 PN |
282 | } |
283 | ||
284 | /** | |
285 | * Test create order api for participant | |
9c5edcd4 | 286 | * |
287 | * @throws \CRM_Core_Exception | |
b8644ae3 | 288 | */ |
e4649817 | 289 | public function testAddOrderForParticipant() { |
290 | $event = $this->eventCreate(); | |
291 | $this->_eventId = $event['id']; | |
9099cab3 | 292 | $p = [ |
b8644ae3 PN |
293 | 'contact_id' => $this->_individualId, |
294 | 'receive_date' => '2010-01-20', | |
b8644ae3 | 295 | 'financial_type_id' => $this->_financialTypeId, |
af595ac2 | 296 | 'contribution_status_id' => 'Pending', |
9099cab3 | 297 | ]; |
b8644ae3 PN |
298 | $priceFields = $this->createPriceSet(); |
299 | foreach ($priceFields['values'] as $key => $priceField) { | |
9099cab3 | 300 | $lineItems[$key] = [ |
b8644ae3 PN |
301 | 'price_field_id' => $priceField['price_field_id'], |
302 | 'price_field_value_id' => $priceField['id'], | |
303 | 'label' => $priceField['label'], | |
304 | 'field_title' => $priceField['label'], | |
305 | 'qty' => 1, | |
306 | 'unit_price' => $priceField['amount'], | |
307 | 'line_total' => $priceField['amount'], | |
308 | 'financial_type_id' => $priceField['financial_type_id'], | |
309 | 'entity_table' => 'civicrm_participant', | |
9099cab3 | 310 | ]; |
b8644ae3 | 311 | } |
9099cab3 | 312 | $p['line_items'][] = [ |
b8644ae3 | 313 | 'line_item' => $lineItems, |
9099cab3 | 314 | 'params' => [ |
b8644ae3 PN |
315 | 'contact_id' => $this->_individualId, |
316 | 'event_id' => $this->_eventId, | |
b8644ae3 PN |
317 | 'role_id' => 1, |
318 | 'register_date' => '2007-07-21 00:00:00', | |
319 | 'source' => 'Online Event Registration: API Testing', | |
9099cab3 CW |
320 | ], |
321 | ]; | |
af595ac2 | 322 | |
54a54e15 | 323 | $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__, __FILE__, 'Create order for participant', 'CreateOrderParticipant'); |
af595ac2 | 324 | $params = ['contribution_id' => $order['id']]; |
b8644ae3 | 325 | $order = $this->callAPISuccess('order', 'get', $params); |
9099cab3 CW |
326 | $expectedResult = [ |
327 | $order['id'] => [ | |
b8644ae3 PN |
328 | 'total_amount' => 300, |
329 | 'contribution_id' => $order['id'], | |
af595ac2 | 330 | 'contribution_status' => 'Pending Label**', |
b8644ae3 | 331 | 'net_amount' => 300, |
9099cab3 CW |
332 | ], |
333 | ]; | |
b8644ae3 | 334 | $this->checkPaymentResult($order, $expectedResult); |
af595ac2 | 335 | $paymentParticipant = $this->callAPISuccessGetSingle('ParticipantPayment', ['contribution_id' => $order['id']]); |
336 | $participant = $this->callAPISuccessGetSingle('Participant', ['participant_id' => $paymentParticipant['participant_id']]); | |
337 | $this->assertEquals('Pending (incomplete transaction)', $participant['participant_status']); | |
9099cab3 | 338 | $this->callAPISuccess('Contribution', 'Delete', [ |
b8644ae3 | 339 | 'id' => $order['id'], |
9099cab3 | 340 | ]); |
af595ac2 | 341 | |
9099cab3 | 342 | $p['line_items'][] = [ |
b8644ae3 | 343 | 'line_item' => $lineItems, |
9099cab3 | 344 | 'params' => [ |
b8644ae3 PN |
345 | 'contact_id' => $this->individualCreate(), |
346 | 'event_id' => $this->_eventId, | |
b8644ae3 PN |
347 | 'role_id' => 1, |
348 | 'register_date' => '2007-07-21 00:00:00', | |
349 | 'source' => 'Online Event Registration: API Testing', | |
9099cab3 CW |
350 | ], |
351 | ]; | |
af595ac2 | 352 | |
b8644ae3 | 353 | $order = $this->callAPISuccess('order', 'create', $p); |
9099cab3 CW |
354 | $expectedResult = [ |
355 | $order['id'] => [ | |
b8644ae3 | 356 | 'total_amount' => 600, |
af595ac2 | 357 | 'contribution_status' => 'Pending Label**', |
b8644ae3 | 358 | 'net_amount' => 600, |
9099cab3 CW |
359 | ], |
360 | ]; | |
361 | $paymentParticipant = [ | |
b8644ae3 | 362 | 'contribution_id' => $order['id'], |
9099cab3 | 363 | ]; |
b8644ae3 PN |
364 | $order = $this->callAPISuccess('order', 'get', $paymentParticipant); |
365 | $this->checkPaymentResult($order, $expectedResult); | |
366 | $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2); | |
9099cab3 | 367 | $this->callAPISuccess('Contribution', 'Delete', [ |
b8644ae3 | 368 | 'id' => $order['id'], |
9099cab3 | 369 | ]); |
b8644ae3 PN |
370 | } |
371 | ||
372 | /** | |
373 | * Test create order api with line items | |
374 | */ | |
375 | public function testAddOrderWithLineItems() { | |
376 | $order = $this->addOrder(TRUE); | |
9099cab3 | 377 | $params = [ |
b8644ae3 | 378 | 'contribution_id' => $order['id'], |
9099cab3 | 379 | ]; |
b8644ae3 | 380 | $order = $this->callAPISuccess('order', 'get', $params); |
9099cab3 CW |
381 | $expectedResult = [ |
382 | $order['id'] => [ | |
b8644ae3 PN |
383 | 'total_amount' => 300, |
384 | 'contribution_id' => $order['id'], | |
385 | 'contribution_status' => 'Completed', | |
386 | 'net_amount' => 300, | |
9099cab3 CW |
387 | ], |
388 | ]; | |
389 | $items[] = [ | |
b8644ae3 PN |
390 | 'entity_table' => 'civicrm_contribution', |
391 | 'entity_id' => $order['id'], | |
392 | 'contribution_id' => $order['id'], | |
393 | 'unit_price' => 100, | |
394 | 'line_total' => 100, | |
9099cab3 CW |
395 | ]; |
396 | $items[] = [ | |
b8644ae3 PN |
397 | 'entity_table' => 'civicrm_contribution', |
398 | 'entity_id' => $order['id'], | |
399 | 'contribution_id' => $order['id'], | |
400 | 'unit_price' => 200, | |
401 | 'line_total' => 200, | |
9099cab3 | 402 | ]; |
b8644ae3 | 403 | $this->checkPaymentResult($order, $expectedResult, $items); |
9099cab3 | 404 | $params = [ |
b8644ae3 PN |
405 | 'entity_table' => 'civicrm_contribution', |
406 | 'entity_id' => $order['id'], | |
9099cab3 | 407 | ]; |
b8644ae3 PN |
408 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
409 | $this->assertEquals($eft['values'][$eft['id']]['amount'], 300); | |
9099cab3 | 410 | $params = [ |
b8644ae3 PN |
411 | 'entity_table' => 'civicrm_financial_item', |
412 | 'financial_trxn_id' => $eft['values'][$eft['id']]['financial_trxn_id'], | |
9099cab3 | 413 | ]; |
b8644ae3 | 414 | $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); |
9099cab3 | 415 | $amounts = [200, 100]; |
b8644ae3 PN |
416 | foreach ($eft['values'] as $value) { |
417 | $this->assertEquals($value['amount'], array_pop($amounts)); | |
418 | } | |
9099cab3 | 419 | $this->callAPISuccess('Contribution', 'Delete', [ |
b8644ae3 | 420 | 'id' => $order['id'], |
9099cab3 | 421 | ]); |
b8644ae3 PN |
422 | } |
423 | ||
5f44f698 PN |
424 | /** |
425 | * Test delete order api | |
426 | */ | |
427 | public function testDeleteOrder() { | |
428 | $order = $this->addOrder(FALSE, 100); | |
9099cab3 | 429 | $params = [ |
5f44f698 | 430 | 'contribution_id' => $order['id'], |
9099cab3 | 431 | ]; |
5f44f698 PN |
432 | try { |
433 | $this->callAPISuccess('order', 'delete', $params); | |
434 | $this->fail("Missed expected exception"); | |
435 | } | |
436 | catch (Exception $expected) { | |
9099cab3 | 437 | $this->callAPISuccess('Contribution', 'create', [ |
5f44f698 PN |
438 | 'contribution_id' => $order['id'], |
439 | 'is_test' => TRUE, | |
9099cab3 | 440 | ]); |
54a54e15 | 441 | $this->callAPIAndDocument('order', 'delete', $params, __FUNCTION__, __FILE__); |
5f44f698 PN |
442 | $order = $this->callAPISuccess('order', 'get', $params); |
443 | $this->assertEquals(0, $order['count']); | |
444 | } | |
445 | } | |
446 | ||
65f7f9f6 PN |
447 | /** |
448 | * Test cancel order api | |
449 | */ | |
450 | public function testCancelOrder() { | |
451 | $contribution = $this->addOrder(FALSE, 100); | |
9099cab3 | 452 | $params = [ |
65f7f9f6 | 453 | 'contribution_id' => $contribution['id'], |
9099cab3 | 454 | ]; |
54a54e15 | 455 | $this->callAPIAndDocument('order', 'cancel', $params, __FUNCTION__, __FILE__); |
65f7f9f6 | 456 | $order = $this->callAPISuccess('Order', 'get', $params); |
9099cab3 CW |
457 | $expectedResult = [ |
458 | $contribution['id'] => [ | |
65f7f9f6 PN |
459 | 'total_amount' => 100, |
460 | 'contribution_id' => $contribution['id'], | |
461 | 'contribution_status' => 'Cancelled', | |
462 | 'net_amount' => 100, | |
9099cab3 CW |
463 | ], |
464 | ]; | |
65f7f9f6 | 465 | $this->checkPaymentResult($order, $expectedResult); |
9099cab3 | 466 | $this->callAPISuccess('Contribution', 'Delete', [ |
65f7f9f6 | 467 | 'id' => $contribution['id'], |
9099cab3 | 468 | ]); |
65f7f9f6 PN |
469 | } |
470 | ||
b63480e3 PN |
471 | /** |
472 | * Test cancel order api | |
473 | */ | |
474 | public function testCancelWithParticipant() { | |
e4649817 | 475 | $event = $this->eventCreate(); |
476 | $this->_eventId = $event['id']; | |
9099cab3 | 477 | $eventParams = [ |
b63480e3 PN |
478 | 'id' => $this->_eventId, |
479 | 'financial_type_id' => 4, | |
480 | 'is_monetary' => 1, | |
9099cab3 | 481 | ]; |
b63480e3 | 482 | $this->callAPISuccess('event', 'create', $eventParams); |
9099cab3 | 483 | $participantParams = [ |
b63480e3 PN |
484 | 'financial_type_id' => 4, |
485 | 'event_id' => $this->_eventId, | |
486 | 'role_id' => 1, | |
487 | 'status_id' => 1, | |
488 | 'fee_currency' => 'USD', | |
489 | 'contact_id' => $this->_individualId, | |
9099cab3 | 490 | ]; |
b63480e3 | 491 | $participant = $this->callAPISuccess('Participant', 'create', $participantParams); |
9099cab3 | 492 | $extraParams = [ |
b63480e3 PN |
493 | 'contribution_mode' => 'participant', |
494 | 'participant_id' => $participant['id'], | |
9099cab3 | 495 | ]; |
b63480e3 | 496 | $contribution = $this->addOrder(TRUE, 100, $extraParams); |
9099cab3 | 497 | $paymentParticipant = [ |
b63480e3 PN |
498 | 'participant_id' => $participant['id'], |
499 | 'contribution_id' => $contribution['id'], | |
9099cab3 | 500 | ]; |
b63480e3 | 501 | $this->callAPISuccess('ParticipantPayment', 'create', $paymentParticipant); |
9099cab3 | 502 | $params = [ |
b63480e3 | 503 | 'contribution_id' => $contribution['id'], |
9099cab3 | 504 | ]; |
b63480e3 PN |
505 | $this->callAPISuccess('order', 'cancel', $params); |
506 | $order = $this->callAPISuccess('Order', 'get', $params); | |
9099cab3 CW |
507 | $expectedResult = [ |
508 | $contribution['id'] => [ | |
b63480e3 PN |
509 | 'total_amount' => 100, |
510 | 'contribution_id' => $contribution['id'], | |
511 | 'contribution_status' => 'Cancelled', | |
512 | 'net_amount' => 100, | |
9099cab3 CW |
513 | ], |
514 | ]; | |
dfd6297d | 515 | $this->checkPaymentResult($order, $expectedResult); |
b63480e3 | 516 | $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $params); |
9099cab3 | 517 | $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]); |
b63480e3 | 518 | $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Cancelled'); |
9099cab3 | 519 | $this->callAPISuccess('Contribution', 'Delete', [ |
b63480e3 | 520 | 'id' => $contribution['id'], |
9099cab3 | 521 | ]); |
b63480e3 PN |
522 | } |
523 | ||
b8e45de5 | 524 | /** |
9c5edcd4 | 525 | * Test an exception is thrown if line items do not add up to total_amount, no tax. |
b8e45de5 O |
526 | */ |
527 | public function testCreateOrderIfTotalAmountDoesNotMatchLineItemsAmountsIfNoTaxSupplied() { | |
528 | $params = [ | |
529 | 'contact_id' => $this->_individualId, | |
530 | 'receive_date' => '2018-01-01', | |
531 | 'total_amount' => 50, | |
532 | 'financial_type_id' => $this->_financialTypeId, | |
af595ac2 | 533 | 'contribution_status_id' => 'Pending', |
b8e45de5 O |
534 | 'line_items' => [ |
535 | 0 => [ | |
536 | 'line_item' => [ | |
537 | '0' => [ | |
538 | 'price_field_id' => 1, | |
539 | 'price_field_value_id' => 1, | |
540 | 'label' => 'Test 1', | |
541 | 'field_title' => 'Test 1', | |
542 | 'qty' => 1, | |
543 | 'unit_price' => 40, | |
544 | 'line_total' => 40, | |
545 | 'financial_type_id' => 1, | |
546 | 'entity_table' => 'civicrm_contribution', | |
547 | ], | |
39b959db | 548 | ], |
b8e45de5 O |
549 | ], |
550 | ], | |
551 | ]; | |
552 | ||
9c5edcd4 | 553 | $this->callAPIFailure('Order', 'create', $params, 'Line item total doesn\'t match with total amount'); |
b8e45de5 O |
554 | } |
555 | ||
556 | /** | |
9c5edcd4 | 557 | * Test an exception is thrown if line items do not add up to total_amount, with tax. |
b8e45de5 O |
558 | */ |
559 | public function testCreateOrderIfTotalAmountDoesNotMatchLineItemsAmountsIfTaxSupplied() { | |
560 | $params = [ | |
561 | 'contact_id' => $this->_individualId, | |
562 | 'receive_date' => '2018-01-01', | |
563 | 'total_amount' => 50, | |
564 | 'financial_type_id' => $this->_financialTypeId, | |
af595ac2 | 565 | 'contribution_status_id' => 'Pending', |
b8e45de5 O |
566 | 'tax_amount' => 15, |
567 | 'line_items' => [ | |
568 | 0 => [ | |
569 | 'line_item' => [ | |
570 | '0' => [ | |
571 | 'price_field_id' => 1, | |
572 | 'price_field_value_id' => 1, | |
573 | 'label' => 'Test 1', | |
574 | 'field_title' => 'Test 1', | |
575 | 'qty' => 1, | |
576 | 'unit_price' => 30, | |
577 | 'line_total' => 30, | |
578 | 'financial_type_id' => 1, | |
579 | 'entity_table' => 'civicrm_contribution', | |
580 | 'tax_amount' => 15, | |
581 | ], | |
39b959db | 582 | ], |
b8e45de5 O |
583 | ], |
584 | ], | |
585 | ]; | |
586 | ||
9c5edcd4 | 587 | $this->callAPIFailure('Order', 'create', $params, 'Line item total doesn\'t match with total amount.'); |
b8e45de5 O |
588 | } |
589 | ||
590 | public function testCreateOrderIfTotalAmountDoesMatchLineItemsAmountsAndTaxSupplied() { | |
591 | $params = [ | |
592 | 'contact_id' => $this->_individualId, | |
593 | 'receive_date' => '2018-01-01', | |
594 | 'total_amount' => 50, | |
595 | 'financial_type_id' => $this->_financialTypeId, | |
af595ac2 | 596 | 'contribution_status_id' => 'Pending', |
b8e45de5 O |
597 | 'tax_amount' => 15, |
598 | 'line_items' => [ | |
599 | 0 => [ | |
600 | 'line_item' => [ | |
601 | '0' => [ | |
602 | 'price_field_id' => 1, | |
603 | 'price_field_value_id' => 1, | |
604 | 'label' => 'Test 1', | |
605 | 'field_title' => 'Test 1', | |
606 | 'qty' => 1, | |
607 | 'unit_price' => 35, | |
608 | 'line_total' => 35, | |
609 | 'financial_type_id' => 1, | |
610 | 'entity_table' => 'civicrm_contribution', | |
611 | 'tax_amount' => 15, | |
612 | ], | |
39b959db | 613 | ], |
b8e45de5 O |
614 | ], |
615 | ], | |
616 | ]; | |
617 | ||
9c5edcd4 | 618 | $order = $this->callAPISuccess('Order', 'create', $params); |
b8e45de5 O |
619 | $this->assertEquals(1, $order['count']); |
620 | } | |
621 | ||
e2887a3c | 622 | /** |
623 | * Test that a contribution can be added in pending mode with a chained payment. | |
624 | * | |
625 | * We have just deprecated creating an order with a status other than pending. It makes | |
626 | * sense to support adding a payment straight away by chaining. | |
627 | * | |
628 | * @throws \CRM_Core_Exception | |
629 | */ | |
630 | public function testCreateWithChainedPayment() { | |
631 | $contributionID = $this->callAPISuccess('Order', 'create', ['contact_id' => $this->_individualId, 'total_amount' => 5, 'financial_type_id' => 2, 'contribution_status_id' => 'Pending', 'api.Payment.create' => ['total_amount' => 5]])['id']; | |
632 | $this->assertEquals('Completed', $this->callAPISuccessGetValue('Contribution', ['id' => $contributionID, 'return' => 'contribution_status'])); | |
633 | } | |
634 | ||
73c0e107 | 635 | } |