3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 $contributionID = $this->contributionCreate($params);
273 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
274 $this->assertEquals('bouncer', $contribution['check_number']);
275 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
277 $fields = CRM_Contribute_BAO_Contribution
::fields();
278 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
279 // need to specify them.
280 $fields['address_id'] = $fields['contribution_address_id'];
281 $fields['check_number'] = $fields['contribution_check_number'];
283 $fieldsLockedIn = array(
284 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
285 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
286 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
287 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
288 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
289 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
291 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
292 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
293 // covered as the test contract now guarantees them in the return array.
294 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
295 foreach ($fields as $fieldName => $fieldSpec) {
296 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
297 $returnField = $fieldName;
298 if ($returnField == 'contribution_contact_id') {
299 $returnField = 'contact_id';
301 $this->assertTrue((!empty($contribution[$returnField]) ||
$contribution[$returnField] === "0"), $returnField);
306 * We need to ensure previous tested behaviour still works as part of the api contract.
308 public function testGetContributionLegacyBehaviour() {
310 'contact_id' => $this->_individualId
,
311 'receive_date' => '2010-01-20',
312 'total_amount' => 100.00,
313 'contribution_type_id' => $this->_financialTypeId
,
314 'non_deductible_amount' => 10.00,
315 'fee_amount' => 5.00,
316 'net_amount' => 95.00,
318 'invoice_id' => 78910,
320 'contribution_status_id' => 1,
322 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
325 'contribution_id' => $this->_contribution
['id'],
327 $contribution = $this->callAPISuccess('contribution', 'get', $params);
328 $financialParams['id'] = $this->_financialTypeId
;
330 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
332 $this->assertEquals(1, $contribution['count']);
333 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
334 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
335 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
336 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
337 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
338 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
339 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
340 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
341 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
342 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
343 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
345 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
346 $p['trxn_id'] = '3847';
347 $p['invoice_id'] = '3847';
349 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
351 // now we have 2 - test getcount
352 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
353 $this->assertEquals(2, $contribution);
354 //test id only format
355 $contribution = $this->callAPISuccess('contribution', 'get', array(
356 'id' => $this->_contribution
['id'],
357 'format.only_id' => 1,
359 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
360 //test id only format
361 $contribution = $this->callAPISuccess('contribution', 'get', array(
362 'id' => $contribution2['id'],
363 'format.only_id' => 1,
365 $this->assertEquals($contribution2['id'], $contribution);
366 $contribution = $this->callAPISuccess('contribution', 'get', array(
367 'id' => $this->_contribution
['id'],
370 $this->assertEquals(1, $contribution['count']);
371 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
372 //test get by contact id works
373 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
375 $this->assertEquals(2, $contribution['count']);
376 $this->callAPISuccess('Contribution', 'Delete', array(
377 'id' => $this->_contribution
['id'],
379 $this->callAPISuccess('Contribution', 'Delete', array(
380 'id' => $contribution2['id'],
385 * Create an contribution_id=FALSE and financial_type_id=Donation.
387 public function testCreateEmptyContributionIDUseDonation() {
389 'contribution_id' => FALSE,
392 'check_permissions' => FALSE,
393 'financial_type_id' => 'Donation',
395 $this->callAPISuccess('contribution', 'create', $params);
399 * Check with complete array + custom field.
401 * Note that the test is written on purpose without any
402 * variables specific to participant so it can be replicated into other entities
403 * and / or moved to the automated test suite
405 public function testCreateWithCustom() {
406 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
408 $params = $this->_params
;
409 $params['custom_' . $ids['custom_field_id']] = "custom string";
411 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
412 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
413 $check = $this->callAPISuccess($this->_entity
, 'get', array(
414 'return.custom_' . $ids['custom_field_id'] => 1,
415 'id' => $result['id'],
417 $this->customFieldDelete($ids['custom_field_id']);
418 $this->customGroupDelete($ids['custom_group_id']);
419 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
423 * Check with complete array + custom field.
425 * Note that the test is written on purpose without any
426 * variables specific to participant so it can be replicated into other entities
427 * and / or moved to the automated test suite
429 public function testCreateGetFieldsWithCustom() {
430 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
431 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
432 $result = $this->callAPISuccess('Contribution', 'getfields', array());
433 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
434 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
435 $this->customFieldDelete($ids['custom_field_id']);
436 $this->customGroupDelete($ids['custom_group_id']);
437 $this->customFieldDelete($idsContact['custom_field_id']);
438 $this->customGroupDelete($idsContact['custom_group_id']);
441 public function testCreateContributionNoLineItems() {
444 'contact_id' => $this->_individualId
,
445 'receive_date' => '20120511',
446 'total_amount' => 100.00,
447 'financial_type_id' => $this->_financialTypeId
,
448 'payment_instrument_id' => 1,
449 'non_deductible_amount' => 10.00,
450 'fee_amount' => 50.00,
451 'net_amount' => 90.00,
453 'invoice_id' => 67890,
455 'contribution_status_id' => 1,
459 $contribution = $this->callAPISuccess('contribution', 'create', $params);
460 $lineItems = $this->callAPISuccess('line_item', 'get', array(
461 'entity_id' => $contribution['id'],
462 'entity_table' => 'civicrm_contribution',
465 $this->assertEquals(0, $lineItems['count']);
469 * Test checks that passing in line items suppresses the create mechanism.
471 public function testCreateContributionChainedLineItems() {
473 'contact_id' => $this->_individualId
,
474 'receive_date' => '20120511',
475 'total_amount' => 100.00,
476 'financial_type_id' => $this->_financialTypeId
,
477 'payment_instrument_id' => 1,
478 'non_deductible_amount' => 10.00,
479 'fee_amount' => 50.00,
480 'net_amount' => 90.00,
482 'invoice_id' => 67890,
484 'contribution_status_id' => 1,
486 'api.line_item.create' => array(
488 'price_field_id' => 1,
490 'line_total' => '20',
491 'unit_price' => '10',
494 'price_field_id' => 1,
496 'line_total' => '80',
497 'unit_price' => '80',
502 $description = "Create Contribution with Nested Line Items.";
503 $subfile = "CreateWithNestedLineItems";
504 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
506 $lineItems = $this->callAPISuccess('line_item', 'get', array(
507 'entity_id' => $contribution['id'],
508 'contribution_id' => $contribution['id'],
509 'entity_table' => 'civicrm_contribution',
512 $this->assertEquals(2, $lineItems['count']);
515 public function testCreateContributionOffline() {
517 'contact_id' => $this->_individualId
,
518 'receive_date' => '20120511',
519 'total_amount' => 100.00,
520 'financial_type_id' => 1,
522 'invoice_id' => 67890,
524 'contribution_status_id' => 1,
527 $contribution = $this->callAPISuccess('contribution', 'create', $params);
528 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
529 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
530 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
531 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
532 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
533 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
534 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
535 $lineItems = $this->callAPISuccess('line_item', 'get', array(
536 'entity_id' => $contribution['id'],
537 'contribution_id' => $contribution['id'],
538 'entity_table' => 'civicrm_contribution',
541 $this->assertEquals(1, $lineItems['count']);
542 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
543 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
544 $this->_checkFinancialRecords($contribution, 'offline');
545 $this->contributionGetnCheck($params, $contribution['id']);
549 * Test create with valid payment instrument.
551 public function testCreateContributionWithPaymentInstrument() {
552 $params = $this->_params +
array('payment_instrument' => 'EFT');
553 $contribution = $this->callAPISuccess('contribution', 'create', $params);
554 $contribution = $this->callAPISuccess('contribution', 'get', array(
556 'id' => $contribution['id'],
558 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
559 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
561 $this->callAPISuccess('contribution', 'create', array(
562 'id' => $contribution['id'],
563 'payment_instrument' => 'Credit Card',
565 $contribution = $this->callAPISuccess('contribution', 'get', array(
567 'id' => $contribution['id'],
569 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
570 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
573 public function testGetContributionByPaymentInstrument() {
574 $params = $this->_params +
array('payment_instrument' => 'EFT');
575 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
576 $this->callAPISuccess('contribution', 'create', $params);
577 $this->callAPISuccess('contribution', 'create', $params2);
578 $contribution = $this->callAPISuccess('contribution', 'get', array(
580 'contribution_payment_instrument' => 'Cash',
582 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
583 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
584 $this->assertEquals(1, $contribution['count']);
585 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
586 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
587 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
588 $this->assertEquals(1, $contribution['count']);
589 $contribution = $this->callAPISuccess('contribution', 'get', array(
591 'payment_instrument_id' => 5,
593 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
594 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
595 $this->assertEquals(1, $contribution['count']);
596 $contribution = $this->callAPISuccess('contribution', 'get', array(
598 'payment_instrument' => 'EFT',
600 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
601 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
602 $this->assertEquals(1, $contribution['count']);
603 $contribution = $this->callAPISuccess('contribution', 'create', array(
604 'id' => $contribution['id'],
605 'payment_instrument' => 'Credit Card',
607 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
608 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
609 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
610 $this->assertEquals(1, $contribution['count']);
614 * CRM-16227 introduces invoice_id as a parameter.
616 public function testGetContributionByInvoice() {
617 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('invoice_id' => 'curly')));
618 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), array('invoice_id' => 'churlish'));
619 $this->callAPISuccessGetCount('Contribution', array(), 2);
620 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
621 // The following don't work. They are the format we are trying to introduce but although the form uses this format
622 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
623 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
624 // come out of convertFormValues
625 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
626 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
627 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
628 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
633 * Check the credit note retrieval is case insensitive.
635 public function testGetCreditNoteCaseInsensitive() {
636 $this->contributionCreate(array('contact_id' => $this->_individualId
));
637 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId
, 'invoice_id' => rand(), 'trxn_id' => rand()));
638 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
639 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
643 * Test retrieval by total_amount works.
647 public function testGetContributionByTotalAmount() {
648 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '5')));
649 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '10')));
650 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
651 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
652 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
653 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
654 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
655 $this->callAPISuccessGetCount('Contribution', array(), 2);
659 * Create test with unique field name on source.
661 public function testCreateContributionSource() {
664 'contact_id' => $this->_individualId
,
665 'receive_date' => date('Ymd'),
666 'total_amount' => 100.00,
667 'financial_type_id' => $this->_financialTypeId
,
668 'payment_instrument_id' => 1,
669 'non_deductible_amount' => 10.00,
670 'fee_amount' => 50.00,
671 'net_amount' => 90.00,
673 'invoice_id' => 67890,
674 'contribution_source' => 'SSF',
675 'contribution_status_id' => 1,
678 $contribution = $this->callAPISuccess('contribution', 'create', $params);
679 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
680 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
684 * Create test with unique field name on source.
686 * @param string $thousandSeparator
687 * punctuation used to refer to thousands.
689 * @dataProvider getThousandSeparators
691 public function testCreateDefaultNow($thousandSeparator) {
692 $this->setCurrencySeparators($thousandSeparator);
693 $params = $this->_params
;
694 unset($params['receive_date'], $params['net_amount']);
696 $params['total_amount'] = $this->formatMoneyInput(5000.77);
697 $params['fee_amount'] = $this->formatMoneyInput(.77);
699 $contribution = $this->callAPISuccess('contribution', 'create', $params);
700 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
701 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
702 $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77));
703 $this->assertEquals(.77, $contribution['fee_amount']);
704 $this->assertEquals(5000, $contribution['net_amount']);
708 * Create test with unique field name on source.
710 public function testCreateContributionNullOutThankyouDate() {
712 $params = $this->_params
;
713 $params['thankyou_date'] = 'yesterday';
715 $contribution = $this->callAPISuccess('contribution', 'create', $params);
716 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
717 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
719 $params['thankyou_date'] = 'null';
720 $contribution = $this->callAPISuccess('contribution', 'create', $params);
721 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
725 * Create test with unique field name on source.
727 public function testCreateContributionSourceInvalidContact() {
731 'receive_date' => date('Ymd'),
732 'total_amount' => 100.00,
733 'financial_type_id' => $this->_financialTypeId
,
734 'payment_instrument_id' => 1,
735 'non_deductible_amount' => 10.00,
736 'fee_amount' => 50.00,
737 'net_amount' => 90.00,
739 'invoice_id' => 67890,
740 'contribution_source' => 'SSF',
741 'contribution_status_id' => 1,
744 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
747 public function testCreateContributionSourceInvalidContContact() {
750 'contribution_contact_id' => 999,
751 'receive_date' => date('Ymd'),
752 'total_amount' => 100.00,
753 'financial_type_id' => $this->_financialTypeId
,
754 'payment_instrument_id' => 1,
755 'non_deductible_amount' => 10.00,
756 'fee_amount' => 50.00,
757 'net_amount' => 90.00,
759 'invoice_id' => 67890,
760 'contribution_source' => 'SSF',
761 'contribution_status_id' => 1,
764 $this->callAPIFailure('contribution', 'create', $params);
768 * Test note created correctly.
770 public function testCreateContributionWithNote() {
771 $description = "Demonstrates creating contribution with Note Entity.";
772 $subfile = "ContributionCreateWithNote";
774 'contact_id' => $this->_individualId
,
775 'receive_date' => '2012-01-01',
776 'total_amount' => 100.00,
777 'financial_type_id' => $this->_financialTypeId
,
778 'payment_instrument_id' => 1,
779 'non_deductible_amount' => 10.00,
780 'fee_amount' => 50.00,
781 'net_amount' => 90.00,
783 'invoice_id' => 67890,
785 'contribution_status_id' => 1,
786 'note' => 'my contribution note',
789 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
790 $result = $this->callAPISuccess('note', 'get', array(
791 'entity_table' => 'civicrm_contribution',
792 'entity_id' => $contribution['id'],
795 $this->assertEquals('my contribution note', $result['values'][0]['note']);
796 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
799 public function testCreateContributionWithNoteUniqueNameAliases() {
801 'contact_id' => $this->_individualId
,
802 'receive_date' => '2012-01-01',
803 'total_amount' => 100.00,
804 'financial_type_id' => $this->_financialTypeId
,
805 'payment_instrument_id' => 1,
806 'non_deductible_amount' => 10.00,
807 'fee_amount' => 50.00,
808 'net_amount' => 90.00,
810 'invoice_id' => 67890,
812 'contribution_status_id' => 1,
813 'contribution_note' => 'my contribution note',
816 $contribution = $this->callAPISuccess('contribution', 'create', $params);
817 $result = $this->callAPISuccess('note', 'get', array(
818 'entity_table' => 'civicrm_contribution',
819 'entity_id' => $contribution['id'],
822 $this->assertEquals('my contribution note', $result['values'][0]['note']);
823 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
827 * This is the test for creating soft credits.
829 public function testCreateContributionWithSoftCredit() {
830 $description = "Demonstrates creating contribution with SoftCredit.";
831 $subfile = "ContributionCreateWithSoftCredit";
832 $contact2 = $this->callAPISuccess('Contact', 'create', array(
833 'display_name' => 'superman',
834 'contact_type' => 'Individual',
837 'contact_id' => $contact2['id'],
839 'soft_credit_type_id' => 3,
842 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
843 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
844 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
846 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
847 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
848 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
850 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
851 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
854 public function testCreateContributionWithSoftCreditDefaults() {
855 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
856 $subfile = "ContributionCreateWithSoftCreditDefaults";
857 $contact2 = $this->callAPISuccess('Contact', 'create', array(
858 'display_name' => 'superman',
859 'contact_type' => 'Individual',
861 $params = $this->_params +
array(
862 'soft_credit_to' => $contact2['id'],
864 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
865 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
867 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
868 // Default soft credit amount = contribution.total_amount
869 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
870 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
872 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
873 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
877 * Test creating contribution with Soft Credit by passing in honor_contact_id.
879 public function testCreateContributionWithHonoreeContact() {
880 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
881 $subfile = "ContributionCreateWithHonoreeContact";
882 $contact2 = $this->callAPISuccess('Contact', 'create', array(
883 'display_name' => 'superman',
884 'contact_type' => 'Individual',
886 $params = $this->_params +
array(
887 'honor_contact_id' => $contact2['id'],
889 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
890 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
892 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
893 // Default soft credit amount = contribution.total_amount
894 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
895 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
896 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
897 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
899 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
900 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
904 * Test using example code.
906 public function testContributionCreateExample() {
907 //make sure at least on page exists since there is a truncate in tear down
908 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
909 require_once 'api/v3/examples/Contribution/Create.php';
910 $result = contribution_create_example();
912 $expectedResult = contribution_create_expectedresult();
913 $this->checkArrayEquals($expectedResult, $result);
914 $this->contributionDelete($id);
918 * Function tests that additional financial records are created when fee amount is recorded.
920 public function testCreateContributionWithFee() {
922 'contact_id' => $this->_individualId
,
923 'receive_date' => '20120511',
924 'total_amount' => 100.00,
926 'financial_type_id' => 1,
928 'invoice_id' => 67890,
930 'contribution_status_id' => 1,
933 $contribution = $this->callAPISuccess('contribution', 'create', $params);
934 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
935 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
936 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
937 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
938 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
939 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
940 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
941 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
942 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
944 $lineItems = $this->callAPISuccess('line_item', 'get', array(
946 'entity_id' => $contribution['id'],
947 'entity_table' => 'civicrm_contribution',
950 $this->assertEquals(1, $lineItems['count']);
951 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
952 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
953 $lineItems = $this->callAPISuccess('line_item', 'get', array(
955 'entity_id' => $contribution['id'],
956 'contribution_id' => $contribution['id'],
957 'entity_table' => 'civicrm_contribution',
960 $this->assertEquals(1, $lineItems['count']);
961 $this->_checkFinancialRecords($contribution, 'feeAmount');
966 * Function tests that additional financial records are created when online contribution is created.
968 public function testCreateContributionOnline() {
969 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
970 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
971 $this->assertAPISuccess($contributionPage);
973 'contact_id' => $this->_individualId
,
974 'receive_date' => '20120511',
975 'total_amount' => 100.00,
976 'financial_type_id' => 1,
977 'contribution_page_id' => $contributionPage['id'],
978 'payment_processor' => $this->paymentProcessorID
,
980 'invoice_id' => 67890,
982 'contribution_status_id' => 1,
986 $contribution = $this->callAPISuccess('contribution', 'create', $params);
987 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
988 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
989 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
990 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
991 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
992 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
993 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
994 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
995 'id' => $this->paymentProcessorID
,
996 'return' => 'payment_instrument_id',
998 $this->_checkFinancialRecords($contribution, 'online');
1002 * Check handling of financial type.
1004 * In the interests of removing financial type / contribution type checks from
1005 * legacy format function lets test that the api is doing this for us
1007 public function testCreateInvalidFinancialType() {
1008 $params = $this->_params
;
1009 $params['financial_type_id'] = 99999;
1010 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
1014 * Check handling of financial type.
1016 * In the interests of removing financial type / contribution type checks from
1017 * legacy format function lets test that the api is doing this for us
1019 public function testValidNamedFinancialType() {
1020 $params = $this->_params
;
1021 $params['financial_type_id'] = 'Donation';
1022 $this->callAPISuccess($this->_entity
, 'create', $params);
1026 * Tests that additional financial records are created.
1028 * Checks when online contribution with pay later option is created
1030 public function testCreateContributionPayLaterOnline() {
1031 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1032 $this->_pageParams
['is_pay_later'] = 1;
1033 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1034 $this->assertAPISuccess($contributionPage);
1036 'contact_id' => $this->_individualId
,
1037 'receive_date' => '20120511',
1038 'total_amount' => 100.00,
1039 'financial_type_id' => 1,
1040 'contribution_page_id' => $contributionPage['id'],
1042 'is_pay_later' => 1,
1043 'invoice_id' => 67890,
1045 'contribution_status_id' => 2,
1049 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1050 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1051 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1052 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1053 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1054 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1055 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1056 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1057 $this->_checkFinancialRecords($contribution, 'payLater');
1061 * Function tests that additional financial records are created for online contribution with pending option.
1063 public function testCreateContributionPendingOnline() {
1064 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1065 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1066 $this->assertAPISuccess($contributionPage);
1068 'contact_id' => $this->_individualId
,
1069 'receive_date' => '20120511',
1070 'total_amount' => 100.00,
1071 'financial_type_id' => 1,
1072 'contribution_page_id' => $contributionPage['id'],
1074 'invoice_id' => 67890,
1076 'contribution_status_id' => 2,
1079 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1080 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1081 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1082 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1083 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1084 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1085 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1086 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1087 $this->_checkFinancialRecords($contribution, 'pending');
1091 * Test that BAO defaults work.
1093 public function testCreateBAODefaults() {
1094 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1095 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1096 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1097 'id' => $contribution['id'],
1098 'api.contribution.delete' => 1,
1100 $this->assertEquals(1, $contribution['contribution_status_id']);
1101 $this->assertEquals('Check', $contribution['payment_instrument']);
1105 * Function tests that line items, financial records are updated when contribution amount is changed.
1107 public function testCreateUpdateContributionChangeTotal() {
1108 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1109 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1111 'entity_id' => $contribution['id'],
1112 'entity_table' => 'civicrm_contribution',
1114 'return' => 'line_total',
1116 $this->assertEquals('100.00', $lineItems);
1117 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1118 // Financial trxn SUM = 100 + 5 (fee)
1119 $this->assertEquals('105.00', $trxnAmount);
1122 'id' => $contribution['id'],
1123 'total_amount' => '125',
1125 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1127 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1129 'entity_id' => $contribution['id'],
1130 'entity_table' => 'civicrm_contribution',
1132 'return' => 'line_total',
1135 $this->assertEquals('125.00', $lineItems);
1136 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1138 // Financial trxn SUM = 125 + 5 (fee).
1139 $this->assertEquals('130.00', $trxnAmount);
1140 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1144 * Function tests that line items, financial records are updated when pay later contribution is received.
1146 public function testCreateUpdateContributionPayLater() {
1147 $contribParams = array(
1148 'contact_id' => $this->_individualId
,
1149 'receive_date' => '2012-01-01',
1150 'total_amount' => 100.00,
1151 'financial_type_id' => $this->_financialTypeId
,
1152 'payment_instrument_id' => 1,
1153 'contribution_status_id' => 2,
1154 'is_pay_later' => 1,
1157 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1159 $newParams = array_merge($contribParams, array(
1160 'id' => $contribution['id'],
1161 'contribution_status_id' => 1,
1164 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1165 $contribution = $contribution['values'][$contribution['id']];
1166 $this->assertEquals($contribution['contribution_status_id'], '1');
1167 $this->_checkFinancialItem($contribution['id'], 'paylater');
1168 $this->_checkFinancialTrxn($contribution, 'payLater');
1172 * Function tests that financial records are updated when Payment Instrument is changed.
1174 public function testCreateUpdateContributionPaymentInstrument() {
1175 $instrumentId = $this->_addPaymentInstrument();
1176 $contribParams = array(
1177 'contact_id' => $this->_individualId
,
1178 'total_amount' => 100.00,
1179 'financial_type_id' => $this->_financialTypeId
,
1180 'payment_instrument_id' => 4,
1181 'contribution_status_id' => 1,
1184 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1186 $newParams = array_merge($contribParams, array(
1187 'id' => $contribution['id'],
1188 'payment_instrument_id' => $instrumentId,
1191 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1192 $this->assertAPISuccess($contribution);
1193 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1195 // cleanup - delete created payment instrument
1196 $this->_deletedAddedPaymentInstrument();
1200 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1202 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1203 $instrumentId = $this->_addPaymentInstrument();
1204 $contribParams = array(
1205 'contact_id' => $this->_individualId
,
1206 'total_amount' => -100.00,
1207 'financial_type_id' => $this->_financialTypeId
,
1208 'payment_instrument_id' => 4,
1209 'contribution_status_id' => 1,
1212 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1214 $newParams = array_merge($contribParams, array(
1215 'id' => $contribution['id'],
1216 'payment_instrument_id' => $instrumentId,
1219 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1220 $this->assertAPISuccess($contribution);
1221 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1223 // cleanup - delete created payment instrument
1224 $this->_deletedAddedPaymentInstrument();
1228 * Function tests that financial records are added when Contribution is Refunded.
1230 public function testCreateUpdateContributionRefund() {
1231 $contributionParams = array(
1232 'contact_id' => $this->_individualId
,
1233 'receive_date' => '2012-01-01',
1234 'total_amount' => 100.00,
1235 'financial_type_id' => $this->_financialTypeId
,
1236 'payment_instrument_id' => 4,
1237 'contribution_status_id' => 1,
1238 'trxn_id' => 'original_payment',
1240 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1241 $newParams = array_merge($contributionParams, array(
1242 'id' => $contribution['id'],
1243 'contribution_status_id' => 'Refunded',
1244 'cancel_date' => '2015-01-01 09:00',
1245 'refund_trxn_id' => 'the refund',
1249 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1250 $this->_checkFinancialTrxn($contribution, 'refund');
1251 $this->_checkFinancialItem($contribution['id'], 'refund');
1252 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1253 'id' => $contribution['id'],
1254 'return' => 'trxn_id',
1259 * Refund a contribution for a financial type with a contra account.
1261 * CRM-17951 the contra account is a financial account with a relationship to a
1262 * financial type. It is not always configured but should be reflected
1263 * in the financial_trxn & financial_item table if it is.
1265 public function testCreateUpdateChargebackContributionDefaultAccount() {
1266 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1267 $this->callAPISuccess('Contribution', 'create', array(
1268 'id' => $contribution['id'],
1269 'contribution_status_id' => 'Chargeback',
1271 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1273 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1274 'contribution_id' => $contribution['id'],
1275 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1277 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1278 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1279 'total_amount' => -100,
1280 'status_id' => 'Chargeback',
1281 'to_financial_account_id' => 6,
1286 * Refund a contribution for a financial type with a contra account.
1288 * CRM-17951 the contra account is a financial account with a relationship to a
1289 * financial type. It is not always configured but should be reflected
1290 * in the financial_trxn & financial_item table if it is.
1292 public function testCreateUpdateChargebackContributionCustomAccount() {
1293 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1294 'name' => 'Chargeback Account',
1295 'is_active' => TRUE,
1298 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1299 'entity_id' => $this->_financialTypeId
,
1300 'entity_table' => 'civicrm_financial_type',
1301 'account_relationship' => 'Chargeback Account is',
1302 'financial_account_id' => 'Chargeback Account',
1305 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1306 $this->callAPISuccess('Contribution', 'create', array(
1307 'id' => $contribution['id'],
1308 'contribution_status_id' => 'Chargeback',
1310 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1312 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1313 'contribution_id' => $contribution['id'],
1314 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1316 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1318 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1319 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1320 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1324 * Refund a contribution for a financial type with a contra account.
1326 * CRM-17951 the contra account is a financial account with a relationship to a
1327 * financial type. It is not always configured but should be reflected
1328 * in the financial_trxn & financial_item table if it is.
1330 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1331 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1332 'name' => 'Refund Account',
1333 'is_active' => TRUE,
1336 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1337 'entity_id' => $this->_financialTypeId
,
1338 'entity_table' => 'civicrm_financial_type',
1339 'account_relationship' => 'Credit/Contra Revenue Account is',
1340 'financial_account_id' => 'Refund Account',
1343 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1344 $this->callAPISuccess('Contribution', 'create', array(
1345 'id' => $contribution['id'],
1346 'contribution_status_id' => 'Refunded',
1349 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1350 'contribution_id' => $contribution['id'],
1351 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1353 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1355 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1356 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1357 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1361 * Function tests that trxn_id is set when passed in.
1363 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1364 * when trxn_id is passed in.
1366 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1367 $contributionParams = array(
1368 'contact_id' => $this->_individualId
,
1369 'receive_date' => '2012-01-01',
1370 'total_amount' => 100.00,
1371 'financial_type_id' => $this->_financialTypeId
,
1372 'payment_instrument_id' => 4,
1373 'contribution_status_id' => 1,
1374 'trxn_id' => 'original_payment',
1376 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1377 $newParams = array_merge($contributionParams, array(
1378 'id' => $contribution['id'],
1379 'contribution_status_id' => 'Refunded',
1380 'cancel_date' => '2015-01-01 09:00',
1381 'trxn_id' => 'the refund',
1385 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1386 $this->_checkFinancialTrxn($contribution, 'refund');
1387 $this->_checkFinancialItem($contribution['id'], 'refund');
1388 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1389 'id' => $contribution['id'],
1390 'return' => 'trxn_id',
1395 * Function tests that trxn_id is set when passed in.
1397 * Here we ensure that the civicrm_contribution.trxn_id is set
1398 * when trxn_id is passed in but if refund_trxn_id is different then that
1399 * is kept for the refund transaction.
1401 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1402 $contributionParams = array(
1403 'contact_id' => $this->_individualId
,
1404 'receive_date' => '2012-01-01',
1405 'total_amount' => 100.00,
1406 'financial_type_id' => $this->_financialTypeId
,
1407 'payment_instrument_id' => 4,
1408 'contribution_status_id' => 1,
1409 'trxn_id' => 'original_payment',
1411 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1412 $newParams = array_merge($contributionParams, array(
1413 'id' => $contribution['id'],
1414 'contribution_status_id' => 'Refunded',
1415 'cancel_date' => '2015-01-01 09:00',
1416 'trxn_id' => 'cont id',
1417 'refund_trxn_id' => 'the refund',
1421 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1422 $this->_checkFinancialTrxn($contribution, 'refund');
1423 $this->_checkFinancialItem($contribution['id'], 'refund');
1424 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1425 'id' => $contribution['id'],
1426 'return' => 'trxn_id',
1431 * Function tests that refund_trxn_id is set when passed in empty.
1433 * Here we ensure that the civicrm_contribution.trxn_id is set
1434 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1435 * is kept for the refund transaction.
1437 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1438 $contributionParams = array(
1439 'contact_id' => $this->_individualId
,
1440 'receive_date' => '2012-01-01',
1441 'total_amount' => 100.00,
1442 'financial_type_id' => $this->_financialTypeId
,
1443 'payment_instrument_id' => 4,
1444 'contribution_status_id' => 1,
1445 'trxn_id' => 'original_payment',
1447 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1448 $newParams = array_merge($contributionParams, array(
1449 'id' => $contribution['id'],
1450 'contribution_status_id' => 'Refunded',
1451 'cancel_date' => '2015-01-01 09:00',
1452 'trxn_id' => 'cont id',
1453 'refund_trxn_id' => '',
1457 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1458 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1459 $this->_checkFinancialItem($contribution['id'], 'refund');
1460 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1461 'id' => $contribution['id'],
1462 'return' => 'trxn_id',
1467 * Function tests invalid contribution status change.
1469 public function testCreateUpdateContributionInValidStatusChange() {
1470 $contribParams = array(
1472 'receive_date' => '2012-01-01',
1473 'total_amount' => 100.00,
1474 'financial_type_id' => 1,
1475 'payment_instrument_id' => 1,
1476 'contribution_status_id' => 1,
1478 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1479 $newParams = array_merge($contribParams, array(
1480 'id' => $contribution['id'],
1481 'contribution_status_id' => 2,
1484 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1489 * Function tests that financial records are added when Pending Contribution is Canceled.
1491 public function testCreateUpdateContributionCancelPending() {
1492 $contribParams = array(
1493 'contact_id' => $this->_individualId
,
1494 'receive_date' => '2012-01-01',
1495 'total_amount' => 100.00,
1496 'financial_type_id' => $this->_financialTypeId
,
1497 'payment_instrument_id' => 1,
1498 'contribution_status_id' => 2,
1499 'is_pay_later' => 1,
1502 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1503 $newParams = array_merge($contribParams, array(
1504 'id' => $contribution['id'],
1505 'contribution_status_id' => 3,
1506 'cancel_date' => '2012-02-02 09:00',
1509 //Check if trxn_date is same as cancel_date.
1510 $checkTrxnDate = array(
1511 'trxn_date' => '2012-02-02 09:00:00',
1513 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1514 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1515 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1519 * Function tests that financial records are added when Financial Type is Changed.
1521 public function testCreateUpdateContributionChangeFinancialType() {
1522 $contribParams = array(
1523 'contact_id' => $this->_individualId
,
1524 'receive_date' => '2012-01-01',
1525 'total_amount' => 100.00,
1526 'financial_type_id' => 1,
1527 'payment_instrument_id' => 1,
1528 'contribution_status_id' => 1,
1531 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1532 $newParams = array_merge($contribParams, array(
1533 'id' => $contribution['id'],
1534 'financial_type_id' => 3,
1537 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1538 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1539 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1543 * Test that update does not change status id CRM-15105.
1545 public function testCreateUpdateWithoutChangingPendingStatus() {
1546 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1547 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1548 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1549 'id' => $contribution['id'],
1550 'api.contribution.delete' => 1,
1552 $this->assertEquals(2, $contribution['contribution_status_id']);
1556 * Test Updating a Contribution.
1558 * CHANGE: we require the API to do an incremental update
1560 public function testCreateUpdateContribution() {
1561 $contributionID = $this->contributionCreate(array(
1562 'contact_id' => $this->_individualId
,
1563 'trxn_id' => 212355,
1564 'financial_type_id' => $this->_financialTypeId
,
1565 'invoice_id' => 'old_invoice',
1567 $old_params = array(
1568 'contribution_id' => $contributionID,
1570 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1571 $this->assertEquals($original['id'], $contributionID);
1572 //set up list of old params, verify
1574 //This should not be required on update:
1575 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1576 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1577 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1578 $old_source = $original['values'][$contributionID]['contribution_source'];
1580 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1581 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1583 //check against values in CiviUnitTestCase::createContribution()
1584 $this->assertEquals($old_contact_id, $this->_individualId
);
1585 $this->assertEquals($old_fee_amount, 5.00);
1586 $this->assertEquals($old_source, 'SSF');
1587 $this->assertEquals($old_trxn_id, 212355);
1588 $this->assertEquals($old_invoice_id, 'old_invoice');
1590 'id' => $contributionID,
1591 'contact_id' => $this->_individualId
,
1592 'total_amount' => 105.00,
1593 'fee_amount' => 7.00,
1594 'financial_type_id' => $this->_financialTypeId
,
1595 'non_deductible_amount' => 22.00,
1596 'contribution_status_id' => 1,
1597 'note' => 'Donating for Noble Cause',
1600 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1602 $new_params = array(
1603 'contribution_id' => $contribution['id'],
1606 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1608 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1609 $this->assertEquals($contribution['total_amount'], 105.00);
1610 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1611 $this->assertEquals($contribution['financial_type'], 'Donation');
1612 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1613 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1614 $this->assertEquals($contribution['fee_amount'], 7.00);
1615 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1616 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1617 $this->assertEquals($contribution['contribution_source'], $old_source);
1618 $this->assertEquals($contribution['contribution_status'], 'Completed');
1620 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1623 'contribution_id' => $contributionID,
1626 $result = $this->callAPISuccess('contribution', 'delete', $params);
1627 $this->assertAPISuccess($result);
1631 * Attempt (but fail) to delete a contribution without parameters.
1633 public function testDeleteEmptyParamsContribution() {
1635 $this->callAPIFailure('contribution', 'delete', $params);
1638 public function testDeleteParamsNotArrayContribution() {
1639 $params = 'contribution_id= 1';
1640 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1641 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1644 public function testDeleteWrongParamContribution() {
1646 'contribution_source' => 'SSF',
1649 $this->callAPIFailure('contribution', 'delete', $params);
1652 public function testDeleteContribution() {
1653 $contributionID = $this->contributionCreate(array(
1654 'contact_id' => $this->_individualId
,
1655 'financial_type_id' => $this->_financialTypeId
,
1658 'id' => $contributionID,
1660 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1664 * Test civicrm_contribution_search with empty params.
1666 * All available contributions expected.
1668 public function testSearchEmptyParams() {
1672 'contact_id' => $this->_individualId
,
1673 'receive_date' => date('Ymd'),
1674 'total_amount' => 100.00,
1675 'financial_type_id' => $this->_financialTypeId
,
1676 'non_deductible_amount' => 10.00,
1677 'fee_amount' => 5.00,
1678 'net_amount' => 95.00,
1680 'invoice_id' => 78910,
1682 'contribution_status_id' => 1,
1685 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1687 $result = $this->callAPISuccess('contribution', 'get', $params);
1688 // We're taking the first element.
1689 $res = $result['values'][$contribution['id']];
1691 $this->assertEquals($p['contact_id'], $res['contact_id']);
1692 $this->assertEquals($p['total_amount'], $res['total_amount']);
1693 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1694 $this->assertEquals($p['net_amount'], $res['net_amount']);
1695 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1696 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1697 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1698 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1699 $this->assertEquals($p['source'], $res['contribution_source']);
1700 // contribution_status_id = 1 => Completed
1701 $this->assertEquals('Completed', $res['contribution_status']);
1703 $this->contributionDelete($contribution['id']);
1707 * Test civicrm_contribution_search. Success expected.
1709 public function testSearch() {
1711 'contact_id' => $this->_individualId
,
1712 'receive_date' => date('Ymd'),
1713 'total_amount' => 100.00,
1714 'financial_type_id' => $this->_financialTypeId
,
1715 'non_deductible_amount' => 10.00,
1716 'contribution_status_id' => 1,
1719 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1722 'contact_id' => $this->_individualId
,
1723 'receive_date' => date('Ymd'),
1724 'total_amount' => 200.00,
1725 'financial_type_id' => $this->_financialTypeId
,
1726 'non_deductible_amount' => 20.00,
1727 'trxn_id' => 5454565,
1728 'invoice_id' => 1212124,
1729 'fee_amount' => 50.00,
1730 'net_amount' => 60.00,
1731 'contribution_status_id' => 2,
1734 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1737 'contribution_id' => $contribution2['id'],
1740 $result = $this->callAPISuccess('contribution', 'get', $params);
1741 $res = $result['values'][$contribution2['id']];
1743 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1744 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1745 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1746 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1747 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1748 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1749 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1750 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1751 // contribution_status_id = 2 => Pending
1752 $this->assertEquals('Pending', $res['contribution_status']);
1754 $this->contributionDelete($contribution1['id']);
1755 $this->contributionDelete($contribution2['id']);
1759 * Test completing a transaction via the API.
1761 * Note that we are creating a logged in user because email goes out from
1764 public function testCompleteTransaction() {
1765 $mut = new CiviMailUtils($this, TRUE);
1766 $this->swapMessageTemplateForTestTemplate();
1767 $this->createLoggedInUser();
1768 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1769 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1770 $this->callAPISuccess('contribution', 'completetransaction', array(
1771 'id' => $contribution['id'],
1773 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1774 $this->assertEquals('SSF', $contribution['contribution_source']);
1775 $this->assertEquals('Completed', $contribution['contribution_status']);
1776 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1777 $mut->checkMailLog(array(
1778 'email:::anthony_anderson@civicrm.org',
1782 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1783 "receipt_date:::\n",
1784 'contributeMode:::notify',
1785 'title:::Contribution',
1786 'displayName:::Mr. Anthony Anderson II',
1787 'contributionStatus:::Completed',
1790 $this->revertTemplateToReservedTemplate();
1794 * Test completing a transaction via the API with a non-USD transaction.
1796 public function testCompleteTransactionEuro() {
1797 $mut = new CiviMailUtils($this, TRUE);
1798 $this->swapMessageTemplateForTestTemplate();
1799 $this->createLoggedInUser();
1800 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1801 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1803 $this->callAPISuccess('contribution', 'completetransaction', array(
1804 'id' => $contribution['id'],
1807 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1808 $this->assertEquals('SSF', $contribution['contribution_source']);
1809 $this->assertEquals('Completed', $contribution['contribution_status']);
1810 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1812 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1813 $entityFinancialTransaction = reset($entityFinancialTransactions);
1814 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1815 $this->assertEquals('EUR', $financialTrxn['currency']);
1817 $mut->checkMailLog(array(
1818 'email:::anthony_anderson@civicrm.org',
1822 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1823 "receipt_date:::\n",
1824 'contributeMode:::notify',
1825 'title:::Contribution',
1826 'displayName:::Mr. Anthony Anderson II',
1827 'contributionStatus:::Completed',
1830 $this->revertTemplateToReservedTemplate();
1834 * Test to ensure mail is sent on chosing pay later
1836 public function testpayLater() {
1837 $mut = new CiviMailUtils($this, TRUE);
1838 $this->swapMessageTemplateForTestTemplate();
1839 $this->createLoggedInUser();
1841 // create contribution page first
1842 $contributionPageParams = array(
1843 'title' => 'Help Support CiviCRM!',
1844 'financial_type_id' => 1,
1845 'is_monetary' => TRUE,
1846 'is_pay_later' => 1,
1847 'is_quick_config' => TRUE,
1848 'pay_later_text' => 'I will send payment by check',
1849 'pay_later_receipt' => 'This is a pay later reciept',
1850 'is_allow_other_amount' => 1,
1851 'min_amount' => 10.00,
1852 'max_amount' => 10000.00,
1853 'goal_amount' => 100000.00,
1854 'is_email_receipt' => 1,
1856 'amount_block_is_active' => 1,
1857 'currency' => 'USD',
1858 'is_billing_required' => 0,
1860 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1862 // submit form values
1863 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1865 'id' => $contributionPageResult['id'],
1866 'contact_id' => $this->_individualId
,
1867 'email-5' => 'anthony_anderson@civicrm.org',
1868 'payment_processor_id' => 0,
1871 'currencyID' => 'USD',
1872 'is_pay_later' => 1,
1873 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1874 'is_quick_config' => 1,
1875 'description' => 'Online Contribution: Help Support CiviCRM!',
1876 'price_set_id' => $priceSet['id'],
1878 $this->callAPISuccess('contribution_page', 'submit', $params);
1880 $mut->checkMailLog(array(
1882 'email:::anthony_anderson@civicrm.org',
1883 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1884 'displayName:::Mr. Anthony Anderson II',
1885 'contributionPageId:::' . $contributionPageResult['id'],
1886 'title:::' . $contributionPageParams['title'],
1887 'amount:::' . $params['amount'],
1890 $this->revertTemplateToReservedTemplate();
1894 * Test to check whether contact billing address is used when no contribution address
1896 public function testBillingAddress() {
1897 $mut = new CiviMailUtils($this, TRUE);
1898 $this->swapMessageTemplateForTestTemplate();
1899 $this->createLoggedInUser();
1901 //Scenario 1: When Contact don't have any address
1902 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1903 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1904 $this->callAPISuccess('contribution', 'completetransaction', array(
1905 'id' => $contribution['id'],
1907 $mut->checkMailLog(array(
1911 // Scenario 2: Contribution using address
1912 $address = $this->callAPISuccess('address', 'create', array(
1913 'street_address' => 'contribution billing st',
1914 'location_type_id' => 2,
1915 'contact_id' => $this->_params
['contact_id'],
1917 $params = array_merge($this->_params
, array(
1918 'contribution_status_id' => 2,
1919 'address_id' => $address['id'],
1922 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1923 $this->callAPISuccess('contribution', 'completetransaction', array(
1924 'id' => $contribution['id'],
1926 $mut->checkMailLog(array(
1927 'address:::contribution billing st',
1930 // Scenario 3: Contribution wtth no address but contact has a billing address
1931 $this->callAPISuccess('address', 'create', array(
1932 'id' => $address['id'],
1933 'street_address' => 'is billing st',
1934 'contact_id' => $this->_params
['contact_id'],
1936 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1937 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1938 $this->callAPISuccess('contribution', 'completetransaction', array(
1939 'id' => $contribution['id'],
1941 $mut->checkMailLog(array(
1942 'address:::is billing st',
1946 $this->revertTemplateToReservedTemplate();
1950 * Test completing a transaction via the API.
1952 * Note that we are creating a logged in user because email goes out from
1955 public function testCompleteTransactionFeeAmount() {
1956 $this->createLoggedInUser();
1957 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1958 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1959 $this->callAPISuccess('contribution', 'completetransaction', array(
1960 'id' => $contribution['id'],
1961 'fee_amount' => '.56',
1962 'trxn_id' => '7778888',
1964 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1965 $this->assertEquals('Completed', $contribution['contribution_status']);
1966 $this->assertEquals('7778888', $contribution['trxn_id']);
1967 $this->assertEquals('.56', $contribution['fee_amount']);
1968 $this->assertEquals('99.44', $contribution['net_amount']);
1972 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
1974 * @param string $thousandSeparator
1975 * punctuation used to refer to thousands.
1977 * @dataProvider getThousandSeparators
1979 public function testCheckTaxAmount($thousandSeparator) {
1980 $this->setCurrencySeparators($thousandSeparator);
1981 $contact = $this->createLoggedInUser();
1982 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1983 'name' => 'Test taxable financial Type',
1987 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1988 'name' => 'Test Tax financial account ',
1989 'contact_id' => $contact,
1990 'financial_account_type_id' => 2,
1997 $financialTypeId = $financialType['id'];
1998 $financialAccountId = $financialAccount['id'];
1999 $financialAccountParams = array(
2000 'entity_table' => 'civicrm_financial_type',
2001 'entity_id' => $financialTypeId,
2002 'account_relationship' => 10,
2003 'financial_account_id' => $financialAccountId,
2005 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
2007 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
2008 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2009 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
2010 $this->callAPISuccess('contribution', 'completetransaction', array(
2011 'id' => $contribution['id'],
2012 'trxn_id' => '777788888',
2013 'fee_amount' => '6.00',
2015 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
2016 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
2017 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
2018 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
2022 * Test repeat contribution successfully creates line item.
2024 public function testRepeatTransaction() {
2025 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2026 $this->callAPISuccess('contribution', 'repeattransaction', array(
2027 'original_contribution_id' => $originalContribution['id'],
2028 'contribution_status_id' => 'Completed',
2029 'trxn_id' => uniqid(),
2031 $lineItemParams = array(
2032 'entity_id' => $originalContribution['id'],
2040 'financial_type_id',
2041 'deductible_amount',
2042 'price_field_value_id',
2046 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2047 'entity_id' => $originalContribution['id'],
2049 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2050 'entity_id' => $originalContribution['id'] +
1,
2052 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2053 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2054 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2055 $this->_checkFinancialRecords(array(
2056 'id' => $originalContribution['id'] +
1,
2057 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2058 'id' => $originalContribution['payment_processor_id'],
2059 'return' => 'payment_instrument_id',
2062 $this->quickCleanUpFinancialEntities();
2066 * Test repeat contribution successfully creates line items (plural).
2068 public function testRepeatTransactionLineItems() {
2070 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2071 $this->callAPISuccess('contribution', 'repeattransaction', array(
2072 'original_contribution_id' => $originalContribution['id'],
2073 'contribution_status_id' => 'Completed',
2074 'trxn_id' => uniqid(),
2077 $lineItemParams = array(
2078 'entity_id' => $originalContribution['id'],
2086 'financial_type_id',
2087 'deductible_amount',
2088 'price_field_value_id',
2092 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2093 'entity_id' => $originalContribution['id'],
2095 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2096 'entity_id' => $originalContribution['id'] +
1,
2099 // unset id and entity_id for all of them to be able to compare the lineItems:
2100 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2101 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2102 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2104 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2105 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2106 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2108 // CRM-19309 so in future we also want to:
2109 // check that financial_line_items have been created for entity_id 3 and 4;
2111 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2112 $this->quickCleanUpFinancialEntities();
2116 * Test repeat contribution successfully creates is_test transaction.
2118 public function testRepeatTransactionIsTest() {
2119 $this->_params
['is_test'] = 1;
2120 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2122 $this->callAPISuccess('contribution', 'repeattransaction', array(
2123 'original_contribution_id' => $originalContribution['id'],
2124 'contribution_status_id' => 'Completed',
2125 'trxn_id' => uniqid(),
2127 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2131 * Test repeat contribution passed in status.
2133 public function testRepeatTransactionPassedInStatus() {
2134 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2136 $this->callAPISuccess('contribution', 'repeattransaction', array(
2137 'original_contribution_id' => $originalContribution['id'],
2138 'contribution_status_id' => 'Pending',
2139 'trxn_id' => uniqid(),
2141 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2145 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2147 public function testRepeatTransactionAcceptRecurID() {
2148 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2149 'contact_id' => $this->_individualId
,
2150 'installments' => '12',
2151 'frequency_interval' => '1',
2153 'contribution_status_id' => 1,
2154 'start_date' => '2012-01-01 00:00:00',
2155 'currency' => 'USD',
2156 'frequency_unit' => 'month',
2157 'payment_processor_id' => $this->paymentProcessorID
,
2159 $this->callAPISuccess('contribution', 'create', array_merge(
2161 array('contribution_recur_id' => $contributionRecur['id']))
2164 $this->callAPISuccess('contribution', 'repeattransaction', array(
2165 'contribution_recur_id' => $contributionRecur['id'],
2166 'contribution_status_id' => 'Completed',
2167 'trxn_id' => uniqid(),
2170 $this->quickCleanUpFinancialEntities();
2174 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2176 public function testRepeatTransactionTestRecurId() {
2177 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2178 'contact_id' => $this->_individualId
,
2179 'frequency_interval' => '1',
2181 'contribution_status_id' => 1,
2182 'start_date' => '2017-01-01 00:00:00',
2183 'currency' => 'USD',
2184 'frequency_unit' => 'month',
2185 'payment_processor_id' => $this->paymentProcessorID
,
2188 $this->callAPISuccess('contribution', 'create', array_merge(
2191 'contribution_recur_id' => $contributionRecur['id'],
2196 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2197 'contribution_recur_id' => $contributionRecur['id'],
2198 'contribution_status_id' => 'Completed',
2199 'trxn_id' => uniqid(),
2202 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2203 $this->quickCleanUpFinancialEntities();
2206 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2208 * ( Tests membership is renewed after repeattransaction. )
2210 public function testRepeatTransactionUsesCompleted() {
2211 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2213 $this->callAPISuccess('contribution', 'create', array(
2214 'contact_id' => $originalContribution['values'][1]['contact_id'],
2215 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2216 'total_amount' => $originalContribution['values'][1]['total_amount'],
2217 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2218 'contribution_status_id' => "Failed",
2221 $this->callAPISuccess('membership', 'create', array(
2222 'id' => $membership['id'],
2223 'end_date' => 'yesterday',
2227 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2228 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2229 'contribution_status_id' => 'Completed',
2230 'trxn_id' => 'bobsled',
2233 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2234 'id' => $membership['id'],
2235 'return' => 'status_id',
2238 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2240 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2241 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2242 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2243 $this->quickCleanUpFinancialEntities();
2244 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2248 * CRM-16397 test appropriate action if total amount has changed for single line items.
2250 public function testRepeatTransactionAlteredAmount() {
2251 $paymentProcessorID = $this->paymentProcessorCreate();
2252 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2253 'contact_id' => $this->_individualId
,
2254 'installments' => '12',
2255 'frequency_interval' => '1',
2257 'contribution_status_id' => 1,
2258 'start_date' => '2012-01-01 00:00:00',
2259 'currency' => 'USD',
2260 'frequency_unit' => 'month',
2261 'payment_processor_id' => $paymentProcessorID,
2263 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2266 'contribution_recur_id' => $contributionRecur['id'],
2270 $this->callAPISuccess('contribution', 'repeattransaction', array(
2271 'original_contribution_id' => $originalContribution['id'],
2272 'contribution_status_id' => 'Completed',
2273 'trxn_id' => uniqid(),
2274 'total_amount' => '400',
2278 $lineItemParams = array(
2279 'entity_id' => $originalContribution['id'],
2287 'financial_type_id',
2288 'deductible_amount',
2289 'price_field_value_id',
2293 $this->callAPISuccessGetSingle('contribution', array(
2294 'total_amount' => 400,
2296 'net_amount' => 350,
2298 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2299 'entity_id' => $originalContribution['id'],
2301 $expectedLineItem = array_merge(
2302 $lineItem1['values'][0], array(
2303 'line_total' => '400.00',
2304 'unit_price' => '400.00',
2308 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2309 'entity_id' => $originalContribution['id'] +
1,
2312 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2313 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2314 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2318 * CRM-17718 test appropriate action if financial type has changed for single line items.
2320 public function testRepeatTransactionPassedInFinancialType() {
2321 $originalContribution = $this->setUpRecurringContribution();
2323 $this->callAPISuccess('contribution', 'repeattransaction', array(
2324 'original_contribution_id' => $originalContribution['id'],
2325 'contribution_status_id' => 'Completed',
2326 'trxn_id' => uniqid(),
2327 'financial_type_id' => 2,
2329 $lineItemParams = array(
2330 'entity_id' => $originalContribution['id'],
2338 'financial_type_id',
2339 'deductible_amount',
2340 'price_field_value_id',
2345 $this->callAPISuccessGetSingle('contribution', array(
2346 'total_amount' => 100,
2347 'financial_type_id' => 2,
2349 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2350 'entity_id' => $originalContribution['id'],
2352 $expectedLineItem = array_merge(
2353 $lineItem1['values'][0], array(
2354 'line_total' => '100.00',
2355 'unit_price' => '100.00',
2356 'financial_type_id' => 2,
2357 'contribution_type_id' => 2,
2360 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2361 'entity_id' => $originalContribution['id'] +
1,
2363 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2364 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2365 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2369 * CRM-17718 test appropriate action if financial type has changed for single line items.
2371 public function testRepeatTransactionUpdatedFinancialType() {
2372 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2374 $this->callAPISuccess('contribution', 'repeattransaction', array(
2375 'contribution_recur_id' => $originalContribution['id'],
2376 'contribution_status_id' => 'Completed',
2377 'trxn_id' => uniqid(),
2379 $lineItemParams = array(
2380 'entity_id' => $originalContribution['id'],
2388 'financial_type_id',
2389 'deductible_amount',
2390 'price_field_value_id',
2395 $this->callAPISuccessGetSingle('contribution', array(
2396 'total_amount' => 100,
2397 'financial_type_id' => 2,
2399 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2400 'entity_id' => $originalContribution['id'],
2402 $expectedLineItem = array_merge(
2403 $lineItem1['values'][0], array(
2404 'line_total' => '100.00',
2405 'unit_price' => '100.00',
2406 'financial_type_id' => 2,
2407 'contribution_type_id' => 2,
2411 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2412 'entity_id' => $originalContribution['id'] +
1,
2414 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2415 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2416 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2420 * CRM-16397 test appropriate action if campaign has been passed in.
2422 public function testRepeatTransactionPassedInCampaign() {
2423 $paymentProcessorID = $this->paymentProcessorCreate();
2424 $campaignID = $this->campaignCreate();
2425 $campaignID2 = $this->campaignCreate();
2426 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2427 'contact_id' => $this->_individualId
,
2428 'installments' => '12',
2429 'frequency_interval' => '1',
2431 'contribution_status_id' => 1,
2432 'start_date' => '2012-01-01 00:00:00',
2433 'currency' => 'USD',
2434 'frequency_unit' => 'month',
2435 'payment_processor_id' => $paymentProcessorID,
2437 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2440 'contribution_recur_id' => $contributionRecur['id'],
2441 'campaign_id' => $campaignID,
2445 $this->callAPISuccess('contribution', 'repeattransaction', array(
2446 'original_contribution_id' => $originalContribution['id'],
2447 'contribution_status_id' => 'Completed',
2448 'trxn_id' => uniqid(),
2449 'campaign_id' => $campaignID2,
2452 $this->callAPISuccessGetSingle('contribution', array(
2453 'total_amount' => 100,
2454 'campaign_id' => $campaignID2,
2459 * CRM-17718 campaign stored on contribution recur gets priority.
2461 * This reflects the fact we permit people to update them.
2463 public function testRepeatTransactionUpdatedCampaign() {
2464 $paymentProcessorID = $this->paymentProcessorCreate();
2465 $campaignID = $this->campaignCreate();
2466 $campaignID2 = $this->campaignCreate();
2467 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2468 'contact_id' => $this->_individualId
,
2469 'installments' => '12',
2470 'frequency_interval' => '1',
2472 'contribution_status_id' => 1,
2473 'start_date' => '2012-01-01 00:00:00',
2474 'currency' => 'USD',
2475 'frequency_unit' => 'month',
2476 'payment_processor_id' => $paymentProcessorID,
2477 'campaign_id' => $campaignID,
2479 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2482 'contribution_recur_id' => $contributionRecur['id'],
2483 'campaign_id' => $campaignID2,
2487 $this->callAPISuccess('contribution', 'repeattransaction', array(
2488 'original_contribution_id' => $originalContribution['id'],
2489 'contribution_status_id' => 'Completed',
2490 'trxn_id' => uniqid(),
2493 $this->callAPISuccessGetSingle('contribution', array(
2494 'total_amount' => 100,
2495 'campaign_id' => $campaignID,
2500 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2502 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2504 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
2505 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2506 // This will made the trick to get the not equals behaviour.
2507 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 4));
2508 $this->callAPISuccess('contribution', 'repeattransaction', array(
2509 'contribution_recur_id' => $originalContribution['id'],
2510 'contribution_status_id' => 'Completed',
2511 'trxn_id' => uniqid(),
2513 $lineItemParams = array(
2514 'entity_id' => $originalContribution['id'],
2522 'financial_type_id',
2523 'deductible_amount',
2524 'price_field_value_id',
2528 $this->callAPISuccessGetSingle('contribution', array(
2529 'total_amount' => 100,
2530 'financial_type_id' => 2,
2532 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2533 'entity_id' => $originalContribution['id'],
2535 $expectedLineItem = array_merge(
2536 $lineItem1['values'][0], array(
2537 'line_total' => '100.00',
2538 'unit_price' => '100.00',
2539 'financial_type_id' => 4,
2540 'contribution_type_id' => 4,
2544 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2545 'entity_id' => $originalContribution['id'] +
1,
2547 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 1));
2548 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2549 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2550 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2555 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2557 public function testCompleteTransactionNetAmountOK() {
2558 $this->createLoggedInUser();
2559 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2560 unset($params['net_amount']);
2561 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2562 $this->callAPISuccess('contribution', 'completetransaction', array(
2563 'id' => $contribution['id'],
2565 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2566 $this->assertEquals('Completed', $contribution['contribution_status']);
2567 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2571 * CRM-14151 - Test completing a transaction via the API.
2573 public function testCompleteTransactionWithReceiptDateSet() {
2574 $this->swapMessageTemplateForTestTemplate();
2575 $mut = new CiviMailUtils($this, TRUE);
2576 $this->createLoggedInUser();
2577 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2578 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2579 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2580 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2581 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2582 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2583 $mut->checkMailLog(array(
2584 'Receipt - Contribution',
2585 'receipt_date:::' . date('Ymd'),
2588 $this->revertTemplateToReservedTemplate();
2592 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2594 public function testCompleteTransactionWithEmailReceiptInput() {
2595 $contributionPage = $this->createReceiptableContributionPage();
2597 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2598 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2599 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2600 // Complete the transaction overriding is_email_receipt to = FALSE
2601 $this->callAPISuccess('contribution', 'completetransaction', array(
2602 'id' => $contribution['id'],
2603 'trxn_date' => date('2011-04-09'),
2604 'trxn_id' => 'kazam',
2605 'is_email_receipt' => 0,
2607 // Check if a receipt was issued
2608 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2609 $this->assertEquals('', $receipt_date);
2613 * Test that $is_recur is assigned to the receipt.
2615 public function testCompleteTransactionForRecurring() {
2617 $this->swapMessageTemplateForTestTemplate();
2618 $recurring = $this->setUpRecurringContribution();
2619 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2621 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2622 $this->_params
['contribution_recur_id'] = $recurring['id'];
2624 $contribution = $this->setUpForCompleteTransaction();
2626 $this->callAPISuccess('contribution', 'completetransaction', array(
2627 'id' => $contribution['id'],
2628 'trxn_date' => date('2011-04-09'),
2629 'trxn_id' => 'kazam',
2630 'is_email_receipt' => 1,
2633 $this->mut
->checkMailLog(array(
2635 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2638 $this->revertTemplateToReservedTemplate();
2641 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2643 * If passed in it will override the default from contribution page.
2645 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2646 $mut = new CiviMailUtils($this, TRUE);
2647 $this->createLoggedInUser();
2648 // Create a Contribution Page with is_email_receipt = FALSE
2649 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2650 'receipt_from_name' => 'Mickey Mouse',
2651 'receipt_from_email' => 'mickey@mouse.com',
2652 'title' => "Test Contribution Page",
2653 'financial_type_id' => 1,
2654 'currency' => 'CAD',
2655 'is_monetary' => TRUE,
2656 'is_email_receipt' => 0,
2658 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2659 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2660 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2661 // Complete the transaction overriding is_email_receipt to = TRUE
2662 $this->callAPISuccess('contribution', 'completetransaction', array(
2663 'id' => $contribution['id'],
2664 'is_email_receipt' => 1,
2666 $mut->checkMailLog(array(
2667 'Please print this receipt for your records.',
2673 * Complete the transaction using the template with all the possible.
2675 public function testCompleteTransactionWithTestTemplate() {
2676 $this->swapMessageTemplateForTestTemplate();
2677 $contribution = $this->setUpForCompleteTransaction();
2678 $this->callAPISuccess('contribution', 'completetransaction', array(
2679 'id' => $contribution['id'],
2680 'trxn_date' => date('2011-04-09'),
2681 'trxn_id' => 'kazam',
2683 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2684 $this->mut
->checkMailLog(array(
2685 'email:::anthony_anderson@civicrm.org',
2689 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2690 'receipt_date:::' . date('Ymd'),
2691 'contributeMode:::notify',
2692 'title:::Contribution',
2693 'displayName:::Mr. Anthony Anderson II',
2695 'contactID:::' . $this->_params
['contact_id'],
2696 'contributionID:::' . $contribution['id'],
2697 'financialTypeId:::1',
2698 'financialTypeName:::Donation',
2701 $this->revertTemplateToReservedTemplate();
2705 * Complete the transaction using the template with all the possible.
2707 public function testCompleteTransactionContributionPageFromAddress() {
2708 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2709 'receipt_from_name' => 'Mickey Mouse',
2710 'receipt_from_email' => 'mickey@mouse.com',
2711 'title' => "Test Contribution Page",
2712 'financial_type_id' => 1,
2713 'currency' => 'NZD',
2714 'goal_amount' => 50,
2715 'is_pay_later' => 1,
2716 'is_monetary' => TRUE,
2717 'is_email_receipt' => TRUE,
2719 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2720 $contribution = $this->setUpForCompleteTransaction();
2721 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2722 $this->mut
->checkMailLog(array(
2730 * Test completing first transaction in a recurring series.
2732 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2734 * @dataProvider getScheduledDateData
2736 * @param array $dataSet
2738 * @throws \Exception
2740 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2741 $paymentProcessorID = $this->paymentProcessorCreate();
2742 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2743 'contact_id' => $this->_individualId
,
2744 'installments' => '2',
2745 'frequency_interval' => '1',
2747 'contribution_status_id' => 'Pending',
2748 'start_date' => '2012-01-01 00:00:00',
2749 'currency' => 'USD',
2750 'frequency_unit' => 'month',
2751 'payment_processor_id' => $paymentProcessorID,
2752 ), $dataSet['data']));
2753 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2756 'contribution_recur_id' => $contributionRecur['id'],
2757 'contribution_status_id' => 'Pending',
2758 'receive_date' => $dataSet['receive_date'],
2761 $this->callAPISuccess('Contribution', 'completetransaction', array(
2762 'id' => $contribution,
2763 'receive_date' => $dataSet['receive_date'],
2765 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2766 'id' => $contributionRecur['id'],
2767 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2769 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2770 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2771 $this->callAPISuccess('Contribution', 'create', array_merge(
2774 'contribution_recur_id' => $contributionRecur['id'],
2775 'contribution_status_id' => 'Completed',
2778 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2779 'id' => $contributionRecur['id'],
2780 'return' => array('contribution_status_id'),
2782 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2786 * Get dates for testing.
2790 public function getScheduledDateData() {
2792 $result[]['2016-08-31-1-month'] = array(
2794 'start_date' => '2016-08-31',
2795 'frequency_interval' => 1,
2796 'frequency_unit' => 'month',
2798 'receive_date' => '2016-08-31',
2799 'expected' => '2016-10-01 00:00:00',
2801 $result[]['2012-01-01-1-month'] = array(
2803 'start_date' => '2012-01-01',
2804 'frequency_interval' => 1,
2805 'frequency_unit' => 'month',
2807 'receive_date' => '2012-01-01',
2808 'expected' => '2012-02-01 00:00:00',
2810 $result[]['2012-01-01-1-month'] = array(
2812 'start_date' => '2012-01-01',
2813 'frequency_interval' => 1,
2814 'frequency_unit' => 'month',
2816 'receive_date' => '2012-02-29',
2817 'expected' => '2012-03-29 00:00:00',
2819 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2821 'start_date' => '2012-01-01',
2822 'frequency_interval' => 1,
2823 'frequency_unit' => 'month',
2824 'next_sched_contribution_date' => '2012-02-29',
2826 'receive_date' => '2012-02-29 16:00:00',
2827 'expected' => '2012-03-29 00:00:00',
2833 * Test completing a pledge with the completeTransaction api..
2835 * Note that we are creating a logged in user because email goes out from
2838 public function testCompleteTransactionUpdatePledgePayment() {
2839 $this->swapMessageTemplateForTestTemplate();
2840 $mut = new CiviMailUtils($this, TRUE);
2841 $mut->clearMessages();
2842 $this->createLoggedInUser();
2843 $contributionID = $this->createPendingPledgeContribution();
2844 $this->callAPISuccess('contribution', 'completetransaction', array(
2845 'id' => $contributionID,
2846 'trxn_date' => '1 Feb 2013',
2848 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2849 'id' => $this->_ids
['pledge'],
2851 $this->assertEquals('Completed', $pledge['pledge_status']);
2853 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2854 'pledge_id' => $this->_ids
['pledge'],
2855 'return' => 'status_id',
2857 $this->assertEquals(1, $status);
2858 $mut->checkMailLog(array(
2860 'receive_date:::20130201000000',
2861 "receipt_date:::\n",
2864 $this->revertTemplateToReservedTemplate();
2868 * Test completing a transaction with an event via the API.
2870 * Note that we are creating a logged in user because email goes out from
2873 public function testCompleteTransactionWithParticipantRecord() {
2874 $mut = new CiviMailUtils($this, TRUE);
2875 $mut->clearMessages();
2876 $this->_individualId
= $this->createLoggedInUser();
2877 $contributionID = $this->createPendingParticipantContribution();
2878 $this->callAPISuccess('contribution', 'completetransaction', array(
2879 'id' => $contributionID,
2882 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2883 'id' => $this->_ids
['participant'],
2884 'return' => 'participant_status_id',
2886 $this->assertEquals(1, $participantStatus);
2888 //Assert only three activities are created.
2889 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_individualId
);
2890 $this->assertEquals(3, count($activities));
2891 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
2892 // record two activities before and after completing payment for Event registration
2893 $this->assertEquals(2, $activityNames['Event Registration']);
2894 // update the original 'Contribution' activity created after completing payment
2895 $this->assertEquals(1, $activityNames['Contribution']);
2897 $mut->checkMailLog(array(
2898 'Annual CiviCRM meet',
2900 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2906 * Test membership is renewed when transaction completed.
2908 public function testCompleteTransactionMembershipPriceSet() {
2909 $this->createPriceSetWithPage('membership');
2910 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2914 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2915 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2916 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2917 'membership_id' => $this->_ids
['membership'],
2919 $this->assertEquals(1, $logs['count']);
2920 $this->assertEquals($stateOfGrace, $membership['status_id']);
2921 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2922 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2923 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2924 $this->callAPISuccessGetSingle('LineItem', array(
2925 'entity_id' => $this->_ids
['membership'],
2926 'entity_table' => 'civicrm_membership',
2928 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2929 'membership_id' => $this->_ids
['membership'],
2931 $this->assertEquals(2, $logs['count']);
2932 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2933 //Assert only three activities are created.
2934 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
2935 $this->assertEquals(3, count($activities));
2936 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
2937 $this->assertArrayHasKey('Contribution', $activityNames);
2938 $this->assertArrayHasKey('Membership Signup', $activityNames);
2939 $this->assertArrayHasKey('Change Membership Status', $activityNames);
2940 $this->cleanUpAfterPriceSets();
2944 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2946 public function testPendingToCompleteContribution() {
2947 $this->createPriceSetWithPage('membership');
2948 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2949 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2950 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2951 $activity = $this->callAPISuccess('Activity', 'get', array(
2952 'activity_type_id' => 'Membership Signup',
2953 'source_record_id' => $this->_ids
['membership'],
2954 'status_id' => 'Scheduled',
2956 $this->assertEquals(1, $activity['count']);
2958 // change pending contribution to completed
2959 $form = new CRM_Contribute_Form_Contribution();
2961 $form->_params
= array(
2962 'id' => $this->_ids
['contribution'],
2963 'total_amount' => 20,
2966 'financial_type_id' => 1,
2967 'contact_id' => $this->_individualId
,
2968 'contribution_status_id' => 1,
2969 'billing_middle_name' => '',
2970 'billing_last_name' => 'Adams',
2971 'billing_street_address-5' => '790L Lincoln St S',
2972 'billing_city-5' => 'Maryknoll',
2973 'billing_state_province_id-5' => 1031,
2974 'billing_postal_code-5' => 10545,
2975 'billing_country_id-5' => 1228,
2976 'frequency_interval' => 1,
2977 'frequency_unit' => 'month',
2978 'installments' => '',
2979 'hidden_AdditionalDetail' => 1,
2980 'hidden_Premium' => 1,
2981 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2982 'receipt_date' => '',
2983 'receipt_date_time' => '',
2984 'payment_processor_id' => $this->paymentProcessorID
,
2985 'currency' => 'USD',
2986 'contribution_page_id' => $this->_ids
['contribution_page'],
2987 'contribution_mode' => 'membership',
2988 'source' => 'Membership Signup and Renewal',
2991 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2993 // Case 2: After successful payment for Pending backoffice there are three activities created
2994 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
2995 $activity = $this->callAPISuccess('Activity', 'get', array(
2996 'activity_type_id' => 'Membership Signup',
2997 'source_record_id' => $this->_ids
['membership'],
2998 'status_id' => 'Completed',
3000 $this->assertEquals(1, $activity['count']);
3001 // 2.b Contribution activity created to record successful payment
3002 $activity = $this->callAPISuccess('Activity', 'get', array(
3003 'activity_type_id' => 'Contribution',
3004 'source_record_id' => $this->_ids
['contribution'],
3005 'status_id' => 'Completed',
3007 $this->assertEquals(1, $activity['count']);
3009 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
3010 $activity = $this->callAPISuccess('Activity', 'get', array(
3011 'activity_type_id' => 'Change Membership Status',
3012 'source_record_id' => $this->_ids
['membership'],
3013 'status_id' => 'Completed',
3015 $this->assertEquals(1, $activity['count']);
3016 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
3018 //Create another pending contribution for renewal
3019 $contribution = $this->callAPISuccess('contribution', 'create', array(
3021 'contact_id' => $this->_ids
['contact'],
3022 'receive_date' => date('Ymd'),
3023 'total_amount' => 20.00,
3024 'financial_type_id' => 1,
3025 'payment_instrument_id' => 'Credit Card',
3026 'non_deductible_amount' => 10.00,
3027 'trxn_id' => 'rdhfi88',
3028 'invoice_id' => 'dofhiewuyr',
3030 'contribution_status_id' => 2,
3031 'contribution_page_id' => $this->_ids
['contribution_page'],
3032 // We can't rely on contribution api to link line items correctly to membership
3033 'skipLineItem' => TRUE,
3034 'api.membership_payment.create' => array('membership_id' => $this->_ids
['membership']),
3037 $this->callAPISuccess('line_item', 'create', array(
3038 'entity_id' => $contribution['id'],
3039 'entity_table' => 'civicrm_contribution',
3040 'contribution_id' => $contribution['id'],
3041 'price_field_id' => $this->_ids
['price_field'][0],
3045 'financial_type_id' => 1,
3046 'price_field_value_id' => $this->_ids
['price_field_value']['cont'],
3048 $this->callAPISuccess('line_item', 'create', array(
3049 'entity_id' => $this->_ids
['membership'],
3050 'entity_table' => 'civicrm_membership',
3051 'contribution_id' => $contribution['id'],
3052 'price_field_id' => $this->_ids
['price_field'][0],
3056 'financial_type_id' => 1,
3057 'price_field_value_id' => $this->_ids
['price_field_value'][0],
3058 'membership_type_id' => $this->_ids
['membership_type'],
3061 //Update it to Failed.
3062 $form->_params
['id'] = $contribution['id'];
3063 $form->_params
['contribution_status_id'] = 4;
3065 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3066 //Existing membership should not get updated to expired.
3067 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3068 $this->assertNotEquals($membership['status_id'], 4);
3072 * Test membership is renewed when transaction completed.
3074 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3075 $this->createPriceSetWithPage('membership');
3076 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
3077 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
3078 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3079 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3080 $this->cleanUpAfterPriceSets();
3083 public function cleanUpAfterPriceSets() {
3084 $this->quickCleanUpFinancialEntities();
3085 $this->contactDelete($this->_ids
['contact']);
3089 * Set up a pending transaction with a specific price field id.
3091 * @param int $priceFieldValueID
3093 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
3094 $contactID = $this->individualCreate();
3095 $membership = $this->callAPISuccess('membership', 'create', array(
3096 'contact_id' => $contactID,
3097 'membership_type_id' => $this->_ids
['membership_type'],
3098 'start_date' => 'yesterday - 1 year',
3099 'end_date' => 'yesterday',
3100 'join_date' => 'yesterday - 1 year',
3102 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3104 'contact_id' => $contactID,
3105 'receive_date' => date('Ymd'),
3106 'total_amount' => 20.00,
3107 'financial_type_id' => 1,
3108 'payment_instrument_id' => 'Credit Card',
3109 'non_deductible_amount' => 10.00,
3110 'trxn_id' => 'jdhfi' . rand(1, 100),
3111 'invoice_id' => 'djfhiew' . rand(5, 100),
3113 'contribution_status_id' => 2,
3114 'contribution_page_id' => $this->_ids
['contribution_page'],
3115 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3118 $this->callAPISuccess('line_item', 'create', array(
3119 'entity_id' => $contribution['id'],
3120 'entity_table' => 'civicrm_contribution',
3121 'contribution_id' => $contribution['id'],
3122 'price_field_id' => $this->_ids
['price_field'][0],
3126 'financial_type_id' => 1,
3127 'price_field_value_id' => $priceFieldValueID,
3129 $this->_ids
['contact'] = $contactID;
3130 $this->_ids
['contribution'] = $contribution['id'];
3131 $this->_ids
['membership'] = $membership['id'];
3135 * Test sending a mail via the API.
3137 public function testSendMail() {
3138 $mut = new CiviMailUtils($this, TRUE);
3139 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3140 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3141 'id' => $contribution['id'],
3142 'receipt_from_email' => 'api@civicrm.org',
3145 $mut->checkMailLog(array(
3147 'Contribution Information',
3148 'Please print this confirmation for your records',
3154 $this->checkCreditCardDetails($mut, $contribution['id']);
3159 * Test sending a mail via the API.
3160 * This simulates webform_civicrm using pay later contribution page
3162 public function testSendconfirmationPayLater() {
3163 $mut = new CiviMailUtils($this, TRUE);
3165 // Create contribution page
3166 $pageParams = array(
3167 'title' => 'Webform Contributions',
3168 'financial_type_id' => 1,
3169 'contribution_type_id' => 1,
3170 'is_confirm_enabled' => 1,
3171 'is_pay_later' => 1,
3172 'pay_later_text' => 'I will send payment by cheque',
3173 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3175 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3177 // Create pay later contribution
3178 $contribParams = array(
3179 'contact_id' => $this->_individualId
,
3180 'financial_type_id' => 1,
3181 'is_pay_later' => 1,
3182 'contribution_status_id' => 2,
3183 'contribution_page_id' => $contributionPage['id'],
3184 'total_amount' => '10.00',
3186 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3189 $lineItemParams = array(
3190 'contribution_id' => $contribution['id'],
3191 'entity_id' => $contribution['id'],
3192 'entity_table' => 'civicrm_contribution',
3193 'label' => 'My lineitem label',
3195 'unit_price' => "10.00",
3196 'line_total' => "10.00",
3198 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3202 civicrm_api3('contribution', 'sendconfirmation', array(
3203 'id' => $contribution['id'],
3204 'receipt_from_email' => 'api@civicrm.org',
3208 catch (Exception
$e) {
3209 // Need to figure out how to stop this some other day
3210 // We don't care about the Payment Processor because this is Pay Later
3211 // The point of this test is to check we get the pay_later version of the mail
3212 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3217 // Retrieve mail & check it has the pay_later_receipt info
3218 $mut->getMostRecentEmail('raw');
3219 $mut->checkMailLog(array(
3220 (string) $contribParams['total_amount'],
3221 $pageParams['pay_later_receipt'],
3231 * Check credit card details in sent mail via API
3233 * @param $mut obj CiviMailUtils instance
3234 * @param int $contributionID Contribution ID
3237 public function checkCreditCardDetails($mut, $contributionID) {
3238 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3239 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3240 'id' => $contributionID,
3241 'receipt_from_email' => 'api@civicrm.org',
3242 'payment_processor_id' => $this->paymentProcessorID
,
3245 $mut->checkMailLog(array(
3246 'Credit Card Information', // credit card header
3247 'Billing Name and Address', // billing header
3248 'anthony_anderson@civicrm.org', // billing name
3256 * Test sending a mail via the API.
3258 public function testSendMailEvent() {
3259 $mut = new CiviMailUtils($this, TRUE);
3260 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3261 $event = $this->eventCreate(array(
3262 'is_email_confirm' => 1,
3263 'confirm_from_email' => 'test@civicrm.org',
3265 $this->_eventID
= $event['id'];
3266 $participantParams = array(
3267 'contact_id' => $this->_individualId
,
3268 'event_id' => $this->_eventID
,
3271 // to ensure it matches later on
3272 'register_date' => '2007-07-21 00:00:00',
3273 'source' => 'Online Event Registration: API Testing',
3276 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3277 $this->callAPISuccess('participant_payment', 'create', array(
3278 'participant_id' => $participant['id'],
3279 'contribution_id' => $contribution['id'],
3281 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3282 'id' => $contribution['id'],
3283 'receipt_from_email' => 'api@civicrm.org',
3287 $mut->checkMailLog(array(
3288 'Annual CiviCRM meet',
3290 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3297 * This function does a GET & compares the result against the $params.
3299 * Use as a double check on Creates.
3301 * @param array $params
3303 * @param bool $delete
3305 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3307 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3313 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3315 $this->assertAPISuccess($contribution, 0);
3316 $values = $contribution['values'][$contribution['id']];
3317 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3318 // this is not returned in id format
3319 unset($params['payment_instrument_id']);
3320 $params['contribution_source'] = $params['source'];
3321 unset($params['source']);
3322 foreach ($params as $key => $value) {
3323 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3328 * Create a pending contribution & linked pending pledge record.
3330 public function createPendingPledgeContribution() {
3332 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
3333 $this->_ids
['pledge'] = $pledgeID;
3334 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
3335 'contribution_status_id' => 'Pending',
3336 'total_amount' => 500,
3339 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3340 'options' => array('limit' => 1),
3343 $this->callAPISuccess('PledgePayment', 'create', array(
3345 'contribution_id' =>
3346 $contribution['id'],
3347 'status_id' => 'Pending',
3348 'scheduled_amount' => 500,
3351 return $contribution['id'];
3355 * Create a pending contribution & linked pending participant record (along with an event).
3357 public function createPendingParticipantContribution() {
3358 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3359 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId
));
3360 $this->_ids
['participant'] = $participantID;
3361 $params = array_merge($this->_params
, array('contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3362 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3363 $this->callAPISuccess('participant_payment', 'create', array(
3364 'contribution_id' => $contribution['id'],
3365 'participant_id' => $participantID,
3367 $this->callAPISuccess('line_item', 'get', array(
3368 'entity_id' => $contribution['id'],
3369 'entity_table' => 'civicrm_contribution',
3370 'api.line_item.create' => array(
3371 'entity_id' => $participantID,
3372 'entity_table' => 'civicrm_participant',
3375 return $contribution['id'];
3379 * Get financial transaction amount.
3381 * @param int $contId
3383 * @return null|string
3385 public function _getFinancialTrxnAmount($contId) {
3387 SUM( ft.total_amount ) AS total
3388 FROM civicrm_financial_trxn AS ft
3389 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3390 WHERE ceft.entity_table = 'civicrm_contribution'
3391 AND ceft.entity_id = {$contId}";
3393 $result = CRM_Core_DAO
::singleValueQuery($query);
3398 * @param int $contId
3400 * @return null|string
3402 public function _getFinancialItemAmount($contId) {
3403 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3406 FROM civicrm_financial_item
3407 WHERE entity_table = 'civicrm_line_item'
3408 AND entity_id = {$lineItem}";
3409 $result = CRM_Core_DAO
::singleValueQuery($query);
3414 * @param int $contId
3417 public function _checkFinancialItem($contId, $context) {
3418 if ($context != 'paylater') {
3420 'entity_id' => $contId,
3421 'entity_table' => 'civicrm_contribution',
3423 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3424 $entityParams = array(
3425 'financial_trxn_id' => $trxn['financial_trxn_id'],
3426 'entity_table' => 'civicrm_financial_item',
3428 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3430 'id' => $entityTrxn['entity_id'],
3433 if ($context == 'paylater') {
3434 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3435 foreach ($lineItems as $key => $item) {
3437 'entity_id' => $key,
3438 'entity_table' => 'civicrm_line_item',
3440 $compareParams = array('status_id' => 1);
3441 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3444 elseif ($context == 'refund') {
3445 $compareParams = array(
3447 'financial_account_id' => 1,
3451 elseif ($context == 'cancelPending') {
3452 $compareParams = array(
3454 'financial_account_id' => 1,
3458 elseif ($context == 'changeFinancial') {
3459 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3461 'entity_id' => $lineKey,
3464 $compareParams = array(
3465 'financial_account_id' => 1,
3467 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3469 'financial_account_id' => 3,
3470 'entity_id' => $lineKey,
3472 $compareParams = array(
3476 if ($context != 'paylater') {
3477 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3482 * Check correct financial transaction entries were created for the change in payment instrument.
3484 * @param int $contributionID
3485 * @param int $originalInstrumentID
3486 * @param int $newInstrumentID
3488 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3490 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3492 $originalTrxnParams = array(
3493 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3494 'payment_instrument_id' => $originalInstrumentID,
3495 'amount' => $amount,
3499 $reversalTrxnParams = array(
3500 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3501 'payment_instrument_id' => $originalInstrumentID,
3502 'amount' => -$amount,
3506 $newTrxnParams = array(
3507 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3508 'payment_instrument_id' => $newInstrumentID,
3509 'amount' => $amount,
3513 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3514 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3515 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3517 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3518 'id' => $entityFinancialTrxn['financial_trxn_id'],
3520 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3521 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3522 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3523 $this->assertEquals(0, $financialTrxn['fee_amount']);
3524 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3525 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3528 $this->assertEquals(1, $financialTrxn['is_payment']);
3529 $this->assertEquals('USD', $financialTrxn['currency']);
3530 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3535 * Check financial transaction.
3537 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3539 * @param array $contribution
3540 * @param string $context
3541 * @param int $instrumentId
3542 * @param array $extraParams
3544 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3545 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3546 $trxn = array_pop($financialTrxns);
3549 'id' => $trxn['financial_trxn_id'],
3551 if ($context == 'payLater') {
3552 $compareParams = array(
3554 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3557 elseif ($context == 'refund') {
3558 $compareParams = array(
3559 'to_financial_account_id' => 6,
3560 'total_amount' => -100,
3562 'trxn_date' => '2015-01-01 09:00:00',
3563 'trxn_id' => 'the refund',
3566 elseif ($context == 'cancelPending') {
3567 $compareParams = array(
3568 'to_financial_account_id' => 7,
3569 'total_amount' => -100,
3573 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3574 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3575 // It does the same thing with greater readability.
3576 // @todo remove handling for
3578 $entityParams = array(
3579 'entity_id' => $contribution['id'],
3580 'entity_table' => 'civicrm_contribution',
3583 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3584 $trxnParams1 = array(
3585 'id' => $trxn['financial_trxn_id'],
3587 if (empty($extraParams)) {
3588 $compareParams = array(
3589 'total_amount' => -100,
3594 $compareParams = array(
3595 'total_amount' => 100,
3599 if ($context == 'paymentInstrument') {
3600 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3601 $compareParams['payment_instrument_id'] = $instrumentId;
3604 $compareParams['to_financial_account_id'] = 12;
3606 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3607 $compareParams['total_amount'] = 100;
3610 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3616 public function _addPaymentInstrument() {
3617 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3618 $optionParams = array(
3619 'option_group_id' => $gId,
3620 'label' => 'Test Card',
3621 'name' => 'Test Card',
3626 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3627 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3628 $financialParams = array(
3629 'entity_table' => 'civicrm_option_value',
3630 'entity_id' => $optionValue['id'],
3631 'account_relationship' => $relationTypeId,
3632 'financial_account_id' => 7,
3634 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3635 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3636 return $optionValue['values'][$optionValue['id']]['value'];
3639 public function _deletedAddedPaymentInstrument() {
3640 $result = $this->callAPISuccess('OptionValue', 'get', array(
3641 'option_group_id' => 'payment_instrument',
3642 'name' => 'Test Card',
3646 if ($id = CRM_Utils_Array
::value('id', $result)) {
3647 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3652 * Set up the basic recurring contribution for tests.
3654 * @param array $generalParams
3655 * Parameters that can be merged into the recurring AND the contribution.
3657 * @param array $recurParams
3658 * Parameters to merge into the recur only.
3662 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3663 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3664 'contact_id' => $this->_individualId
,
3665 'installments' => '12',
3666 'frequency_interval' => '1',
3668 'contribution_status_id' => 1,
3669 'start_date' => '2012-01-01 00:00:00',
3670 'currency' => 'USD',
3671 'frequency_unit' => 'month',
3672 'payment_processor_id' => $this->paymentProcessorID
,
3673 ), $generalParams, $recurParams));
3674 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3677 'contribution_recur_id' => $contributionRecur['id'],
3680 return $originalContribution;
3684 * Set up a basic auto-renew membership for tests.
3686 * @param array $generalParams
3687 * Parameters that can be merged into the recurring AND the contribution.
3689 * @param array $recurParams
3690 * Parameters to merge into the recur only.
3694 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3695 $newContact = $this->callAPISuccess('Contact', 'create', array(
3696 'contact_type' => 'Individual',
3697 'sort_name' => 'McTesterson, Testy',
3698 'display_name' => 'Testy McTesterson',
3699 'preferred_language' => 'en_US',
3700 'preferred_mail_format' => 'Both',
3701 'first_name' => 'Testy',
3702 'last_name' => 'McTesterson',
3703 'contact_is_deleted' => '0',
3705 'email' => 'tmctesterson@example.com',
3708 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3709 'domain_id' => "Default Domain Name",
3710 'member_of_contact_id' => 1,
3711 'financial_type_id' => "Member Dues",
3712 'duration_unit' => "month",
3713 'duration_interval' => 1,
3714 'period_type' => 'rolling',
3715 'name' => "Standard Member",
3716 'minimum_fee' => 100,
3718 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3719 'contact_id' => $newContact['id'],
3720 'installments' => '12',
3721 'frequency_interval' => '1',
3723 'contribution_status_id' => 1,
3724 'start_date' => '2012-01-01 00:00:00',
3725 'currency' => 'USD',
3726 'frequency_unit' => 'month',
3727 'payment_processor_id' => $this->paymentProcessorID
,
3728 ), $generalParams, $recurParams));
3730 $membership = $this->callAPISuccess('membership', 'create', array(
3731 'contact_id' => $newContact['id'],
3732 'contribution_recur_id' => $contributionRecur['id'],
3733 'financial_type_id' => "Member Dues",
3734 'membership_type_id' => $membershipType['id'],
3736 'skipLineItem' => TRUE,
3739 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3740 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3743 'contact_id' => $newContact['id'],
3744 'contribution_recur_id' => $contributionRecur['id'],
3745 'financial_type_id' => "Member Dues",
3746 'contribution_status_id' => 1,
3747 'invoice_id' => uniqid(),
3750 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3751 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3752 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3753 $this->callAPISuccess('LineItem', 'getsingle', array());
3754 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3756 return array($originalContribution, $membership);
3759 * Set up a repeat transaction.
3761 * @param array $recurParams
3765 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3766 $paymentProcessorID = $this->paymentProcessorCreate();
3767 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3768 'contact_id' => $this->_individualId
,
3769 'installments' => '12',
3770 'frequency_interval' => '1',
3772 'contribution_status_id' => 1,
3773 'start_date' => '2012-01-01 00:00:00',
3774 'currency' => 'USD',
3775 'frequency_unit' => 'month',
3776 'payment_processor_id' => $paymentProcessorID,
3779 $originalContribution = '';
3780 if ($flag == 'multiple') {
3781 // CRM-19309 create a contribution + also add in line_items (plural):
3782 $params = array_merge($this->_params
, $contributionParams);
3783 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3786 'contribution_recur_id' => $contributionRecur['id'],
3787 'skipLineItem' => 1,
3788 'api.line_item.create' => array(
3790 'price_field_id' => 1,
3792 'line_total' => '20',
3793 'unit_price' => '10',
3794 'financial_type_id' => 1,
3797 'price_field_id' => 1,
3799 'line_total' => '80',
3800 'unit_price' => '80',
3801 'financial_type_id' => 2,
3808 elseif ($flag == 'single') {
3809 $params = array_merge($this->_params
, array('contribution_recur_id' => $contributionRecur['id']));
3810 $params = array_merge($params, $contributionParams);
3811 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3813 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3814 return $originalContribution;
3818 * Common set up routine.
3822 protected function setUpForCompleteTransaction() {
3823 $this->mut
= new CiviMailUtils($this, TRUE);
3824 $this->createLoggedInUser();
3825 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3826 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3827 return $contribution;
3831 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3833 public function testRepeatTransactionWithNonCreditCardDefault() {
3834 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3835 'contact_id' => $this->_individualId
,
3836 'installments' => '12',
3837 'frequency_interval' => '1',
3839 'contribution_status_id' => 1,
3840 'start_date' => '2012-01-01 00:00:00',
3841 'currency' => 'USD',
3842 'frequency_unit' => 'month',
3843 'payment_processor_id' => $this->paymentProcessorID
,
3845 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3847 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3849 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
3850 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3851 'contribution_status_id' => 'Completed',
3852 'trxn_id' => uniqid(),
3853 'original_contribution_id' => $contribution1,
3855 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3856 $this->quickCleanUpFinancialEntities();
3860 * CRM-20008 Tests repeattransaction creates pending membership.
3862 public function testRepeatTransactionPendingMembership() {
3863 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3864 $this->callAPISuccess('membership', 'create', array(
3865 'id' => $membership['id'],
3866 'end_date' => 'yesterday',
3867 'status_id' => 'Expired',
3869 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3870 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3871 'contribution_status_id' => 'Pending',
3872 'trxn_id' => uniqid(),
3874 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3875 'id' => $membership['id'],
3876 'return' => 'status_id',
3879 // Let's see if the membership payments got created while we're at it.
3880 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3881 'memberhip_id' => $membership['id'],
3883 $this->assertEquals(2, $membershipPayments['count']);
3885 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3886 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3887 $membership = $this->callAPISuccessGetSingle('membership', array(
3888 'id' => $membership['id'],
3889 'return' => 'status_id, end_date',
3891 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3892 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3894 $this->quickCleanUpFinancialEntities();
3895 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3899 * Test sending a mail via the API.
3901 public function testSendMailWithAPISetFromDetails() {
3902 $mut = new CiviMailUtils($this, TRUE);
3903 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3904 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3905 'id' => $contribution['id'],
3906 'receipt_from_email' => 'api@civicrm.org',
3907 'receipt_from_name' => 'CiviCRM LLC',
3909 $mut->checkMailLog(array(
3910 'From: CiviCRM LLC <api@civicrm.org>',
3911 'Contribution Information',
3912 'Please print this confirmation for your records',
3921 * Test sending a mail via the API.
3923 public function testSendMailWithNoFromSetFallToDomain() {
3924 $this->createLoggedInUser();
3925 $mut = new CiviMailUtils($this, TRUE);
3926 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3927 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3928 'id' => $contribution['id'],
3930 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3931 $mut->checkMailLog(array(
3932 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3933 'Contribution Information',
3934 'Please print this confirmation for your records',
3943 * Test sending a mail via the API.
3945 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3946 $this->createLoggedInUser();
3947 $mut = new CiviMailUtils($this, TRUE);
3948 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3949 $this->callAPISuccess('contribution', 'repeattransaction', array(
3950 'contribution_status_id' => 'Completed',
3951 'trxn_id' => uniqid(),
3952 'original_contribution_id' => $contribution,
3954 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3955 $mut->checkMailLog(array(
3956 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3957 'Contribution Information',
3958 'Please print this confirmation for your records',
3967 * Test sending a mail via the API.
3969 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3970 $mut = new CiviMailUtils($this, TRUE);
3971 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3972 $paymentProcessorID = $this->paymentProcessorCreate();
3973 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3974 'contact_id' => $this->_individualId
,
3975 'installments' => '12',
3976 'frequency_interval' => '1',
3978 'contribution_status_id' => 1,
3979 'start_date' => '2012-01-01 00:00:00',
3980 'currency' => 'USD',
3981 'frequency_unit' => 'month',
3982 'payment_processor_id' => $paymentProcessorID,
3984 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3987 'contribution_recur_id' => $contributionRecur['id'],
3988 'contribution_page_id' => $contributionPage['id']))
3990 $this->callAPISuccess('contribution', 'repeattransaction', array(
3991 'contribution_status_id' => 'Completed',
3992 'trxn_id' => uniqid(),
3993 'original_contribution_id' => $originalContribution,
3996 $mut->checkMailLog(array(
3997 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3998 'Contribution Information',
3999 'Please print this confirmation for your records',
4008 * Test sending a mail via the API.
4010 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4011 $mut = new CiviMailUtils($this, TRUE);
4012 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
4013 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
4014 foreach ($fromEmail['values'] as $from) {
4015 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
4017 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
4018 $this->callAPISuccess('contribution', 'repeattransaction', array(
4019 'contribution_status_id' => 'Completed',
4020 'trxn_id' => uniqid(),
4021 'original_contribution_id' => $originalContribution,
4024 $mut->checkMailLog(array(
4025 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4026 'Contribution Information',
4027 'Please print this confirmation for your records',
4036 * Create a Contribution Page with is_email_receipt = TRUE.
4038 * @param array $params
4039 * Params to overwrite with.
4043 protected function createReceiptableContributionPage($params = array()) {
4044 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
4045 'receipt_from_name' => 'Mickey Mouse',
4046 'receipt_from_email' => 'mickey@mouse.com',
4047 'title' => "Test Contribution Page",
4048 'financial_type_id' => 1,
4049 'currency' => 'CAD',
4050 'is_monetary' => TRUE,
4051 'is_email_receipt' => TRUE,
4053 return $contributionPage;
4057 * function to test card_type and pan truncation.
4059 public function testCardTypeAndPanTruncation() {
4060 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
4061 $contactId = $this->individualCreate();
4063 'contact_id' => $contactId,
4064 'receive_date' => '2016-01-20',
4065 'total_amount' => 100,
4066 'financial_type_id' => 1,
4067 'payment_instrument' => 'Credit Card',
4068 'card_type_id' => $creditCardTypeIDs['Visa'],
4069 'pan_truncation' => 4567,
4071 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4072 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
4073 $financialTrxn = $this->callAPISuccessGetSingle(
4076 'id' => $lastFinancialTrxnId['financialTrxnId'],
4077 'return' => array('card_type_id', 'pan_truncation'),
4080 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4081 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
4083 'id' => $contribution['id'],
4084 'pan_truncation' => 2345,
4085 'card_type_id' => $creditCardTypeIDs['Amex'],
4087 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4088 $financialTrxn = $this->callAPISuccessGetSingle(
4091 'id' => $lastFinancialTrxnId['financialTrxnId'],
4092 'return' => array('card_type_id', 'pan_truncation'),
4095 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4096 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
4100 * Test repeat contribution uses non default currency
4101 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4103 public function testRepeatTransactionWithDifferenceCurrency() {
4104 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4105 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4106 'original_contribution_id' => $originalContribution['id'],
4107 'contribution_status_id' => 'Completed',
4108 'trxn_id' => uniqid(),
4110 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4114 * Get the financial items for the contribution.
4116 * @param int $contributionID
4119 * Array of associated financial items.
4121 protected function getFinancialTransactionsForContribution($contributionID) {
4122 $trxnParams = array(
4123 'entity_id' => $contributionID,
4124 'entity_table' => 'civicrm_contribution',
4126 // @todo the following function has naming errors & has a weird signature & appears to
4127 // only be called from test classes. Move into test suite & maybe just use api
4128 // from this function.
4129 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));