3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_contribute_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
35 class api_v3_OrderTest
extends CiviUnitTestCase
{
38 * Assume empty database with just civicrm_data.
40 protected $_individualId;
41 protected $_financialTypeId = 1;
42 protected $_apiversion;
48 public function setUp() {
51 $this->_apiversion
= 3;
52 $this->_individualId
= $this->individualCreate();
56 * Clean up after each test.
58 public function tearDown() {
59 $this->quickCleanUpFinancialEntities();
60 $this->quickCleanup(array('civicrm_uf_match'));
66 public function testGetOrder() {
67 $contribution = $this->addOrder(FALSE, 100);
70 'contribution_id' => $contribution['id'],
73 $order = $this->callAPIAndDocument('Order', 'get', $params, __FUNCTION__
, __FILE__
);
75 $this->assertEquals(1, $order['count']);
76 $expectedResult = array(
77 $contribution['id'] => array(
78 'total_amount' => 100,
79 'contribution_id' => $contribution['id'],
80 'contribution_status' => 'Completed',
85 'entity_table' => 'civicrm_contribution',
86 'entity_id' => $contribution['id'],
87 'contribution_id' => $contribution['id'],
90 'financial_type_id' => 1,
92 $this->checkPaymentResult($order, $expectedResult, $lineItems);
93 $this->callAPISuccess('Contribution', 'Delete', array(
94 'id' => $contribution['id'],
99 * Test Get Order api for participant contribution.
101 public function testGetOrderParticipant() {
102 $this->addOrder(FALSE, 100);
103 list($items, $contribution) = $this->createParticipantWithContribution();
106 'contribution_id' => $contribution['id'],
109 $order = $this->callAPISuccess('Order', 'get', $params);
111 $this->assertEquals(2, count($order['values'][$contribution['id']]['line_items']));
112 $this->callAPISuccess('Contribution', 'Delete', array(
113 'id' => $contribution['id'],
118 * Function to assert db values.
120 public function checkPaymentResult($results, $expectedResult, $lineItems = NULL) {
121 foreach ($expectedResult[$results['id']] as $key => $value) {
122 $this->assertEquals($results['values'][$results['id']][$key], $value);
126 foreach ($lineItems as $key => $items) {
127 foreach ($items as $k => $item) {
128 $this->assertEquals($results['values'][$results['id']]['line_items'][$key][$k], $item);
137 * @param bool $isPriceSet
138 * @param float $amount
139 * @param array $extraParams
143 public function addOrder($isPriceSet, $amount = 300.00, $extraParams = array()) {
145 'contact_id' => $this->_individualId
,
146 'receive_date' => '2010-01-20',
147 'total_amount' => $amount,
148 'financial_type_id' => $this->_financialTypeId
,
149 'contribution_status_id' => 1,
153 $priceFields = $this->createPriceSet();
154 foreach ($priceFields['values'] as $key => $priceField) {
155 $lineItems[1][$key] = array(
156 'price_field_id' => $priceField['price_field_id'],
157 'price_field_value_id' => $priceField['id'],
158 'label' => $priceField['label'],
159 'field_title' => $priceField['label'],
161 'unit_price' => $priceField['amount'],
162 'line_total' => $priceField['amount'],
163 'financial_type_id' => $priceField['financial_type_id'],
166 $p['line_item'] = $lineItems;
168 $p = array_merge($extraParams, $p);
169 return $this->callAPISuccess('Contribution', 'create', $p);
173 * Test create order api
175 public function testAddOrder() {
176 $order = $this->addOrder(FALSE, 100);
178 'contribution_id' => $order['id'],
180 $order = $this->callAPISuccess('order', 'get', $params);
181 $expectedResult = array(
182 $order['id'] => array(
183 'total_amount' => 100,
184 'contribution_id' => $order['id'],
185 'contribution_status' => 'Completed',
189 $lineItems[] = array(
190 'entity_table' => 'civicrm_contribution',
191 'entity_id' => $order['id'],
192 'contribution_id' => $order['id'],
195 'financial_type_id' => 1,
197 $this->checkPaymentResult($order, $expectedResult, $lineItems);
198 $this->callAPISuccess('Contribution', 'Delete', array(
199 'id' => $order['id'],
204 * Test create order api for membership
206 public function testAddOrderForMembership() {
207 $membershipType = $this->membershipTypeCreate();
208 $membershipType1 = $this->membershipTypeCreate();
209 $membershipType = $membershipTypes = array($membershipType, $membershipType1);
211 'contact_id' => $this->_individualId
,
212 'receive_date' => '2010-01-20',
213 'total_amount' => 200,
214 'financial_type_id' => $this->_financialTypeId
,
215 'contribution_status_id' => 1,
217 $priceFields = $this->createPriceSet();
218 foreach ($priceFields['values'] as $key => $priceField) {
219 $lineItems[$key] = array(
220 'price_field_id' => $priceField['price_field_id'],
221 'price_field_value_id' => $priceField['id'],
222 'label' => $priceField['label'],
223 'field_title' => $priceField['label'],
225 'unit_price' => $priceField['amount'],
226 'line_total' => $priceField['amount'],
227 'financial_type_id' => $priceField['financial_type_id'],
228 'entity_table' => 'civicrm_membership',
229 'membership_type_id' => array_pop($membershipType),
232 $p['line_items'][] = array(
233 'line_item' => array(array_pop($lineItems)),
235 'contact_id' => $this->_individualId
,
236 'membership_type_id' => array_pop($membershipTypes),
237 'join_date' => '2006-01-21',
238 'start_date' => '2006-01-21',
239 'end_date' => '2006-12-21',
240 'source' => 'Payment',
245 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__
, __FILE__
);
247 'contribution_id' => $order['id'],
249 $order = $this->callAPISuccess('order', 'get', $params);
250 $expectedResult = array(
251 $order['id'] => array(
252 'total_amount' => 200,
253 'contribution_id' => $order['id'],
254 'contribution_status' => 'Completed',
258 $this->checkPaymentResult($order, $expectedResult);
259 $this->callAPISuccessGetCount('MembershipPayment', $params, 1);
260 $this->callAPISuccess('Contribution', 'Delete', array(
261 'id' => $order['id'],
263 $p['line_items'][] = array(
264 'line_item' => array(array_pop($lineItems)),
266 'contact_id' => $this->_individualId
,
267 'membership_type_id' => array_pop($membershipTypes),
268 'join_date' => '2006-01-21',
269 'start_date' => '2006-01-21',
270 'end_date' => '2006-12-21',
271 'source' => 'Payment',
276 $p['total_amount'] = 300;
277 $order = $this->callAPISuccess('order', 'create', $p);
278 $expectedResult = array(
279 $order['id'] => array(
280 'total_amount' => 300,
281 'contribution_status' => 'Completed',
285 $paymentMembership = array(
286 'contribution_id' => $order['id'],
288 $order = $this->callAPISuccess('order', 'get', $paymentMembership);
289 $this->checkPaymentResult($order, $expectedResult);
290 $this->callAPISuccessGetCount('MembershipPayment', $paymentMembership, 2);
291 $this->callAPISuccess('Contribution', 'Delete', array(
292 'id' => $order['id'],
297 * Test create order api for participant
299 public function testAddOrderForParticipant() {
300 $event = $this->eventCreate();
301 $this->_eventId
= $event['id'];
303 'contact_id' => $this->_individualId
,
304 'receive_date' => '2010-01-20',
305 'total_amount' => 300,
306 'financial_type_id' => $this->_financialTypeId
,
307 'contribution_status_id' => 1,
309 $priceFields = $this->createPriceSet();
310 foreach ($priceFields['values'] as $key => $priceField) {
311 $lineItems[$key] = array(
312 'price_field_id' => $priceField['price_field_id'],
313 'price_field_value_id' => $priceField['id'],
314 'label' => $priceField['label'],
315 'field_title' => $priceField['label'],
317 'unit_price' => $priceField['amount'],
318 'line_total' => $priceField['amount'],
319 'financial_type_id' => $priceField['financial_type_id'],
320 'entity_table' => 'civicrm_participant',
323 $p['line_items'][] = array(
324 'line_item' => $lineItems,
326 'contact_id' => $this->_individualId
,
327 'event_id' => $this->_eventId
,
330 'register_date' => '2007-07-21 00:00:00',
331 'source' => 'Online Event Registration: API Testing',
334 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__
, __FILE__
, 'Create order for participant', 'CreateOrderParticipant');
336 'contribution_id' => $order['id'],
338 $order = $this->callAPISuccess('order', 'get', $params);
339 $expectedResult = array(
340 $order['id'] => array(
341 'total_amount' => 300,
342 'contribution_id' => $order['id'],
343 'contribution_status' => 'Completed',
347 $this->checkPaymentResult($order, $expectedResult);
348 $this->callAPISuccessGetCount('ParticipantPayment', $params, 1);
349 $this->callAPISuccess('Contribution', 'Delete', array(
350 'id' => $order['id'],
352 $p['line_items'][] = array(
353 'line_item' => $lineItems,
355 'contact_id' => $this->individualCreate(),
356 'event_id' => $this->_eventId
,
359 'register_date' => '2007-07-21 00:00:00',
360 'source' => 'Online Event Registration: API Testing',
363 $p['total_amount'] = 600;
364 $order = $this->callAPISuccess('order', 'create', $p);
365 $expectedResult = array(
366 $order['id'] => array(
367 'total_amount' => 600,
368 'contribution_status' => 'Completed',
372 $paymentParticipant = array(
373 'contribution_id' => $order['id'],
375 $order = $this->callAPISuccess('order', 'get', $paymentParticipant);
376 $this->checkPaymentResult($order, $expectedResult);
377 $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2);
378 $this->callAPISuccess('Contribution', 'Delete', array(
379 'id' => $order['id'],
384 * Test create order api with line items
386 public function testAddOrderWithLineItems() {
387 $order = $this->addOrder(TRUE);
389 'contribution_id' => $order['id'],
391 $order = $this->callAPISuccess('order', 'get', $params);
392 $expectedResult = array(
393 $order['id'] => array(
394 'total_amount' => 300,
395 'contribution_id' => $order['id'],
396 'contribution_status' => 'Completed',
401 'entity_table' => 'civicrm_contribution',
402 'entity_id' => $order['id'],
403 'contribution_id' => $order['id'],
408 'entity_table' => 'civicrm_contribution',
409 'entity_id' => $order['id'],
410 'contribution_id' => $order['id'],
414 $this->checkPaymentResult($order, $expectedResult, $items);
416 'entity_table' => 'civicrm_contribution',
417 'entity_id' => $order['id'],
419 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
420 $this->assertEquals($eft['values'][$eft['id']]['amount'], 300);
422 'entity_table' => 'civicrm_financial_item',
423 'financial_trxn_id' => $eft['values'][$eft['id']]['financial_trxn_id'],
425 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
426 $amounts = array(200, 100);
427 foreach ($eft['values'] as $value) {
428 $this->assertEquals($value['amount'], array_pop($amounts));
430 $this->callAPISuccess('Contribution', 'Delete', array(
431 'id' => $order['id'],
436 * Test delete order api
438 public function testDeleteOrder() {
439 $order = $this->addOrder(FALSE, 100);
441 'contribution_id' => $order['id'],
444 $this->callAPISuccess('order', 'delete', $params);
445 $this->fail("Missed expected exception");
447 catch (Exception
$expected) {
448 $this->callAPISuccess('Contribution', 'create', array(
449 'contribution_id' => $order['id'],
452 $this->callAPIAndDocument('order', 'delete', $params, __FUNCTION__
, __FILE__
);
453 $order = $this->callAPISuccess('order', 'get', $params);
454 $this->assertEquals(0, $order['count']);
459 * Test cancel order api
461 public function testCancelOrder() {
462 $contribution = $this->addOrder(FALSE, 100);
464 'contribution_id' => $contribution['id'],
466 $this->callAPIAndDocument('order', 'cancel', $params, __FUNCTION__
, __FILE__
);
467 $order = $this->callAPISuccess('Order', 'get', $params);
468 $expectedResult = array(
469 $contribution['id'] => array(
470 'total_amount' => 100,
471 'contribution_id' => $contribution['id'],
472 'contribution_status' => 'Cancelled',
476 $this->checkPaymentResult($order, $expectedResult);
477 $this->callAPISuccess('Contribution', 'Delete', array(
478 'id' => $contribution['id'],
483 * Test cancel order api
485 public function testCancelWithParticipant() {
486 $event = $this->eventCreate();
487 $this->_eventId
= $event['id'];
488 $eventParams = array(
489 'id' => $this->_eventId
,
490 'financial_type_id' => 4,
493 $this->callAPISuccess('event', 'create', $eventParams);
494 $participantParams = array(
495 'financial_type_id' => 4,
496 'event_id' => $this->_eventId
,
499 'fee_currency' => 'USD',
500 'contact_id' => $this->_individualId
,
502 $participant = $this->callAPISuccess('Participant', 'create', $participantParams);
503 $extraParams = array(
504 'contribution_mode' => 'participant',
505 'participant_id' => $participant['id'],
507 $contribution = $this->addOrder(TRUE, 100, $extraParams);
508 $paymentParticipant = array(
509 'participant_id' => $participant['id'],
510 'contribution_id' => $contribution['id'],
512 $this->callAPISuccess('ParticipantPayment', 'create', $paymentParticipant);
514 'contribution_id' => $contribution['id'],
516 $this->callAPISuccess('order', 'cancel', $params);
517 $order = $this->callAPISuccess('Order', 'get', $params);
518 $expectedResult = array(
519 $contribution['id'] => array(
520 'total_amount' => 100,
521 'contribution_id' => $contribution['id'],
522 'contribution_status' => 'Cancelled',
526 $this->checkPaymentResult($order, $expectedResult);
527 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $params);
528 $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id']));
529 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Cancelled');
530 $this->callAPISuccess('Contribution', 'Delete', array(
531 'id' => $contribution['id'],