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();
53 * Payment processor ID (dummy processor).
57 protected $paymentProcessorID;
60 * Parameters to create payment processor.
64 protected $_processorParams = array();
67 * ID of created event.
76 public function setUp() {
79 $this->_apiversion
= 3;
80 $this->_individualId
= $this->individualCreate();
81 $this->_params
= array(
82 'contact_id' => $this->_individualId
,
83 'receive_date' => '20120511',
84 'total_amount' => 100.00,
85 'financial_type_id' => $this->_financialTypeId
,
86 'non_deductible_amount' => 10.00,
88 'net_amount' => 95.00,
90 'contribution_status_id' => 1,
92 $this->_processorParams
= array(
95 'payment_processor_type_id' => 10,
96 'financial_account_id' => 12,
99 'url_site' => 'http://dummy.com',
100 'url_recur' => 'http://dummy.com',
103 $this->paymentProcessorID
= $this->processorCreate();
104 $this->_pageParams
= array(
105 'title' => 'Test Contribution Page',
106 'financial_type_id' => 1,
108 'financial_account_id' => 1,
109 'payment_processor' => $this->paymentProcessorID
,
111 'is_allow_other_amount' => 1,
113 'max_amount' => 1000,
118 * Clean up after each test.
120 public function tearDown() {
121 $this->quickCleanUpFinancialEntities();
122 $this->quickCleanup(array('civicrm_uf_match'));
128 public function testGetContribution() {
130 'contact_id' => $this->_individualId
,
131 'receive_date' => '2010-01-20',
132 'total_amount' => 100.00,
133 'financial_type_id' => $this->_financialTypeId
,
134 'non_deductible_amount' => 10.00,
135 'fee_amount' => 5.00,
136 'net_amount' => 95.00,
138 'invoice_id' => 78910,
140 'contribution_status_id' => 1,
142 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
145 'contribution_id' => $this->_contribution
['id'],
148 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
149 $financialParams['id'] = $this->_financialTypeId
;
151 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
153 $this->assertEquals(1, $contribution['count']);
154 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
155 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
156 // Passing back a string rather than an id seems like an error/cruft.
157 // If it is to be introduced we should discuss.
158 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
159 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
160 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
161 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
162 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
163 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
164 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
165 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
166 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
167 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
168 $p['trxn_id'] = '3847';
169 $p['invoice_id'] = '3847';
171 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
173 // Now we have 2 - test getcount.
174 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
175 $this->assertEquals(2, $contribution);
176 // Test id only format.
177 $contribution = $this->callAPISuccess('contribution', 'get', array(
178 'id' => $this->_contribution
['id'],
179 'format.only_id' => 1,
181 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
182 // Test id only format.
183 $contribution = $this->callAPISuccess('contribution', 'get', array(
184 'id' => $contribution2['id'],
185 'format.only_id' => 1,
187 $this->assertEquals($contribution2['id'], $contribution);
189 $contribution = $this->callAPISuccess('contribution', 'get', array(
190 'id' => $this->_contribution
['id'],
192 $this->assertEquals(1, $contribution['count']);
194 // Test get by contact id works.
195 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
197 $this->assertEquals(2, $contribution['count']);
198 $this->callAPISuccess('Contribution', 'Delete', array(
199 'id' => $this->_contribution
['id'],
201 $this->callAPISuccess('Contribution', 'Delete', array(
202 'id' => $contribution2['id'],
207 * Test that test contributions can be retrieved.
209 public function testGetTestContribution() {
210 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('is_test' => 1)));
211 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
215 * We need to ensure previous tested behaviour still works as part of the api contract.
217 public function testGetContributionLegacyBehaviour() {
219 'contact_id' => $this->_individualId
,
220 'receive_date' => '2010-01-20',
221 'total_amount' => 100.00,
222 'contribution_type_id' => $this->_financialTypeId
,
223 'non_deductible_amount' => 10.00,
224 'fee_amount' => 5.00,
225 'net_amount' => 95.00,
227 'invoice_id' => 78910,
229 'contribution_status_id' => 1,
231 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
234 'contribution_id' => $this->_contribution
['id'],
236 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
237 $financialParams['id'] = $this->_financialTypeId
;
239 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
241 $this->assertEquals(1, $contribution['count']);
242 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
243 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
244 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
245 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
246 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
247 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
248 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
249 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
250 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
251 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
252 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
254 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
255 $p['trxn_id'] = '3847';
256 $p['invoice_id'] = '3847';
258 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
260 // now we have 2 - test getcount
261 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
262 $this->assertEquals(2, $contribution);
263 //test id only format
264 $contribution = $this->callAPISuccess('contribution', 'get', array(
265 'id' => $this->_contribution
['id'],
266 'format.only_id' => 1,
268 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
269 //test id only format
270 $contribution = $this->callAPISuccess('contribution', 'get', array(
271 'id' => $contribution2['id'],
272 'format.only_id' => 1,
274 $this->assertEquals($contribution2['id'], $contribution);
275 $contribution = $this->callAPISuccess('contribution', 'get', array(
276 'id' => $this->_contribution
['id'],
279 $this->assertEquals(1, $contribution['count']);
280 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
281 //test get by contact id works
282 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
284 $this->assertEquals(2, $contribution['count']);
285 $this->callAPISuccess('Contribution', 'Delete', array(
286 'id' => $this->_contribution
['id'],
288 $this->callAPISuccess('Contribution', 'Delete', array(
289 'id' => $contribution2['id'],
294 * Create an contribution_id=FALSE and financial_type_id=Donation.
296 public function testCreateEmptyContributionIDUseDonation() {
298 'contribution_id' => FALSE,
301 'check_permissions' => FALSE,
302 'financial_type_id' => 'Donation',
304 $this->callAPISuccess('contribution', 'create', $params);
308 * Check with complete array + custom field.
310 * Note that the test is written on purpose without any
311 * variables specific to participant so it can be replicated into other entities
312 * and / or moved to the automated test suite
314 public function testCreateWithCustom() {
315 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
317 $params = $this->_params
;
318 $params['custom_' . $ids['custom_field_id']] = "custom string";
320 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
321 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
322 $check = $this->callAPISuccess($this->_entity
, 'get', array(
323 'return.custom_' . $ids['custom_field_id'] => 1,
324 'id' => $result['id'],
326 $this->customFieldDelete($ids['custom_field_id']);
327 $this->customGroupDelete($ids['custom_group_id']);
328 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
332 * Check with complete array + custom field.
334 * Note that the test is written on purpose without any
335 * variables specific to participant so it can be replicated into other entities
336 * and / or moved to the automated test suite
338 public function testCreateGetFieldsWithCustom() {
339 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
340 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
341 $result = $this->callAPISuccess('Contribution', 'getfields', array());
342 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
343 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
344 $this->customFieldDelete($ids['custom_field_id']);
345 $this->customGroupDelete($ids['custom_group_id']);
346 $this->customFieldDelete($idsContact['custom_field_id']);
347 $this->customGroupDelete($idsContact['custom_group_id']);
350 public function testCreateContributionNoLineItems() {
353 'contact_id' => $this->_individualId
,
354 'receive_date' => '20120511',
355 'total_amount' => 100.00,
356 'financial_type_id' => $this->_financialTypeId
,
357 'payment_instrument_id' => 1,
358 'non_deductible_amount' => 10.00,
359 'fee_amount' => 50.00,
360 'net_amount' => 90.00,
362 'invoice_id' => 67890,
364 'contribution_status_id' => 1,
368 $contribution = $this->callAPISuccess('contribution', 'create', $params);
369 $lineItems = $this->callAPISuccess('line_item', 'get', array(
370 'entity_id' => $contribution['id'],
371 'entity_table' => 'civicrm_contribution',
374 $this->assertEquals(0, $lineItems['count']);
378 * Test checks that passing in line items suppresses the create mechanism.
380 public function testCreateContributionChainedLineItems() {
382 'contact_id' => $this->_individualId
,
383 'receive_date' => '20120511',
384 'total_amount' => 100.00,
385 'financial_type_id' => $this->_financialTypeId
,
386 'payment_instrument_id' => 1,
387 'non_deductible_amount' => 10.00,
388 'fee_amount' => 50.00,
389 'net_amount' => 90.00,
391 'invoice_id' => 67890,
393 'contribution_status_id' => 1,
395 'api.line_item.create' => array(
397 'price_field_id' => 1,
399 'line_total' => '20',
400 'unit_price' => '10',
403 'price_field_id' => 1,
405 'line_total' => '80',
406 'unit_price' => '80',
411 $description = "Create Contribution with Nested Line Items.";
412 $subfile = "CreateWithNestedLineItems";
413 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
415 $lineItems = $this->callAPISuccess('line_item', 'get', array(
416 'entity_id' => $contribution['id'],
417 'contribution_id' => $contribution['id'],
418 'entity_table' => 'civicrm_contribution',
421 $this->assertEquals(2, $lineItems['count']);
424 public function testCreateContributionOffline() {
426 'contact_id' => $this->_individualId
,
427 'receive_date' => '20120511',
428 'total_amount' => 100.00,
429 'financial_type_id' => 1,
431 'invoice_id' => 67890,
433 'contribution_status_id' => 1,
436 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
437 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
438 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
439 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
440 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
441 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
442 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
443 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
444 $lineItems = $this->callAPISuccess('line_item', 'get', array(
445 'entity_id' => $contribution['id'],
446 'contribution_id' => $contribution['id'],
447 'entity_table' => 'civicrm_contribution',
450 $this->assertEquals(1, $lineItems['count']);
451 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
452 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
453 $this->_checkFinancialRecords($contribution, 'offline');
454 $this->contributionGetnCheck($params, $contribution['id']);
458 * Test create with valid payment instrument.
460 public function testCreateContributionWithPaymentInstrument() {
461 $params = $this->_params +
array('payment_instrument' => 'EFT');
462 $contribution = $this->callAPISuccess('contribution', 'create', $params);
463 $contribution = $this->callAPISuccess('contribution', 'get', array(
465 'id' => $contribution['id'],
467 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
468 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
470 $this->callAPISuccess('contribution', 'create', array(
471 'id' => $contribution['id'],
472 'payment_instrument' => 'Credit Card',
474 $contribution = $this->callAPISuccess('contribution', 'get', array(
476 'id' => $contribution['id'],
478 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
479 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
482 public function testGetContributionByPaymentInstrument() {
483 $params = $this->_params +
array('payment_instrument' => 'EFT');
484 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
485 $this->callAPISuccess('contribution', 'create', $params);
486 $this->callAPISuccess('contribution', 'create', $params2);
487 $contribution = $this->callAPISuccess('contribution', 'get', array(
489 'contribution_payment_instrument' => 'Cash',
491 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
492 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
493 $this->assertEquals(1, $contribution['count']);
494 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
495 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
496 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
497 $this->assertEquals(1, $contribution['count']);
498 $contribution = $this->callAPISuccess('contribution', 'get', array(
500 'payment_instrument_id' => 5,
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', 'get', array(
507 'payment_instrument' => 'EFT',
509 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
510 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
511 $this->assertEquals(1, $contribution['count']);
512 $contribution = $this->callAPISuccess('contribution', 'create', array(
513 'id' => $contribution['id'],
514 'payment_instrument' => 'Credit Card',
516 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
517 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
518 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
519 $this->assertEquals(1, $contribution['count']);
523 * CRM-16227 introduces invoice_id as a parameter.
525 public function testGetContributionByInvoice() {
526 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('invoice_id' => 'curly')));
527 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), array('invoice_id' => 'churlish'));
528 $this->callAPISuccessGetCount('Contribution', array(), 2);
529 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
530 // The following don't work. They are the format we are trying to introduce but although the form uses this format
531 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
532 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
533 // come out of convertFormValues
534 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
535 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
536 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
537 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
542 * Create test with unique field name on source.
544 public function testCreateContributionSource() {
547 'contact_id' => $this->_individualId
,
548 'receive_date' => date('Ymd'),
549 'total_amount' => 100.00,
550 'financial_type_id' => $this->_financialTypeId
,
551 'payment_instrument_id' => 1,
552 'non_deductible_amount' => 10.00,
553 'fee_amount' => 50.00,
554 'net_amount' => 90.00,
556 'invoice_id' => 67890,
557 'contribution_source' => 'SSF',
558 'contribution_status_id' => 1,
561 $contribution = $this->callAPISuccess('contribution', 'create', $params);
562 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
563 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
567 * Create test with unique field name on source.
569 public function testCreateDefaultNow() {
571 $params = $this->_params
;
572 unset($params['receive_date']);
574 $contribution = $this->callAPISuccess('contribution', 'create', $params);
575 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
576 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
580 * Create test with unique field name on source.
582 public function testCreateContributionSourceInvalidContact() {
586 'receive_date' => date('Ymd'),
587 'total_amount' => 100.00,
588 'financial_type_id' => $this->_financialTypeId
,
589 'payment_instrument_id' => 1,
590 'non_deductible_amount' => 10.00,
591 'fee_amount' => 50.00,
592 'net_amount' => 90.00,
594 'invoice_id' => 67890,
595 'contribution_source' => 'SSF',
596 'contribution_status_id' => 1,
599 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
602 public function testCreateContributionSourceInvalidContContact() {
605 'contribution_contact_id' => 999,
606 'receive_date' => date('Ymd'),
607 'total_amount' => 100.00,
608 'financial_type_id' => $this->_financialTypeId
,
609 'payment_instrument_id' => 1,
610 'non_deductible_amount' => 10.00,
611 'fee_amount' => 50.00,
612 'net_amount' => 90.00,
614 'invoice_id' => 67890,
615 'contribution_source' => 'SSF',
616 'contribution_status_id' => 1,
619 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
623 * Test note created correctly.
625 public function testCreateContributionWithNote() {
626 $description = "Demonstrates creating contribution with Note Entity.";
627 $subfile = "ContributionCreateWithNote";
629 'contact_id' => $this->_individualId
,
630 'receive_date' => '2012-01-01',
631 'total_amount' => 100.00,
632 'financial_type_id' => $this->_financialTypeId
,
633 'payment_instrument_id' => 1,
634 'non_deductible_amount' => 10.00,
635 'fee_amount' => 50.00,
636 'net_amount' => 90.00,
638 'invoice_id' => 67890,
640 'contribution_status_id' => 1,
641 'note' => 'my contribution note',
644 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
645 $result = $this->callAPISuccess('note', 'get', array(
646 'entity_table' => 'civicrm_contribution',
647 'entity_id' => $contribution['id'],
650 $this->assertEquals('my contribution note', $result['values'][0]['note']);
651 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
654 public function testCreateContributionWithNoteUniqueNameAliases() {
656 'contact_id' => $this->_individualId
,
657 'receive_date' => '2012-01-01',
658 'total_amount' => 100.00,
659 'financial_type_id' => $this->_financialTypeId
,
660 'payment_instrument_id' => 1,
661 'non_deductible_amount' => 10.00,
662 'fee_amount' => 50.00,
663 'net_amount' => 90.00,
665 'invoice_id' => 67890,
667 'contribution_status_id' => 1,
668 'contribution_note' => 'my contribution note',
671 $contribution = $this->callAPISuccess('contribution', 'create', $params);
672 $result = $this->callAPISuccess('note', 'get', array(
673 'entity_table' => 'civicrm_contribution',
674 'entity_id' => $contribution['id'],
677 $this->assertEquals('my contribution note', $result['values'][0]['note']);
678 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
682 * This is the test for creating soft credits.
684 public function testCreateContributionWithSoftCredit() {
685 $description = "Demonstrates creating contribution with SoftCredit.";
686 $subfile = "ContributionCreateWithSoftCredit";
687 $contact2 = $this->callAPISuccess('Contact', 'create', array(
688 'display_name' => 'superman',
689 'contact_type' => 'Individual',
692 'contact_id' => $contact2['id'],
694 'soft_credit_type_id' => 3,
697 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
698 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
699 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
701 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
702 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
703 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
705 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
706 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
709 public function testCreateContributionWithSoftCreditDefaults() {
710 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
711 $subfile = "ContributionCreateWithSoftCreditDefaults";
712 $contact2 = $this->callAPISuccess('Contact', 'create', array(
713 'display_name' => 'superman',
714 'contact_type' => 'Individual',
716 $params = $this->_params +
array(
717 'soft_credit_to' => $contact2['id'],
719 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
720 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
722 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
723 // Default soft credit amount = contribution.total_amount
724 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
725 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
727 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
728 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
731 public function testCreateContributionWithHonoreeContact() {
732 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
733 $subfile = "ContributionCreateWithHonoreeContact";
734 $contact2 = $this->callAPISuccess('Contact', 'create', array(
735 'display_name' => 'superman',
736 'contact_type' => 'Individual',
738 $params = $this->_params +
array(
739 'honor_contact_id' => $contact2['id'],
741 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
742 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
744 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
745 // Default soft credit amount = contribution.total_amount
746 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
747 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
748 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
750 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
751 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
755 * Test using example code.
757 public function testContributionCreateExample() {
758 //make sure at least on page exists since there is a truncate in tear down
759 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
760 require_once 'api/v3/examples/Contribution/Create.php';
761 $result = contribution_create_example();
763 $expectedResult = contribution_create_expectedresult();
764 $this->checkArrayEquals($expectedResult, $result);
765 $this->contributionDelete($id);
769 * Function tests that additional financial records are created when fee amount is recorded.
771 public function testCreateContributionWithFee() {
773 'contact_id' => $this->_individualId
,
774 'receive_date' => '20120511',
775 'total_amount' => 100.00,
777 'financial_type_id' => 1,
779 'invoice_id' => 67890,
781 'contribution_status_id' => 1,
784 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
785 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
786 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
787 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
788 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
789 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
790 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
791 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
792 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
793 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
795 $lineItems = $this->callAPISuccess('line_item', 'get', array(
797 'entity_id' => $contribution['id'],
798 'entity_table' => 'civicrm_contribution',
801 $this->assertEquals(1, $lineItems['count']);
802 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
803 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
804 $lineItems = $this->callAPISuccess('line_item', 'get', array(
806 'entity_id' => $contribution['id'],
807 'contribution_id' => $contribution['id'],
808 'entity_table' => 'civicrm_contribution',
811 $this->assertEquals(1, $lineItems['count']);
812 $this->_checkFinancialRecords($contribution, 'feeAmount');
817 * Function tests that additional financial records are created when online contribution is created.
819 public function testCreateContributionOnline() {
820 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
821 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
822 $this->assertAPISuccess($contributionPage);
824 'contact_id' => $this->_individualId
,
825 'receive_date' => '20120511',
826 'total_amount' => 100.00,
827 'financial_type_id' => 1,
828 'contribution_page_id' => $contributionPage['id'],
829 'payment_processor' => 1,
831 'invoice_id' => 67890,
833 'contribution_status_id' => 1,
837 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
838 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
839 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
840 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
841 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
842 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
843 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
844 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
845 $this->_checkFinancialRecords($contribution, 'online');
849 * Check handling of financial type.
851 * In the interests of removing financial type / contribution type checks from
852 * legacy format function lets test that the api is doing this for us
854 public function testCreateInvalidFinancialType() {
855 $params = $this->_params
;
856 $params['financial_type_id'] = 99999;
857 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
861 * Check handling of financial type.
863 * In the interests of removing financial type / contribution type checks from
864 * legacy format function lets test that the api is doing this for us
866 public function testValidNamedFinancialType() {
867 $params = $this->_params
;
868 $params['financial_type_id'] = 'Donation';
869 $this->callAPISuccess($this->_entity
, 'create', $params);
873 * Tests that additional financial records are created.
875 * Checks when online contribution with pay later option is created
877 public function testCreateContributionPayLaterOnline() {
878 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
879 $this->_pageParams
['is_pay_later'] = 1;
880 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
881 $this->assertAPISuccess($contributionPage);
883 'contact_id' => $this->_individualId
,
884 'receive_date' => '20120511',
885 'total_amount' => 100.00,
886 'financial_type_id' => 1,
887 'contribution_page_id' => $contributionPage['id'],
890 'invoice_id' => 67890,
892 'contribution_status_id' => 2,
896 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
897 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
898 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
899 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
900 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
901 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
902 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
903 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
904 $this->_checkFinancialRecords($contribution, 'payLater');
908 * Function tests that additional financial records are created for online contribution with pending option.
910 public function testCreateContributionPendingOnline() {
911 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
912 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
913 $this->assertAPISuccess($contributionPage);
915 'contact_id' => $this->_individualId
,
916 'receive_date' => '20120511',
917 'total_amount' => 100.00,
918 'financial_type_id' => 1,
919 'contribution_page_id' => $contributionPage['id'],
921 'invoice_id' => 67890,
923 'contribution_status_id' => 2,
926 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
927 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
928 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
929 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
930 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
931 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
932 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
933 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
934 $this->_checkFinancialRecords($contribution, 'pending');
938 * Test that BAO defaults work.
940 public function testCreateBAODefaults() {
941 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
942 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
943 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
944 'id' => $contribution['id'],
945 'api.contribution.delete' => 1,
947 $this->assertEquals(1, $contribution['contribution_status_id']);
948 $this->assertEquals('Check', $contribution['payment_instrument']);
952 * Function tests that line items, financial records are updated when contribution amount is changed.
954 public function testCreateUpdateContributionChangeTotal() {
955 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
956 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
958 'entity_id' => $contribution['id'],
959 'entity_table' => 'civicrm_contribution',
961 'return' => 'line_total',
963 $this->assertEquals('100.00', $lineItems);
964 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
965 // Financial trxn SUM = 100 + 5 (fee)
966 $this->assertEquals('105.00', $trxnAmount);
969 'id' => $contribution['id'],
970 'total_amount' => '125',
972 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
974 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
976 'entity_id' => $contribution['id'],
977 'entity_table' => 'civicrm_contribution',
979 'return' => 'line_total',
982 $this->assertEquals('125.00', $lineItems);
983 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
985 // Financial trxn SUM = 125 + 5 (fee).
986 $this->assertEquals('130.00', $trxnAmount);
987 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
991 * Function tests that line items, financial records are updated when pay later contribution is received.
993 public function testCreateUpdateContributionPayLater() {
994 $contribParams = array(
995 'contact_id' => $this->_individualId
,
996 'receive_date' => '2012-01-01',
997 'total_amount' => 100.00,
998 'financial_type_id' => $this->_financialTypeId
,
999 'payment_instrument_id' => 1,
1000 'contribution_status_id' => 2,
1001 'is_pay_later' => 1,
1004 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1006 $newParams = array_merge($contribParams, array(
1007 'id' => $contribution['id'],
1008 'contribution_status_id' => 1,
1011 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1012 $contribution = $contribution['values'][$contribution['id']];
1013 $this->assertEquals($contribution['contribution_status_id'], '1');
1014 $this->_checkFinancialItem($contribution['id'], 'paylater');
1015 $this->_checkFinancialTrxn($contribution, 'payLater');
1019 * Function tests that financial records are updated when Payment Instrument is changed.
1021 public function testCreateUpdateContributionPaymentInstrument() {
1022 $instrumentId = $this->_addPaymentInstrument();
1023 $contribParams = array(
1024 'contact_id' => $this->_individualId
,
1025 'total_amount' => 100.00,
1026 'financial_type_id' => $this->_financialTypeId
,
1027 'payment_instrument_id' => 4,
1028 'contribution_status_id' => 1,
1031 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1033 $newParams = array_merge($contribParams, array(
1034 'id' => $contribution['id'],
1035 'payment_instrument_id' => $instrumentId,
1038 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1039 $this->assertAPISuccess($contribution);
1040 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1044 * Function tests that financial records are added when Contribution is Refunded.
1046 public function testCreateUpdateContributionRefund() {
1047 $contributionParams = array(
1048 'contact_id' => $this->_individualId
,
1049 'receive_date' => '2012-01-01',
1050 'total_amount' => 100.00,
1051 'financial_type_id' => $this->_financialTypeId
,
1052 'payment_instrument_id' => 4,
1053 'contribution_status_id' => 1,
1054 'trxn_id' => 'original_payment',
1056 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1057 $newParams = array_merge($contributionParams, array(
1058 'id' => $contribution['id'],
1059 'contribution_status_id' => 'Refunded',
1060 'cancel_date' => '2015-01-01 09:00',
1061 'refund_trxn_id' => 'the refund',
1065 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1066 $this->_checkFinancialTrxn($contribution, 'refund');
1067 $this->_checkFinancialItem($contribution['id'], 'refund');
1068 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1069 'id' => $contribution['id'],
1070 'return' => 'trxn_id',
1075 * Function tests that trxn_id is set when passed in.
1077 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1078 * when trxn_id is passed in.
1080 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1081 $contributionParams = array(
1082 'contact_id' => $this->_individualId
,
1083 'receive_date' => '2012-01-01',
1084 'total_amount' => 100.00,
1085 'financial_type_id' => $this->_financialTypeId
,
1086 'payment_instrument_id' => 4,
1087 'contribution_status_id' => 1,
1088 'trxn_id' => 'original_payment',
1090 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1091 $newParams = array_merge($contributionParams, array(
1092 'id' => $contribution['id'],
1093 'contribution_status_id' => 'Refunded',
1094 'cancel_date' => '2015-01-01 09:00',
1095 'trxn_id' => 'the refund',
1099 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1100 $this->_checkFinancialTrxn($contribution, 'refund');
1101 $this->_checkFinancialItem($contribution['id'], 'refund');
1102 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1103 'id' => $contribution['id'],
1104 'return' => 'trxn_id',
1109 * Function tests that trxn_id is set when passed in.
1111 * Here we ensure that the civicrm_contribution.trxn_id is set
1112 * when trxn_id is passed in but if refund_trxn_id is different then that
1113 * is kept for the refund transaction.
1115 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1116 $contributionParams = array(
1117 'contact_id' => $this->_individualId
,
1118 'receive_date' => '2012-01-01',
1119 'total_amount' => 100.00,
1120 'financial_type_id' => $this->_financialTypeId
,
1121 'payment_instrument_id' => 4,
1122 'contribution_status_id' => 1,
1123 'trxn_id' => 'original_payment',
1125 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1126 $newParams = array_merge($contributionParams, array(
1127 'id' => $contribution['id'],
1128 'contribution_status_id' => 'Refunded',
1129 'cancel_date' => '2015-01-01 09:00',
1130 'trxn_id' => 'cont id',
1131 'refund_trxn_id' => 'the refund',
1135 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1136 $this->_checkFinancialTrxn($contribution, 'refund');
1137 $this->_checkFinancialItem($contribution['id'], 'refund');
1138 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1139 'id' => $contribution['id'],
1140 'return' => 'trxn_id',
1145 * Function tests that refund_trxn_id is set when passed in empty.
1147 * Here we ensure that the civicrm_contribution.trxn_id is set
1148 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1149 * is kept for the refund transaction.
1151 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1152 $contributionParams = array(
1153 'contact_id' => $this->_individualId
,
1154 'receive_date' => '2012-01-01',
1155 'total_amount' => 100.00,
1156 'financial_type_id' => $this->_financialTypeId
,
1157 'payment_instrument_id' => 4,
1158 'contribution_status_id' => 1,
1159 'trxn_id' => 'original_payment',
1161 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1162 $newParams = array_merge($contributionParams, array(
1163 'id' => $contribution['id'],
1164 'contribution_status_id' => 'Refunded',
1165 'cancel_date' => '2015-01-01 09:00',
1166 'trxn_id' => 'cont id',
1167 'refund_trxn_id' => '',
1171 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1172 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1173 $this->_checkFinancialItem($contribution['id'], 'refund');
1174 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1175 'id' => $contribution['id'],
1176 'return' => 'trxn_id',
1181 * Function tests invalid contribution status change.
1183 public function testCreateUpdateContributionInValidStatusChange() {
1184 $contribParams = array(
1186 'receive_date' => '2012-01-01',
1187 'total_amount' => 100.00,
1188 'financial_type_id' => 1,
1189 'payment_instrument_id' => 1,
1190 'contribution_status_id' => 1,
1192 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1193 $newParams = array_merge($contribParams, array(
1194 'id' => $contribution['id'],
1195 'contribution_status_id' => 2,
1198 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1203 * Function tests that financial records are added when Pending Contribution is Canceled.
1205 public function testCreateUpdateContributionCancelPending() {
1206 $contribParams = array(
1207 'contact_id' => $this->_individualId
,
1208 'receive_date' => '2012-01-01',
1209 'total_amount' => 100.00,
1210 'financial_type_id' => $this->_financialTypeId
,
1211 'payment_instrument_id' => 1,
1212 'contribution_status_id' => 2,
1213 'is_pay_later' => 1,
1216 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1217 $newParams = array_merge($contribParams, array(
1218 'id' => $contribution['id'],
1219 'contribution_status_id' => 3,
1222 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1223 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1224 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1228 * Function tests that financial records are added when Financial Type is Changed.
1230 public function testCreateUpdateContributionChangeFinancialType() {
1231 $contribParams = array(
1232 'contact_id' => $this->_individualId
,
1233 'receive_date' => '2012-01-01',
1234 'total_amount' => 100.00,
1235 'financial_type_id' => 1,
1236 'payment_instrument_id' => 1,
1237 'contribution_status_id' => 1,
1240 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1241 $newParams = array_merge($contribParams, array(
1242 'id' => $contribution['id'],
1243 'financial_type_id' => 3,
1246 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1247 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1248 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1252 * Test that update does not change status id CRM-15105.
1254 public function testCreateUpdateWithoutChangingPendingStatus() {
1255 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1256 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1257 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1258 'id' => $contribution['id'],
1259 'api.contribution.delete' => 1,
1261 $this->assertEquals(2, $contribution['contribution_status_id']);
1265 * Test Updating a Contribution.
1267 * CHANGE: we require the API to do an incremental update
1269 public function testCreateUpdateContribution() {
1271 $contributionID = $this->contributionCreate(array(
1272 'contact_id' => $this->_individualId
,
1273 'trxn_id' => 212355,
1274 'financial_type_id' => $this->_financialTypeId
,
1275 'invoice_id' => 'old_invoice',
1277 $old_params = array(
1278 'contribution_id' => $contributionID,
1280 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1281 $this->assertEquals($original['id'], $contributionID);
1282 //set up list of old params, verify
1284 //This should not be required on update:
1285 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1286 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1287 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1288 $old_source = $original['values'][$contributionID]['contribution_source'];
1290 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1291 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1293 //check against values in CiviUnitTestCase::createContribution()
1294 $this->assertEquals($old_contact_id, $this->_individualId
);
1295 $this->assertEquals($old_fee_amount, 5.00);
1296 $this->assertEquals($old_source, 'SSF');
1297 $this->assertEquals($old_trxn_id, 212355);
1298 $this->assertEquals($old_invoice_id, 'old_invoice');
1300 'id' => $contributionID,
1301 'contact_id' => $this->_individualId
,
1302 'total_amount' => 110.00,
1303 'financial_type_id' => $this->_financialTypeId
,
1304 'non_deductible_amount' => 10.00,
1305 'net_amount' => 100.00,
1306 'contribution_status_id' => 1,
1307 'note' => 'Donating for Nobel Cause',
1311 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1313 $new_params = array(
1314 'contribution_id' => $contribution['id'],
1317 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1319 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1320 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1321 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId
);
1322 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1323 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1324 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1325 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1326 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1327 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1328 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1329 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1331 'contribution_id' => $contributionID,
1334 $result = $this->callAPISuccess('contribution', 'delete', $params);
1335 $this->assertAPISuccess($result);
1338 ///////////////// civicrm_contribution_delete methods
1341 * Attempt (but fail) to delete a contribution without parameters.
1343 public function testDeleteEmptyParamsContribution() {
1345 $this->callAPIFailure('contribution', 'delete', $params);
1348 public function testDeleteParamsNotArrayContribution() {
1349 $params = 'contribution_id= 1';
1350 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1351 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1354 public function testDeleteWrongParamContribution() {
1356 'contribution_source' => 'SSF',
1359 $this->callAPIFailure('contribution', 'delete', $params);
1362 public function testDeleteContribution() {
1363 $contributionID = $this->contributionCreate(array(
1364 'contact_id' => $this->_individualId
,
1365 'financial_type_id' => $this->_financialTypeId
,
1368 'id' => $contributionID,
1370 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1374 * Test civicrm_contribution_search with empty params.
1376 * All available contributions expected.
1378 public function testSearchEmptyParams() {
1382 'contact_id' => $this->_individualId
,
1383 'receive_date' => date('Ymd'),
1384 'total_amount' => 100.00,
1385 'financial_type_id' => $this->_financialTypeId
,
1386 'non_deductible_amount' => 10.00,
1387 'fee_amount' => 5.00,
1388 'net_amount' => 95.00,
1390 'invoice_id' => 78910,
1392 'contribution_status_id' => 1,
1395 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1397 $result = $this->callAPISuccess('contribution', 'get', $params);
1398 // We're taking the first element.
1399 $res = $result['values'][$contribution['id']];
1401 $this->assertEquals($p['contact_id'], $res['contact_id']);
1402 $this->assertEquals($p['total_amount'], $res['total_amount']);
1403 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1404 $this->assertEquals($p['net_amount'], $res['net_amount']);
1405 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1406 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1407 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1408 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1409 $this->assertEquals($p['source'], $res['contribution_source']);
1410 // contribution_status_id = 1 => Completed
1411 $this->assertEquals('Completed', $res['contribution_status']);
1413 $this->contributionDelete($contribution['id']);
1417 * Test civicrm_contribution_search. Success expected.
1419 public function testSearch() {
1421 'contact_id' => $this->_individualId
,
1422 'receive_date' => date('Ymd'),
1423 'total_amount' => 100.00,
1424 'financial_type_id' => $this->_financialTypeId
,
1425 'non_deductible_amount' => 10.00,
1426 'contribution_status_id' => 1,
1429 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1432 'contact_id' => $this->_individualId
,
1433 'receive_date' => date('Ymd'),
1434 'total_amount' => 200.00,
1435 'financial_type_id' => $this->_financialTypeId
,
1436 'non_deductible_amount' => 20.00,
1437 'trxn_id' => 5454565,
1438 'invoice_id' => 1212124,
1439 'fee_amount' => 50.00,
1440 'net_amount' => 60.00,
1441 'contribution_status_id' => 2,
1444 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1447 'contribution_id' => $contribution2['id'],
1450 $result = $this->callAPISuccess('contribution', 'get', $params);
1451 $res = $result['values'][$contribution2['id']];
1453 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1454 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1455 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1456 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1457 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1458 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1459 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1460 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1461 // contribution_status_id = 2 => Pending
1462 $this->assertEquals('Pending', $res['contribution_status']);
1464 $this->contributionDelete($contribution1['id']);
1465 $this->contributionDelete($contribution2['id']);
1469 * Test completing a transaction via the API.
1471 * Note that we are creating a logged in user because email goes out from
1474 public function testCompleteTransaction() {
1475 $mut = new CiviMailUtils($this, TRUE);
1476 $this->createLoggedInUser();
1477 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1478 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1479 $this->callAPISuccess('contribution', 'completetransaction', array(
1480 'id' => $contribution['id'],
1482 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1483 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1484 $mut->checkMailLog(array(
1485 'Receipt - Contribution',
1486 'Please print this confirmation for your records.',
1492 * Test completing a transaction via the API.
1494 * Note that we are creating a logged in user because email goes out from
1497 public function testCompleteTransactionFeeAmount() {
1498 $this->createLoggedInUser();
1499 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1500 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1501 $this->callAPISuccess('contribution', 'completetransaction', array(
1502 'id' => $contribution['id'],
1503 'fee_amount' => '.56',
1504 'trxn_id' => '7778888',
1506 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1507 $this->assertEquals('Completed', $contribution['contribution_status']);
1508 $this->assertEquals('7778888', $contribution['trxn_id']);
1509 $this->assertEquals('.56', $contribution['fee_amount']);
1510 $this->assertEquals('99.44', $contribution['net_amount']);
1514 * Test repeat contribution successfully creates line items.
1516 public function testRepeatTransaction() {
1517 $paymentProcessorID = $this->paymentProcessorCreate();
1518 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1519 'contact_id' => $this->_individualId
,
1520 'installments' => '12',
1521 'frequency_interval' => '1',
1523 'contribution_status_id' => 1,
1524 'start_date' => '2012-01-01 00:00:00',
1525 'currency' => 'USD',
1526 'frequency_unit' => 'month',
1527 'payment_processor_id' => $paymentProcessorID,
1529 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1531 array('contribution_recur_id' => $contributionRecur['id']))
1534 $this->callAPISuccess('contribution', 'repeattransaction', array(
1535 'original_contribution_id' => $originalContribution['id'],
1536 'contribution_status_id' => 'Completed',
1537 'trxn_id' => uniqid(),
1539 $lineItemParams = array(
1540 'entity_id' => $originalContribution['id'],
1548 'financial_type_id',
1549 'deductible_amount',
1550 'price_field_value_id',
1554 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1555 'entity_id' => $originalContribution['id'],
1557 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1558 'entity_id' => $originalContribution['id'] +
1,
1560 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1561 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1562 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1564 $this->quickCleanUpFinancialEntities();
1568 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1570 public function testRepeatTransactionAcceptRecurID() {
1571 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1572 'contact_id' => $this->_individualId
,
1573 'installments' => '12',
1574 'frequency_interval' => '1',
1576 'contribution_status_id' => 1,
1577 'start_date' => '2012-01-01 00:00:00',
1578 'currency' => 'USD',
1579 'frequency_unit' => 'month',
1580 'payment_processor_id' => $this->paymentProcessorID
,
1582 $this->callAPISuccess('contribution', 'create', array_merge(
1584 array('contribution_recur_id' => $contributionRecur['id']))
1587 $this->callAPISuccess('contribution', 'repeattransaction', array(
1588 'contribution_recur_id' => $contributionRecur['id'],
1589 'contribution_status_id' => 'Completed',
1590 'trxn_id' => uniqid(),
1593 $this->quickCleanUpFinancialEntities();
1597 * CRM-16397 test appropriate action if total amount has changed for single line items.
1599 public function testRepeatTransactionAlteredAmount() {
1600 $paymentProcessorID = $this->paymentProcessorCreate();
1601 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1602 'contact_id' => $this->_individualId
,
1603 'installments' => '12',
1604 'frequency_interval' => '1',
1606 'contribution_status_id' => 1,
1607 'start_date' => '2012-01-01 00:00:00',
1608 'currency' => 'USD',
1609 'frequency_unit' => 'month',
1610 'payment_processor_id' => $paymentProcessorID,
1612 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1615 'contribution_recur_id' => $contributionRecur['id'],
1619 $this->callAPISuccess('contribution', 'repeattransaction', array(
1620 'original_contribution_id' => $originalContribution['id'],
1621 'contribution_status_id' => 'Completed',
1622 'trxn_id' => uniqid(),
1623 'total_amount' => '400',
1626 $lineItemParams = array(
1627 'entity_id' => $originalContribution['id'],
1635 'financial_type_id',
1636 'deductible_amount',
1637 'price_field_value_id',
1641 $this->callAPISuccessGetSingle('contribution', array(
1642 'total_amount' => 400,
1644 'net_amount' => 350,
1646 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1647 'entity_id' => $originalContribution['id'],
1649 $expectedLineItem = array_merge(
1650 $lineItem1['values'][0], array(
1651 'line_total' => '400.00',
1652 'unit_price' => '400.00',
1656 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1657 'entity_id' => $originalContribution['id'] +
1,
1659 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1660 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1661 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1665 * CRM-17718 test appropriate action if financial type has changed for single line items.
1667 public function testRepeatTransactionPassedInFinancialType() {
1668 $originalContribution = $this->setUpRecurringContribution();
1670 $this->callAPISuccess('contribution', 'repeattransaction', array(
1671 'original_contribution_id' => $originalContribution['id'],
1672 'contribution_status_id' => 'Completed',
1673 'trxn_id' => uniqid(),
1674 'financial_type_id' => 2,
1676 $lineItemParams = array(
1677 'entity_id' => $originalContribution['id'],
1685 'financial_type_id',
1686 'deductible_amount',
1687 'price_field_value_id',
1692 $this->callAPISuccessGetSingle('contribution', array(
1693 'total_amount' => 100,
1694 'financial_type_id' => 2,
1696 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1697 'entity_id' => $originalContribution['id'],
1699 $expectedLineItem = array_merge(
1700 $lineItem1['values'][0], array(
1701 'line_total' => '100.00',
1702 'unit_price' => '100.00',
1703 'financial_type_id' => 2,
1707 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1708 'entity_id' => $originalContribution['id'] +
1,
1710 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1711 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1712 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1716 * CRM-17718 test appropriate action if financial type has changed for single line items.
1718 public function testRepeatTransactionUpdatedFinancialType() {
1719 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
1721 $this->callAPISuccess('contribution', 'repeattransaction', array(
1722 'contribution_recur_id' => $originalContribution['id'],
1723 'contribution_status_id' => 'Completed',
1724 'trxn_id' => uniqid(),
1726 $lineItemParams = array(
1727 'entity_id' => $originalContribution['id'],
1735 'financial_type_id',
1736 'deductible_amount',
1737 'price_field_value_id',
1742 $this->callAPISuccessGetSingle('contribution', array(
1743 'total_amount' => 100,
1744 'financial_type_id' => 2,
1746 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1747 'entity_id' => $originalContribution['id'],
1749 $expectedLineItem = array_merge(
1750 $lineItem1['values'][0], array(
1751 'line_total' => '100.00',
1752 'unit_price' => '100.00',
1753 'financial_type_id' => 2,
1757 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1758 'entity_id' => $originalContribution['id'] +
1,
1760 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1761 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1762 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1766 * CRM-16397 test appropriate action if campaign has been passed in.
1768 public function testRepeatTransactionPassedInCampaign() {
1769 $paymentProcessorID = $this->paymentProcessorCreate();
1770 $campaignID = $this->campaignCreate();
1771 $campaignID2 = $this->campaignCreate();
1772 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1773 'contact_id' => $this->_individualId
,
1774 'installments' => '12',
1775 'frequency_interval' => '1',
1777 'contribution_status_id' => 1,
1778 'start_date' => '2012-01-01 00:00:00',
1779 'currency' => 'USD',
1780 'frequency_unit' => 'month',
1781 'payment_processor_id' => $paymentProcessorID,
1783 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1786 'contribution_recur_id' => $contributionRecur['id'],
1787 'campaign_id' => $campaignID,
1791 $this->callAPISuccess('contribution', 'repeattransaction', array(
1792 'original_contribution_id' => $originalContribution['id'],
1793 'contribution_status_id' => 'Completed',
1794 'trxn_id' => uniqid(),
1795 'campaign_id' => $campaignID2,
1798 $this->callAPISuccessGetSingle('contribution', array(
1799 'total_amount' => 100,
1800 'campaign_id' => $campaignID2,
1805 * CRM-17718 campaign stored on contribution recur gets priority.
1807 * This reflects the fact we permit people to update them.
1809 public function testRepeatTransactionUpdatedCampaign() {
1810 $paymentProcessorID = $this->paymentProcessorCreate();
1811 $campaignID = $this->campaignCreate();
1812 $campaignID2 = $this->campaignCreate();
1813 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1814 'contact_id' => $this->_individualId
,
1815 'installments' => '12',
1816 'frequency_interval' => '1',
1818 'contribution_status_id' => 1,
1819 'start_date' => '2012-01-01 00:00:00',
1820 'currency' => 'USD',
1821 'frequency_unit' => 'month',
1822 'payment_processor_id' => $paymentProcessorID,
1823 'campaign_id' => $campaignID,
1825 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1828 'contribution_recur_id' => $contributionRecur['id'],
1829 'campaign_id' => $campaignID2,
1833 $this->callAPISuccess('contribution', 'repeattransaction', array(
1834 'original_contribution_id' => $originalContribution['id'],
1835 'contribution_status_id' => 'Completed',
1836 'trxn_id' => uniqid(),
1839 $this->callAPISuccessGetSingle('contribution', array(
1840 'total_amount' => 100,
1841 'campaign_id' => $campaignID,
1846 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
1848 public function testCompleteTransactionNetAmountOK() {
1849 $this->createLoggedInUser();
1850 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1851 unset($params['net_amount']);
1852 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1853 $this->callAPISuccess('contribution', 'completetransaction', array(
1854 'id' => $contribution['id'],
1856 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1857 $this->assertEquals('Completed', $contribution['contribution_status']);
1858 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
1862 * CRM-14151 - Test completing a transaction via the API.
1864 public function testCompleteTransactionWithReceiptDateSet() {
1865 $mut = new CiviMailUtils($this, TRUE);
1866 $this->createLoggedInUser();
1867 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1868 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1869 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
1870 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1871 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1872 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
1873 $mut->checkMailLog(array(
1874 'Receipt - Contribution',
1875 'Please print this confirmation for your records.',
1881 * Test completing first transaction in a recurring series.
1883 * The status should be set to 'in progress' and the next scheduled payment date calculated.
1885 public function testCompleteTransactionSetStatusToInProgress() {
1886 $paymentProcessorID = $this->paymentProcessorCreate();
1887 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1888 'contact_id' => $this->_individualId
,
1889 'installments' => '12',
1890 'frequency_interval' => '1',
1892 'contribution_status_id' => 'Pending',
1893 'start_date' => '2012-01-01 00:00:00',
1894 'currency' => 'USD',
1895 'frequency_unit' => 'month',
1896 'payment_processor_id' => $paymentProcessorID,
1898 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
1901 'contribution_recur_id' => $contributionRecur['id'],
1902 'contribution_status_id' => 'Pending',
1905 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
1906 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
1907 'id' => $contributionRecur['id'],
1908 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
1910 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
1911 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
1915 * Test completing a transaction with an event via the API.
1917 * Note that we are creating a logged in user because email goes out from
1920 public function testCompleteTransactionWithParticipantRecord() {
1921 $mut = new CiviMailUtils($this, TRUE);
1922 $mut->clearMessages();
1923 $this->createLoggedInUser();
1924 $contributionID = $this->createPendingParticipantContribution();
1925 $this->callAPISuccess('contribution', 'completetransaction', array(
1926 'id' => $contributionID,
1929 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1930 'id' => $this->_ids
['participant'],
1931 'return' => 'participant_status_id',
1933 $this->assertEquals(1, $participantStatus);
1934 $mut->checkMailLog(array(
1935 'Annual CiviCRM meet',
1937 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1943 * Test membership is renewed when transaction completed.
1945 public function testCompleteTransactionMembershipPriceSet() {
1946 $this->createPriceSetWithPage('membership');
1947 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
1951 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1952 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1953 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
1954 'membership_id' => $this->_ids
['membership'],
1956 $this->assertEquals(1, $logs['count']);
1957 $this->assertEquals($stateOfGrace, $membership['status_id']);
1958 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1959 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1960 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1961 $this->callAPISuccessGetSingle('LineItem', array(
1962 'entity_id' => $this->_ids
['membership'],
1963 'entity_table' => 'civicrm_membership',
1965 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
1966 'membership_id' => $this->_ids
['membership'],
1968 $this->assertEquals(2, $logs['count']);
1969 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
1970 $this->cleanUpAfterPriceSets();
1974 * Test membership is renewed when transaction completed.
1976 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1977 $this->createPriceSetWithPage('membership');
1978 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1979 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1980 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1981 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1982 $this->cleanUpAfterPriceSets();
1985 public function cleanUpAfterPriceSets() {
1986 $this->quickCleanUpFinancialEntities();
1987 $this->contactDelete($this->_ids
['contact']);
1992 * Create price set with contribution test for test setup.
1994 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1995 * on parent class at some point (fn is not in 4.4).
1998 * @param array $params
2000 public function createPriceSetWithPage($entity, $params = array()) {
2001 $membershipTypeID = $this->membershipTypeCreate();
2002 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
2003 'title' => "Test Contribution Page",
2004 'financial_type_id' => 1,
2005 'currency' => 'NZD',
2006 'goal_amount' => 50,
2007 'is_pay_later' => 1,
2008 'is_monetary' => TRUE,
2009 'is_email_receipt' => FALSE,
2011 $priceSet = $this->callAPISuccess('price_set', 'create', array(
2012 'is_quick_config' => 0,
2013 'extends' => 'CiviMember',
2014 'financial_type_id' => 1,
2015 'title' => 'my Page',
2017 $priceSetID = $priceSet['id'];
2019 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
2020 $priceField = $this->callAPISuccess('price_field', 'create', array(
2021 'price_set_id' => $priceSetID,
2022 'label' => 'Goat Breed',
2023 'html_type' => 'Radio',
2025 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2026 'price_set_id' => $priceSetID,
2027 'price_field_id' => $priceField['id'],
2028 'label' => 'Long Haired Goat',
2030 'financial_type_id' => 'Donation',
2031 'membership_type_id' => $membershipTypeID,
2032 'membership_num_terms' => 1,
2035 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
2036 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2037 'price_set_id' => $priceSetID,
2038 'price_field_id' => $priceField['id'],
2039 'label' => 'Shoe-eating Goat',
2041 'financial_type_id' => 'Donation',
2042 'membership_type_id' => $membershipTypeID,
2043 'membership_num_terms' => 2,
2046 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
2047 $this->_ids
['price_set'] = $priceSetID;
2048 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
2049 $this->_ids
['price_field'] = array($priceField['id']);
2051 $this->_ids
['membership_type'] = $membershipTypeID;
2055 * Set up a pending transaction with a specific price field id.
2057 * @param int $priceFieldValueID
2059 public function setUpPendingContribution($priceFieldValueID) {
2060 $contactID = $this->individualCreate();
2061 $membership = $this->callAPISuccess('membership', 'create', array(
2062 'contact_id' => $contactID,
2063 'membership_type_id' => $this->_ids
['membership_type'],
2064 'start_date' => 'yesterday - 1 year',
2065 'end_date' => 'yesterday',
2066 'join_date' => 'yesterday - 1 year',
2068 $contribution = $this->callAPISuccess('contribution', 'create', array(
2070 'contact_id' => $contactID,
2071 'receive_date' => date('Ymd'),
2072 'total_amount' => 100.00,
2073 'financial_type_id' => 1,
2074 'payment_instrument_id' => 'Credit Card',
2075 'non_deductible_amount' => 10.00,
2076 'trxn_id' => 'jdhfi88',
2077 'invoice_id' => 'djfhiewuyr',
2079 'contribution_status_id' => 2,
2080 'contribution_page_id' => $this->_ids
['contribution_page'],
2081 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2084 $this->callAPISuccess('line_item', 'create', array(
2085 'entity_id' => $contribution['id'],
2086 'entity_table' => 'civicrm_contribution',
2087 'contribution_id' => $contribution['id'],
2088 'price_field_id' => $this->_ids
['price_field'][0],
2092 'financial_type_id' => 1,
2093 'price_field_value_id' => $priceFieldValueID,
2095 $this->_ids
['contact'] = $contactID;
2096 $this->_ids
['contribution'] = $contribution['id'];
2097 $this->_ids
['membership'] = $membership['id'];
2101 * Test sending a mail via the API.
2103 public function testSendMail() {
2104 $mut = new CiviMailUtils($this, TRUE);
2105 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
2106 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2107 'id' => $contribution['id'],
2108 'receipt_from_email' => 'api@civicrm.org',
2111 $mut->checkMailLog(array(
2113 'Contribution Information',
2114 'Please print this confirmation for your records',
2123 * Test sending a mail via the API.
2125 public function testSendMailEvent() {
2126 $mut = new CiviMailUtils($this, TRUE);
2127 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
2128 $event = $this->eventCreate(array(
2129 'is_email_confirm' => 1,
2130 'confirm_from_email' => 'test@civicrm.org',
2132 $this->_eventID
= $event['id'];
2133 $participantParams = array(
2134 'contact_id' => $this->_individualId
,
2135 'event_id' => $this->_eventID
,
2138 // to ensure it matches later on
2139 'register_date' => '2007-07-21 00:00:00',
2140 'source' => 'Online Event Registration: API Testing',
2143 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2144 $this->callAPISuccess('participant_payment', 'create', array(
2145 'participant_id' => $participant['id'],
2146 'contribution_id' => $contribution['id'],
2148 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2149 'id' => $contribution['id'],
2150 'receipt_from_email' => 'api@civicrm.org',
2154 $mut->checkMailLog(array(
2155 'Annual CiviCRM meet',
2157 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
2164 * This function does a GET & compares the result against the $params.
2166 * Use as a double check on Creates.
2168 * @param array $params
2170 * @param bool $delete
2172 public function contributionGetnCheck($params, $id, $delete = TRUE) {
2174 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
2180 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
2182 $this->assertAPISuccess($contribution, 0);
2183 $values = $contribution['values'][$contribution['id']];
2184 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2185 // this is not returned in id format
2186 unset($params['payment_instrument_id']);
2187 $params['contribution_source'] = $params['source'];
2188 unset($params['source']);
2189 foreach ($params as $key => $value) {
2190 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
2195 * Create a pending contribution & linked pending participant record (along with an event).
2197 public function createPendingParticipantContribution() {
2198 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
2199 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
2200 $this->_ids
['participant'] = $participantID;
2201 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
2202 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2203 $this->callAPISuccess('participant_payment', 'create', array(
2204 'contribution_id' => $contribution['id'],
2205 'participant_id' => $participantID,
2207 $this->callAPISuccess('line_item', 'get', array(
2208 'entity_id' => $contribution['id'],
2209 'entity_table' => 'civicrm_contribution',
2210 'api.line_item.create' => array(
2211 'entity_id' => $participantID,
2212 'entity_table' => 'civicrm_participant',
2215 return $contribution['id'];
2219 * Get financial transaction amount.
2221 * @param int $contId
2223 * @return null|string
2225 public function _getFinancialTrxnAmount($contId) {
2227 SUM( ft.total_amount ) AS total
2228 FROM civicrm_financial_trxn AS ft
2229 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2230 WHERE ceft.entity_table = 'civicrm_contribution'
2231 AND ceft.entity_id = {$contId}";
2233 $result = CRM_Core_DAO
::singleValueQuery($query);
2238 * @param int $contId
2240 * @return null|string
2242 public function _getFinancialItemAmount($contId) {
2243 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
2246 FROM civicrm_financial_item
2247 WHERE entity_table = 'civicrm_line_item'
2248 AND entity_id = {$lineItem}";
2249 $result = CRM_Core_DAO
::singleValueQuery($query);
2254 * @param int $contId
2257 public function _checkFinancialItem($contId, $context) {
2258 if ($context != 'paylater') {
2260 'entity_id' => $contId,
2261 'entity_table' => 'civicrm_contribution',
2263 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
2264 $entityParams = array(
2265 'financial_trxn_id' => $trxn['financial_trxn_id'],
2266 'entity_table' => 'civicrm_financial_item',
2268 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2270 'id' => $entityTrxn['entity_id'],
2273 if ($context == 'paylater') {
2274 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
2275 foreach ($lineItems as $key => $item) {
2277 'entity_id' => $key,
2278 'entity_table' => 'civicrm_line_item',
2280 $compareParams = array('status_id' => 1);
2281 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2284 elseif ($context == 'refund') {
2285 $compareParams = array(
2287 'financial_account_id' => 1,
2291 elseif ($context == 'cancelPending') {
2292 $compareParams = array(
2294 'financial_account_id' => 1,
2298 elseif ($context == 'changeFinancial') {
2299 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
2301 'entity_id' => $lineKey,
2304 $compareParams = array(
2305 'financial_account_id' => 1,
2307 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2309 'financial_account_id' => 3,
2310 'entity_id' => $lineKey,
2312 $compareParams = array(
2316 if ($context != 'paylater') {
2317 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2322 * @param array $contribution
2323 * @param string $context
2324 * @param int $instrumentId
2326 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
2327 $trxnParams = array(
2328 'entity_id' => $contribution['id'],
2329 'entity_table' => 'civicrm_contribution',
2331 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2333 'id' => $trxn['financial_trxn_id'],
2335 if ($context == 'payLater') {
2336 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2337 $compareParams = array(
2339 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
2342 elseif ($context == 'refund') {
2343 $compareParams = array(
2344 'to_financial_account_id' => 6,
2345 'total_amount' => -100,
2347 'trxn_date' => '2015-01-01 09:00:00',
2348 'trxn_id' => 'the refund',
2351 elseif ($context == 'cancelPending') {
2352 $compareParams = array(
2353 'to_financial_account_id' => 7,
2354 'total_amount' => -100,
2358 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
2359 $entityParams = array(
2360 'entity_id' => $contribution['id'],
2361 'entity_table' => 'civicrm_contribution',
2364 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2365 $trxnParams1 = array(
2366 'id' => $trxn['financial_trxn_id'],
2368 $compareParams = array(
2369 'total_amount' => -100,
2372 if ($context == 'paymentInstrument') {
2373 $compareParams +
= array(
2374 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
2375 'payment_instrument_id' => 4,
2379 $compareParams['to_financial_account_id'] = 12;
2381 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
2382 $compareParams['total_amount'] = 100;
2383 if ($context == 'paymentInstrument') {
2384 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
2385 $compareParams['payment_instrument_id'] = $instrumentId;
2388 $compareParams['to_financial_account_id'] = 12;
2392 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
2398 public function _addPaymentInstrument() {
2399 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2400 $optionParams = array(
2401 'option_group_id' => $gId,
2402 'label' => 'Test Card',
2403 'name' => 'Test Card',
2408 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2409 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2410 $financialParams = array(
2411 'entity_table' => 'civicrm_option_value',
2412 'entity_id' => $optionValue['id'],
2413 'account_relationship' => $relationTypeId,
2414 'financial_account_id' => 7,
2416 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
2417 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2418 return $optionValue['values'][$optionValue['id']]['value'];
2422 * @param array $params
2425 public function _checkFinancialRecords($params, $context) {
2426 $entityParams = array(
2427 'entity_id' => $params['id'],
2428 'entity_table' => 'civicrm_contribution',
2430 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2431 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
2432 if ($context == 'pending') {
2433 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
2434 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2437 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2438 $trxnParams = array(
2439 'id' => $trxn['financial_trxn_id'],
2441 if ($context != 'online' && $context != 'payLater') {
2442 $compareParams = array(
2443 'to_financial_account_id' => 6,
2444 'total_amount' => 100,
2448 if ($context == 'feeAmount') {
2449 $compareParams['fee_amount'] = 50;
2451 elseif ($context == 'online') {
2452 $compareParams = array(
2453 'to_financial_account_id' => 12,
2454 'total_amount' => 100,
2458 elseif ($context == 'payLater') {
2459 $compareParams = array(
2460 'to_financial_account_id' => 7,
2461 'total_amount' => 100,
2465 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2466 $entityParams = array(
2467 'financial_trxn_id' => $trxn['financial_trxn_id'],
2468 'entity_table' => 'civicrm_financial_item',
2470 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2471 $fitemParams = array(
2472 'id' => $entityTrxn['entity_id'],
2474 $compareParams = array(
2477 'financial_account_id' => 1,
2479 if ($context == 'payLater') {
2480 $compareParams = array(
2483 'financial_account_id' => 1,
2486 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2487 if ($context == 'feeAmount') {
2489 'entity_id' => $params['id'],
2490 'entity_table' => 'civicrm_contribution',
2492 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
2493 $trxnParams = array(
2494 'id' => $maxTrxn['financial_trxn_id'],
2496 $compareParams = array(
2497 'to_financial_account_id' => 5,
2498 'from_financial_account_id' => 6,
2499 'total_amount' => 50,
2502 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
2503 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2504 $fitemParams = array(
2505 'entity_id' => $trxnId['financialTrxnId'],
2506 'entity_table' => 'civicrm_financial_trxn',
2508 $compareParams = array(
2511 'financial_account_id' => 5,
2513 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2518 * Set up the basic recurring contribution for tests.
2520 * @param array $generalParams
2521 * Parameters that can be merged into the recurring AND the contribution.
2523 * @param array $recurParams
2524 * Parameters to merge into the recur only.
2528 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
2529 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2530 'contact_id' => $this->_individualId
,
2531 'installments' => '12',
2532 'frequency_interval' => '1',
2534 'contribution_status_id' => 1,
2535 'start_date' => '2012-01-01 00:00:00',
2536 'currency' => 'USD',
2537 'frequency_unit' => 'month',
2538 'payment_processor_id' => $this->paymentProcessorID
,
2539 ), $generalParams, $recurParams));
2540 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2543 'contribution_recur_id' => $contributionRecur['id'],
2546 return $originalContribution;