3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_contribute_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
35 class api_v3_ContributionTest
extends CiviUnitTestCase
{
38 * Assume empty database with just civicrm_data.
40 protected $_individualId;
41 protected $_contribution;
42 protected $_financialTypeId = 1;
43 protected $_apiversion;
44 protected $_entity = 'Contribution';
47 protected $_ids = array();
48 protected $_pageParams = array();
50 * Payment processor ID (dummy processor).
54 protected $paymentProcessorID;
57 * Parameters to create payment processor.
61 protected $_processorParams = array();
64 * ID of created event.
78 public function setUp() {
81 $this->_apiversion
= 3;
82 $this->_individualId
= $this->individualCreate();
83 $this->_params
= array(
84 'contact_id' => $this->_individualId
,
85 'receive_date' => '20120511',
86 'total_amount' => 100.00,
87 'financial_type_id' => $this->_financialTypeId
,
88 'non_deductible_amount' => 10.00,
90 'net_amount' => 95.00,
92 'contribution_status_id' => 1,
94 $this->_processorParams
= array(
97 'payment_processor_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
98 'financial_account_id' => 12,
101 'url_site' => 'http://dummy.com',
102 'url_recur' => 'http://dummy.com',
105 $this->paymentProcessorID
= $this->processorCreate();
106 $this->_pageParams
= array(
107 'title' => 'Test Contribution Page',
108 'financial_type_id' => 1,
110 'financial_account_id' => 1,
111 'payment_processor' => $this->paymentProcessorID
,
113 'is_allow_other_amount' => 1,
115 'max_amount' => 1000,
120 * Clean up after each test.
122 public function tearDown() {
123 $this->quickCleanUpFinancialEntities();
124 $this->quickCleanup(array('civicrm_uf_match'));
130 public function testGetContribution() {
132 'contact_id' => $this->_individualId
,
133 'receive_date' => '2010-01-20',
134 'total_amount' => 100.00,
135 'financial_type_id' => $this->_financialTypeId
,
136 'non_deductible_amount' => 10.00,
137 'fee_amount' => 5.00,
138 'net_amount' => 95.00,
140 'invoice_id' => 78910,
142 'contribution_status_id' => 1,
144 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
147 'contribution_id' => $this->_contribution
['id'],
150 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
151 $financialParams['id'] = $this->_financialTypeId
;
153 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
155 $this->assertEquals(1, $contribution['count']);
156 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
157 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
158 // Passing back a string rather than an id seems like an error/cruft.
159 // If it is to be introduced we should discuss.
160 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
161 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
162 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
163 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
164 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
165 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
166 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
167 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
168 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
169 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
170 $p['trxn_id'] = '3847';
171 $p['invoice_id'] = '3847';
173 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
175 // Now we have 2 - test getcount.
176 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
177 $this->assertEquals(2, $contribution);
178 // Test id only format.
179 $contribution = $this->callAPISuccess('contribution', 'get', array(
180 'id' => $this->_contribution
['id'],
181 'format.only_id' => 1,
183 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
184 // Test id only format.
185 $contribution = $this->callAPISuccess('contribution', 'get', array(
186 'id' => $contribution2['id'],
187 'format.only_id' => 1,
189 $this->assertEquals($contribution2['id'], $contribution);
191 $contribution = $this->callAPISuccess('contribution', 'get', array(
192 'id' => $this->_contribution
['id'],
194 $this->assertEquals(1, $contribution['count']);
196 // Test get by contact id works.
197 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
199 $this->assertEquals(2, $contribution['count']);
200 $this->callAPISuccess('Contribution', 'Delete', array(
201 'id' => $this->_contribution
['id'],
203 $this->callAPISuccess('Contribution', 'Delete', array(
204 'id' => $contribution2['id'],
209 * Test that test contributions can be retrieved.
211 public function testGetTestContribution() {
212 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('is_test' => 1)));
213 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
217 * We need to ensure previous tested behaviour still works as part of the api contract.
219 public function testGetContributionLegacyBehaviour() {
221 'contact_id' => $this->_individualId
,
222 'receive_date' => '2010-01-20',
223 'total_amount' => 100.00,
224 'contribution_type_id' => $this->_financialTypeId
,
225 'non_deductible_amount' => 10.00,
226 'fee_amount' => 5.00,
227 'net_amount' => 95.00,
229 'invoice_id' => 78910,
231 'contribution_status_id' => 1,
233 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
236 'contribution_id' => $this->_contribution
['id'],
238 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
239 $financialParams['id'] = $this->_financialTypeId
;
241 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
243 $this->assertEquals(1, $contribution['count']);
244 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
245 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
246 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
247 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
248 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
249 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
250 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
251 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
252 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
253 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
254 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
256 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
257 $p['trxn_id'] = '3847';
258 $p['invoice_id'] = '3847';
260 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
262 // now we have 2 - test getcount
263 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
264 $this->assertEquals(2, $contribution);
265 //test id only format
266 $contribution = $this->callAPISuccess('contribution', 'get', array(
267 'id' => $this->_contribution
['id'],
268 'format.only_id' => 1,
270 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
271 //test id only format
272 $contribution = $this->callAPISuccess('contribution', 'get', array(
273 'id' => $contribution2['id'],
274 'format.only_id' => 1,
276 $this->assertEquals($contribution2['id'], $contribution);
277 $contribution = $this->callAPISuccess('contribution', 'get', array(
278 'id' => $this->_contribution
['id'],
281 $this->assertEquals(1, $contribution['count']);
282 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
283 //test get by contact id works
284 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
286 $this->assertEquals(2, $contribution['count']);
287 $this->callAPISuccess('Contribution', 'Delete', array(
288 'id' => $this->_contribution
['id'],
290 $this->callAPISuccess('Contribution', 'Delete', array(
291 'id' => $contribution2['id'],
296 * Create an contribution_id=FALSE and financial_type_id=Donation.
298 public function testCreateEmptyContributionIDUseDonation() {
300 'contribution_id' => FALSE,
303 'check_permissions' => FALSE,
304 'financial_type_id' => 'Donation',
306 $this->callAPISuccess('contribution', 'create', $params);
310 * Check with complete array + custom field.
312 * Note that the test is written on purpose without any
313 * variables specific to participant so it can be replicated into other entities
314 * and / or moved to the automated test suite
316 public function testCreateWithCustom() {
317 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
319 $params = $this->_params
;
320 $params['custom_' . $ids['custom_field_id']] = "custom string";
322 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
323 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
324 $check = $this->callAPISuccess($this->_entity
, 'get', array(
325 'return.custom_' . $ids['custom_field_id'] => 1,
326 'id' => $result['id'],
328 $this->customFieldDelete($ids['custom_field_id']);
329 $this->customGroupDelete($ids['custom_group_id']);
330 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
334 * Check with complete array + custom field.
336 * Note that the test is written on purpose without any
337 * variables specific to participant so it can be replicated into other entities
338 * and / or moved to the automated test suite
340 public function testCreateGetFieldsWithCustom() {
341 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
342 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
343 $result = $this->callAPISuccess('Contribution', 'getfields', array());
344 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
345 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
346 $this->customFieldDelete($ids['custom_field_id']);
347 $this->customGroupDelete($ids['custom_group_id']);
348 $this->customFieldDelete($idsContact['custom_field_id']);
349 $this->customGroupDelete($idsContact['custom_group_id']);
352 public function testCreateContributionNoLineItems() {
355 'contact_id' => $this->_individualId
,
356 'receive_date' => '20120511',
357 'total_amount' => 100.00,
358 'financial_type_id' => $this->_financialTypeId
,
359 'payment_instrument_id' => 1,
360 'non_deductible_amount' => 10.00,
361 'fee_amount' => 50.00,
362 'net_amount' => 90.00,
364 'invoice_id' => 67890,
366 'contribution_status_id' => 1,
370 $contribution = $this->callAPISuccess('contribution', 'create', $params);
371 $lineItems = $this->callAPISuccess('line_item', 'get', array(
372 'entity_id' => $contribution['id'],
373 'entity_table' => 'civicrm_contribution',
376 $this->assertEquals(0, $lineItems['count']);
380 * Test checks that passing in line items suppresses the create mechanism.
382 public function testCreateContributionChainedLineItems() {
384 'contact_id' => $this->_individualId
,
385 'receive_date' => '20120511',
386 'total_amount' => 100.00,
387 'financial_type_id' => $this->_financialTypeId
,
388 'payment_instrument_id' => 1,
389 'non_deductible_amount' => 10.00,
390 'fee_amount' => 50.00,
391 'net_amount' => 90.00,
393 'invoice_id' => 67890,
395 'contribution_status_id' => 1,
397 'api.line_item.create' => array(
399 'price_field_id' => 1,
401 'line_total' => '20',
402 'unit_price' => '10',
405 'price_field_id' => 1,
407 'line_total' => '80',
408 'unit_price' => '80',
413 $description = "Create Contribution with Nested Line Items.";
414 $subfile = "CreateWithNestedLineItems";
415 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
417 $lineItems = $this->callAPISuccess('line_item', 'get', array(
418 'entity_id' => $contribution['id'],
419 'contribution_id' => $contribution['id'],
420 'entity_table' => 'civicrm_contribution',
423 $this->assertEquals(2, $lineItems['count']);
426 public function testCreateContributionOffline() {
428 'contact_id' => $this->_individualId
,
429 'receive_date' => '20120511',
430 'total_amount' => 100.00,
431 'financial_type_id' => 1,
433 'invoice_id' => 67890,
435 'contribution_status_id' => 1,
438 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
439 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
440 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
441 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
442 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
443 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
444 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
445 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
446 $lineItems = $this->callAPISuccess('line_item', 'get', array(
447 'entity_id' => $contribution['id'],
448 'contribution_id' => $contribution['id'],
449 'entity_table' => 'civicrm_contribution',
452 $this->assertEquals(1, $lineItems['count']);
453 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
454 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
455 $this->_checkFinancialRecords($contribution, 'offline');
456 $this->contributionGetnCheck($params, $contribution['id']);
460 * Test create with valid payment instrument.
462 public function testCreateContributionWithPaymentInstrument() {
463 $params = $this->_params +
array('payment_instrument' => 'EFT');
464 $contribution = $this->callAPISuccess('contribution', 'create', $params);
465 $contribution = $this->callAPISuccess('contribution', 'get', array(
467 'id' => $contribution['id'],
469 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
470 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
472 $this->callAPISuccess('contribution', 'create', array(
473 'id' => $contribution['id'],
474 'payment_instrument' => 'Credit Card',
476 $contribution = $this->callAPISuccess('contribution', 'get', array(
478 'id' => $contribution['id'],
480 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
481 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
484 public function testGetContributionByPaymentInstrument() {
485 $params = $this->_params +
array('payment_instrument' => 'EFT');
486 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
487 $this->callAPISuccess('contribution', 'create', $params);
488 $this->callAPISuccess('contribution', 'create', $params2);
489 $contribution = $this->callAPISuccess('contribution', 'get', array(
491 'contribution_payment_instrument' => 'Cash',
493 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
494 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
495 $this->assertEquals(1, $contribution['count']);
496 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
497 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
498 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
499 $this->assertEquals(1, $contribution['count']);
500 $contribution = $this->callAPISuccess('contribution', 'get', array(
502 'payment_instrument_id' => 5,
504 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
505 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
506 $this->assertEquals(1, $contribution['count']);
507 $contribution = $this->callAPISuccess('contribution', 'get', array(
509 'payment_instrument' => 'EFT',
511 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
512 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
513 $this->assertEquals(1, $contribution['count']);
514 $contribution = $this->callAPISuccess('contribution', 'create', array(
515 'id' => $contribution['id'],
516 'payment_instrument' => 'Credit Card',
518 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
519 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
520 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
521 $this->assertEquals(1, $contribution['count']);
525 * CRM-16227 introduces invoice_id as a parameter.
527 public function testGetContributionByInvoice() {
528 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('invoice_id' => 'curly')));
529 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), array('invoice_id' => 'churlish'));
530 $this->callAPISuccessGetCount('Contribution', array(), 2);
531 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
532 // The following don't work. They are the format we are trying to introduce but although the form uses this format
533 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
534 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
535 // come out of convertFormValues
536 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
537 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
538 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
539 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
544 * Create test with unique field name on source.
546 public function testCreateContributionSource() {
549 'contact_id' => $this->_individualId
,
550 'receive_date' => date('Ymd'),
551 'total_amount' => 100.00,
552 'financial_type_id' => $this->_financialTypeId
,
553 'payment_instrument_id' => 1,
554 'non_deductible_amount' => 10.00,
555 'fee_amount' => 50.00,
556 'net_amount' => 90.00,
558 'invoice_id' => 67890,
559 'contribution_source' => 'SSF',
560 'contribution_status_id' => 1,
563 $contribution = $this->callAPISuccess('contribution', 'create', $params);
564 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
565 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
569 * Create test with unique field name on source.
571 public function testCreateDefaultNow() {
573 $params = $this->_params
;
574 unset($params['receive_date']);
576 $contribution = $this->callAPISuccess('contribution', 'create', $params);
577 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
578 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
582 * Create test with unique field name on source.
584 public function testCreateContributionSourceInvalidContact() {
588 'receive_date' => date('Ymd'),
589 'total_amount' => 100.00,
590 'financial_type_id' => $this->_financialTypeId
,
591 'payment_instrument_id' => 1,
592 'non_deductible_amount' => 10.00,
593 'fee_amount' => 50.00,
594 'net_amount' => 90.00,
596 'invoice_id' => 67890,
597 'contribution_source' => 'SSF',
598 'contribution_status_id' => 1,
601 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
604 public function testCreateContributionSourceInvalidContContact() {
607 'contribution_contact_id' => 999,
608 'receive_date' => date('Ymd'),
609 'total_amount' => 100.00,
610 'financial_type_id' => $this->_financialTypeId
,
611 'payment_instrument_id' => 1,
612 'non_deductible_amount' => 10.00,
613 'fee_amount' => 50.00,
614 'net_amount' => 90.00,
616 'invoice_id' => 67890,
617 'contribution_source' => 'SSF',
618 'contribution_status_id' => 1,
621 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
625 * Test note created correctly.
627 public function testCreateContributionWithNote() {
628 $description = "Demonstrates creating contribution with Note Entity.";
629 $subfile = "ContributionCreateWithNote";
631 'contact_id' => $this->_individualId
,
632 'receive_date' => '2012-01-01',
633 'total_amount' => 100.00,
634 'financial_type_id' => $this->_financialTypeId
,
635 'payment_instrument_id' => 1,
636 'non_deductible_amount' => 10.00,
637 'fee_amount' => 50.00,
638 'net_amount' => 90.00,
640 'invoice_id' => 67890,
642 'contribution_status_id' => 1,
643 'note' => 'my contribution note',
646 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
647 $result = $this->callAPISuccess('note', 'get', array(
648 'entity_table' => 'civicrm_contribution',
649 'entity_id' => $contribution['id'],
652 $this->assertEquals('my contribution note', $result['values'][0]['note']);
653 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
656 public function testCreateContributionWithNoteUniqueNameAliases() {
658 'contact_id' => $this->_individualId
,
659 'receive_date' => '2012-01-01',
660 'total_amount' => 100.00,
661 'financial_type_id' => $this->_financialTypeId
,
662 'payment_instrument_id' => 1,
663 'non_deductible_amount' => 10.00,
664 'fee_amount' => 50.00,
665 'net_amount' => 90.00,
667 'invoice_id' => 67890,
669 'contribution_status_id' => 1,
670 'contribution_note' => 'my contribution note',
673 $contribution = $this->callAPISuccess('contribution', 'create', $params);
674 $result = $this->callAPISuccess('note', 'get', array(
675 'entity_table' => 'civicrm_contribution',
676 'entity_id' => $contribution['id'],
679 $this->assertEquals('my contribution note', $result['values'][0]['note']);
680 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
684 * This is the test for creating soft credits.
686 public function testCreateContributionWithSoftCredit() {
687 $description = "Demonstrates creating contribution with SoftCredit.";
688 $subfile = "ContributionCreateWithSoftCredit";
689 $contact2 = $this->callAPISuccess('Contact', 'create', array(
690 'display_name' => 'superman',
691 'contact_type' => 'Individual',
694 'contact_id' => $contact2['id'],
696 'soft_credit_type_id' => 3,
699 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
700 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
701 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
703 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
704 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
705 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
707 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
708 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
711 public function testCreateContributionWithSoftCreditDefaults() {
712 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
713 $subfile = "ContributionCreateWithSoftCreditDefaults";
714 $contact2 = $this->callAPISuccess('Contact', 'create', array(
715 'display_name' => 'superman',
716 'contact_type' => 'Individual',
718 $params = $this->_params +
array(
719 'soft_credit_to' => $contact2['id'],
721 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
722 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
724 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
725 // Default soft credit amount = contribution.total_amount
726 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
727 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
729 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
730 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
733 public function testCreateContributionWithHonoreeContact() {
734 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
735 $subfile = "ContributionCreateWithHonoreeContact";
736 $contact2 = $this->callAPISuccess('Contact', 'create', array(
737 'display_name' => 'superman',
738 'contact_type' => 'Individual',
740 $params = $this->_params +
array(
741 'honor_contact_id' => $contact2['id'],
743 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
744 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
746 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
747 // Default soft credit amount = contribution.total_amount
748 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
749 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
750 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
752 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
753 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
757 * Test using example code.
759 public function testContributionCreateExample() {
760 //make sure at least on page exists since there is a truncate in tear down
761 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
762 require_once 'api/v3/examples/Contribution/Create.php';
763 $result = contribution_create_example();
765 $expectedResult = contribution_create_expectedresult();
766 $this->checkArrayEquals($expectedResult, $result);
767 $this->contributionDelete($id);
771 * Function tests that additional financial records are created when fee amount is recorded.
773 public function testCreateContributionWithFee() {
775 'contact_id' => $this->_individualId
,
776 'receive_date' => '20120511',
777 'total_amount' => 100.00,
779 'financial_type_id' => 1,
781 'invoice_id' => 67890,
783 'contribution_status_id' => 1,
786 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
787 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
788 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
789 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
790 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
791 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
792 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
793 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
794 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
795 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
797 $lineItems = $this->callAPISuccess('line_item', 'get', array(
799 'entity_id' => $contribution['id'],
800 'entity_table' => 'civicrm_contribution',
803 $this->assertEquals(1, $lineItems['count']);
804 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
805 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
806 $lineItems = $this->callAPISuccess('line_item', 'get', array(
808 'entity_id' => $contribution['id'],
809 'contribution_id' => $contribution['id'],
810 'entity_table' => 'civicrm_contribution',
813 $this->assertEquals(1, $lineItems['count']);
814 $this->_checkFinancialRecords($contribution, 'feeAmount');
819 * Function tests that additional financial records are created when online contribution is created.
821 public function testCreateContributionOnline() {
822 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
823 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
824 $this->assertAPISuccess($contributionPage);
826 'contact_id' => $this->_individualId
,
827 'receive_date' => '20120511',
828 'total_amount' => 100.00,
829 'financial_type_id' => 1,
830 'contribution_page_id' => $contributionPage['id'],
831 'payment_processor' => 1,
833 'invoice_id' => 67890,
835 'contribution_status_id' => 1,
839 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
840 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
841 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
842 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
843 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
844 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
845 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
846 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
847 $this->_checkFinancialRecords($contribution, 'online');
851 * Check handling of financial type.
853 * In the interests of removing financial type / contribution type checks from
854 * legacy format function lets test that the api is doing this for us
856 public function testCreateInvalidFinancialType() {
857 $params = $this->_params
;
858 $params['financial_type_id'] = 99999;
859 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
863 * Check handling of financial type.
865 * In the interests of removing financial type / contribution type checks from
866 * legacy format function lets test that the api is doing this for us
868 public function testValidNamedFinancialType() {
869 $params = $this->_params
;
870 $params['financial_type_id'] = 'Donation';
871 $this->callAPISuccess($this->_entity
, 'create', $params);
875 * Tests that additional financial records are created.
877 * Checks when online contribution with pay later option is created
879 public function testCreateContributionPayLaterOnline() {
880 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
881 $this->_pageParams
['is_pay_later'] = 1;
882 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
883 $this->assertAPISuccess($contributionPage);
885 'contact_id' => $this->_individualId
,
886 'receive_date' => '20120511',
887 'total_amount' => 100.00,
888 'financial_type_id' => 1,
889 'contribution_page_id' => $contributionPage['id'],
892 'invoice_id' => 67890,
894 'contribution_status_id' => 2,
898 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
899 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
900 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
901 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
902 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
903 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
904 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
905 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
906 $this->_checkFinancialRecords($contribution, 'payLater');
910 * Function tests that additional financial records are created for online contribution with pending option.
912 public function testCreateContributionPendingOnline() {
913 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
914 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
915 $this->assertAPISuccess($contributionPage);
917 'contact_id' => $this->_individualId
,
918 'receive_date' => '20120511',
919 'total_amount' => 100.00,
920 'financial_type_id' => 1,
921 'contribution_page_id' => $contributionPage['id'],
923 'invoice_id' => 67890,
925 'contribution_status_id' => 2,
928 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
929 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
930 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
931 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
932 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
933 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
934 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
935 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
936 $this->_checkFinancialRecords($contribution, 'pending');
940 * Test that BAO defaults work.
942 public function testCreateBAODefaults() {
943 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
944 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
945 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
946 'id' => $contribution['id'],
947 'api.contribution.delete' => 1,
949 $this->assertEquals(1, $contribution['contribution_status_id']);
950 $this->assertEquals('Check', $contribution['payment_instrument']);
954 * Function tests that line items, financial records are updated when contribution amount is changed.
956 public function testCreateUpdateContributionChangeTotal() {
957 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
958 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
960 'entity_id' => $contribution['id'],
961 'entity_table' => 'civicrm_contribution',
963 'return' => 'line_total',
965 $this->assertEquals('100.00', $lineItems);
966 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
967 // Financial trxn SUM = 100 + 5 (fee)
968 $this->assertEquals('105.00', $trxnAmount);
971 'id' => $contribution['id'],
972 'total_amount' => '125',
974 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
976 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
978 'entity_id' => $contribution['id'],
979 'entity_table' => 'civicrm_contribution',
981 'return' => 'line_total',
984 $this->assertEquals('125.00', $lineItems);
985 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
987 // Financial trxn SUM = 125 + 5 (fee).
988 $this->assertEquals('130.00', $trxnAmount);
989 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
993 * Function tests that line items, financial records are updated when pay later contribution is received.
995 public function testCreateUpdateContributionPayLater() {
996 $contribParams = array(
997 'contact_id' => $this->_individualId
,
998 'receive_date' => '2012-01-01',
999 'total_amount' => 100.00,
1000 'financial_type_id' => $this->_financialTypeId
,
1001 'payment_instrument_id' => 1,
1002 'contribution_status_id' => 2,
1003 'is_pay_later' => 1,
1006 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1008 $newParams = array_merge($contribParams, array(
1009 'id' => $contribution['id'],
1010 'contribution_status_id' => 1,
1013 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1014 $contribution = $contribution['values'][$contribution['id']];
1015 $this->assertEquals($contribution['contribution_status_id'], '1');
1016 $this->_checkFinancialItem($contribution['id'], 'paylater');
1017 $this->_checkFinancialTrxn($contribution, 'payLater');
1021 * Function tests that financial records are updated when Payment Instrument is changed.
1023 public function testCreateUpdateContributionPaymentInstrument() {
1024 $instrumentId = $this->_addPaymentInstrument();
1025 $contribParams = array(
1026 'contact_id' => $this->_individualId
,
1027 'total_amount' => 100.00,
1028 'financial_type_id' => $this->_financialTypeId
,
1029 'payment_instrument_id' => 4,
1030 'contribution_status_id' => 1,
1033 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1035 $newParams = array_merge($contribParams, array(
1036 'id' => $contribution['id'],
1037 'payment_instrument_id' => $instrumentId,
1040 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1041 $this->assertAPISuccess($contribution);
1042 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1046 * Function tests that financial records are added when Contribution is Refunded.
1048 public function testCreateUpdateContributionRefund() {
1049 $contributionParams = array(
1050 'contact_id' => $this->_individualId
,
1051 'receive_date' => '2012-01-01',
1052 'total_amount' => 100.00,
1053 'financial_type_id' => $this->_financialTypeId
,
1054 'payment_instrument_id' => 4,
1055 'contribution_status_id' => 1,
1056 'trxn_id' => 'original_payment',
1058 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1059 $newParams = array_merge($contributionParams, array(
1060 'id' => $contribution['id'],
1061 'contribution_status_id' => 'Refunded',
1062 'cancel_date' => '2015-01-01 09:00',
1063 'refund_trxn_id' => 'the refund',
1067 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1068 $this->_checkFinancialTrxn($contribution, 'refund');
1069 $this->_checkFinancialItem($contribution['id'], 'refund');
1070 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1071 'id' => $contribution['id'],
1072 'return' => 'trxn_id',
1077 * Refund a contribution for a financial type with a contra account.
1079 * CRM-17951 the contra account is a financial account with a relationship to a
1080 * financial type. It is not always configured but should be reflected
1081 * in the financial_trxn & financial_item table if it is.
1083 public function testCreateUpdateChargebackContributionDefaultAccount() {
1084 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1085 $this->callAPISuccess('Contribution', 'create', array(
1086 'id' => $contribution['id'],
1087 'contribution_status_id' => 'Chargeback',
1089 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1091 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1092 'contribution_id' => $contribution['id'],
1093 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1095 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1096 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1097 'total_amount' => -100,
1098 'status_id' => 'Chargeback',
1099 'to_financial_account_id' => 6,
1104 * Refund a contribution for a financial type with a contra account.
1106 * CRM-17951 the contra account is a financial account with a relationship to a
1107 * financial type. It is not always configured but should be reflected
1108 * in the financial_trxn & financial_item table if it is.
1110 public function testCreateUpdateChargebackContributionCustomAccount() {
1111 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1112 'name' => 'Chargeback Account',
1113 'is_active' => TRUE,
1116 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1117 'entity_id' => $this->_financialTypeId
,
1118 'entity_table' => 'civicrm_financial_type',
1119 'account_relationship' => 'Chargeback Account is',
1120 'financial_account_id' => 'Chargeback Account',
1123 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1124 $this->callAPISuccess('Contribution', 'create', array(
1125 'id' => $contribution['id'],
1126 'contribution_status_id' => 'Chargeback',
1128 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1130 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1131 'contribution_id' => $contribution['id'],
1132 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1134 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1136 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1137 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1138 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1142 * Refund a contribution for a financial type with a contra account.
1144 * CRM-17951 the contra account is a financial account with a relationship to a
1145 * financial type. It is not always configured but should be reflected
1146 * in the financial_trxn & financial_item table if it is.
1148 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1149 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1150 'name' => 'Refund Account',
1151 'is_active' => TRUE,
1154 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1155 'entity_id' => $this->_financialTypeId
,
1156 'entity_table' => 'civicrm_financial_type',
1157 'account_relationship' => 'Credit/Contra Revenue Account is',
1158 'financial_account_id' => 'Refund Account',
1161 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1162 $this->callAPISuccess('Contribution', 'create', array(
1163 'id' => $contribution['id'],
1164 'contribution_status_id' => 'Refunded',
1167 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1168 'contribution_id' => $contribution['id'],
1169 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1171 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1173 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1174 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1175 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1179 * Function tests that trxn_id is set when passed in.
1181 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1182 * when trxn_id is passed in.
1184 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1185 $contributionParams = array(
1186 'contact_id' => $this->_individualId
,
1187 'receive_date' => '2012-01-01',
1188 'total_amount' => 100.00,
1189 'financial_type_id' => $this->_financialTypeId
,
1190 'payment_instrument_id' => 4,
1191 'contribution_status_id' => 1,
1192 'trxn_id' => 'original_payment',
1194 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1195 $newParams = array_merge($contributionParams, array(
1196 'id' => $contribution['id'],
1197 'contribution_status_id' => 'Refunded',
1198 'cancel_date' => '2015-01-01 09:00',
1199 'trxn_id' => 'the refund',
1203 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1204 $this->_checkFinancialTrxn($contribution, 'refund');
1205 $this->_checkFinancialItem($contribution['id'], 'refund');
1206 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1207 'id' => $contribution['id'],
1208 'return' => 'trxn_id',
1213 * Function tests that trxn_id is set when passed in.
1215 * Here we ensure that the civicrm_contribution.trxn_id is set
1216 * when trxn_id is passed in but if refund_trxn_id is different then that
1217 * is kept for the refund transaction.
1219 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1220 $contributionParams = array(
1221 'contact_id' => $this->_individualId
,
1222 'receive_date' => '2012-01-01',
1223 'total_amount' => 100.00,
1224 'financial_type_id' => $this->_financialTypeId
,
1225 'payment_instrument_id' => 4,
1226 'contribution_status_id' => 1,
1227 'trxn_id' => 'original_payment',
1229 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1230 $newParams = array_merge($contributionParams, array(
1231 'id' => $contribution['id'],
1232 'contribution_status_id' => 'Refunded',
1233 'cancel_date' => '2015-01-01 09:00',
1234 'trxn_id' => 'cont id',
1235 'refund_trxn_id' => 'the refund',
1239 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1240 $this->_checkFinancialTrxn($contribution, 'refund');
1241 $this->_checkFinancialItem($contribution['id'], 'refund');
1242 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1243 'id' => $contribution['id'],
1244 'return' => 'trxn_id',
1249 * Function tests that refund_trxn_id is set when passed in empty.
1251 * Here we ensure that the civicrm_contribution.trxn_id is set
1252 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1253 * is kept for the refund transaction.
1255 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1256 $contributionParams = array(
1257 'contact_id' => $this->_individualId
,
1258 'receive_date' => '2012-01-01',
1259 'total_amount' => 100.00,
1260 'financial_type_id' => $this->_financialTypeId
,
1261 'payment_instrument_id' => 4,
1262 'contribution_status_id' => 1,
1263 'trxn_id' => 'original_payment',
1265 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1266 $newParams = array_merge($contributionParams, array(
1267 'id' => $contribution['id'],
1268 'contribution_status_id' => 'Refunded',
1269 'cancel_date' => '2015-01-01 09:00',
1270 'trxn_id' => 'cont id',
1271 'refund_trxn_id' => '',
1275 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1276 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1277 $this->_checkFinancialItem($contribution['id'], 'refund');
1278 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1279 'id' => $contribution['id'],
1280 'return' => 'trxn_id',
1285 * Function tests invalid contribution status change.
1287 public function testCreateUpdateContributionInValidStatusChange() {
1288 $contribParams = array(
1290 'receive_date' => '2012-01-01',
1291 'total_amount' => 100.00,
1292 'financial_type_id' => 1,
1293 'payment_instrument_id' => 1,
1294 'contribution_status_id' => 1,
1296 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1297 $newParams = array_merge($contribParams, array(
1298 'id' => $contribution['id'],
1299 'contribution_status_id' => 2,
1302 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1307 * Function tests that financial records are added when Pending Contribution is Canceled.
1309 public function testCreateUpdateContributionCancelPending() {
1310 $contribParams = array(
1311 'contact_id' => $this->_individualId
,
1312 'receive_date' => '2012-01-01',
1313 'total_amount' => 100.00,
1314 'financial_type_id' => $this->_financialTypeId
,
1315 'payment_instrument_id' => 1,
1316 'contribution_status_id' => 2,
1317 'is_pay_later' => 1,
1320 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1321 $newParams = array_merge($contribParams, array(
1322 'id' => $contribution['id'],
1323 'contribution_status_id' => 3,
1326 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1327 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1328 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1332 * Function tests that financial records are added when Financial Type is Changed.
1334 public function testCreateUpdateContributionChangeFinancialType() {
1335 $contribParams = array(
1336 'contact_id' => $this->_individualId
,
1337 'receive_date' => '2012-01-01',
1338 'total_amount' => 100.00,
1339 'financial_type_id' => 1,
1340 'payment_instrument_id' => 1,
1341 'contribution_status_id' => 1,
1344 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1345 $newParams = array_merge($contribParams, array(
1346 'id' => $contribution['id'],
1347 'financial_type_id' => 3,
1350 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1351 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1352 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1356 * Test that update does not change status id CRM-15105.
1358 public function testCreateUpdateWithoutChangingPendingStatus() {
1359 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1360 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1361 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1362 'id' => $contribution['id'],
1363 'api.contribution.delete' => 1,
1365 $this->assertEquals(2, $contribution['contribution_status_id']);
1369 * Test Updating a Contribution.
1371 * CHANGE: we require the API to do an incremental update
1373 public function testCreateUpdateContribution() {
1375 $contributionID = $this->contributionCreate(array(
1376 'contact_id' => $this->_individualId
,
1377 'trxn_id' => 212355,
1378 'financial_type_id' => $this->_financialTypeId
,
1379 'invoice_id' => 'old_invoice',
1381 $old_params = array(
1382 'contribution_id' => $contributionID,
1384 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1385 $this->assertEquals($original['id'], $contributionID);
1386 //set up list of old params, verify
1388 //This should not be required on update:
1389 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1390 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1391 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1392 $old_source = $original['values'][$contributionID]['contribution_source'];
1394 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1395 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1397 //check against values in CiviUnitTestCase::createContribution()
1398 $this->assertEquals($old_contact_id, $this->_individualId
);
1399 $this->assertEquals($old_fee_amount, 5.00);
1400 $this->assertEquals($old_source, 'SSF');
1401 $this->assertEquals($old_trxn_id, 212355);
1402 $this->assertEquals($old_invoice_id, 'old_invoice');
1404 'id' => $contributionID,
1405 'contact_id' => $this->_individualId
,
1406 'total_amount' => 110.00,
1407 'financial_type_id' => $this->_financialTypeId
,
1408 'non_deductible_amount' => 10.00,
1409 'net_amount' => 100.00,
1410 'contribution_status_id' => 1,
1411 'note' => 'Donating for Nobel Cause',
1415 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1417 $new_params = array(
1418 'contribution_id' => $contribution['id'],
1421 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1423 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1424 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1425 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId
);
1426 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1427 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1428 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1429 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1430 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1431 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1432 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1433 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1435 'contribution_id' => $contributionID,
1438 $result = $this->callAPISuccess('contribution', 'delete', $params);
1439 $this->assertAPISuccess($result);
1442 ///////////////// civicrm_contribution_delete methods
1445 * Attempt (but fail) to delete a contribution without parameters.
1447 public function testDeleteEmptyParamsContribution() {
1449 $this->callAPIFailure('contribution', 'delete', $params);
1452 public function testDeleteParamsNotArrayContribution() {
1453 $params = 'contribution_id= 1';
1454 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1455 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1458 public function testDeleteWrongParamContribution() {
1460 'contribution_source' => 'SSF',
1463 $this->callAPIFailure('contribution', 'delete', $params);
1466 public function testDeleteContribution() {
1467 $contributionID = $this->contributionCreate(array(
1468 'contact_id' => $this->_individualId
,
1469 'financial_type_id' => $this->_financialTypeId
,
1472 'id' => $contributionID,
1474 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1478 * Test civicrm_contribution_search with empty params.
1480 * All available contributions expected.
1482 public function testSearchEmptyParams() {
1486 'contact_id' => $this->_individualId
,
1487 'receive_date' => date('Ymd'),
1488 'total_amount' => 100.00,
1489 'financial_type_id' => $this->_financialTypeId
,
1490 'non_deductible_amount' => 10.00,
1491 'fee_amount' => 5.00,
1492 'net_amount' => 95.00,
1494 'invoice_id' => 78910,
1496 'contribution_status_id' => 1,
1499 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1501 $result = $this->callAPISuccess('contribution', 'get', $params);
1502 // We're taking the first element.
1503 $res = $result['values'][$contribution['id']];
1505 $this->assertEquals($p['contact_id'], $res['contact_id']);
1506 $this->assertEquals($p['total_amount'], $res['total_amount']);
1507 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1508 $this->assertEquals($p['net_amount'], $res['net_amount']);
1509 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1510 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1511 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1512 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1513 $this->assertEquals($p['source'], $res['contribution_source']);
1514 // contribution_status_id = 1 => Completed
1515 $this->assertEquals('Completed', $res['contribution_status']);
1517 $this->contributionDelete($contribution['id']);
1521 * Test civicrm_contribution_search. Success expected.
1523 public function testSearch() {
1525 'contact_id' => $this->_individualId
,
1526 'receive_date' => date('Ymd'),
1527 'total_amount' => 100.00,
1528 'financial_type_id' => $this->_financialTypeId
,
1529 'non_deductible_amount' => 10.00,
1530 'contribution_status_id' => 1,
1533 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1536 'contact_id' => $this->_individualId
,
1537 'receive_date' => date('Ymd'),
1538 'total_amount' => 200.00,
1539 'financial_type_id' => $this->_financialTypeId
,
1540 'non_deductible_amount' => 20.00,
1541 'trxn_id' => 5454565,
1542 'invoice_id' => 1212124,
1543 'fee_amount' => 50.00,
1544 'net_amount' => 60.00,
1545 'contribution_status_id' => 2,
1548 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1551 'contribution_id' => $contribution2['id'],
1554 $result = $this->callAPISuccess('contribution', 'get', $params);
1555 $res = $result['values'][$contribution2['id']];
1557 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1558 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1559 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1560 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1561 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1562 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1563 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1564 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1565 // contribution_status_id = 2 => Pending
1566 $this->assertEquals('Pending', $res['contribution_status']);
1568 $this->contributionDelete($contribution1['id']);
1569 $this->contributionDelete($contribution2['id']);
1573 * Test completing a transaction via the API.
1575 * Note that we are creating a logged in user because email goes out from
1578 public function testCompleteTransaction() {
1579 $mut = new CiviMailUtils($this, TRUE);
1580 $this->swapMessageTemplateForTestTemplate();
1581 $this->createLoggedInUser();
1582 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1583 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1584 $this->callAPISuccess('contribution', 'completetransaction', array(
1585 'id' => $contribution['id'],
1587 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1588 $this->assertEquals('SSF', $contribution['contribution_source']);
1589 $this->assertEquals('Completed', $contribution['contribution_status']);
1590 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1591 $mut->checkMailLog(array(
1592 'email:::anthony_anderson@civicrm.org',
1596 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1597 "receipt_date:::\n",
1598 'contributeMode:::notify',
1599 'title:::Contribution',
1600 'displayName:::Mr. Anthony Anderson II',
1603 $this->revertTemplateToReservedTemplate();
1607 * Test to check whether contact billing address is used when no contribution address
1609 public function testBillingAddress() {
1610 $mut = new CiviMailUtils($this, TRUE);
1611 $this->swapMessageTemplateForTestTemplate();
1612 $this->createLoggedInUser();
1614 //Scenario 1: When Contact don't have any address
1615 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1616 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1617 $this->callAPISuccess('contribution', 'completetransaction', array(
1618 'id' => $contribution['id'],
1620 $mut->checkMailLog(array(
1624 // Scenario 2: Contribution using address
1625 $address = $this->callAPISuccess('address', 'create', array(
1626 'street_address' => 'contribution billing st',
1627 'location_type_id' => 2,
1628 'contact_id' => $this->_params
['contact_id'],
1630 $params = array_merge($this->_params
, array(
1631 'contribution_status_id' => 2,
1632 'address_id' => $address['id'],
1635 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1636 $this->callAPISuccess('contribution', 'completetransaction', array(
1637 'id' => $contribution['id'],
1639 $mut->checkMailLog(array(
1640 'address:::contribution billing st',
1643 // Scenario 3: Contribution wtth no address but contact has a billing address
1644 $this->callAPISuccess('address', 'create', array(
1645 'id' => $address['id'],
1646 'street_address' => 'is billing st',
1647 'contact_id' => $this->_params
['contact_id'],
1649 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1650 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1651 $this->callAPISuccess('contribution', 'completetransaction', array(
1652 'id' => $contribution['id'],
1654 $mut->checkMailLog(array(
1655 'address:::is billing st',
1659 $this->revertTemplateToReservedTemplate();
1663 * Test completing a transaction via the API.
1665 * Note that we are creating a logged in user because email goes out from
1668 public function testCompleteTransactionFeeAmount() {
1669 $this->createLoggedInUser();
1670 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1671 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1672 $this->callAPISuccess('contribution', 'completetransaction', array(
1673 'id' => $contribution['id'],
1674 'fee_amount' => '.56',
1675 'trxn_id' => '7778888',
1677 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1678 $this->assertEquals('Completed', $contribution['contribution_status']);
1679 $this->assertEquals('7778888', $contribution['trxn_id']);
1680 $this->assertEquals('.56', $contribution['fee_amount']);
1681 $this->assertEquals('99.44', $contribution['net_amount']);
1685 * Test repeat contribution successfully creates line items.
1687 public function testRepeatTransaction() {
1688 $originalContribution = $this->setUpRepeatTransaction();
1690 $this->callAPISuccess('contribution', 'repeattransaction', array(
1691 'original_contribution_id' => $originalContribution['id'],
1692 'contribution_status_id' => 'Completed',
1693 'trxn_id' => uniqid(),
1695 $lineItemParams = array(
1696 'entity_id' => $originalContribution['id'],
1704 'financial_type_id',
1705 'deductible_amount',
1706 'price_field_value_id',
1710 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1711 'entity_id' => $originalContribution['id'],
1713 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1714 'entity_id' => $originalContribution['id'] +
1,
1716 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1717 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1718 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1720 $this->quickCleanUpFinancialEntities();
1724 * Test repeat contribution successfully creates line items.
1726 public function testRepeatTransactionIsTest() {
1727 $this->_params
['is_test'] = 1;
1728 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1));
1730 $this->callAPISuccess('contribution', 'repeattransaction', array(
1731 'original_contribution_id' => $originalContribution['id'],
1732 'contribution_status_id' => 'Completed',
1733 'trxn_id' => uniqid(),
1735 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
1739 * Test repeat contribution passed in status.
1741 public function testRepeatTransactionPassedInStatus() {
1742 $originalContribution = $this->setUpRepeatTransaction();
1744 $this->callAPISuccess('contribution', 'repeattransaction', array(
1745 'original_contribution_id' => $originalContribution['id'],
1746 'contribution_status_id' => 'Pending',
1747 'trxn_id' => uniqid(),
1749 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
1753 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1755 public function testRepeatTransactionAcceptRecurID() {
1756 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1757 'contact_id' => $this->_individualId
,
1758 'installments' => '12',
1759 'frequency_interval' => '1',
1761 'contribution_status_id' => 1,
1762 'start_date' => '2012-01-01 00:00:00',
1763 'currency' => 'USD',
1764 'frequency_unit' => 'month',
1765 'payment_processor_id' => $this->paymentProcessorID
,
1767 $this->callAPISuccess('contribution', 'create', array_merge(
1769 array('contribution_recur_id' => $contributionRecur['id']))
1772 $this->callAPISuccess('contribution', 'repeattransaction', array(
1773 'contribution_recur_id' => $contributionRecur['id'],
1774 'contribution_status_id' => 'Completed',
1775 'trxn_id' => uniqid(),
1778 $this->quickCleanUpFinancialEntities();
1782 * CRM-16397 test appropriate action if total amount has changed for single line items.
1784 public function testRepeatTransactionAlteredAmount() {
1785 $paymentProcessorID = $this->paymentProcessorCreate();
1786 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1787 'contact_id' => $this->_individualId
,
1788 'installments' => '12',
1789 'frequency_interval' => '1',
1791 'contribution_status_id' => 1,
1792 'start_date' => '2012-01-01 00:00:00',
1793 'currency' => 'USD',
1794 'frequency_unit' => 'month',
1795 'payment_processor_id' => $paymentProcessorID,
1797 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1800 'contribution_recur_id' => $contributionRecur['id'],
1804 $this->callAPISuccess('contribution', 'repeattransaction', array(
1805 'original_contribution_id' => $originalContribution['id'],
1806 'contribution_status_id' => 'Completed',
1807 'trxn_id' => uniqid(),
1808 'total_amount' => '400',
1811 $lineItemParams = array(
1812 'entity_id' => $originalContribution['id'],
1820 'financial_type_id',
1821 'deductible_amount',
1822 'price_field_value_id',
1826 $this->callAPISuccessGetSingle('contribution', array(
1827 'total_amount' => 400,
1829 'net_amount' => 350,
1831 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1832 'entity_id' => $originalContribution['id'],
1834 $expectedLineItem = array_merge(
1835 $lineItem1['values'][0], array(
1836 'line_total' => '400.00',
1837 'unit_price' => '400.00',
1841 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1842 'entity_id' => $originalContribution['id'] +
1,
1844 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1845 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1846 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1850 * CRM-17718 test appropriate action if financial type has changed for single line items.
1852 public function testRepeatTransactionPassedInFinancialType() {
1853 $originalContribution = $this->setUpRecurringContribution();
1855 $this->callAPISuccess('contribution', 'repeattransaction', array(
1856 'original_contribution_id' => $originalContribution['id'],
1857 'contribution_status_id' => 'Completed',
1858 'trxn_id' => uniqid(),
1859 'financial_type_id' => 2,
1861 $lineItemParams = array(
1862 'entity_id' => $originalContribution['id'],
1870 'financial_type_id',
1871 'deductible_amount',
1872 'price_field_value_id',
1877 $this->callAPISuccessGetSingle('contribution', array(
1878 'total_amount' => 100,
1879 'financial_type_id' => 2,
1881 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1882 'entity_id' => $originalContribution['id'],
1884 $expectedLineItem = array_merge(
1885 $lineItem1['values'][0], array(
1886 'line_total' => '100.00',
1887 'unit_price' => '100.00',
1888 'financial_type_id' => 2,
1892 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1893 'entity_id' => $originalContribution['id'] +
1,
1895 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1896 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1897 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1901 * CRM-17718 test appropriate action if financial type has changed for single line items.
1903 public function testRepeatTransactionUpdatedFinancialType() {
1904 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
1906 $this->callAPISuccess('contribution', 'repeattransaction', array(
1907 'contribution_recur_id' => $originalContribution['id'],
1908 'contribution_status_id' => 'Completed',
1909 'trxn_id' => uniqid(),
1911 $lineItemParams = array(
1912 'entity_id' => $originalContribution['id'],
1920 'financial_type_id',
1921 'deductible_amount',
1922 'price_field_value_id',
1927 $this->callAPISuccessGetSingle('contribution', array(
1928 'total_amount' => 100,
1929 'financial_type_id' => 2,
1931 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1932 'entity_id' => $originalContribution['id'],
1934 $expectedLineItem = array_merge(
1935 $lineItem1['values'][0], array(
1936 'line_total' => '100.00',
1937 'unit_price' => '100.00',
1938 'financial_type_id' => 2,
1942 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1943 'entity_id' => $originalContribution['id'] +
1,
1945 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1946 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1947 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1951 * CRM-16397 test appropriate action if campaign has been passed in.
1953 public function testRepeatTransactionPassedInCampaign() {
1954 $paymentProcessorID = $this->paymentProcessorCreate();
1955 $campaignID = $this->campaignCreate();
1956 $campaignID2 = $this->campaignCreate();
1957 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1958 'contact_id' => $this->_individualId
,
1959 'installments' => '12',
1960 'frequency_interval' => '1',
1962 'contribution_status_id' => 1,
1963 'start_date' => '2012-01-01 00:00:00',
1964 'currency' => 'USD',
1965 'frequency_unit' => 'month',
1966 'payment_processor_id' => $paymentProcessorID,
1968 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1971 'contribution_recur_id' => $contributionRecur['id'],
1972 'campaign_id' => $campaignID,
1976 $this->callAPISuccess('contribution', 'repeattransaction', array(
1977 'original_contribution_id' => $originalContribution['id'],
1978 'contribution_status_id' => 'Completed',
1979 'trxn_id' => uniqid(),
1980 'campaign_id' => $campaignID2,
1983 $this->callAPISuccessGetSingle('contribution', array(
1984 'total_amount' => 100,
1985 'campaign_id' => $campaignID2,
1990 * CRM-17718 campaign stored on contribution recur gets priority.
1992 * This reflects the fact we permit people to update them.
1994 public function testRepeatTransactionUpdatedCampaign() {
1995 $paymentProcessorID = $this->paymentProcessorCreate();
1996 $campaignID = $this->campaignCreate();
1997 $campaignID2 = $this->campaignCreate();
1998 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1999 'contact_id' => $this->_individualId
,
2000 'installments' => '12',
2001 'frequency_interval' => '1',
2003 'contribution_status_id' => 1,
2004 'start_date' => '2012-01-01 00:00:00',
2005 'currency' => 'USD',
2006 'frequency_unit' => 'month',
2007 'payment_processor_id' => $paymentProcessorID,
2008 'campaign_id' => $campaignID,
2010 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2013 'contribution_recur_id' => $contributionRecur['id'],
2014 'campaign_id' => $campaignID2,
2018 $this->callAPISuccess('contribution', 'repeattransaction', array(
2019 'original_contribution_id' => $originalContribution['id'],
2020 'contribution_status_id' => 'Completed',
2021 'trxn_id' => uniqid(),
2024 $this->callAPISuccessGetSingle('contribution', array(
2025 'total_amount' => 100,
2026 'campaign_id' => $campaignID,
2031 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2033 public function testCompleteTransactionNetAmountOK() {
2034 $this->createLoggedInUser();
2035 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2036 unset($params['net_amount']);
2037 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2038 $this->callAPISuccess('contribution', 'completetransaction', array(
2039 'id' => $contribution['id'],
2041 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2042 $this->assertEquals('Completed', $contribution['contribution_status']);
2043 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2047 * CRM-14151 - Test completing a transaction via the API.
2049 public function testCompleteTransactionWithReceiptDateSet() {
2050 $this->swapMessageTemplateForTestTemplate();
2051 $mut = new CiviMailUtils($this, TRUE);
2052 $this->createLoggedInUser();
2053 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2054 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2055 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2056 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2057 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2058 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2059 $mut->checkMailLog(array(
2060 'Receipt - Contribution',
2061 'receipt_date:::' . date('Ymd'),
2064 $this->revertTemplateToReservedTemplate();
2069 * Complete the transaction using the template with all the possible.
2071 public function testCompleteTransactionWithTestTemplate() {
2072 $this->swapMessageTemplateForTestTemplate();
2073 $contribution = $this->setUpForCompleteTransaction();
2074 $this->callAPISuccess('contribution', 'completetransaction', array(
2075 'id' => $contribution['id'],
2076 'trxn_date' => date('2011-04-09'),
2077 'trxn_id' => 'kazam',
2079 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2080 $this->mut
->checkMailLog(array(
2081 'email:::anthony_anderson@civicrm.org',
2085 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2086 'receipt_date:::' . date('Ymd'),
2087 'contributeMode:::notify',
2088 'title:::Contribution',
2089 'displayName:::Mr. Anthony Anderson II',
2091 'contactID:::' . $this->_params
['contact_id'],
2092 'contributionID:::' . $contribution['id'],
2093 'financialTypeId:::1',
2094 'financialTypeName:::Donation',
2097 $this->revertTemplateToReservedTemplate();
2101 * Complete the transaction using the template with all the possible.
2103 public function testCompleteTransactionContributionPageFromAddress() {
2104 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2105 'receipt_from_name' => 'Mickey Mouse',
2106 'receipt_from_email' => 'mickey@mouse.com',
2107 'title' => "Test Contribution Page",
2108 'financial_type_id' => 1,
2109 'currency' => 'NZD',
2110 'goal_amount' => 50,
2111 'is_pay_later' => 1,
2112 'is_monetary' => TRUE,
2113 'is_email_receipt' => TRUE,
2115 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2116 $contribution = $this->setUpForCompleteTransaction();
2117 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2118 $this->mut
->checkMailLog(array(
2126 * Test completing first transaction in a recurring series.
2128 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2130 public function testCompleteTransactionSetStatusToInProgress() {
2131 $paymentProcessorID = $this->paymentProcessorCreate();
2132 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2133 'contact_id' => $this->_individualId
,
2134 'installments' => '12',
2135 'frequency_interval' => '1',
2137 'contribution_status_id' => 'Pending',
2138 'start_date' => '2012-01-01 00:00:00',
2139 'currency' => 'USD',
2140 'frequency_unit' => 'month',
2141 'payment_processor_id' => $paymentProcessorID,
2143 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2146 'contribution_recur_id' => $contributionRecur['id'],
2147 'contribution_status_id' => 'Pending',
2150 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2151 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2152 'id' => $contributionRecur['id'],
2153 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2155 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2156 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2160 * Test completing a pledge with the completeTransaction api..
2162 * Note that we are creating a logged in user because email goes out from
2165 public function testCompleteTransactionUpdatePledgePayment() {
2166 $this->swapMessageTemplateForTestTemplate();
2167 $mut = new CiviMailUtils($this, TRUE);
2168 $mut->clearMessages();
2169 $this->createLoggedInUser();
2170 $contributionID = $this->createPendingPledgeContribution();
2171 $this->callAPISuccess('contribution', 'completetransaction', array(
2172 'id' => $contributionID,
2173 'trxn_date' => '1 Feb 2013',
2175 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2176 'id' => $this->_ids
['pledge'],
2178 $this->assertEquals('Completed', $pledge['pledge_status']);
2180 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2181 'pledge_id' => $this->_ids
['pledge'],
2182 'return' => 'status_id',
2184 $this->assertEquals(1, $status);
2185 $mut->checkMailLog(array(
2187 'receive_date:::20130201000000',
2188 "receipt_date:::\n",
2191 $this->revertTemplateToReservedTemplate();
2195 * Test completing a transaction with an event via the API.
2197 * Note that we are creating a logged in user because email goes out from
2200 public function testCompleteTransactionWithParticipantRecord() {
2201 $mut = new CiviMailUtils($this, TRUE);
2202 $mut->clearMessages();
2203 $this->createLoggedInUser();
2204 $contributionID = $this->createPendingParticipantContribution();
2205 $this->callAPISuccess('contribution', 'completetransaction', array(
2206 'id' => $contributionID,
2209 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2210 'id' => $this->_ids
['participant'],
2211 'return' => 'participant_status_id',
2213 $this->assertEquals(1, $participantStatus);
2214 $mut->checkMailLog(array(
2215 'Annual CiviCRM meet',
2217 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2223 * Test membership is renewed when transaction completed.
2225 public function testCompleteTransactionMembershipPriceSet() {
2226 $this->createPriceSetWithPage('membership');
2227 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2231 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2232 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2233 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2234 'membership_id' => $this->_ids
['membership'],
2236 $this->assertEquals(1, $logs['count']);
2237 $this->assertEquals($stateOfGrace, $membership['status_id']);
2238 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2239 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2240 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2241 $this->callAPISuccessGetSingle('LineItem', array(
2242 'entity_id' => $this->_ids
['membership'],
2243 'entity_table' => 'civicrm_membership',
2245 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2246 'membership_id' => $this->_ids
['membership'],
2248 $this->assertEquals(2, $logs['count']);
2249 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2250 $this->cleanUpAfterPriceSets();
2254 * Test membership is renewed when transaction completed.
2256 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2257 $this->createPriceSetWithPage('membership');
2258 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
2259 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2260 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2261 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2262 $this->cleanUpAfterPriceSets();
2265 public function cleanUpAfterPriceSets() {
2266 $this->quickCleanUpFinancialEntities();
2267 $this->contactDelete($this->_ids
['contact']);
2272 * Create price set with contribution test for test setup.
2274 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
2275 * on parent class at some point (fn is not in 4.4).
2278 * @param array $params
2280 public function createPriceSetWithPage($entity, $params = array()) {
2281 $membershipTypeID = $this->membershipTypeCreate();
2282 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
2283 'title' => "Test Contribution Page",
2284 'financial_type_id' => 1,
2285 'currency' => 'NZD',
2286 'goal_amount' => 50,
2287 'is_pay_later' => 1,
2288 'is_monetary' => TRUE,
2289 'is_email_receipt' => FALSE,
2291 $priceSet = $this->callAPISuccess('price_set', 'create', array(
2292 'is_quick_config' => 0,
2293 'extends' => 'CiviMember',
2294 'financial_type_id' => 1,
2295 'title' => 'my Page',
2297 $priceSetID = $priceSet['id'];
2299 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
2300 $priceField = $this->callAPISuccess('price_field', 'create', array(
2301 'price_set_id' => $priceSetID,
2302 'label' => 'Goat Breed',
2303 'html_type' => 'Radio',
2305 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2306 'price_set_id' => $priceSetID,
2307 'price_field_id' => $priceField['id'],
2308 'label' => 'Long Haired Goat',
2310 'financial_type_id' => 'Donation',
2311 'membership_type_id' => $membershipTypeID,
2312 'membership_num_terms' => 1,
2315 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
2316 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2317 'price_set_id' => $priceSetID,
2318 'price_field_id' => $priceField['id'],
2319 'label' => 'Shoe-eating Goat',
2321 'financial_type_id' => 'Donation',
2322 'membership_type_id' => $membershipTypeID,
2323 'membership_num_terms' => 2,
2326 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
2327 $this->_ids
['price_set'] = $priceSetID;
2328 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
2329 $this->_ids
['price_field'] = array($priceField['id']);
2331 $this->_ids
['membership_type'] = $membershipTypeID;
2335 * Set up a pending transaction with a specific price field id.
2337 * @param int $priceFieldValueID
2339 public function setUpPendingContribution($priceFieldValueID) {
2340 $contactID = $this->individualCreate();
2341 $membership = $this->callAPISuccess('membership', 'create', array(
2342 'contact_id' => $contactID,
2343 'membership_type_id' => $this->_ids
['membership_type'],
2344 'start_date' => 'yesterday - 1 year',
2345 'end_date' => 'yesterday',
2346 'join_date' => 'yesterday - 1 year',
2348 $contribution = $this->callAPISuccess('contribution', 'create', array(
2350 'contact_id' => $contactID,
2351 'receive_date' => date('Ymd'),
2352 'total_amount' => 100.00,
2353 'financial_type_id' => 1,
2354 'payment_instrument_id' => 'Credit Card',
2355 'non_deductible_amount' => 10.00,
2356 'trxn_id' => 'jdhfi88',
2357 'invoice_id' => 'djfhiewuyr',
2359 'contribution_status_id' => 2,
2360 'contribution_page_id' => $this->_ids
['contribution_page'],
2361 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2364 $this->callAPISuccess('line_item', 'create', array(
2365 'entity_id' => $contribution['id'],
2366 'entity_table' => 'civicrm_contribution',
2367 'contribution_id' => $contribution['id'],
2368 'price_field_id' => $this->_ids
['price_field'][0],
2372 'financial_type_id' => 1,
2373 'price_field_value_id' => $priceFieldValueID,
2375 $this->_ids
['contact'] = $contactID;
2376 $this->_ids
['contribution'] = $contribution['id'];
2377 $this->_ids
['membership'] = $membership['id'];
2381 * Test sending a mail via the API.
2383 public function testSendMail() {
2384 $mut = new CiviMailUtils($this, TRUE);
2385 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
2386 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2387 'id' => $contribution['id'],
2388 'receipt_from_email' => 'api@civicrm.org',
2391 $mut->checkMailLog(array(
2393 'Contribution Information',
2394 'Please print this confirmation for your records',
2403 * Test sending a mail via the API.
2405 public function testSendMailEvent() {
2406 $mut = new CiviMailUtils($this, TRUE);
2407 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
2408 $event = $this->eventCreate(array(
2409 'is_email_confirm' => 1,
2410 'confirm_from_email' => 'test@civicrm.org',
2412 $this->_eventID
= $event['id'];
2413 $participantParams = array(
2414 'contact_id' => $this->_individualId
,
2415 'event_id' => $this->_eventID
,
2418 // to ensure it matches later on
2419 'register_date' => '2007-07-21 00:00:00',
2420 'source' => 'Online Event Registration: API Testing',
2423 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2424 $this->callAPISuccess('participant_payment', 'create', array(
2425 'participant_id' => $participant['id'],
2426 'contribution_id' => $contribution['id'],
2428 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2429 'id' => $contribution['id'],
2430 'receipt_from_email' => 'api@civicrm.org',
2434 $mut->checkMailLog(array(
2435 'Annual CiviCRM meet',
2437 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
2444 * This function does a GET & compares the result against the $params.
2446 * Use as a double check on Creates.
2448 * @param array $params
2450 * @param bool $delete
2452 public function contributionGetnCheck($params, $id, $delete = TRUE) {
2454 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
2460 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
2462 $this->assertAPISuccess($contribution, 0);
2463 $values = $contribution['values'][$contribution['id']];
2464 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2465 // this is not returned in id format
2466 unset($params['payment_instrument_id']);
2467 $params['contribution_source'] = $params['source'];
2468 unset($params['source']);
2469 foreach ($params as $key => $value) {
2470 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
2475 * Create a pending contribution & linked pending pledge record.
2477 public function createPendingPledgeContribution() {
2479 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
2480 $this->_ids
['pledge'] = $pledgeID;
2481 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
2482 'contribution_status_id' => 'Pending',
2483 'total_amount' => 500,
2486 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
2487 'options' => array('limit' => 1),
2490 $this->callAPISuccess('PledgePayment', 'create', array(
2492 'contribution_id' =>
2493 $contribution['id'],
2494 'status_id' => 'Pending',
2495 'scheduled_amount' => 500,
2498 return $contribution['id'];
2502 * Create a pending contribution & linked pending participant record (along with an event).
2504 public function createPendingParticipantContribution() {
2505 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
2506 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
2507 $this->_ids
['participant'] = $participantID;
2508 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
2509 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2510 $this->callAPISuccess('participant_payment', 'create', array(
2511 'contribution_id' => $contribution['id'],
2512 'participant_id' => $participantID,
2514 $this->callAPISuccess('line_item', 'get', array(
2515 'entity_id' => $contribution['id'],
2516 'entity_table' => 'civicrm_contribution',
2517 'api.line_item.create' => array(
2518 'entity_id' => $participantID,
2519 'entity_table' => 'civicrm_participant',
2522 return $contribution['id'];
2526 * Get financial transaction amount.
2528 * @param int $contId
2530 * @return null|string
2532 public function _getFinancialTrxnAmount($contId) {
2534 SUM( ft.total_amount ) AS total
2535 FROM civicrm_financial_trxn AS ft
2536 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2537 WHERE ceft.entity_table = 'civicrm_contribution'
2538 AND ceft.entity_id = {$contId}";
2540 $result = CRM_Core_DAO
::singleValueQuery($query);
2545 * @param int $contId
2547 * @return null|string
2549 public function _getFinancialItemAmount($contId) {
2550 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
2553 FROM civicrm_financial_item
2554 WHERE entity_table = 'civicrm_line_item'
2555 AND entity_id = {$lineItem}";
2556 $result = CRM_Core_DAO
::singleValueQuery($query);
2561 * @param int $contId
2564 public function _checkFinancialItem($contId, $context) {
2565 if ($context != 'paylater') {
2567 'entity_id' => $contId,
2568 'entity_table' => 'civicrm_contribution',
2570 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
2571 $entityParams = array(
2572 'financial_trxn_id' => $trxn['financial_trxn_id'],
2573 'entity_table' => 'civicrm_financial_item',
2575 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2577 'id' => $entityTrxn['entity_id'],
2580 if ($context == 'paylater') {
2581 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
2582 foreach ($lineItems as $key => $item) {
2584 'entity_id' => $key,
2585 'entity_table' => 'civicrm_line_item',
2587 $compareParams = array('status_id' => 1);
2588 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2591 elseif ($context == 'refund') {
2592 $compareParams = array(
2594 'financial_account_id' => 1,
2598 elseif ($context == 'cancelPending') {
2599 $compareParams = array(
2601 'financial_account_id' => 1,
2605 elseif ($context == 'changeFinancial') {
2606 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
2608 'entity_id' => $lineKey,
2611 $compareParams = array(
2612 'financial_account_id' => 1,
2614 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2616 'financial_account_id' => 3,
2617 'entity_id' => $lineKey,
2619 $compareParams = array(
2623 if ($context != 'paylater') {
2624 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2629 * Check financial transaction.
2631 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
2633 * @param array $contribution
2634 * @param string $context
2635 * @param int $instrumentId
2636 * @param array $extraParams
2638 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
2639 $trxnParams = array(
2640 'entity_id' => $contribution['id'],
2641 'entity_table' => 'civicrm_contribution',
2643 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2645 'id' => $trxn['financial_trxn_id'],
2647 if ($context == 'payLater') {
2648 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2649 $compareParams = array(
2651 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
2654 elseif ($context == 'refund') {
2655 $compareParams = array(
2656 'to_financial_account_id' => 6,
2657 'total_amount' => -100,
2659 'trxn_date' => '2015-01-01 09:00:00',
2660 'trxn_id' => 'the refund',
2663 elseif ($context == 'cancelPending') {
2664 $compareParams = array(
2665 'to_financial_account_id' => 7,
2666 'total_amount' => -100,
2670 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
2671 $entityParams = array(
2672 'entity_id' => $contribution['id'],
2673 'entity_table' => 'civicrm_contribution',
2676 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2677 $trxnParams1 = array(
2678 'id' => $trxn['financial_trxn_id'],
2680 $compareParams = array(
2681 'total_amount' => -100,
2684 if ($context == 'paymentInstrument') {
2685 $compareParams +
= array(
2686 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
2687 'payment_instrument_id' => 4,
2691 $compareParams['to_financial_account_id'] = 12;
2693 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
2694 $compareParams['total_amount'] = 100;
2695 if ($context == 'paymentInstrument') {
2696 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
2697 $compareParams['payment_instrument_id'] = $instrumentId;
2700 $compareParams['to_financial_account_id'] = 12;
2704 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
2710 public function _addPaymentInstrument() {
2711 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2712 $optionParams = array(
2713 'option_group_id' => $gId,
2714 'label' => 'Test Card',
2715 'name' => 'Test Card',
2720 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2721 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2722 $financialParams = array(
2723 'entity_table' => 'civicrm_option_value',
2724 'entity_id' => $optionValue['id'],
2725 'account_relationship' => $relationTypeId,
2726 'financial_account_id' => 7,
2728 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
2729 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2730 return $optionValue['values'][$optionValue['id']]['value'];
2734 * @param array $params
2737 public function _checkFinancialRecords($params, $context) {
2738 $entityParams = array(
2739 'entity_id' => $params['id'],
2740 'entity_table' => 'civicrm_contribution',
2742 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2743 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
2744 if ($context == 'pending') {
2745 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
2746 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2749 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2750 $trxnParams = array(
2751 'id' => $trxn['financial_trxn_id'],
2753 if ($context != 'online' && $context != 'payLater') {
2754 $compareParams = array(
2755 'to_financial_account_id' => 6,
2756 'total_amount' => 100,
2760 if ($context == 'feeAmount') {
2761 $compareParams['fee_amount'] = 50;
2763 elseif ($context == 'online') {
2764 $compareParams = array(
2765 'to_financial_account_id' => 12,
2766 'total_amount' => 100,
2770 elseif ($context == 'payLater') {
2771 $compareParams = array(
2772 'to_financial_account_id' => 7,
2773 'total_amount' => 100,
2777 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2778 $entityParams = array(
2779 'financial_trxn_id' => $trxn['financial_trxn_id'],
2780 'entity_table' => 'civicrm_financial_item',
2782 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2783 $fitemParams = array(
2784 'id' => $entityTrxn['entity_id'],
2786 $compareParams = array(
2789 'financial_account_id' => 1,
2791 if ($context == 'payLater') {
2792 $compareParams = array(
2795 'financial_account_id' => 1,
2798 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2799 if ($context == 'feeAmount') {
2801 'entity_id' => $params['id'],
2802 'entity_table' => 'civicrm_contribution',
2804 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
2805 $trxnParams = array(
2806 'id' => $maxTrxn['financial_trxn_id'],
2808 $compareParams = array(
2809 'to_financial_account_id' => 5,
2810 'from_financial_account_id' => 6,
2811 'total_amount' => 50,
2814 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
2815 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2816 $fitemParams = array(
2817 'entity_id' => $trxnId['financialTrxnId'],
2818 'entity_table' => 'civicrm_financial_trxn',
2820 $compareParams = array(
2823 'financial_account_id' => 5,
2825 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2830 * Set up the basic recurring contribution for tests.
2832 * @param array $generalParams
2833 * Parameters that can be merged into the recurring AND the contribution.
2835 * @param array $recurParams
2836 * Parameters to merge into the recur only.
2840 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
2841 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2842 'contact_id' => $this->_individualId
,
2843 'installments' => '12',
2844 'frequency_interval' => '1',
2846 'contribution_status_id' => 1,
2847 'start_date' => '2012-01-01 00:00:00',
2848 'currency' => 'USD',
2849 'frequency_unit' => 'month',
2850 'payment_processor_id' => $this->paymentProcessorID
,
2851 ), $generalParams, $recurParams));
2852 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2855 'contribution_recur_id' => $contributionRecur['id'],
2858 return $originalContribution;
2862 * Set up a repeat transaction.
2864 * @param array $recurParams
2868 protected function setUpRepeatTransaction($recurParams = array()) {
2869 $paymentProcessorID = $this->paymentProcessorCreate();
2870 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2871 'contact_id' => $this->_individualId
,
2872 'installments' => '12',
2873 'frequency_interval' => '1',
2875 'contribution_status_id' => 1,
2876 'start_date' => '2012-01-01 00:00:00',
2877 'currency' => 'USD',
2878 'frequency_unit' => 'month',
2879 'payment_processor_id' => $paymentProcessorID,
2881 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2883 array('contribution_recur_id' => $contributionRecur['id']))
2885 return $originalContribution;
2889 * Common set up routine.
2893 protected function setUpForCompleteTransaction() {
2894 $this->mut
= new CiviMailUtils($this, TRUE);
2895 $this->createLoggedInUser();
2896 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2897 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2898 return $contribution;