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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 require_once 'CiviTest/CiviMailUtils.php';
33 * Test APIv3 civicrm_contribute_* functions
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
38 class api_v3_ContributionTest
extends CiviUnitTestCase
{
41 * Assume empty database with just civicrm_data.
43 protected $_individualId;
44 protected $_contribution;
45 protected $_financialTypeId = 1;
46 protected $_apiversion;
47 protected $_entity = 'Contribution';
50 protected $_ids = array();
51 protected $_pageParams = array();
54 * Parameters to create payment processor.
58 protected $_processorParams = array();
61 * ID of created event.
70 public function setUp() {
73 $this->_apiversion
= 3;
74 $this->_individualId
= $this->individualCreate();
75 $this->_params
= array(
76 'contact_id' => $this->_individualId
,
77 'receive_date' => '20120511',
78 'total_amount' => 100.00,
79 'financial_type_id' => $this->_financialTypeId
,
80 'non_deductible_amount' => 10.00,
82 'net_amount' => 95.00,
84 'contribution_status_id' => 1,
86 $this->_processorParams
= array(
89 'payment_processor_type_id' => 10,
90 'financial_account_id' => 12,
93 'url_site' => 'http://dummy.com',
94 'url_recur' => 'http://dummy.com',
97 $this->_pageParams
= array(
98 'title' => 'Test Contribution Page',
99 'financial_type_id' => 1,
101 'financial_account_id' => 1,
102 'payment_processor' => $this->processorCreate(),
104 'is_allow_other_amount' => 1,
106 'max_amount' => 1000,
111 * Clean up after each test.
113 public function tearDown() {
114 $this->quickCleanUpFinancialEntities();
115 $this->quickCleanup(array('civicrm_uf_match'));
121 public function testGetContribution() {
123 'contact_id' => $this->_individualId
,
124 'receive_date' => '2010-01-20',
125 'total_amount' => 100.00,
126 'financial_type_id' => $this->_financialTypeId
,
127 'non_deductible_amount' => 10.00,
128 'fee_amount' => 5.00,
129 'net_amount' => 95.00,
131 'invoice_id' => 78910,
133 'contribution_status_id' => 1,
135 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
138 'contribution_id' => $this->_contribution
['id'],
141 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
142 $financialParams['id'] = $this->_financialTypeId
;
144 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
146 $this->assertEquals(1, $contribution['count']);
147 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
148 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
149 // Passing back a string rather than an id seems like an error/cruft.
150 // If it is to be introduced we should discuss.
151 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
152 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
153 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
154 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
155 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
156 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
157 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
158 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
159 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
160 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
161 $p['trxn_id'] = '3847';
162 $p['invoice_id'] = '3847';
164 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
166 // Now we have 2 - test getcount.
167 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
168 $this->assertEquals(2, $contribution);
169 // Test id only format.
170 $contribution = $this->callAPISuccess('contribution', 'get', array(
171 'id' => $this->_contribution
['id'],
172 'format.only_id' => 1,
174 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
175 // Test id only format.
176 $contribution = $this->callAPISuccess('contribution', 'get', array(
177 'id' => $contribution2['id'],
178 'format.only_id' => 1,
180 $this->assertEquals($contribution2['id'], $contribution);
182 $contribution = $this->callAPISuccess('contribution', 'get', array(
183 'id' => $this->_contribution
['id'],
185 $this->assertEquals(1, $contribution['count']);
187 // Test get by contact id works.
188 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
190 $this->assertEquals(2, $contribution['count']);
191 $this->callAPISuccess('Contribution', 'Delete', array(
192 'id' => $this->_contribution
['id'],
194 $this->callAPISuccess('Contribution', 'Delete', array(
195 'id' => $contribution2['id'],
200 * Test that test contributions can be retrieved.
202 public function testGetTestContribution() {
203 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('is_test' => 1)));
204 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
208 * We need to ensure previous tested behaviour still works as part of the api contract.
210 public function testGetContributionLegacyBehaviour() {
212 'contact_id' => $this->_individualId
,
213 'receive_date' => '2010-01-20',
214 'total_amount' => 100.00,
215 'contribution_type_id' => $this->_financialTypeId
,
216 'non_deductible_amount' => 10.00,
217 'fee_amount' => 5.00,
218 'net_amount' => 95.00,
220 'invoice_id' => 78910,
222 'contribution_status_id' => 1,
224 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
227 'contribution_id' => $this->_contribution
['id'],
229 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
230 $financialParams['id'] = $this->_financialTypeId
;
232 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
234 $this->assertEquals(1, $contribution['count']);
235 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
236 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
237 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
238 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
239 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
240 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
241 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
242 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
243 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
244 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
245 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
247 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
248 $p['trxn_id'] = '3847';
249 $p['invoice_id'] = '3847';
251 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
253 // now we have 2 - test getcount
254 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
255 $this->assertEquals(2, $contribution);
256 //test id only format
257 $contribution = $this->callAPISuccess('contribution', 'get', array(
258 'id' => $this->_contribution
['id'],
259 'format.only_id' => 1,
261 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
262 //test id only format
263 $contribution = $this->callAPISuccess('contribution', 'get', array(
264 'id' => $contribution2['id'],
265 'format.only_id' => 1,
267 $this->assertEquals($contribution2['id'], $contribution);
268 $contribution = $this->callAPISuccess('contribution', 'get', array(
269 'id' => $this->_contribution
['id'],
272 $this->assertEquals(1, $contribution['count']);
273 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
274 //test get by contact id works
275 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
277 $this->assertEquals(2, $contribution['count']);
278 $this->callAPISuccess('Contribution', 'Delete', array(
279 'id' => $this->_contribution
['id'],
281 $this->callAPISuccess('Contribution', 'Delete', array(
282 'id' => $contribution2['id'],
287 * Create an contribution_id=FALSE and financial_type_id=Donation.
289 public function testCreateEmptyContributionIDUseDonation() {
291 'contribution_id' => FALSE,
294 'check_permissions' => FALSE,
295 'financial_type_id' => 'Donation',
297 $this->callAPISuccess('contribution', 'create', $params);
301 * Check with complete array + custom field.
303 * Note that the test is written on purpose without any
304 * variables specific to participant so it can be replicated into other entities
305 * and / or moved to the automated test suite
307 public function testCreateWithCustom() {
308 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
310 $params = $this->_params
;
311 $params['custom_' . $ids['custom_field_id']] = "custom string";
313 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
314 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
315 $check = $this->callAPISuccess($this->_entity
, 'get', array(
316 'return.custom_' . $ids['custom_field_id'] => 1,
317 'id' => $result['id'],
319 $this->customFieldDelete($ids['custom_field_id']);
320 $this->customGroupDelete($ids['custom_group_id']);
321 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
325 * Check with complete array + custom field.
327 * Note that the test is written on purpose without any
328 * variables specific to participant so it can be replicated into other entities
329 * and / or moved to the automated test suite
331 public function testCreateGetFieldsWithCustom() {
332 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
333 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
334 $result = $this->callAPISuccess('Contribution', 'getfields', array());
335 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
336 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
337 $this->customFieldDelete($ids['custom_field_id']);
338 $this->customGroupDelete($ids['custom_group_id']);
339 $this->customFieldDelete($idsContact['custom_field_id']);
340 $this->customGroupDelete($idsContact['custom_group_id']);
343 public function testCreateContributionNoLineItems() {
346 'contact_id' => $this->_individualId
,
347 'receive_date' => '20120511',
348 'total_amount' => 100.00,
349 'financial_type_id' => $this->_financialTypeId
,
350 'payment_instrument_id' => 1,
351 'non_deductible_amount' => 10.00,
352 'fee_amount' => 50.00,
353 'net_amount' => 90.00,
355 'invoice_id' => 67890,
357 'contribution_status_id' => 1,
361 $contribution = $this->callAPISuccess('contribution', 'create', $params);
362 $lineItems = $this->callAPISuccess('line_item', 'get', array(
363 'entity_id' => $contribution['id'],
364 'entity_table' => 'civicrm_contribution',
367 $this->assertEquals(0, $lineItems['count']);
371 * Test checks that passing in line items suppresses the create mechanism.
373 public function testCreateContributionChainedLineItems() {
375 'contact_id' => $this->_individualId
,
376 'receive_date' => '20120511',
377 'total_amount' => 100.00,
378 'financial_type_id' => $this->_financialTypeId
,
379 'payment_instrument_id' => 1,
380 'non_deductible_amount' => 10.00,
381 'fee_amount' => 50.00,
382 'net_amount' => 90.00,
384 'invoice_id' => 67890,
386 'contribution_status_id' => 1,
388 'api.line_item.create' => array(
390 'price_field_id' => 1,
392 'line_total' => '20',
393 'unit_price' => '10',
396 'price_field_id' => 1,
398 'line_total' => '80',
399 'unit_price' => '80',
404 $description = "Create Contribution with Nested Line Items.";
405 $subfile = "CreateWithNestedLineItems";
406 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
408 $lineItems = $this->callAPISuccess('line_item', 'get', array(
409 'entity_id' => $contribution['id'],
410 'contribution_id' => $contribution['id'],
411 'entity_table' => 'civicrm_contribution',
414 $this->assertEquals(2, $lineItems['count']);
417 public function testCreateContributionOffline() {
419 'contact_id' => $this->_individualId
,
420 'receive_date' => '20120511',
421 'total_amount' => 100.00,
422 'financial_type_id' => 1,
424 'invoice_id' => 67890,
426 'contribution_status_id' => 1,
429 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
430 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
431 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
432 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
433 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
434 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
435 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
436 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
437 $lineItems = $this->callAPISuccess('line_item', 'get', array(
438 'entity_id' => $contribution['id'],
439 'contribution_id' => $contribution['id'],
440 'entity_table' => 'civicrm_contribution',
443 $this->assertEquals(1, $lineItems['count']);
444 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
445 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
446 $this->_checkFinancialRecords($contribution, 'offline');
447 $this->contributionGetnCheck($params, $contribution['id']);
451 * Test create with valid payment instrument.
453 public function testCreateContributionWithPaymentInstrument() {
454 $params = $this->_params +
array('payment_instrument' => 'EFT');
455 $contribution = $this->callAPISuccess('contribution', 'create', $params);
456 $contribution = $this->callAPISuccess('contribution', 'get', array(
458 'id' => $contribution['id'],
460 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
461 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
463 $this->callAPISuccess('contribution', 'create', array(
464 'id' => $contribution['id'],
465 'payment_instrument' => 'Credit Card',
467 $contribution = $this->callAPISuccess('contribution', 'get', array(
469 'id' => $contribution['id'],
471 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
472 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
475 public function testGetContributionByPaymentInstrument() {
476 $params = $this->_params +
array('payment_instrument' => 'EFT');
477 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
478 $this->callAPISuccess('contribution', 'create', $params);
479 $this->callAPISuccess('contribution', 'create', $params2);
480 $contribution = $this->callAPISuccess('contribution', 'get', array(
482 'contribution_payment_instrument' => 'Cash',
484 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
485 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
486 $this->assertEquals(1, $contribution['count']);
487 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
488 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
489 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
490 $this->assertEquals(1, $contribution['count']);
491 $contribution = $this->callAPISuccess('contribution', 'get', array(
493 'payment_instrument_id' => 5,
495 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
496 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
497 $this->assertEquals(1, $contribution['count']);
498 $contribution = $this->callAPISuccess('contribution', 'get', array(
500 'payment_instrument' => 'EFT',
502 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
503 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
504 $this->assertEquals(1, $contribution['count']);
505 $contribution = $this->callAPISuccess('contribution', 'create', array(
506 'id' => $contribution['id'],
507 'payment_instrument' => 'Credit Card',
509 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
510 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
511 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
512 $this->assertEquals(1, $contribution['count']);
516 * CRM-16227 introduces invoice_id as a parameter.
518 public function testGetContributionByInvoice() {
519 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('invoice_id' => 'curly')));
520 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), array('invoice_id' => 'churlish'));
521 $this->callAPISuccessGetCount('Contribution', array(), 2);
522 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
523 // The following don't work. They are the format we are trying to introduce but although the form uses this format
524 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
525 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
526 // come out of convertFormValues
527 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
528 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
529 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
530 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
535 * Create test with unique field name on source.
537 public function testCreateContributionSource() {
540 'contact_id' => $this->_individualId
,
541 'receive_date' => date('Ymd'),
542 'total_amount' => 100.00,
543 'financial_type_id' => $this->_financialTypeId
,
544 'payment_instrument_id' => 1,
545 'non_deductible_amount' => 10.00,
546 'fee_amount' => 50.00,
547 'net_amount' => 90.00,
549 'invoice_id' => 67890,
550 'contribution_source' => 'SSF',
551 'contribution_status_id' => 1,
554 $contribution = $this->callAPISuccess('contribution', 'create', $params);
555 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
556 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
560 * Create test with unique field name on source.
562 public function testCreateDefaultNow() {
564 $params = $this->_params
;
565 unset($params['receive_date']);
567 $contribution = $this->callAPISuccess('contribution', 'create', $params);
568 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
569 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
573 * Create test with unique field name on source.
575 public function testCreateContributionSourceInvalidContact() {
579 'receive_date' => date('Ymd'),
580 'total_amount' => 100.00,
581 'financial_type_id' => $this->_financialTypeId
,
582 'payment_instrument_id' => 1,
583 'non_deductible_amount' => 10.00,
584 'fee_amount' => 50.00,
585 'net_amount' => 90.00,
587 'invoice_id' => 67890,
588 'contribution_source' => 'SSF',
589 'contribution_status_id' => 1,
592 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
595 public function testCreateContributionSourceInvalidContContact() {
598 'contribution_contact_id' => 999,
599 'receive_date' => date('Ymd'),
600 'total_amount' => 100.00,
601 'financial_type_id' => $this->_financialTypeId
,
602 'payment_instrument_id' => 1,
603 'non_deductible_amount' => 10.00,
604 'fee_amount' => 50.00,
605 'net_amount' => 90.00,
607 'invoice_id' => 67890,
608 'contribution_source' => 'SSF',
609 'contribution_status_id' => 1,
612 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
616 * Test note created correctly.
618 public function testCreateContributionWithNote() {
619 $description = "Demonstrates creating contribution with Note Entity.";
620 $subfile = "ContributionCreateWithNote";
622 'contact_id' => $this->_individualId
,
623 'receive_date' => '2012-01-01',
624 'total_amount' => 100.00,
625 'financial_type_id' => $this->_financialTypeId
,
626 'payment_instrument_id' => 1,
627 'non_deductible_amount' => 10.00,
628 'fee_amount' => 50.00,
629 'net_amount' => 90.00,
631 'invoice_id' => 67890,
633 'contribution_status_id' => 1,
634 'note' => 'my contribution note',
637 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
638 $result = $this->callAPISuccess('note', 'get', array(
639 'entity_table' => 'civicrm_contribution',
640 'entity_id' => $contribution['id'],
643 $this->assertEquals('my contribution note', $result['values'][0]['note']);
644 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
647 public function testCreateContributionWithNoteUniqueNameAliases() {
649 'contact_id' => $this->_individualId
,
650 'receive_date' => '2012-01-01',
651 'total_amount' => 100.00,
652 'financial_type_id' => $this->_financialTypeId
,
653 'payment_instrument_id' => 1,
654 'non_deductible_amount' => 10.00,
655 'fee_amount' => 50.00,
656 'net_amount' => 90.00,
658 'invoice_id' => 67890,
660 'contribution_status_id' => 1,
661 'contribution_note' => 'my contribution note',
664 $contribution = $this->callAPISuccess('contribution', 'create', $params);
665 $result = $this->callAPISuccess('note', 'get', array(
666 'entity_table' => 'civicrm_contribution',
667 'entity_id' => $contribution['id'],
670 $this->assertEquals('my contribution note', $result['values'][0]['note']);
671 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
675 * This is the test for creating soft credits.
677 public function testCreateContributionWithSoftCredit() {
678 $description = "Demonstrates creating contribution with SoftCredit.";
679 $subfile = "ContributionCreateWithSoftCredit";
680 $contact2 = $this->callAPISuccess('Contact', 'create', array(
681 'display_name' => 'superman',
682 'contact_type' => 'Individual',
685 'contact_id' => $contact2['id'],
687 'soft_credit_type_id' => 3,
690 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
691 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
692 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
694 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
695 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
696 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
698 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
699 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
702 public function testCreateContributionWithSoftCreditDefaults() {
703 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
704 $subfile = "ContributionCreateWithSoftCreditDefaults";
705 $contact2 = $this->callAPISuccess('Contact', 'create', array(
706 'display_name' => 'superman',
707 'contact_type' => 'Individual',
709 $params = $this->_params +
array(
710 'soft_credit_to' => $contact2['id'],
712 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
713 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
715 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
716 // Default soft credit amount = contribution.total_amount
717 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
718 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
720 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
721 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
724 public function testCreateContributionWithHonoreeContact() {
725 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
726 $subfile = "ContributionCreateWithHonoreeContact";
727 $contact2 = $this->callAPISuccess('Contact', 'create', array(
728 'display_name' => 'superman',
729 'contact_type' => 'Individual',
731 $params = $this->_params +
array(
732 'honor_contact_id' => $contact2['id'],
734 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
735 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
737 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
738 // Default soft credit amount = contribution.total_amount
739 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
740 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
741 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
743 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
744 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
748 * Test using example code.
750 public function testContributionCreateExample() {
751 //make sure at least on page exists since there is a truncate in tear down
752 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
753 require_once 'api/v3/examples/Contribution/Create.php';
754 $result = contribution_create_example();
756 $expectedResult = contribution_create_expectedresult();
757 $this->checkArrayEquals($expectedResult, $result);
758 $this->contributionDelete($id);
762 * Function tests that additional financial records are created when fee amount is recorded.
764 public function testCreateContributionWithFee() {
766 'contact_id' => $this->_individualId
,
767 'receive_date' => '20120511',
768 'total_amount' => 100.00,
770 'financial_type_id' => 1,
772 'invoice_id' => 67890,
774 'contribution_status_id' => 1,
777 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
778 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
779 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
780 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
781 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
782 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
783 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
784 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
785 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
786 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
788 $lineItems = $this->callAPISuccess('line_item', 'get', array(
790 'entity_id' => $contribution['id'],
791 'entity_table' => 'civicrm_contribution',
794 $this->assertEquals(1, $lineItems['count']);
795 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
796 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
797 $lineItems = $this->callAPISuccess('line_item', 'get', array(
799 'entity_id' => $contribution['id'],
800 'contribution_id' => $contribution['id'],
801 'entity_table' => 'civicrm_contribution',
804 $this->assertEquals(1, $lineItems['count']);
805 $this->_checkFinancialRecords($contribution, 'feeAmount');
810 * Function tests that additional financial records are created when online contribution is created.
812 public function testCreateContributionOnline() {
813 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
814 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
815 $this->assertAPISuccess($contributionPage);
817 'contact_id' => $this->_individualId
,
818 'receive_date' => '20120511',
819 'total_amount' => 100.00,
820 'financial_type_id' => 1,
821 'contribution_page_id' => $contributionPage['id'],
822 'payment_processor' => 1,
824 'invoice_id' => 67890,
826 'contribution_status_id' => 1,
830 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
831 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
832 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
833 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
834 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
835 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
836 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
837 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
838 $this->_checkFinancialRecords($contribution, 'online');
842 * Check handling of financial type.
844 * In the interests of removing financial type / contribution type checks from
845 * legacy format function lets test that the api is doing this for us
847 public function testCreateInvalidFinancialType() {
848 $params = $this->_params
;
849 $params['financial_type_id'] = 99999;
850 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
854 * Check handling of financial type.
856 * In the interests of removing financial type / contribution type checks from
857 * legacy format function lets test that the api is doing this for us
859 public function testValidNamedFinancialType() {
860 $params = $this->_params
;
861 $params['financial_type_id'] = 'Donation';
862 $this->callAPISuccess($this->_entity
, 'create', $params);
866 * Tests that additional financial records are created.
868 * Checks when online contribution with pay later option is created
870 public function testCreateContributionPayLaterOnline() {
871 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
872 $this->_pageParams
['is_pay_later'] = 1;
873 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
874 $this->assertAPISuccess($contributionPage);
876 'contact_id' => $this->_individualId
,
877 'receive_date' => '20120511',
878 'total_amount' => 100.00,
879 'financial_type_id' => 1,
880 'contribution_page_id' => $contributionPage['id'],
883 'invoice_id' => 67890,
885 'contribution_status_id' => 2,
889 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
890 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
891 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
892 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
893 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
894 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
895 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
896 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
897 $this->_checkFinancialRecords($contribution, 'payLater');
901 * Function tests that additional financial records are created for online contribution with pending option.
903 public function testCreateContributionPendingOnline() {
904 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
905 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
906 $this->assertAPISuccess($contributionPage);
908 'contact_id' => $this->_individualId
,
909 'receive_date' => '20120511',
910 'total_amount' => 100.00,
911 'financial_type_id' => 1,
912 'contribution_page_id' => $contributionPage['id'],
914 'invoice_id' => 67890,
916 'contribution_status_id' => 2,
919 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
920 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
921 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
922 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
923 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
924 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
925 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
926 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
927 $this->_checkFinancialRecords($contribution, 'pending');
931 * Test that BAO defaults work.
933 public function testCreateBAODefaults() {
934 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
935 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
936 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
937 'id' => $contribution['id'],
938 'api.contribution.delete' => 1,
940 $this->assertEquals(1, $contribution['contribution_status_id']);
941 $this->assertEquals('Check', $contribution['payment_instrument']);
945 * Function tests that line items, financial records are updated when contribution amount is changed.
947 public function testCreateUpdateContributionChangeTotal() {
948 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
949 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
951 'entity_id' => $contribution['id'],
952 'entity_table' => 'civicrm_contribution',
954 'return' => 'line_total',
956 $this->assertEquals('100.00', $lineItems);
957 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
958 // Financial trxn SUM = 100 + 5 (fee)
959 $this->assertEquals('105.00', $trxnAmount);
962 'id' => $contribution['id'],
963 'total_amount' => '125',
965 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
967 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
969 'entity_id' => $contribution['id'],
970 'entity_table' => 'civicrm_contribution',
972 'return' => 'line_total',
975 $this->assertEquals('125.00', $lineItems);
976 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
978 // Financial trxn SUM = 125 + 5 (fee).
979 $this->assertEquals('130.00', $trxnAmount);
980 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
984 * Function tests that line items, financial records are updated when pay later contribution is received.
986 public function testCreateUpdateContributionPayLater() {
987 $contribParams = array(
988 'contact_id' => $this->_individualId
,
989 'receive_date' => '2012-01-01',
990 'total_amount' => 100.00,
991 'financial_type_id' => $this->_financialTypeId
,
992 'payment_instrument_id' => 1,
993 'contribution_status_id' => 2,
997 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
999 $newParams = array_merge($contribParams, array(
1000 'id' => $contribution['id'],
1001 'contribution_status_id' => 1,
1004 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1005 $contribution = $contribution['values'][$contribution['id']];
1006 $this->assertEquals($contribution['contribution_status_id'], '1');
1007 $this->_checkFinancialItem($contribution['id'], 'paylater');
1008 $this->_checkFinancialTrxn($contribution, 'payLater');
1012 * Function tests that financial records are updated when Payment Instrument is changed.
1014 public function testCreateUpdateContributionPaymentInstrument() {
1015 $instrumentId = $this->_addPaymentInstrument();
1016 $contribParams = array(
1017 'contact_id' => $this->_individualId
,
1018 'total_amount' => 100.00,
1019 'financial_type_id' => $this->_financialTypeId
,
1020 'payment_instrument_id' => 4,
1021 'contribution_status_id' => 1,
1024 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1026 $newParams = array_merge($contribParams, array(
1027 'id' => $contribution['id'],
1028 'payment_instrument_id' => $instrumentId,
1031 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1032 $this->assertAPISuccess($contribution);
1033 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1037 * Function tests that financial records are added when Contribution is Refunded.
1039 public function testCreateUpdateContributionRefund() {
1040 $contribParams = array(
1041 'contact_id' => $this->_individualId
,
1042 'receive_date' => '2012-01-01',
1043 'total_amount' => 100.00,
1044 'financial_type_id' => $this->_financialTypeId
,
1045 'payment_instrument_id' => 4,
1046 'contribution_status_id' => 1,
1049 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1050 $newParams = array_merge($contribParams, array(
1051 'id' => $contribution['id'],
1052 'contribution_status_id' => 7,
1056 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1057 $this->_checkFinancialTrxn($contribution, 'refund');
1058 $this->_checkFinancialItem($contribution['id'], 'refund');
1062 * Function tests invalid contribution status change.
1064 public function testCreateUpdateContributionInValidStatusChange() {
1065 $contribParams = array(
1067 'receive_date' => '2012-01-01',
1068 'total_amount' => 100.00,
1069 'financial_type_id' => 1,
1070 'payment_instrument_id' => 1,
1071 'contribution_status_id' => 1,
1073 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1074 $newParams = array_merge($contribParams, array(
1075 'id' => $contribution['id'],
1076 'contribution_status_id' => 2,
1079 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1084 * Function tests that financial records are added when Pending Contribution is Canceled.
1086 public function testCreateUpdateContributionCancelPending() {
1087 $contribParams = array(
1088 'contact_id' => $this->_individualId
,
1089 'receive_date' => '2012-01-01',
1090 'total_amount' => 100.00,
1091 'financial_type_id' => $this->_financialTypeId
,
1092 'payment_instrument_id' => 1,
1093 'contribution_status_id' => 2,
1094 'is_pay_later' => 1,
1097 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1098 $newParams = array_merge($contribParams, array(
1099 'id' => $contribution['id'],
1100 'contribution_status_id' => 3,
1103 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1104 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1105 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1109 * Function tests that financial records are added when Financial Type is Changed.
1111 public function testCreateUpdateContributionChangeFinancialType() {
1112 $contribParams = array(
1113 'contact_id' => $this->_individualId
,
1114 'receive_date' => '2012-01-01',
1115 'total_amount' => 100.00,
1116 'financial_type_id' => 1,
1117 'payment_instrument_id' => 1,
1118 'contribution_status_id' => 1,
1121 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1122 $newParams = array_merge($contribParams, array(
1123 'id' => $contribution['id'],
1124 'financial_type_id' => 3,
1127 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1128 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1129 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1133 * Test that update does not change status id CRM-15105.
1135 public function testCreateUpdateWithoutChangingPendingStatus() {
1136 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1137 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1138 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1139 'id' => $contribution['id'],
1140 'api.contribution.delete' => 1,
1142 $this->assertEquals(2, $contribution['contribution_status_id']);
1146 * Test Updating a Contribution.
1148 * CHANGE: we require the API to do an incremental update
1150 public function testCreateUpdateContribution() {
1152 $contributionID = $this->contributionCreate(array(
1153 'contact_id' => $this->_individualId
,
1154 'trxn_id' => 212355,
1155 'financial_type_id' => $this->_financialTypeId
,
1156 'invoice_id' => 'old_invoice',
1158 $old_params = array(
1159 'contribution_id' => $contributionID,
1161 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1162 $this->assertEquals($original['id'], $contributionID);
1163 //set up list of old params, verify
1165 //This should not be required on update:
1166 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1167 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1168 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1169 $old_source = $original['values'][$contributionID]['contribution_source'];
1171 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1172 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1174 //check against values in CiviUnitTestCase::createContribution()
1175 $this->assertEquals($old_contact_id, $this->_individualId
);
1176 $this->assertEquals($old_fee_amount, 5.00);
1177 $this->assertEquals($old_source, 'SSF');
1178 $this->assertEquals($old_trxn_id, 212355);
1179 $this->assertEquals($old_invoice_id, 'old_invoice');
1181 'id' => $contributionID,
1182 'contact_id' => $this->_individualId
,
1183 'total_amount' => 110.00,
1184 'financial_type_id' => $this->_financialTypeId
,
1185 'non_deductible_amount' => 10.00,
1186 'net_amount' => 100.00,
1187 'contribution_status_id' => 1,
1188 'note' => 'Donating for Nobel Cause',
1192 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1194 $new_params = array(
1195 'contribution_id' => $contribution['id'],
1198 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1200 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1201 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1202 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId
);
1203 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1204 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1205 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1206 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1207 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1208 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1209 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1210 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1212 'contribution_id' => $contributionID,
1215 $result = $this->callAPISuccess('contribution', 'delete', $params);
1216 $this->assertAPISuccess($result);
1219 ///////////////// civicrm_contribution_delete methods
1222 * Attempt (but fail) to delete a contribution without parameters.
1224 public function testDeleteEmptyParamsContribution() {
1226 $this->callAPIFailure('contribution', 'delete', $params);
1229 public function testDeleteParamsNotArrayContribution() {
1230 $params = 'contribution_id= 1';
1231 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1232 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1235 public function testDeleteWrongParamContribution() {
1237 'contribution_source' => 'SSF',
1240 $this->callAPIFailure('contribution', 'delete', $params);
1243 public function testDeleteContribution() {
1244 $contributionID = $this->contributionCreate(array(
1245 'contact_id' => $this->_individualId
,
1246 'financial_type_id' => $this->_financialTypeId
,
1249 'id' => $contributionID,
1251 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1255 * Test civicrm_contribution_search with empty params.
1257 * All available contributions expected.
1259 public function testSearchEmptyParams() {
1263 'contact_id' => $this->_individualId
,
1264 'receive_date' => date('Ymd'),
1265 'total_amount' => 100.00,
1266 'financial_type_id' => $this->_financialTypeId
,
1267 'non_deductible_amount' => 10.00,
1268 'fee_amount' => 5.00,
1269 'net_amount' => 95.00,
1271 'invoice_id' => 78910,
1273 'contribution_status_id' => 1,
1276 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1278 $result = $this->callAPISuccess('contribution', 'get', $params);
1279 // We're taking the first element.
1280 $res = $result['values'][$contribution['id']];
1282 $this->assertEquals($p['contact_id'], $res['contact_id']);
1283 $this->assertEquals($p['total_amount'], $res['total_amount']);
1284 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1285 $this->assertEquals($p['net_amount'], $res['net_amount']);
1286 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1287 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1288 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1289 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1290 $this->assertEquals($p['source'], $res['contribution_source']);
1291 // contribution_status_id = 1 => Completed
1292 $this->assertEquals('Completed', $res['contribution_status']);
1294 $this->contributionDelete($contribution['id']);
1298 * Test civicrm_contribution_search. Success expected.
1300 public function testSearch() {
1302 'contact_id' => $this->_individualId
,
1303 'receive_date' => date('Ymd'),
1304 'total_amount' => 100.00,
1305 'financial_type_id' => $this->_financialTypeId
,
1306 'non_deductible_amount' => 10.00,
1307 'contribution_status_id' => 1,
1310 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1313 'contact_id' => $this->_individualId
,
1314 'receive_date' => date('Ymd'),
1315 'total_amount' => 200.00,
1316 'financial_type_id' => $this->_financialTypeId
,
1317 'non_deductible_amount' => 20.00,
1318 'trxn_id' => 5454565,
1319 'invoice_id' => 1212124,
1320 'fee_amount' => 50.00,
1321 'net_amount' => 60.00,
1322 'contribution_status_id' => 2,
1325 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1328 'contribution_id' => $contribution2['id'],
1331 $result = $this->callAPISuccess('contribution', 'get', $params);
1332 $res = $result['values'][$contribution2['id']];
1334 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1335 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1336 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1337 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1338 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1339 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1340 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1341 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1342 // contribution_status_id = 2 => Pending
1343 $this->assertEquals('Pending', $res['contribution_status']);
1345 $this->contributionDelete($contribution1['id']);
1346 $this->contributionDelete($contribution2['id']);
1350 * Test completing a transaction via the API.
1352 * Note that we are creating a logged in user because email goes out from
1355 public function testCompleteTransaction() {
1356 $mut = new CiviMailUtils($this, TRUE);
1357 $this->createLoggedInUser();
1358 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1359 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1360 $this->callAPISuccess('contribution', 'completetransaction', array(
1361 'id' => $contribution['id'],
1363 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1364 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1365 $mut->checkMailLog(array(
1366 'Receipt - Contribution',
1367 'Please print this confirmation for your records.',
1373 * Test completing a transaction via the API.
1375 * Note that we are creating a logged in user because email goes out from
1378 public function testCompleteTransactionFeeAmount() {
1379 $this->createLoggedInUser();
1380 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1381 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1382 $this->callAPISuccess('contribution', 'completetransaction', array(
1383 'id' => $contribution['id'],
1384 'fee_amount' => '.56',
1385 'trxn_id' => '7778888',
1387 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1388 $this->assertEquals('Completed', $contribution['contribution_status']);
1389 $this->assertEquals('7778888', $contribution['trxn_id']);
1390 $this->assertEquals('.56', $contribution['fee_amount']);
1391 $this->assertEquals('99.44', $contribution['net_amount']);
1395 * Test repeat contribution successfully creates line items.
1397 public function testRepeatTransaction() {
1398 $paymentProcessorID = $this->paymentProcessorCreate();
1399 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1400 'contact_id' => $this->_individualId
,
1401 'installments' => '12',
1402 'frequency_interval' => '1',
1404 'contribution_status_id' => 1,
1405 'start_date' => '2012-01-01 00:00:00',
1406 'currency' => 'USD',
1407 'frequency_unit' => 'month',
1408 'payment_processor_id' => $paymentProcessorID,
1410 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1412 array('contribution_recur_id' => $contributionRecur['id']))
1415 $this->callAPISuccess('contribution', 'repeattransaction', array(
1416 'original_contribution_id' => $originalContribution['id'],
1417 'contribution_status_id' => 'Completed',
1418 'trxn_id' => uniqid(),
1420 $lineItemParams = array(
1421 'entity_id' => $originalContribution['id'],
1429 'financial_type_id',
1430 'deductible_amount',
1431 'price_field_value_id',
1435 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1436 'entity_id' => $originalContribution['id'],
1438 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1439 'entity_id' => $originalContribution['id'] +
1,
1441 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1442 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1443 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1445 $this->quickCleanUpFinancialEntities();
1449 * CRM-16397 test appropriate action if total amount has changed for single line items.
1451 public function testRepeatTransactionAlteredAmount() {
1452 $paymentProcessorID = $this->paymentProcessorCreate();
1453 $campaignID = $this->campaignCreate();
1454 $campaignID2 = $this->campaignCreate();
1455 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1456 'contact_id' => $this->_individualId
,
1457 'installments' => '12',
1458 'frequency_interval' => '1',
1460 'contribution_status_id' => 1,
1461 'start_date' => '2012-01-01 00:00:00',
1462 'currency' => 'USD',
1463 'frequency_unit' => 'month',
1464 'payment_processor_id' => $paymentProcessorID,
1466 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1469 'contribution_recur_id' => $contributionRecur['id'],
1470 'campaign_id' => $campaignID,
1474 $this->callAPISuccess('contribution', 'repeattransaction', array(
1475 'original_contribution_id' => $originalContribution['id'],
1476 'contribution_status_id' => 'Completed',
1477 'trxn_id' => uniqid(),
1478 'total_amount' => '400',
1480 'campaign_id' => $campaignID2,
1482 $lineItemParams = array(
1483 'entity_id' => $originalContribution['id'],
1491 'financial_type_id',
1492 'deductible_amount',
1493 'price_field_value_id',
1497 $this->callAPISuccessGetSingle('contribution', array(
1498 'total_amount' => 400,
1499 'campaign_id' => $campaignID2,
1501 'net_amount' => 350,
1503 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1504 'entity_id' => $originalContribution['id'],
1506 $expectedLineItem = array_merge(
1507 $lineItem1['values'][0], array(
1508 'line_total' => '400.00',
1509 'unit_price' => '400.00',
1513 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1514 'entity_id' => $originalContribution['id'] +
1,
1516 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1517 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1518 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1520 $this->quickCleanUpFinancialEntities();
1524 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
1526 public function testCompleteTransactionNetAmountOK() {
1527 $this->createLoggedInUser();
1528 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1529 unset($params['net_amount']);
1530 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1531 $this->callAPISuccess('contribution', 'completetransaction', array(
1532 'id' => $contribution['id'],
1534 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1535 $this->assertEquals('Completed', $contribution['contribution_status']);
1536 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
1540 * CRM-14151 - Test completing a transaction via the API.
1542 public function testCompleteTransactionWithReceiptDateSet() {
1543 $mut = new CiviMailUtils($this, TRUE);
1544 $this->createLoggedInUser();
1545 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1546 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1547 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
1548 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1549 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1550 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
1551 $mut->checkMailLog(array(
1552 'Receipt - Contribution',
1553 'Please print this confirmation for your records.',
1559 * Test completing a transaction with an event via the API.
1561 * Note that we are creating a logged in user because email goes out from
1564 public function testCompleteTransactionWithParticipantRecord() {
1565 $mut = new CiviMailUtils($this, TRUE);
1566 $mut->clearMessages();
1567 $this->createLoggedInUser();
1568 $contributionID = $this->createPendingParticipantContribution();
1569 $this->callAPISuccess('contribution', 'completetransaction', array(
1570 'id' => $contributionID,
1573 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1574 'id' => $this->_ids
['participant'],
1575 'return' => 'participant_status_id',
1577 $this->assertEquals(1, $participantStatus);
1578 $mut->checkMailLog(array(
1579 'Annual CiviCRM meet',
1581 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1587 * Test membership is renewed when transaction completed.
1589 public function testCompleteTransactionMembershipPriceSet() {
1590 $this->createPriceSetWithPage('membership');
1591 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
1595 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1596 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1597 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
1598 'membership_id' => $this->_ids
['membership'],
1600 $this->assertEquals(1, $logs['count']);
1601 $this->assertEquals($stateOfGrace, $membership['status_id']);
1602 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1603 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1604 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1605 $this->callAPISuccessGetSingle('LineItem', array(
1606 'entity_id' => $this->_ids
['membership'],
1607 'entity_table' => 'civicrm_membership',
1609 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
1610 'membership_id' => $this->_ids
['membership'],
1612 $this->assertEquals(2, $logs['count']);
1613 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
1614 $this->cleanUpAfterPriceSets();
1618 * Test membership is renewed when transaction completed.
1620 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1621 $this->createPriceSetWithPage('membership');
1622 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1623 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1624 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1625 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1626 $this->cleanUpAfterPriceSets();
1629 public function cleanUpAfterPriceSets() {
1630 $this->quickCleanUpFinancialEntities();
1631 $this->contactDelete($this->_ids
['contact']);
1636 * Create price set with contribution test for test setup.
1638 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1639 * on parent class at some point (fn is not in 4.4).
1642 * @param array $params
1644 public function createPriceSetWithPage($entity, $params = array()) {
1645 $membershipTypeID = $this->membershipTypeCreate();
1646 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1647 'title' => "Test Contribution Page",
1648 'financial_type_id' => 1,
1649 'currency' => 'NZD',
1650 'goal_amount' => 50,
1651 'is_pay_later' => 1,
1652 'is_monetary' => TRUE,
1653 'is_email_receipt' => FALSE,
1655 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1656 'is_quick_config' => 0,
1657 'extends' => 'CiviMember',
1658 'financial_type_id' => 1,
1659 'title' => 'my Page',
1661 $priceSetID = $priceSet['id'];
1663 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1664 $priceField = $this->callAPISuccess('price_field', 'create', array(
1665 'price_set_id' => $priceSetID,
1666 'label' => 'Goat Breed',
1667 'html_type' => 'Radio',
1669 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1670 'price_set_id' => $priceSetID,
1671 'price_field_id' => $priceField['id'],
1672 'label' => 'Long Haired Goat',
1674 'financial_type_id' => 'Donation',
1675 'membership_type_id' => $membershipTypeID,
1676 'membership_num_terms' => 1,
1679 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1680 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1681 'price_set_id' => $priceSetID,
1682 'price_field_id' => $priceField['id'],
1683 'label' => 'Shoe-eating Goat',
1685 'financial_type_id' => 'Donation',
1686 'membership_type_id' => $membershipTypeID,
1687 'membership_num_terms' => 2,
1690 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1691 $this->_ids
['price_set'] = $priceSetID;
1692 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1693 $this->_ids
['price_field'] = array($priceField['id']);
1695 $this->_ids
['membership_type'] = $membershipTypeID;
1699 * Set up a pending transaction with a specific price field id.
1701 * @param int $priceFieldValueID
1703 public function setUpPendingContribution($priceFieldValueID) {
1704 $contactID = $this->individualCreate();
1705 $membership = $this->callAPISuccess('membership', 'create', array(
1706 'contact_id' => $contactID,
1707 'membership_type_id' => $this->_ids
['membership_type'],
1708 'start_date' => 'yesterday - 1 year',
1709 'end_date' => 'yesterday',
1710 'join_date' => 'yesterday - 1 year',
1712 $contribution = $this->callAPISuccess('contribution', 'create', array(
1714 'contact_id' => $contactID,
1715 'receive_date' => date('Ymd'),
1716 'total_amount' => 100.00,
1717 'financial_type_id' => 1,
1718 'payment_instrument_id' => 'Credit Card',
1719 'non_deductible_amount' => 10.00,
1720 'trxn_id' => 'jdhfi88',
1721 'invoice_id' => 'djfhiewuyr',
1723 'contribution_status_id' => 2,
1724 'contribution_page_id' => $this->_ids
['contribution_page'],
1725 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1728 $this->callAPISuccess('line_item', 'create', array(
1729 'entity_id' => $contribution['id'],
1730 'entity_table' => 'civicrm_contribution',
1731 'contribution_id' => $contribution['id'],
1732 'price_field_id' => $this->_ids
['price_field'][0],
1736 'financial_type_id' => 1,
1737 'price_field_value_id' => $priceFieldValueID,
1739 $this->_ids
['contact'] = $contactID;
1740 $this->_ids
['contribution'] = $contribution['id'];
1741 $this->_ids
['membership'] = $membership['id'];
1745 * Test sending a mail via the API.
1747 public function testSendMail() {
1748 $mut = new CiviMailUtils($this, TRUE);
1749 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1750 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1751 'id' => $contribution['id'],
1752 'receipt_from_email' => 'api@civicrm.org',
1755 $mut->checkMailLog(array(
1757 'Contribution Information',
1758 'Please print this confirmation for your records',
1767 * Test sending a mail via the API.
1769 public function testSendMailEvent() {
1770 $mut = new CiviMailUtils($this, TRUE);
1771 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1772 $event = $this->eventCreate(array(
1773 'is_email_confirm' => 1,
1774 'confirm_from_email' => 'test@civicrm.org',
1776 $this->_eventID
= $event['id'];
1777 $participantParams = array(
1778 'contact_id' => $this->_individualId
,
1779 'event_id' => $this->_eventID
,
1782 // to ensure it matches later on
1783 'register_date' => '2007-07-21 00:00:00',
1784 'source' => 'Online Event Registration: API Testing',
1787 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1788 $this->callAPISuccess('participant_payment', 'create', array(
1789 'participant_id' => $participant['id'],
1790 'contribution_id' => $contribution['id'],
1792 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1793 'id' => $contribution['id'],
1794 'receipt_from_email' => 'api@civicrm.org',
1798 $mut->checkMailLog(array(
1799 'Annual CiviCRM meet',
1801 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1808 * This function does a GET & compares the result against the $params.
1810 * Use as a double check on Creates.
1812 * @param array $params
1814 * @param bool $delete
1816 public function contributionGetnCheck($params, $id, $delete = TRUE) {
1818 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1824 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1826 $this->assertAPISuccess($contribution, 0);
1827 $values = $contribution['values'][$contribution['id']];
1828 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1829 // this is not returned in id format
1830 unset($params['payment_instrument_id']);
1831 $params['contribution_source'] = $params['source'];
1832 unset($params['source']);
1833 foreach ($params as $key => $value) {
1834 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
1839 * Create a pending contribution & linked pending participant record (along with an event).
1841 public function createPendingParticipantContribution() {
1842 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1843 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1844 $this->_ids
['participant'] = $participantID;
1845 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1846 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1847 $this->callAPISuccess('participant_payment', 'create', array(
1848 'contribution_id' => $contribution['id'],
1849 'participant_id' => $participantID,
1851 $this->callAPISuccess('line_item', 'get', array(
1852 'entity_id' => $contribution['id'],
1853 'entity_table' => 'civicrm_contribution',
1854 'api.line_item.create' => array(
1855 'entity_id' => $participantID,
1856 'entity_table' => 'civicrm_participant',
1859 return $contribution['id'];
1863 * Get financial transaction amount.
1865 * @param int $contId
1867 * @return null|string
1869 public function _getFinancialTrxnAmount($contId) {
1871 SUM( ft.total_amount ) AS total
1872 FROM civicrm_financial_trxn AS ft
1873 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1874 WHERE ceft.entity_table = 'civicrm_contribution'
1875 AND ceft.entity_id = {$contId}";
1877 $result = CRM_Core_DAO
::singleValueQuery($query);
1882 * @param int $contId
1884 * @return null|string
1886 public function _getFinancialItemAmount($contId) {
1887 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1890 FROM civicrm_financial_item
1891 WHERE entity_table = 'civicrm_line_item'
1892 AND entity_id = {$lineItem}";
1893 $result = CRM_Core_DAO
::singleValueQuery($query);
1898 * @param int $contId
1901 public function _checkFinancialItem($contId, $context) {
1902 if ($context != 'paylater') {
1904 'entity_id' => $contId,
1905 'entity_table' => 'civicrm_contribution',
1907 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1908 $entityParams = array(
1909 'financial_trxn_id' => $trxn['financial_trxn_id'],
1910 'entity_table' => 'civicrm_financial_item',
1912 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1914 'id' => $entityTrxn['entity_id'],
1917 if ($context == 'paylater') {
1918 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1919 foreach ($lineItems as $key => $item) {
1921 'entity_id' => $key,
1922 'entity_table' => 'civicrm_line_item',
1924 $compareParams = array('status_id' => 1);
1925 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1928 elseif ($context == 'refund') {
1929 $compareParams = array(
1931 'financial_account_id' => 1,
1935 elseif ($context == 'cancelPending') {
1936 $compareParams = array(
1938 'financial_account_id' => 1,
1942 elseif ($context == 'changeFinancial') {
1943 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1945 'entity_id' => $lineKey,
1948 $compareParams = array(
1949 'financial_account_id' => 1,
1951 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1953 'financial_account_id' => 3,
1954 'entity_id' => $lineKey,
1956 $compareParams = array(
1960 if ($context != 'paylater') {
1961 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1966 * @param array $contribution
1967 * @param string $context
1968 * @param int $instrumentId
1970 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1971 $trxnParams = array(
1972 'entity_id' => $contribution['id'],
1973 'entity_table' => 'civicrm_contribution',
1975 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1977 'id' => $trxn['financial_trxn_id'],
1979 if ($context == 'payLater') {
1980 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1981 $compareParams = array(
1983 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1986 elseif ($context == 'refund') {
1987 $compareParams = array(
1988 'to_financial_account_id' => 6,
1989 'total_amount' => -100,
1993 elseif ($context == 'cancelPending') {
1994 $compareParams = array(
1995 'to_financial_account_id' => 7,
1996 'total_amount' => -100,
2000 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
2001 $entityParams = array(
2002 'entity_id' => $contribution['id'],
2003 'entity_table' => 'civicrm_contribution',
2006 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2007 $trxnParams1 = array(
2008 'id' => $trxn['financial_trxn_id'],
2010 $compareParams = array(
2011 'total_amount' => -100,
2014 if ($context == 'paymentInstrument') {
2015 $compareParams +
= array(
2016 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
2017 'payment_instrument_id' => 4,
2021 $compareParams['to_financial_account_id'] = 12;
2023 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
2024 $compareParams['total_amount'] = 100;
2025 if ($context == 'paymentInstrument') {
2026 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
2027 $compareParams['payment_instrument_id'] = $instrumentId;
2030 $compareParams['to_financial_account_id'] = 12;
2034 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
2040 public function _addPaymentInstrument() {
2041 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2042 $optionParams = array(
2043 'option_group_id' => $gId,
2044 'label' => 'Test Card',
2045 'name' => 'Test Card',
2050 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2051 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2052 $financialParams = array(
2053 'entity_table' => 'civicrm_option_value',
2054 'entity_id' => $optionValue['id'],
2055 'account_relationship' => $relationTypeId,
2056 'financial_account_id' => 7,
2058 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
2059 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2060 return $optionValue['values'][$optionValue['id']]['value'];
2064 * @param array $params
2067 public function _checkFinancialRecords($params, $context) {
2068 $entityParams = array(
2069 'entity_id' => $params['id'],
2070 'entity_table' => 'civicrm_contribution',
2072 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2073 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
2074 if ($context == 'pending') {
2075 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
2076 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2079 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2080 $trxnParams = array(
2081 'id' => $trxn['financial_trxn_id'],
2083 if ($context != 'online' && $context != 'payLater') {
2084 $compareParams = array(
2085 'to_financial_account_id' => 6,
2086 'total_amount' => 100,
2090 if ($context == 'feeAmount') {
2091 $compareParams['fee_amount'] = 50;
2093 elseif ($context == 'online') {
2094 $compareParams = array(
2095 'to_financial_account_id' => 12,
2096 'total_amount' => 100,
2100 elseif ($context == 'payLater') {
2101 $compareParams = array(
2102 'to_financial_account_id' => 7,
2103 'total_amount' => 100,
2107 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2108 $entityParams = array(
2109 'financial_trxn_id' => $trxn['financial_trxn_id'],
2110 'entity_table' => 'civicrm_financial_item',
2112 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2113 $fitemParams = array(
2114 'id' => $entityTrxn['entity_id'],
2116 $compareParams = array(
2119 'financial_account_id' => 1,
2121 if ($context == 'payLater') {
2122 $compareParams = array(
2125 'financial_account_id' => 1,
2128 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2129 if ($context == 'feeAmount') {
2131 'entity_id' => $params['id'],
2132 'entity_table' => 'civicrm_contribution',
2134 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
2135 $trxnParams = array(
2136 'id' => $maxTrxn['financial_trxn_id'],
2138 $compareParams = array(
2139 'to_financial_account_id' => 5,
2140 'from_financial_account_id' => 6,
2141 'total_amount' => 50,
2144 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
2145 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2146 $fitemParams = array(
2147 'entity_id' => $trxnId['financialTrxnId'],
2148 'entity_table' => 'civicrm_financial_trxn',
2150 $compareParams = array(
2153 'financial_account_id' => 5,
2155 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);