3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
{
37 protected $_individualId;
38 protected $_financialTypeId = 1;
39 protected $_apiversion;
45 public function setUp() {
48 $this->_apiversion
= 3;
49 $this->_individualId
= $this->individualCreate();
53 * Clean up after each test.
55 public function tearDown() {
56 $this->quickCleanUpFinancialEntities();
57 $this->quickCleanup(['civicrm_uf_match']);
63 public function testGetOrder() {
64 $contribution = $this->addOrder(FALSE, 100);
67 'contribution_id' => $contribution['id'],
70 $order = $this->callAPIAndDocument('Order', 'get', $params, __FUNCTION__
, __FILE__
);
72 $this->assertEquals(1, $order['count']);
74 $contribution['id'] => [
75 'total_amount' => 100,
76 'contribution_id' => $contribution['id'],
77 'contribution_status' => 'Completed',
82 'entity_table' => 'civicrm_contribution',
83 'entity_id' => $contribution['id'],
84 'contribution_id' => $contribution['id'],
87 'financial_type_id' => 1,
89 $this->checkPaymentResult($order, $expectedResult, $lineItems);
90 $this->callAPISuccess('Contribution', 'Delete', [
91 'id' => $contribution['id'],
96 * Test Get Order api for participant contribution.
98 public function testGetOrderParticipant() {
99 $this->addOrder(FALSE, 100);
100 list($items, $contribution) = $this->createParticipantWithContribution();
103 'contribution_id' => $contribution['id'],
106 $order = $this->callAPISuccess('Order', 'get', $params);
108 $this->assertEquals(2, count($order['values'][$contribution['id']]['line_items']));
109 $this->callAPISuccess('Contribution', 'Delete', [
110 'id' => $contribution['id'],
115 * Function to assert db values.
117 public function checkPaymentResult($results, $expectedResult, $lineItems = NULL) {
118 foreach ($expectedResult[$results['id']] as $key => $value) {
119 $this->assertEquals($results['values'][$results['id']][$key], $value);
123 foreach ($lineItems as $key => $items) {
124 foreach ($items as $k => $item) {
125 $this->assertEquals($results['values'][$results['id']]['line_items'][$key][$k], $item);
134 * @param bool $isPriceSet
135 * @param float $amount
136 * @param array $extraParams
140 public function addOrder($isPriceSet, $amount = 300.00, $extraParams = []) {
142 'contact_id' => $this->_individualId
,
143 'receive_date' => '2010-01-20',
144 'total_amount' => $amount,
145 'financial_type_id' => $this->_financialTypeId
,
146 'contribution_status_id' => 1,
150 $priceFields = $this->createPriceSet();
151 foreach ($priceFields['values'] as $key => $priceField) {
152 $lineItems[1][$key] = [
153 'price_field_id' => $priceField['price_field_id'],
154 'price_field_value_id' => $priceField['id'],
155 'label' => $priceField['label'],
156 'field_title' => $priceField['label'],
158 'unit_price' => $priceField['amount'],
159 'line_total' => $priceField['amount'],
160 'financial_type_id' => $priceField['financial_type_id'],
163 $p['line_item'] = $lineItems;
165 $p = array_merge($extraParams, $p);
166 return $this->callAPISuccess('Contribution', 'create', $p);
170 * Test create order api
172 public function testAddOrder() {
173 $order = $this->addOrder(FALSE, 100);
175 'contribution_id' => $order['id'],
177 $order = $this->callAPISuccess('order', 'get', $params);
180 'total_amount' => 100,
181 'contribution_id' => $order['id'],
182 'contribution_status' => 'Completed',
187 'entity_table' => 'civicrm_contribution',
188 'entity_id' => $order['id'],
189 'contribution_id' => $order['id'],
192 'financial_type_id' => 1,
194 $this->checkPaymentResult($order, $expectedResult, $lineItems);
195 $this->callAPISuccess('Contribution', 'Delete', [
196 'id' => $order['id'],
201 * Test create order api for membership
203 public function testAddOrderForMembership() {
204 $membershipType = $this->membershipTypeCreate();
205 $membershipType1 = $this->membershipTypeCreate();
206 $membershipType = $membershipTypes = [$membershipType, $membershipType1];
208 'contact_id' => $this->_individualId
,
209 'receive_date' => '2010-01-20',
210 'total_amount' => 200,
211 'financial_type_id' => 'Event Fee',
212 'contribution_status_id' => 1,
214 $priceFields = $this->createPriceSet();
215 foreach ($priceFields['values'] as $key => $priceField) {
217 'price_field_id' => $priceField['price_field_id'],
218 'price_field_value_id' => $priceField['id'],
219 'label' => $priceField['label'],
220 'field_title' => $priceField['label'],
222 'unit_price' => $priceField['amount'],
223 'line_total' => $priceField['amount'],
224 'financial_type_id' => $priceField['financial_type_id'],
225 'entity_table' => 'civicrm_membership',
226 'membership_type_id' => array_pop($membershipType),
229 $p['line_items'][] = [
230 'line_item' => [array_pop($lineItems)],
232 'contact_id' => $this->_individualId
,
233 'membership_type_id' => array_pop($membershipTypes),
234 'join_date' => '2006-01-21',
235 'start_date' => '2006-01-21',
236 'end_date' => '2006-12-21',
237 'source' => 'Payment',
242 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__
, __FILE__
);
244 'contribution_id' => $order['id'],
246 $order = $this->callAPISuccess('order', 'get', $params);
249 'total_amount' => 200,
250 'contribution_id' => $order['id'],
251 'contribution_status' => 'Completed',
255 $this->checkPaymentResult($order, $expectedResult);
256 $this->callAPISuccessGetCount('MembershipPayment', $params, 1);
257 $this->callAPISuccess('Contribution', 'Delete', [
258 'id' => $order['id'],
260 $p['line_items'][] = [
261 'line_item' => [array_pop($lineItems)],
263 'contact_id' => $this->_individualId
,
264 'membership_type_id' => array_pop($membershipTypes),
265 'join_date' => '2006-01-21',
266 'start_date' => '2006-01-21',
267 'end_date' => '2006-12-21',
268 'source' => 'Payment',
273 $p['total_amount'] = 300;
274 $order = $this->callAPISuccess('order', 'create', $p);
277 'total_amount' => 300,
278 'contribution_status' => 'Completed',
282 $paymentMembership = [
283 'contribution_id' => $order['id'],
285 $order = $this->callAPISuccess('order', 'get', $paymentMembership);
286 $this->checkPaymentResult($order, $expectedResult);
287 $this->callAPISuccessGetCount('MembershipPayment', $paymentMembership, 2);
288 $this->callAPISuccess('Contribution', 'Delete', [
289 'id' => $order['id'],
294 * Test create order api for participant
296 public function testAddOrderForParticipant() {
297 $event = $this->eventCreate();
298 $this->_eventId
= $event['id'];
300 'contact_id' => $this->_individualId
,
301 'receive_date' => '2010-01-20',
302 'total_amount' => 300,
303 'financial_type_id' => $this->_financialTypeId
,
304 'contribution_status_id' => 1,
306 $priceFields = $this->createPriceSet();
307 foreach ($priceFields['values'] as $key => $priceField) {
309 'price_field_id' => $priceField['price_field_id'],
310 'price_field_value_id' => $priceField['id'],
311 'label' => $priceField['label'],
312 'field_title' => $priceField['label'],
314 'unit_price' => $priceField['amount'],
315 'line_total' => $priceField['amount'],
316 'financial_type_id' => $priceField['financial_type_id'],
317 'entity_table' => 'civicrm_participant',
320 $p['line_items'][] = [
321 'line_item' => $lineItems,
323 'contact_id' => $this->_individualId
,
324 'event_id' => $this->_eventId
,
327 'register_date' => '2007-07-21 00:00:00',
328 'source' => 'Online Event Registration: API Testing',
331 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__
, __FILE__
, 'Create order for participant', 'CreateOrderParticipant');
333 'contribution_id' => $order['id'],
335 $order = $this->callAPISuccess('order', 'get', $params);
338 'total_amount' => 300,
339 'contribution_id' => $order['id'],
340 'contribution_status' => 'Completed',
344 $this->checkPaymentResult($order, $expectedResult);
345 $this->callAPISuccessGetCount('ParticipantPayment', $params, 1);
346 $this->callAPISuccess('Contribution', 'Delete', [
347 'id' => $order['id'],
349 $p['line_items'][] = [
350 'line_item' => $lineItems,
352 'contact_id' => $this->individualCreate(),
353 'event_id' => $this->_eventId
,
356 'register_date' => '2007-07-21 00:00:00',
357 'source' => 'Online Event Registration: API Testing',
360 $p['total_amount'] = 600;
361 $order = $this->callAPISuccess('order', 'create', $p);
364 'total_amount' => 600,
365 'contribution_status' => 'Completed',
369 $paymentParticipant = [
370 'contribution_id' => $order['id'],
372 $order = $this->callAPISuccess('order', 'get', $paymentParticipant);
373 $this->checkPaymentResult($order, $expectedResult);
374 $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2);
375 $this->callAPISuccess('Contribution', 'Delete', [
376 'id' => $order['id'],
381 * Test create order api with line items
383 public function testAddOrderWithLineItems() {
384 $order = $this->addOrder(TRUE);
386 'contribution_id' => $order['id'],
388 $order = $this->callAPISuccess('order', 'get', $params);
391 'total_amount' => 300,
392 'contribution_id' => $order['id'],
393 'contribution_status' => 'Completed',
398 'entity_table' => 'civicrm_contribution',
399 'entity_id' => $order['id'],
400 'contribution_id' => $order['id'],
405 'entity_table' => 'civicrm_contribution',
406 'entity_id' => $order['id'],
407 'contribution_id' => $order['id'],
411 $this->checkPaymentResult($order, $expectedResult, $items);
413 'entity_table' => 'civicrm_contribution',
414 'entity_id' => $order['id'],
416 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
417 $this->assertEquals($eft['values'][$eft['id']]['amount'], 300);
419 'entity_table' => 'civicrm_financial_item',
420 'financial_trxn_id' => $eft['values'][$eft['id']]['financial_trxn_id'],
422 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
423 $amounts = [200, 100];
424 foreach ($eft['values'] as $value) {
425 $this->assertEquals($value['amount'], array_pop($amounts));
427 $this->callAPISuccess('Contribution', 'Delete', [
428 'id' => $order['id'],
433 * Test delete order api
435 public function testDeleteOrder() {
436 $order = $this->addOrder(FALSE, 100);
438 'contribution_id' => $order['id'],
441 $this->callAPISuccess('order', 'delete', $params);
442 $this->fail("Missed expected exception");
444 catch (Exception
$expected) {
445 $this->callAPISuccess('Contribution', 'create', [
446 'contribution_id' => $order['id'],
449 $this->callAPIAndDocument('order', 'delete', $params, __FUNCTION__
, __FILE__
);
450 $order = $this->callAPISuccess('order', 'get', $params);
451 $this->assertEquals(0, $order['count']);
456 * Test cancel order api
458 public function testCancelOrder() {
459 $contribution = $this->addOrder(FALSE, 100);
461 'contribution_id' => $contribution['id'],
463 $this->callAPIAndDocument('order', 'cancel', $params, __FUNCTION__
, __FILE__
);
464 $order = $this->callAPISuccess('Order', 'get', $params);
466 $contribution['id'] => [
467 'total_amount' => 100,
468 'contribution_id' => $contribution['id'],
469 'contribution_status' => 'Cancelled',
473 $this->checkPaymentResult($order, $expectedResult);
474 $this->callAPISuccess('Contribution', 'Delete', [
475 'id' => $contribution['id'],
480 * Test cancel order api
482 public function testCancelWithParticipant() {
483 $event = $this->eventCreate();
484 $this->_eventId
= $event['id'];
486 'id' => $this->_eventId
,
487 'financial_type_id' => 4,
490 $this->callAPISuccess('event', 'create', $eventParams);
491 $participantParams = [
492 'financial_type_id' => 4,
493 'event_id' => $this->_eventId
,
496 'fee_currency' => 'USD',
497 'contact_id' => $this->_individualId
,
499 $participant = $this->callAPISuccess('Participant', 'create', $participantParams);
501 'contribution_mode' => 'participant',
502 'participant_id' => $participant['id'],
504 $contribution = $this->addOrder(TRUE, 100, $extraParams);
505 $paymentParticipant = [
506 'participant_id' => $participant['id'],
507 'contribution_id' => $contribution['id'],
509 $this->callAPISuccess('ParticipantPayment', 'create', $paymentParticipant);
511 'contribution_id' => $contribution['id'],
513 $this->callAPISuccess('order', 'cancel', $params);
514 $order = $this->callAPISuccess('Order', 'get', $params);
516 $contribution['id'] => [
517 'total_amount' => 100,
518 'contribution_id' => $contribution['id'],
519 'contribution_status' => 'Cancelled',
523 $this->checkPaymentResult($order, $expectedResult);
524 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $params);
525 $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
526 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Cancelled');
527 $this->callAPISuccess('Contribution', 'Delete', [
528 'id' => $contribution['id'],
533 * @expectedException CiviCRM_API3_Exception
534 * @expectedExceptionMessage Line item total doesn't match with total amount.
536 public function testCreateOrderIfTotalAmountDoesNotMatchLineItemsAmountsIfNoTaxSupplied() {
538 'contact_id' => $this->_individualId
,
539 'receive_date' => '2018-01-01',
540 'total_amount' => 50,
541 'financial_type_id' => $this->_financialTypeId
,
542 'contribution_status_id' => 1,
547 'price_field_id' => 1,
548 'price_field_value_id' => 1,
550 'field_title' => 'Test 1',
554 'financial_type_id' => 1,
555 'entity_table' => 'civicrm_contribution',
562 civicrm_api3('Order', 'create', $params);
566 * @expectedException CiviCRM_API3_Exception
567 * @expectedExceptionMessage Line item total doesn't match with total amount.
569 public function testCreateOrderIfTotalAmountDoesNotMatchLineItemsAmountsIfTaxSupplied() {
571 'contact_id' => $this->_individualId
,
572 'receive_date' => '2018-01-01',
573 'total_amount' => 50,
574 'financial_type_id' => $this->_financialTypeId
,
575 'contribution_status_id' => 1,
581 'price_field_id' => 1,
582 'price_field_value_id' => 1,
584 'field_title' => 'Test 1',
588 'financial_type_id' => 1,
589 'entity_table' => 'civicrm_contribution',
597 civicrm_api3('Order', 'create', $params);
600 public function testCreateOrderIfTotalAmountDoesMatchLineItemsAmountsAndTaxSupplied() {
602 'contact_id' => $this->_individualId
,
603 'receive_date' => '2018-01-01',
604 'total_amount' => 50,
605 'financial_type_id' => $this->_financialTypeId
,
606 'contribution_status_id' => 1,
612 'price_field_id' => 1,
613 'price_field_value_id' => 1,
615 'field_title' => 'Test 1',
619 'financial_type_id' => 1,
620 'entity_table' => 'civicrm_contribution',
628 $order = civicrm_api3('Order', 'create', $params);
629 $this->assertEquals(1, $order['count']);