3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_contribute_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
35 class api_v3_ContributionTest
extends CiviUnitTestCase
{
37 use CRMTraits_Profile_ProfileTrait
;
39 protected $_individualId;
40 protected $_contribution;
41 protected $_financialTypeId = 1;
42 protected $_apiversion;
43 protected $_entity = 'Contribution';
47 protected $_pageParams = [];
49 * Payment processor ID (dummy processor).
53 protected $paymentProcessorID;
56 * Parameters to create payment processor.
60 protected $_processorParams = [];
63 * ID of created event.
77 public function setUp() {
80 $this->_apiversion
= 3;
81 $this->_individualId
= $this->individualCreate();
83 'contact_id' => $this->_individualId
,
84 'receive_date' => '20120511',
85 'total_amount' => 100.00,
86 'financial_type_id' => $this->_financialTypeId
,
87 'non_deductible_amount' => 10.00,
89 'net_amount' => 95.00,
91 'contribution_status_id' => 1,
93 $this->_processorParams
= [
96 'payment_processor_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
97 'financial_account_id' => 12,
100 'url_site' => 'http://dummy.com',
101 'url_recur' => 'http://dummy.com',
104 $this->paymentProcessorID
= $this->processorCreate();
105 $this->_pageParams
= [
106 'title' => 'Test Contribution Page',
107 'financial_type_id' => 1,
109 'financial_account_id' => 1,
110 'payment_processor' => $this->paymentProcessorID
,
112 'is_allow_other_amount' => 1,
114 'max_amount' => 1000,
119 * Clean up after each test.
121 public function tearDown() {
122 $this->quickCleanUpFinancialEntities();
123 $this->quickCleanup(['civicrm_uf_match']);
124 $financialAccounts = $this->callAPISuccess('FinancialAccount', 'get', []);
125 foreach ($financialAccounts['values'] as $financialAccount) {
126 if ($financialAccount['name'] == 'Test Tax financial account ' ||
$financialAccount['name'] == 'Test taxable financial Type') {
127 $entityFinancialTypes = $this->callAPISuccess('EntityFinancialAccount', 'get', [
128 'financial_account_id' => $financialAccount['id'],
130 foreach ($entityFinancialTypes['values'] as $entityFinancialType) {
131 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialType['id']]);
133 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
141 public function testGetContribution() {
142 $contributionSettings = $this->enableTaxAndInvoicing();
143 $invoice_prefix = CRM_Contribute_BAO_Contribution
::checkContributeSettings('invoice_prefix', TRUE);
145 'contact_id' => $this->_individualId
,
146 'receive_date' => '2010-01-20',
147 'total_amount' => 100.00,
148 'financial_type_id' => $this->_financialTypeId
,
149 'non_deductible_amount' => 10.00,
150 'fee_amount' => 5.00,
151 'net_amount' => 95.00,
153 'invoice_id' => 78910,
155 'contribution_status_id' => 1,
157 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
160 'contribution_id' => $this->_contribution
['id'],
163 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
164 $financialParams['id'] = $this->_financialTypeId
;
166 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
168 $this->assertEquals(1, $contributions['count']);
169 $contribution = $contributions['values'][$contributions['id']];
170 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
171 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
172 // Passing back a string rather than an id seems like an error/cruft.
173 // If it is to be introduced we should discuss.
174 $this->assertEquals($contribution['financial_type_id'], 1);
175 $this->assertEquals($contribution['total_amount'], 100.00);
176 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
177 $this->assertEquals($contribution['fee_amount'], 5.00);
178 $this->assertEquals($contribution['net_amount'], 95.00);
179 $this->assertEquals($contribution['trxn_id'], 23456);
180 $this->assertEquals($contribution['invoice_id'], 78910);
181 $this->assertEquals($contribution['invoice_number'], $invoice_prefix . $contributions['id']);
182 $this->assertEquals($contribution['contribution_source'], 'SSF');
183 $this->assertEquals($contribution['contribution_status'], 'Completed');
184 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
185 $p['trxn_id'] = '3847';
186 $p['invoice_id'] = '3847';
188 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
190 // Now we have 2 - test getcount.
191 $contribution = $this->callAPISuccess('contribution', 'getcount', []);
192 $this->assertEquals(2, $contribution);
193 // Test id only format.
194 $contribution = $this->callAPISuccess('contribution', 'get', [
195 'id' => $this->_contribution
['id'],
196 'format.only_id' => 1,
198 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
199 // Test id only format.
200 $contribution = $this->callAPISuccess('contribution', 'get', [
201 'id' => $contribution2['id'],
202 'format.only_id' => 1,
204 $this->assertEquals($contribution2['id'], $contribution);
206 $contribution = $this->callAPISuccess('contribution', 'get', [
207 'id' => $this->_contribution
['id'],
209 $this->assertEquals(1, $contribution['count']);
211 // Test get by contact id works.
212 $contribution = $this->callAPISuccess('contribution', 'get', ['contact_id' => $this->_individualId
]);
214 $this->assertEquals(2, $contribution['count']);
215 $this->callAPISuccess('Contribution', 'Delete', [
216 'id' => $this->_contribution
['id'],
218 $this->callAPISuccess('Contribution', 'Delete', [
219 'id' => $contribution2['id'],
224 * Test that test contributions can be retrieved.
226 public function testGetTestContribution() {
227 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, ['is_test' => 1]));
228 $this->callAPISuccessGetSingle('Contribution', ['is_test' => 1]);
232 * Test Creating a check contribution with original check_number field
234 public function testCreateCheckContribution() {
235 $params = $this->_params
;
236 $params['contribution_check_number'] = 'bouncer';
237 $params['payment_instrument_id'] = 'Check';
238 $params['cancel_date'] = 'yesterday';
239 $params['receipt_date'] = 'yesterday';
240 $params['thankyou_date'] = 'yesterday';
241 $params['revenue_recognition_date'] = 'yesterday';
242 $params['amount_level'] = 'Unreasonable';
243 $params['cancel_reason'] = 'You lose sucker';
244 $params['creditnote_id'] = 'sudo rm -rf';
245 $params['tax_amount'] = '1';
246 $address = $this->callAPISuccess('Address', 'create', [
247 'street_address' => 'Knockturn Alley',
248 'contact_id' => $this->_individualId
,
249 'location_type_id' => 'Home',
251 $params['address_id'] = $address['id'];
252 $contributionPage = $this->contributionPageCreate();
253 $params['contribution_page_id'] = $contributionPage['id'];
254 $params['campaign_id'] = $this->campaignCreate();
255 $contributionID = $this->contributionCreate($params);
256 $getResult = $this->callAPISuccess('Contribution', 'get', ['id' => $contributionID]);
257 $this->assertEquals('bouncer', $getResult['values'][$contributionID]['check_number']);
258 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['entity_id' => $contributionID, 'entity_table' => 'civicrm_contribution']);
259 foreach ($entityFinancialTrxn['values'] as $eft) {
260 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'get', ['id' => $eft['financial_trxn_id']]);
261 $this->assertEquals('bouncer', $financialTrxn['values'][$financialTrxn['id']]['check_number']);
266 * Test the 'return' param works for all fields.
268 public function testGetContributionReturnFunctionality() {
269 $params = $this->_params
;
270 $params['contribution_check_number'] = 'bouncer';
271 $params['payment_instrument_id'] = 'Check';
272 $params['cancel_date'] = 'yesterday';
273 $params['receipt_date'] = 'yesterday';
274 $params['thankyou_date'] = 'yesterday';
275 $params['revenue_recognition_date'] = 'yesterday';
276 $params['amount_level'] = 'Unreasonable';
277 $params['cancel_reason'] = 'You lose sucker';
278 $params['creditnote_id'] = 'sudo rm -rf';
279 $params['tax_amount'] = '1';
280 $address = $this->callAPISuccess('Address', 'create', [
281 'street_address' => 'Knockturn Alley',
282 'contact_id' => $this->_individualId
,
283 'location_type_id' => 'Home',
285 $params['address_id'] = $address['id'];
286 $contributionPage = $this->contributionPageCreate();
287 $params['contribution_page_id'] = $contributionPage['id'];
288 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', [
289 'contact_id' => $this->_individualId
,
290 'frequency_interval' => 1,
293 $params['contribution_recur_id'] = $contributionRecur['id'];
295 $params['campaign_id'] = $this->campaignCreate();
297 $contributionID = $this->contributionCreate($params);
299 // update contribution with invoice number
300 $params = array_merge($params, [
301 'id' => $contributionID,
302 'invoice_number' => CRM_Utils_Array
::value('invoice_prefix', Civi
::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
304 'invoice_id' => 6789,
306 $contributionID = $this->contributionCreate($params);
308 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
309 $this->assertEquals('bouncer', $contribution['check_number']);
310 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
312 $fields = CRM_Contribute_BAO_Contribution
::fields();
313 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
314 // need to specify them.
315 $fields['address_id'] = $fields['contribution_address_id'];
316 $fields['check_number'] = $fields['contribution_check_number'];
319 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
320 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
321 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'contribution_cancel_date', 'cancel_reason',
322 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
323 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
324 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
326 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
327 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
328 // covered as the test contract now guarantees them in the return array.
329 $this->assertEquals($missingFields, [29 => 'decoy'], 'A field which was covered by the test contract has changed.');
330 foreach ($fields as $fieldName => $fieldSpec) {
331 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID, 'return' => $fieldName]);
332 $returnField = $fieldName;
333 if ($returnField == 'contribution_contact_id') {
334 $returnField = 'contact_id';
336 $this->assertTrue((!empty($contribution[$returnField]) ||
$contribution[$returnField] === "0"), $returnField);
338 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['entity_id' => $contributionID, 'entity_table' => 'civicrm_contribution']);
339 foreach ($entityFinancialTrxn['values'] as $eft) {
340 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'get', ['id' => $eft['financial_trxn_id']]);
341 $this->assertEquals('bouncer', $financialTrxn['values'][$financialTrxn['id']]['check_number']);
346 * Test cancel reason works as a filter.
348 public function testFilterCancelReason() {
349 $params = $this->_params
;
350 $params['cancel_date'] = 'yesterday';
351 $params['cancel_reason'] = 'You lose sucker';
352 $this->callAPISuccess('Contribution', 'create', $params);
353 $params = $this->_params
;
354 $params['cancel_date'] = 'yesterday';
355 $params['cancel_reason'] = 'You are a winner';
356 $this->callAPISuccess('Contribution', 'create', $params);
357 $this->callAPISuccessGetCount('Contribution', ['cancel_reason' => 'You are a winner'], 1);
361 * We need to ensure previous tested behaviour still works as part of the api contract.
363 public function testGetContributionLegacyBehaviour() {
365 'contact_id' => $this->_individualId
,
366 'receive_date' => '2010-01-20',
367 'total_amount' => 100.00,
368 'contribution_type_id' => $this->_financialTypeId
,
369 'non_deductible_amount' => 10.00,
370 'fee_amount' => 5.00,
371 'net_amount' => 95.00,
373 'invoice_id' => 78910,
375 'contribution_status_id' => 1,
377 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
380 'contribution_id' => $this->_contribution
['id'],
382 $contribution = $this->callAPISuccess('contribution', 'get', $params);
383 $financialParams['id'] = $this->_financialTypeId
;
385 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
387 $this->assertEquals(1, $contribution['count']);
388 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
389 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
390 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
391 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
392 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
393 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
394 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
395 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
396 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
397 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
398 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
400 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
401 $p['trxn_id'] = '3847';
402 $p['invoice_id'] = '3847';
404 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
406 // now we have 2 - test getcount
407 $contribution = $this->callAPISuccess('contribution', 'getcount', []);
408 $this->assertEquals(2, $contribution);
409 //test id only format
410 $contribution = $this->callAPISuccess('contribution', 'get', [
411 'id' => $this->_contribution
['id'],
412 'format.only_id' => 1,
414 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
415 //test id only format
416 $contribution = $this->callAPISuccess('contribution', 'get', [
417 'id' => $contribution2['id'],
418 'format.only_id' => 1,
420 $this->assertEquals($contribution2['id'], $contribution);
421 $contribution = $this->callAPISuccess('contribution', 'get', [
422 'id' => $this->_contribution
['id'],
425 $this->assertEquals(1, $contribution['count']);
426 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
427 //test get by contact id works
428 $contribution = $this->callAPISuccess('contribution', 'get', ['contact_id' => $this->_individualId
]);
430 $this->assertEquals(2, $contribution['count']);
431 $this->callAPISuccess('Contribution', 'Delete', [
432 'id' => $this->_contribution
['id'],
434 $this->callAPISuccess('Contribution', 'Delete', [
435 'id' => $contribution2['id'],
440 * Create an contribution_id=FALSE and financial_type_id=Donation.
442 public function testCreateEmptyContributionIDUseDonation() {
444 'contribution_id' => FALSE,
447 'check_permissions' => FALSE,
448 'financial_type_id' => 'Donation',
450 $this->callAPISuccess('contribution', 'create', $params);
454 * Check with complete array + custom field.
456 * Note that the test is written on purpose without any
457 * variables specific to participant so it can be replicated into other entities
458 * and / or moved to the automated test suite
460 public function testCreateWithCustom() {
461 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
463 $params = $this->_params
;
464 $params['custom_' . $ids['custom_field_id']] = "custom string";
466 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
467 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
468 $check = $this->callAPISuccess($this->_entity
, 'get', [
469 'return.custom_' . $ids['custom_field_id'] => 1,
470 'id' => $result['id'],
472 $this->customFieldDelete($ids['custom_field_id']);
473 $this->customGroupDelete($ids['custom_group_id']);
474 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
478 * Check with complete array + custom field.
480 * Note that the test is written on purpose without any
481 * variables specific to participant so it can be replicated into other entities
482 * and / or moved to the automated test suite
484 public function testCreateGetFieldsWithCustom() {
485 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
486 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
487 $result = $this->callAPISuccess('Contribution', 'getfields', []);
488 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
489 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
490 $this->customFieldDelete($ids['custom_field_id']);
491 $this->customGroupDelete($ids['custom_group_id']);
492 $this->customFieldDelete($idsContact['custom_field_id']);
493 $this->customGroupDelete($idsContact['custom_group_id']);
496 public function testCreateContributionNoLineItems() {
499 'contact_id' => $this->_individualId
,
500 'receive_date' => '20120511',
501 'total_amount' => 100.00,
502 'financial_type_id' => $this->_financialTypeId
,
503 'payment_instrument_id' => 1,
504 'non_deductible_amount' => 10.00,
505 'fee_amount' => 50.00,
506 'net_amount' => 90.00,
508 'invoice_id' => 67890,
510 'contribution_status_id' => 1,
514 $contribution = $this->callAPISuccess('contribution', 'create', $params);
515 $lineItems = $this->callAPISuccess('line_item', 'get', [
516 'entity_id' => $contribution['id'],
517 'entity_table' => 'civicrm_contribution',
520 $this->assertEquals(0, $lineItems['count']);
524 * Test checks that passing in line items suppresses the create mechanism.
526 public function testCreateContributionChainedLineItems() {
528 'contact_id' => $this->_individualId
,
529 'receive_date' => '20120511',
530 'total_amount' => 100.00,
531 'financial_type_id' => $this->_financialTypeId
,
532 'payment_instrument_id' => 1,
533 'non_deductible_amount' => 10.00,
534 'fee_amount' => 50.00,
535 'net_amount' => 90.00,
537 'invoice_id' => 67890,
539 'contribution_status_id' => 1,
541 'api.line_item.create' => [
543 'price_field_id' => 1,
545 'line_total' => '20',
546 'unit_price' => '10',
549 'price_field_id' => 1,
551 'line_total' => '80',
552 'unit_price' => '80',
557 $description = "Create Contribution with Nested Line Items.";
558 $subfile = "CreateWithNestedLineItems";
559 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
561 $lineItems = $this->callAPISuccess('line_item', 'get', [
562 'entity_id' => $contribution['id'],
563 'contribution_id' => $contribution['id'],
564 'entity_table' => 'civicrm_contribution',
567 $this->assertEquals(2, $lineItems['count']);
570 public function testCreateContributionOffline() {
572 'contact_id' => $this->_individualId
,
573 'receive_date' => '20120511',
574 'total_amount' => 100.00,
575 'financial_type_id' => 1,
577 'invoice_id' => 67890,
579 'contribution_status_id' => 1,
582 $contribution = $this->callAPISuccess('contribution', 'create', $params);
583 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
584 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
585 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
586 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
587 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
588 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
589 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
590 $lineItems = $this->callAPISuccess('line_item', 'get', [
591 'entity_id' => $contribution['id'],
592 'contribution_id' => $contribution['id'],
593 'entity_table' => 'civicrm_contribution',
596 $this->assertEquals(1, $lineItems['count']);
597 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
598 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
599 $this->_checkFinancialRecords($contribution, 'offline');
600 $this->contributionGetnCheck($params, $contribution['id']);
604 * Test create with valid payment instrument.
606 public function testCreateContributionWithPaymentInstrument() {
607 $params = $this->_params +
['payment_instrument' => 'EFT'];
608 $contribution = $this->callAPISuccess('contribution', 'create', $params);
609 $contribution = $this->callAPISuccess('contribution', 'get', [
611 'id' => $contribution['id'],
613 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
614 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
616 $this->callAPISuccess('contribution', 'create', [
617 'id' => $contribution['id'],
618 'payment_instrument' => 'Credit Card',
620 $contribution = $this->callAPISuccess('contribution', 'get', [
622 'id' => $contribution['id'],
624 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
625 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
628 public function testGetContributionByPaymentInstrument() {
629 $params = $this->_params +
['payment_instrument' => 'EFT'];
630 $params2 = $this->_params +
['payment_instrument' => 'Cash'];
631 $this->callAPISuccess('contribution', 'create', $params);
632 $this->callAPISuccess('contribution', 'create', $params2);
633 $contribution = $this->callAPISuccess('contribution', 'get', [
635 'contribution_payment_instrument' => 'Cash',
637 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
638 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
639 $this->assertEquals(1, $contribution['count']);
640 $contribution = $this->callAPISuccess('contribution', 'get', ['sequential' => 1, 'payment_instrument' => 'Cash']);
641 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
642 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
643 $this->assertEquals(1, $contribution['count']);
644 $contribution = $this->callAPISuccess('contribution', 'get', [
646 'payment_instrument_id' => 5,
648 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
649 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
650 $this->assertEquals(1, $contribution['count']);
651 $contribution = $this->callAPISuccess('contribution', 'get', [
653 'payment_instrument' => 'EFT',
655 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
656 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
657 $this->assertEquals(1, $contribution['count']);
658 $contribution = $this->callAPISuccess('contribution', 'create', [
659 'id' => $contribution['id'],
660 'payment_instrument' => 'Credit Card',
662 $contribution = $this->callAPISuccess('contribution', 'get', ['sequential' => 1, 'id' => $contribution['id']]);
663 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
664 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
665 $this->assertEquals(1, $contribution['count']);
669 * CRM-16227 introduces invoice_id as a parameter.
671 public function testGetContributionByInvoice() {
672 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, ['invoice_id' => 'curly']));
673 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), ['invoice_id' => 'churlish']);
674 $this->callAPISuccessGetCount('Contribution', [], 2);
675 $this->callAPISuccessGetSingle('Contribution', ['invoice_id' => 'curly']);
676 // The following don't work. They are the format we are trying to introduce but although the form uses this format
677 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
678 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
679 // come out of convertFormValues
680 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
681 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
682 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
683 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
688 * Check the credit note retrieval is case insensitive.
690 public function testGetCreditNoteCaseInsensitive() {
691 $this->contributionCreate(['contact_id' => $this->_individualId
]);
692 $this->contributionCreate(['creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId
, 'invoice_id' => rand(), 'trxn_id' => rand()]);
693 $contribution = $this->callAPISuccess('Contribution', 'getsingle', ['creditnote_id' => 'CN1234']);
694 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
698 * Test retrieval by total_amount works.
702 public function testGetContributionByTotalAmount() {
703 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, ['total_amount' => '5']));
704 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, ['total_amount' => '10']));
705 $this->callAPISuccessGetCount('Contribution', ['total_amount' => 10], 1);
706 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => 6]], 1);
707 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => 0]], 2);
708 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => -5]], 2);
709 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['<' => 0]], 0);
710 $this->callAPISuccessGetCount('Contribution', [], 2);
714 * Create test with unique field name on source.
716 public function testCreateContributionSource() {
719 'contact_id' => $this->_individualId
,
720 'receive_date' => date('Ymd'),
721 'total_amount' => 100.00,
722 'financial_type_id' => $this->_financialTypeId
,
723 'payment_instrument_id' => 1,
724 'non_deductible_amount' => 10.00,
725 'fee_amount' => 50.00,
726 'net_amount' => 90.00,
728 'invoice_id' => 67890,
729 'contribution_source' => 'SSF',
730 'contribution_status_id' => 1,
733 $contribution = $this->callAPISuccess('contribution', 'create', $params);
734 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
735 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
739 * Create test with unique field name on source.
741 * @param string $thousandSeparator
742 * punctuation used to refer to thousands.
744 * @dataProvider getThousandSeparators
746 public function testCreateDefaultNow($thousandSeparator) {
747 $this->setCurrencySeparators($thousandSeparator);
748 $params = $this->_params
;
749 unset($params['receive_date'], $params['net_amount']);
751 $params['total_amount'] = $this->formatMoneyInput(5000.77);
752 $params['fee_amount'] = $this->formatMoneyInput(.77);
753 $params['skipCleanMoney'] = FALSE;
755 $contribution = $this->callAPISuccess('contribution', 'create', $params);
756 $contribution = $this->callAPISuccessGetSingle('contribution', ['id' => $contribution['id']]);
757 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
758 $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77));
759 $this->assertEquals(.77, $contribution['fee_amount']);
760 $this->assertEquals(5000, $contribution['net_amount']);
764 * Create test with unique field name on source.
766 public function testCreateContributionNullOutThankyouDate() {
768 $params = $this->_params
;
769 $params['thankyou_date'] = 'yesterday';
771 $contribution = $this->callAPISuccess('contribution', 'create', $params);
772 $contribution = $this->callAPISuccessGetSingle('contribution', ['id' => $contribution['id']]);
773 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
775 $params['thankyou_date'] = 'null';
776 $contribution = $this->callAPISuccess('contribution', 'create', $params);
777 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
781 * Create test with unique field name on source.
783 public function testCreateContributionSourceInvalidContact() {
787 'receive_date' => date('Ymd'),
788 'total_amount' => 100.00,
789 'financial_type_id' => $this->_financialTypeId
,
790 'payment_instrument_id' => 1,
791 'non_deductible_amount' => 10.00,
792 'fee_amount' => 50.00,
793 'net_amount' => 90.00,
795 'invoice_id' => 67890,
796 'contribution_source' => 'SSF',
797 'contribution_status_id' => 1,
800 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
803 public function testCreateContributionSourceInvalidContContact() {
806 'contribution_contact_id' => 999,
807 'receive_date' => date('Ymd'),
808 'total_amount' => 100.00,
809 'financial_type_id' => $this->_financialTypeId
,
810 'payment_instrument_id' => 1,
811 'non_deductible_amount' => 10.00,
812 'fee_amount' => 50.00,
813 'net_amount' => 90.00,
815 'invoice_id' => 67890,
816 'contribution_source' => 'SSF',
817 'contribution_status_id' => 1,
820 $this->callAPIFailure('contribution', 'create', $params);
824 * Test note created correctly.
826 public function testCreateContributionWithNote() {
827 $description = "Demonstrates creating contribution with Note Entity.";
828 $subfile = "ContributionCreateWithNote";
830 'contact_id' => $this->_individualId
,
831 'receive_date' => '2012-01-01',
832 'total_amount' => 100.00,
833 'financial_type_id' => $this->_financialTypeId
,
834 'payment_instrument_id' => 1,
835 'non_deductible_amount' => 10.00,
836 'fee_amount' => 50.00,
837 'net_amount' => 90.00,
839 'invoice_id' => 67890,
841 'contribution_status_id' => 1,
842 'note' => 'my contribution note',
845 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
846 $result = $this->callAPISuccess('note', 'get', [
847 'entity_table' => 'civicrm_contribution',
848 'entity_id' => $contribution['id'],
851 $this->assertEquals('my contribution note', $result['values'][0]['note']);
852 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
855 public function testCreateContributionWithNoteUniqueNameAliases() {
857 'contact_id' => $this->_individualId
,
858 'receive_date' => '2012-01-01',
859 'total_amount' => 100.00,
860 'financial_type_id' => $this->_financialTypeId
,
861 'payment_instrument_id' => 1,
862 'non_deductible_amount' => 10.00,
863 'fee_amount' => 50.00,
864 'net_amount' => 90.00,
866 'invoice_id' => 67890,
868 'contribution_status_id' => 1,
869 'contribution_note' => 'my contribution note',
872 $contribution = $this->callAPISuccess('contribution', 'create', $params);
873 $result = $this->callAPISuccess('note', 'get', [
874 'entity_table' => 'civicrm_contribution',
875 'entity_id' => $contribution['id'],
878 $this->assertEquals('my contribution note', $result['values'][0]['note']);
879 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
883 * This is the test for creating soft credits.
885 public function testCreateContributionWithSoftCredit() {
886 $description = "Demonstrates creating contribution with SoftCredit.";
887 $subfile = "ContributionCreateWithSoftCredit";
888 $contact2 = $this->callAPISuccess('Contact', 'create', [
889 'display_name' => 'superman',
890 'contact_type' => 'Individual',
893 'contact_id' => $contact2['id'],
895 'soft_credit_type_id' => 3,
898 $params = $this->_params +
['soft_credit' => [1 => $softParams]];
899 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
900 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
902 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
903 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
904 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
906 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
907 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
910 public function testCreateContributionWithSoftCreditDefaults() {
911 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
912 $subfile = "ContributionCreateWithSoftCreditDefaults";
913 $contact2 = $this->callAPISuccess('Contact', 'create', [
914 'display_name' => 'superman',
915 'contact_type' => 'Individual',
917 $params = $this->_params +
[
918 'soft_credit_to' => $contact2['id'],
920 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
921 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
923 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
924 // Default soft credit amount = contribution.total_amount
925 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
926 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
928 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
929 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
933 * Test creating contribution with Soft Credit by passing in honor_contact_id.
935 public function testCreateContributionWithHonoreeContact() {
936 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
937 $subfile = "ContributionCreateWithHonoreeContact";
938 $contact2 = $this->callAPISuccess('Contact', 'create', [
939 'display_name' => 'superman',
940 'contact_type' => 'Individual',
942 $params = $this->_params +
[
943 'honor_contact_id' => $contact2['id'],
945 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
946 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
948 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
949 // Default soft credit amount = contribution.total_amount
950 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
951 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
952 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
953 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
955 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
956 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
960 * Test using example code.
962 public function testContributionCreateExample() {
963 //make sure at least on page exists since there is a truncate in tear down
964 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
965 require_once 'api/v3/examples/Contribution/Create.ex.php';
966 $result = contribution_create_example();
968 $expectedResult = contribution_create_expectedresult();
969 $this->checkArrayEquals($expectedResult, $result);
970 $this->contributionDelete($id);
974 * Function tests that additional financial records are created when fee amount is recorded.
976 public function testCreateContributionWithFee() {
978 'contact_id' => $this->_individualId
,
979 'receive_date' => '20120511',
980 'total_amount' => 100.00,
982 'financial_type_id' => 1,
984 'invoice_id' => 67890,
986 'contribution_status_id' => 1,
989 $contribution = $this->callAPISuccess('contribution', 'create', $params);
990 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
991 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
992 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
993 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
994 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
995 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
996 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
997 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
998 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
1000 $lineItems = $this->callAPISuccess('line_item', 'get', [
1002 'entity_id' => $contribution['id'],
1003 'entity_table' => 'civicrm_contribution',
1006 $this->assertEquals(1, $lineItems['count']);
1007 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
1008 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
1009 $lineItems = $this->callAPISuccess('line_item', 'get', [
1011 'entity_id' => $contribution['id'],
1012 'contribution_id' => $contribution['id'],
1013 'entity_table' => 'civicrm_contribution',
1016 $this->assertEquals(1, $lineItems['count']);
1017 $this->_checkFinancialRecords($contribution, 'feeAmount');
1021 * Function tests that additional financial records are created when online contribution is created.
1023 public function testCreateContributionOnline() {
1024 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1025 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1026 $this->assertAPISuccess($contributionPage);
1028 'contact_id' => $this->_individualId
,
1029 'receive_date' => '20120511',
1030 'total_amount' => 100.00,
1031 'financial_type_id' => 1,
1032 'contribution_page_id' => $contributionPage['id'],
1033 'payment_processor' => $this->paymentProcessorID
,
1035 'invoice_id' => 67890,
1037 'contribution_status_id' => 1,
1041 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1042 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1043 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1044 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1045 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1046 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1047 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1048 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
1049 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', [
1050 'id' => $this->paymentProcessorID
,
1051 'return' => 'payment_instrument_id',
1053 $this->_checkFinancialRecords($contribution, 'online');
1057 * Check handling of financial type.
1059 * In the interests of removing financial type / contribution type checks from
1060 * legacy format function lets test that the api is doing this for us
1062 public function testCreateInvalidFinancialType() {
1063 $params = $this->_params
;
1064 $params['financial_type_id'] = 99999;
1065 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
1069 * Check handling of financial type.
1071 * In the interests of removing financial type / contribution type checks from
1072 * legacy format function lets test that the api is doing this for us
1074 public function testValidNamedFinancialType() {
1075 $params = $this->_params
;
1076 $params['financial_type_id'] = 'Donation';
1077 $this->callAPISuccess($this->_entity
, 'create', $params);
1081 * Tests that additional financial records are created.
1083 * Checks when online contribution with pay later option is created
1085 public function testCreateContributionPayLaterOnline() {
1086 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1087 $this->_pageParams
['is_pay_later'] = 1;
1088 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1089 $this->assertAPISuccess($contributionPage);
1091 'contact_id' => $this->_individualId
,
1092 'receive_date' => '20120511',
1093 'total_amount' => 100.00,
1094 'financial_type_id' => 1,
1095 'contribution_page_id' => $contributionPage['id'],
1097 'is_pay_later' => 1,
1098 'invoice_id' => 67890,
1100 'contribution_status_id' => 2,
1104 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1105 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1106 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1107 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1108 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1109 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1110 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1111 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1112 $this->_checkFinancialRecords($contribution, 'payLater');
1116 * Function tests that additional financial records are created for online contribution with pending option.
1118 public function testCreateContributionPendingOnline() {
1119 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1120 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1121 $this->assertAPISuccess($contributionPage);
1123 'contact_id' => $this->_individualId
,
1124 'receive_date' => '20120511',
1125 'total_amount' => 100.00,
1126 'financial_type_id' => 1,
1127 'contribution_page_id' => $contributionPage['id'],
1129 'invoice_id' => 67890,
1131 'contribution_status_id' => 2,
1134 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1135 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1136 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1137 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1138 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1139 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1140 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1141 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1142 $this->_checkFinancialRecords($contribution, 'pending');
1146 * Test that BAO defaults work.
1148 public function testCreateBAODefaults() {
1149 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1150 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1151 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1152 'id' => $contribution['id'],
1153 'api.contribution.delete' => 1,
1155 $this->assertEquals(1, $contribution['contribution_status_id']);
1156 $this->assertEquals('Check', $contribution['payment_instrument']);
1157 $this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
1161 * Test that getsingle can be chained with delete.
1163 public function testDeleteChainedGetSingle() {
1164 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1165 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1166 'id' => $contribution['id'],
1167 'api.contribution.delete' => 1,
1169 $this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
1173 * Function tests that line items, financial records are updated when contribution amount is changed.
1175 public function testCreateUpdateContributionChangeTotal() {
1176 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1177 $lineItems = $this->callAPISuccess('line_item', 'getvalue', [
1179 'entity_id' => $contribution['id'],
1180 'entity_table' => 'civicrm_contribution',
1182 'return' => 'line_total',
1184 $this->assertEquals('100.00', $lineItems);
1185 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1186 // Financial trxn SUM = 100 + 5 (fee)
1187 $this->assertEquals('105.00', $trxnAmount);
1190 'id' => $contribution['id'],
1191 'total_amount' => '125',
1193 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1195 $lineItems = $this->callAPISuccess('line_item', 'getvalue', [
1197 'entity_id' => $contribution['id'],
1198 'entity_table' => 'civicrm_contribution',
1200 'return' => 'line_total',
1203 $this->assertEquals('125.00', $lineItems);
1204 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1206 // Financial trxn SUM = 125 + 5 (fee).
1207 $this->assertEquals('130.00', $trxnAmount);
1208 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1212 * Function tests that line items, financial records are updated when pay later contribution is received.
1214 public function testCreateUpdateContributionPayLater() {
1216 'contact_id' => $this->_individualId
,
1217 'receive_date' => '2012-01-01',
1218 'total_amount' => 100.00,
1219 'financial_type_id' => $this->_financialTypeId
,
1220 'payment_instrument_id' => 1,
1221 'contribution_status_id' => 2,
1222 'is_pay_later' => 1,
1225 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1227 $newParams = array_merge($contribParams, [
1228 'id' => $contribution['id'],
1229 'contribution_status_id' => 1,
1231 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1232 $contribution = $contribution['values'][$contribution['id']];
1233 $this->assertEquals($contribution['contribution_status_id'], '1');
1234 $this->_checkFinancialItem($contribution['id'], 'paylater');
1235 $this->_checkFinancialTrxn($contribution, 'payLater');
1239 * Function tests that financial records are updated when Payment Instrument is changed.
1241 public function testCreateUpdateContributionPaymentInstrument() {
1242 $instrumentId = $this->_addPaymentInstrument();
1244 'contact_id' => $this->_individualId
,
1245 'total_amount' => 100.00,
1246 'financial_type_id' => $this->_financialTypeId
,
1247 'payment_instrument_id' => 4,
1248 'contribution_status_id' => 1,
1251 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1253 $newParams = array_merge($contribParams, [
1254 'id' => $contribution['id'],
1255 'payment_instrument_id' => $instrumentId,
1257 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1258 $this->assertAPISuccess($contribution);
1259 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1261 // cleanup - delete created payment instrument
1262 $this->_deletedAddedPaymentInstrument();
1266 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1268 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1269 $instrumentId = $this->_addPaymentInstrument();
1271 'contact_id' => $this->_individualId
,
1272 'total_amount' => -100.00,
1273 'financial_type_id' => $this->_financialTypeId
,
1274 'payment_instrument_id' => 4,
1275 'contribution_status_id' => 1,
1278 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1280 $newParams = array_merge($contribParams, [
1281 'id' => $contribution['id'],
1282 'payment_instrument_id' => $instrumentId,
1284 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1285 $this->assertAPISuccess($contribution);
1286 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1288 // cleanup - delete created payment instrument
1289 $this->_deletedAddedPaymentInstrument();
1293 * Function tests that financial records are added when Contribution is Refunded.
1295 public function testCreateUpdateContributionRefund() {
1296 $contributionParams = [
1297 'contact_id' => $this->_individualId
,
1298 'receive_date' => '2012-01-01',
1299 'total_amount' => 100.00,
1300 'financial_type_id' => $this->_financialTypeId
,
1301 'payment_instrument_id' => 4,
1302 'contribution_status_id' => 1,
1303 'trxn_id' => 'original_payment',
1305 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1306 $newParams = array_merge($contributionParams, [
1307 'id' => $contribution['id'],
1308 'contribution_status_id' => 'Refunded',
1309 'cancel_date' => '2015-01-01 09:00',
1310 'refund_trxn_id' => 'the refund',
1313 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1314 $this->_checkFinancialTrxn($contribution, 'refund');
1315 $this->_checkFinancialItem($contribution['id'], 'refund');
1316 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', [
1317 'id' => $contribution['id'],
1318 'return' => 'trxn_id',
1323 * Refund a contribution for a financial type with a contra account.
1325 * CRM-17951 the contra account is a financial account with a relationship to a
1326 * financial type. It is not always configured but should be reflected
1327 * in the financial_trxn & financial_item table if it is.
1329 public function testCreateUpdateChargebackContributionDefaultAccount() {
1330 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1331 $this->callAPISuccess('Contribution', 'create', [
1332 'id' => $contribution['id'],
1333 'contribution_status_id' => 'Chargeback',
1335 $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Chargeback']);
1337 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
1338 'contribution_id' => $contribution['id'],
1339 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1341 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1342 $this->callAPISuccessGetSingle('FinancialTrxn', [
1343 'total_amount' => -100,
1344 'status_id' => 'Chargeback',
1345 'to_financial_account_id' => 6,
1350 * Refund a contribution for a financial type with a contra account.
1352 * CRM-17951 the contra account is a financial account with a relationship to a
1353 * financial type. It is not always configured but should be reflected
1354 * in the financial_trxn & financial_item table if it is.
1356 public function testCreateUpdateChargebackContributionCustomAccount() {
1357 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', [
1358 'name' => 'Chargeback Account',
1359 'is_active' => TRUE,
1362 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [
1363 'entity_id' => $this->_financialTypeId
,
1364 'entity_table' => 'civicrm_financial_type',
1365 'account_relationship' => 'Chargeback Account is',
1366 'financial_account_id' => 'Chargeback Account',
1369 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1370 $this->callAPISuccess('Contribution', 'create', [
1371 'id' => $contribution['id'],
1372 'contribution_status_id' => 'Chargeback',
1374 $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Chargeback']);
1376 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
1377 'contribution_id' => $contribution['id'],
1378 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1380 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1382 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
1383 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialAccount['id']]);
1384 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
1388 * Refund a contribution for a financial type with a contra account.
1390 * CRM-17951 the contra account is a financial account with a relationship to a
1391 * financial type. It is not always configured but should be reflected
1392 * in the financial_trxn & financial_item table if it is.
1394 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1395 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', [
1396 'name' => 'Refund Account',
1397 'is_active' => TRUE,
1400 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [
1401 'entity_id' => $this->_financialTypeId
,
1402 'entity_table' => 'civicrm_financial_type',
1403 'account_relationship' => 'Credit/Contra Revenue Account is',
1404 'financial_account_id' => 'Refund Account',
1407 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1408 $this->callAPISuccess('Contribution', 'create', [
1409 'id' => $contribution['id'],
1410 'contribution_status_id' => 'Refunded',
1413 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
1414 'contribution_id' => $contribution['id'],
1415 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1417 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1419 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
1420 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialAccount['id']]);
1421 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
1425 * Function tests that trxn_id is set when passed in.
1427 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1428 * when trxn_id is passed in.
1430 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1431 $contributionParams = [
1432 'contact_id' => $this->_individualId
,
1433 'receive_date' => '2012-01-01',
1434 'total_amount' => 100.00,
1435 'financial_type_id' => $this->_financialTypeId
,
1436 'payment_instrument_id' => 4,
1437 'contribution_status_id' => 1,
1438 'trxn_id' => 'original_payment',
1440 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1441 $newParams = array_merge($contributionParams, [
1442 'id' => $contribution['id'],
1443 'contribution_status_id' => 'Refunded',
1444 'cancel_date' => '2015-01-01 09:00',
1445 'trxn_id' => 'the refund',
1448 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1449 $this->_checkFinancialTrxn($contribution, 'refund');
1450 $this->_checkFinancialItem($contribution['id'], 'refund');
1451 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', [
1452 'id' => $contribution['id'],
1453 'return' => 'trxn_id',
1458 * Function tests that trxn_id is set when passed in.
1460 * Here we ensure that the civicrm_contribution.trxn_id is set
1461 * when trxn_id is passed in but if refund_trxn_id is different then that
1462 * is kept for the refund transaction.
1464 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1465 $contributionParams = [
1466 'contact_id' => $this->_individualId
,
1467 'receive_date' => '2012-01-01',
1468 'total_amount' => 100.00,
1469 'financial_type_id' => $this->_financialTypeId
,
1470 'payment_instrument_id' => 4,
1471 'contribution_status_id' => 1,
1472 'trxn_id' => 'original_payment',
1474 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1475 $newParams = array_merge($contributionParams, [
1476 'id' => $contribution['id'],
1477 'contribution_status_id' => 'Refunded',
1478 'cancel_date' => '2015-01-01 09:00',
1479 'trxn_id' => 'cont id',
1480 'refund_trxn_id' => 'the refund',
1483 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1484 $this->_checkFinancialTrxn($contribution, 'refund');
1485 $this->_checkFinancialItem($contribution['id'], 'refund');
1486 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', [
1487 'id' => $contribution['id'],
1488 'return' => 'trxn_id',
1493 * Function tests that refund_trxn_id is set when passed in empty.
1495 * Here we ensure that the civicrm_contribution.trxn_id is set
1496 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1497 * is kept for the refund transaction.
1499 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1500 $contributionParams = [
1501 'contact_id' => $this->_individualId
,
1502 'receive_date' => '2012-01-01',
1503 'total_amount' => 100.00,
1504 'financial_type_id' => $this->_financialTypeId
,
1505 'payment_instrument_id' => 4,
1506 'contribution_status_id' => 1,
1507 'trxn_id' => 'original_payment',
1509 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1510 $newParams = array_merge($contributionParams, [
1511 'id' => $contribution['id'],
1512 'contribution_status_id' => 'Refunded',
1513 'cancel_date' => '2015-01-01 09:00',
1514 'trxn_id' => 'cont id',
1515 'refund_trxn_id' => '',
1518 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1519 $this->_checkFinancialTrxn($contribution, 'refund', NULL, ['trxn_id' => NULL]);
1520 $this->_checkFinancialItem($contribution['id'], 'refund');
1521 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', [
1522 'id' => $contribution['id'],
1523 'return' => 'trxn_id',
1528 * Function tests invalid contribution status change.
1530 public function testCreateUpdateContributionInValidStatusChange() {
1533 'receive_date' => '2012-01-01',
1534 'total_amount' => 100.00,
1535 'financial_type_id' => 1,
1536 'payment_instrument_id' => 1,
1537 'contribution_status_id' => 1,
1539 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1540 $newParams = array_merge($contribParams, [
1541 'id' => $contribution['id'],
1542 'contribution_status_id' => 2,
1544 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1549 * Function tests that financial records are added when Pending Contribution is Canceled.
1551 public function testCreateUpdateContributionCancelPending() {
1553 'contact_id' => $this->_individualId
,
1554 'receive_date' => '2012-01-01',
1555 'total_amount' => 100.00,
1556 'financial_type_id' => $this->_financialTypeId
,
1557 'payment_instrument_id' => 1,
1558 'contribution_status_id' => 2,
1559 'is_pay_later' => 1,
1562 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1563 $newParams = array_merge($contribParams, [
1564 'id' => $contribution['id'],
1565 'contribution_status_id' => 3,
1566 'cancel_date' => '2012-02-02 09:00',
1568 //Check if trxn_date is same as cancel_date.
1570 'trxn_date' => '2012-02-02 09:00:00',
1572 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1573 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1574 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1578 * Function tests that financial records are added when Financial Type is Changed.
1580 public function testCreateUpdateContributionChangeFinancialType() {
1582 'contact_id' => $this->_individualId
,
1583 'receive_date' => '2012-01-01',
1584 'total_amount' => 100.00,
1585 'financial_type_id' => 1,
1586 'payment_instrument_id' => 1,
1587 'contribution_status_id' => 1,
1590 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1591 $newParams = array_merge($contribParams, [
1592 'id' => $contribution['id'],
1593 'financial_type_id' => 3,
1595 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1596 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1597 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1601 * Test that update does not change status id CRM-15105.
1603 public function testCreateUpdateWithoutChangingPendingStatus() {
1604 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, ['contribution_status_id' => 2]));
1605 $this->callAPISuccess('contribution', 'create', ['id' => $contribution['id'], 'source' => 'new source']);
1606 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1607 'id' => $contribution['id'],
1608 'api.contribution.delete' => 1,
1610 $this->assertEquals(2, $contribution['contribution_status_id']);
1614 * Test Updating a Contribution.
1616 * CHANGE: we require the API to do an incremental update
1618 public function testCreateUpdateContribution() {
1619 $contributionID = $this->contributionCreate([
1620 'contact_id' => $this->_individualId
,
1621 'trxn_id' => 212355,
1622 'financial_type_id' => $this->_financialTypeId
,
1623 'invoice_id' => 'old_invoice',
1626 'contribution_id' => $contributionID,
1628 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1629 $this->assertEquals($original['id'], $contributionID);
1630 //set up list of old params, verify
1632 //This should not be required on update:
1633 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1634 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1635 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1636 $old_source = $original['values'][$contributionID]['contribution_source'];
1638 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1639 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1641 //check against values in CiviUnitTestCase::createContribution()
1642 $this->assertEquals($old_contact_id, $this->_individualId
);
1643 $this->assertEquals($old_fee_amount, 5.00);
1644 $this->assertEquals($old_source, 'SSF');
1645 $this->assertEquals($old_trxn_id, 212355);
1646 $this->assertEquals($old_invoice_id, 'old_invoice');
1648 'id' => $contributionID,
1649 'contact_id' => $this->_individualId
,
1650 'total_amount' => 105.00,
1651 'fee_amount' => 7.00,
1652 'financial_type_id' => $this->_financialTypeId
,
1653 'non_deductible_amount' => 22.00,
1654 'contribution_status_id' => 1,
1655 'note' => 'Donating for Noble Cause',
1658 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1661 'contribution_id' => $contribution['id'],
1663 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1665 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1666 $this->assertEquals($contribution['total_amount'], 105.00);
1667 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1668 $this->assertEquals($contribution['financial_type'], 'Donation');
1669 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1670 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1671 $this->assertEquals($contribution['fee_amount'], 7.00);
1672 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1673 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1674 $this->assertEquals($contribution['contribution_source'], $old_source);
1675 $this->assertEquals($contribution['contribution_status'], 'Completed');
1677 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1680 'contribution_id' => $contributionID,
1682 $result = $this->callAPISuccess('contribution', 'delete', $params);
1683 $this->assertAPISuccess($result);
1687 * Check that net_amount is updated when a contribution is updated.
1689 * Update fee amount AND total amount, just fee amount, just total amount
1690 * and neither to check that net_amount is keep updated.
1692 public function testUpdateContributionNetAmountVariants() {
1693 $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
1695 $this->callAPISuccess('Contribution', 'create', [
1696 'id' => $contributionID,
1697 'total_amount' => 90,
1700 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1701 'id' => $contributionID,
1702 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1704 $this->assertEquals(6, $contribution['fee_amount']);
1705 $this->assertEquals(90, $contribution['total_amount']);
1706 $this->assertEquals(84, $contribution['net_amount']);
1708 $this->callAPISuccess('Contribution', 'create', [
1709 'id' => $contributionID,
1712 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1713 'id' => $contributionID,
1714 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1716 $this->assertEquals(3, $contribution['fee_amount']);
1717 $this->assertEquals(90, $contribution['total_amount']);
1718 $this->assertEquals(87, $contribution['net_amount']);
1720 $this->callAPISuccess('Contribution', 'create', [
1721 'id' => $contributionID,
1722 'total_amount' => 200,
1724 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1725 'id' => $contributionID,
1726 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1728 $this->assertEquals(3, $contribution['fee_amount']);
1729 $this->assertEquals(200, $contribution['total_amount']);
1730 $this->assertEquals(197, $contribution['net_amount']);
1732 $this->callAPISuccess('Contribution', 'create', [
1733 'id' => $contributionID,
1734 'payment_instrument' => 'Cash',
1736 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1737 'id' => $contributionID,
1738 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1740 $this->assertEquals(3, $contribution['fee_amount']);
1741 $this->assertEquals(200, $contribution['total_amount']);
1742 $this->assertEquals(197, $contribution['net_amount']);
1746 * Attempt (but fail) to delete a contribution without parameters.
1748 public function testDeleteEmptyParamsContribution() {
1750 $this->callAPIFailure('contribution', 'delete', $params);
1753 public function testDeleteParamsNotArrayContribution() {
1754 $params = 'contribution_id= 1';
1755 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1756 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1759 public function testDeleteWrongParamContribution() {
1761 'contribution_source' => 'SSF',
1763 $this->callAPIFailure('contribution', 'delete', $params);
1766 public function testDeleteContribution() {
1767 $contributionID = $this->contributionCreate([
1768 'contact_id' => $this->_individualId
,
1769 'financial_type_id' => $this->_financialTypeId
,
1772 'id' => $contributionID,
1774 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1778 * Test civicrm_contribution_search with empty params.
1780 * All available contributions expected.
1782 public function testSearchEmptyParams() {
1786 'contact_id' => $this->_individualId
,
1787 'receive_date' => date('Ymd'),
1788 'total_amount' => 100.00,
1789 'financial_type_id' => $this->_financialTypeId
,
1790 'non_deductible_amount' => 10.00,
1791 'fee_amount' => 5.00,
1792 'net_amount' => 95.00,
1794 'invoice_id' => 78910,
1796 'contribution_status_id' => 1,
1798 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1800 $result = $this->callAPISuccess('contribution', 'get', $params);
1801 // We're taking the first element.
1802 $res = $result['values'][$contribution['id']];
1804 $this->assertEquals($p['contact_id'], $res['contact_id']);
1805 $this->assertEquals($p['total_amount'], $res['total_amount']);
1806 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1807 $this->assertEquals($p['net_amount'], $res['net_amount']);
1808 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1809 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1810 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1811 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1812 $this->assertEquals($p['source'], $res['contribution_source']);
1813 // contribution_status_id = 1 => Completed
1814 $this->assertEquals('Completed', $res['contribution_status']);
1816 $this->contributionDelete($contribution['id']);
1820 * Test civicrm_contribution_search. Success expected.
1822 public function testSearch() {
1824 'contact_id' => $this->_individualId
,
1825 'receive_date' => date('Ymd'),
1826 'total_amount' => 100.00,
1827 'financial_type_id' => $this->_financialTypeId
,
1828 'non_deductible_amount' => 10.00,
1829 'contribution_status_id' => 1,
1831 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1834 'contact_id' => $this->_individualId
,
1835 'receive_date' => date('Ymd'),
1836 'total_amount' => 200.00,
1837 'financial_type_id' => $this->_financialTypeId
,
1838 'non_deductible_amount' => 20.00,
1839 'trxn_id' => 5454565,
1840 'invoice_id' => 1212124,
1841 'fee_amount' => 50.00,
1842 'net_amount' => 60.00,
1843 'contribution_status_id' => 2,
1845 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1848 'contribution_id' => $contribution2['id'],
1850 $result = $this->callAPISuccess('contribution', 'get', $params);
1851 $res = $result['values'][$contribution2['id']];
1853 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1854 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1855 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1856 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1857 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1858 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1859 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1860 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1861 // contribution_status_id = 2 => Pending
1862 $this->assertEquals('Pending', $res['contribution_status']);
1864 $this->contributionDelete($contribution1['id']);
1865 $this->contributionDelete($contribution2['id']);
1869 * Test completing a transaction via the API.
1871 * Note that we are creating a logged in user because email goes out from
1874 public function testCompleteTransaction() {
1875 $mut = new CiviMailUtils($this, TRUE);
1876 $this->swapMessageTemplateForTestTemplate();
1877 $this->createLoggedInUser();
1878 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
1879 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1880 $this->callAPISuccess('contribution', 'completetransaction', [
1881 'id' => $contribution['id'],
1883 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
1884 $this->assertEquals('SSF', $contribution['contribution_source']);
1885 $this->assertEquals('Completed', $contribution['contribution_status']);
1886 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1887 $mut->checkMailLog([
1888 'email:::anthony_anderson@civicrm.org',
1892 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1893 "receipt_date:::\n",
1894 'contributeMode:::notify',
1895 'title:::Contribution',
1896 'displayName:::Mr. Anthony Anderson II',
1897 'contributionStatus:::Completed',
1900 $this->revertTemplateToReservedTemplate();
1904 * Test completing a transaction via the API with a non-USD transaction.
1906 public function testCompleteTransactionEuro() {
1907 $mut = new CiviMailUtils($this, TRUE);
1908 $this->swapMessageTemplateForTestTemplate();
1909 $this->createLoggedInUser();
1910 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'currency' => 'EUR']);
1911 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1913 $this->callAPISuccess('contribution', 'completetransaction', [
1914 'id' => $contribution['id'],
1917 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
1918 $this->assertEquals('SSF', $contribution['contribution_source']);
1919 $this->assertEquals('Completed', $contribution['contribution_status']);
1920 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1922 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1923 $entityFinancialTransaction = reset($entityFinancialTransactions);
1924 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', ['id' => $entityFinancialTransaction['financial_trxn_id']]);
1925 $this->assertEquals('EUR', $financialTrxn['currency']);
1927 $mut->checkMailLog([
1928 'email:::anthony_anderson@civicrm.org',
1932 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1933 "receipt_date:::\n",
1934 'contributeMode:::notify',
1935 'title:::Contribution',
1936 'displayName:::Mr. Anthony Anderson II',
1937 'contributionStatus:::Completed',
1940 $this->revertTemplateToReservedTemplate();
1944 * Test to ensure mail is sent on chosing pay later
1946 public function testpayLater() {
1947 $mut = new CiviMailUtils($this, TRUE);
1948 $this->swapMessageTemplateForTestTemplate();
1949 $this->createLoggedInUser();
1951 // create contribution page first
1952 $contributionPageParams = [
1953 'title' => 'Help Support CiviCRM!',
1954 'financial_type_id' => 1,
1955 'is_monetary' => TRUE,
1956 'is_pay_later' => 1,
1957 'is_quick_config' => TRUE,
1958 'pay_later_text' => 'I will send payment by check',
1959 'pay_later_receipt' => 'This is a pay later reciept',
1960 'is_allow_other_amount' => 1,
1961 'min_amount' => 10.00,
1962 'max_amount' => 10000.00,
1963 'goal_amount' => 100000.00,
1964 'is_email_receipt' => 1,
1966 'amount_block_is_active' => 1,
1967 'currency' => 'USD',
1968 'is_billing_required' => 0,
1970 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1972 // submit form values
1973 $priceSet = $this->callAPISuccess('price_set', 'getsingle', ['name' => 'default_contribution_amount']);
1975 'id' => $contributionPageResult['id'],
1976 'contact_id' => $this->_individualId
,
1977 'email-5' => 'anthony_anderson@civicrm.org',
1978 'payment_processor_id' => 0,
1981 'currencyID' => 'USD',
1982 'is_pay_later' => 1,
1983 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1984 'is_quick_config' => 1,
1985 'description' => 'Online Contribution: Help Support CiviCRM!',
1986 'price_set_id' => $priceSet['id'],
1988 $this->callAPISuccess('contribution_page', 'submit', $params);
1990 $mut->checkMailLog([
1992 'email:::anthony_anderson@civicrm.org',
1993 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1994 'displayName:::Mr. Anthony Anderson II',
1995 'contributionPageId:::' . $contributionPageResult['id'],
1996 'title:::' . $contributionPageParams['title'],
1997 'amount:::' . $params['amount'],
2000 $this->revertTemplateToReservedTemplate();
2004 * Test to check whether contact billing address is used when no contribution address
2006 public function testBillingAddress() {
2007 $mut = new CiviMailUtils($this, TRUE);
2008 $this->swapMessageTemplateForTestTemplate();
2009 $this->createLoggedInUser();
2011 //Scenario 1: When Contact don't have any address
2012 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2013 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2014 $this->callAPISuccess('contribution', 'completetransaction', [
2015 'id' => $contribution['id'],
2017 $mut->checkMailLog([
2021 // Scenario 2: Contribution using address
2022 $address = $this->callAPISuccess('address', 'create', [
2023 'street_address' => 'contribution billing st',
2024 'location_type_id' => 2,
2025 'contact_id' => $this->_params
['contact_id'],
2027 $params = array_merge($this->_params
, [
2028 'contribution_status_id' => 2,
2029 'address_id' => $address['id'],
2032 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2033 $this->callAPISuccess('contribution', 'completetransaction', [
2034 'id' => $contribution['id'],
2036 $mut->checkMailLog([
2037 'address:::contribution billing st',
2040 // Scenario 3: Contribution wtth no address but contact has a billing address
2041 $this->callAPISuccess('address', 'create', [
2042 'id' => $address['id'],
2043 'street_address' => 'is billing st',
2044 'contact_id' => $this->_params
['contact_id'],
2046 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2047 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2048 $this->callAPISuccess('contribution', 'completetransaction', [
2049 'id' => $contribution['id'],
2051 $mut->checkMailLog([
2052 'address:::is billing st',
2056 $this->revertTemplateToReservedTemplate();
2060 * Test completing a transaction via the API.
2062 * Note that we are creating a logged in user because email goes out from
2065 public function testCompleteTransactionFeeAmount() {
2066 $this->createLoggedInUser();
2067 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2068 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2069 $this->callAPISuccess('contribution', 'completetransaction', [
2070 'id' => $contribution['id'],
2071 'fee_amount' => '.56',
2072 'trxn_id' => '7778888',
2074 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id'], 'sequential' => 1]);
2075 $this->assertEquals('Completed', $contribution['contribution_status']);
2076 $this->assertEquals('7778888', $contribution['trxn_id']);
2077 $this->assertEquals('.56', $contribution['fee_amount']);
2078 $this->assertEquals('99.44', $contribution['net_amount']);
2082 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
2084 * @param string $thousandSeparator
2085 * punctuation used to refer to thousands.
2087 * @dataProvider getThousandSeparators
2089 public function testCheckTaxAmount($thousandSeparator) {
2090 $this->setCurrencySeparators($thousandSeparator);
2091 $contact = $this->createLoggedInUser();
2092 $financialType = $this->callAPISuccess('financial_type', 'create', [
2093 'name' => 'Test taxable financial Type',
2097 $financialAccount = $this->callAPISuccess('financial_account', 'create', [
2098 'name' => 'Test Tax financial account ',
2099 'contact_id' => $contact,
2100 'financial_account_type_id' => 2,
2107 $financialTypeId = $financialType['id'];
2108 $financialAccountId = $financialAccount['id'];
2109 $financialAccountParams = [
2110 'entity_table' => 'civicrm_financial_type',
2111 'entity_id' => $financialTypeId,
2112 'account_relationship' => 10,
2113 'financial_account_id' => $financialAccountId,
2115 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
2117 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'financial_type_id' => $financialTypeId]);
2118 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2119 $contribution1 = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1]);
2120 $this->callAPISuccess('contribution', 'completetransaction', [
2121 'id' => $contribution['id'],
2122 'trxn_id' => '777788888',
2123 'fee_amount' => '6.00',
2125 $contribution2 = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'return' => ['tax_amount', 'fee_amount', 'net_amount'], 'sequential' => 1]);
2126 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
2127 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
2128 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
2132 * Test repeat contribution successfully creates line item.
2134 public function testRepeatTransaction() {
2135 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');
2136 $this->callAPISuccess('contribution', 'repeattransaction', [
2137 'original_contribution_id' => $originalContribution['id'],
2138 'contribution_status_id' => 'Completed',
2139 'trxn_id' => uniqid(),
2142 'entity_id' => $originalContribution['id'],
2150 'financial_type_id',
2151 'deductible_amount',
2152 'price_field_value_id',
2156 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2157 'entity_id' => $originalContribution['id'],
2159 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2160 'entity_id' => $originalContribution['id'] +
1,
2162 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2163 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2164 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2165 $this->_checkFinancialRecords([
2166 'id' => $originalContribution['id'] +
1,
2167 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', [
2168 'id' => $originalContribution['payment_processor_id'],
2169 'return' => 'payment_instrument_id',
2172 $this->quickCleanUpFinancialEntities();
2176 * Test repeat contribution successfully creates line items (plural).
2178 public function testRepeatTransactionLineItems() {
2180 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'multiple');
2181 $this->callAPISuccess('contribution', 'repeattransaction', [
2182 'original_contribution_id' => $originalContribution['id'],
2183 'contribution_status_id' => 'Completed',
2184 'trxn_id' => uniqid(),
2188 'entity_id' => $originalContribution['id'],
2196 'financial_type_id',
2197 'deductible_amount',
2198 'price_field_value_id',
2202 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2203 'entity_id' => $originalContribution['id'],
2205 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2206 'entity_id' => $originalContribution['id'] +
1,
2209 // unset id and entity_id for all of them to be able to compare the lineItems:
2210 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2211 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2212 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2214 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2215 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2216 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2218 // CRM-19309 so in future we also want to:
2219 // check that financial_line_items have been created for entity_id 3 and 4;
2221 $this->callAPISuccessGetCount('FinancialItem', ['description' => 'Sales Tax', 'amount' => 0], 0);
2222 $this->quickCleanUpFinancialEntities();
2226 * Test repeat contribution successfully creates is_test transaction.
2228 public function testRepeatTransactionIsTest() {
2229 $this->_params
['is_test'] = 1;
2230 $originalContribution = $this->setUpRepeatTransaction(['is_test' => 1], 'single');
2232 $this->callAPISuccess('contribution', 'repeattransaction', [
2233 'original_contribution_id' => $originalContribution['id'],
2234 'contribution_status_id' => 'Completed',
2235 'trxn_id' => uniqid(),
2237 $this->callAPISuccessGetCount('Contribution', ['contribution_test' => 1], 2);
2241 * Test repeat contribution passed in status.
2243 public function testRepeatTransactionPassedInStatus() {
2244 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');
2246 $this->callAPISuccess('contribution', 'repeattransaction', [
2247 'original_contribution_id' => $originalContribution['id'],
2248 'contribution_status_id' => 'Pending',
2249 'trxn_id' => uniqid(),
2251 $this->callAPISuccessGetCount('Contribution', ['contribution_status_id' => 2], 1);
2255 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2257 public function testRepeatTransactionAcceptRecurID() {
2258 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2259 'contact_id' => $this->_individualId
,
2260 'installments' => '12',
2261 'frequency_interval' => '1',
2263 'contribution_status_id' => 1,
2264 'start_date' => '2012-01-01 00:00:00',
2265 'currency' => 'USD',
2266 'frequency_unit' => 'month',
2267 'payment_processor_id' => $this->paymentProcessorID
,
2269 $this->callAPISuccess('contribution', 'create', array_merge(
2271 ['contribution_recur_id' => $contributionRecur['id']])
2274 $this->callAPISuccess('contribution', 'repeattransaction', [
2275 'contribution_recur_id' => $contributionRecur['id'],
2276 'contribution_status_id' => 'Completed',
2277 'trxn_id' => uniqid(),
2280 $this->quickCleanUpFinancialEntities();
2284 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2286 public function testRepeatTransactionTestRecurId() {
2287 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2288 'contact_id' => $this->_individualId
,
2289 'frequency_interval' => '1',
2291 'contribution_status_id' => 1,
2292 'start_date' => '2017-01-01 00:00:00',
2293 'currency' => 'USD',
2294 'frequency_unit' => 'month',
2295 'payment_processor_id' => $this->paymentProcessorID
,
2298 $this->callAPISuccess('contribution', 'create', array_merge(
2301 'contribution_recur_id' => $contributionRecur['id'],
2306 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', [
2307 'contribution_recur_id' => $contributionRecur['id'],
2308 'contribution_status_id' => 'Completed',
2309 'trxn_id' => uniqid(),
2312 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2313 $this->quickCleanUpFinancialEntities();
2317 * CRM-19945 Tests that Contribute.repeattransaction renews a membership when contribution status=Completed
2320 public function testRepeatTransactionMembershipRenewCompletedContribution() {
2321 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2323 $this->callAPISuccess('contribution', 'create', [
2324 'contact_id' => $originalContribution['values'][1]['contact_id'],
2325 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2326 'total_amount' => $originalContribution['values'][1]['total_amount'],
2327 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2328 'contribution_status_id' => "Failed",
2331 $this->callAPISuccess('membership', 'create', [
2332 'id' => $membership['id'],
2333 'end_date' => 'yesterday',
2334 'status_id' => 'Expired',
2337 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
2338 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2339 'contribution_status_id' => 'Completed',
2340 'trxn_id' => 'bobsled',
2343 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', [
2344 'id' => $membership['id'],
2345 'return' => 'status_id',
2348 $membership = $this->callAPISuccess('membership', 'get', [
2349 'id' => $membership['id'],
2352 $this->assertEquals('New', CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2354 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id']]);
2355 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2356 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']]);
2357 $this->quickCleanUpFinancialEntities();
2358 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2362 * CRM-19945 Tests that Contribute.repeattransaction DOES NOT renew a membership when contribution status=Failed
2364 * @dataProvider contributionStatusProvider
2366 public function testRepeatTransactionMembershipRenewContributionNotCompleted($contributionStatus) {
2367 // Completed status should renew so we don't test that here
2368 // In Progress status is only for recurring contributions so we don't test that here
2369 if (in_array($contributionStatus['name'], ['Completed', 'In Progress'])) {
2372 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2374 $this->callAPISuccess('contribution', 'create', [
2375 'contact_id' => $originalContribution['values'][1]['contact_id'],
2376 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2377 'total_amount' => $originalContribution['values'][1]['total_amount'],
2378 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2379 'contribution_status_id' => "Completed",
2382 $this->callAPISuccess('membership', 'create', [
2383 'id' => $membership['id'],
2384 'end_date' => 'yesterday',
2385 'status_id' => 'Expired',
2388 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
2389 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2390 'contribution_status_id' => $contributionStatus['name'],
2391 'trxn_id' => 'bobsled',
2394 $updatedMembership = $this->callAPISuccess('membership', 'getsingle', [
2395 'id' => $membership['id'],
2398 $dateTime = new DateTime('yesterday');
2399 $this->assertEquals($dateTime->format('Y-m-d'), $updatedMembership['end_date']);
2400 $this->assertEquals(CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Expired'), $updatedMembership['status_id']);
2402 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id']]);
2403 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2404 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']]);
2405 $this->quickCleanUpFinancialEntities();
2406 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2410 * Dataprovider provides contribution status as [optionvalue=>contribution_status_name]
2411 * FIXME: buildOptions seems to die in CRM_Core_Config::_construct when in test mode.
2414 * @throws \CiviCRM_API3_Exception
2416 public function contributionStatusProvider() {
2417 $contributionStatuses = civicrm_api3('OptionValue', 'get', [
2418 'return' => ["id", "name"],
2419 'option_group_id' => "contribution_status",
2421 foreach ($contributionStatuses['values'] as $statusName) {
2422 $statuses[] = [$statusName];
2428 * CRM-16397 test appropriate action if total amount has changed for single line items.
2430 public function testRepeatTransactionAlteredAmount() {
2431 $paymentProcessorID = $this->paymentProcessorCreate();
2432 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2433 'contact_id' => $this->_individualId
,
2434 'installments' => '12',
2435 'frequency_interval' => '1',
2437 'contribution_status_id' => 1,
2438 'start_date' => '2012-01-01 00:00:00',
2439 'currency' => 'USD',
2440 'frequency_unit' => 'month',
2441 'payment_processor_id' => $paymentProcessorID,
2443 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2446 'contribution_recur_id' => $contributionRecur['id'],
2450 $this->callAPISuccess('contribution', 'repeattransaction', [
2451 'original_contribution_id' => $originalContribution['id'],
2452 'contribution_status_id' => 'Completed',
2453 'trxn_id' => uniqid(),
2454 'total_amount' => '400',
2459 'entity_id' => $originalContribution['id'],
2467 'financial_type_id',
2468 'deductible_amount',
2469 'price_field_value_id',
2473 $this->callAPISuccessGetSingle('contribution', [
2474 'total_amount' => 400,
2476 'net_amount' => 350,
2478 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2479 'entity_id' => $originalContribution['id'],
2481 $expectedLineItem = array_merge(
2482 $lineItem1['values'][0], [
2483 'line_total' => '400.00',
2484 'unit_price' => '400.00',
2488 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2489 'entity_id' => $originalContribution['id'] +
1,
2492 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2493 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2494 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2498 * CRM-17718 test appropriate action if financial type has changed for single line items.
2500 public function testRepeatTransactionPassedInFinancialType() {
2501 $originalContribution = $this->setUpRecurringContribution();
2503 $this->callAPISuccess('contribution', 'repeattransaction', [
2504 'original_contribution_id' => $originalContribution['id'],
2505 'contribution_status_id' => 'Completed',
2506 'trxn_id' => uniqid(),
2507 'financial_type_id' => 2,
2510 'entity_id' => $originalContribution['id'],
2518 'financial_type_id',
2519 'deductible_amount',
2520 'price_field_value_id',
2525 $this->callAPISuccessGetSingle('contribution', [
2526 'total_amount' => 100,
2527 'financial_type_id' => 2,
2529 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2530 'entity_id' => $originalContribution['id'],
2532 $expectedLineItem = array_merge(
2533 $lineItem1['values'][0], [
2534 'line_total' => '100.00',
2535 'unit_price' => '100.00',
2536 'financial_type_id' => 2,
2537 'contribution_type_id' => 2,
2540 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2541 'entity_id' => $originalContribution['id'] +
1,
2543 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2544 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2545 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2549 * CRM-17718 test appropriate action if financial type has changed for single line items.
2551 public function testRepeatTransactionUpdatedFinancialType() {
2552 $originalContribution = $this->setUpRecurringContribution([], ['financial_type_id' => 2]);
2554 $this->callAPISuccess('contribution', 'repeattransaction', [
2555 'contribution_recur_id' => $originalContribution['id'],
2556 'contribution_status_id' => 'Completed',
2557 'trxn_id' => uniqid(),
2560 'entity_id' => $originalContribution['id'],
2568 'financial_type_id',
2569 'deductible_amount',
2570 'price_field_value_id',
2575 $this->callAPISuccessGetSingle('contribution', [
2576 'total_amount' => 100,
2577 'financial_type_id' => 2,
2579 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2580 'entity_id' => $originalContribution['id'],
2582 $expectedLineItem = array_merge(
2583 $lineItem1['values'][0], [
2584 'line_total' => '100.00',
2585 'unit_price' => '100.00',
2586 'financial_type_id' => 2,
2587 'contribution_type_id' => 2,
2591 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2592 'entity_id' => $originalContribution['id'] +
1,
2594 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2595 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2596 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2600 * CRM-16397 test appropriate action if campaign has been passed in.
2602 public function testRepeatTransactionPassedInCampaign() {
2603 $paymentProcessorID = $this->paymentProcessorCreate();
2604 $campaignID = $this->campaignCreate();
2605 $campaignID2 = $this->campaignCreate();
2606 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2607 'contact_id' => $this->_individualId
,
2608 'installments' => '12',
2609 'frequency_interval' => '1',
2611 'contribution_status_id' => 1,
2612 'start_date' => '2012-01-01 00:00:00',
2613 'currency' => 'USD',
2614 'frequency_unit' => 'month',
2615 'payment_processor_id' => $paymentProcessorID,
2617 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2620 'contribution_recur_id' => $contributionRecur['id'],
2621 'campaign_id' => $campaignID,
2625 $this->callAPISuccess('contribution', 'repeattransaction', [
2626 'original_contribution_id' => $originalContribution['id'],
2627 'contribution_status_id' => 'Completed',
2628 'trxn_id' => uniqid(),
2629 'campaign_id' => $campaignID2,
2632 $this->callAPISuccessGetSingle('contribution', [
2633 'total_amount' => 100,
2634 'campaign_id' => $campaignID2,
2639 * CRM-17718 campaign stored on contribution recur gets priority.
2641 * This reflects the fact we permit people to update them.
2643 public function testRepeatTransactionUpdatedCampaign() {
2644 $paymentProcessorID = $this->paymentProcessorCreate();
2645 $campaignID = $this->campaignCreate();
2646 $campaignID2 = $this->campaignCreate();
2647 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2648 'contact_id' => $this->_individualId
,
2649 'installments' => '12',
2650 'frequency_interval' => '1',
2652 'contribution_status_id' => 1,
2653 'start_date' => '2012-01-01 00:00:00',
2654 'currency' => 'USD',
2655 'frequency_unit' => 'month',
2656 'payment_processor_id' => $paymentProcessorID,
2657 'campaign_id' => $campaignID,
2659 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2662 'contribution_recur_id' => $contributionRecur['id'],
2663 'campaign_id' => $campaignID2,
2667 $this->callAPISuccess('contribution', 'repeattransaction', [
2668 'original_contribution_id' => $originalContribution['id'],
2669 'contribution_status_id' => 'Completed',
2670 'trxn_id' => uniqid(),
2673 $this->callAPISuccessGetSingle('contribution', [
2674 'total_amount' => 100,
2675 'campaign_id' => $campaignID,
2680 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2682 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2684 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
2685 $originalContribution = $this->setUpRecurringContribution([], ['financial_type_id' => 2]);
2686 // This will made the trick to get the not equals behaviour.
2687 $this->callAPISuccess('line_item', 'create', ['id' => 1, 'financial_type_id' => 4]);
2688 $this->callAPISuccess('contribution', 'repeattransaction', [
2689 'contribution_recur_id' => $originalContribution['id'],
2690 'contribution_status_id' => 'Completed',
2691 'trxn_id' => uniqid(),
2694 'entity_id' => $originalContribution['id'],
2702 'financial_type_id',
2703 'deductible_amount',
2704 'price_field_value_id',
2708 $this->callAPISuccessGetSingle('contribution', [
2709 'total_amount' => 100,
2710 'financial_type_id' => 2,
2712 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2713 'entity_id' => $originalContribution['id'],
2715 $expectedLineItem = array_merge(
2716 $lineItem1['values'][0], [
2717 'line_total' => '100.00',
2718 'unit_price' => '100.00',
2719 'financial_type_id' => 4,
2720 'contribution_type_id' => 4,
2724 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2725 'entity_id' => $originalContribution['id'] +
1,
2727 $this->callAPISuccess('line_item', 'create', ['id' => 1, 'financial_type_id' => 1]);
2728 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2729 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2730 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2734 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2736 public function testCompleteTransactionNetAmountOK() {
2737 $this->createLoggedInUser();
2738 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2739 unset($params['net_amount']);
2740 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2741 $this->callAPISuccess('contribution', 'completetransaction', [
2742 'id' => $contribution['id'],
2744 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
2745 $this->assertEquals('Completed', $contribution['contribution_status']);
2746 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2750 * CRM-14151 - Test completing a transaction via the API.
2752 public function testCompleteTransactionWithReceiptDateSet() {
2753 $this->swapMessageTemplateForTestTemplate();
2754 $mut = new CiviMailUtils($this, TRUE);
2755 $this->createLoggedInUser();
2756 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
2757 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2758 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $contribution['id'], 'trxn_date' => date('Y-m-d')]);
2759 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'sequential' => 1]);
2760 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2761 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2762 $mut->checkMailLog([
2763 'Receipt - Contribution',
2764 'receipt_date:::' . date('Ymd'),
2767 $this->revertTemplateToReservedTemplate();
2771 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2773 public function testCompleteTransactionWithEmailReceiptInput() {
2774 $contributionPage = $this->createReceiptableContributionPage();
2776 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2777 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2778 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2779 // Complete the transaction overriding is_email_receipt to = FALSE
2780 $this->callAPISuccess('contribution', 'completetransaction', [
2781 'id' => $contribution['id'],
2782 'trxn_date' => date('2011-04-09'),
2783 'trxn_id' => 'kazam',
2784 'is_email_receipt' => 0,
2786 // Check if a receipt was issued
2787 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', ['id' => $contribution['id'], 'return' => 'receipt_date']);
2788 $this->assertEquals('', $receipt_date);
2792 * Test that $is_recur is assigned to the receipt.
2794 public function testCompleteTransactionForRecurring() {
2796 $this->swapMessageTemplateForTestTemplate();
2797 $recurring = $this->setUpRecurringContribution();
2798 $contributionPage = $this->createReceiptableContributionPage(['is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1]);
2800 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2801 $this->_params
['contribution_recur_id'] = $recurring['id'];
2803 $contribution = $this->setUpForCompleteTransaction();
2805 $this->callAPISuccess('contribution', 'completetransaction', [
2806 'id' => $contribution['id'],
2807 'trxn_date' => date('2011-04-09'),
2808 'trxn_id' => 'kazam',
2809 'is_email_receipt' => 1,
2812 $this->mut
->checkMailLog([
2814 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2817 $this->revertTemplateToReservedTemplate();
2821 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2823 * If passed in it will override the default from contribution page.
2825 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2826 $mut = new CiviMailUtils($this, TRUE);
2827 $this->createLoggedInUser();
2828 // Create a Contribution Page with is_email_receipt = FALSE
2829 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [
2830 'receipt_from_name' => 'Mickey Mouse',
2831 'receipt_from_email' => 'mickey@mouse.com',
2832 'title' => "Test Contribution Page",
2833 'financial_type_id' => 1,
2834 'currency' => 'CAD',
2835 'is_monetary' => TRUE,
2836 'is_email_receipt' => 0,
2838 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2839 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
2840 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2841 // Complete the transaction overriding is_email_receipt to = TRUE
2842 $this->callAPISuccess('contribution', 'completetransaction', [
2843 'id' => $contribution['id'],
2844 'is_email_receipt' => 1,
2846 $mut->checkMailLog([
2847 'Please print this receipt for your records.',
2853 * Complete the transaction using the template with all the possible.
2855 public function testCompleteTransactionWithTestTemplate() {
2856 $this->swapMessageTemplateForTestTemplate();
2857 $contribution = $this->setUpForCompleteTransaction();
2858 $this->callAPISuccess('contribution', 'completetransaction', [
2859 'id' => $contribution['id'],
2860 'trxn_date' => date('2011-04-09'),
2861 'trxn_id' => 'kazam',
2863 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', ['id' => $contribution['id'], 'return' => 'receive_date']);
2864 $this->mut
->checkMailLog([
2865 'email:::anthony_anderson@civicrm.org',
2869 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2870 'receipt_date:::' . date('Ymd'),
2871 'contributeMode:::notify',
2872 'title:::Contribution',
2873 'displayName:::Mr. Anthony Anderson II',
2875 'contactID:::' . $this->_params
['contact_id'],
2876 'contributionID:::' . $contribution['id'],
2877 'financialTypeId:::1',
2878 'financialTypeName:::Donation',
2881 $this->revertTemplateToReservedTemplate();
2885 * Complete the transaction using the template with all the possible.
2887 public function testCompleteTransactionContributionPageFromAddress() {
2888 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [
2889 'receipt_from_name' => 'Mickey Mouse',
2890 'receipt_from_email' => 'mickey@mouse.com',
2891 'title' => "Test Contribution Page",
2892 'financial_type_id' => 1,
2893 'currency' => 'NZD',
2894 'goal_amount' => 50,
2895 'is_pay_later' => 1,
2896 'is_monetary' => TRUE,
2897 'is_email_receipt' => TRUE,
2899 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2900 $contribution = $this->setUpForCompleteTransaction();
2901 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $contribution['id']]);
2902 $this->mut
->checkMailLog([
2910 * Test completing first transaction in a recurring series.
2912 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2914 * @dataProvider getScheduledDateData
2916 * @param array $dataSet
2918 * @throws \Exception
2920 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2921 $paymentProcessorID = $this->paymentProcessorCreate();
2922 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
2923 'contact_id' => $this->_individualId
,
2924 'installments' => '2',
2925 'frequency_interval' => '1',
2927 'contribution_status_id' => 'Pending',
2928 'start_date' => '2012-01-01 00:00:00',
2929 'currency' => 'USD',
2930 'frequency_unit' => 'month',
2931 'payment_processor_id' => $paymentProcessorID,
2932 ], $dataSet['data']));
2933 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2936 'contribution_recur_id' => $contributionRecur['id'],
2937 'contribution_status_id' => 'Pending',
2938 'receive_date' => $dataSet['receive_date'],
2941 $this->callAPISuccess('Contribution', 'completetransaction', [
2942 'id' => $contribution,
2943 'receive_date' => $dataSet['receive_date'],
2945 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', [
2946 'id' => $contributionRecur['id'],
2947 'return' => ['next_sched_contribution_date', 'contribution_status_id'],
2949 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2950 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2951 $this->callAPISuccess('Contribution', 'create', array_merge(
2954 'contribution_recur_id' => $contributionRecur['id'],
2955 'contribution_status_id' => 'Completed',
2958 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', [
2959 'id' => $contributionRecur['id'],
2960 'return' => ['contribution_status_id'],
2962 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2966 * Get dates for testing.
2970 public function getScheduledDateData() {
2972 $result[]['2016-08-31-1-month'] = [
2974 'start_date' => '2016-08-31',
2975 'frequency_interval' => 1,
2976 'frequency_unit' => 'month',
2978 'receive_date' => '2016-08-31',
2979 'expected' => '2016-10-01 00:00:00',
2981 $result[]['2012-01-01-1-month'] = [
2983 'start_date' => '2012-01-01',
2984 'frequency_interval' => 1,
2985 'frequency_unit' => 'month',
2987 'receive_date' => '2012-01-01',
2988 'expected' => '2012-02-01 00:00:00',
2990 $result[]['2012-01-01-1-month'] = [
2992 'start_date' => '2012-01-01',
2993 'frequency_interval' => 1,
2994 'frequency_unit' => 'month',
2996 'receive_date' => '2012-02-29',
2997 'expected' => '2012-03-29 00:00:00',
2999 $result['receive_date_includes_time']['2012-01-01-1-month'] = [
3001 'start_date' => '2012-01-01',
3002 'frequency_interval' => 1,
3003 'frequency_unit' => 'month',
3004 'next_sched_contribution_date' => '2012-02-29',
3006 'receive_date' => '2012-02-29 16:00:00',
3007 'expected' => '2012-03-29 00:00:00',
3013 * Test completing a pledge with the completeTransaction api..
3015 * Note that we are creating a logged in user because email goes out from
3018 public function testCompleteTransactionUpdatePledgePayment() {
3019 $this->swapMessageTemplateForTestTemplate();
3020 $mut = new CiviMailUtils($this, TRUE);
3021 $mut->clearMessages();
3022 $this->createLoggedInUser();
3023 $contributionID = $this->createPendingPledgeContribution();
3024 $this->callAPISuccess('contribution', 'completetransaction', [
3025 'id' => $contributionID,
3026 'trxn_date' => '1 Feb 2013',
3028 $pledge = $this->callAPISuccessGetSingle('Pledge', [
3029 'id' => $this->_ids
['pledge'],
3031 $this->assertEquals('Completed', $pledge['pledge_status']);
3033 $status = $this->callAPISuccessGetValue('PledgePayment', [
3034 'pledge_id' => $this->_ids
['pledge'],
3035 'return' => 'status_id',
3037 $this->assertEquals(1, $status);
3038 $mut->checkMailLog([
3040 'receive_date:::20130201000000',
3041 "receipt_date:::\n",
3044 $this->revertTemplateToReservedTemplate();
3048 * Test completing a transaction with an event via the API.
3050 * Note that we are creating a logged in user because email goes out from
3053 * @throws \CRM_Core_Exception
3055 public function testCompleteTransactionWithParticipantRecord() {
3056 $mut = new CiviMailUtils($this, TRUE);
3057 $mut->clearMessages();
3058 $this->_individualId
= $this->createLoggedInUser();
3059 $contributionID = $this->createPendingParticipantContribution();
3060 $this->createJoinedProfile(['entity_id' => $this->_ids
['event']['test'], 'entity_table' => 'civicrm_event']);
3062 $this->callAPISuccess('contribution', 'completetransaction', [
3063 'id' => $contributionID,
3066 $participantStatus = $this->callAPISuccessGetValue('participant', [
3067 'id' => $this->_ids
['participant'],
3068 'return' => 'participant_status_id',
3070 $this->assertEquals(1, $participantStatus);
3072 //Assert only three activities are created.
3073 $activities = $this->callAPISuccess('Activity', 'get', [
3074 'contact_id' => $this->_individualId
,
3077 $this->assertEquals(3, count($activities));
3078 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
3079 // record two activities before and after completing payment for Event registration
3080 $this->assertEquals(2, $activityNames['Event Registration']);
3081 // update the original 'Contribution' activity created after completing payment
3082 $this->assertEquals(1, $activityNames['Contribution']);
3084 $mut->checkMailLog([
3085 'Annual CiviCRM meet',
3087 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
3088 'First Name: Logged In',
3090 ], ['Back end title']);
3095 * Test membership is renewed when transaction completed.
3097 public function testCompleteTransactionMembershipPriceSet() {
3098 $this->createPriceSetWithPage('membership');
3099 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', [
3103 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
3104 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3105 $logs = $this->callAPISuccess('MembershipLog', 'get', [
3106 'membership_id' => $this->_ids
['membership'],
3108 $this->assertEquals(1, $logs['count']);
3109 $this->assertEquals($stateOfGrace, $membership['status_id']);
3110 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $this->_ids
['contribution']]);
3111 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3112 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
3113 $this->callAPISuccessGetSingle('LineItem', [
3114 'entity_id' => $this->_ids
['membership'],
3115 'entity_table' => 'civicrm_membership',
3117 $logs = $this->callAPISuccess('MembershipLog', 'get', [
3118 'membership_id' => $this->_ids
['membership'],
3120 $this->assertEquals(2, $logs['count']);
3121 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
3122 //Assert only three activities are created.
3123 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
3124 $this->assertEquals(3, count($activities));
3125 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
3126 $this->assertArrayHasKey('Contribution', $activityNames);
3127 $this->assertArrayHasKey('Membership Signup', $activityNames);
3128 $this->assertArrayHasKey('Change Membership Status', $activityNames);
3129 $this->cleanUpAfterPriceSets();
3133 * Test if renewal activity is create after changing Pending contribution to Completed via offline
3135 public function testPendingToCompleteContribution() {
3136 $this->createPriceSetWithPage('membership');
3137 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
3138 $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3139 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
3140 $activity = $this->callAPISuccess('Activity', 'get', [
3141 'activity_type_id' => 'Membership Signup',
3142 'source_record_id' => $this->_ids
['membership'],
3143 'status_id' => 'Scheduled',
3145 $this->assertEquals(1, $activity['count']);
3147 // change pending contribution to completed
3148 $form = new CRM_Contribute_Form_Contribution();
3151 'id' => $this->_ids
['contribution'],
3152 'total_amount' => 20,
3155 'financial_type_id' => 1,
3156 'contact_id' => $this->_individualId
,
3157 'contribution_status_id' => 1,
3158 'billing_middle_name' => '',
3159 'billing_last_name' => 'Adams',
3160 'billing_street_address-5' => '790L Lincoln St S',
3161 'billing_city-5' => 'Maryknoll',
3162 'billing_state_province_id-5' => 1031,
3163 'billing_postal_code-5' => 10545,
3164 'billing_country_id-5' => 1228,
3165 'frequency_interval' => 1,
3166 'frequency_unit' => 'month',
3167 'installments' => '',
3168 'hidden_AdditionalDetail' => 1,
3169 'hidden_Premium' => 1,
3170 'from_email_address' => '"civi45" <civi45@civicrm.com>',
3171 'receipt_date' => '',
3172 'receipt_date_time' => '',
3173 'payment_processor_id' => $this->paymentProcessorID
,
3174 'currency' => 'USD',
3175 'contribution_page_id' => $this->_ids
['contribution_page'],
3176 'contribution_mode' => 'membership',
3177 'source' => 'Membership Signup and Renewal',
3180 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3182 // Case 2: After successful payment for Pending backoffice there are three activities created
3183 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
3184 $activity = $this->callAPISuccess('Activity', 'get', [
3185 'activity_type_id' => 'Membership Signup',
3186 'source_record_id' => $this->_ids
['membership'],
3187 'status_id' => 'Completed',
3189 $this->assertEquals(1, $activity['count']);
3190 // 2.b Contribution activity created to record successful payment
3191 $activity = $this->callAPISuccess('Activity', 'get', [
3192 'activity_type_id' => 'Contribution',
3193 'source_record_id' => $this->_ids
['contribution'],
3194 'status_id' => 'Completed',
3196 $this->assertEquals(1, $activity['count']);
3198 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
3199 $activity = $this->callAPISuccess('Activity', 'get', [
3200 'activity_type_id' => 'Change Membership Status',
3201 'source_record_id' => $this->_ids
['membership'],
3202 'status_id' => 'Completed',
3204 $this->assertEquals(1, $activity['count']);
3205 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
3207 //Create another pending contribution for renewal
3208 $contribution = $this->callAPISuccess('contribution', 'create', [
3210 'contact_id' => $this->_ids
['contact'],
3211 'receive_date' => date('Ymd'),
3212 'total_amount' => 20.00,
3213 'financial_type_id' => 1,
3214 'payment_instrument_id' => 'Credit Card',
3215 'non_deductible_amount' => 10.00,
3216 'trxn_id' => 'rdhfi88',
3217 'invoice_id' => 'dofhiewuyr',
3219 'contribution_status_id' => 2,
3220 'contribution_page_id' => $this->_ids
['contribution_page'],
3221 // We can't rely on contribution api to link line items correctly to membership
3222 'skipLineItem' => TRUE,
3223 'api.membership_payment.create' => ['membership_id' => $this->_ids
['membership']],
3226 $this->callAPISuccess('line_item', 'create', [
3227 'entity_id' => $contribution['id'],
3228 'entity_table' => 'civicrm_contribution',
3229 'contribution_id' => $contribution['id'],
3230 'price_field_id' => $this->_ids
['price_field'][0],
3234 'financial_type_id' => 1,
3235 'price_field_value_id' => $this->_ids
['price_field_value']['cont'],
3237 $this->callAPISuccess('line_item', 'create', [
3238 'entity_id' => $this->_ids
['membership'],
3239 'entity_table' => 'civicrm_membership',
3240 'contribution_id' => $contribution['id'],
3241 'price_field_id' => $this->_ids
['price_field'][0],
3245 'financial_type_id' => 1,
3246 'price_field_value_id' => $this->_ids
['price_field_value'][0],
3247 'membership_type_id' => $this->_ids
['membership_type'],
3250 //Update it to Failed.
3251 $form->_params
['id'] = $contribution['id'];
3252 $form->_params
['contribution_status_id'] = 4;
3254 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3255 //Existing membership should not get updated to expired.
3256 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3257 $this->assertNotEquals($membership['status_id'], 4);
3261 * Test membership is renewed when transaction completed.
3263 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3264 $this->createPriceSetWithPage('membership');
3265 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
3266 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $this->_ids
['contribution']]);
3267 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3268 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3269 $this->cleanUpAfterPriceSets();
3272 public function cleanUpAfterPriceSets() {
3273 $this->quickCleanUpFinancialEntities();
3274 $this->contactDelete($this->_ids
['contact']);
3278 * Set up a pending transaction with a specific price field id.
3280 * @param int $priceFieldValueID
3281 * @param array $contriParams
3283 public function setUpPendingContribution($priceFieldValueID, $contriParams = []) {
3284 $contactID = $this->individualCreate();
3285 $membership = $this->callAPISuccess('membership', 'create', [
3286 'contact_id' => $contactID,
3287 'membership_type_id' => $this->_ids
['membership_type'],
3288 'start_date' => 'yesterday - 1 year',
3289 'end_date' => 'yesterday',
3290 'join_date' => 'yesterday - 1 year',
3292 $contribution = $this->callAPISuccess('contribution', 'create', array_merge([
3294 'contact_id' => $contactID,
3295 'receive_date' => date('Ymd'),
3296 'total_amount' => 20.00,
3297 'financial_type_id' => 1,
3298 'payment_instrument_id' => 'Credit Card',
3299 'non_deductible_amount' => 10.00,
3300 'trxn_id' => 'jdhfi' . rand(1, 100),
3301 'invoice_id' => 'djfhiew' . rand(5, 100),
3303 'contribution_status_id' => 2,
3304 'contribution_page_id' => $this->_ids
['contribution_page'],
3305 'api.membership_payment.create' => ['membership_id' => $membership['id']],
3308 $this->callAPISuccess('line_item', 'create', [
3309 'entity_id' => $contribution['id'],
3310 'entity_table' => 'civicrm_contribution',
3311 'contribution_id' => $contribution['id'],
3312 'price_field_id' => $this->_ids
['price_field'][0],
3316 'financial_type_id' => 1,
3317 'price_field_value_id' => $priceFieldValueID,
3319 $this->_ids
['contact'] = $contactID;
3320 $this->_ids
['contribution'] = $contribution['id'];
3321 $this->_ids
['membership'] = $membership['id'];
3325 * Test sending a mail via the API.
3327 public function testSendMail() {
3328 $mut = new CiviMailUtils($this, TRUE);
3329 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3330 $this->callAPISuccess('contribution', 'sendconfirmation', [
3331 'id' => $contribution['id'],
3332 'receipt_from_email' => 'api@civicrm.org',
3334 $mut->checkMailLog([
3336 'Contribution Information',
3337 'Please print this confirmation for your records',
3342 $this->checkCreditCardDetails($mut, $contribution['id']);
3347 * Test sending a mail via the API.
3348 * This simulates webform_civicrm using pay later contribution page
3350 public function testSendconfirmationPayLater() {
3351 $mut = new CiviMailUtils($this, TRUE);
3353 // Create contribution page
3355 'title' => 'Webform Contributions',
3356 'financial_type_id' => 1,
3357 'contribution_type_id' => 1,
3358 'is_confirm_enabled' => 1,
3359 'is_pay_later' => 1,
3360 'pay_later_text' => 'I will send payment by cheque',
3361 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3363 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3365 // Create pay later contribution
3367 'contact_id' => $this->_individualId
,
3368 'financial_type_id' => 1,
3369 'is_pay_later' => 1,
3370 'contribution_status_id' => 2,
3371 'contribution_page_id' => $contributionPage['id'],
3372 'total_amount' => '10.00',
3374 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3378 'contribution_id' => $contribution['id'],
3379 'entity_id' => $contribution['id'],
3380 'entity_table' => 'civicrm_contribution',
3381 'label' => 'My lineitem label',
3383 'unit_price' => "10.00",
3384 'line_total' => "10.00",
3386 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3390 civicrm_api3('contribution', 'sendconfirmation', [
3391 'id' => $contribution['id'],
3392 'receipt_from_email' => 'api@civicrm.org',
3395 catch (Exception
$e) {
3396 // Need to figure out how to stop this some other day
3397 // We don't care about the Payment Processor because this is Pay Later
3398 // The point of this test is to check we get the pay_later version of the mail
3399 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3404 // Retrieve mail & check it has the pay_later_receipt info
3405 $mut->getMostRecentEmail('raw');
3406 $mut->checkMailLog([
3407 (string) $contribParams['total_amount'],
3408 $pageParams['pay_later_receipt'],
3416 * Check credit card details in sent mail via API
3418 * @param $mut obj CiviMailUtils instance
3419 * @param int $contributionID Contribution ID
3422 public function checkCreditCardDetails($mut, $contributionID) {
3423 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3424 $this->callAPISuccess('contribution', 'sendconfirmation', [
3425 'id' => $contributionID,
3426 'receipt_from_email' => 'api@civicrm.org',
3427 'payment_processor_id' => $this->paymentProcessorID
,
3429 $mut->checkMailLog([
3430 // credit card header
3431 'Credit Card Information',
3433 'Billing Name and Address',
3435 'anthony_anderson@civicrm.org',
3442 * Test sending a mail via the API.
3444 public function testSendMailEvent() {
3445 $mut = new CiviMailUtils($this, TRUE);
3446 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3447 $event = $this->eventCreate([
3448 'is_email_confirm' => 1,
3449 'confirm_from_email' => 'test@civicrm.org',
3451 $this->_eventID
= $event['id'];
3452 $participantParams = [
3453 'contact_id' => $this->_individualId
,
3454 'event_id' => $this->_eventID
,
3457 // to ensure it matches later on
3458 'register_date' => '2007-07-21 00:00:00',
3459 'source' => 'Online Event Registration: API Testing',
3462 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3463 $this->callAPISuccess('participant_payment', 'create', [
3464 'participant_id' => $participant['id'],
3465 'contribution_id' => $contribution['id'],
3467 $this->callAPISuccess('contribution', 'sendconfirmation', [
3468 'id' => $contribution['id'],
3469 'receipt_from_email' => 'api@civicrm.org',
3472 $mut->checkMailLog([
3473 'Annual CiviCRM meet',
3475 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3481 * This function does a GET & compares the result against the $params.
3483 * Use as a double check on Creates.
3485 * @param array $params
3487 * @param bool $delete
3489 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3491 $contribution = $this->callAPISuccess('Contribution', 'Get', [
3496 $this->callAPISuccess('contribution', 'delete', ['id' => $id]);
3498 $this->assertAPISuccess($contribution, 0);
3499 $values = $contribution['values'][$contribution['id']];
3500 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3501 // this is not returned in id format
3502 unset($params['payment_instrument_id']);
3503 $params['contribution_source'] = $params['source'];
3504 unset($params['source']);
3505 foreach ($params as $key => $value) {
3506 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3511 * Create a pending contribution & linked pending pledge record.
3513 public function createPendingPledgeContribution() {
3515 $pledgeID = $this->pledgeCreate(['contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500]);
3516 $this->_ids
['pledge'] = $pledgeID;
3517 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, [
3518 'contribution_status_id' => 'Pending',
3519 'total_amount' => 500,
3521 $paymentID = $this->callAPISuccessGetValue('PledgePayment', [
3522 'options' => ['limit' => 1],
3525 $this->callAPISuccess('PledgePayment', 'create', [
3527 'contribution_id' =>
3528 $contribution['id'],
3529 'status_id' => 'Pending',
3530 'scheduled_amount' => 500,
3533 return $contribution['id'];
3537 * Create a pending contribution & linked pending participant record (along with an event).
3539 public function createPendingParticipantContribution() {
3540 $this->_ids
['event']['test'] = $this->eventCreate(['is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'])['id'];
3541 $participantID = $this->participantCreate(['event_id' => $this->_ids
['event']['test'], 'status_id' => 6, 'contact_id' => $this->_individualId
]);
3542 $this->_ids
['participant'] = $participantID;
3543 $params = array_merge($this->_params
, ['contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee']);
3544 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3545 $this->callAPISuccess('participant_payment', 'create', [
3546 'contribution_id' => $contribution['id'],
3547 'participant_id' => $participantID,
3549 $this->callAPISuccess('line_item', 'get', [
3550 'entity_id' => $contribution['id'],
3551 'entity_table' => 'civicrm_contribution',
3552 'api.line_item.create' => [
3553 'entity_id' => $participantID,
3554 'entity_table' => 'civicrm_participant',
3557 return $contribution['id'];
3561 * Get financial transaction amount.
3563 * @param int $contId
3565 * @return null|string
3567 public function _getFinancialTrxnAmount($contId) {
3569 SUM( ft.total_amount ) AS total
3570 FROM civicrm_financial_trxn AS ft
3571 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3572 WHERE ceft.entity_table = 'civicrm_contribution'
3573 AND ceft.entity_id = {$contId}";
3575 $result = CRM_Core_DAO
::singleValueQuery($query);
3580 * @param int $contId
3582 * @return null|string
3584 public function _getFinancialItemAmount($contId) {
3585 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3588 FROM civicrm_financial_item
3589 WHERE entity_table = 'civicrm_line_item'
3590 AND entity_id = {$lineItem}";
3591 $result = CRM_Core_DAO
::singleValueQuery($query);
3596 * @param int $contId
3599 public function _checkFinancialItem($contId, $context) {
3600 if ($context != 'paylater') {
3602 'entity_id' => $contId,
3603 'entity_table' => 'civicrm_contribution',
3605 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3607 'financial_trxn_id' => $trxn['financial_trxn_id'],
3608 'entity_table' => 'civicrm_financial_item',
3610 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3612 'id' => $entityTrxn['entity_id'],
3615 if ($context == 'paylater') {
3616 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3617 foreach ($lineItems as $key => $item) {
3619 'entity_id' => $key,
3620 'entity_table' => 'civicrm_line_item',
3622 $compareParams = ['status_id' => 1];
3623 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3626 elseif ($context == 'refund') {
3629 'financial_account_id' => 1,
3633 elseif ($context == 'cancelPending') {
3636 'financial_account_id' => 1,
3640 elseif ($context == 'changeFinancial') {
3641 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3643 'entity_id' => $lineKey,
3647 'financial_account_id' => 1,
3649 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3651 'financial_account_id' => 3,
3652 'entity_id' => $lineKey,
3658 if ($context != 'paylater') {
3659 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3664 * Check correct financial transaction entries were created for the change in payment instrument.
3666 * @param int $contributionID
3667 * @param int $originalInstrumentID
3668 * @param int $newInstrumentID
3669 * @param int $amount
3671 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3673 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3675 $originalTrxnParams = [
3676 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3677 'payment_instrument_id' => $originalInstrumentID,
3678 'amount' => $amount,
3682 $reversalTrxnParams = [
3683 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3684 'payment_instrument_id' => $originalInstrumentID,
3685 'amount' => -$amount,
3690 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3691 'payment_instrument_id' => $newInstrumentID,
3692 'amount' => $amount,
3696 foreach ([$originalTrxnParams, $reversalTrxnParams, $newTrxnParams] as $index => $transaction) {
3697 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3698 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3700 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', [
3701 'id' => $entityFinancialTrxn['financial_trxn_id'],
3703 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3704 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3705 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3706 $this->assertEquals(0, $financialTrxn['fee_amount']);
3707 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3708 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3711 $this->assertEquals(1, $financialTrxn['is_payment']);
3712 $this->assertEquals('USD', $financialTrxn['currency']);
3713 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3718 * Check financial transaction.
3720 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3722 * @param array $contribution
3723 * @param string $context
3724 * @param int $instrumentId
3725 * @param array $extraParams
3727 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = []) {
3728 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3729 $trxn = array_pop($financialTrxns);
3732 'id' => $trxn['financial_trxn_id'],
3734 if ($context == 'payLater') {
3737 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3740 elseif ($context == 'refund') {
3742 'to_financial_account_id' => 6,
3743 'total_amount' => -100,
3745 'trxn_date' => '2015-01-01 09:00:00',
3746 'trxn_id' => 'the refund',
3749 elseif ($context == 'cancelPending') {
3751 'to_financial_account_id' => 7,
3752 'total_amount' => -100,
3756 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3757 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3758 // It does the same thing with greater readability.
3759 // @todo remove handling for
3762 'entity_id' => $contribution['id'],
3763 'entity_table' => 'civicrm_contribution',
3766 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3768 'id' => $trxn['financial_trxn_id'],
3770 if (empty($extraParams)) {
3772 'total_amount' => -100,
3778 'total_amount' => 100,
3782 if ($context == 'paymentInstrument') {
3783 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3784 $compareParams['payment_instrument_id'] = $instrumentId;
3787 $compareParams['to_financial_account_id'] = 12;
3789 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3790 $compareParams['total_amount'] = 100;
3793 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3799 public function _addPaymentInstrument() {
3800 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3802 'option_group_id' => $gId,
3803 'label' => 'Test Card',
3804 'name' => 'Test Card',
3809 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3810 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3811 $financialParams = [
3812 'entity_table' => 'civicrm_option_value',
3813 'entity_id' => $optionValue['id'],
3814 'account_relationship' => $relationTypeId,
3815 'financial_account_id' => 7,
3817 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams);
3818 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3819 return $optionValue['values'][$optionValue['id']]['value'];
3822 public function _deletedAddedPaymentInstrument() {
3823 $result = $this->callAPISuccess('OptionValue', 'get', [
3824 'option_group_id' => 'payment_instrument',
3825 'name' => 'Test Card',
3829 if ($id = CRM_Utils_Array
::value('id', $result)) {
3830 $this->callAPISuccess('OptionValue', 'delete', ['id' => $id]);
3835 * Set up the basic recurring contribution for tests.
3837 * @param array $generalParams
3838 * Parameters that can be merged into the recurring AND the contribution.
3840 * @param array $recurParams
3841 * Parameters to merge into the recur only.
3845 protected function setUpRecurringContribution($generalParams = [], $recurParams = []) {
3846 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
3847 'contact_id' => $this->_individualId
,
3848 'installments' => '12',
3849 'frequency_interval' => '1',
3851 'contribution_status_id' => 1,
3852 'start_date' => '2012-01-01 00:00:00',
3853 'currency' => 'USD',
3854 'frequency_unit' => 'month',
3855 'payment_processor_id' => $this->paymentProcessorID
,
3856 ], $generalParams, $recurParams));
3857 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3860 'contribution_recur_id' => $contributionRecur['id'],
3863 return $originalContribution;
3867 * Set up a basic auto-renew membership for tests.
3869 * @param array $generalParams
3870 * Parameters that can be merged into the recurring AND the contribution.
3872 * @param array $recurParams
3873 * Parameters to merge into the recur only.
3877 protected function setUpAutoRenewMembership($generalParams = [], $recurParams = []) {
3878 $newContact = $this->callAPISuccess('Contact', 'create', [
3879 'contact_type' => 'Individual',
3880 'sort_name' => 'McTesterson, Testy',
3881 'display_name' => 'Testy McTesterson',
3882 'preferred_language' => 'en_US',
3883 'preferred_mail_format' => 'Both',
3884 'first_name' => 'Testy',
3885 'last_name' => 'McTesterson',
3886 'contact_is_deleted' => '0',
3888 'email' => 'tmctesterson@example.com',
3891 $membershipType = $this->callAPISuccess('MembershipType', 'create', [
3892 'domain_id' => "Default Domain Name",
3893 'member_of_contact_id' => 1,
3894 'financial_type_id' => "Member Dues",
3895 'duration_unit' => "month",
3896 'duration_interval' => 1,
3897 'period_type' => 'rolling',
3898 'name' => "Standard Member",
3899 'minimum_fee' => 100,
3901 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
3902 'contact_id' => $newContact['id'],
3903 'installments' => '12',
3904 'frequency_interval' => '1',
3906 'contribution_status_id' => 1,
3907 'start_date' => '2012-01-01 00:00:00',
3908 'currency' => 'USD',
3909 'frequency_unit' => 'month',
3910 'payment_processor_id' => $this->paymentProcessorID
,
3911 ], $generalParams, $recurParams));
3913 $membership = $this->callAPISuccess('membership', 'create', [
3914 'contact_id' => $newContact['id'],
3915 'contribution_recur_id' => $contributionRecur['id'],
3916 'financial_type_id' => "Member Dues",
3917 'membership_type_id' => $membershipType['id'],
3919 'skipLineItem' => TRUE,
3922 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3923 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3926 'contact_id' => $newContact['id'],
3927 'contribution_recur_id' => $contributionRecur['id'],
3928 'financial_type_id' => "Member Dues",
3929 'contribution_status_id' => 1,
3930 'invoice_id' => uniqid(),
3933 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', []);
3934 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3935 $membership = $this->callAPISuccess('Membership', 'getsingle', ['id' => $lineItem['entity_id']]);
3936 $this->callAPISuccess('LineItem', 'getsingle', []);
3937 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']], 1);
3939 return [$originalContribution, $membership];
3943 * Set up a repeat transaction.
3945 * @param array $recurParams
3946 * @param mixed $flag
3947 * @param array $contributionParams
3950 protected function setUpRepeatTransaction($recurParams = [], $flag, $contributionParams = []) {
3951 $paymentProcessorID = $this->paymentProcessorCreate();
3952 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
3953 'contact_id' => $this->_individualId
,
3954 'installments' => '12',
3955 'frequency_interval' => '1',
3957 'contribution_status_id' => 1,
3958 'start_date' => '2012-01-01 00:00:00',
3959 'currency' => 'USD',
3960 'frequency_unit' => 'month',
3961 'payment_processor_id' => $paymentProcessorID,
3964 $originalContribution = '';
3965 if ($flag == 'multiple') {
3966 // CRM-19309 create a contribution + also add in line_items (plural):
3967 $params = array_merge($this->_params
, $contributionParams);
3968 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3971 'contribution_recur_id' => $contributionRecur['id'],
3972 'skipLineItem' => 1,
3973 'api.line_item.create' => [
3975 'price_field_id' => 1,
3977 'line_total' => '20',
3978 'unit_price' => '10',
3979 'financial_type_id' => 1,
3982 'price_field_id' => 1,
3984 'line_total' => '80',
3985 'unit_price' => '80',
3986 'financial_type_id' => 2,
3993 elseif ($flag == 'single') {
3994 $params = array_merge($this->_params
, ['contribution_recur_id' => $contributionRecur['id']]);
3995 $params = array_merge($params, $contributionParams);
3996 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3998 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3999 return $originalContribution;
4003 * Common set up routine.
4007 protected function setUpForCompleteTransaction() {
4008 $this->mut
= new CiviMailUtils($this, TRUE);
4009 $this->createLoggedInUser();
4010 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
4011 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4012 return $contribution;
4016 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
4018 public function testRepeatTransactionWithNonCreditCardDefault() {
4019 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
4020 'contact_id' => $this->_individualId
,
4021 'installments' => '12',
4022 'frequency_interval' => '1',
4024 'contribution_status_id' => 1,
4025 'start_date' => '2012-01-01 00:00:00',
4026 'currency' => 'USD',
4027 'frequency_unit' => 'month',
4028 'payment_processor_id' => $this->paymentProcessorID
,
4030 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
4032 ['contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2])
4034 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
4035 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', [
4036 'contribution_status_id' => 'Completed',
4037 'trxn_id' => uniqid(),
4038 'original_contribution_id' => $contribution1,
4040 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
4041 $this->quickCleanUpFinancialEntities();
4045 * CRM-20008 Tests repeattransaction creates pending membership.
4047 public function testRepeatTransactionMembershipCreatePendingContribution() {
4048 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
4049 $this->callAPISuccess('membership', 'create', [
4050 'id' => $membership['id'],
4051 'end_date' => 'yesterday',
4052 'status_id' => 'Expired',
4054 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', [
4055 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
4056 'contribution_status_id' => 'Pending',
4057 'trxn_id' => uniqid(),
4059 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', [
4060 'id' => $membership['id'],
4061 'return' => 'status_id',
4064 // Let's see if the membership payments got created while we're at it.
4065 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', [
4066 'membership_id' => $membership['id'],
4068 $this->assertEquals(2, $membershipPayments['count']);
4070 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
4071 $this->callAPISuccess('Contribution', 'completetransaction', ['id' => $repeatedContribution['id']]);
4072 $membership = $this->callAPISuccessGetSingle('membership', [
4073 'id' => $membership['id'],
4074 'return' => 'status_id, end_date',
4076 $this->assertEquals('New', CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
4077 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
4079 $this->quickCleanUpFinancialEntities();
4080 $this->contactDelete($originalContribution['values'][1]['contact_id']);
4084 * Test sending a mail via the API.
4086 public function testSendMailWithAPISetFromDetails() {
4087 $mut = new CiviMailUtils($this, TRUE);
4088 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
4089 $this->callAPISuccess('contribution', 'sendconfirmation', [
4090 'id' => $contribution['id'],
4091 'receipt_from_email' => 'api@civicrm.org',
4092 'receipt_from_name' => 'CiviCRM LLC',
4094 $mut->checkMailLog([
4095 'From: CiviCRM LLC <api@civicrm.org>',
4096 'Contribution Information',
4097 'Please print this confirmation for your records',
4105 * Test sending a mail via the API.
4107 public function testSendMailWithNoFromSetFallToDomain() {
4108 $this->createLoggedInUser();
4109 $mut = new CiviMailUtils($this, TRUE);
4110 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
4111 $this->callAPISuccess('contribution', 'sendconfirmation', [
4112 'id' => $contribution['id'],
4114 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => 1]);
4115 $mut->checkMailLog([
4116 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4117 'Contribution Information',
4118 'Please print this confirmation for your records',
4126 * Test sending a mail via the API.
4128 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
4129 $this->createLoggedInUser();
4130 $mut = new CiviMailUtils($this, TRUE);
4131 $contribution = $this->setUpRepeatTransaction([], 'single');
4132 $this->callAPISuccess('contribution', 'repeattransaction', [
4133 'contribution_status_id' => 'Completed',
4134 'trxn_id' => uniqid(),
4135 'original_contribution_id' => $contribution,
4137 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => 1]);
4138 $mut->checkMailLog([
4139 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4140 'Contribution Information',
4141 'Please print this confirmation for your records',
4150 * Test sending a mail via the API.
4152 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
4153 $mut = new CiviMailUtils($this, TRUE);
4154 $contributionPage = $this->contributionPageCreate(['receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1]);
4155 $paymentProcessorID = $this->paymentProcessorCreate();
4156 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
4157 'contact_id' => $this->_individualId
,
4158 'installments' => '12',
4159 'frequency_interval' => '1',
4161 'contribution_status_id' => 1,
4162 'start_date' => '2012-01-01 00:00:00',
4163 'currency' => 'USD',
4164 'frequency_unit' => 'month',
4165 'payment_processor_id' => $paymentProcessorID,
4167 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
4170 'contribution_recur_id' => $contributionRecur['id'],
4171 'contribution_page_id' => $contributionPage['id'],
4174 $this->callAPISuccess('contribution', 'repeattransaction', [
4175 'contribution_status_id' => 'Completed',
4176 'trxn_id' => uniqid(),
4177 'original_contribution_id' => $originalContribution,
4180 $mut->checkMailLog([
4181 'From: CiviCRM LLC <contributionpage@civicrm.org>',
4182 'Contribution Information',
4183 'Please print this confirmation for your records',
4191 * Test sending a mail via the API.
4193 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4194 $mut = new CiviMailUtils($this, TRUE);
4195 $originalContribution = $contribution = $this->setUpRepeatTransaction([], 'single');
4196 $fromEmail = $this->CallAPISuccess('optionValue', 'get', ['is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1]);
4197 foreach ($fromEmail['values'] as $from) {
4198 $this->callAPISuccess('optionValue', 'create', ['is_default' => 0, 'id' => $from['id']]);
4200 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => CRM_Core_Config
::domainID()]);
4201 $this->callAPISuccess('contribution', 'repeattransaction', [
4202 'contribution_status_id' => 'Completed',
4203 'trxn_id' => uniqid(),
4204 'original_contribution_id' => $originalContribution,
4206 $mut->checkMailLog([
4207 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4208 'Contribution Information',
4209 'Please print this confirmation for your records',
4217 * Create a Contribution Page with is_email_receipt = TRUE.
4219 * @param array $params
4220 * Params to overwrite with.
4224 protected function createReceiptableContributionPage($params = []) {
4225 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge([
4226 'receipt_from_name' => 'Mickey Mouse',
4227 'receipt_from_email' => 'mickey@mouse.com',
4228 'title' => "Test Contribution Page",
4229 'financial_type_id' => 1,
4230 'currency' => 'CAD',
4231 'is_monetary' => TRUE,
4232 'is_email_receipt' => TRUE,
4234 return $contributionPage;
4238 * function to test card_type and pan truncation.
4240 public function testCardTypeAndPanTruncation() {
4241 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
4242 $contactId = $this->individualCreate();
4244 'contact_id' => $contactId,
4245 'receive_date' => '2016-01-20',
4246 'total_amount' => 100,
4247 'financial_type_id' => 1,
4248 'payment_instrument' => 'Credit Card',
4249 'card_type_id' => $creditCardTypeIDs['Visa'],
4250 'pan_truncation' => 4567,
4252 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4253 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
4254 $financialTrxn = $this->callAPISuccessGetSingle(
4257 'id' => $lastFinancialTrxnId['financialTrxnId'],
4258 'return' => ['card_type_id', 'pan_truncation'],
4261 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4262 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
4264 'id' => $contribution['id'],
4265 'pan_truncation' => 2345,
4266 'card_type_id' => $creditCardTypeIDs['Amex'],
4268 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4269 $financialTrxn = $this->callAPISuccessGetSingle(
4272 'id' => $lastFinancialTrxnId['financialTrxnId'],
4273 'return' => ['card_type_id', 'pan_truncation'],
4276 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4277 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
4281 * Test repeat contribution uses non default currency
4282 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4284 public function testRepeatTransactionWithDifferenceCurrency() {
4285 $originalContribution = $this->setUpRepeatTransaction(['currency' => 'AUD'], 'single', ['currency' => 'AUD']);
4286 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
4287 'original_contribution_id' => $originalContribution['id'],
4288 'contribution_status_id' => 'Completed',
4289 'trxn_id' => uniqid(),
4291 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4295 * Get the financial items for the contribution.
4297 * @param int $contributionID
4300 * Array of associated financial items.
4302 protected function getFinancialTransactionsForContribution($contributionID) {
4304 'entity_id' => $contributionID,
4305 'entity_table' => 'civicrm_contribution',
4307 // @todo the following function has naming errors & has a weird signature & appears to
4308 // only be called from test classes. Move into test suite & maybe just use api
4309 // from this function.
4310 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, []));
4314 * Test getunique api call for Contribution entity
4316 public function testContributionGetUnique() {
4317 $result = $this->callAPIAndDocument($this->_entity
, 'getunique', [], __FUNCTION__
, __FILE__
);
4318 $this->assertEquals(2, $result['count']);
4319 $this->assertEquals(['trxn_id'], $result['values']['UI_contrib_trxn_id']);
4320 $this->assertEquals(['invoice_id'], $result['values']['UI_contrib_invoice_id']);
4324 * Test Repeat Transaction Contribution with Tax amount.
4325 * https://lab.civicrm.org/dev/core/issues/806
4327 public function testRepeatContributionWithTaxAmount() {
4328 $this->enableTaxAndInvoicing();
4329 $financialType = $this->callAPISuccess('financial_type', 'create', [
4330 'name' => 'Test taxable financial Type',
4334 $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
4335 $contribution = $this->setUpRepeatTransaction(
4339 'financial_type_id' => $financialType['id'],
4342 $this->callAPISuccess('contribution', 'repeattransaction', [
4343 'original_contribution_id' => $contribution['id'],
4344 'contribution_status_id' => 'Completed',
4345 'trxn_id' => uniqid(),
4347 $this->callAPISuccessGetCount('Contribution', [], 2);
4350 public function testGetCurrencyOptions() {
4351 $result = $this->callAPISuccess('Contribution', 'getoptions', [
4352 'field' => 'currency',
4354 $this->assertEquals('US Dollar', $result['values']['USD']);
4355 $this->assertNotContains('$', $result['values']);
4356 $result = $this->callAPISuccess('Contribution', 'getoptions', [
4357 'field' => 'currency',
4358 'context' => "abbreviate",
4360 $this->assertEquals('$', $result['values']['USD']);
4361 $this->assertNotContains('US Dollar', $result['values']);