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 $contributionPage = $this->createPriceSetWithPage('membership');
2948 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2952 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2953 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2954 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2955 $activity = $this->callAPISuccess('Activity', 'get', array(
2956 'activity_type_id' => 'Membership Signup',
2957 'source_record_id' => $this->_ids
['membership'],
2958 'status_id' => 'Scheduled',
2960 $this->assertEquals(1, $activity['count']);
2962 // change pending contribution to completed
2963 $form = new CRM_Contribute_Form_Contribution();
2965 $form->_params
= array(
2966 'id' => $this->_ids
['contribution'],
2967 'total_amount' => 20,
2970 'financial_type_id' => 1,
2971 'contact_id' => $this->_individualId
,
2972 'contribution_status_id' => 1,
2973 'billing_middle_name' => '',
2974 'billing_last_name' => 'Adams',
2975 'billing_street_address-5' => '790L Lincoln St S',
2976 'billing_city-5' => 'Maryknoll',
2977 'billing_state_province_id-5' => 1031,
2978 'billing_postal_code-5' => 10545,
2979 'billing_country_id-5' => 1228,
2980 'frequency_interval' => 1,
2981 'frequency_unit' => 'month',
2982 'installments' => '',
2983 'hidden_AdditionalDetail' => 1,
2984 'hidden_Premium' => 1,
2985 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2986 'receipt_date' => '',
2987 'receipt_date_time' => '',
2988 'payment_processor_id' => $this->paymentProcessorID
,
2989 'currency' => 'USD',
2990 'contribution_page_id' => $this->_ids
['contribution_page'],
2991 'contribution_mode' => 'membership',
2992 'source' => 'Membership Signup and Renewal',
2995 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2997 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
3000 // Case 2: After successful payment for Pending backoffice there are three activities created
3001 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
3002 $activity = $this->callAPISuccess('Activity', 'get', array(
3003 'activity_type_id' => 'Membership Signup',
3004 'source_record_id' => $this->_ids
['membership'],
3005 'status_id' => 'Completed',
3007 $this->assertEquals(1, $activity['count']);
3008 // 2.b Contribution activity created to record successful payment
3009 $activity = $this->callAPISuccess('Activity', 'get', array(
3010 'activity_type_id' => 'Contribution',
3011 'source_record_id' => $this->_ids
['contribution'],
3012 'status_id' => 'Completed',
3014 $this->assertEquals(1, $activity['count']);
3016 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
3017 $activity = $this->callAPISuccess('Activity', 'get', array(
3018 'activity_type_id' => 'Change Membership Status',
3019 'source_record_id' => $this->_ids
['membership'],
3020 'status_id' => 'Completed',
3022 $this->assertEquals(1, $activity['count']);
3023 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
3025 //Create another pending contribution for renewal
3026 $contribution = $this->callAPISuccess('contribution', 'create', array(
3028 'contact_id' => $this->_ids
['contact'],
3029 'receive_date' => date('Ymd'),
3030 'total_amount' => 20.00,
3031 'financial_type_id' => 1,
3032 'payment_instrument_id' => 'Credit Card',
3033 'non_deductible_amount' => 10.00,
3034 'trxn_id' => 'rdhfi88',
3035 'invoice_id' => 'dofhiewuyr',
3037 'contribution_status_id' => 2,
3038 'contribution_page_id' => $this->_ids
['contribution_page'],
3039 'api.membership_payment.create' => array('membership_id' => $this->_ids
['membership']),
3042 $this->callAPISuccess('line_item', 'create', array(
3043 'entity_id' => $contribution['id'],
3044 'entity_table' => 'civicrm_contribution',
3045 'contribution_id' => $contribution['id'],
3046 'price_field_id' => $this->_ids
['price_field'][0],
3050 'financial_type_id' => 1,
3051 'price_field_value_id' => $this->_ids
['price_field_value'][0],
3054 //Update it to Failed.
3055 $form->_params
['id'] = $contribution['id'];
3056 $form->_params
['contribution_status_id'] = 4;
3058 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3060 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
3063 //Existing membership should not get updated to expired.
3064 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3065 $this->assertNotEquals($membership['status_id'], 4);
3069 * Test membership is renewed when transaction completed.
3071 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3072 $this->createPriceSetWithPage('membership');
3073 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
3074 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
3075 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3076 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3077 $this->cleanUpAfterPriceSets();
3080 public function cleanUpAfterPriceSets() {
3081 $this->quickCleanUpFinancialEntities();
3082 $this->contactDelete($this->_ids
['contact']);
3086 * Set up a pending transaction with a specific price field id.
3088 * @param int $priceFieldValueID
3090 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
3091 $contactID = $this->individualCreate();
3092 $membership = $this->callAPISuccess('membership', 'create', array(
3093 'contact_id' => $contactID,
3094 'membership_type_id' => $this->_ids
['membership_type'],
3095 'start_date' => 'yesterday - 1 year',
3096 'end_date' => 'yesterday',
3097 'join_date' => 'yesterday - 1 year',
3099 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3101 'contact_id' => $contactID,
3102 'receive_date' => date('Ymd'),
3103 'total_amount' => 20.00,
3104 'financial_type_id' => 1,
3105 'payment_instrument_id' => 'Credit Card',
3106 'non_deductible_amount' => 10.00,
3107 'trxn_id' => 'jdhfi' . rand(1, 100),
3108 'invoice_id' => 'djfhiew' . rand(5, 100),
3110 'contribution_status_id' => 2,
3111 'contribution_page_id' => $this->_ids
['contribution_page'],
3112 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3115 $this->callAPISuccess('line_item', 'create', array(
3116 'entity_id' => $contribution['id'],
3117 'entity_table' => 'civicrm_contribution',
3118 'contribution_id' => $contribution['id'],
3119 'price_field_id' => $this->_ids
['price_field'][0],
3123 'financial_type_id' => 1,
3124 'price_field_value_id' => $priceFieldValueID,
3126 $this->_ids
['contact'] = $contactID;
3127 $this->_ids
['contribution'] = $contribution['id'];
3128 $this->_ids
['membership'] = $membership['id'];
3132 * Test sending a mail via the API.
3134 public function testSendMail() {
3135 $mut = new CiviMailUtils($this, TRUE);
3136 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3137 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3138 'id' => $contribution['id'],
3139 'receipt_from_email' => 'api@civicrm.org',
3142 $mut->checkMailLog(array(
3144 'Contribution Information',
3145 'Please print this confirmation for your records',
3151 $this->checkCreditCardDetails($mut, $contribution['id']);
3156 * Test sending a mail via the API.
3157 * This simulates webform_civicrm using pay later contribution page
3159 public function testSendconfirmationPayLater() {
3160 $mut = new CiviMailUtils($this, TRUE);
3162 // Create contribution page
3163 $pageParams = array(
3164 'title' => 'Webform Contributions',
3165 'financial_type_id' => 1,
3166 'contribution_type_id' => 1,
3167 'is_confirm_enabled' => 1,
3168 'is_pay_later' => 1,
3169 'pay_later_text' => 'I will send payment by cheque',
3170 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3172 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3174 // Create pay later contribution
3175 $contribParams = array(
3176 'contact_id' => $this->_individualId
,
3177 'financial_type_id' => 1,
3178 'is_pay_later' => 1,
3179 'contribution_status_id' => 2,
3180 'contribution_page_id' => $contributionPage['id'],
3181 'total_amount' => '10.00',
3183 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3186 $lineItemParams = array(
3187 'contribution_id' => $contribution['id'],
3188 'entity_id' => $contribution['id'],
3189 'entity_table' => 'civicrm_contribution',
3190 'label' => 'My lineitem label',
3192 'unit_price' => "10.00",
3193 'line_total' => "10.00",
3195 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3199 civicrm_api3('contribution', 'sendconfirmation', array(
3200 'id' => $contribution['id'],
3201 'receipt_from_email' => 'api@civicrm.org',
3205 catch (Exception
$e) {
3206 // Need to figure out how to stop this some other day
3207 // We don't care about the Payment Processor because this is Pay Later
3208 // The point of this test is to check we get the pay_later version of the mail
3209 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3214 // Retrieve mail & check it has the pay_later_receipt info
3215 $mut->getMostRecentEmail('raw');
3216 $mut->checkMailLog(array(
3217 (string) $contribParams['total_amount'],
3218 $pageParams['pay_later_receipt'],
3228 * Check credit card details in sent mail via API
3230 * @param $mut obj CiviMailUtils instance
3231 * @param int $contributionID Contribution ID
3234 public function checkCreditCardDetails($mut, $contributionID) {
3235 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3236 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3237 'id' => $contributionID,
3238 'receipt_from_email' => 'api@civicrm.org',
3239 'payment_processor_id' => $this->paymentProcessorID
,
3242 $mut->checkMailLog(array(
3243 'Credit Card Information', // credit card header
3244 'Billing Name and Address', // billing header
3245 'anthony_anderson@civicrm.org', // billing name
3253 * Test sending a mail via the API.
3255 public function testSendMailEvent() {
3256 $mut = new CiviMailUtils($this, TRUE);
3257 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3258 $event = $this->eventCreate(array(
3259 'is_email_confirm' => 1,
3260 'confirm_from_email' => 'test@civicrm.org',
3262 $this->_eventID
= $event['id'];
3263 $participantParams = array(
3264 'contact_id' => $this->_individualId
,
3265 'event_id' => $this->_eventID
,
3268 // to ensure it matches later on
3269 'register_date' => '2007-07-21 00:00:00',
3270 'source' => 'Online Event Registration: API Testing',
3273 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3274 $this->callAPISuccess('participant_payment', 'create', array(
3275 'participant_id' => $participant['id'],
3276 'contribution_id' => $contribution['id'],
3278 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3279 'id' => $contribution['id'],
3280 'receipt_from_email' => 'api@civicrm.org',
3284 $mut->checkMailLog(array(
3285 'Annual CiviCRM meet',
3287 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3294 * This function does a GET & compares the result against the $params.
3296 * Use as a double check on Creates.
3298 * @param array $params
3300 * @param bool $delete
3302 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3304 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3310 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3312 $this->assertAPISuccess($contribution, 0);
3313 $values = $contribution['values'][$contribution['id']];
3314 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3315 // this is not returned in id format
3316 unset($params['payment_instrument_id']);
3317 $params['contribution_source'] = $params['source'];
3318 unset($params['source']);
3319 foreach ($params as $key => $value) {
3320 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3325 * Create a pending contribution & linked pending pledge record.
3327 public function createPendingPledgeContribution() {
3329 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
3330 $this->_ids
['pledge'] = $pledgeID;
3331 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
3332 'contribution_status_id' => 'Pending',
3333 'total_amount' => 500,
3336 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3337 'options' => array('limit' => 1),
3340 $this->callAPISuccess('PledgePayment', 'create', array(
3342 'contribution_id' =>
3343 $contribution['id'],
3344 'status_id' => 'Pending',
3345 'scheduled_amount' => 500,
3348 return $contribution['id'];
3352 * Create a pending contribution & linked pending participant record (along with an event).
3354 public function createPendingParticipantContribution() {
3355 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3356 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId
));
3357 $this->_ids
['participant'] = $participantID;
3358 $params = array_merge($this->_params
, array('contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3359 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3360 $this->callAPISuccess('participant_payment', 'create', array(
3361 'contribution_id' => $contribution['id'],
3362 'participant_id' => $participantID,
3364 $this->callAPISuccess('line_item', 'get', array(
3365 'entity_id' => $contribution['id'],
3366 'entity_table' => 'civicrm_contribution',
3367 'api.line_item.create' => array(
3368 'entity_id' => $participantID,
3369 'entity_table' => 'civicrm_participant',
3372 return $contribution['id'];
3376 * Get financial transaction amount.
3378 * @param int $contId
3380 * @return null|string
3382 public function _getFinancialTrxnAmount($contId) {
3384 SUM( ft.total_amount ) AS total
3385 FROM civicrm_financial_trxn AS ft
3386 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3387 WHERE ceft.entity_table = 'civicrm_contribution'
3388 AND ceft.entity_id = {$contId}";
3390 $result = CRM_Core_DAO
::singleValueQuery($query);
3395 * @param int $contId
3397 * @return null|string
3399 public function _getFinancialItemAmount($contId) {
3400 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3403 FROM civicrm_financial_item
3404 WHERE entity_table = 'civicrm_line_item'
3405 AND entity_id = {$lineItem}";
3406 $result = CRM_Core_DAO
::singleValueQuery($query);
3411 * @param int $contId
3414 public function _checkFinancialItem($contId, $context) {
3415 if ($context != 'paylater') {
3417 'entity_id' => $contId,
3418 'entity_table' => 'civicrm_contribution',
3420 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3421 $entityParams = array(
3422 'financial_trxn_id' => $trxn['financial_trxn_id'],
3423 'entity_table' => 'civicrm_financial_item',
3425 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3427 'id' => $entityTrxn['entity_id'],
3430 if ($context == 'paylater') {
3431 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3432 foreach ($lineItems as $key => $item) {
3434 'entity_id' => $key,
3435 'entity_table' => 'civicrm_line_item',
3437 $compareParams = array('status_id' => 1);
3438 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3441 elseif ($context == 'refund') {
3442 $compareParams = array(
3444 'financial_account_id' => 1,
3448 elseif ($context == 'cancelPending') {
3449 $compareParams = array(
3451 'financial_account_id' => 1,
3455 elseif ($context == 'changeFinancial') {
3456 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3458 'entity_id' => $lineKey,
3461 $compareParams = array(
3462 'financial_account_id' => 1,
3464 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3466 'financial_account_id' => 3,
3467 'entity_id' => $lineKey,
3469 $compareParams = array(
3473 if ($context != 'paylater') {
3474 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3479 * Check correct financial transaction entries were created for the change in payment instrument.
3481 * @param int $contributionID
3482 * @param int $originalInstrumentID
3483 * @param int $newInstrumentID
3485 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3487 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3489 $originalTrxnParams = array(
3490 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3491 'payment_instrument_id' => $originalInstrumentID,
3492 'amount' => $amount,
3496 $reversalTrxnParams = array(
3497 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3498 'payment_instrument_id' => $originalInstrumentID,
3499 'amount' => -$amount,
3503 $newTrxnParams = array(
3504 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3505 'payment_instrument_id' => $newInstrumentID,
3506 'amount' => $amount,
3510 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3511 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3512 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3514 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3515 'id' => $entityFinancialTrxn['financial_trxn_id'],
3517 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3518 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3519 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3520 $this->assertEquals(0, $financialTrxn['fee_amount']);
3521 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3522 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3525 $this->assertEquals(1, $financialTrxn['is_payment']);
3526 $this->assertEquals('USD', $financialTrxn['currency']);
3527 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3532 * Check financial transaction.
3534 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3536 * @param array $contribution
3537 * @param string $context
3538 * @param int $instrumentId
3539 * @param array $extraParams
3541 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3542 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3543 $trxn = array_pop($financialTrxns);
3546 'id' => $trxn['financial_trxn_id'],
3548 if ($context == 'payLater') {
3549 $compareParams = array(
3551 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3554 elseif ($context == 'refund') {
3555 $compareParams = array(
3556 'to_financial_account_id' => 6,
3557 'total_amount' => -100,
3559 'trxn_date' => '2015-01-01 09:00:00',
3560 'trxn_id' => 'the refund',
3563 elseif ($context == 'cancelPending') {
3564 $compareParams = array(
3565 'to_financial_account_id' => 7,
3566 'total_amount' => -100,
3570 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3571 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3572 // It does the same thing with greater readability.
3573 // @todo remove handling for
3575 $entityParams = array(
3576 'entity_id' => $contribution['id'],
3577 'entity_table' => 'civicrm_contribution',
3580 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3581 $trxnParams1 = array(
3582 'id' => $trxn['financial_trxn_id'],
3584 if (empty($extraParams)) {
3585 $compareParams = array(
3586 'total_amount' => -100,
3591 $compareParams = array(
3592 'total_amount' => 100,
3596 if ($context == 'paymentInstrument') {
3597 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3598 $compareParams['payment_instrument_id'] = $instrumentId;
3601 $compareParams['to_financial_account_id'] = 12;
3603 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3604 $compareParams['total_amount'] = 100;
3607 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3613 public function _addPaymentInstrument() {
3614 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3615 $optionParams = array(
3616 'option_group_id' => $gId,
3617 'label' => 'Test Card',
3618 'name' => 'Test Card',
3623 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3624 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3625 $financialParams = array(
3626 'entity_table' => 'civicrm_option_value',
3627 'entity_id' => $optionValue['id'],
3628 'account_relationship' => $relationTypeId,
3629 'financial_account_id' => 7,
3631 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3632 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3633 return $optionValue['values'][$optionValue['id']]['value'];
3636 public function _deletedAddedPaymentInstrument() {
3637 $result = $this->callAPISuccess('OptionValue', 'get', array(
3638 'option_group_id' => 'payment_instrument',
3639 'name' => 'Test Card',
3643 if ($id = CRM_Utils_Array
::value('id', $result)) {
3644 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3649 * Set up the basic recurring contribution for tests.
3651 * @param array $generalParams
3652 * Parameters that can be merged into the recurring AND the contribution.
3654 * @param array $recurParams
3655 * Parameters to merge into the recur only.
3659 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3660 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3661 'contact_id' => $this->_individualId
,
3662 'installments' => '12',
3663 'frequency_interval' => '1',
3665 'contribution_status_id' => 1,
3666 'start_date' => '2012-01-01 00:00:00',
3667 'currency' => 'USD',
3668 'frequency_unit' => 'month',
3669 'payment_processor_id' => $this->paymentProcessorID
,
3670 ), $generalParams, $recurParams));
3671 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3674 'contribution_recur_id' => $contributionRecur['id'],
3677 return $originalContribution;
3681 * Set up a basic auto-renew membership for tests.
3683 * @param array $generalParams
3684 * Parameters that can be merged into the recurring AND the contribution.
3686 * @param array $recurParams
3687 * Parameters to merge into the recur only.
3691 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3692 $newContact = $this->callAPISuccess('Contact', 'create', array(
3693 'contact_type' => 'Individual',
3694 'sort_name' => 'McTesterson, Testy',
3695 'display_name' => 'Testy McTesterson',
3696 'preferred_language' => 'en_US',
3697 'preferred_mail_format' => 'Both',
3698 'first_name' => 'Testy',
3699 'last_name' => 'McTesterson',
3700 'contact_is_deleted' => '0',
3702 'email' => 'tmctesterson@example.com',
3705 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3706 'domain_id' => "Default Domain Name",
3707 'member_of_contact_id' => 1,
3708 'financial_type_id' => "Member Dues",
3709 'duration_unit' => "month",
3710 'duration_interval' => 1,
3711 'period_type' => 'rolling',
3712 'name' => "Standard Member",
3713 'minimum_fee' => 100,
3715 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3716 'contact_id' => $newContact['id'],
3717 'installments' => '12',
3718 'frequency_interval' => '1',
3720 'contribution_status_id' => 1,
3721 'start_date' => '2012-01-01 00:00:00',
3722 'currency' => 'USD',
3723 'frequency_unit' => 'month',
3724 'payment_processor_id' => $this->paymentProcessorID
,
3725 ), $generalParams, $recurParams));
3727 $membership = $this->callAPISuccess('membership', 'create', array(
3728 'contact_id' => $newContact['id'],
3729 'contribution_recur_id' => $contributionRecur['id'],
3730 'financial_type_id' => "Member Dues",
3731 'membership_type_id' => $membershipType['id'],
3733 'skipLineItem' => TRUE,
3736 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3737 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3740 'contact_id' => $newContact['id'],
3741 'contribution_recur_id' => $contributionRecur['id'],
3742 'financial_type_id' => "Member Dues",
3743 'contribution_status_id' => 1,
3744 'invoice_id' => uniqid(),
3747 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3748 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3749 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3750 $this->callAPISuccess('LineItem', 'getsingle', array());
3751 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3753 return array($originalContribution, $membership);
3756 * Set up a repeat transaction.
3758 * @param array $recurParams
3762 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3763 $paymentProcessorID = $this->paymentProcessorCreate();
3764 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3765 'contact_id' => $this->_individualId
,
3766 'installments' => '12',
3767 'frequency_interval' => '1',
3769 'contribution_status_id' => 1,
3770 'start_date' => '2012-01-01 00:00:00',
3771 'currency' => 'USD',
3772 'frequency_unit' => 'month',
3773 'payment_processor_id' => $paymentProcessorID,
3776 $originalContribution = '';
3777 if ($flag == 'multiple') {
3778 // CRM-19309 create a contribution + also add in line_items (plural):
3779 $params = array_merge($this->_params
, $contributionParams);
3780 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3783 'contribution_recur_id' => $contributionRecur['id'],
3784 'skipLineItem' => 1,
3785 'api.line_item.create' => array(
3787 'price_field_id' => 1,
3789 'line_total' => '20',
3790 'unit_price' => '10',
3791 'financial_type_id' => 1,
3794 'price_field_id' => 1,
3796 'line_total' => '80',
3797 'unit_price' => '80',
3798 'financial_type_id' => 2,
3805 elseif ($flag == 'single') {
3806 $params = array_merge($this->_params
, array('contribution_recur_id' => $contributionRecur['id']));
3807 $params = array_merge($params, $contributionParams);
3808 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3810 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3811 return $originalContribution;
3815 * Common set up routine.
3819 protected function setUpForCompleteTransaction() {
3820 $this->mut
= new CiviMailUtils($this, TRUE);
3821 $this->createLoggedInUser();
3822 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3823 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3824 return $contribution;
3828 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3830 public function testRepeatTransactionWithNonCreditCardDefault() {
3831 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3832 'contact_id' => $this->_individualId
,
3833 'installments' => '12',
3834 'frequency_interval' => '1',
3836 'contribution_status_id' => 1,
3837 'start_date' => '2012-01-01 00:00:00',
3838 'currency' => 'USD',
3839 'frequency_unit' => 'month',
3840 'payment_processor_id' => $this->paymentProcessorID
,
3842 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3844 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3846 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
3847 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3848 'contribution_status_id' => 'Completed',
3849 'trxn_id' => uniqid(),
3850 'original_contribution_id' => $contribution1,
3852 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3853 $this->quickCleanUpFinancialEntities();
3857 * CRM-20008 Tests repeattransaction creates pending membership.
3859 public function testRepeatTransactionPendingMembership() {
3860 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3861 $this->callAPISuccess('membership', 'create', array(
3862 'id' => $membership['id'],
3863 'end_date' => 'yesterday',
3864 'status_id' => 'Expired',
3866 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3867 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3868 'contribution_status_id' => 'Pending',
3869 'trxn_id' => uniqid(),
3871 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3872 'id' => $membership['id'],
3873 'return' => 'status_id',
3876 // Let's see if the membership payments got created while we're at it.
3877 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3878 'memberhip_id' => $membership['id'],
3880 $this->assertEquals(2, $membershipPayments['count']);
3882 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3883 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3884 $membership = $this->callAPISuccessGetSingle('membership', array(
3885 'id' => $membership['id'],
3886 'return' => 'status_id, end_date',
3888 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3889 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3891 $this->quickCleanUpFinancialEntities();
3892 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3896 * Test sending a mail via the API.
3898 public function testSendMailWithAPISetFromDetails() {
3899 $mut = new CiviMailUtils($this, TRUE);
3900 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3901 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3902 'id' => $contribution['id'],
3903 'receipt_from_email' => 'api@civicrm.org',
3904 'receipt_from_name' => 'CiviCRM LLC',
3906 $mut->checkMailLog(array(
3907 'From: CiviCRM LLC <api@civicrm.org>',
3908 'Contribution Information',
3909 'Please print this confirmation for your records',
3918 * Test sending a mail via the API.
3920 public function testSendMailWithNoFromSetFallToDomain() {
3921 $this->createLoggedInUser();
3922 $mut = new CiviMailUtils($this, TRUE);
3923 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3924 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3925 'id' => $contribution['id'],
3927 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3928 $mut->checkMailLog(array(
3929 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3930 'Contribution Information',
3931 'Please print this confirmation for your records',
3940 * Test sending a mail via the API.
3942 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3943 $this->createLoggedInUser();
3944 $mut = new CiviMailUtils($this, TRUE);
3945 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3946 $this->callAPISuccess('contribution', 'repeattransaction', array(
3947 'contribution_status_id' => 'Completed',
3948 'trxn_id' => uniqid(),
3949 'original_contribution_id' => $contribution,
3951 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3952 $mut->checkMailLog(array(
3953 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3954 'Contribution Information',
3955 'Please print this confirmation for your records',
3964 * Test sending a mail via the API.
3966 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3967 $mut = new CiviMailUtils($this, TRUE);
3968 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3969 $paymentProcessorID = $this->paymentProcessorCreate();
3970 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3971 'contact_id' => $this->_individualId
,
3972 'installments' => '12',
3973 'frequency_interval' => '1',
3975 'contribution_status_id' => 1,
3976 'start_date' => '2012-01-01 00:00:00',
3977 'currency' => 'USD',
3978 'frequency_unit' => 'month',
3979 'payment_processor_id' => $paymentProcessorID,
3981 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3984 'contribution_recur_id' => $contributionRecur['id'],
3985 'contribution_page_id' => $contributionPage['id']))
3987 $this->callAPISuccess('contribution', 'repeattransaction', array(
3988 'contribution_status_id' => 'Completed',
3989 'trxn_id' => uniqid(),
3990 'original_contribution_id' => $originalContribution,
3993 $mut->checkMailLog(array(
3994 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3995 'Contribution Information',
3996 'Please print this confirmation for your records',
4005 * Test sending a mail via the API.
4007 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4008 $mut = new CiviMailUtils($this, TRUE);
4009 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
4010 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
4011 foreach ($fromEmail['values'] as $from) {
4012 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
4014 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
4015 $this->callAPISuccess('contribution', 'repeattransaction', array(
4016 'contribution_status_id' => 'Completed',
4017 'trxn_id' => uniqid(),
4018 'original_contribution_id' => $originalContribution,
4021 $mut->checkMailLog(array(
4022 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4023 'Contribution Information',
4024 'Please print this confirmation for your records',
4033 * Create a Contribution Page with is_email_receipt = TRUE.
4035 * @param array $params
4036 * Params to overwrite with.
4040 protected function createReceiptableContributionPage($params = array()) {
4041 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
4042 'receipt_from_name' => 'Mickey Mouse',
4043 'receipt_from_email' => 'mickey@mouse.com',
4044 'title' => "Test Contribution Page",
4045 'financial_type_id' => 1,
4046 'currency' => 'CAD',
4047 'is_monetary' => TRUE,
4048 'is_email_receipt' => TRUE,
4050 return $contributionPage;
4054 * function to test card_type and pan truncation.
4056 public function testCardTypeAndPanTruncation() {
4057 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
4058 $contactId = $this->individualCreate();
4060 'contact_id' => $contactId,
4061 'receive_date' => '2016-01-20',
4062 'total_amount' => 100,
4063 'financial_type_id' => 1,
4064 'payment_instrument' => 'Credit Card',
4065 'card_type_id' => $creditCardTypeIDs['Visa'],
4066 'pan_truncation' => 4567,
4068 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4069 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
4070 $financialTrxn = $this->callAPISuccessGetSingle(
4073 'id' => $lastFinancialTrxnId['financialTrxnId'],
4074 'return' => array('card_type_id', 'pan_truncation'),
4077 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4078 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
4080 'id' => $contribution['id'],
4081 'pan_truncation' => 2345,
4082 'card_type_id' => $creditCardTypeIDs['Amex'],
4084 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4085 $financialTrxn = $this->callAPISuccessGetSingle(
4088 'id' => $lastFinancialTrxnId['financialTrxnId'],
4089 'return' => array('card_type_id', 'pan_truncation'),
4092 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4093 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
4097 * Test repeat contribution uses non default currency
4098 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4100 public function testRepeatTransactionWithDifferenceCurrency() {
4101 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4102 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4103 'original_contribution_id' => $originalContribution['id'],
4104 'contribution_status_id' => 'Completed',
4105 'trxn_id' => uniqid(),
4107 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4111 * Get the financial items for the contribution.
4113 * @param int $contributionID
4116 * Array of associated financial items.
4118 protected function getFinancialTransactionsForContribution($contributionID) {
4119 $trxnParams = array(
4120 'entity_id' => $contributionID,
4121 'entity_table' => 'civicrm_contribution',
4123 // @todo the following function has naming errors & has a weird signature & appears to
4124 // only be called from test classes. Move into test suite & maybe just use api
4125 // from this function.
4126 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));