3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 $contribution = $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, $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 require_once 'CiviTest/Membership.php';
208 $membership = new Membership();
209 $membershipType = $membership->createMembershipType();
210 $membershipType1 = $membership->createMembershipType();
211 $membershipType = $membershipTypes = array($membershipType->id
, $membershipType1->id
);
213 'contact_id' => $this->_individualId
,
214 'receive_date' => '2010-01-20',
215 'total_amount' => 200,
216 'financial_type_id' => $this->_financialTypeId
,
217 'contribution_status_id' => 1,
219 $priceFields = $this->createPriceSet();
220 foreach ($priceFields['values'] as $key => $priceField) {
221 $lineItems[$key] = array(
222 'price_field_id' => $priceField['price_field_id'],
223 'price_field_value_id' => $priceField['id'],
224 'label' => $priceField['label'],
225 'field_title' => $priceField['label'],
227 'unit_price' => $priceField['amount'],
228 'line_total' => $priceField['amount'],
229 'financial_type_id' => $priceField['financial_type_id'],
230 'entity_table' => 'civicrm_membership',
231 'membership_type_id' => array_pop($membershipType),
234 $p['line_items'][] = array(
235 'line_item' => array(array_pop($lineItems)),
237 'contact_id' => $this->_individualId
,
238 'membership_type_id' => array_pop($membershipTypes),
239 'join_date' => '2006-01-21',
240 'start_date' => '2006-01-21',
241 'end_date' => '2006-12-21',
242 'source' => 'Payment',
247 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__
, __FILE__
);
249 'contribution_id' => $order['id'],
251 $order = $this->callAPISuccess('order', 'get', $params);
252 $expectedResult = array(
253 $order['id'] => array(
254 'total_amount' => 200,
255 'contribution_id' => $order['id'],
256 'contribution_status' => 'Completed',
260 $this->checkPaymentResult($order, $expectedResult);
261 $this->callAPISuccessGetCount('MembershipPayment', $params, 1);
262 $this->callAPISuccess('Contribution', 'Delete', array(
263 'id' => $order['id'],
265 $p['line_items'][] = array(
266 'line_item' => array(array_pop($lineItems)),
268 'contact_id' => $this->_individualId
,
269 'membership_type_id' => array_pop($membershipTypes),
270 'join_date' => '2006-01-21',
271 'start_date' => '2006-01-21',
272 'end_date' => '2006-12-21',
273 'source' => 'Payment',
278 $p['total_amount'] = 300;
279 $order = $this->callAPISuccess('order', 'create', $p);
280 $expectedResult = array(
281 $order['id'] => array(
282 'total_amount' => 300,
283 'contribution_status' => 'Completed',
287 $paymentMembership = array(
288 'contribution_id' => $order['id'],
290 $order = $this->callAPISuccess('order', 'get', $paymentMembership);
291 $this->checkPaymentResult($order, $expectedResult);
292 $this->callAPISuccessGetCount('MembershipPayment', $paymentMembership, 2);
293 $this->callAPISuccess('Contribution', 'Delete', array(
294 'id' => $order['id'],
299 * Test create order api for participant
301 public function testAddOrderForPariticipant() {
302 require_once 'CiviTest/Event.php';
303 $this->_eventId
= Event
::create($this->_individualId
);
305 'contact_id' => $this->_individualId
,
306 'receive_date' => '2010-01-20',
307 'total_amount' => 300,
308 'financial_type_id' => $this->_financialTypeId
,
309 'contribution_status_id' => 1,
311 $priceFields = $this->createPriceSet();
312 foreach ($priceFields['values'] as $key => $priceField) {
313 $lineItems[$key] = array(
314 'price_field_id' => $priceField['price_field_id'],
315 'price_field_value_id' => $priceField['id'],
316 'label' => $priceField['label'],
317 'field_title' => $priceField['label'],
319 'unit_price' => $priceField['amount'],
320 'line_total' => $priceField['amount'],
321 'financial_type_id' => $priceField['financial_type_id'],
322 'entity_table' => 'civicrm_participant',
325 $p['line_items'][] = array(
326 'line_item' => $lineItems,
328 'contact_id' => $this->_individualId
,
329 'event_id' => $this->_eventId
,
332 'register_date' => '2007-07-21 00:00:00',
333 'source' => 'Online Event Registration: API Testing',
336 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__
, __FILE__
, 'Create order for participant', 'CreateOrderParticipant');
338 'contribution_id' => $order['id'],
340 $order = $this->callAPISuccess('order', 'get', $params);
341 $expectedResult = array(
342 $order['id'] => array(
343 'total_amount' => 300,
344 'contribution_id' => $order['id'],
345 'contribution_status' => 'Completed',
349 $this->checkPaymentResult($order, $expectedResult);
350 $this->callAPISuccessGetCount('ParticipantPayment', $params, 1);
351 $this->callAPISuccess('Contribution', 'Delete', array(
352 'id' => $order['id'],
354 $p['line_items'][] = array(
355 'line_item' => $lineItems,
357 'contact_id' => $this->individualCreate(),
358 'event_id' => $this->_eventId
,
361 'register_date' => '2007-07-21 00:00:00',
362 'source' => 'Online Event Registration: API Testing',
365 $p['total_amount'] = 600;
366 $order = $this->callAPISuccess('order', 'create', $p);
367 $expectedResult = array(
368 $order['id'] => array(
369 'total_amount' => 600,
370 'contribution_status' => 'Completed',
374 $paymentParticipant = array(
375 'contribution_id' => $order['id'],
377 $order = $this->callAPISuccess('order', 'get', $paymentParticipant);
378 $this->checkPaymentResult($order, $expectedResult);
379 $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2);
380 $this->callAPISuccess('Contribution', 'Delete', array(
381 'id' => $order['id'],
386 * Test create order api with line items
388 public function testAddOrderWithLineItems() {
389 $order = $this->addOrder(TRUE);
391 'contribution_id' => $order['id'],
393 $order = $this->callAPISuccess('order', 'get', $params);
394 $expectedResult = array(
395 $order['id'] => array(
396 'total_amount' => 300,
397 'contribution_id' => $order['id'],
398 'contribution_status' => 'Completed',
403 'entity_table' => 'civicrm_contribution',
404 'entity_id' => $order['id'],
405 'contribution_id' => $order['id'],
410 'entity_table' => 'civicrm_contribution',
411 'entity_id' => $order['id'],
412 'contribution_id' => $order['id'],
416 $this->checkPaymentResult($order, $expectedResult, $items);
418 'entity_table' => 'civicrm_contribution',
419 'entity_id' => $order['id'],
421 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
422 $this->assertEquals($eft['values'][$eft['id']]['amount'], 300);
424 'entity_table' => 'civicrm_financial_item',
425 'financial_trxn_id' => $eft['values'][$eft['id']]['financial_trxn_id'],
427 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
428 $amounts = array(200, 100);
429 foreach ($eft['values'] as $value) {
430 $this->assertEquals($value['amount'], array_pop($amounts));
432 $this->callAPISuccess('Contribution', 'Delete', array(
433 'id' => $order['id'],
438 * Test delete order api
440 public function testDeleteOrder() {
441 $order = $this->addOrder(FALSE, 100);
443 'contribution_id' => $order['id'],
446 $this->callAPISuccess('order', 'delete', $params);
447 $this->fail("Missed expected exception");
449 catch (Exception
$expected) {
450 $this->callAPISuccess('Contribution', 'create', array(
451 'contribution_id' => $order['id'],
454 $this->callAPIAndDocument('order', 'delete', $params, __FUNCTION__
, __FILE__
);
455 $order = $this->callAPISuccess('order', 'get', $params);
456 $this->assertEquals(0, $order['count']);
461 * Test cancel order api
463 public function testCancelOrder() {
464 $contribution = $this->addOrder(FALSE, 100);
466 'contribution_id' => $contribution['id'],
468 $this->callAPIAndDocument('order', 'cancel', $params, __FUNCTION__
, __FILE__
);
469 $order = $this->callAPISuccess('Order', 'get', $params);
470 $expectedResult = array(
471 $contribution['id'] => array(
472 'total_amount' => 100,
473 'contribution_id' => $contribution['id'],
474 'contribution_status' => 'Cancelled',
478 $this->checkPaymentResult($order, $expectedResult);
479 $this->callAPISuccess('Contribution', 'Delete', array(
480 'id' => $contribution['id'],
485 * Test cancel order api
487 public function testCancelWithParticipant() {
488 $this->_eventId
= Event
::create($this->_individualId
);
489 $eventParams = array(
490 'id' => $this->_eventId
,
491 'financial_type_id' => 4,
494 $this->callAPISuccess('event', 'create', $eventParams);
495 $participantParams = array(
496 'financial_type_id' => 4,
497 'event_id' => $this->_eventId
,
500 'fee_currency' => 'USD',
501 'contact_id' => $this->_individualId
,
503 $participant = $this->callAPISuccess('Participant', 'create', $participantParams);
504 $extraParams = array(
505 'contribution_mode' => 'participant',
506 'participant_id' => $participant['id'],
508 $contribution = $this->addOrder(TRUE, 100, $extraParams);
509 $paymentParticipant = array(
510 'participant_id' => $participant['id'],
511 'contribution_id' => $contribution['id'],
513 $this->callAPISuccess('ParticipantPayment', 'create', $paymentParticipant);
515 'contribution_id' => $contribution['id'],
517 $this->callAPISuccess('order', 'cancel', $params);
518 $order = $this->callAPISuccess('Order', 'get', $params);
519 $expectedResult = array(
520 $contribution['id'] => array(
521 'total_amount' => 100,
522 'contribution_id' => $contribution['id'],
523 'contribution_status' => 'Cancelled',
527 $this->checkPaymentResult($order, $expectedResult);
528 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $params);
529 $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id']));
530 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Cancelled');
531 $this->callAPISuccess('Contribution', 'Delete', array(
532 'id' => $contribution['id'],