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
{
38 * Assume empty database with just civicrm_data.
40 protected $_individualId;
41 protected $_contribution;
42 protected $_financialTypeId = 1;
43 protected $_apiversion;
44 protected $_entity = 'Contribution';
47 protected $_ids = array();
48 protected $_pageParams = array();
50 * Payment processor ID (dummy processor).
54 protected $paymentProcessorID;
57 * Parameters to create payment processor.
61 protected $_processorParams = array();
64 * ID of created event.
78 public function setUp() {
81 $this->_apiversion
= 3;
82 $this->_individualId
= $this->individualCreate();
83 $this->_params
= array(
84 'contact_id' => $this->_individualId
,
85 'receive_date' => '20120511',
86 'total_amount' => 100.00,
87 'financial_type_id' => $this->_financialTypeId
,
88 'non_deductible_amount' => 10.00,
90 'net_amount' => 95.00,
92 'contribution_status_id' => 1,
94 $this->_processorParams
= array(
97 'payment_processor_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
98 'financial_account_id' => 12,
101 'url_site' => 'http://dummy.com',
102 'url_recur' => 'http://dummy.com',
105 $this->paymentProcessorID
= $this->processorCreate();
106 $this->_pageParams
= array(
107 'title' => 'Test Contribution Page',
108 'financial_type_id' => 1,
110 'financial_account_id' => 1,
111 'payment_processor' => $this->paymentProcessorID
,
113 'is_allow_other_amount' => 1,
115 'max_amount' => 1000,
120 * Clean up after each test.
122 public function tearDown() {
123 $this->quickCleanUpFinancialEntities();
124 $this->quickCleanup(array('civicrm_uf_match'));
125 $financialAccounts = $this->callAPISuccess('FinancialAccount', 'get', array());
126 foreach ($financialAccounts['values'] as $financialAccount) {
127 if ($financialAccount['name'] == 'Test Tax financial account ' ||
$financialAccount['name'] == 'Test taxable financial Type') {
128 $entityFinancialTypes = $this->callAPISuccess('EntityFinancialAccount', 'get', array(
129 'financial_account_id' => $financialAccount['id'],
131 foreach ($entityFinancialTypes['values'] as $entityFinancialType) {
132 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialType['id']));
134 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
142 public function testGetContribution() {
143 $contributionSettings = $this->enableTaxAndInvoicing();
144 $invoice_prefix = CRM_Contribute_BAO_Contribution
::checkContributeSettings('invoice_prefix', TRUE);
146 'contact_id' => $this->_individualId
,
147 'receive_date' => '2010-01-20',
148 'total_amount' => 100.00,
149 'financial_type_id' => $this->_financialTypeId
,
150 'non_deductible_amount' => 10.00,
151 'fee_amount' => 5.00,
152 'net_amount' => 95.00,
154 'invoice_id' => 78910,
156 'contribution_status_id' => 1,
158 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
161 'contribution_id' => $this->_contribution
['id'],
164 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
165 $financialParams['id'] = $this->_financialTypeId
;
167 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
169 $this->assertEquals(1, $contributions['count']);
170 $contribution = $contributions['values'][$contributions['id']];
171 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
172 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
173 // Passing back a string rather than an id seems like an error/cruft.
174 // If it is to be introduced we should discuss.
175 $this->assertEquals($contribution['financial_type_id'], 1);
176 $this->assertEquals($contribution['total_amount'], 100.00);
177 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
178 $this->assertEquals($contribution['fee_amount'], 5.00);
179 $this->assertEquals($contribution['net_amount'], 95.00);
180 $this->assertEquals($contribution['trxn_id'], 23456);
181 $this->assertEquals($contribution['invoice_id'], 78910);
182 $this->assertEquals($contribution['invoice_number'], $invoice_prefix . $contributions['id']);
183 $this->assertEquals($contribution['contribution_source'], 'SSF');
184 $this->assertEquals($contribution['contribution_status'], 'Completed');
185 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
186 $p['trxn_id'] = '3847';
187 $p['invoice_id'] = '3847';
189 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
191 // Now we have 2 - test getcount.
192 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
193 $this->assertEquals(2, $contribution);
194 // Test id only format.
195 $contribution = $this->callAPISuccess('contribution', 'get', array(
196 'id' => $this->_contribution
['id'],
197 'format.only_id' => 1,
199 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
200 // Test id only format.
201 $contribution = $this->callAPISuccess('contribution', 'get', array(
202 'id' => $contribution2['id'],
203 'format.only_id' => 1,
205 $this->assertEquals($contribution2['id'], $contribution);
207 $contribution = $this->callAPISuccess('contribution', 'get', array(
208 'id' => $this->_contribution
['id'],
210 $this->assertEquals(1, $contribution['count']);
212 // Test get by contact id works.
213 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
215 $this->assertEquals(2, $contribution['count']);
216 $this->callAPISuccess('Contribution', 'Delete', array(
217 'id' => $this->_contribution
['id'],
219 $this->callAPISuccess('Contribution', 'Delete', array(
220 'id' => $contribution2['id'],
225 * Test that test contributions can be retrieved.
227 public function testGetTestContribution() {
228 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('is_test' => 1)));
229 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
233 * Test the 'return' param works for all fields.
235 public function testGetContributionReturnFunctionality() {
236 $params = $this->_params
;
237 $params['check_number'] = 'bouncer';
238 $params['payment_instrument_id'] = 'Check';
239 $params['cancel_date'] = 'yesterday';
240 $params['receipt_date'] = 'yesterday';
241 $params['thankyou_date'] = 'yesterday';
242 $params['revenue_recognition_date'] = 'yesterday';
243 $params['amount_level'] = 'Unreasonable';
244 $params['cancel_reason'] = 'You lose sucker';
245 $params['creditnote_id'] = 'sudo rm -rf';
246 $params['tax_amount'] = '1';
247 $address = $this->callAPISuccess('Address', 'create', array(
248 'street_address' => 'Knockturn Alley',
249 'contact_id' => $this->_individualId
,
250 'location_type_id' => 'Home',
252 $params['address_id'] = $address['id'];
253 $contributionPage = $this->contributionPageCreate();
254 $params['contribution_page_id'] = $contributionPage['id'];
255 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array(
256 'contact_id' => $this->_individualId
,
257 'frequency_interval' => 1,
260 $params['contribution_recur_id'] = $contributionRecur['id'];
262 $params['campaign_id'] = $this->campaignCreate();
264 $contributionID = $this->contributionCreate($params);
266 // update contribution with invoice number
267 $params = array_merge($params, array(
268 'id' => $contributionID,
269 'invoice_number' => CRM_Utils_Array
::value('invoice_prefix', Civi
::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
271 'invoice_id' => 6789,
273 $contributionID = $this->contributionCreate($params);
275 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
276 $this->assertEquals('bouncer', $contribution['check_number']);
277 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
279 $fields = CRM_Contribute_BAO_Contribution
::fields();
280 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
281 // need to specify them.
282 $fields['address_id'] = $fields['contribution_address_id'];
283 $fields['check_number'] = $fields['contribution_check_number'];
285 $fieldsLockedIn = array(
286 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
287 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
288 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
289 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
290 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
291 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
293 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
294 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
295 // covered as the test contract now guarantees them in the return array.
296 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
297 foreach ($fields as $fieldName => $fieldSpec) {
298 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
299 $returnField = $fieldName;
300 if ($returnField == 'contribution_contact_id') {
301 $returnField = 'contact_id';
303 $this->assertTrue((!empty($contribution[$returnField]) ||
$contribution[$returnField] === "0"), $returnField);
308 * Test cancel reason works as a filter.
310 public function testFilterCancelReason() {
311 $params = $this->_params
;
312 $params['cancel_date'] = 'yesterday';
313 $params['cancel_reason'] = 'You lose sucker';
314 $this->callAPISuccess('Contribution', 'create', $params);
315 $params = $this->_params
;
316 $params['cancel_date'] = 'yesterday';
317 $params['cancel_reason'] = 'You are a winner';
318 $this->callAPISuccess('Contribution', 'create', $params);
319 $this->callAPISuccessGetCount('Contribution', ['cancel_reason' => 'You are a winner'], 1);
323 * We need to ensure previous tested behaviour still works as part of the api contract.
325 public function testGetContributionLegacyBehaviour() {
327 'contact_id' => $this->_individualId
,
328 'receive_date' => '2010-01-20',
329 'total_amount' => 100.00,
330 'contribution_type_id' => $this->_financialTypeId
,
331 'non_deductible_amount' => 10.00,
332 'fee_amount' => 5.00,
333 'net_amount' => 95.00,
335 'invoice_id' => 78910,
337 'contribution_status_id' => 1,
339 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
342 'contribution_id' => $this->_contribution
['id'],
344 $contribution = $this->callAPISuccess('contribution', 'get', $params);
345 $financialParams['id'] = $this->_financialTypeId
;
347 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
349 $this->assertEquals(1, $contribution['count']);
350 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
351 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
352 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
353 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
354 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
355 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
356 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
357 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
358 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
359 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
360 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
362 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
363 $p['trxn_id'] = '3847';
364 $p['invoice_id'] = '3847';
366 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
368 // now we have 2 - test getcount
369 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
370 $this->assertEquals(2, $contribution);
371 //test id only format
372 $contribution = $this->callAPISuccess('contribution', 'get', array(
373 'id' => $this->_contribution
['id'],
374 'format.only_id' => 1,
376 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
377 //test id only format
378 $contribution = $this->callAPISuccess('contribution', 'get', array(
379 'id' => $contribution2['id'],
380 'format.only_id' => 1,
382 $this->assertEquals($contribution2['id'], $contribution);
383 $contribution = $this->callAPISuccess('contribution', 'get', array(
384 'id' => $this->_contribution
['id'],
387 $this->assertEquals(1, $contribution['count']);
388 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
389 //test get by contact id works
390 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
392 $this->assertEquals(2, $contribution['count']);
393 $this->callAPISuccess('Contribution', 'Delete', array(
394 'id' => $this->_contribution
['id'],
396 $this->callAPISuccess('Contribution', 'Delete', array(
397 'id' => $contribution2['id'],
402 * Create an contribution_id=FALSE and financial_type_id=Donation.
404 public function testCreateEmptyContributionIDUseDonation() {
406 'contribution_id' => FALSE,
409 'check_permissions' => FALSE,
410 'financial_type_id' => 'Donation',
412 $this->callAPISuccess('contribution', 'create', $params);
416 * Check with complete array + custom field.
418 * Note that the test is written on purpose without any
419 * variables specific to participant so it can be replicated into other entities
420 * and / or moved to the automated test suite
422 public function testCreateWithCustom() {
423 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
425 $params = $this->_params
;
426 $params['custom_' . $ids['custom_field_id']] = "custom string";
428 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
429 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
430 $check = $this->callAPISuccess($this->_entity
, 'get', array(
431 'return.custom_' . $ids['custom_field_id'] => 1,
432 'id' => $result['id'],
434 $this->customFieldDelete($ids['custom_field_id']);
435 $this->customGroupDelete($ids['custom_group_id']);
436 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
440 * Check with complete array + custom field.
442 * Note that the test is written on purpose without any
443 * variables specific to participant so it can be replicated into other entities
444 * and / or moved to the automated test suite
446 public function testCreateGetFieldsWithCustom() {
447 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
448 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
449 $result = $this->callAPISuccess('Contribution', 'getfields', array());
450 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
451 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
452 $this->customFieldDelete($ids['custom_field_id']);
453 $this->customGroupDelete($ids['custom_group_id']);
454 $this->customFieldDelete($idsContact['custom_field_id']);
455 $this->customGroupDelete($idsContact['custom_group_id']);
458 public function testCreateContributionNoLineItems() {
461 'contact_id' => $this->_individualId
,
462 'receive_date' => '20120511',
463 'total_amount' => 100.00,
464 'financial_type_id' => $this->_financialTypeId
,
465 'payment_instrument_id' => 1,
466 'non_deductible_amount' => 10.00,
467 'fee_amount' => 50.00,
468 'net_amount' => 90.00,
470 'invoice_id' => 67890,
472 'contribution_status_id' => 1,
476 $contribution = $this->callAPISuccess('contribution', 'create', $params);
477 $lineItems = $this->callAPISuccess('line_item', 'get', array(
478 'entity_id' => $contribution['id'],
479 'entity_table' => 'civicrm_contribution',
482 $this->assertEquals(0, $lineItems['count']);
486 * Test checks that passing in line items suppresses the create mechanism.
488 public function testCreateContributionChainedLineItems() {
490 'contact_id' => $this->_individualId
,
491 'receive_date' => '20120511',
492 'total_amount' => 100.00,
493 'financial_type_id' => $this->_financialTypeId
,
494 'payment_instrument_id' => 1,
495 'non_deductible_amount' => 10.00,
496 'fee_amount' => 50.00,
497 'net_amount' => 90.00,
499 'invoice_id' => 67890,
501 'contribution_status_id' => 1,
503 'api.line_item.create' => array(
505 'price_field_id' => 1,
507 'line_total' => '20',
508 'unit_price' => '10',
511 'price_field_id' => 1,
513 'line_total' => '80',
514 'unit_price' => '80',
519 $description = "Create Contribution with Nested Line Items.";
520 $subfile = "CreateWithNestedLineItems";
521 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
523 $lineItems = $this->callAPISuccess('line_item', 'get', array(
524 'entity_id' => $contribution['id'],
525 'contribution_id' => $contribution['id'],
526 'entity_table' => 'civicrm_contribution',
529 $this->assertEquals(2, $lineItems['count']);
532 public function testCreateContributionOffline() {
534 'contact_id' => $this->_individualId
,
535 'receive_date' => '20120511',
536 'total_amount' => 100.00,
537 'financial_type_id' => 1,
539 'invoice_id' => 67890,
541 'contribution_status_id' => 1,
544 $contribution = $this->callAPISuccess('contribution', 'create', $params);
545 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
546 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
547 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
548 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
549 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
550 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
551 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
552 $lineItems = $this->callAPISuccess('line_item', 'get', array(
553 'entity_id' => $contribution['id'],
554 'contribution_id' => $contribution['id'],
555 'entity_table' => 'civicrm_contribution',
558 $this->assertEquals(1, $lineItems['count']);
559 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
560 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
561 $this->_checkFinancialRecords($contribution, 'offline');
562 $this->contributionGetnCheck($params, $contribution['id']);
566 * Test create with valid payment instrument.
568 public function testCreateContributionWithPaymentInstrument() {
569 $params = $this->_params +
array('payment_instrument' => 'EFT');
570 $contribution = $this->callAPISuccess('contribution', 'create', $params);
571 $contribution = $this->callAPISuccess('contribution', 'get', array(
573 'id' => $contribution['id'],
575 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
576 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
578 $this->callAPISuccess('contribution', 'create', array(
579 'id' => $contribution['id'],
580 'payment_instrument' => 'Credit Card',
582 $contribution = $this->callAPISuccess('contribution', 'get', array(
584 'id' => $contribution['id'],
586 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
587 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
590 public function testGetContributionByPaymentInstrument() {
591 $params = $this->_params +
array('payment_instrument' => 'EFT');
592 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
593 $this->callAPISuccess('contribution', 'create', $params);
594 $this->callAPISuccess('contribution', 'create', $params2);
595 $contribution = $this->callAPISuccess('contribution', 'get', array(
597 'contribution_payment_instrument' => 'Cash',
599 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
600 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
601 $this->assertEquals(1, $contribution['count']);
602 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
603 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
604 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
605 $this->assertEquals(1, $contribution['count']);
606 $contribution = $this->callAPISuccess('contribution', 'get', array(
608 'payment_instrument_id' => 5,
610 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
611 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
612 $this->assertEquals(1, $contribution['count']);
613 $contribution = $this->callAPISuccess('contribution', 'get', array(
615 'payment_instrument' => 'EFT',
617 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
618 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
619 $this->assertEquals(1, $contribution['count']);
620 $contribution = $this->callAPISuccess('contribution', 'create', array(
621 'id' => $contribution['id'],
622 'payment_instrument' => 'Credit Card',
624 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
625 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
626 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
627 $this->assertEquals(1, $contribution['count']);
631 * CRM-16227 introduces invoice_id as a parameter.
633 public function testGetContributionByInvoice() {
634 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('invoice_id' => 'curly')));
635 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), array('invoice_id' => 'churlish'));
636 $this->callAPISuccessGetCount('Contribution', array(), 2);
637 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
638 // The following don't work. They are the format we are trying to introduce but although the form uses this format
639 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
640 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
641 // come out of convertFormValues
642 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
643 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
644 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
645 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
650 * Check the credit note retrieval is case insensitive.
652 public function testGetCreditNoteCaseInsensitive() {
653 $this->contributionCreate(array('contact_id' => $this->_individualId
));
654 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId
, 'invoice_id' => rand(), 'trxn_id' => rand()));
655 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
656 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
660 * Test retrieval by total_amount works.
664 public function testGetContributionByTotalAmount() {
665 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '5')));
666 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '10')));
667 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
668 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
669 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
670 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
671 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
672 $this->callAPISuccessGetCount('Contribution', array(), 2);
676 * Create test with unique field name on source.
678 public function testCreateContributionSource() {
681 'contact_id' => $this->_individualId
,
682 'receive_date' => date('Ymd'),
683 'total_amount' => 100.00,
684 'financial_type_id' => $this->_financialTypeId
,
685 'payment_instrument_id' => 1,
686 'non_deductible_amount' => 10.00,
687 'fee_amount' => 50.00,
688 'net_amount' => 90.00,
690 'invoice_id' => 67890,
691 'contribution_source' => 'SSF',
692 'contribution_status_id' => 1,
695 $contribution = $this->callAPISuccess('contribution', 'create', $params);
696 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
697 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
701 * Create test with unique field name on source.
703 * @param string $thousandSeparator
704 * punctuation used to refer to thousands.
706 * @dataProvider getThousandSeparators
708 public function testCreateDefaultNow($thousandSeparator) {
709 $this->setCurrencySeparators($thousandSeparator);
710 $params = $this->_params
;
711 unset($params['receive_date'], $params['net_amount']);
713 $params['total_amount'] = $this->formatMoneyInput(5000.77);
714 $params['fee_amount'] = $this->formatMoneyInput(.77);
715 $params['skipCleanMoney'] = FALSE;
717 $contribution = $this->callAPISuccess('contribution', 'create', $params);
718 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
719 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
720 $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77));
721 $this->assertEquals(.77, $contribution['fee_amount']);
722 $this->assertEquals(5000, $contribution['net_amount']);
726 * Create test with unique field name on source.
728 public function testCreateContributionNullOutThankyouDate() {
730 $params = $this->_params
;
731 $params['thankyou_date'] = 'yesterday';
733 $contribution = $this->callAPISuccess('contribution', 'create', $params);
734 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
735 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
737 $params['thankyou_date'] = 'null';
738 $contribution = $this->callAPISuccess('contribution', 'create', $params);
739 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
743 * Create test with unique field name on source.
745 public function testCreateContributionSourceInvalidContact() {
749 'receive_date' => date('Ymd'),
750 'total_amount' => 100.00,
751 'financial_type_id' => $this->_financialTypeId
,
752 'payment_instrument_id' => 1,
753 'non_deductible_amount' => 10.00,
754 'fee_amount' => 50.00,
755 'net_amount' => 90.00,
757 'invoice_id' => 67890,
758 'contribution_source' => 'SSF',
759 'contribution_status_id' => 1,
762 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
765 public function testCreateContributionSourceInvalidContContact() {
768 'contribution_contact_id' => 999,
769 'receive_date' => date('Ymd'),
770 'total_amount' => 100.00,
771 'financial_type_id' => $this->_financialTypeId
,
772 'payment_instrument_id' => 1,
773 'non_deductible_amount' => 10.00,
774 'fee_amount' => 50.00,
775 'net_amount' => 90.00,
777 'invoice_id' => 67890,
778 'contribution_source' => 'SSF',
779 'contribution_status_id' => 1,
782 $this->callAPIFailure('contribution', 'create', $params);
786 * Test note created correctly.
788 public function testCreateContributionWithNote() {
789 $description = "Demonstrates creating contribution with Note Entity.";
790 $subfile = "ContributionCreateWithNote";
792 'contact_id' => $this->_individualId
,
793 'receive_date' => '2012-01-01',
794 'total_amount' => 100.00,
795 'financial_type_id' => $this->_financialTypeId
,
796 'payment_instrument_id' => 1,
797 'non_deductible_amount' => 10.00,
798 'fee_amount' => 50.00,
799 'net_amount' => 90.00,
801 'invoice_id' => 67890,
803 'contribution_status_id' => 1,
804 'note' => 'my contribution note',
807 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
808 $result = $this->callAPISuccess('note', 'get', array(
809 'entity_table' => 'civicrm_contribution',
810 'entity_id' => $contribution['id'],
813 $this->assertEquals('my contribution note', $result['values'][0]['note']);
814 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
817 public function testCreateContributionWithNoteUniqueNameAliases() {
819 'contact_id' => $this->_individualId
,
820 'receive_date' => '2012-01-01',
821 'total_amount' => 100.00,
822 'financial_type_id' => $this->_financialTypeId
,
823 'payment_instrument_id' => 1,
824 'non_deductible_amount' => 10.00,
825 'fee_amount' => 50.00,
826 'net_amount' => 90.00,
828 'invoice_id' => 67890,
830 'contribution_status_id' => 1,
831 'contribution_note' => 'my contribution note',
834 $contribution = $this->callAPISuccess('contribution', 'create', $params);
835 $result = $this->callAPISuccess('note', 'get', array(
836 'entity_table' => 'civicrm_contribution',
837 'entity_id' => $contribution['id'],
840 $this->assertEquals('my contribution note', $result['values'][0]['note']);
841 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
845 * This is the test for creating soft credits.
847 public function testCreateContributionWithSoftCredit() {
848 $description = "Demonstrates creating contribution with SoftCredit.";
849 $subfile = "ContributionCreateWithSoftCredit";
850 $contact2 = $this->callAPISuccess('Contact', 'create', array(
851 'display_name' => 'superman',
852 'contact_type' => 'Individual',
855 'contact_id' => $contact2['id'],
857 'soft_credit_type_id' => 3,
860 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
861 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
862 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
864 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
865 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
866 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
868 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
869 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
872 public function testCreateContributionWithSoftCreditDefaults() {
873 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
874 $subfile = "ContributionCreateWithSoftCreditDefaults";
875 $contact2 = $this->callAPISuccess('Contact', 'create', array(
876 'display_name' => 'superman',
877 'contact_type' => 'Individual',
879 $params = $this->_params +
array(
880 'soft_credit_to' => $contact2['id'],
882 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
883 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
885 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
886 // Default soft credit amount = contribution.total_amount
887 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
888 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
890 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
891 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
895 * Test creating contribution with Soft Credit by passing in honor_contact_id.
897 public function testCreateContributionWithHonoreeContact() {
898 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
899 $subfile = "ContributionCreateWithHonoreeContact";
900 $contact2 = $this->callAPISuccess('Contact', 'create', array(
901 'display_name' => 'superman',
902 'contact_type' => 'Individual',
904 $params = $this->_params +
array(
905 'honor_contact_id' => $contact2['id'],
907 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
908 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
910 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
911 // Default soft credit amount = contribution.total_amount
912 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
913 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
914 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
915 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
917 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
918 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
922 * Test using example code.
924 public function testContributionCreateExample() {
925 //make sure at least on page exists since there is a truncate in tear down
926 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
927 require_once 'api/v3/examples/Contribution/Create.php';
928 $result = contribution_create_example();
930 $expectedResult = contribution_create_expectedresult();
931 $this->checkArrayEquals($expectedResult, $result);
932 $this->contributionDelete($id);
936 * Function tests that additional financial records are created when fee amount is recorded.
938 public function testCreateContributionWithFee() {
940 'contact_id' => $this->_individualId
,
941 'receive_date' => '20120511',
942 'total_amount' => 100.00,
944 'financial_type_id' => 1,
946 'invoice_id' => 67890,
948 'contribution_status_id' => 1,
951 $contribution = $this->callAPISuccess('contribution', 'create', $params);
952 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
953 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
954 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
955 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
956 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
957 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
958 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
959 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
960 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
962 $lineItems = $this->callAPISuccess('line_item', 'get', array(
964 'entity_id' => $contribution['id'],
965 'entity_table' => 'civicrm_contribution',
968 $this->assertEquals(1, $lineItems['count']);
969 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
970 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
971 $lineItems = $this->callAPISuccess('line_item', 'get', array(
973 'entity_id' => $contribution['id'],
974 'contribution_id' => $contribution['id'],
975 'entity_table' => 'civicrm_contribution',
978 $this->assertEquals(1, $lineItems['count']);
979 $this->_checkFinancialRecords($contribution, 'feeAmount');
984 * Function tests that additional financial records are created when online contribution is created.
986 public function testCreateContributionOnline() {
987 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
988 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
989 $this->assertAPISuccess($contributionPage);
991 'contact_id' => $this->_individualId
,
992 'receive_date' => '20120511',
993 'total_amount' => 100.00,
994 'financial_type_id' => 1,
995 'contribution_page_id' => $contributionPage['id'],
996 'payment_processor' => $this->paymentProcessorID
,
998 'invoice_id' => 67890,
1000 'contribution_status_id' => 1,
1004 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1005 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1006 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1007 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1008 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1009 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1010 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1011 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
1012 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
1013 'id' => $this->paymentProcessorID
,
1014 'return' => 'payment_instrument_id',
1016 $this->_checkFinancialRecords($contribution, 'online');
1020 * Check handling of financial type.
1022 * In the interests of removing financial type / contribution type checks from
1023 * legacy format function lets test that the api is doing this for us
1025 public function testCreateInvalidFinancialType() {
1026 $params = $this->_params
;
1027 $params['financial_type_id'] = 99999;
1028 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
1032 * Check handling of financial type.
1034 * In the interests of removing financial type / contribution type checks from
1035 * legacy format function lets test that the api is doing this for us
1037 public function testValidNamedFinancialType() {
1038 $params = $this->_params
;
1039 $params['financial_type_id'] = 'Donation';
1040 $this->callAPISuccess($this->_entity
, 'create', $params);
1044 * Tests that additional financial records are created.
1046 * Checks when online contribution with pay later option is created
1048 public function testCreateContributionPayLaterOnline() {
1049 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1050 $this->_pageParams
['is_pay_later'] = 1;
1051 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1052 $this->assertAPISuccess($contributionPage);
1054 'contact_id' => $this->_individualId
,
1055 'receive_date' => '20120511',
1056 'total_amount' => 100.00,
1057 'financial_type_id' => 1,
1058 'contribution_page_id' => $contributionPage['id'],
1060 'is_pay_later' => 1,
1061 'invoice_id' => 67890,
1063 'contribution_status_id' => 2,
1067 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1068 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1069 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1070 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1071 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1072 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1073 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1074 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1075 $this->_checkFinancialRecords($contribution, 'payLater');
1079 * Function tests that additional financial records are created for online contribution with pending option.
1081 public function testCreateContributionPendingOnline() {
1082 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1083 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1084 $this->assertAPISuccess($contributionPage);
1086 'contact_id' => $this->_individualId
,
1087 'receive_date' => '20120511',
1088 'total_amount' => 100.00,
1089 'financial_type_id' => 1,
1090 'contribution_page_id' => $contributionPage['id'],
1092 'invoice_id' => 67890,
1094 'contribution_status_id' => 2,
1097 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1098 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1099 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1100 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1101 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1102 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1103 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1104 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1105 $this->_checkFinancialRecords($contribution, 'pending');
1109 * Test that BAO defaults work.
1111 public function testCreateBAODefaults() {
1112 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1113 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1114 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1115 'id' => $contribution['id'],
1116 'api.contribution.delete' => 1,
1118 $this->assertEquals(1, $contribution['contribution_status_id']);
1119 $this->assertEquals('Check', $contribution['payment_instrument']);
1120 $this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
1124 * Test that getsingle can be chained with delete.
1126 public function testDeleteChainedGetSingle() {
1127 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1128 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1129 'id' => $contribution['id'],
1130 'api.contribution.delete' => 1,
1132 $this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
1136 * Function tests that line items, financial records are updated when contribution amount is changed.
1138 public function testCreateUpdateContributionChangeTotal() {
1139 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1140 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1142 'entity_id' => $contribution['id'],
1143 'entity_table' => 'civicrm_contribution',
1145 'return' => 'line_total',
1147 $this->assertEquals('100.00', $lineItems);
1148 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1149 // Financial trxn SUM = 100 + 5 (fee)
1150 $this->assertEquals('105.00', $trxnAmount);
1153 'id' => $contribution['id'],
1154 'total_amount' => '125',
1156 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1158 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1160 'entity_id' => $contribution['id'],
1161 'entity_table' => 'civicrm_contribution',
1163 'return' => 'line_total',
1166 $this->assertEquals('125.00', $lineItems);
1167 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1169 // Financial trxn SUM = 125 + 5 (fee).
1170 $this->assertEquals('130.00', $trxnAmount);
1171 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1175 * Function tests that line items, financial records are updated when pay later contribution is received.
1177 public function testCreateUpdateContributionPayLater() {
1178 $contribParams = array(
1179 'contact_id' => $this->_individualId
,
1180 'receive_date' => '2012-01-01',
1181 'total_amount' => 100.00,
1182 'financial_type_id' => $this->_financialTypeId
,
1183 'payment_instrument_id' => 1,
1184 'contribution_status_id' => 2,
1185 'is_pay_later' => 1,
1188 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1190 $newParams = array_merge($contribParams, array(
1191 'id' => $contribution['id'],
1192 'contribution_status_id' => 1,
1195 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1196 $contribution = $contribution['values'][$contribution['id']];
1197 $this->assertEquals($contribution['contribution_status_id'], '1');
1198 $this->_checkFinancialItem($contribution['id'], 'paylater');
1199 $this->_checkFinancialTrxn($contribution, 'payLater');
1203 * Function tests that financial records are updated when Payment Instrument is changed.
1205 public function testCreateUpdateContributionPaymentInstrument() {
1206 $instrumentId = $this->_addPaymentInstrument();
1207 $contribParams = array(
1208 'contact_id' => $this->_individualId
,
1209 'total_amount' => 100.00,
1210 'financial_type_id' => $this->_financialTypeId
,
1211 'payment_instrument_id' => 4,
1212 'contribution_status_id' => 1,
1215 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1217 $newParams = array_merge($contribParams, array(
1218 'id' => $contribution['id'],
1219 'payment_instrument_id' => $instrumentId,
1222 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1223 $this->assertAPISuccess($contribution);
1224 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1226 // cleanup - delete created payment instrument
1227 $this->_deletedAddedPaymentInstrument();
1231 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1233 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1234 $instrumentId = $this->_addPaymentInstrument();
1235 $contribParams = array(
1236 'contact_id' => $this->_individualId
,
1237 'total_amount' => -100.00,
1238 'financial_type_id' => $this->_financialTypeId
,
1239 'payment_instrument_id' => 4,
1240 'contribution_status_id' => 1,
1243 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1245 $newParams = array_merge($contribParams, array(
1246 'id' => $contribution['id'],
1247 'payment_instrument_id' => $instrumentId,
1250 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1251 $this->assertAPISuccess($contribution);
1252 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1254 // cleanup - delete created payment instrument
1255 $this->_deletedAddedPaymentInstrument();
1259 * Function tests that financial records are added when Contribution is Refunded.
1261 public function testCreateUpdateContributionRefund() {
1262 $contributionParams = array(
1263 'contact_id' => $this->_individualId
,
1264 'receive_date' => '2012-01-01',
1265 'total_amount' => 100.00,
1266 'financial_type_id' => $this->_financialTypeId
,
1267 'payment_instrument_id' => 4,
1268 'contribution_status_id' => 1,
1269 'trxn_id' => 'original_payment',
1271 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1272 $newParams = array_merge($contributionParams, array(
1273 'id' => $contribution['id'],
1274 'contribution_status_id' => 'Refunded',
1275 'cancel_date' => '2015-01-01 09:00',
1276 'refund_trxn_id' => 'the refund',
1280 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1281 $this->_checkFinancialTrxn($contribution, 'refund');
1282 $this->_checkFinancialItem($contribution['id'], 'refund');
1283 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1284 'id' => $contribution['id'],
1285 'return' => 'trxn_id',
1290 * Refund a contribution for a financial type with a contra account.
1292 * CRM-17951 the contra account is a financial account with a relationship to a
1293 * financial type. It is not always configured but should be reflected
1294 * in the financial_trxn & financial_item table if it is.
1296 public function testCreateUpdateChargebackContributionDefaultAccount() {
1297 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1298 $this->callAPISuccess('Contribution', 'create', array(
1299 'id' => $contribution['id'],
1300 'contribution_status_id' => 'Chargeback',
1302 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1304 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1305 'contribution_id' => $contribution['id'],
1306 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1308 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1309 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1310 'total_amount' => -100,
1311 'status_id' => 'Chargeback',
1312 'to_financial_account_id' => 6,
1317 * Refund a contribution for a financial type with a contra account.
1319 * CRM-17951 the contra account is a financial account with a relationship to a
1320 * financial type. It is not always configured but should be reflected
1321 * in the financial_trxn & financial_item table if it is.
1323 public function testCreateUpdateChargebackContributionCustomAccount() {
1324 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1325 'name' => 'Chargeback Account',
1326 'is_active' => TRUE,
1329 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1330 'entity_id' => $this->_financialTypeId
,
1331 'entity_table' => 'civicrm_financial_type',
1332 'account_relationship' => 'Chargeback Account is',
1333 'financial_account_id' => 'Chargeback Account',
1336 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1337 $this->callAPISuccess('Contribution', 'create', array(
1338 'id' => $contribution['id'],
1339 'contribution_status_id' => 'Chargeback',
1341 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1343 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1344 'contribution_id' => $contribution['id'],
1345 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1347 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1349 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1350 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1351 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1355 * Refund a contribution for a financial type with a contra account.
1357 * CRM-17951 the contra account is a financial account with a relationship to a
1358 * financial type. It is not always configured but should be reflected
1359 * in the financial_trxn & financial_item table if it is.
1361 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1362 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1363 'name' => 'Refund Account',
1364 'is_active' => TRUE,
1367 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1368 'entity_id' => $this->_financialTypeId
,
1369 'entity_table' => 'civicrm_financial_type',
1370 'account_relationship' => 'Credit/Contra Revenue Account is',
1371 'financial_account_id' => 'Refund Account',
1374 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1375 $this->callAPISuccess('Contribution', 'create', array(
1376 'id' => $contribution['id'],
1377 'contribution_status_id' => 'Refunded',
1380 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1381 'contribution_id' => $contribution['id'],
1382 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1384 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1386 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1387 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1388 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1392 * Function tests that trxn_id is set when passed in.
1394 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1395 * when trxn_id is passed in.
1397 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1398 $contributionParams = array(
1399 'contact_id' => $this->_individualId
,
1400 'receive_date' => '2012-01-01',
1401 'total_amount' => 100.00,
1402 'financial_type_id' => $this->_financialTypeId
,
1403 'payment_instrument_id' => 4,
1404 'contribution_status_id' => 1,
1405 'trxn_id' => 'original_payment',
1407 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1408 $newParams = array_merge($contributionParams, array(
1409 'id' => $contribution['id'],
1410 'contribution_status_id' => 'Refunded',
1411 'cancel_date' => '2015-01-01 09:00',
1412 'trxn_id' => 'the refund',
1416 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1417 $this->_checkFinancialTrxn($contribution, 'refund');
1418 $this->_checkFinancialItem($contribution['id'], 'refund');
1419 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1420 'id' => $contribution['id'],
1421 'return' => 'trxn_id',
1426 * Function tests that trxn_id is set when passed in.
1428 * Here we ensure that the civicrm_contribution.trxn_id is set
1429 * when trxn_id is passed in but if refund_trxn_id is different then that
1430 * is kept for the refund transaction.
1432 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1433 $contributionParams = array(
1434 'contact_id' => $this->_individualId
,
1435 'receive_date' => '2012-01-01',
1436 'total_amount' => 100.00,
1437 'financial_type_id' => $this->_financialTypeId
,
1438 'payment_instrument_id' => 4,
1439 'contribution_status_id' => 1,
1440 'trxn_id' => 'original_payment',
1442 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1443 $newParams = array_merge($contributionParams, array(
1444 'id' => $contribution['id'],
1445 'contribution_status_id' => 'Refunded',
1446 'cancel_date' => '2015-01-01 09:00',
1447 'trxn_id' => 'cont id',
1448 'refund_trxn_id' => 'the refund',
1452 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1453 $this->_checkFinancialTrxn($contribution, 'refund');
1454 $this->_checkFinancialItem($contribution['id'], 'refund');
1455 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1456 'id' => $contribution['id'],
1457 'return' => 'trxn_id',
1462 * Function tests that refund_trxn_id is set when passed in empty.
1464 * Here we ensure that the civicrm_contribution.trxn_id is set
1465 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1466 * is kept for the refund transaction.
1468 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1469 $contributionParams = array(
1470 'contact_id' => $this->_individualId
,
1471 'receive_date' => '2012-01-01',
1472 'total_amount' => 100.00,
1473 'financial_type_id' => $this->_financialTypeId
,
1474 'payment_instrument_id' => 4,
1475 'contribution_status_id' => 1,
1476 'trxn_id' => 'original_payment',
1478 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1479 $newParams = array_merge($contributionParams, array(
1480 'id' => $contribution['id'],
1481 'contribution_status_id' => 'Refunded',
1482 'cancel_date' => '2015-01-01 09:00',
1483 'trxn_id' => 'cont id',
1484 'refund_trxn_id' => '',
1488 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1489 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1490 $this->_checkFinancialItem($contribution['id'], 'refund');
1491 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1492 'id' => $contribution['id'],
1493 'return' => 'trxn_id',
1498 * Function tests invalid contribution status change.
1500 public function testCreateUpdateContributionInValidStatusChange() {
1501 $contribParams = array(
1503 'receive_date' => '2012-01-01',
1504 'total_amount' => 100.00,
1505 'financial_type_id' => 1,
1506 'payment_instrument_id' => 1,
1507 'contribution_status_id' => 1,
1509 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1510 $newParams = array_merge($contribParams, array(
1511 'id' => $contribution['id'],
1512 'contribution_status_id' => 2,
1515 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1520 * Function tests that financial records are added when Pending Contribution is Canceled.
1522 public function testCreateUpdateContributionCancelPending() {
1523 $contribParams = array(
1524 'contact_id' => $this->_individualId
,
1525 'receive_date' => '2012-01-01',
1526 'total_amount' => 100.00,
1527 'financial_type_id' => $this->_financialTypeId
,
1528 'payment_instrument_id' => 1,
1529 'contribution_status_id' => 2,
1530 'is_pay_later' => 1,
1533 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1534 $newParams = array_merge($contribParams, array(
1535 'id' => $contribution['id'],
1536 'contribution_status_id' => 3,
1537 'cancel_date' => '2012-02-02 09:00',
1540 //Check if trxn_date is same as cancel_date.
1541 $checkTrxnDate = array(
1542 'trxn_date' => '2012-02-02 09:00:00',
1544 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1545 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1546 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1550 * Function tests that financial records are added when Financial Type is Changed.
1552 public function testCreateUpdateContributionChangeFinancialType() {
1553 $contribParams = array(
1554 'contact_id' => $this->_individualId
,
1555 'receive_date' => '2012-01-01',
1556 'total_amount' => 100.00,
1557 'financial_type_id' => 1,
1558 'payment_instrument_id' => 1,
1559 'contribution_status_id' => 1,
1562 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1563 $newParams = array_merge($contribParams, array(
1564 'id' => $contribution['id'],
1565 'financial_type_id' => 3,
1568 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1569 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1570 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1574 * Test that update does not change status id CRM-15105.
1576 public function testCreateUpdateWithoutChangingPendingStatus() {
1577 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1578 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1579 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1580 'id' => $contribution['id'],
1581 'api.contribution.delete' => 1,
1583 $this->assertEquals(2, $contribution['contribution_status_id']);
1587 * Test Updating a Contribution.
1589 * CHANGE: we require the API to do an incremental update
1591 public function testCreateUpdateContribution() {
1592 $contributionID = $this->contributionCreate(array(
1593 'contact_id' => $this->_individualId
,
1594 'trxn_id' => 212355,
1595 'financial_type_id' => $this->_financialTypeId
,
1596 'invoice_id' => 'old_invoice',
1598 $old_params = array(
1599 'contribution_id' => $contributionID,
1601 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1602 $this->assertEquals($original['id'], $contributionID);
1603 //set up list of old params, verify
1605 //This should not be required on update:
1606 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1607 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1608 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1609 $old_source = $original['values'][$contributionID]['contribution_source'];
1611 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1612 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1614 //check against values in CiviUnitTestCase::createContribution()
1615 $this->assertEquals($old_contact_id, $this->_individualId
);
1616 $this->assertEquals($old_fee_amount, 5.00);
1617 $this->assertEquals($old_source, 'SSF');
1618 $this->assertEquals($old_trxn_id, 212355);
1619 $this->assertEquals($old_invoice_id, 'old_invoice');
1621 'id' => $contributionID,
1622 'contact_id' => $this->_individualId
,
1623 'total_amount' => 105.00,
1624 'fee_amount' => 7.00,
1625 'financial_type_id' => $this->_financialTypeId
,
1626 'non_deductible_amount' => 22.00,
1627 'contribution_status_id' => 1,
1628 'note' => 'Donating for Noble Cause',
1631 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1633 $new_params = array(
1634 'contribution_id' => $contribution['id'],
1637 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1639 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1640 $this->assertEquals($contribution['total_amount'], 105.00);
1641 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1642 $this->assertEquals($contribution['financial_type'], 'Donation');
1643 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1644 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1645 $this->assertEquals($contribution['fee_amount'], 7.00);
1646 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1647 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1648 $this->assertEquals($contribution['contribution_source'], $old_source);
1649 $this->assertEquals($contribution['contribution_status'], 'Completed');
1651 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1654 'contribution_id' => $contributionID,
1657 $result = $this->callAPISuccess('contribution', 'delete', $params);
1658 $this->assertAPISuccess($result);
1662 * Check that net_amount is updated when a contribution is updated.
1664 * Update fee amount AND total amount, just fee amount, just total amount
1665 * and neither to check that net_amount is keep updated.
1667 public function testUpdateContributionNetAmountVariants() {
1668 $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
1670 $this->callAPISuccess('Contribution', 'create', [
1671 'id' => $contributionID,
1672 'total_amount' => 90,
1675 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1676 'id' => $contributionID,
1677 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1679 $this->assertEquals(6, $contribution['fee_amount']);
1680 $this->assertEquals(90, $contribution['total_amount']);
1681 $this->assertEquals(84, $contribution['net_amount']);
1683 $this->callAPISuccess('Contribution', 'create', [
1684 'id' => $contributionID,
1687 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1688 'id' => $contributionID,
1689 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1691 $this->assertEquals(3, $contribution['fee_amount']);
1692 $this->assertEquals(90, $contribution['total_amount']);
1693 $this->assertEquals(87, $contribution['net_amount']);
1695 $this->callAPISuccess('Contribution', 'create', [
1696 'id' => $contributionID,
1697 'total_amount' => 200,
1699 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1700 'id' => $contributionID,
1701 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1703 $this->assertEquals(3, $contribution['fee_amount']);
1704 $this->assertEquals(200, $contribution['total_amount']);
1705 $this->assertEquals(197, $contribution['net_amount']);
1707 $this->callAPISuccess('Contribution', 'create', [
1708 'id' => $contributionID,
1709 'payment_instrument' => 'Cash'
1711 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1712 'id' => $contributionID,
1713 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1715 $this->assertEquals(3, $contribution['fee_amount']);
1716 $this->assertEquals(200, $contribution['total_amount']);
1717 $this->assertEquals(197, $contribution['net_amount']);
1721 * Attempt (but fail) to delete a contribution without parameters.
1723 public function testDeleteEmptyParamsContribution() {
1725 $this->callAPIFailure('contribution', 'delete', $params);
1728 public function testDeleteParamsNotArrayContribution() {
1729 $params = 'contribution_id= 1';
1730 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1731 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1734 public function testDeleteWrongParamContribution() {
1736 'contribution_source' => 'SSF',
1739 $this->callAPIFailure('contribution', 'delete', $params);
1742 public function testDeleteContribution() {
1743 $contributionID = $this->contributionCreate(array(
1744 'contact_id' => $this->_individualId
,
1745 'financial_type_id' => $this->_financialTypeId
,
1748 'id' => $contributionID,
1750 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1754 * Test civicrm_contribution_search with empty params.
1756 * All available contributions expected.
1758 public function testSearchEmptyParams() {
1762 'contact_id' => $this->_individualId
,
1763 'receive_date' => date('Ymd'),
1764 'total_amount' => 100.00,
1765 'financial_type_id' => $this->_financialTypeId
,
1766 'non_deductible_amount' => 10.00,
1767 'fee_amount' => 5.00,
1768 'net_amount' => 95.00,
1770 'invoice_id' => 78910,
1772 'contribution_status_id' => 1,
1775 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1777 $result = $this->callAPISuccess('contribution', 'get', $params);
1778 // We're taking the first element.
1779 $res = $result['values'][$contribution['id']];
1781 $this->assertEquals($p['contact_id'], $res['contact_id']);
1782 $this->assertEquals($p['total_amount'], $res['total_amount']);
1783 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1784 $this->assertEquals($p['net_amount'], $res['net_amount']);
1785 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1786 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1787 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1788 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1789 $this->assertEquals($p['source'], $res['contribution_source']);
1790 // contribution_status_id = 1 => Completed
1791 $this->assertEquals('Completed', $res['contribution_status']);
1793 $this->contributionDelete($contribution['id']);
1797 * Test civicrm_contribution_search. Success expected.
1799 public function testSearch() {
1801 'contact_id' => $this->_individualId
,
1802 'receive_date' => date('Ymd'),
1803 'total_amount' => 100.00,
1804 'financial_type_id' => $this->_financialTypeId
,
1805 'non_deductible_amount' => 10.00,
1806 'contribution_status_id' => 1,
1809 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1812 'contact_id' => $this->_individualId
,
1813 'receive_date' => date('Ymd'),
1814 'total_amount' => 200.00,
1815 'financial_type_id' => $this->_financialTypeId
,
1816 'non_deductible_amount' => 20.00,
1817 'trxn_id' => 5454565,
1818 'invoice_id' => 1212124,
1819 'fee_amount' => 50.00,
1820 'net_amount' => 60.00,
1821 'contribution_status_id' => 2,
1824 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1827 'contribution_id' => $contribution2['id'],
1830 $result = $this->callAPISuccess('contribution', 'get', $params);
1831 $res = $result['values'][$contribution2['id']];
1833 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1834 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1835 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1836 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1837 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1838 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1839 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1840 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1841 // contribution_status_id = 2 => Pending
1842 $this->assertEquals('Pending', $res['contribution_status']);
1844 $this->contributionDelete($contribution1['id']);
1845 $this->contributionDelete($contribution2['id']);
1849 * Test completing a transaction via the API.
1851 * Note that we are creating a logged in user because email goes out from
1854 public function testCompleteTransaction() {
1855 $mut = new CiviMailUtils($this, TRUE);
1856 $this->swapMessageTemplateForTestTemplate();
1857 $this->createLoggedInUser();
1858 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1859 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1860 $this->callAPISuccess('contribution', 'completetransaction', array(
1861 'id' => $contribution['id'],
1863 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1864 $this->assertEquals('SSF', $contribution['contribution_source']);
1865 $this->assertEquals('Completed', $contribution['contribution_status']);
1866 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1867 $mut->checkMailLog(array(
1868 'email:::anthony_anderson@civicrm.org',
1872 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1873 "receipt_date:::\n",
1874 'contributeMode:::notify',
1875 'title:::Contribution',
1876 'displayName:::Mr. Anthony Anderson II',
1877 'contributionStatus:::Completed',
1880 $this->revertTemplateToReservedTemplate();
1884 * Test completing a transaction via the API with a non-USD transaction.
1886 public function testCompleteTransactionEuro() {
1887 $mut = new CiviMailUtils($this, TRUE);
1888 $this->swapMessageTemplateForTestTemplate();
1889 $this->createLoggedInUser();
1890 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1891 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1893 $this->callAPISuccess('contribution', 'completetransaction', array(
1894 'id' => $contribution['id'],
1897 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1898 $this->assertEquals('SSF', $contribution['contribution_source']);
1899 $this->assertEquals('Completed', $contribution['contribution_status']);
1900 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1902 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1903 $entityFinancialTransaction = reset($entityFinancialTransactions);
1904 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1905 $this->assertEquals('EUR', $financialTrxn['currency']);
1907 $mut->checkMailLog(array(
1908 'email:::anthony_anderson@civicrm.org',
1912 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1913 "receipt_date:::\n",
1914 'contributeMode:::notify',
1915 'title:::Contribution',
1916 'displayName:::Mr. Anthony Anderson II',
1917 'contributionStatus:::Completed',
1920 $this->revertTemplateToReservedTemplate();
1924 * Test to ensure mail is sent on chosing pay later
1926 public function testpayLater() {
1927 $mut = new CiviMailUtils($this, TRUE);
1928 $this->swapMessageTemplateForTestTemplate();
1929 $this->createLoggedInUser();
1931 // create contribution page first
1932 $contributionPageParams = array(
1933 'title' => 'Help Support CiviCRM!',
1934 'financial_type_id' => 1,
1935 'is_monetary' => TRUE,
1936 'is_pay_later' => 1,
1937 'is_quick_config' => TRUE,
1938 'pay_later_text' => 'I will send payment by check',
1939 'pay_later_receipt' => 'This is a pay later reciept',
1940 'is_allow_other_amount' => 1,
1941 'min_amount' => 10.00,
1942 'max_amount' => 10000.00,
1943 'goal_amount' => 100000.00,
1944 'is_email_receipt' => 1,
1946 'amount_block_is_active' => 1,
1947 'currency' => 'USD',
1948 'is_billing_required' => 0,
1950 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1952 // submit form values
1953 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1955 'id' => $contributionPageResult['id'],
1956 'contact_id' => $this->_individualId
,
1957 'email-5' => 'anthony_anderson@civicrm.org',
1958 'payment_processor_id' => 0,
1961 'currencyID' => 'USD',
1962 'is_pay_later' => 1,
1963 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1964 'is_quick_config' => 1,
1965 'description' => 'Online Contribution: Help Support CiviCRM!',
1966 'price_set_id' => $priceSet['id'],
1968 $this->callAPISuccess('contribution_page', 'submit', $params);
1970 $mut->checkMailLog(array(
1972 'email:::anthony_anderson@civicrm.org',
1973 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1974 'displayName:::Mr. Anthony Anderson II',
1975 'contributionPageId:::' . $contributionPageResult['id'],
1976 'title:::' . $contributionPageParams['title'],
1977 'amount:::' . $params['amount'],
1980 $this->revertTemplateToReservedTemplate();
1984 * Test to check whether contact billing address is used when no contribution address
1986 public function testBillingAddress() {
1987 $mut = new CiviMailUtils($this, TRUE);
1988 $this->swapMessageTemplateForTestTemplate();
1989 $this->createLoggedInUser();
1991 //Scenario 1: When Contact don't have any address
1992 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1993 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1994 $this->callAPISuccess('contribution', 'completetransaction', array(
1995 'id' => $contribution['id'],
1997 $mut->checkMailLog(array(
2001 // Scenario 2: Contribution using address
2002 $address = $this->callAPISuccess('address', 'create', array(
2003 'street_address' => 'contribution billing st',
2004 'location_type_id' => 2,
2005 'contact_id' => $this->_params
['contact_id'],
2007 $params = array_merge($this->_params
, array(
2008 'contribution_status_id' => 2,
2009 'address_id' => $address['id'],
2012 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2013 $this->callAPISuccess('contribution', 'completetransaction', array(
2014 'id' => $contribution['id'],
2016 $mut->checkMailLog(array(
2017 'address:::contribution billing st',
2020 // Scenario 3: Contribution wtth no address but contact has a billing address
2021 $this->callAPISuccess('address', 'create', array(
2022 'id' => $address['id'],
2023 'street_address' => 'is billing st',
2024 'contact_id' => $this->_params
['contact_id'],
2026 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2027 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2028 $this->callAPISuccess('contribution', 'completetransaction', array(
2029 'id' => $contribution['id'],
2031 $mut->checkMailLog(array(
2032 'address:::is billing st',
2036 $this->revertTemplateToReservedTemplate();
2040 * Test completing a transaction via the API.
2042 * Note that we are creating a logged in user because email goes out from
2045 public function testCompleteTransactionFeeAmount() {
2046 $this->createLoggedInUser();
2047 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2048 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2049 $this->callAPISuccess('contribution', 'completetransaction', array(
2050 'id' => $contribution['id'],
2051 'fee_amount' => '.56',
2052 'trxn_id' => '7778888',
2054 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
2055 $this->assertEquals('Completed', $contribution['contribution_status']);
2056 $this->assertEquals('7778888', $contribution['trxn_id']);
2057 $this->assertEquals('.56', $contribution['fee_amount']);
2058 $this->assertEquals('99.44', $contribution['net_amount']);
2062 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
2064 * @param string $thousandSeparator
2065 * punctuation used to refer to thousands.
2067 * @dataProvider getThousandSeparators
2069 public function testCheckTaxAmount($thousandSeparator) {
2070 $this->setCurrencySeparators($thousandSeparator);
2071 $contact = $this->createLoggedInUser();
2072 $financialType = $this->callAPISuccess('financial_type', 'create', array(
2073 'name' => 'Test taxable financial Type',
2077 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
2078 'name' => 'Test Tax financial account ',
2079 'contact_id' => $contact,
2080 'financial_account_type_id' => 2,
2087 $financialTypeId = $financialType['id'];
2088 $financialAccountId = $financialAccount['id'];
2089 $financialAccountParams = array(
2090 'entity_table' => 'civicrm_financial_type',
2091 'entity_id' => $financialTypeId,
2092 'account_relationship' => 10,
2093 'financial_account_id' => $financialAccountId,
2095 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
2097 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
2098 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2099 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
2100 $this->callAPISuccess('contribution', 'completetransaction', array(
2101 'id' => $contribution['id'],
2102 'trxn_id' => '777788888',
2103 'fee_amount' => '6.00',
2105 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
2106 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
2107 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
2108 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
2112 * Test repeat contribution successfully creates line item.
2114 public function testRepeatTransaction() {
2115 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2116 $this->callAPISuccess('contribution', 'repeattransaction', array(
2117 'original_contribution_id' => $originalContribution['id'],
2118 'contribution_status_id' => 'Completed',
2119 'trxn_id' => uniqid(),
2121 $lineItemParams = array(
2122 'entity_id' => $originalContribution['id'],
2130 'financial_type_id',
2131 'deductible_amount',
2132 'price_field_value_id',
2136 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2137 'entity_id' => $originalContribution['id'],
2139 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2140 'entity_id' => $originalContribution['id'] +
1,
2142 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2143 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2144 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2145 $this->_checkFinancialRecords(array(
2146 'id' => $originalContribution['id'] +
1,
2147 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2148 'id' => $originalContribution['payment_processor_id'],
2149 'return' => 'payment_instrument_id',
2152 $this->quickCleanUpFinancialEntities();
2156 * Test repeat contribution successfully creates line items (plural).
2158 public function testRepeatTransactionLineItems() {
2160 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2161 $this->callAPISuccess('contribution', 'repeattransaction', array(
2162 'original_contribution_id' => $originalContribution['id'],
2163 'contribution_status_id' => 'Completed',
2164 'trxn_id' => uniqid(),
2167 $lineItemParams = array(
2168 'entity_id' => $originalContribution['id'],
2176 'financial_type_id',
2177 'deductible_amount',
2178 'price_field_value_id',
2182 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2183 'entity_id' => $originalContribution['id'],
2185 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2186 'entity_id' => $originalContribution['id'] +
1,
2189 // unset id and entity_id for all of them to be able to compare the lineItems:
2190 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2191 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2192 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2194 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2195 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2196 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2198 // CRM-19309 so in future we also want to:
2199 // check that financial_line_items have been created for entity_id 3 and 4;
2201 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2202 $this->quickCleanUpFinancialEntities();
2206 * Test repeat contribution successfully creates is_test transaction.
2208 public function testRepeatTransactionIsTest() {
2209 $this->_params
['is_test'] = 1;
2210 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2212 $this->callAPISuccess('contribution', 'repeattransaction', array(
2213 'original_contribution_id' => $originalContribution['id'],
2214 'contribution_status_id' => 'Completed',
2215 'trxn_id' => uniqid(),
2217 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2221 * Test repeat contribution passed in status.
2223 public function testRepeatTransactionPassedInStatus() {
2224 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2226 $this->callAPISuccess('contribution', 'repeattransaction', array(
2227 'original_contribution_id' => $originalContribution['id'],
2228 'contribution_status_id' => 'Pending',
2229 'trxn_id' => uniqid(),
2231 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2235 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2237 public function testRepeatTransactionAcceptRecurID() {
2238 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2239 'contact_id' => $this->_individualId
,
2240 'installments' => '12',
2241 'frequency_interval' => '1',
2243 'contribution_status_id' => 1,
2244 'start_date' => '2012-01-01 00:00:00',
2245 'currency' => 'USD',
2246 'frequency_unit' => 'month',
2247 'payment_processor_id' => $this->paymentProcessorID
,
2249 $this->callAPISuccess('contribution', 'create', array_merge(
2251 array('contribution_recur_id' => $contributionRecur['id']))
2254 $this->callAPISuccess('contribution', 'repeattransaction', array(
2255 'contribution_recur_id' => $contributionRecur['id'],
2256 'contribution_status_id' => 'Completed',
2257 'trxn_id' => uniqid(),
2260 $this->quickCleanUpFinancialEntities();
2264 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2266 public function testRepeatTransactionTestRecurId() {
2267 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2268 'contact_id' => $this->_individualId
,
2269 'frequency_interval' => '1',
2271 'contribution_status_id' => 1,
2272 'start_date' => '2017-01-01 00:00:00',
2273 'currency' => 'USD',
2274 'frequency_unit' => 'month',
2275 'payment_processor_id' => $this->paymentProcessorID
,
2278 $this->callAPISuccess('contribution', 'create', array_merge(
2281 'contribution_recur_id' => $contributionRecur['id'],
2286 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2287 'contribution_recur_id' => $contributionRecur['id'],
2288 'contribution_status_id' => 'Completed',
2289 'trxn_id' => uniqid(),
2292 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2293 $this->quickCleanUpFinancialEntities();
2297 * CRM-19945 Tests that Contribute.repeattransaction renews a membership when contribution status=Completed
2300 public function testRepeatTransactionMembershipRenewCompletedContribution() {
2301 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2303 $this->callAPISuccess('contribution', 'create', array(
2304 'contact_id' => $originalContribution['values'][1]['contact_id'],
2305 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2306 'total_amount' => $originalContribution['values'][1]['total_amount'],
2307 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2308 'contribution_status_id' => "Failed",
2311 $this->callAPISuccess('membership', 'create', array(
2312 'id' => $membership['id'],
2313 'end_date' => 'yesterday',
2314 'status_id' => 'Expired',
2317 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2318 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2319 'contribution_status_id' => 'Completed',
2320 'trxn_id' => 'bobsled',
2323 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2324 'id' => $membership['id'],
2325 'return' => 'status_id',
2328 $membership = $this->callAPISuccess('membership', 'get', array(
2329 'id' => $membership['id'],
2332 $this->assertEquals('New', CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2334 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2335 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2336 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2337 $this->quickCleanUpFinancialEntities();
2338 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2342 * CRM-19945 Tests that Contribute.repeattransaction DOES NOT renew a membership when contribution status=Failed
2344 * @dataProvider contributionStatusProvider
2346 public function testRepeatTransactionMembershipRenewContributionNotCompleted($contributionStatus) {
2347 // Completed status should renew so we don't test that here
2348 // In Progress status is only for recurring contributions so we don't test that here
2349 if (in_array($contributionStatus['name'], ['Completed', 'In Progress'])) {
2352 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2354 $this->callAPISuccess('contribution', 'create', array(
2355 'contact_id' => $originalContribution['values'][1]['contact_id'],
2356 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2357 'total_amount' => $originalContribution['values'][1]['total_amount'],
2358 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2359 'contribution_status_id' => "Completed",
2362 $this->callAPISuccess('membership', 'create', array(
2363 'id' => $membership['id'],
2364 'end_date' => 'yesterday',
2365 'status_id' => 'Expired',
2368 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2369 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2370 'contribution_status_id' => $contributionStatus['name'],
2371 'trxn_id' => 'bobsled',
2374 $updatedMembership = $this->callAPISuccess('membership', 'getsingle', array(
2375 'id' => $membership['id'],
2378 $dateTime = new DateTime('yesterday');
2379 $this->assertEquals($dateTime->format('Y-m-d'), $updatedMembership['end_date']);
2380 $this->assertEquals(CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Expired'), $updatedMembership['status_id']);
2382 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2383 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2384 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2385 $this->quickCleanUpFinancialEntities();
2386 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2390 * Dataprovider provides contribution status as [optionvalue=>contribution_status_name]
2391 * FIXME: buildOptions seems to die in CRM_Core_Config::_construct when in test mode.
2394 * @throws \CiviCRM_API3_Exception
2396 public function contributionStatusProvider() {
2397 $contributionStatuses = civicrm_api3('OptionValue', 'get', [
2398 'return' => ["id", "name"],
2399 'option_group_id' => "contribution_status",
2401 foreach ($contributionStatuses['values'] as $statusName) {
2402 $statuses[] = [$statusName];
2408 * CRM-16397 test appropriate action if total amount has changed for single line items.
2410 public function testRepeatTransactionAlteredAmount() {
2411 $paymentProcessorID = $this->paymentProcessorCreate();
2412 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2413 'contact_id' => $this->_individualId
,
2414 'installments' => '12',
2415 'frequency_interval' => '1',
2417 'contribution_status_id' => 1,
2418 'start_date' => '2012-01-01 00:00:00',
2419 'currency' => 'USD',
2420 'frequency_unit' => 'month',
2421 'payment_processor_id' => $paymentProcessorID,
2423 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2426 'contribution_recur_id' => $contributionRecur['id'],
2430 $this->callAPISuccess('contribution', 'repeattransaction', array(
2431 'original_contribution_id' => $originalContribution['id'],
2432 'contribution_status_id' => 'Completed',
2433 'trxn_id' => uniqid(),
2434 'total_amount' => '400',
2438 $lineItemParams = array(
2439 'entity_id' => $originalContribution['id'],
2447 'financial_type_id',
2448 'deductible_amount',
2449 'price_field_value_id',
2453 $this->callAPISuccessGetSingle('contribution', array(
2454 'total_amount' => 400,
2456 'net_amount' => 350,
2458 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2459 'entity_id' => $originalContribution['id'],
2461 $expectedLineItem = array_merge(
2462 $lineItem1['values'][0], array(
2463 'line_total' => '400.00',
2464 'unit_price' => '400.00',
2468 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2469 'entity_id' => $originalContribution['id'] +
1,
2472 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2473 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2474 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2478 * CRM-17718 test appropriate action if financial type has changed for single line items.
2480 public function testRepeatTransactionPassedInFinancialType() {
2481 $originalContribution = $this->setUpRecurringContribution();
2483 $this->callAPISuccess('contribution', 'repeattransaction', array(
2484 'original_contribution_id' => $originalContribution['id'],
2485 'contribution_status_id' => 'Completed',
2486 'trxn_id' => uniqid(),
2487 'financial_type_id' => 2,
2489 $lineItemParams = array(
2490 'entity_id' => $originalContribution['id'],
2498 'financial_type_id',
2499 'deductible_amount',
2500 'price_field_value_id',
2505 $this->callAPISuccessGetSingle('contribution', array(
2506 'total_amount' => 100,
2507 'financial_type_id' => 2,
2509 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2510 'entity_id' => $originalContribution['id'],
2512 $expectedLineItem = array_merge(
2513 $lineItem1['values'][0], array(
2514 'line_total' => '100.00',
2515 'unit_price' => '100.00',
2516 'financial_type_id' => 2,
2517 'contribution_type_id' => 2,
2520 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2521 'entity_id' => $originalContribution['id'] +
1,
2523 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2524 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2525 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2529 * CRM-17718 test appropriate action if financial type has changed for single line items.
2531 public function testRepeatTransactionUpdatedFinancialType() {
2532 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2534 $this->callAPISuccess('contribution', 'repeattransaction', array(
2535 'contribution_recur_id' => $originalContribution['id'],
2536 'contribution_status_id' => 'Completed',
2537 'trxn_id' => uniqid(),
2539 $lineItemParams = array(
2540 'entity_id' => $originalContribution['id'],
2548 'financial_type_id',
2549 'deductible_amount',
2550 'price_field_value_id',
2555 $this->callAPISuccessGetSingle('contribution', array(
2556 'total_amount' => 100,
2557 'financial_type_id' => 2,
2559 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2560 'entity_id' => $originalContribution['id'],
2562 $expectedLineItem = array_merge(
2563 $lineItem1['values'][0], array(
2564 'line_total' => '100.00',
2565 'unit_price' => '100.00',
2566 'financial_type_id' => 2,
2567 'contribution_type_id' => 2,
2571 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2572 'entity_id' => $originalContribution['id'] +
1,
2574 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2575 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2576 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2580 * CRM-16397 test appropriate action if campaign has been passed in.
2582 public function testRepeatTransactionPassedInCampaign() {
2583 $paymentProcessorID = $this->paymentProcessorCreate();
2584 $campaignID = $this->campaignCreate();
2585 $campaignID2 = $this->campaignCreate();
2586 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2587 'contact_id' => $this->_individualId
,
2588 'installments' => '12',
2589 'frequency_interval' => '1',
2591 'contribution_status_id' => 1,
2592 'start_date' => '2012-01-01 00:00:00',
2593 'currency' => 'USD',
2594 'frequency_unit' => 'month',
2595 'payment_processor_id' => $paymentProcessorID,
2597 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2600 'contribution_recur_id' => $contributionRecur['id'],
2601 'campaign_id' => $campaignID,
2605 $this->callAPISuccess('contribution', 'repeattransaction', array(
2606 'original_contribution_id' => $originalContribution['id'],
2607 'contribution_status_id' => 'Completed',
2608 'trxn_id' => uniqid(),
2609 'campaign_id' => $campaignID2,
2612 $this->callAPISuccessGetSingle('contribution', array(
2613 'total_amount' => 100,
2614 'campaign_id' => $campaignID2,
2619 * CRM-17718 campaign stored on contribution recur gets priority.
2621 * This reflects the fact we permit people to update them.
2623 public function testRepeatTransactionUpdatedCampaign() {
2624 $paymentProcessorID = $this->paymentProcessorCreate();
2625 $campaignID = $this->campaignCreate();
2626 $campaignID2 = $this->campaignCreate();
2627 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2628 'contact_id' => $this->_individualId
,
2629 'installments' => '12',
2630 'frequency_interval' => '1',
2632 'contribution_status_id' => 1,
2633 'start_date' => '2012-01-01 00:00:00',
2634 'currency' => 'USD',
2635 'frequency_unit' => 'month',
2636 'payment_processor_id' => $paymentProcessorID,
2637 'campaign_id' => $campaignID,
2639 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2642 'contribution_recur_id' => $contributionRecur['id'],
2643 'campaign_id' => $campaignID2,
2647 $this->callAPISuccess('contribution', 'repeattransaction', array(
2648 'original_contribution_id' => $originalContribution['id'],
2649 'contribution_status_id' => 'Completed',
2650 'trxn_id' => uniqid(),
2653 $this->callAPISuccessGetSingle('contribution', array(
2654 'total_amount' => 100,
2655 'campaign_id' => $campaignID,
2660 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2662 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2664 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
2665 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2666 // This will made the trick to get the not equals behaviour.
2667 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 4));
2668 $this->callAPISuccess('contribution', 'repeattransaction', array(
2669 'contribution_recur_id' => $originalContribution['id'],
2670 'contribution_status_id' => 'Completed',
2671 'trxn_id' => uniqid(),
2673 $lineItemParams = array(
2674 'entity_id' => $originalContribution['id'],
2682 'financial_type_id',
2683 'deductible_amount',
2684 'price_field_value_id',
2688 $this->callAPISuccessGetSingle('contribution', array(
2689 'total_amount' => 100,
2690 'financial_type_id' => 2,
2692 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2693 'entity_id' => $originalContribution['id'],
2695 $expectedLineItem = array_merge(
2696 $lineItem1['values'][0], array(
2697 'line_total' => '100.00',
2698 'unit_price' => '100.00',
2699 'financial_type_id' => 4,
2700 'contribution_type_id' => 4,
2704 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2705 'entity_id' => $originalContribution['id'] +
1,
2707 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 1));
2708 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2709 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2710 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2715 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2717 public function testCompleteTransactionNetAmountOK() {
2718 $this->createLoggedInUser();
2719 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2720 unset($params['net_amount']);
2721 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2722 $this->callAPISuccess('contribution', 'completetransaction', array(
2723 'id' => $contribution['id'],
2725 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2726 $this->assertEquals('Completed', $contribution['contribution_status']);
2727 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2731 * CRM-14151 - Test completing a transaction via the API.
2733 public function testCompleteTransactionWithReceiptDateSet() {
2734 $this->swapMessageTemplateForTestTemplate();
2735 $mut = new CiviMailUtils($this, TRUE);
2736 $this->createLoggedInUser();
2737 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2738 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2739 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2740 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2741 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2742 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2743 $mut->checkMailLog(array(
2744 'Receipt - Contribution',
2745 'receipt_date:::' . date('Ymd'),
2748 $this->revertTemplateToReservedTemplate();
2752 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2754 public function testCompleteTransactionWithEmailReceiptInput() {
2755 $contributionPage = $this->createReceiptableContributionPage();
2757 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2758 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2759 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2760 // Complete the transaction overriding is_email_receipt to = FALSE
2761 $this->callAPISuccess('contribution', 'completetransaction', array(
2762 'id' => $contribution['id'],
2763 'trxn_date' => date('2011-04-09'),
2764 'trxn_id' => 'kazam',
2765 'is_email_receipt' => 0,
2767 // Check if a receipt was issued
2768 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2769 $this->assertEquals('', $receipt_date);
2773 * Test that $is_recur is assigned to the receipt.
2775 public function testCompleteTransactionForRecurring() {
2777 $this->swapMessageTemplateForTestTemplate();
2778 $recurring = $this->setUpRecurringContribution();
2779 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2781 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2782 $this->_params
['contribution_recur_id'] = $recurring['id'];
2784 $contribution = $this->setUpForCompleteTransaction();
2786 $this->callAPISuccess('contribution', 'completetransaction', array(
2787 'id' => $contribution['id'],
2788 'trxn_date' => date('2011-04-09'),
2789 'trxn_id' => 'kazam',
2790 'is_email_receipt' => 1,
2793 $this->mut
->checkMailLog(array(
2795 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2798 $this->revertTemplateToReservedTemplate();
2801 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2803 * If passed in it will override the default from contribution page.
2805 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2806 $mut = new CiviMailUtils($this, TRUE);
2807 $this->createLoggedInUser();
2808 // Create a Contribution Page with is_email_receipt = FALSE
2809 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2810 'receipt_from_name' => 'Mickey Mouse',
2811 'receipt_from_email' => 'mickey@mouse.com',
2812 'title' => "Test Contribution Page",
2813 'financial_type_id' => 1,
2814 'currency' => 'CAD',
2815 'is_monetary' => TRUE,
2816 'is_email_receipt' => 0,
2818 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2819 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2820 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2821 // Complete the transaction overriding is_email_receipt to = TRUE
2822 $this->callAPISuccess('contribution', 'completetransaction', array(
2823 'id' => $contribution['id'],
2824 'is_email_receipt' => 1,
2826 $mut->checkMailLog(array(
2827 'Please print this receipt for your records.',
2833 * Complete the transaction using the template with all the possible.
2835 public function testCompleteTransactionWithTestTemplate() {
2836 $this->swapMessageTemplateForTestTemplate();
2837 $contribution = $this->setUpForCompleteTransaction();
2838 $this->callAPISuccess('contribution', 'completetransaction', array(
2839 'id' => $contribution['id'],
2840 'trxn_date' => date('2011-04-09'),
2841 'trxn_id' => 'kazam',
2843 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2844 $this->mut
->checkMailLog(array(
2845 'email:::anthony_anderson@civicrm.org',
2849 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2850 'receipt_date:::' . date('Ymd'),
2851 'contributeMode:::notify',
2852 'title:::Contribution',
2853 'displayName:::Mr. Anthony Anderson II',
2855 'contactID:::' . $this->_params
['contact_id'],
2856 'contributionID:::' . $contribution['id'],
2857 'financialTypeId:::1',
2858 'financialTypeName:::Donation',
2861 $this->revertTemplateToReservedTemplate();
2865 * Complete the transaction using the template with all the possible.
2867 public function testCompleteTransactionContributionPageFromAddress() {
2868 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2869 'receipt_from_name' => 'Mickey Mouse',
2870 'receipt_from_email' => 'mickey@mouse.com',
2871 'title' => "Test Contribution Page",
2872 'financial_type_id' => 1,
2873 'currency' => 'NZD',
2874 'goal_amount' => 50,
2875 'is_pay_later' => 1,
2876 'is_monetary' => TRUE,
2877 'is_email_receipt' => TRUE,
2879 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2880 $contribution = $this->setUpForCompleteTransaction();
2881 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2882 $this->mut
->checkMailLog(array(
2890 * Test completing first transaction in a recurring series.
2892 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2894 * @dataProvider getScheduledDateData
2896 * @param array $dataSet
2898 * @throws \Exception
2900 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2901 $paymentProcessorID = $this->paymentProcessorCreate();
2902 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2903 'contact_id' => $this->_individualId
,
2904 'installments' => '2',
2905 'frequency_interval' => '1',
2907 'contribution_status_id' => 'Pending',
2908 'start_date' => '2012-01-01 00:00:00',
2909 'currency' => 'USD',
2910 'frequency_unit' => 'month',
2911 'payment_processor_id' => $paymentProcessorID,
2912 ), $dataSet['data']));
2913 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2916 'contribution_recur_id' => $contributionRecur['id'],
2917 'contribution_status_id' => 'Pending',
2918 'receive_date' => $dataSet['receive_date'],
2921 $this->callAPISuccess('Contribution', 'completetransaction', array(
2922 'id' => $contribution,
2923 'receive_date' => $dataSet['receive_date'],
2925 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2926 'id' => $contributionRecur['id'],
2927 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2929 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2930 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2931 $this->callAPISuccess('Contribution', 'create', array_merge(
2934 'contribution_recur_id' => $contributionRecur['id'],
2935 'contribution_status_id' => 'Completed',
2938 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2939 'id' => $contributionRecur['id'],
2940 'return' => array('contribution_status_id'),
2942 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2946 * Get dates for testing.
2950 public function getScheduledDateData() {
2952 $result[]['2016-08-31-1-month'] = array(
2954 'start_date' => '2016-08-31',
2955 'frequency_interval' => 1,
2956 'frequency_unit' => 'month',
2958 'receive_date' => '2016-08-31',
2959 'expected' => '2016-10-01 00:00:00',
2961 $result[]['2012-01-01-1-month'] = array(
2963 'start_date' => '2012-01-01',
2964 'frequency_interval' => 1,
2965 'frequency_unit' => 'month',
2967 'receive_date' => '2012-01-01',
2968 'expected' => '2012-02-01 00:00:00',
2970 $result[]['2012-01-01-1-month'] = array(
2972 'start_date' => '2012-01-01',
2973 'frequency_interval' => 1,
2974 'frequency_unit' => 'month',
2976 'receive_date' => '2012-02-29',
2977 'expected' => '2012-03-29 00:00:00',
2979 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2981 'start_date' => '2012-01-01',
2982 'frequency_interval' => 1,
2983 'frequency_unit' => 'month',
2984 'next_sched_contribution_date' => '2012-02-29',
2986 'receive_date' => '2012-02-29 16:00:00',
2987 'expected' => '2012-03-29 00:00:00',
2993 * Test completing a pledge with the completeTransaction api..
2995 * Note that we are creating a logged in user because email goes out from
2998 public function testCompleteTransactionUpdatePledgePayment() {
2999 $this->swapMessageTemplateForTestTemplate();
3000 $mut = new CiviMailUtils($this, TRUE);
3001 $mut->clearMessages();
3002 $this->createLoggedInUser();
3003 $contributionID = $this->createPendingPledgeContribution();
3004 $this->callAPISuccess('contribution', 'completetransaction', array(
3005 'id' => $contributionID,
3006 'trxn_date' => '1 Feb 2013',
3008 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
3009 'id' => $this->_ids
['pledge'],
3011 $this->assertEquals('Completed', $pledge['pledge_status']);
3013 $status = $this->callAPISuccessGetValue('PledgePayment', array(
3014 'pledge_id' => $this->_ids
['pledge'],
3015 'return' => 'status_id',
3017 $this->assertEquals(1, $status);
3018 $mut->checkMailLog(array(
3020 'receive_date:::20130201000000',
3021 "receipt_date:::\n",
3024 $this->revertTemplateToReservedTemplate();
3028 * Test completing a transaction with an event via the API.
3030 * Note that we are creating a logged in user because email goes out from
3033 public function testCompleteTransactionWithParticipantRecord() {
3034 $mut = new CiviMailUtils($this, TRUE);
3035 $mut->clearMessages();
3036 $this->_individualId
= $this->createLoggedInUser();
3037 $contributionID = $this->createPendingParticipantContribution();
3038 $this->callAPISuccess('contribution', 'completetransaction', array(
3039 'id' => $contributionID,
3042 $participantStatus = $this->callAPISuccessGetValue('participant', array(
3043 'id' => $this->_ids
['participant'],
3044 'return' => 'participant_status_id',
3046 $this->assertEquals(1, $participantStatus);
3048 //Assert only three activities are created.
3049 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_individualId
);
3050 $this->assertEquals(3, count($activities));
3051 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
3052 // record two activities before and after completing payment for Event registration
3053 $this->assertEquals(2, $activityNames['Event Registration']);
3054 // update the original 'Contribution' activity created after completing payment
3055 $this->assertEquals(1, $activityNames['Contribution']);
3057 $mut->checkMailLog(array(
3058 'Annual CiviCRM meet',
3060 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
3066 * Test membership is renewed when transaction completed.
3068 public function testCompleteTransactionMembershipPriceSet() {
3069 $this->createPriceSetWithPage('membership');
3070 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
3074 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
3075 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3076 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
3077 'membership_id' => $this->_ids
['membership'],
3079 $this->assertEquals(1, $logs['count']);
3080 $this->assertEquals($stateOfGrace, $membership['status_id']);
3081 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
3082 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3083 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
3084 $this->callAPISuccessGetSingle('LineItem', array(
3085 'entity_id' => $this->_ids
['membership'],
3086 'entity_table' => 'civicrm_membership',
3088 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
3089 'membership_id' => $this->_ids
['membership'],
3091 $this->assertEquals(2, $logs['count']);
3092 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
3093 //Assert only three activities are created.
3094 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
3095 $this->assertEquals(3, count($activities));
3096 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
3097 $this->assertArrayHasKey('Contribution', $activityNames);
3098 $this->assertArrayHasKey('Membership Signup', $activityNames);
3099 $this->assertArrayHasKey('Change Membership Status', $activityNames);
3100 $this->cleanUpAfterPriceSets();
3104 * Test if renewal activity is create after changing Pending contribution to Completed via offline
3106 public function testPendingToCompleteContribution() {
3107 $this->createPriceSetWithPage('membership');
3108 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
3109 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3110 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
3111 $activity = $this->callAPISuccess('Activity', 'get', array(
3112 'activity_type_id' => 'Membership Signup',
3113 'source_record_id' => $this->_ids
['membership'],
3114 'status_id' => 'Scheduled',
3116 $this->assertEquals(1, $activity['count']);
3118 // change pending contribution to completed
3119 $form = new CRM_Contribute_Form_Contribution();
3121 $form->_params
= array(
3122 'id' => $this->_ids
['contribution'],
3123 'total_amount' => 20,
3126 'financial_type_id' => 1,
3127 'contact_id' => $this->_individualId
,
3128 'contribution_status_id' => 1,
3129 'billing_middle_name' => '',
3130 'billing_last_name' => 'Adams',
3131 'billing_street_address-5' => '790L Lincoln St S',
3132 'billing_city-5' => 'Maryknoll',
3133 'billing_state_province_id-5' => 1031,
3134 'billing_postal_code-5' => 10545,
3135 'billing_country_id-5' => 1228,
3136 'frequency_interval' => 1,
3137 'frequency_unit' => 'month',
3138 'installments' => '',
3139 'hidden_AdditionalDetail' => 1,
3140 'hidden_Premium' => 1,
3141 'from_email_address' => '"civi45" <civi45@civicrm.com>',
3142 'receipt_date' => '',
3143 'receipt_date_time' => '',
3144 'payment_processor_id' => $this->paymentProcessorID
,
3145 'currency' => 'USD',
3146 'contribution_page_id' => $this->_ids
['contribution_page'],
3147 'contribution_mode' => 'membership',
3148 'source' => 'Membership Signup and Renewal',
3151 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3153 // Case 2: After successful payment for Pending backoffice there are three activities created
3154 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
3155 $activity = $this->callAPISuccess('Activity', 'get', array(
3156 'activity_type_id' => 'Membership Signup',
3157 'source_record_id' => $this->_ids
['membership'],
3158 'status_id' => 'Completed',
3160 $this->assertEquals(1, $activity['count']);
3161 // 2.b Contribution activity created to record successful payment
3162 $activity = $this->callAPISuccess('Activity', 'get', array(
3163 'activity_type_id' => 'Contribution',
3164 'source_record_id' => $this->_ids
['contribution'],
3165 'status_id' => 'Completed',
3167 $this->assertEquals(1, $activity['count']);
3169 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
3170 $activity = $this->callAPISuccess('Activity', 'get', array(
3171 'activity_type_id' => 'Change Membership Status',
3172 'source_record_id' => $this->_ids
['membership'],
3173 'status_id' => 'Completed',
3175 $this->assertEquals(1, $activity['count']);
3176 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
3178 //Create another pending contribution for renewal
3179 $contribution = $this->callAPISuccess('contribution', 'create', array(
3181 'contact_id' => $this->_ids
['contact'],
3182 'receive_date' => date('Ymd'),
3183 'total_amount' => 20.00,
3184 'financial_type_id' => 1,
3185 'payment_instrument_id' => 'Credit Card',
3186 'non_deductible_amount' => 10.00,
3187 'trxn_id' => 'rdhfi88',
3188 'invoice_id' => 'dofhiewuyr',
3190 'contribution_status_id' => 2,
3191 'contribution_page_id' => $this->_ids
['contribution_page'],
3192 // We can't rely on contribution api to link line items correctly to membership
3193 'skipLineItem' => TRUE,
3194 'api.membership_payment.create' => array('membership_id' => $this->_ids
['membership']),
3197 $this->callAPISuccess('line_item', 'create', array(
3198 'entity_id' => $contribution['id'],
3199 'entity_table' => 'civicrm_contribution',
3200 'contribution_id' => $contribution['id'],
3201 'price_field_id' => $this->_ids
['price_field'][0],
3205 'financial_type_id' => 1,
3206 'price_field_value_id' => $this->_ids
['price_field_value']['cont'],
3208 $this->callAPISuccess('line_item', 'create', array(
3209 'entity_id' => $this->_ids
['membership'],
3210 'entity_table' => 'civicrm_membership',
3211 'contribution_id' => $contribution['id'],
3212 'price_field_id' => $this->_ids
['price_field'][0],
3216 'financial_type_id' => 1,
3217 'price_field_value_id' => $this->_ids
['price_field_value'][0],
3218 'membership_type_id' => $this->_ids
['membership_type'],
3221 //Update it to Failed.
3222 $form->_params
['id'] = $contribution['id'];
3223 $form->_params
['contribution_status_id'] = 4;
3225 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3226 //Existing membership should not get updated to expired.
3227 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3228 $this->assertNotEquals($membership['status_id'], 4);
3232 * Test membership is renewed when transaction completed.
3234 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3235 $this->createPriceSetWithPage('membership');
3236 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
3237 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
3238 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3239 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3240 $this->cleanUpAfterPriceSets();
3243 public function cleanUpAfterPriceSets() {
3244 $this->quickCleanUpFinancialEntities();
3245 $this->contactDelete($this->_ids
['contact']);
3249 * Set up a pending transaction with a specific price field id.
3251 * @param int $priceFieldValueID
3253 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
3254 $contactID = $this->individualCreate();
3255 $membership = $this->callAPISuccess('membership', 'create', array(
3256 'contact_id' => $contactID,
3257 'membership_type_id' => $this->_ids
['membership_type'],
3258 'start_date' => 'yesterday - 1 year',
3259 'end_date' => 'yesterday',
3260 'join_date' => 'yesterday - 1 year',
3262 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3264 'contact_id' => $contactID,
3265 'receive_date' => date('Ymd'),
3266 'total_amount' => 20.00,
3267 'financial_type_id' => 1,
3268 'payment_instrument_id' => 'Credit Card',
3269 'non_deductible_amount' => 10.00,
3270 'trxn_id' => 'jdhfi' . rand(1, 100),
3271 'invoice_id' => 'djfhiew' . rand(5, 100),
3273 'contribution_status_id' => 2,
3274 'contribution_page_id' => $this->_ids
['contribution_page'],
3275 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3278 $this->callAPISuccess('line_item', 'create', array(
3279 'entity_id' => $contribution['id'],
3280 'entity_table' => 'civicrm_contribution',
3281 'contribution_id' => $contribution['id'],
3282 'price_field_id' => $this->_ids
['price_field'][0],
3286 'financial_type_id' => 1,
3287 'price_field_value_id' => $priceFieldValueID,
3289 $this->_ids
['contact'] = $contactID;
3290 $this->_ids
['contribution'] = $contribution['id'];
3291 $this->_ids
['membership'] = $membership['id'];
3295 * Test sending a mail via the API.
3297 public function testSendMail() {
3298 $mut = new CiviMailUtils($this, TRUE);
3299 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3300 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3301 'id' => $contribution['id'],
3302 'receipt_from_email' => 'api@civicrm.org',
3305 $mut->checkMailLog(array(
3307 'Contribution Information',
3308 'Please print this confirmation for your records',
3314 $this->checkCreditCardDetails($mut, $contribution['id']);
3319 * Test sending a mail via the API.
3320 * This simulates webform_civicrm using pay later contribution page
3322 public function testSendconfirmationPayLater() {
3323 $mut = new CiviMailUtils($this, TRUE);
3325 // Create contribution page
3326 $pageParams = array(
3327 'title' => 'Webform Contributions',
3328 'financial_type_id' => 1,
3329 'contribution_type_id' => 1,
3330 'is_confirm_enabled' => 1,
3331 'is_pay_later' => 1,
3332 'pay_later_text' => 'I will send payment by cheque',
3333 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3335 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3337 // Create pay later contribution
3338 $contribParams = array(
3339 'contact_id' => $this->_individualId
,
3340 'financial_type_id' => 1,
3341 'is_pay_later' => 1,
3342 'contribution_status_id' => 2,
3343 'contribution_page_id' => $contributionPage['id'],
3344 'total_amount' => '10.00',
3346 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3349 $lineItemParams = array(
3350 'contribution_id' => $contribution['id'],
3351 'entity_id' => $contribution['id'],
3352 'entity_table' => 'civicrm_contribution',
3353 'label' => 'My lineitem label',
3355 'unit_price' => "10.00",
3356 'line_total' => "10.00",
3358 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3362 civicrm_api3('contribution', 'sendconfirmation', array(
3363 'id' => $contribution['id'],
3364 'receipt_from_email' => 'api@civicrm.org',
3368 catch (Exception
$e) {
3369 // Need to figure out how to stop this some other day
3370 // We don't care about the Payment Processor because this is Pay Later
3371 // The point of this test is to check we get the pay_later version of the mail
3372 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3377 // Retrieve mail & check it has the pay_later_receipt info
3378 $mut->getMostRecentEmail('raw');
3379 $mut->checkMailLog(array(
3380 (string) $contribParams['total_amount'],
3381 $pageParams['pay_later_receipt'],
3391 * Check credit card details in sent mail via API
3393 * @param $mut obj CiviMailUtils instance
3394 * @param int $contributionID Contribution ID
3397 public function checkCreditCardDetails($mut, $contributionID) {
3398 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3399 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3400 'id' => $contributionID,
3401 'receipt_from_email' => 'api@civicrm.org',
3402 'payment_processor_id' => $this->paymentProcessorID
,
3405 $mut->checkMailLog(array(
3406 'Credit Card Information', // credit card header
3407 'Billing Name and Address', // billing header
3408 'anthony_anderson@civicrm.org', // billing name
3416 * Test sending a mail via the API.
3418 public function testSendMailEvent() {
3419 $mut = new CiviMailUtils($this, TRUE);
3420 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3421 $event = $this->eventCreate(array(
3422 'is_email_confirm' => 1,
3423 'confirm_from_email' => 'test@civicrm.org',
3425 $this->_eventID
= $event['id'];
3426 $participantParams = array(
3427 'contact_id' => $this->_individualId
,
3428 'event_id' => $this->_eventID
,
3431 // to ensure it matches later on
3432 'register_date' => '2007-07-21 00:00:00',
3433 'source' => 'Online Event Registration: API Testing',
3436 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3437 $this->callAPISuccess('participant_payment', 'create', array(
3438 'participant_id' => $participant['id'],
3439 'contribution_id' => $contribution['id'],
3441 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3442 'id' => $contribution['id'],
3443 'receipt_from_email' => 'api@civicrm.org',
3447 $mut->checkMailLog(array(
3448 'Annual CiviCRM meet',
3450 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3457 * This function does a GET & compares the result against the $params.
3459 * Use as a double check on Creates.
3461 * @param array $params
3463 * @param bool $delete
3465 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3467 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3473 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3475 $this->assertAPISuccess($contribution, 0);
3476 $values = $contribution['values'][$contribution['id']];
3477 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3478 // this is not returned in id format
3479 unset($params['payment_instrument_id']);
3480 $params['contribution_source'] = $params['source'];
3481 unset($params['source']);
3482 foreach ($params as $key => $value) {
3483 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3488 * Create a pending contribution & linked pending pledge record.
3490 public function createPendingPledgeContribution() {
3492 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
3493 $this->_ids
['pledge'] = $pledgeID;
3494 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
3495 'contribution_status_id' => 'Pending',
3496 'total_amount' => 500,
3499 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3500 'options' => array('limit' => 1),
3503 $this->callAPISuccess('PledgePayment', 'create', array(
3505 'contribution_id' =>
3506 $contribution['id'],
3507 'status_id' => 'Pending',
3508 'scheduled_amount' => 500,
3511 return $contribution['id'];
3515 * Create a pending contribution & linked pending participant record (along with an event).
3517 public function createPendingParticipantContribution() {
3518 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3519 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId
));
3520 $this->_ids
['participant'] = $participantID;
3521 $params = array_merge($this->_params
, array('contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3522 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3523 $this->callAPISuccess('participant_payment', 'create', array(
3524 'contribution_id' => $contribution['id'],
3525 'participant_id' => $participantID,
3527 $this->callAPISuccess('line_item', 'get', array(
3528 'entity_id' => $contribution['id'],
3529 'entity_table' => 'civicrm_contribution',
3530 'api.line_item.create' => array(
3531 'entity_id' => $participantID,
3532 'entity_table' => 'civicrm_participant',
3535 return $contribution['id'];
3539 * Get financial transaction amount.
3541 * @param int $contId
3543 * @return null|string
3545 public function _getFinancialTrxnAmount($contId) {
3547 SUM( ft.total_amount ) AS total
3548 FROM civicrm_financial_trxn AS ft
3549 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3550 WHERE ceft.entity_table = 'civicrm_contribution'
3551 AND ceft.entity_id = {$contId}";
3553 $result = CRM_Core_DAO
::singleValueQuery($query);
3558 * @param int $contId
3560 * @return null|string
3562 public function _getFinancialItemAmount($contId) {
3563 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3566 FROM civicrm_financial_item
3567 WHERE entity_table = 'civicrm_line_item'
3568 AND entity_id = {$lineItem}";
3569 $result = CRM_Core_DAO
::singleValueQuery($query);
3574 * @param int $contId
3577 public function _checkFinancialItem($contId, $context) {
3578 if ($context != 'paylater') {
3580 'entity_id' => $contId,
3581 'entity_table' => 'civicrm_contribution',
3583 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3584 $entityParams = array(
3585 'financial_trxn_id' => $trxn['financial_trxn_id'],
3586 'entity_table' => 'civicrm_financial_item',
3588 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3590 'id' => $entityTrxn['entity_id'],
3593 if ($context == 'paylater') {
3594 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3595 foreach ($lineItems as $key => $item) {
3597 'entity_id' => $key,
3598 'entity_table' => 'civicrm_line_item',
3600 $compareParams = array('status_id' => 1);
3601 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3604 elseif ($context == 'refund') {
3605 $compareParams = array(
3607 'financial_account_id' => 1,
3611 elseif ($context == 'cancelPending') {
3612 $compareParams = array(
3614 'financial_account_id' => 1,
3618 elseif ($context == 'changeFinancial') {
3619 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3621 'entity_id' => $lineKey,
3624 $compareParams = array(
3625 'financial_account_id' => 1,
3627 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3629 'financial_account_id' => 3,
3630 'entity_id' => $lineKey,
3632 $compareParams = array(
3636 if ($context != 'paylater') {
3637 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3642 * Check correct financial transaction entries were created for the change in payment instrument.
3644 * @param int $contributionID
3645 * @param int $originalInstrumentID
3646 * @param int $newInstrumentID
3648 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3650 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3652 $originalTrxnParams = array(
3653 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3654 'payment_instrument_id' => $originalInstrumentID,
3655 'amount' => $amount,
3659 $reversalTrxnParams = array(
3660 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3661 'payment_instrument_id' => $originalInstrumentID,
3662 'amount' => -$amount,
3666 $newTrxnParams = array(
3667 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3668 'payment_instrument_id' => $newInstrumentID,
3669 'amount' => $amount,
3673 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3674 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3675 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3677 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3678 'id' => $entityFinancialTrxn['financial_trxn_id'],
3680 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3681 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3682 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3683 $this->assertEquals(0, $financialTrxn['fee_amount']);
3684 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3685 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3688 $this->assertEquals(1, $financialTrxn['is_payment']);
3689 $this->assertEquals('USD', $financialTrxn['currency']);
3690 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3695 * Check financial transaction.
3697 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3699 * @param array $contribution
3700 * @param string $context
3701 * @param int $instrumentId
3702 * @param array $extraParams
3704 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3705 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3706 $trxn = array_pop($financialTrxns);
3709 'id' => $trxn['financial_trxn_id'],
3711 if ($context == 'payLater') {
3712 $compareParams = array(
3714 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3717 elseif ($context == 'refund') {
3718 $compareParams = array(
3719 'to_financial_account_id' => 6,
3720 'total_amount' => -100,
3722 'trxn_date' => '2015-01-01 09:00:00',
3723 'trxn_id' => 'the refund',
3726 elseif ($context == 'cancelPending') {
3727 $compareParams = array(
3728 'to_financial_account_id' => 7,
3729 'total_amount' => -100,
3733 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3734 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3735 // It does the same thing with greater readability.
3736 // @todo remove handling for
3738 $entityParams = array(
3739 'entity_id' => $contribution['id'],
3740 'entity_table' => 'civicrm_contribution',
3743 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3744 $trxnParams1 = array(
3745 'id' => $trxn['financial_trxn_id'],
3747 if (empty($extraParams)) {
3748 $compareParams = array(
3749 'total_amount' => -100,
3754 $compareParams = array(
3755 'total_amount' => 100,
3759 if ($context == 'paymentInstrument') {
3760 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3761 $compareParams['payment_instrument_id'] = $instrumentId;
3764 $compareParams['to_financial_account_id'] = 12;
3766 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3767 $compareParams['total_amount'] = 100;
3770 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3776 public function _addPaymentInstrument() {
3777 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3778 $optionParams = array(
3779 'option_group_id' => $gId,
3780 'label' => 'Test Card',
3781 'name' => 'Test Card',
3786 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3787 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3788 $financialParams = array(
3789 'entity_table' => 'civicrm_option_value',
3790 'entity_id' => $optionValue['id'],
3791 'account_relationship' => $relationTypeId,
3792 'financial_account_id' => 7,
3794 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3795 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3796 return $optionValue['values'][$optionValue['id']]['value'];
3799 public function _deletedAddedPaymentInstrument() {
3800 $result = $this->callAPISuccess('OptionValue', 'get', array(
3801 'option_group_id' => 'payment_instrument',
3802 'name' => 'Test Card',
3806 if ($id = CRM_Utils_Array
::value('id', $result)) {
3807 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3812 * Set up the basic recurring contribution for tests.
3814 * @param array $generalParams
3815 * Parameters that can be merged into the recurring AND the contribution.
3817 * @param array $recurParams
3818 * Parameters to merge into the recur only.
3822 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3823 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3824 'contact_id' => $this->_individualId
,
3825 'installments' => '12',
3826 'frequency_interval' => '1',
3828 'contribution_status_id' => 1,
3829 'start_date' => '2012-01-01 00:00:00',
3830 'currency' => 'USD',
3831 'frequency_unit' => 'month',
3832 'payment_processor_id' => $this->paymentProcessorID
,
3833 ), $generalParams, $recurParams));
3834 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3837 'contribution_recur_id' => $contributionRecur['id'],
3840 return $originalContribution;
3844 * Set up a basic auto-renew membership for tests.
3846 * @param array $generalParams
3847 * Parameters that can be merged into the recurring AND the contribution.
3849 * @param array $recurParams
3850 * Parameters to merge into the recur only.
3854 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3855 $newContact = $this->callAPISuccess('Contact', 'create', array(
3856 'contact_type' => 'Individual',
3857 'sort_name' => 'McTesterson, Testy',
3858 'display_name' => 'Testy McTesterson',
3859 'preferred_language' => 'en_US',
3860 'preferred_mail_format' => 'Both',
3861 'first_name' => 'Testy',
3862 'last_name' => 'McTesterson',
3863 'contact_is_deleted' => '0',
3865 'email' => 'tmctesterson@example.com',
3868 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3869 'domain_id' => "Default Domain Name",
3870 'member_of_contact_id' => 1,
3871 'financial_type_id' => "Member Dues",
3872 'duration_unit' => "month",
3873 'duration_interval' => 1,
3874 'period_type' => 'rolling',
3875 'name' => "Standard Member",
3876 'minimum_fee' => 100,
3878 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3879 'contact_id' => $newContact['id'],
3880 'installments' => '12',
3881 'frequency_interval' => '1',
3883 'contribution_status_id' => 1,
3884 'start_date' => '2012-01-01 00:00:00',
3885 'currency' => 'USD',
3886 'frequency_unit' => 'month',
3887 'payment_processor_id' => $this->paymentProcessorID
,
3888 ), $generalParams, $recurParams));
3890 $membership = $this->callAPISuccess('membership', 'create', array(
3891 'contact_id' => $newContact['id'],
3892 'contribution_recur_id' => $contributionRecur['id'],
3893 'financial_type_id' => "Member Dues",
3894 'membership_type_id' => $membershipType['id'],
3896 'skipLineItem' => TRUE,
3899 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3900 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3903 'contact_id' => $newContact['id'],
3904 'contribution_recur_id' => $contributionRecur['id'],
3905 'financial_type_id' => "Member Dues",
3906 'contribution_status_id' => 1,
3907 'invoice_id' => uniqid(),
3910 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3911 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3912 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3913 $this->callAPISuccess('LineItem', 'getsingle', array());
3914 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3916 return array($originalContribution, $membership);
3919 * Set up a repeat transaction.
3921 * @param array $recurParams
3925 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3926 $paymentProcessorID = $this->paymentProcessorCreate();
3927 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3928 'contact_id' => $this->_individualId
,
3929 'installments' => '12',
3930 'frequency_interval' => '1',
3932 'contribution_status_id' => 1,
3933 'start_date' => '2012-01-01 00:00:00',
3934 'currency' => 'USD',
3935 'frequency_unit' => 'month',
3936 'payment_processor_id' => $paymentProcessorID,
3939 $originalContribution = '';
3940 if ($flag == 'multiple') {
3941 // CRM-19309 create a contribution + also add in line_items (plural):
3942 $params = array_merge($this->_params
, $contributionParams);
3943 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3946 'contribution_recur_id' => $contributionRecur['id'],
3947 'skipLineItem' => 1,
3948 'api.line_item.create' => array(
3950 'price_field_id' => 1,
3952 'line_total' => '20',
3953 'unit_price' => '10',
3954 'financial_type_id' => 1,
3957 'price_field_id' => 1,
3959 'line_total' => '80',
3960 'unit_price' => '80',
3961 'financial_type_id' => 2,
3968 elseif ($flag == 'single') {
3969 $params = array_merge($this->_params
, array('contribution_recur_id' => $contributionRecur['id']));
3970 $params = array_merge($params, $contributionParams);
3971 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3973 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3974 return $originalContribution;
3978 * Common set up routine.
3982 protected function setUpForCompleteTransaction() {
3983 $this->mut
= new CiviMailUtils($this, TRUE);
3984 $this->createLoggedInUser();
3985 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3986 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3987 return $contribution;
3991 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3993 public function testRepeatTransactionWithNonCreditCardDefault() {
3994 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3995 'contact_id' => $this->_individualId
,
3996 'installments' => '12',
3997 'frequency_interval' => '1',
3999 'contribution_status_id' => 1,
4000 'start_date' => '2012-01-01 00:00:00',
4001 'currency' => 'USD',
4002 'frequency_unit' => 'month',
4003 'payment_processor_id' => $this->paymentProcessorID
,
4005 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
4007 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
4009 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
4010 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
4011 'contribution_status_id' => 'Completed',
4012 'trxn_id' => uniqid(),
4013 'original_contribution_id' => $contribution1,
4015 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
4016 $this->quickCleanUpFinancialEntities();
4020 * CRM-20008 Tests repeattransaction creates pending membership.
4022 public function testRepeatTransactionMembershipCreatePendingContribution() {
4023 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
4024 $this->callAPISuccess('membership', 'create', array(
4025 'id' => $membership['id'],
4026 'end_date' => 'yesterday',
4027 'status_id' => 'Expired',
4029 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4030 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
4031 'contribution_status_id' => 'Pending',
4032 'trxn_id' => uniqid(),
4034 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
4035 'id' => $membership['id'],
4036 'return' => 'status_id',
4039 // Let's see if the membership payments got created while we're at it.
4040 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
4041 'membership_id' => $membership['id'],
4043 $this->assertEquals(2, $membershipPayments['count']);
4045 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
4046 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
4047 $membership = $this->callAPISuccessGetSingle('membership', array(
4048 'id' => $membership['id'],
4049 'return' => 'status_id, end_date',
4051 $this->assertEquals('New', CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
4052 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
4054 $this->quickCleanUpFinancialEntities();
4055 $this->contactDelete($originalContribution['values'][1]['contact_id']);
4059 * Test sending a mail via the API.
4061 public function testSendMailWithAPISetFromDetails() {
4062 $mut = new CiviMailUtils($this, TRUE);
4063 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
4064 $this->callAPISuccess('contribution', 'sendconfirmation', array(
4065 'id' => $contribution['id'],
4066 'receipt_from_email' => 'api@civicrm.org',
4067 'receipt_from_name' => 'CiviCRM LLC',
4069 $mut->checkMailLog(array(
4070 'From: CiviCRM LLC <api@civicrm.org>',
4071 'Contribution Information',
4072 'Please print this confirmation for your records',
4081 * Test sending a mail via the API.
4083 public function testSendMailWithNoFromSetFallToDomain() {
4084 $this->createLoggedInUser();
4085 $mut = new CiviMailUtils($this, TRUE);
4086 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
4087 $this->callAPISuccess('contribution', 'sendconfirmation', array(
4088 'id' => $contribution['id'],
4090 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
4091 $mut->checkMailLog(array(
4092 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4093 'Contribution Information',
4094 'Please print this confirmation for your records',
4103 * Test sending a mail via the API.
4105 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
4106 $this->createLoggedInUser();
4107 $mut = new CiviMailUtils($this, TRUE);
4108 $contribution = $this->setUpRepeatTransaction(array(), 'single');
4109 $this->callAPISuccess('contribution', 'repeattransaction', array(
4110 'contribution_status_id' => 'Completed',
4111 'trxn_id' => uniqid(),
4112 'original_contribution_id' => $contribution,
4114 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
4115 $mut->checkMailLog(array(
4116 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4117 'Contribution Information',
4118 'Please print this confirmation for your records',
4127 * Test sending a mail via the API.
4129 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
4130 $mut = new CiviMailUtils($this, TRUE);
4131 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
4132 $paymentProcessorID = $this->paymentProcessorCreate();
4133 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
4134 'contact_id' => $this->_individualId
,
4135 'installments' => '12',
4136 'frequency_interval' => '1',
4138 'contribution_status_id' => 1,
4139 'start_date' => '2012-01-01 00:00:00',
4140 'currency' => 'USD',
4141 'frequency_unit' => 'month',
4142 'payment_processor_id' => $paymentProcessorID,
4144 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
4147 'contribution_recur_id' => $contributionRecur['id'],
4148 'contribution_page_id' => $contributionPage['id']))
4150 $this->callAPISuccess('contribution', 'repeattransaction', array(
4151 'contribution_status_id' => 'Completed',
4152 'trxn_id' => uniqid(),
4153 'original_contribution_id' => $originalContribution,
4156 $mut->checkMailLog(array(
4157 'From: CiviCRM LLC <contributionpage@civicrm.org>',
4158 'Contribution Information',
4159 'Please print this confirmation for your records',
4168 * Test sending a mail via the API.
4170 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4171 $mut = new CiviMailUtils($this, TRUE);
4172 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
4173 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
4174 foreach ($fromEmail['values'] as $from) {
4175 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
4177 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
4178 $this->callAPISuccess('contribution', 'repeattransaction', array(
4179 'contribution_status_id' => 'Completed',
4180 'trxn_id' => uniqid(),
4181 'original_contribution_id' => $originalContribution,
4184 $mut->checkMailLog(array(
4185 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4186 'Contribution Information',
4187 'Please print this confirmation for your records',
4196 * Create a Contribution Page with is_email_receipt = TRUE.
4198 * @param array $params
4199 * Params to overwrite with.
4203 protected function createReceiptableContributionPage($params = array()) {
4204 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
4205 'receipt_from_name' => 'Mickey Mouse',
4206 'receipt_from_email' => 'mickey@mouse.com',
4207 'title' => "Test Contribution Page",
4208 'financial_type_id' => 1,
4209 'currency' => 'CAD',
4210 'is_monetary' => TRUE,
4211 'is_email_receipt' => TRUE,
4213 return $contributionPage;
4217 * function to test card_type and pan truncation.
4219 public function testCardTypeAndPanTruncation() {
4220 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
4221 $contactId = $this->individualCreate();
4223 'contact_id' => $contactId,
4224 'receive_date' => '2016-01-20',
4225 'total_amount' => 100,
4226 'financial_type_id' => 1,
4227 'payment_instrument' => 'Credit Card',
4228 'card_type_id' => $creditCardTypeIDs['Visa'],
4229 'pan_truncation' => 4567,
4231 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4232 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
4233 $financialTrxn = $this->callAPISuccessGetSingle(
4236 'id' => $lastFinancialTrxnId['financialTrxnId'],
4237 'return' => array('card_type_id', 'pan_truncation'),
4240 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4241 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
4243 'id' => $contribution['id'],
4244 'pan_truncation' => 2345,
4245 'card_type_id' => $creditCardTypeIDs['Amex'],
4247 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4248 $financialTrxn = $this->callAPISuccessGetSingle(
4251 'id' => $lastFinancialTrxnId['financialTrxnId'],
4252 'return' => array('card_type_id', 'pan_truncation'),
4255 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4256 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
4260 * Test repeat contribution uses non default currency
4261 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4263 public function testRepeatTransactionWithDifferenceCurrency() {
4264 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4265 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4266 'original_contribution_id' => $originalContribution['id'],
4267 'contribution_status_id' => 'Completed',
4268 'trxn_id' => uniqid(),
4270 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4274 * Get the financial items for the contribution.
4276 * @param int $contributionID
4279 * Array of associated financial items.
4281 protected function getFinancialTransactionsForContribution($contributionID) {
4282 $trxnParams = array(
4283 'entity_id' => $contributionID,
4284 'entity_table' => 'civicrm_contribution',
4286 // @todo the following function has naming errors & has a weird signature & appears to
4287 // only be called from test classes. Move into test suite & maybe just use api
4288 // from this function.
4289 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));
4293 * Test getunique api call for Contribution entity
4295 public function testContributionGetUnique() {
4296 $result = $this->callAPIAndDocument($this->_entity
, 'getunique', array(), __FUNCTION__
, __FILE__
);
4297 $this->assertEquals(2, $result['count']);
4298 $this->assertEquals(array('trxn_id'), $result['values']['UI_contrib_trxn_id']);
4299 $this->assertEquals(array('invoice_id'), $result['values']['UI_contrib_invoice_id']);
4303 * Test Repeat Transaction Contribution with Tax amount.
4304 * https://lab.civicrm.org/dev/core/issues/806
4306 public function testRepeatContributionWithTaxAmount() {
4307 $this->enableTaxAndInvoicing();
4308 $financialType = $this->callAPISuccess('financial_type', 'create', [
4309 'name' => 'Test taxable financial Type',
4313 $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
4314 $contribution = $this->setUpRepeatTransaction(
4318 'financial_type_id' => $financialType['id'],
4321 $this->callAPISuccess('contribution', 'repeattransaction', array(
4322 'original_contribution_id' => $contribution['id'],
4323 'contribution_status_id' => 'Completed',
4324 'trxn_id' => uniqid(),
4326 $this->callAPISuccessGetCount('Contribution', [], 2);