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 * Test retrieval by total_amount works.
548 public function testGetContributionByTotalAmount() {
549 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '5')));
550 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '10')));
551 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
552 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
553 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
554 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
555 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
556 $this->callAPISuccessGetCount('Contribution', array(), 2);
560 * Create test with unique field name on source.
562 public function testCreateContributionSource() {
565 'contact_id' => $this->_individualId
,
566 'receive_date' => date('Ymd'),
567 'total_amount' => 100.00,
568 'financial_type_id' => $this->_financialTypeId
,
569 'payment_instrument_id' => 1,
570 'non_deductible_amount' => 10.00,
571 'fee_amount' => 50.00,
572 'net_amount' => 90.00,
574 'invoice_id' => 67890,
575 'contribution_source' => 'SSF',
576 'contribution_status_id' => 1,
579 $contribution = $this->callAPISuccess('contribution', 'create', $params);
580 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
581 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
585 * Create test with unique field name on source.
587 public function testCreateDefaultNow() {
589 $params = $this->_params
;
590 unset($params['receive_date']);
592 $contribution = $this->callAPISuccess('contribution', 'create', $params);
593 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
594 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
598 * Create test with unique field name on source.
600 public function testCreateContributionSourceInvalidContact() {
604 'receive_date' => date('Ymd'),
605 'total_amount' => 100.00,
606 'financial_type_id' => $this->_financialTypeId
,
607 'payment_instrument_id' => 1,
608 'non_deductible_amount' => 10.00,
609 'fee_amount' => 50.00,
610 'net_amount' => 90.00,
612 'invoice_id' => 67890,
613 'contribution_source' => 'SSF',
614 'contribution_status_id' => 1,
617 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
620 public function testCreateContributionSourceInvalidContContact() {
623 'contribution_contact_id' => 999,
624 'receive_date' => date('Ymd'),
625 'total_amount' => 100.00,
626 'financial_type_id' => $this->_financialTypeId
,
627 'payment_instrument_id' => 1,
628 'non_deductible_amount' => 10.00,
629 'fee_amount' => 50.00,
630 'net_amount' => 90.00,
632 'invoice_id' => 67890,
633 'contribution_source' => 'SSF',
634 'contribution_status_id' => 1,
637 $this->callAPIFailure('contribution', 'create', $params);
641 * Test note created correctly.
643 public function testCreateContributionWithNote() {
644 $description = "Demonstrates creating contribution with Note Entity.";
645 $subfile = "ContributionCreateWithNote";
647 'contact_id' => $this->_individualId
,
648 'receive_date' => '2012-01-01',
649 'total_amount' => 100.00,
650 'financial_type_id' => $this->_financialTypeId
,
651 'payment_instrument_id' => 1,
652 'non_deductible_amount' => 10.00,
653 'fee_amount' => 50.00,
654 'net_amount' => 90.00,
656 'invoice_id' => 67890,
658 'contribution_status_id' => 1,
659 'note' => 'my contribution note',
662 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
663 $result = $this->callAPISuccess('note', 'get', array(
664 'entity_table' => 'civicrm_contribution',
665 'entity_id' => $contribution['id'],
668 $this->assertEquals('my contribution note', $result['values'][0]['note']);
669 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
672 public function testCreateContributionWithNoteUniqueNameAliases() {
674 'contact_id' => $this->_individualId
,
675 'receive_date' => '2012-01-01',
676 'total_amount' => 100.00,
677 'financial_type_id' => $this->_financialTypeId
,
678 'payment_instrument_id' => 1,
679 'non_deductible_amount' => 10.00,
680 'fee_amount' => 50.00,
681 'net_amount' => 90.00,
683 'invoice_id' => 67890,
685 'contribution_status_id' => 1,
686 'contribution_note' => 'my contribution note',
689 $contribution = $this->callAPISuccess('contribution', 'create', $params);
690 $result = $this->callAPISuccess('note', 'get', array(
691 'entity_table' => 'civicrm_contribution',
692 'entity_id' => $contribution['id'],
695 $this->assertEquals('my contribution note', $result['values'][0]['note']);
696 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
700 * This is the test for creating soft credits.
702 public function testCreateContributionWithSoftCredit() {
703 $description = "Demonstrates creating contribution with SoftCredit.";
704 $subfile = "ContributionCreateWithSoftCredit";
705 $contact2 = $this->callAPISuccess('Contact', 'create', array(
706 'display_name' => 'superman',
707 'contact_type' => 'Individual',
710 'contact_id' => $contact2['id'],
712 'soft_credit_type_id' => 3,
715 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
716 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
717 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
719 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
720 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
721 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
723 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
724 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
727 public function testCreateContributionWithSoftCreditDefaults() {
728 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
729 $subfile = "ContributionCreateWithSoftCreditDefaults";
730 $contact2 = $this->callAPISuccess('Contact', 'create', array(
731 'display_name' => 'superman',
732 'contact_type' => 'Individual',
734 $params = $this->_params +
array(
735 'soft_credit_to' => $contact2['id'],
737 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
738 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
740 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
741 // Default soft credit amount = contribution.total_amount
742 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
743 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
745 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
746 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
749 public function testCreateContributionWithHonoreeContact() {
750 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
751 $subfile = "ContributionCreateWithHonoreeContact";
752 $contact2 = $this->callAPISuccess('Contact', 'create', array(
753 'display_name' => 'superman',
754 'contact_type' => 'Individual',
756 $params = $this->_params +
array(
757 'honor_contact_id' => $contact2['id'],
759 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
760 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
762 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
763 // Default soft credit amount = contribution.total_amount
764 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
765 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
766 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
768 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
769 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
773 * Test using example code.
775 public function testContributionCreateExample() {
776 //make sure at least on page exists since there is a truncate in tear down
777 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
778 require_once 'api/v3/examples/Contribution/Create.php';
779 $result = contribution_create_example();
781 $expectedResult = contribution_create_expectedresult();
782 $this->checkArrayEquals($expectedResult, $result);
783 $this->contributionDelete($id);
787 * Function tests that additional financial records are created when fee amount is recorded.
789 public function testCreateContributionWithFee() {
791 'contact_id' => $this->_individualId
,
792 'receive_date' => '20120511',
793 'total_amount' => 100.00,
795 'financial_type_id' => 1,
797 'invoice_id' => 67890,
799 'contribution_status_id' => 1,
802 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
803 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
804 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
805 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
806 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
807 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
808 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
809 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
810 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
811 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
813 $lineItems = $this->callAPISuccess('line_item', 'get', array(
815 'entity_id' => $contribution['id'],
816 'entity_table' => 'civicrm_contribution',
819 $this->assertEquals(1, $lineItems['count']);
820 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
821 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
822 $lineItems = $this->callAPISuccess('line_item', 'get', array(
824 'entity_id' => $contribution['id'],
825 'contribution_id' => $contribution['id'],
826 'entity_table' => 'civicrm_contribution',
829 $this->assertEquals(1, $lineItems['count']);
830 $this->_checkFinancialRecords($contribution, 'feeAmount');
835 * Function tests that additional financial records are created when online contribution is created.
837 public function testCreateContributionOnline() {
838 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
839 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
840 $this->assertAPISuccess($contributionPage);
842 'contact_id' => $this->_individualId
,
843 'receive_date' => '20120511',
844 'total_amount' => 100.00,
845 'financial_type_id' => 1,
846 'contribution_page_id' => $contributionPage['id'],
847 'payment_processor' => $this->paymentProcessorID
,
849 'invoice_id' => 67890,
851 'contribution_status_id' => 1,
855 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
856 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
857 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
858 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
859 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
860 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
861 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
862 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
863 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
864 'id' => $this->paymentProcessorID
,
865 'return' => 'payment_instrument_id',
867 $this->_checkFinancialRecords($contribution, 'online');
871 * Check handling of financial type.
873 * In the interests of removing financial type / contribution type checks from
874 * legacy format function lets test that the api is doing this for us
876 public function testCreateInvalidFinancialType() {
877 $params = $this->_params
;
878 $params['financial_type_id'] = 99999;
879 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
883 * Check handling of financial type.
885 * In the interests of removing financial type / contribution type checks from
886 * legacy format function lets test that the api is doing this for us
888 public function testValidNamedFinancialType() {
889 $params = $this->_params
;
890 $params['financial_type_id'] = 'Donation';
891 $this->callAPISuccess($this->_entity
, 'create', $params);
895 * Tests that additional financial records are created.
897 * Checks when online contribution with pay later option is created
899 public function testCreateContributionPayLaterOnline() {
900 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
901 $this->_pageParams
['is_pay_later'] = 1;
902 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
903 $this->assertAPISuccess($contributionPage);
905 'contact_id' => $this->_individualId
,
906 'receive_date' => '20120511',
907 'total_amount' => 100.00,
908 'financial_type_id' => 1,
909 'contribution_page_id' => $contributionPage['id'],
912 'invoice_id' => 67890,
914 'contribution_status_id' => 2,
918 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
919 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
920 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
921 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
922 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
923 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
924 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
925 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
926 $this->_checkFinancialRecords($contribution, 'payLater');
930 * Function tests that additional financial records are created for online contribution with pending option.
932 public function testCreateContributionPendingOnline() {
933 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
934 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
935 $this->assertAPISuccess($contributionPage);
937 'contact_id' => $this->_individualId
,
938 'receive_date' => '20120511',
939 'total_amount' => 100.00,
940 'financial_type_id' => 1,
941 'contribution_page_id' => $contributionPage['id'],
943 'invoice_id' => 67890,
945 'contribution_status_id' => 2,
948 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
949 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
950 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
951 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
952 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
953 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
954 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
955 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
956 $this->_checkFinancialRecords($contribution, 'pending');
960 * Test that BAO defaults work.
962 public function testCreateBAODefaults() {
963 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
964 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
965 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
966 'id' => $contribution['id'],
967 'api.contribution.delete' => 1,
969 $this->assertEquals(1, $contribution['contribution_status_id']);
970 $this->assertEquals('Check', $contribution['payment_instrument']);
974 * Function tests that line items, financial records are updated when contribution amount is changed.
976 public function testCreateUpdateContributionChangeTotal() {
977 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
978 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
980 'entity_id' => $contribution['id'],
981 'entity_table' => 'civicrm_contribution',
983 'return' => 'line_total',
985 $this->assertEquals('100.00', $lineItems);
986 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
987 // Financial trxn SUM = 100 + 5 (fee)
988 $this->assertEquals('105.00', $trxnAmount);
991 'id' => $contribution['id'],
992 'total_amount' => '125',
994 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
996 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
998 'entity_id' => $contribution['id'],
999 'entity_table' => 'civicrm_contribution',
1001 'return' => 'line_total',
1004 $this->assertEquals('125.00', $lineItems);
1005 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1007 // Financial trxn SUM = 125 + 5 (fee).
1008 $this->assertEquals('130.00', $trxnAmount);
1009 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1013 * Function tests that line items, financial records are updated when pay later contribution is received.
1015 public function testCreateUpdateContributionPayLater() {
1016 $contribParams = array(
1017 'contact_id' => $this->_individualId
,
1018 'receive_date' => '2012-01-01',
1019 'total_amount' => 100.00,
1020 'financial_type_id' => $this->_financialTypeId
,
1021 'payment_instrument_id' => 1,
1022 'contribution_status_id' => 2,
1023 'is_pay_later' => 1,
1026 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1028 $newParams = array_merge($contribParams, array(
1029 'id' => $contribution['id'],
1030 'contribution_status_id' => 1,
1033 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1034 $contribution = $contribution['values'][$contribution['id']];
1035 $this->assertEquals($contribution['contribution_status_id'], '1');
1036 $this->_checkFinancialItem($contribution['id'], 'paylater');
1037 $this->_checkFinancialTrxn($contribution, 'payLater');
1041 * Function tests that financial records are updated when Payment Instrument is changed.
1043 public function testCreateUpdateContributionPaymentInstrument() {
1044 $instrumentId = $this->_addPaymentInstrument();
1045 $contribParams = array(
1046 'contact_id' => $this->_individualId
,
1047 'total_amount' => 100.00,
1048 'financial_type_id' => $this->_financialTypeId
,
1049 'payment_instrument_id' => 4,
1050 'contribution_status_id' => 1,
1053 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1055 $newParams = array_merge($contribParams, array(
1056 'id' => $contribution['id'],
1057 'payment_instrument_id' => $instrumentId,
1060 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1061 $this->assertAPISuccess($contribution);
1062 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1066 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1068 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1069 $instrumentId = $this->_addPaymentInstrument();
1070 $contribParams = array(
1071 'contact_id' => $this->_individualId
,
1072 'total_amount' => -100.00,
1073 'financial_type_id' => $this->_financialTypeId
,
1074 'payment_instrument_id' => 4,
1075 'contribution_status_id' => 1,
1078 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1080 $newParams = array_merge($contribParams, array(
1081 'id' => $contribution['id'],
1082 'payment_instrument_id' => $instrumentId,
1085 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1086 $this->assertAPISuccess($contribution);
1087 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId, array('total_amount' => '-100.00'));
1091 * Function tests that financial records are added when Contribution is Refunded.
1093 public function testCreateUpdateContributionRefund() {
1094 $contributionParams = array(
1095 'contact_id' => $this->_individualId
,
1096 'receive_date' => '2012-01-01',
1097 'total_amount' => 100.00,
1098 'financial_type_id' => $this->_financialTypeId
,
1099 'payment_instrument_id' => 4,
1100 'contribution_status_id' => 1,
1101 'trxn_id' => 'original_payment',
1103 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1104 $newParams = array_merge($contributionParams, array(
1105 'id' => $contribution['id'],
1106 'contribution_status_id' => 'Refunded',
1107 'cancel_date' => '2015-01-01 09:00',
1108 'refund_trxn_id' => 'the refund',
1112 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1113 $this->_checkFinancialTrxn($contribution, 'refund');
1114 $this->_checkFinancialItem($contribution['id'], 'refund');
1115 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1116 'id' => $contribution['id'],
1117 'return' => 'trxn_id',
1122 * Refund a contribution for a financial type with a contra account.
1124 * CRM-17951 the contra account is a financial account with a relationship to a
1125 * financial type. It is not always configured but should be reflected
1126 * in the financial_trxn & financial_item table if it is.
1128 public function testCreateUpdateChargebackContributionDefaultAccount() {
1129 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1130 $this->callAPISuccess('Contribution', 'create', array(
1131 'id' => $contribution['id'],
1132 'contribution_status_id' => 'Chargeback',
1134 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1136 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1137 'contribution_id' => $contribution['id'],
1138 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1140 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1141 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1142 'total_amount' => -100,
1143 'status_id' => 'Chargeback',
1144 'to_financial_account_id' => 6,
1149 * Refund a contribution for a financial type with a contra account.
1151 * CRM-17951 the contra account is a financial account with a relationship to a
1152 * financial type. It is not always configured but should be reflected
1153 * in the financial_trxn & financial_item table if it is.
1155 public function testCreateUpdateChargebackContributionCustomAccount() {
1156 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1157 'name' => 'Chargeback Account',
1158 'is_active' => TRUE,
1161 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1162 'entity_id' => $this->_financialTypeId
,
1163 'entity_table' => 'civicrm_financial_type',
1164 'account_relationship' => 'Chargeback Account is',
1165 'financial_account_id' => 'Chargeback Account',
1168 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1169 $this->callAPISuccess('Contribution', 'create', array(
1170 'id' => $contribution['id'],
1171 'contribution_status_id' => 'Chargeback',
1173 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1175 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1176 'contribution_id' => $contribution['id'],
1177 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1179 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1181 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1182 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1183 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1187 * Refund a contribution for a financial type with a contra account.
1189 * CRM-17951 the contra account is a financial account with a relationship to a
1190 * financial type. It is not always configured but should be reflected
1191 * in the financial_trxn & financial_item table if it is.
1193 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1194 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1195 'name' => 'Refund Account',
1196 'is_active' => TRUE,
1199 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1200 'entity_id' => $this->_financialTypeId
,
1201 'entity_table' => 'civicrm_financial_type',
1202 'account_relationship' => 'Credit/Contra Revenue Account is',
1203 'financial_account_id' => 'Refund Account',
1206 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1207 $this->callAPISuccess('Contribution', 'create', array(
1208 'id' => $contribution['id'],
1209 'contribution_status_id' => 'Refunded',
1212 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1213 'contribution_id' => $contribution['id'],
1214 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1216 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1218 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1219 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1220 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1224 * Function tests that trxn_id is set when passed in.
1226 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1227 * when trxn_id is passed in.
1229 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1230 $contributionParams = array(
1231 'contact_id' => $this->_individualId
,
1232 'receive_date' => '2012-01-01',
1233 'total_amount' => 100.00,
1234 'financial_type_id' => $this->_financialTypeId
,
1235 'payment_instrument_id' => 4,
1236 'contribution_status_id' => 1,
1237 'trxn_id' => 'original_payment',
1239 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1240 $newParams = array_merge($contributionParams, array(
1241 'id' => $contribution['id'],
1242 'contribution_status_id' => 'Refunded',
1243 'cancel_date' => '2015-01-01 09:00',
1244 'trxn_id' => 'the refund',
1248 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1249 $this->_checkFinancialTrxn($contribution, 'refund');
1250 $this->_checkFinancialItem($contribution['id'], 'refund');
1251 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1252 'id' => $contribution['id'],
1253 'return' => 'trxn_id',
1258 * Function tests that trxn_id is set when passed in.
1260 * Here we ensure that the civicrm_contribution.trxn_id is set
1261 * when trxn_id is passed in but if refund_trxn_id is different then that
1262 * is kept for the refund transaction.
1264 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1265 $contributionParams = array(
1266 'contact_id' => $this->_individualId
,
1267 'receive_date' => '2012-01-01',
1268 'total_amount' => 100.00,
1269 'financial_type_id' => $this->_financialTypeId
,
1270 'payment_instrument_id' => 4,
1271 'contribution_status_id' => 1,
1272 'trxn_id' => 'original_payment',
1274 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1275 $newParams = array_merge($contributionParams, array(
1276 'id' => $contribution['id'],
1277 'contribution_status_id' => 'Refunded',
1278 'cancel_date' => '2015-01-01 09:00',
1279 'trxn_id' => 'cont id',
1280 'refund_trxn_id' => 'the refund',
1284 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1285 $this->_checkFinancialTrxn($contribution, 'refund');
1286 $this->_checkFinancialItem($contribution['id'], 'refund');
1287 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1288 'id' => $contribution['id'],
1289 'return' => 'trxn_id',
1294 * Function tests that refund_trxn_id is set when passed in empty.
1296 * Here we ensure that the civicrm_contribution.trxn_id is set
1297 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1298 * is kept for the refund transaction.
1300 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1301 $contributionParams = array(
1302 'contact_id' => $this->_individualId
,
1303 'receive_date' => '2012-01-01',
1304 'total_amount' => 100.00,
1305 'financial_type_id' => $this->_financialTypeId
,
1306 'payment_instrument_id' => 4,
1307 'contribution_status_id' => 1,
1308 'trxn_id' => 'original_payment',
1310 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1311 $newParams = array_merge($contributionParams, array(
1312 'id' => $contribution['id'],
1313 'contribution_status_id' => 'Refunded',
1314 'cancel_date' => '2015-01-01 09:00',
1315 'trxn_id' => 'cont id',
1316 'refund_trxn_id' => '',
1320 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1321 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1322 $this->_checkFinancialItem($contribution['id'], 'refund');
1323 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1324 'id' => $contribution['id'],
1325 'return' => 'trxn_id',
1330 * Function tests invalid contribution status change.
1332 public function testCreateUpdateContributionInValidStatusChange() {
1333 $contribParams = array(
1335 'receive_date' => '2012-01-01',
1336 'total_amount' => 100.00,
1337 'financial_type_id' => 1,
1338 'payment_instrument_id' => 1,
1339 'contribution_status_id' => 1,
1341 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1342 $newParams = array_merge($contribParams, array(
1343 'id' => $contribution['id'],
1344 'contribution_status_id' => 2,
1347 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1352 * Function tests that financial records are added when Pending Contribution is Canceled.
1354 public function testCreateUpdateContributionCancelPending() {
1355 $contribParams = array(
1356 'contact_id' => $this->_individualId
,
1357 'receive_date' => '2012-01-01',
1358 'total_amount' => 100.00,
1359 'financial_type_id' => $this->_financialTypeId
,
1360 'payment_instrument_id' => 1,
1361 'contribution_status_id' => 2,
1362 'is_pay_later' => 1,
1365 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1366 $newParams = array_merge($contribParams, array(
1367 'id' => $contribution['id'],
1368 'contribution_status_id' => 3,
1369 'cancel_date' => '2012-02-02 09:00',
1372 //Check if trxn_date is same as cancel_date.
1373 $checkTrxnDate = array(
1374 'trxn_date' => '2012-02-02 09:00:00',
1376 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1377 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1378 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1382 * Function tests that financial records are added when Financial Type is Changed.
1384 public function testCreateUpdateContributionChangeFinancialType() {
1385 $contribParams = array(
1386 'contact_id' => $this->_individualId
,
1387 'receive_date' => '2012-01-01',
1388 'total_amount' => 100.00,
1389 'financial_type_id' => 1,
1390 'payment_instrument_id' => 1,
1391 'contribution_status_id' => 1,
1394 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1395 $newParams = array_merge($contribParams, array(
1396 'id' => $contribution['id'],
1397 'financial_type_id' => 3,
1400 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1401 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1402 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1406 * Test that update does not change status id CRM-15105.
1408 public function testCreateUpdateWithoutChangingPendingStatus() {
1409 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1410 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1411 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1412 'id' => $contribution['id'],
1413 'api.contribution.delete' => 1,
1415 $this->assertEquals(2, $contribution['contribution_status_id']);
1419 * Test Updating a Contribution.
1421 * CHANGE: we require the API to do an incremental update
1423 public function testCreateUpdateContribution() {
1425 $contributionID = $this->contributionCreate(array(
1426 'contact_id' => $this->_individualId
,
1427 'trxn_id' => 212355,
1428 'financial_type_id' => $this->_financialTypeId
,
1429 'invoice_id' => 'old_invoice',
1431 $old_params = array(
1432 'contribution_id' => $contributionID,
1434 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1435 $this->assertEquals($original['id'], $contributionID);
1436 //set up list of old params, verify
1438 //This should not be required on update:
1439 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1440 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1441 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1442 $old_source = $original['values'][$contributionID]['contribution_source'];
1444 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1445 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1447 //check against values in CiviUnitTestCase::createContribution()
1448 $this->assertEquals($old_contact_id, $this->_individualId
);
1449 $this->assertEquals($old_fee_amount, 5.00);
1450 $this->assertEquals($old_source, 'SSF');
1451 $this->assertEquals($old_trxn_id, 212355);
1452 $this->assertEquals($old_invoice_id, 'old_invoice');
1454 'id' => $contributionID,
1455 'contact_id' => $this->_individualId
,
1456 'total_amount' => 110.00,
1457 'financial_type_id' => $this->_financialTypeId
,
1458 'non_deductible_amount' => 10.00,
1459 'net_amount' => 100.00,
1460 'contribution_status_id' => 1,
1461 'note' => 'Donating for Nobel Cause',
1465 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1467 $new_params = array(
1468 'contribution_id' => $contribution['id'],
1471 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1473 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1474 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1475 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId
);
1476 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1477 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1478 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1479 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1480 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1481 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1482 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1483 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1485 'contribution_id' => $contributionID,
1488 $result = $this->callAPISuccess('contribution', 'delete', $params);
1489 $this->assertAPISuccess($result);
1492 ///////////////// civicrm_contribution_delete methods
1495 * Attempt (but fail) to delete a contribution without parameters.
1497 public function testDeleteEmptyParamsContribution() {
1499 $this->callAPIFailure('contribution', 'delete', $params);
1502 public function testDeleteParamsNotArrayContribution() {
1503 $params = 'contribution_id= 1';
1504 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1505 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1508 public function testDeleteWrongParamContribution() {
1510 'contribution_source' => 'SSF',
1513 $this->callAPIFailure('contribution', 'delete', $params);
1516 public function testDeleteContribution() {
1517 $contributionID = $this->contributionCreate(array(
1518 'contact_id' => $this->_individualId
,
1519 'financial_type_id' => $this->_financialTypeId
,
1522 'id' => $contributionID,
1524 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1528 * Test civicrm_contribution_search with empty params.
1530 * All available contributions expected.
1532 public function testSearchEmptyParams() {
1536 'contact_id' => $this->_individualId
,
1537 'receive_date' => date('Ymd'),
1538 'total_amount' => 100.00,
1539 'financial_type_id' => $this->_financialTypeId
,
1540 'non_deductible_amount' => 10.00,
1541 'fee_amount' => 5.00,
1542 'net_amount' => 95.00,
1544 'invoice_id' => 78910,
1546 'contribution_status_id' => 1,
1549 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1551 $result = $this->callAPISuccess('contribution', 'get', $params);
1552 // We're taking the first element.
1553 $res = $result['values'][$contribution['id']];
1555 $this->assertEquals($p['contact_id'], $res['contact_id']);
1556 $this->assertEquals($p['total_amount'], $res['total_amount']);
1557 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1558 $this->assertEquals($p['net_amount'], $res['net_amount']);
1559 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1560 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1561 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1562 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1563 $this->assertEquals($p['source'], $res['contribution_source']);
1564 // contribution_status_id = 1 => Completed
1565 $this->assertEquals('Completed', $res['contribution_status']);
1567 $this->contributionDelete($contribution['id']);
1571 * Test civicrm_contribution_search. Success expected.
1573 public function testSearch() {
1575 'contact_id' => $this->_individualId
,
1576 'receive_date' => date('Ymd'),
1577 'total_amount' => 100.00,
1578 'financial_type_id' => $this->_financialTypeId
,
1579 'non_deductible_amount' => 10.00,
1580 'contribution_status_id' => 1,
1583 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1586 'contact_id' => $this->_individualId
,
1587 'receive_date' => date('Ymd'),
1588 'total_amount' => 200.00,
1589 'financial_type_id' => $this->_financialTypeId
,
1590 'non_deductible_amount' => 20.00,
1591 'trxn_id' => 5454565,
1592 'invoice_id' => 1212124,
1593 'fee_amount' => 50.00,
1594 'net_amount' => 60.00,
1595 'contribution_status_id' => 2,
1598 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1601 'contribution_id' => $contribution2['id'],
1604 $result = $this->callAPISuccess('contribution', 'get', $params);
1605 $res = $result['values'][$contribution2['id']];
1607 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1608 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1609 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1610 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1611 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1612 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1613 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1614 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1615 // contribution_status_id = 2 => Pending
1616 $this->assertEquals('Pending', $res['contribution_status']);
1618 $this->contributionDelete($contribution1['id']);
1619 $this->contributionDelete($contribution2['id']);
1623 * Test completing a transaction via the API.
1625 * Note that we are creating a logged in user because email goes out from
1628 public function testCompleteTransaction() {
1629 $mut = new CiviMailUtils($this, TRUE);
1630 $this->swapMessageTemplateForTestTemplate();
1631 $this->createLoggedInUser();
1632 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1633 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1634 $this->callAPISuccess('contribution', 'completetransaction', array(
1635 'id' => $contribution['id'],
1637 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1638 $this->assertEquals('SSF', $contribution['contribution_source']);
1639 $this->assertEquals('Completed', $contribution['contribution_status']);
1640 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1641 $mut->checkMailLog(array(
1642 'email:::anthony_anderson@civicrm.org',
1646 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1647 "receipt_date:::\n",
1648 'contributeMode:::notify',
1649 'title:::Contribution',
1650 'displayName:::Mr. Anthony Anderson II',
1653 $this->revertTemplateToReservedTemplate();
1657 * Test to ensure mail is sent on chosing pay later
1659 public function testpayLater() {
1660 $mut = new CiviMailUtils($this, TRUE);
1661 $this->swapMessageTemplateForTestTemplate();
1662 $this->createLoggedInUser();
1664 // create contribution page first
1665 $contributionPageParams = array(
1666 'title' => 'Help Support CiviCRM!',
1667 'financial_type_id' => 1,
1668 'is_monetary' => TRUE,
1669 'is_pay_later' => 1,
1670 'is_quick_config' => TRUE,
1671 'pay_later_text' => 'I will send payment by check',
1672 'pay_later_receipt' => 'This is a pay later reciept',
1673 'is_allow_other_amount' => 1,
1674 'min_amount' => 10.00,
1675 'max_amount' => 10000.00,
1676 'goal_amount' => 100000.00,
1677 'is_email_receipt' => 1,
1679 'amount_block_is_active' => 1,
1680 'currency' => 'USD',
1681 'is_billing_required' => 0,
1683 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1685 // submit form values
1686 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1688 'id' => $contributionPageResult['id'],
1689 'contact_id' => $this->_individualId
,
1690 'email-5' => 'anthony_anderson@civicrm.org',
1691 'payment_processor_id' => 0,
1694 'currencyID' => 'USD',
1695 'is_pay_later' => 1,
1696 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1697 'is_quick_config' => 1,
1698 'description' => 'Online Contribution: Help Support CiviCRM!',
1699 'price_set_id' => $priceSet['id'],
1701 $this->callAPISuccess('contribution_page', 'submit', $params);
1703 $mut->checkMailLog(array(
1705 'email:::anthony_anderson@civicrm.org',
1706 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1707 'displayName:::Mr. Anthony Anderson II',
1708 'contributionPageId:::' . $contributionPageResult['id'],
1709 'title:::' . $contributionPageParams['title'],
1710 'amount:::' . $params['amount'],
1713 $this->revertTemplateToReservedTemplate();
1718 * Test to check whether contact billing address is used when no contribution address
1720 public function testBillingAddress() {
1721 $mut = new CiviMailUtils($this, TRUE);
1722 $this->swapMessageTemplateForTestTemplate();
1723 $this->createLoggedInUser();
1725 //Scenario 1: When Contact don't have any address
1726 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1727 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1728 $this->callAPISuccess('contribution', 'completetransaction', array(
1729 'id' => $contribution['id'],
1731 $mut->checkMailLog(array(
1735 // Scenario 2: Contribution using address
1736 $address = $this->callAPISuccess('address', 'create', array(
1737 'street_address' => 'contribution billing st',
1738 'location_type_id' => 2,
1739 'contact_id' => $this->_params
['contact_id'],
1741 $params = array_merge($this->_params
, array(
1742 'contribution_status_id' => 2,
1743 'address_id' => $address['id'],
1746 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1747 $this->callAPISuccess('contribution', 'completetransaction', array(
1748 'id' => $contribution['id'],
1750 $mut->checkMailLog(array(
1751 'address:::contribution billing st',
1754 // Scenario 3: Contribution wtth no address but contact has a billing address
1755 $this->callAPISuccess('address', 'create', array(
1756 'id' => $address['id'],
1757 'street_address' => 'is billing st',
1758 'contact_id' => $this->_params
['contact_id'],
1760 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1761 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1762 $this->callAPISuccess('contribution', 'completetransaction', array(
1763 'id' => $contribution['id'],
1765 $mut->checkMailLog(array(
1766 'address:::is billing st',
1770 $this->revertTemplateToReservedTemplate();
1774 * Test completing a transaction via the API.
1776 * Note that we are creating a logged in user because email goes out from
1779 public function testCompleteTransactionFeeAmount() {
1780 $this->createLoggedInUser();
1781 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1782 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1783 $this->callAPISuccess('contribution', 'completetransaction', array(
1784 'id' => $contribution['id'],
1785 'fee_amount' => '.56',
1786 'trxn_id' => '7778888',
1788 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1789 $this->assertEquals('Completed', $contribution['contribution_status']);
1790 $this->assertEquals('7778888', $contribution['trxn_id']);
1791 $this->assertEquals('.56', $contribution['fee_amount']);
1792 $this->assertEquals('99.44', $contribution['net_amount']);
1796 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed
1798 public function testCheckTaxAmount() {
1799 $contact = $this->createLoggedInUser();
1800 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1801 'name' => 'Test taxable financial Type',
1805 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1806 'name' => 'Test Tax financial account ',
1807 'contact_id' => $contact,
1808 'financial_account_type_id' => 2,
1815 $financialTypeId = $financialType['id'];
1816 $financialAccountId = $financialAccount['id'];
1817 $financialAccountParams = array(
1818 'entity_table' => 'civicrm_financial_type',
1819 'entity_id' => $financialTypeId,
1820 'account_relationship' => 10,
1821 'financial_account_id' => $financialAccountId,
1823 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
1824 $taxRates = CRM_Core_PseudoConstant
::getTaxRates();
1825 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1826 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1827 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1828 $this->callAPISuccess('contribution', 'completetransaction', array(
1829 'id' => $contribution['id'],
1830 'trxn_id' => '777788888',
1831 'fee_amount' => '6.00',
1833 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
1834 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
1835 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
1836 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
1840 * Test repeat contribution successfully creates line item.
1842 public function testRepeatTransaction() {
1843 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
1844 $this->callAPISuccess('contribution', 'repeattransaction', array(
1845 'original_contribution_id' => $originalContribution['id'],
1846 'contribution_status_id' => 'Completed',
1847 'trxn_id' => uniqid(),
1849 $lineItemParams = array(
1850 'entity_id' => $originalContribution['id'],
1858 'financial_type_id',
1859 'deductible_amount',
1860 'price_field_value_id',
1864 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1865 'entity_id' => $originalContribution['id'],
1867 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1868 'entity_id' => $originalContribution['id'] +
1,
1870 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1871 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1872 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1873 $this->_checkFinancialRecords(array(
1874 'id' => $originalContribution['id'] +
1,
1875 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
1876 'id' => $originalContribution['payment_processor_id'],
1877 'return' => 'payment_instrument_id',
1880 $this->quickCleanUpFinancialEntities();
1884 * Test repeat contribution successfully creates line items (plural).
1886 public function testRepeatTransactionLineItems() {
1888 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
1889 $this->callAPISuccess('contribution', 'repeattransaction', array(
1890 'original_contribution_id' => $originalContribution['id'],
1891 'contribution_status_id' => 'Completed',
1892 'trxn_id' => uniqid(),
1895 $lineItemParams = array(
1896 'entity_id' => $originalContribution['id'],
1904 'financial_type_id',
1905 'deductible_amount',
1906 'price_field_value_id',
1910 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1911 'entity_id' => $originalContribution['id'],
1913 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1914 'entity_id' => $originalContribution['id'] +
1,
1917 // unset id and entity_id for all of them to be able to compare the lineItems:
1918 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1919 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1920 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1922 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
1923 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
1924 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
1926 // CRM-19309 so in future we also want to:
1927 // check that financial_line_items have been created for entity_id 3 and 4;
1929 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
1930 $this->quickCleanUpFinancialEntities();
1934 * Test repeat contribution successfully creates is_test transaction.
1936 public function testRepeatTransactionIsTest() {
1937 $this->_params
['is_test'] = 1;
1938 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
1940 $this->callAPISuccess('contribution', 'repeattransaction', array(
1941 'original_contribution_id' => $originalContribution['id'],
1942 'contribution_status_id' => 'Completed',
1943 'trxn_id' => uniqid(),
1945 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
1949 * Test repeat contribution passed in status.
1951 public function testRepeatTransactionPassedInStatus() {
1952 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
1954 $this->callAPISuccess('contribution', 'repeattransaction', array(
1955 'original_contribution_id' => $originalContribution['id'],
1956 'contribution_status_id' => 'Pending',
1957 'trxn_id' => uniqid(),
1959 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
1963 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1965 public function testRepeatTransactionAcceptRecurID() {
1966 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1967 'contact_id' => $this->_individualId
,
1968 'installments' => '12',
1969 'frequency_interval' => '1',
1971 'contribution_status_id' => 1,
1972 'start_date' => '2012-01-01 00:00:00',
1973 'currency' => 'USD',
1974 'frequency_unit' => 'month',
1975 'payment_processor_id' => $this->paymentProcessorID
,
1977 $this->callAPISuccess('contribution', 'create', array_merge(
1979 array('contribution_recur_id' => $contributionRecur['id']))
1982 $this->callAPISuccess('contribution', 'repeattransaction', array(
1983 'contribution_recur_id' => $contributionRecur['id'],
1984 'contribution_status_id' => 'Completed',
1985 'trxn_id' => uniqid(),
1988 $this->quickCleanUpFinancialEntities();
1992 * CRM-16397 test appropriate action if total amount has changed for single line items.
1994 public function testRepeatTransactionAlteredAmount() {
1995 $paymentProcessorID = $this->paymentProcessorCreate();
1996 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1997 'contact_id' => $this->_individualId
,
1998 'installments' => '12',
1999 'frequency_interval' => '1',
2001 'contribution_status_id' => 1,
2002 'start_date' => '2012-01-01 00:00:00',
2003 'currency' => 'USD',
2004 'frequency_unit' => 'month',
2005 'payment_processor_id' => $paymentProcessorID,
2007 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2010 'contribution_recur_id' => $contributionRecur['id'],
2014 $this->callAPISuccess('contribution', 'repeattransaction', array(
2015 'original_contribution_id' => $originalContribution['id'],
2016 'contribution_status_id' => 'Completed',
2017 'trxn_id' => uniqid(),
2018 'total_amount' => '400',
2022 $lineItemParams = array(
2023 'entity_id' => $originalContribution['id'],
2031 'financial_type_id',
2032 'deductible_amount',
2033 'price_field_value_id',
2037 $this->callAPISuccessGetSingle('contribution', array(
2038 'total_amount' => 400,
2040 'net_amount' => 350,
2042 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2043 'entity_id' => $originalContribution['id'],
2045 $expectedLineItem = array_merge(
2046 $lineItem1['values'][0], array(
2047 'line_total' => '400.00',
2048 'unit_price' => '400.00',
2052 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2053 'entity_id' => $originalContribution['id'] +
1,
2056 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2057 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2058 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2062 * CRM-17718 test appropriate action if financial type has changed for single line items.
2064 public function testRepeatTransactionPassedInFinancialType() {
2065 $originalContribution = $this->setUpRecurringContribution();
2067 $this->callAPISuccess('contribution', 'repeattransaction', array(
2068 'original_contribution_id' => $originalContribution['id'],
2069 'contribution_status_id' => 'Completed',
2070 'trxn_id' => uniqid(),
2071 'financial_type_id' => 2,
2073 $lineItemParams = array(
2074 'entity_id' => $originalContribution['id'],
2082 'financial_type_id',
2083 'deductible_amount',
2084 'price_field_value_id',
2089 $this->callAPISuccessGetSingle('contribution', array(
2090 'total_amount' => 100,
2091 'financial_type_id' => 2,
2093 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2094 'entity_id' => $originalContribution['id'],
2096 $expectedLineItem = array_merge(
2097 $lineItem1['values'][0], array(
2098 'line_total' => '100.00',
2099 'unit_price' => '100.00',
2100 'financial_type_id' => 2,
2101 'contribution_type_id' => 2,
2104 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2105 'entity_id' => $originalContribution['id'] +
1,
2107 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2108 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2109 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2113 * CRM-17718 test appropriate action if financial type has changed for single line items.
2115 public function testRepeatTransactionUpdatedFinancialType() {
2116 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2118 $this->callAPISuccess('contribution', 'repeattransaction', array(
2119 'contribution_recur_id' => $originalContribution['id'],
2120 'contribution_status_id' => 'Completed',
2121 'trxn_id' => uniqid(),
2123 $lineItemParams = array(
2124 'entity_id' => $originalContribution['id'],
2132 'financial_type_id',
2133 'deductible_amount',
2134 'price_field_value_id',
2139 $this->callAPISuccessGetSingle('contribution', array(
2140 'total_amount' => 100,
2141 'financial_type_id' => 2,
2143 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2144 'entity_id' => $originalContribution['id'],
2146 $expectedLineItem = array_merge(
2147 $lineItem1['values'][0], array(
2148 'line_total' => '100.00',
2149 'unit_price' => '100.00',
2150 'financial_type_id' => 2,
2151 'contribution_type_id' => 2,
2155 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2156 'entity_id' => $originalContribution['id'] +
1,
2158 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2159 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2160 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2164 * CRM-16397 test appropriate action if campaign has been passed in.
2166 public function testRepeatTransactionPassedInCampaign() {
2167 $paymentProcessorID = $this->paymentProcessorCreate();
2168 $campaignID = $this->campaignCreate();
2169 $campaignID2 = $this->campaignCreate();
2170 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2171 'contact_id' => $this->_individualId
,
2172 'installments' => '12',
2173 'frequency_interval' => '1',
2175 'contribution_status_id' => 1,
2176 'start_date' => '2012-01-01 00:00:00',
2177 'currency' => 'USD',
2178 'frequency_unit' => 'month',
2179 'payment_processor_id' => $paymentProcessorID,
2181 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2184 'contribution_recur_id' => $contributionRecur['id'],
2185 'campaign_id' => $campaignID,
2189 $this->callAPISuccess('contribution', 'repeattransaction', array(
2190 'original_contribution_id' => $originalContribution['id'],
2191 'contribution_status_id' => 'Completed',
2192 'trxn_id' => uniqid(),
2193 'campaign_id' => $campaignID2,
2196 $this->callAPISuccessGetSingle('contribution', array(
2197 'total_amount' => 100,
2198 'campaign_id' => $campaignID2,
2203 * CRM-17718 campaign stored on contribution recur gets priority.
2205 * This reflects the fact we permit people to update them.
2207 public function testRepeatTransactionUpdatedCampaign() {
2208 $paymentProcessorID = $this->paymentProcessorCreate();
2209 $campaignID = $this->campaignCreate();
2210 $campaignID2 = $this->campaignCreate();
2211 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2212 'contact_id' => $this->_individualId
,
2213 'installments' => '12',
2214 'frequency_interval' => '1',
2216 'contribution_status_id' => 1,
2217 'start_date' => '2012-01-01 00:00:00',
2218 'currency' => 'USD',
2219 'frequency_unit' => 'month',
2220 'payment_processor_id' => $paymentProcessorID,
2221 'campaign_id' => $campaignID,
2223 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2226 'contribution_recur_id' => $contributionRecur['id'],
2227 'campaign_id' => $campaignID2,
2231 $this->callAPISuccess('contribution', 'repeattransaction', array(
2232 'original_contribution_id' => $originalContribution['id'],
2233 'contribution_status_id' => 'Completed',
2234 'trxn_id' => uniqid(),
2237 $this->callAPISuccessGetSingle('contribution', array(
2238 'total_amount' => 100,
2239 'campaign_id' => $campaignID,
2244 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2246 public function testCompleteTransactionNetAmountOK() {
2247 $this->createLoggedInUser();
2248 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2249 unset($params['net_amount']);
2250 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2251 $this->callAPISuccess('contribution', 'completetransaction', array(
2252 'id' => $contribution['id'],
2254 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2255 $this->assertEquals('Completed', $contribution['contribution_status']);
2256 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2260 * CRM-14151 - Test completing a transaction via the API.
2262 public function testCompleteTransactionWithReceiptDateSet() {
2263 $this->swapMessageTemplateForTestTemplate();
2264 $mut = new CiviMailUtils($this, TRUE);
2265 $this->createLoggedInUser();
2266 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2267 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2268 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2269 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2270 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2271 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2272 $mut->checkMailLog(array(
2273 'Receipt - Contribution',
2274 'receipt_date:::' . date('Ymd'),
2277 $this->revertTemplateToReservedTemplate();
2281 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2283 public function testCompleteTransactionWithEmailReceiptInput() {
2284 // Create a Contribution Page with is_email_receipt = TRUE
2285 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2286 'receipt_from_name' => 'Mickey Mouse',
2287 'receipt_from_email' => 'mickey@mouse.com',
2288 'title' => "Test Contribution Page",
2289 'financial_type_id' => 1,
2290 'currency' => 'CAD',
2291 'is_monetary' => TRUE,
2292 'is_email_receipt' => TRUE,
2294 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2295 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2296 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2297 // Complete the transaction overriding is_email_receipt to = FALSE
2298 $this->callAPISuccess('contribution', 'completetransaction', array(
2299 'id' => $contribution['id'],
2300 'trxn_date' => date('2011-04-09'),
2301 'trxn_id' => 'kazam',
2302 'is_email_receipt' => 0,
2304 // Check if a receipt was issued
2305 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2306 $this->assertEquals('', $receipt_date);
2310 * Complete the transaction using the template with all the possible.
2312 public function testCompleteTransactionWithTestTemplate() {
2313 $this->swapMessageTemplateForTestTemplate();
2314 $contribution = $this->setUpForCompleteTransaction();
2315 $this->callAPISuccess('contribution', 'completetransaction', array(
2316 'id' => $contribution['id'],
2317 'trxn_date' => date('2011-04-09'),
2318 'trxn_id' => 'kazam',
2320 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2321 $this->mut
->checkMailLog(array(
2322 'email:::anthony_anderson@civicrm.org',
2326 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2327 'receipt_date:::' . date('Ymd'),
2328 'contributeMode:::notify',
2329 'title:::Contribution',
2330 'displayName:::Mr. Anthony Anderson II',
2332 'contactID:::' . $this->_params
['contact_id'],
2333 'contributionID:::' . $contribution['id'],
2334 'financialTypeId:::1',
2335 'financialTypeName:::Donation',
2338 $this->revertTemplateToReservedTemplate();
2342 * Complete the transaction using the template with all the possible.
2344 public function testCompleteTransactionContributionPageFromAddress() {
2345 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2346 'receipt_from_name' => 'Mickey Mouse',
2347 'receipt_from_email' => 'mickey@mouse.com',
2348 'title' => "Test Contribution Page",
2349 'financial_type_id' => 1,
2350 'currency' => 'NZD',
2351 'goal_amount' => 50,
2352 'is_pay_later' => 1,
2353 'is_monetary' => TRUE,
2354 'is_email_receipt' => TRUE,
2356 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2357 $contribution = $this->setUpForCompleteTransaction();
2358 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2359 $this->mut
->checkMailLog(array(
2367 * Test completing first transaction in a recurring series.
2369 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2371 public function testCompleteTransactionSetStatusToInProgress() {
2372 $paymentProcessorID = $this->paymentProcessorCreate();
2373 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2374 'contact_id' => $this->_individualId
,
2375 'installments' => '12',
2376 'frequency_interval' => '1',
2378 'contribution_status_id' => 'Pending',
2379 'start_date' => '2012-01-01 00:00:00',
2380 'currency' => 'USD',
2381 'frequency_unit' => 'month',
2382 'payment_processor_id' => $paymentProcessorID,
2384 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2387 'contribution_recur_id' => $contributionRecur['id'],
2388 'contribution_status_id' => 'Pending',
2391 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2392 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2393 'id' => $contributionRecur['id'],
2394 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2396 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2397 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2401 * Test completing a pledge with the completeTransaction api..
2403 * Note that we are creating a logged in user because email goes out from
2406 public function testCompleteTransactionUpdatePledgePayment() {
2407 $this->swapMessageTemplateForTestTemplate();
2408 $mut = new CiviMailUtils($this, TRUE);
2409 $mut->clearMessages();
2410 $this->createLoggedInUser();
2411 $contributionID = $this->createPendingPledgeContribution();
2412 $this->callAPISuccess('contribution', 'completetransaction', array(
2413 'id' => $contributionID,
2414 'trxn_date' => '1 Feb 2013',
2416 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2417 'id' => $this->_ids
['pledge'],
2419 $this->assertEquals('Completed', $pledge['pledge_status']);
2421 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2422 'pledge_id' => $this->_ids
['pledge'],
2423 'return' => 'status_id',
2425 $this->assertEquals(1, $status);
2426 $mut->checkMailLog(array(
2428 'receive_date:::20130201000000',
2429 "receipt_date:::\n",
2432 $this->revertTemplateToReservedTemplate();
2436 * Test completing a transaction with an event via the API.
2438 * Note that we are creating a logged in user because email goes out from
2441 public function testCompleteTransactionWithParticipantRecord() {
2442 $mut = new CiviMailUtils($this, TRUE);
2443 $mut->clearMessages();
2444 $this->createLoggedInUser();
2445 $contributionID = $this->createPendingParticipantContribution();
2446 $this->callAPISuccess('contribution', 'completetransaction', array(
2447 'id' => $contributionID,
2450 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2451 'id' => $this->_ids
['participant'],
2452 'return' => 'participant_status_id',
2454 $this->assertEquals(1, $participantStatus);
2455 $mut->checkMailLog(array(
2456 'Annual CiviCRM meet',
2458 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2464 * Test membership is renewed when transaction completed.
2466 public function testCompleteTransactionMembershipPriceSet() {
2467 $this->createPriceSetWithPage('membership');
2468 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2472 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2473 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2474 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2475 'membership_id' => $this->_ids
['membership'],
2477 $this->assertEquals(1, $logs['count']);
2478 $this->assertEquals($stateOfGrace, $membership['status_id']);
2479 $contribution = $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2480 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2481 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2482 $this->callAPISuccessGetSingle('LineItem', array(
2483 'entity_id' => $this->_ids
['membership'],
2484 'entity_table' => 'civicrm_membership',
2486 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2487 'membership_id' => $this->_ids
['membership'],
2489 //CRM-19600: Ensure that 'Membership Renewal' activity is created after successful membership regsitration
2490 $activity = $this->callAPISuccess('Activity', 'get', array(
2491 'activity_type_id' => 'Membership Renewal',
2492 'source_record_id' => $contribution['id'],
2494 $this->assertEquals(1, $activity['count']);
2495 $this->assertEquals(2, $logs['count']);
2496 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2497 $this->cleanUpAfterPriceSets();
2501 * Test membership is renewed when transaction completed.
2503 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2504 $this->createPriceSetWithPage('membership');
2505 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
2506 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2507 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2508 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2509 $this->cleanUpAfterPriceSets();
2512 public function cleanUpAfterPriceSets() {
2513 $this->quickCleanUpFinancialEntities();
2514 $this->contactDelete($this->_ids
['contact']);
2519 * Create price set with contribution test for test setup.
2521 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
2522 * on parent class at some point (fn is not in 4.4).
2525 * @param array $params
2527 public function createPriceSetWithPage($entity, $params = array()) {
2528 $membershipTypeID = $this->membershipTypeCreate();
2529 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
2530 'title' => "Test Contribution Page",
2531 'financial_type_id' => 1,
2532 'currency' => 'NZD',
2533 'goal_amount' => 50,
2534 'is_pay_later' => 1,
2535 'is_monetary' => TRUE,
2536 'is_email_receipt' => FALSE,
2538 $priceSet = $this->callAPISuccess('price_set', 'create', array(
2539 'is_quick_config' => 0,
2540 'extends' => 'CiviMember',
2541 'financial_type_id' => 1,
2542 'title' => 'my Page',
2544 $priceSetID = $priceSet['id'];
2546 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
2547 $priceField = $this->callAPISuccess('price_field', 'create', array(
2548 'price_set_id' => $priceSetID,
2549 'label' => 'Goat Breed',
2550 'html_type' => 'Radio',
2552 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2553 'price_set_id' => $priceSetID,
2554 'price_field_id' => $priceField['id'],
2555 'label' => 'Long Haired Goat',
2557 'financial_type_id' => 'Donation',
2558 'membership_type_id' => $membershipTypeID,
2559 'membership_num_terms' => 1,
2562 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
2563 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2564 'price_set_id' => $priceSetID,
2565 'price_field_id' => $priceField['id'],
2566 'label' => 'Shoe-eating Goat',
2568 'financial_type_id' => 'Donation',
2569 'membership_type_id' => $membershipTypeID,
2570 'membership_num_terms' => 2,
2573 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
2574 $this->_ids
['price_set'] = $priceSetID;
2575 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
2576 $this->_ids
['price_field'] = array($priceField['id']);
2578 $this->_ids
['membership_type'] = $membershipTypeID;
2582 * Set up a pending transaction with a specific price field id.
2584 * @param int $priceFieldValueID
2586 public function setUpPendingContribution($priceFieldValueID) {
2587 $contactID = $this->individualCreate();
2588 $membership = $this->callAPISuccess('membership', 'create', array(
2589 'contact_id' => $contactID,
2590 'membership_type_id' => $this->_ids
['membership_type'],
2591 'start_date' => 'yesterday - 1 year',
2592 'end_date' => 'yesterday',
2593 'join_date' => 'yesterday - 1 year',
2595 $contribution = $this->callAPISuccess('contribution', 'create', array(
2597 'contact_id' => $contactID,
2598 'receive_date' => date('Ymd'),
2599 'total_amount' => 100.00,
2600 'financial_type_id' => 1,
2601 'payment_instrument_id' => 'Credit Card',
2602 'non_deductible_amount' => 10.00,
2603 'trxn_id' => 'jdhfi88',
2604 'invoice_id' => 'djfhiewuyr',
2606 'contribution_status_id' => 2,
2607 'contribution_page_id' => $this->_ids
['contribution_page'],
2608 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2611 $this->callAPISuccess('line_item', 'create', array(
2612 'entity_id' => $contribution['id'],
2613 'entity_table' => 'civicrm_contribution',
2614 'contribution_id' => $contribution['id'],
2615 'price_field_id' => $this->_ids
['price_field'][0],
2619 'financial_type_id' => 1,
2620 'price_field_value_id' => $priceFieldValueID,
2622 $this->_ids
['contact'] = $contactID;
2623 $this->_ids
['contribution'] = $contribution['id'];
2624 $this->_ids
['membership'] = $membership['id'];
2628 * Test sending a mail via the API.
2630 public function testSendMail() {
2631 $mut = new CiviMailUtils($this, TRUE);
2632 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
2633 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2634 'id' => $contribution['id'],
2635 'receipt_from_email' => 'api@civicrm.org',
2638 $mut->checkMailLog(array(
2640 'Contribution Information',
2641 'Please print this confirmation for your records',
2647 $this->checkCreditCardDetails($mut, $contribution['id']);
2652 * Check credit card details in sent mail via API
2654 * @param $mut obj CiviMailUtils instance
2655 * @param int $contributionID Contribution ID
2658 public function checkCreditCardDetails($mut, $contributionID) {
2659 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
2660 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2661 'id' => $contributionID,
2662 'receipt_from_email' => 'api@civicrm.org',
2663 'payment_processor_id' => $this->paymentProcessorID
,
2666 $mut->checkMailLog(array(
2667 'Credit Card Information', // credit card header
2668 'Billing Name and Address', // billing header
2669 'anthony_anderson@civicrm.org', // billing name
2677 * Test sending a mail via the API.
2679 public function testSendMailEvent() {
2680 $mut = new CiviMailUtils($this, TRUE);
2681 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
2682 $event = $this->eventCreate(array(
2683 'is_email_confirm' => 1,
2684 'confirm_from_email' => 'test@civicrm.org',
2686 $this->_eventID
= $event['id'];
2687 $participantParams = array(
2688 'contact_id' => $this->_individualId
,
2689 'event_id' => $this->_eventID
,
2692 // to ensure it matches later on
2693 'register_date' => '2007-07-21 00:00:00',
2694 'source' => 'Online Event Registration: API Testing',
2697 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2698 $this->callAPISuccess('participant_payment', 'create', array(
2699 'participant_id' => $participant['id'],
2700 'contribution_id' => $contribution['id'],
2702 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2703 'id' => $contribution['id'],
2704 'receipt_from_email' => 'api@civicrm.org',
2708 $mut->checkMailLog(array(
2709 'Annual CiviCRM meet',
2711 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
2718 * This function does a GET & compares the result against the $params.
2720 * Use as a double check on Creates.
2722 * @param array $params
2724 * @param bool $delete
2726 public function contributionGetnCheck($params, $id, $delete = TRUE) {
2728 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
2734 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
2736 $this->assertAPISuccess($contribution, 0);
2737 $values = $contribution['values'][$contribution['id']];
2738 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2739 // this is not returned in id format
2740 unset($params['payment_instrument_id']);
2741 $params['contribution_source'] = $params['source'];
2742 unset($params['source']);
2743 foreach ($params as $key => $value) {
2744 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
2749 * Create a pending contribution & linked pending pledge record.
2751 public function createPendingPledgeContribution() {
2753 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
2754 $this->_ids
['pledge'] = $pledgeID;
2755 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
2756 'contribution_status_id' => 'Pending',
2757 'total_amount' => 500,
2760 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
2761 'options' => array('limit' => 1),
2764 $this->callAPISuccess('PledgePayment', 'create', array(
2766 'contribution_id' =>
2767 $contribution['id'],
2768 'status_id' => 'Pending',
2769 'scheduled_amount' => 500,
2772 return $contribution['id'];
2776 * Create a pending contribution & linked pending participant record (along with an event).
2778 public function createPendingParticipantContribution() {
2779 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
2780 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
2781 $this->_ids
['participant'] = $participantID;
2782 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
2783 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2784 $this->callAPISuccess('participant_payment', 'create', array(
2785 'contribution_id' => $contribution['id'],
2786 'participant_id' => $participantID,
2788 $this->callAPISuccess('line_item', 'get', array(
2789 'entity_id' => $contribution['id'],
2790 'entity_table' => 'civicrm_contribution',
2791 'api.line_item.create' => array(
2792 'entity_id' => $participantID,
2793 'entity_table' => 'civicrm_participant',
2796 return $contribution['id'];
2800 * Get financial transaction amount.
2802 * @param int $contId
2804 * @return null|string
2806 public function _getFinancialTrxnAmount($contId) {
2808 SUM( ft.total_amount ) AS total
2809 FROM civicrm_financial_trxn AS ft
2810 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2811 WHERE ceft.entity_table = 'civicrm_contribution'
2812 AND ceft.entity_id = {$contId}";
2814 $result = CRM_Core_DAO
::singleValueQuery($query);
2819 * @param int $contId
2821 * @return null|string
2823 public function _getFinancialItemAmount($contId) {
2824 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
2827 FROM civicrm_financial_item
2828 WHERE entity_table = 'civicrm_line_item'
2829 AND entity_id = {$lineItem}";
2830 $result = CRM_Core_DAO
::singleValueQuery($query);
2835 * @param int $contId
2838 public function _checkFinancialItem($contId, $context) {
2839 if ($context != 'paylater') {
2841 'entity_id' => $contId,
2842 'entity_table' => 'civicrm_contribution',
2844 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
2845 $entityParams = array(
2846 'financial_trxn_id' => $trxn['financial_trxn_id'],
2847 'entity_table' => 'civicrm_financial_item',
2849 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2851 'id' => $entityTrxn['entity_id'],
2854 if ($context == 'paylater') {
2855 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
2856 foreach ($lineItems as $key => $item) {
2858 'entity_id' => $key,
2859 'entity_table' => 'civicrm_line_item',
2861 $compareParams = array('status_id' => 1);
2862 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2865 elseif ($context == 'refund') {
2866 $compareParams = array(
2868 'financial_account_id' => 1,
2872 elseif ($context == 'cancelPending') {
2873 $compareParams = array(
2875 'financial_account_id' => 1,
2879 elseif ($context == 'changeFinancial') {
2880 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
2882 'entity_id' => $lineKey,
2885 $compareParams = array(
2886 'financial_account_id' => 1,
2888 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2890 'financial_account_id' => 3,
2891 'entity_id' => $lineKey,
2893 $compareParams = array(
2897 if ($context != 'paylater') {
2898 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2903 * Check financial transaction.
2905 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
2907 * @param array $contribution
2908 * @param string $context
2909 * @param int $instrumentId
2910 * @param array $extraParams
2912 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
2913 $trxnParams = array(
2914 'entity_id' => $contribution['id'],
2915 'entity_table' => 'civicrm_contribution',
2917 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2919 'id' => $trxn['financial_trxn_id'],
2921 if ($context == 'payLater') {
2922 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2923 $compareParams = array(
2925 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
2928 elseif ($context == 'refund') {
2929 $compareParams = array(
2930 'to_financial_account_id' => 6,
2931 'total_amount' => -100,
2933 'trxn_date' => '2015-01-01 09:00:00',
2934 'trxn_id' => 'the refund',
2937 elseif ($context == 'cancelPending') {
2938 $compareParams = array(
2939 'to_financial_account_id' => 7,
2940 'total_amount' => -100,
2944 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
2945 $entityParams = array(
2946 'entity_id' => $contribution['id'],
2947 'entity_table' => 'civicrm_contribution',
2950 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2951 $trxnParams1 = array(
2952 'id' => $trxn['financial_trxn_id'],
2954 if (empty($extraParams)) {
2955 $compareParams = array(
2956 'total_amount' => -100,
2961 $compareParams = array(
2962 'total_amount' => 100,
2966 if ($context == 'paymentInstrument') {
2967 $compareParams +
= array(
2968 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
2969 'payment_instrument_id' => 4,
2973 $compareParams['to_financial_account_id'] = 12;
2975 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
2976 $compareParams['total_amount'] = 100;
2977 if ($context == 'paymentInstrument') {
2978 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
2979 $compareParams['payment_instrument_id'] = $instrumentId;
2982 $compareParams['to_financial_account_id'] = 12;
2986 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
2992 public function _addPaymentInstrument() {
2993 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2994 $optionParams = array(
2995 'option_group_id' => $gId,
2996 'label' => 'Test Card',
2997 'name' => 'Test Card',
3002 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3003 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3004 $financialParams = array(
3005 'entity_table' => 'civicrm_option_value',
3006 'entity_id' => $optionValue['id'],
3007 'account_relationship' => $relationTypeId,
3008 'financial_account_id' => 7,
3010 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3011 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3012 return $optionValue['values'][$optionValue['id']]['value'];
3016 * Set up the basic recurring contribution for tests.
3018 * @param array $generalParams
3019 * Parameters that can be merged into the recurring AND the contribution.
3021 * @param array $recurParams
3022 * Parameters to merge into the recur only.
3026 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3027 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3028 'contact_id' => $this->_individualId
,
3029 'installments' => '12',
3030 'frequency_interval' => '1',
3032 'contribution_status_id' => 1,
3033 'start_date' => '2012-01-01 00:00:00',
3034 'currency' => 'USD',
3035 'frequency_unit' => 'month',
3036 'payment_processor_id' => $this->paymentProcessorID
,
3037 ), $generalParams, $recurParams));
3038 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3041 'contribution_recur_id' => $contributionRecur['id'],
3044 return $originalContribution;
3048 * Set up a repeat transaction.
3050 * @param array $recurParams
3054 protected function setUpRepeatTransaction($recurParams = array(), $flag) {
3055 $paymentProcessorID = $this->paymentProcessorCreate();
3056 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3057 'contact_id' => $this->_individualId
,
3058 'installments' => '12',
3059 'frequency_interval' => '1',
3061 'contribution_status_id' => 1,
3062 'start_date' => '2012-01-01 00:00:00',
3063 'currency' => 'USD',
3064 'frequency_unit' => 'month',
3065 'payment_processor_id' => $paymentProcessorID,
3068 $originalContribution = '';
3069 if ($flag == 'multiple') {
3070 // CRM-19309 create a contribution + also add in line_items (plural):
3071 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3074 'contribution_recur_id' => $contributionRecur['id'],
3075 'skipLineItem' => 1,
3076 'api.line_item.create' => array(
3078 'price_field_id' => 1,
3080 'line_total' => '20',
3081 'unit_price' => '10',
3082 'financial_type_id' => 1,
3085 'price_field_id' => 1,
3087 'line_total' => '80',
3088 'unit_price' => '80',
3089 'financial_type_id' => 2,
3096 elseif ($flag == 'single') {
3097 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3099 array('contribution_recur_id' => $contributionRecur['id']))
3102 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3103 return $originalContribution;
3107 * Common set up routine.
3111 protected function setUpForCompleteTransaction() {
3112 $this->mut
= new CiviMailUtils($this, TRUE);
3113 $this->createLoggedInUser();
3114 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3115 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3116 return $contribution;