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);
698 $params['skipCleanMoney'] = FALSE;
700 $contribution = $this->callAPISuccess('contribution', 'create', $params);
701 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
702 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
703 $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77));
704 $this->assertEquals(.77, $contribution['fee_amount']);
705 $this->assertEquals(5000, $contribution['net_amount']);
709 * Create test with unique field name on source.
711 public function testCreateContributionNullOutThankyouDate() {
713 $params = $this->_params
;
714 $params['thankyou_date'] = 'yesterday';
716 $contribution = $this->callAPISuccess('contribution', 'create', $params);
717 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
718 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
720 $params['thankyou_date'] = 'null';
721 $contribution = $this->callAPISuccess('contribution', 'create', $params);
722 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
726 * Create test with unique field name on source.
728 public function testCreateContributionSourceInvalidContact() {
732 'receive_date' => date('Ymd'),
733 'total_amount' => 100.00,
734 'financial_type_id' => $this->_financialTypeId
,
735 'payment_instrument_id' => 1,
736 'non_deductible_amount' => 10.00,
737 'fee_amount' => 50.00,
738 'net_amount' => 90.00,
740 'invoice_id' => 67890,
741 'contribution_source' => 'SSF',
742 'contribution_status_id' => 1,
745 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
748 public function testCreateContributionSourceInvalidContContact() {
751 'contribution_contact_id' => 999,
752 'receive_date' => date('Ymd'),
753 'total_amount' => 100.00,
754 'financial_type_id' => $this->_financialTypeId
,
755 'payment_instrument_id' => 1,
756 'non_deductible_amount' => 10.00,
757 'fee_amount' => 50.00,
758 'net_amount' => 90.00,
760 'invoice_id' => 67890,
761 'contribution_source' => 'SSF',
762 'contribution_status_id' => 1,
765 $this->callAPIFailure('contribution', 'create', $params);
769 * Test note created correctly.
771 public function testCreateContributionWithNote() {
772 $description = "Demonstrates creating contribution with Note Entity.";
773 $subfile = "ContributionCreateWithNote";
775 'contact_id' => $this->_individualId
,
776 'receive_date' => '2012-01-01',
777 'total_amount' => 100.00,
778 'financial_type_id' => $this->_financialTypeId
,
779 'payment_instrument_id' => 1,
780 'non_deductible_amount' => 10.00,
781 'fee_amount' => 50.00,
782 'net_amount' => 90.00,
784 'invoice_id' => 67890,
786 'contribution_status_id' => 1,
787 'note' => 'my contribution note',
790 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
791 $result = $this->callAPISuccess('note', 'get', array(
792 'entity_table' => 'civicrm_contribution',
793 'entity_id' => $contribution['id'],
796 $this->assertEquals('my contribution note', $result['values'][0]['note']);
797 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
800 public function testCreateContributionWithNoteUniqueNameAliases() {
802 'contact_id' => $this->_individualId
,
803 'receive_date' => '2012-01-01',
804 'total_amount' => 100.00,
805 'financial_type_id' => $this->_financialTypeId
,
806 'payment_instrument_id' => 1,
807 'non_deductible_amount' => 10.00,
808 'fee_amount' => 50.00,
809 'net_amount' => 90.00,
811 'invoice_id' => 67890,
813 'contribution_status_id' => 1,
814 'contribution_note' => 'my contribution note',
817 $contribution = $this->callAPISuccess('contribution', 'create', $params);
818 $result = $this->callAPISuccess('note', 'get', array(
819 'entity_table' => 'civicrm_contribution',
820 'entity_id' => $contribution['id'],
823 $this->assertEquals('my contribution note', $result['values'][0]['note']);
824 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
828 * This is the test for creating soft credits.
830 public function testCreateContributionWithSoftCredit() {
831 $description = "Demonstrates creating contribution with SoftCredit.";
832 $subfile = "ContributionCreateWithSoftCredit";
833 $contact2 = $this->callAPISuccess('Contact', 'create', array(
834 'display_name' => 'superman',
835 'contact_type' => 'Individual',
838 'contact_id' => $contact2['id'],
840 'soft_credit_type_id' => 3,
843 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
844 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
845 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
847 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
848 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
849 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
851 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
852 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
855 public function testCreateContributionWithSoftCreditDefaults() {
856 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
857 $subfile = "ContributionCreateWithSoftCreditDefaults";
858 $contact2 = $this->callAPISuccess('Contact', 'create', array(
859 'display_name' => 'superman',
860 'contact_type' => 'Individual',
862 $params = $this->_params +
array(
863 'soft_credit_to' => $contact2['id'],
865 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
866 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
868 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
869 // Default soft credit amount = contribution.total_amount
870 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
871 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
873 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
874 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
878 * Test creating contribution with Soft Credit by passing in honor_contact_id.
880 public function testCreateContributionWithHonoreeContact() {
881 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
882 $subfile = "ContributionCreateWithHonoreeContact";
883 $contact2 = $this->callAPISuccess('Contact', 'create', array(
884 'display_name' => 'superman',
885 'contact_type' => 'Individual',
887 $params = $this->_params +
array(
888 'honor_contact_id' => $contact2['id'],
890 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
891 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
893 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
894 // Default soft credit amount = contribution.total_amount
895 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
896 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
897 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
898 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
900 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
901 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
905 * Test using example code.
907 public function testContributionCreateExample() {
908 //make sure at least on page exists since there is a truncate in tear down
909 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
910 require_once 'api/v3/examples/Contribution/Create.php';
911 $result = contribution_create_example();
913 $expectedResult = contribution_create_expectedresult();
914 $this->checkArrayEquals($expectedResult, $result);
915 $this->contributionDelete($id);
919 * Function tests that additional financial records are created when fee amount is recorded.
921 public function testCreateContributionWithFee() {
923 'contact_id' => $this->_individualId
,
924 'receive_date' => '20120511',
925 'total_amount' => 100.00,
927 'financial_type_id' => 1,
929 'invoice_id' => 67890,
931 'contribution_status_id' => 1,
934 $contribution = $this->callAPISuccess('contribution', 'create', $params);
935 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
936 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
937 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
938 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
939 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
940 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
941 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
942 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
943 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
945 $lineItems = $this->callAPISuccess('line_item', 'get', array(
947 'entity_id' => $contribution['id'],
948 'entity_table' => 'civicrm_contribution',
951 $this->assertEquals(1, $lineItems['count']);
952 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
953 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
954 $lineItems = $this->callAPISuccess('line_item', 'get', array(
956 'entity_id' => $contribution['id'],
957 'contribution_id' => $contribution['id'],
958 'entity_table' => 'civicrm_contribution',
961 $this->assertEquals(1, $lineItems['count']);
962 $this->_checkFinancialRecords($contribution, 'feeAmount');
967 * Function tests that additional financial records are created when online contribution is created.
969 public function testCreateContributionOnline() {
970 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
971 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
972 $this->assertAPISuccess($contributionPage);
974 'contact_id' => $this->_individualId
,
975 'receive_date' => '20120511',
976 'total_amount' => 100.00,
977 'financial_type_id' => 1,
978 'contribution_page_id' => $contributionPage['id'],
979 'payment_processor' => $this->paymentProcessorID
,
981 'invoice_id' => 67890,
983 'contribution_status_id' => 1,
987 $contribution = $this->callAPISuccess('contribution', 'create', $params);
988 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
989 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
990 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
991 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
992 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
993 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
994 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
995 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
996 'id' => $this->paymentProcessorID
,
997 'return' => 'payment_instrument_id',
999 $this->_checkFinancialRecords($contribution, 'online');
1003 * Check handling of financial type.
1005 * In the interests of removing financial type / contribution type checks from
1006 * legacy format function lets test that the api is doing this for us
1008 public function testCreateInvalidFinancialType() {
1009 $params = $this->_params
;
1010 $params['financial_type_id'] = 99999;
1011 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
1015 * Check handling of financial type.
1017 * In the interests of removing financial type / contribution type checks from
1018 * legacy format function lets test that the api is doing this for us
1020 public function testValidNamedFinancialType() {
1021 $params = $this->_params
;
1022 $params['financial_type_id'] = 'Donation';
1023 $this->callAPISuccess($this->_entity
, 'create', $params);
1027 * Tests that additional financial records are created.
1029 * Checks when online contribution with pay later option is created
1031 public function testCreateContributionPayLaterOnline() {
1032 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1033 $this->_pageParams
['is_pay_later'] = 1;
1034 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1035 $this->assertAPISuccess($contributionPage);
1037 'contact_id' => $this->_individualId
,
1038 'receive_date' => '20120511',
1039 'total_amount' => 100.00,
1040 'financial_type_id' => 1,
1041 'contribution_page_id' => $contributionPage['id'],
1043 'is_pay_later' => 1,
1044 'invoice_id' => 67890,
1046 'contribution_status_id' => 2,
1050 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1051 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1052 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1053 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1054 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1055 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1056 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1057 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1058 $this->_checkFinancialRecords($contribution, 'payLater');
1062 * Function tests that additional financial records are created for online contribution with pending option.
1064 public function testCreateContributionPendingOnline() {
1065 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1066 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1067 $this->assertAPISuccess($contributionPage);
1069 'contact_id' => $this->_individualId
,
1070 'receive_date' => '20120511',
1071 'total_amount' => 100.00,
1072 'financial_type_id' => 1,
1073 'contribution_page_id' => $contributionPage['id'],
1075 'invoice_id' => 67890,
1077 'contribution_status_id' => 2,
1080 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1081 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1082 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1083 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1084 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1085 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1086 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1087 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1088 $this->_checkFinancialRecords($contribution, 'pending');
1092 * Test that BAO defaults work.
1094 public function testCreateBAODefaults() {
1095 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1096 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1097 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1098 'id' => $contribution['id'],
1099 'api.contribution.delete' => 1,
1101 $this->assertEquals(1, $contribution['contribution_status_id']);
1102 $this->assertEquals('Check', $contribution['payment_instrument']);
1106 * Function tests that line items, financial records are updated when contribution amount is changed.
1108 public function testCreateUpdateContributionChangeTotal() {
1109 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1110 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1112 'entity_id' => $contribution['id'],
1113 'entity_table' => 'civicrm_contribution',
1115 'return' => 'line_total',
1117 $this->assertEquals('100.00', $lineItems);
1118 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1119 // Financial trxn SUM = 100 + 5 (fee)
1120 $this->assertEquals('105.00', $trxnAmount);
1123 'id' => $contribution['id'],
1124 'total_amount' => '125',
1126 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1128 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1130 'entity_id' => $contribution['id'],
1131 'entity_table' => 'civicrm_contribution',
1133 'return' => 'line_total',
1136 $this->assertEquals('125.00', $lineItems);
1137 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1139 // Financial trxn SUM = 125 + 5 (fee).
1140 $this->assertEquals('130.00', $trxnAmount);
1141 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1145 * Function tests that line items, financial records are updated when pay later contribution is received.
1147 public function testCreateUpdateContributionPayLater() {
1148 $contribParams = array(
1149 'contact_id' => $this->_individualId
,
1150 'receive_date' => '2012-01-01',
1151 'total_amount' => 100.00,
1152 'financial_type_id' => $this->_financialTypeId
,
1153 'payment_instrument_id' => 1,
1154 'contribution_status_id' => 2,
1155 'is_pay_later' => 1,
1158 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1160 $newParams = array_merge($contribParams, array(
1161 'id' => $contribution['id'],
1162 'contribution_status_id' => 1,
1165 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1166 $contribution = $contribution['values'][$contribution['id']];
1167 $this->assertEquals($contribution['contribution_status_id'], '1');
1168 $this->_checkFinancialItem($contribution['id'], 'paylater');
1169 $this->_checkFinancialTrxn($contribution, 'payLater');
1173 * Function tests that financial records are updated when Payment Instrument is changed.
1175 public function testCreateUpdateContributionPaymentInstrument() {
1176 $instrumentId = $this->_addPaymentInstrument();
1177 $contribParams = array(
1178 'contact_id' => $this->_individualId
,
1179 'total_amount' => 100.00,
1180 'financial_type_id' => $this->_financialTypeId
,
1181 'payment_instrument_id' => 4,
1182 'contribution_status_id' => 1,
1185 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1187 $newParams = array_merge($contribParams, array(
1188 'id' => $contribution['id'],
1189 'payment_instrument_id' => $instrumentId,
1192 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1193 $this->assertAPISuccess($contribution);
1194 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1196 // cleanup - delete created payment instrument
1197 $this->_deletedAddedPaymentInstrument();
1201 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1203 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1204 $instrumentId = $this->_addPaymentInstrument();
1205 $contribParams = array(
1206 'contact_id' => $this->_individualId
,
1207 'total_amount' => -100.00,
1208 'financial_type_id' => $this->_financialTypeId
,
1209 'payment_instrument_id' => 4,
1210 'contribution_status_id' => 1,
1213 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1215 $newParams = array_merge($contribParams, array(
1216 'id' => $contribution['id'],
1217 'payment_instrument_id' => $instrumentId,
1220 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1221 $this->assertAPISuccess($contribution);
1222 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1224 // cleanup - delete created payment instrument
1225 $this->_deletedAddedPaymentInstrument();
1229 * Function tests that financial records are added when Contribution is Refunded.
1231 public function testCreateUpdateContributionRefund() {
1232 $contributionParams = array(
1233 'contact_id' => $this->_individualId
,
1234 'receive_date' => '2012-01-01',
1235 'total_amount' => 100.00,
1236 'financial_type_id' => $this->_financialTypeId
,
1237 'payment_instrument_id' => 4,
1238 'contribution_status_id' => 1,
1239 'trxn_id' => 'original_payment',
1241 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1242 $newParams = array_merge($contributionParams, array(
1243 'id' => $contribution['id'],
1244 'contribution_status_id' => 'Refunded',
1245 'cancel_date' => '2015-01-01 09:00',
1246 'refund_trxn_id' => 'the refund',
1250 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1251 $this->_checkFinancialTrxn($contribution, 'refund');
1252 $this->_checkFinancialItem($contribution['id'], 'refund');
1253 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1254 'id' => $contribution['id'],
1255 'return' => 'trxn_id',
1260 * Refund a contribution for a financial type with a contra account.
1262 * CRM-17951 the contra account is a financial account with a relationship to a
1263 * financial type. It is not always configured but should be reflected
1264 * in the financial_trxn & financial_item table if it is.
1266 public function testCreateUpdateChargebackContributionDefaultAccount() {
1267 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1268 $this->callAPISuccess('Contribution', 'create', array(
1269 'id' => $contribution['id'],
1270 'contribution_status_id' => 'Chargeback',
1272 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1274 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1275 'contribution_id' => $contribution['id'],
1276 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1278 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1279 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1280 'total_amount' => -100,
1281 'status_id' => 'Chargeback',
1282 'to_financial_account_id' => 6,
1287 * Refund a contribution for a financial type with a contra account.
1289 * CRM-17951 the contra account is a financial account with a relationship to a
1290 * financial type. It is not always configured but should be reflected
1291 * in the financial_trxn & financial_item table if it is.
1293 public function testCreateUpdateChargebackContributionCustomAccount() {
1294 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1295 'name' => 'Chargeback Account',
1296 'is_active' => TRUE,
1299 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1300 'entity_id' => $this->_financialTypeId
,
1301 'entity_table' => 'civicrm_financial_type',
1302 'account_relationship' => 'Chargeback Account is',
1303 'financial_account_id' => 'Chargeback Account',
1306 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1307 $this->callAPISuccess('Contribution', 'create', array(
1308 'id' => $contribution['id'],
1309 'contribution_status_id' => 'Chargeback',
1311 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1313 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1314 'contribution_id' => $contribution['id'],
1315 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1317 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1319 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1320 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1321 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1325 * Refund a contribution for a financial type with a contra account.
1327 * CRM-17951 the contra account is a financial account with a relationship to a
1328 * financial type. It is not always configured but should be reflected
1329 * in the financial_trxn & financial_item table if it is.
1331 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1332 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1333 'name' => 'Refund Account',
1334 'is_active' => TRUE,
1337 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1338 'entity_id' => $this->_financialTypeId
,
1339 'entity_table' => 'civicrm_financial_type',
1340 'account_relationship' => 'Credit/Contra Revenue Account is',
1341 'financial_account_id' => 'Refund Account',
1344 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1345 $this->callAPISuccess('Contribution', 'create', array(
1346 'id' => $contribution['id'],
1347 'contribution_status_id' => 'Refunded',
1350 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1351 'contribution_id' => $contribution['id'],
1352 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1354 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1356 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1357 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1358 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1362 * Function tests that trxn_id is set when passed in.
1364 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1365 * when trxn_id is passed in.
1367 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1368 $contributionParams = array(
1369 'contact_id' => $this->_individualId
,
1370 'receive_date' => '2012-01-01',
1371 'total_amount' => 100.00,
1372 'financial_type_id' => $this->_financialTypeId
,
1373 'payment_instrument_id' => 4,
1374 'contribution_status_id' => 1,
1375 'trxn_id' => 'original_payment',
1377 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1378 $newParams = array_merge($contributionParams, array(
1379 'id' => $contribution['id'],
1380 'contribution_status_id' => 'Refunded',
1381 'cancel_date' => '2015-01-01 09:00',
1382 'trxn_id' => 'the refund',
1386 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1387 $this->_checkFinancialTrxn($contribution, 'refund');
1388 $this->_checkFinancialItem($contribution['id'], 'refund');
1389 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1390 'id' => $contribution['id'],
1391 'return' => 'trxn_id',
1396 * Function tests that trxn_id is set when passed in.
1398 * Here we ensure that the civicrm_contribution.trxn_id is set
1399 * when trxn_id is passed in but if refund_trxn_id is different then that
1400 * is kept for the refund transaction.
1402 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1403 $contributionParams = array(
1404 'contact_id' => $this->_individualId
,
1405 'receive_date' => '2012-01-01',
1406 'total_amount' => 100.00,
1407 'financial_type_id' => $this->_financialTypeId
,
1408 'payment_instrument_id' => 4,
1409 'contribution_status_id' => 1,
1410 'trxn_id' => 'original_payment',
1412 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1413 $newParams = array_merge($contributionParams, array(
1414 'id' => $contribution['id'],
1415 'contribution_status_id' => 'Refunded',
1416 'cancel_date' => '2015-01-01 09:00',
1417 'trxn_id' => 'cont id',
1418 'refund_trxn_id' => 'the refund',
1422 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1423 $this->_checkFinancialTrxn($contribution, 'refund');
1424 $this->_checkFinancialItem($contribution['id'], 'refund');
1425 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1426 'id' => $contribution['id'],
1427 'return' => 'trxn_id',
1432 * Function tests that refund_trxn_id is set when passed in empty.
1434 * Here we ensure that the civicrm_contribution.trxn_id is set
1435 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1436 * is kept for the refund transaction.
1438 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1439 $contributionParams = array(
1440 'contact_id' => $this->_individualId
,
1441 'receive_date' => '2012-01-01',
1442 'total_amount' => 100.00,
1443 'financial_type_id' => $this->_financialTypeId
,
1444 'payment_instrument_id' => 4,
1445 'contribution_status_id' => 1,
1446 'trxn_id' => 'original_payment',
1448 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1449 $newParams = array_merge($contributionParams, array(
1450 'id' => $contribution['id'],
1451 'contribution_status_id' => 'Refunded',
1452 'cancel_date' => '2015-01-01 09:00',
1453 'trxn_id' => 'cont id',
1454 'refund_trxn_id' => '',
1458 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1459 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1460 $this->_checkFinancialItem($contribution['id'], 'refund');
1461 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1462 'id' => $contribution['id'],
1463 'return' => 'trxn_id',
1468 * Function tests invalid contribution status change.
1470 public function testCreateUpdateContributionInValidStatusChange() {
1471 $contribParams = array(
1473 'receive_date' => '2012-01-01',
1474 'total_amount' => 100.00,
1475 'financial_type_id' => 1,
1476 'payment_instrument_id' => 1,
1477 'contribution_status_id' => 1,
1479 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1480 $newParams = array_merge($contribParams, array(
1481 'id' => $contribution['id'],
1482 'contribution_status_id' => 2,
1485 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1490 * Function tests that financial records are added when Pending Contribution is Canceled.
1492 public function testCreateUpdateContributionCancelPending() {
1493 $contribParams = array(
1494 'contact_id' => $this->_individualId
,
1495 'receive_date' => '2012-01-01',
1496 'total_amount' => 100.00,
1497 'financial_type_id' => $this->_financialTypeId
,
1498 'payment_instrument_id' => 1,
1499 'contribution_status_id' => 2,
1500 'is_pay_later' => 1,
1503 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1504 $newParams = array_merge($contribParams, array(
1505 'id' => $contribution['id'],
1506 'contribution_status_id' => 3,
1507 'cancel_date' => '2012-02-02 09:00',
1510 //Check if trxn_date is same as cancel_date.
1511 $checkTrxnDate = array(
1512 'trxn_date' => '2012-02-02 09:00:00',
1514 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1515 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1516 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1520 * Function tests that financial records are added when Financial Type is Changed.
1522 public function testCreateUpdateContributionChangeFinancialType() {
1523 $contribParams = array(
1524 'contact_id' => $this->_individualId
,
1525 'receive_date' => '2012-01-01',
1526 'total_amount' => 100.00,
1527 'financial_type_id' => 1,
1528 'payment_instrument_id' => 1,
1529 'contribution_status_id' => 1,
1532 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1533 $newParams = array_merge($contribParams, array(
1534 'id' => $contribution['id'],
1535 'financial_type_id' => 3,
1538 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1539 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1540 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1544 * Test that update does not change status id CRM-15105.
1546 public function testCreateUpdateWithoutChangingPendingStatus() {
1547 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1548 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1549 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1550 'id' => $contribution['id'],
1551 'api.contribution.delete' => 1,
1553 $this->assertEquals(2, $contribution['contribution_status_id']);
1557 * Test Updating a Contribution.
1559 * CHANGE: we require the API to do an incremental update
1561 public function testCreateUpdateContribution() {
1562 $contributionID = $this->contributionCreate(array(
1563 'contact_id' => $this->_individualId
,
1564 'trxn_id' => 212355,
1565 'financial_type_id' => $this->_financialTypeId
,
1566 'invoice_id' => 'old_invoice',
1568 $old_params = array(
1569 'contribution_id' => $contributionID,
1571 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1572 $this->assertEquals($original['id'], $contributionID);
1573 //set up list of old params, verify
1575 //This should not be required on update:
1576 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1577 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1578 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1579 $old_source = $original['values'][$contributionID]['contribution_source'];
1581 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1582 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1584 //check against values in CiviUnitTestCase::createContribution()
1585 $this->assertEquals($old_contact_id, $this->_individualId
);
1586 $this->assertEquals($old_fee_amount, 5.00);
1587 $this->assertEquals($old_source, 'SSF');
1588 $this->assertEquals($old_trxn_id, 212355);
1589 $this->assertEquals($old_invoice_id, 'old_invoice');
1591 'id' => $contributionID,
1592 'contact_id' => $this->_individualId
,
1593 'total_amount' => 105.00,
1594 'fee_amount' => 7.00,
1595 'financial_type_id' => $this->_financialTypeId
,
1596 'non_deductible_amount' => 22.00,
1597 'contribution_status_id' => 1,
1598 'note' => 'Donating for Noble Cause',
1601 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1603 $new_params = array(
1604 'contribution_id' => $contribution['id'],
1607 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1609 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1610 $this->assertEquals($contribution['total_amount'], 105.00);
1611 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1612 $this->assertEquals($contribution['financial_type'], 'Donation');
1613 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1614 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1615 $this->assertEquals($contribution['fee_amount'], 7.00);
1616 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1617 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1618 $this->assertEquals($contribution['contribution_source'], $old_source);
1619 $this->assertEquals($contribution['contribution_status'], 'Completed');
1621 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1624 'contribution_id' => $contributionID,
1627 $result = $this->callAPISuccess('contribution', 'delete', $params);
1628 $this->assertAPISuccess($result);
1632 * Attempt (but fail) to delete a contribution without parameters.
1634 public function testDeleteEmptyParamsContribution() {
1636 $this->callAPIFailure('contribution', 'delete', $params);
1639 public function testDeleteParamsNotArrayContribution() {
1640 $params = 'contribution_id= 1';
1641 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1642 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1645 public function testDeleteWrongParamContribution() {
1647 'contribution_source' => 'SSF',
1650 $this->callAPIFailure('contribution', 'delete', $params);
1653 public function testDeleteContribution() {
1654 $contributionID = $this->contributionCreate(array(
1655 'contact_id' => $this->_individualId
,
1656 'financial_type_id' => $this->_financialTypeId
,
1659 'id' => $contributionID,
1661 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1665 * Test civicrm_contribution_search with empty params.
1667 * All available contributions expected.
1669 public function testSearchEmptyParams() {
1673 'contact_id' => $this->_individualId
,
1674 'receive_date' => date('Ymd'),
1675 'total_amount' => 100.00,
1676 'financial_type_id' => $this->_financialTypeId
,
1677 'non_deductible_amount' => 10.00,
1678 'fee_amount' => 5.00,
1679 'net_amount' => 95.00,
1681 'invoice_id' => 78910,
1683 'contribution_status_id' => 1,
1686 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1688 $result = $this->callAPISuccess('contribution', 'get', $params);
1689 // We're taking the first element.
1690 $res = $result['values'][$contribution['id']];
1692 $this->assertEquals($p['contact_id'], $res['contact_id']);
1693 $this->assertEquals($p['total_amount'], $res['total_amount']);
1694 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1695 $this->assertEquals($p['net_amount'], $res['net_amount']);
1696 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1697 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1698 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1699 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1700 $this->assertEquals($p['source'], $res['contribution_source']);
1701 // contribution_status_id = 1 => Completed
1702 $this->assertEquals('Completed', $res['contribution_status']);
1704 $this->contributionDelete($contribution['id']);
1708 * Test civicrm_contribution_search. Success expected.
1710 public function testSearch() {
1712 'contact_id' => $this->_individualId
,
1713 'receive_date' => date('Ymd'),
1714 'total_amount' => 100.00,
1715 'financial_type_id' => $this->_financialTypeId
,
1716 'non_deductible_amount' => 10.00,
1717 'contribution_status_id' => 1,
1720 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1723 'contact_id' => $this->_individualId
,
1724 'receive_date' => date('Ymd'),
1725 'total_amount' => 200.00,
1726 'financial_type_id' => $this->_financialTypeId
,
1727 'non_deductible_amount' => 20.00,
1728 'trxn_id' => 5454565,
1729 'invoice_id' => 1212124,
1730 'fee_amount' => 50.00,
1731 'net_amount' => 60.00,
1732 'contribution_status_id' => 2,
1735 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1738 'contribution_id' => $contribution2['id'],
1741 $result = $this->callAPISuccess('contribution', 'get', $params);
1742 $res = $result['values'][$contribution2['id']];
1744 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1745 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1746 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1747 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1748 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1749 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1750 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1751 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1752 // contribution_status_id = 2 => Pending
1753 $this->assertEquals('Pending', $res['contribution_status']);
1755 $this->contributionDelete($contribution1['id']);
1756 $this->contributionDelete($contribution2['id']);
1760 * Test completing a transaction via the API.
1762 * Note that we are creating a logged in user because email goes out from
1765 public function testCompleteTransaction() {
1766 $mut = new CiviMailUtils($this, TRUE);
1767 $this->swapMessageTemplateForTestTemplate();
1768 $this->createLoggedInUser();
1769 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1770 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1771 $this->callAPISuccess('contribution', 'completetransaction', array(
1772 'id' => $contribution['id'],
1774 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1775 $this->assertEquals('SSF', $contribution['contribution_source']);
1776 $this->assertEquals('Completed', $contribution['contribution_status']);
1777 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1778 $mut->checkMailLog(array(
1779 'email:::anthony_anderson@civicrm.org',
1783 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1784 "receipt_date:::\n",
1785 'contributeMode:::notify',
1786 'title:::Contribution',
1787 'displayName:::Mr. Anthony Anderson II',
1788 'contributionStatus:::Completed',
1791 $this->revertTemplateToReservedTemplate();
1795 * Test completing a transaction via the API with a non-USD transaction.
1797 public function testCompleteTransactionEuro() {
1798 $mut = new CiviMailUtils($this, TRUE);
1799 $this->swapMessageTemplateForTestTemplate();
1800 $this->createLoggedInUser();
1801 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1802 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1804 $this->callAPISuccess('contribution', 'completetransaction', array(
1805 'id' => $contribution['id'],
1808 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1809 $this->assertEquals('SSF', $contribution['contribution_source']);
1810 $this->assertEquals('Completed', $contribution['contribution_status']);
1811 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1813 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1814 $entityFinancialTransaction = reset($entityFinancialTransactions);
1815 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1816 $this->assertEquals('EUR', $financialTrxn['currency']);
1818 $mut->checkMailLog(array(
1819 'email:::anthony_anderson@civicrm.org',
1823 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1824 "receipt_date:::\n",
1825 'contributeMode:::notify',
1826 'title:::Contribution',
1827 'displayName:::Mr. Anthony Anderson II',
1828 'contributionStatus:::Completed',
1831 $this->revertTemplateToReservedTemplate();
1835 * Test to ensure mail is sent on chosing pay later
1837 public function testpayLater() {
1838 $mut = new CiviMailUtils($this, TRUE);
1839 $this->swapMessageTemplateForTestTemplate();
1840 $this->createLoggedInUser();
1842 // create contribution page first
1843 $contributionPageParams = array(
1844 'title' => 'Help Support CiviCRM!',
1845 'financial_type_id' => 1,
1846 'is_monetary' => TRUE,
1847 'is_pay_later' => 1,
1848 'is_quick_config' => TRUE,
1849 'pay_later_text' => 'I will send payment by check',
1850 'pay_later_receipt' => 'This is a pay later reciept',
1851 'is_allow_other_amount' => 1,
1852 'min_amount' => 10.00,
1853 'max_amount' => 10000.00,
1854 'goal_amount' => 100000.00,
1855 'is_email_receipt' => 1,
1857 'amount_block_is_active' => 1,
1858 'currency' => 'USD',
1859 'is_billing_required' => 0,
1861 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1863 // submit form values
1864 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1866 'id' => $contributionPageResult['id'],
1867 'contact_id' => $this->_individualId
,
1868 'email-5' => 'anthony_anderson@civicrm.org',
1869 'payment_processor_id' => 0,
1872 'currencyID' => 'USD',
1873 'is_pay_later' => 1,
1874 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1875 'is_quick_config' => 1,
1876 'description' => 'Online Contribution: Help Support CiviCRM!',
1877 'price_set_id' => $priceSet['id'],
1879 $this->callAPISuccess('contribution_page', 'submit', $params);
1881 $mut->checkMailLog(array(
1883 'email:::anthony_anderson@civicrm.org',
1884 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1885 'displayName:::Mr. Anthony Anderson II',
1886 'contributionPageId:::' . $contributionPageResult['id'],
1887 'title:::' . $contributionPageParams['title'],
1888 'amount:::' . $params['amount'],
1891 $this->revertTemplateToReservedTemplate();
1895 * Test to check whether contact billing address is used when no contribution address
1897 public function testBillingAddress() {
1898 $mut = new CiviMailUtils($this, TRUE);
1899 $this->swapMessageTemplateForTestTemplate();
1900 $this->createLoggedInUser();
1902 //Scenario 1: When Contact don't have any address
1903 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1904 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1905 $this->callAPISuccess('contribution', 'completetransaction', array(
1906 'id' => $contribution['id'],
1908 $mut->checkMailLog(array(
1912 // Scenario 2: Contribution using address
1913 $address = $this->callAPISuccess('address', 'create', array(
1914 'street_address' => 'contribution billing st',
1915 'location_type_id' => 2,
1916 'contact_id' => $this->_params
['contact_id'],
1918 $params = array_merge($this->_params
, array(
1919 'contribution_status_id' => 2,
1920 'address_id' => $address['id'],
1923 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1924 $this->callAPISuccess('contribution', 'completetransaction', array(
1925 'id' => $contribution['id'],
1927 $mut->checkMailLog(array(
1928 'address:::contribution billing st',
1931 // Scenario 3: Contribution wtth no address but contact has a billing address
1932 $this->callAPISuccess('address', 'create', array(
1933 'id' => $address['id'],
1934 'street_address' => 'is billing st',
1935 'contact_id' => $this->_params
['contact_id'],
1937 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1938 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1939 $this->callAPISuccess('contribution', 'completetransaction', array(
1940 'id' => $contribution['id'],
1942 $mut->checkMailLog(array(
1943 'address:::is billing st',
1947 $this->revertTemplateToReservedTemplate();
1951 * Test completing a transaction via the API.
1953 * Note that we are creating a logged in user because email goes out from
1956 public function testCompleteTransactionFeeAmount() {
1957 $this->createLoggedInUser();
1958 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1959 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1960 $this->callAPISuccess('contribution', 'completetransaction', array(
1961 'id' => $contribution['id'],
1962 'fee_amount' => '.56',
1963 'trxn_id' => '7778888',
1965 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1966 $this->assertEquals('Completed', $contribution['contribution_status']);
1967 $this->assertEquals('7778888', $contribution['trxn_id']);
1968 $this->assertEquals('.56', $contribution['fee_amount']);
1969 $this->assertEquals('99.44', $contribution['net_amount']);
1973 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
1975 * @param string $thousandSeparator
1976 * punctuation used to refer to thousands.
1978 * @dataProvider getThousandSeparators
1980 public function testCheckTaxAmount($thousandSeparator) {
1981 $this->setCurrencySeparators($thousandSeparator);
1982 $contact = $this->createLoggedInUser();
1983 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1984 'name' => 'Test taxable financial Type',
1988 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1989 'name' => 'Test Tax financial account ',
1990 'contact_id' => $contact,
1991 'financial_account_type_id' => 2,
1998 $financialTypeId = $financialType['id'];
1999 $financialAccountId = $financialAccount['id'];
2000 $financialAccountParams = array(
2001 'entity_table' => 'civicrm_financial_type',
2002 'entity_id' => $financialTypeId,
2003 'account_relationship' => 10,
2004 'financial_account_id' => $financialAccountId,
2006 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
2008 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
2009 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2010 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
2011 $this->callAPISuccess('contribution', 'completetransaction', array(
2012 'id' => $contribution['id'],
2013 'trxn_id' => '777788888',
2014 'fee_amount' => '6.00',
2016 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
2017 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
2018 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
2019 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
2023 * Test repeat contribution successfully creates line item.
2025 public function testRepeatTransaction() {
2026 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2027 $this->callAPISuccess('contribution', 'repeattransaction', array(
2028 'original_contribution_id' => $originalContribution['id'],
2029 'contribution_status_id' => 'Completed',
2030 'trxn_id' => uniqid(),
2032 $lineItemParams = array(
2033 'entity_id' => $originalContribution['id'],
2041 'financial_type_id',
2042 'deductible_amount',
2043 'price_field_value_id',
2047 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2048 'entity_id' => $originalContribution['id'],
2050 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2051 'entity_id' => $originalContribution['id'] +
1,
2053 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2054 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2055 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2056 $this->_checkFinancialRecords(array(
2057 'id' => $originalContribution['id'] +
1,
2058 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2059 'id' => $originalContribution['payment_processor_id'],
2060 'return' => 'payment_instrument_id',
2063 $this->quickCleanUpFinancialEntities();
2067 * Test repeat contribution successfully creates line items (plural).
2069 public function testRepeatTransactionLineItems() {
2071 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2072 $this->callAPISuccess('contribution', 'repeattransaction', array(
2073 'original_contribution_id' => $originalContribution['id'],
2074 'contribution_status_id' => 'Completed',
2075 'trxn_id' => uniqid(),
2078 $lineItemParams = array(
2079 'entity_id' => $originalContribution['id'],
2087 'financial_type_id',
2088 'deductible_amount',
2089 'price_field_value_id',
2093 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2094 'entity_id' => $originalContribution['id'],
2096 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2097 'entity_id' => $originalContribution['id'] +
1,
2100 // unset id and entity_id for all of them to be able to compare the lineItems:
2101 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2102 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2103 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2105 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2106 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2107 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2109 // CRM-19309 so in future we also want to:
2110 // check that financial_line_items have been created for entity_id 3 and 4;
2112 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2113 $this->quickCleanUpFinancialEntities();
2117 * Test repeat contribution successfully creates is_test transaction.
2119 public function testRepeatTransactionIsTest() {
2120 $this->_params
['is_test'] = 1;
2121 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2123 $this->callAPISuccess('contribution', 'repeattransaction', array(
2124 'original_contribution_id' => $originalContribution['id'],
2125 'contribution_status_id' => 'Completed',
2126 'trxn_id' => uniqid(),
2128 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2132 * Test repeat contribution passed in status.
2134 public function testRepeatTransactionPassedInStatus() {
2135 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2137 $this->callAPISuccess('contribution', 'repeattransaction', array(
2138 'original_contribution_id' => $originalContribution['id'],
2139 'contribution_status_id' => 'Pending',
2140 'trxn_id' => uniqid(),
2142 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2146 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2148 public function testRepeatTransactionAcceptRecurID() {
2149 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2150 'contact_id' => $this->_individualId
,
2151 'installments' => '12',
2152 'frequency_interval' => '1',
2154 'contribution_status_id' => 1,
2155 'start_date' => '2012-01-01 00:00:00',
2156 'currency' => 'USD',
2157 'frequency_unit' => 'month',
2158 'payment_processor_id' => $this->paymentProcessorID
,
2160 $this->callAPISuccess('contribution', 'create', array_merge(
2162 array('contribution_recur_id' => $contributionRecur['id']))
2165 $this->callAPISuccess('contribution', 'repeattransaction', array(
2166 'contribution_recur_id' => $contributionRecur['id'],
2167 'contribution_status_id' => 'Completed',
2168 'trxn_id' => uniqid(),
2171 $this->quickCleanUpFinancialEntities();
2175 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2177 public function testRepeatTransactionTestRecurId() {
2178 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2179 'contact_id' => $this->_individualId
,
2180 'frequency_interval' => '1',
2182 'contribution_status_id' => 1,
2183 'start_date' => '2017-01-01 00:00:00',
2184 'currency' => 'USD',
2185 'frequency_unit' => 'month',
2186 'payment_processor_id' => $this->paymentProcessorID
,
2189 $this->callAPISuccess('contribution', 'create', array_merge(
2192 'contribution_recur_id' => $contributionRecur['id'],
2197 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2198 'contribution_recur_id' => $contributionRecur['id'],
2199 'contribution_status_id' => 'Completed',
2200 'trxn_id' => uniqid(),
2203 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2204 $this->quickCleanUpFinancialEntities();
2207 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2209 * ( Tests membership is renewed after repeattransaction. )
2211 public function testRepeatTransactionUsesCompleted() {
2212 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2214 $this->callAPISuccess('contribution', 'create', array(
2215 'contact_id' => $originalContribution['values'][1]['contact_id'],
2216 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2217 'total_amount' => $originalContribution['values'][1]['total_amount'],
2218 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2219 'contribution_status_id' => "Failed",
2222 $this->callAPISuccess('membership', 'create', array(
2223 'id' => $membership['id'],
2224 'end_date' => 'yesterday',
2228 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2229 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2230 'contribution_status_id' => 'Completed',
2231 'trxn_id' => 'bobsled',
2234 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2235 'id' => $membership['id'],
2236 'return' => 'status_id',
2239 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2241 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2242 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2243 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2244 $this->quickCleanUpFinancialEntities();
2245 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2249 * CRM-16397 test appropriate action if total amount has changed for single line items.
2251 public function testRepeatTransactionAlteredAmount() {
2252 $paymentProcessorID = $this->paymentProcessorCreate();
2253 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2254 'contact_id' => $this->_individualId
,
2255 'installments' => '12',
2256 'frequency_interval' => '1',
2258 'contribution_status_id' => 1,
2259 'start_date' => '2012-01-01 00:00:00',
2260 'currency' => 'USD',
2261 'frequency_unit' => 'month',
2262 'payment_processor_id' => $paymentProcessorID,
2264 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2267 'contribution_recur_id' => $contributionRecur['id'],
2271 $this->callAPISuccess('contribution', 'repeattransaction', array(
2272 'original_contribution_id' => $originalContribution['id'],
2273 'contribution_status_id' => 'Completed',
2274 'trxn_id' => uniqid(),
2275 'total_amount' => '400',
2279 $lineItemParams = array(
2280 'entity_id' => $originalContribution['id'],
2288 'financial_type_id',
2289 'deductible_amount',
2290 'price_field_value_id',
2294 $this->callAPISuccessGetSingle('contribution', array(
2295 'total_amount' => 400,
2297 'net_amount' => 350,
2299 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2300 'entity_id' => $originalContribution['id'],
2302 $expectedLineItem = array_merge(
2303 $lineItem1['values'][0], array(
2304 'line_total' => '400.00',
2305 'unit_price' => '400.00',
2309 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2310 'entity_id' => $originalContribution['id'] +
1,
2313 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2314 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2315 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2319 * CRM-17718 test appropriate action if financial type has changed for single line items.
2321 public function testRepeatTransactionPassedInFinancialType() {
2322 $originalContribution = $this->setUpRecurringContribution();
2324 $this->callAPISuccess('contribution', 'repeattransaction', array(
2325 'original_contribution_id' => $originalContribution['id'],
2326 'contribution_status_id' => 'Completed',
2327 'trxn_id' => uniqid(),
2328 'financial_type_id' => 2,
2330 $lineItemParams = array(
2331 'entity_id' => $originalContribution['id'],
2339 'financial_type_id',
2340 'deductible_amount',
2341 'price_field_value_id',
2346 $this->callAPISuccessGetSingle('contribution', array(
2347 'total_amount' => 100,
2348 'financial_type_id' => 2,
2350 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2351 'entity_id' => $originalContribution['id'],
2353 $expectedLineItem = array_merge(
2354 $lineItem1['values'][0], array(
2355 'line_total' => '100.00',
2356 'unit_price' => '100.00',
2357 'financial_type_id' => 2,
2358 'contribution_type_id' => 2,
2361 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2362 'entity_id' => $originalContribution['id'] +
1,
2364 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2365 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2366 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2370 * CRM-17718 test appropriate action if financial type has changed for single line items.
2372 public function testRepeatTransactionUpdatedFinancialType() {
2373 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2375 $this->callAPISuccess('contribution', 'repeattransaction', array(
2376 'contribution_recur_id' => $originalContribution['id'],
2377 'contribution_status_id' => 'Completed',
2378 'trxn_id' => uniqid(),
2380 $lineItemParams = array(
2381 'entity_id' => $originalContribution['id'],
2389 'financial_type_id',
2390 'deductible_amount',
2391 'price_field_value_id',
2396 $this->callAPISuccessGetSingle('contribution', array(
2397 'total_amount' => 100,
2398 'financial_type_id' => 2,
2400 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2401 'entity_id' => $originalContribution['id'],
2403 $expectedLineItem = array_merge(
2404 $lineItem1['values'][0], array(
2405 'line_total' => '100.00',
2406 'unit_price' => '100.00',
2407 'financial_type_id' => 2,
2408 'contribution_type_id' => 2,
2412 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2413 'entity_id' => $originalContribution['id'] +
1,
2415 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2416 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2417 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2421 * CRM-16397 test appropriate action if campaign has been passed in.
2423 public function testRepeatTransactionPassedInCampaign() {
2424 $paymentProcessorID = $this->paymentProcessorCreate();
2425 $campaignID = $this->campaignCreate();
2426 $campaignID2 = $this->campaignCreate();
2427 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2428 'contact_id' => $this->_individualId
,
2429 'installments' => '12',
2430 'frequency_interval' => '1',
2432 'contribution_status_id' => 1,
2433 'start_date' => '2012-01-01 00:00:00',
2434 'currency' => 'USD',
2435 'frequency_unit' => 'month',
2436 'payment_processor_id' => $paymentProcessorID,
2438 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2441 'contribution_recur_id' => $contributionRecur['id'],
2442 'campaign_id' => $campaignID,
2446 $this->callAPISuccess('contribution', 'repeattransaction', array(
2447 'original_contribution_id' => $originalContribution['id'],
2448 'contribution_status_id' => 'Completed',
2449 'trxn_id' => uniqid(),
2450 'campaign_id' => $campaignID2,
2453 $this->callAPISuccessGetSingle('contribution', array(
2454 'total_amount' => 100,
2455 'campaign_id' => $campaignID2,
2460 * CRM-17718 campaign stored on contribution recur gets priority.
2462 * This reflects the fact we permit people to update them.
2464 public function testRepeatTransactionUpdatedCampaign() {
2465 $paymentProcessorID = $this->paymentProcessorCreate();
2466 $campaignID = $this->campaignCreate();
2467 $campaignID2 = $this->campaignCreate();
2468 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2469 'contact_id' => $this->_individualId
,
2470 'installments' => '12',
2471 'frequency_interval' => '1',
2473 'contribution_status_id' => 1,
2474 'start_date' => '2012-01-01 00:00:00',
2475 'currency' => 'USD',
2476 'frequency_unit' => 'month',
2477 'payment_processor_id' => $paymentProcessorID,
2478 'campaign_id' => $campaignID,
2480 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2483 'contribution_recur_id' => $contributionRecur['id'],
2484 'campaign_id' => $campaignID2,
2488 $this->callAPISuccess('contribution', 'repeattransaction', array(
2489 'original_contribution_id' => $originalContribution['id'],
2490 'contribution_status_id' => 'Completed',
2491 'trxn_id' => uniqid(),
2494 $this->callAPISuccessGetSingle('contribution', array(
2495 'total_amount' => 100,
2496 'campaign_id' => $campaignID,
2501 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2503 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2505 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
2506 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2507 // This will made the trick to get the not equals behaviour.
2508 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 4));
2509 $this->callAPISuccess('contribution', 'repeattransaction', array(
2510 'contribution_recur_id' => $originalContribution['id'],
2511 'contribution_status_id' => 'Completed',
2512 'trxn_id' => uniqid(),
2514 $lineItemParams = array(
2515 'entity_id' => $originalContribution['id'],
2523 'financial_type_id',
2524 'deductible_amount',
2525 'price_field_value_id',
2529 $this->callAPISuccessGetSingle('contribution', array(
2530 'total_amount' => 100,
2531 'financial_type_id' => 2,
2533 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2534 'entity_id' => $originalContribution['id'],
2536 $expectedLineItem = array_merge(
2537 $lineItem1['values'][0], array(
2538 'line_total' => '100.00',
2539 'unit_price' => '100.00',
2540 'financial_type_id' => 4,
2541 'contribution_type_id' => 4,
2545 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2546 'entity_id' => $originalContribution['id'] +
1,
2548 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 1));
2549 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2550 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2551 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2556 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2558 public function testCompleteTransactionNetAmountOK() {
2559 $this->createLoggedInUser();
2560 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2561 unset($params['net_amount']);
2562 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2563 $this->callAPISuccess('contribution', 'completetransaction', array(
2564 'id' => $contribution['id'],
2566 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2567 $this->assertEquals('Completed', $contribution['contribution_status']);
2568 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2572 * CRM-14151 - Test completing a transaction via the API.
2574 public function testCompleteTransactionWithReceiptDateSet() {
2575 $this->swapMessageTemplateForTestTemplate();
2576 $mut = new CiviMailUtils($this, TRUE);
2577 $this->createLoggedInUser();
2578 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2579 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2580 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2581 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2582 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2583 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2584 $mut->checkMailLog(array(
2585 'Receipt - Contribution',
2586 'receipt_date:::' . date('Ymd'),
2589 $this->revertTemplateToReservedTemplate();
2593 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2595 public function testCompleteTransactionWithEmailReceiptInput() {
2596 $contributionPage = $this->createReceiptableContributionPage();
2598 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2599 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2600 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2601 // Complete the transaction overriding is_email_receipt to = FALSE
2602 $this->callAPISuccess('contribution', 'completetransaction', array(
2603 'id' => $contribution['id'],
2604 'trxn_date' => date('2011-04-09'),
2605 'trxn_id' => 'kazam',
2606 'is_email_receipt' => 0,
2608 // Check if a receipt was issued
2609 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2610 $this->assertEquals('', $receipt_date);
2614 * Test that $is_recur is assigned to the receipt.
2616 public function testCompleteTransactionForRecurring() {
2618 $this->swapMessageTemplateForTestTemplate();
2619 $recurring = $this->setUpRecurringContribution();
2620 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2622 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2623 $this->_params
['contribution_recur_id'] = $recurring['id'];
2625 $contribution = $this->setUpForCompleteTransaction();
2627 $this->callAPISuccess('contribution', 'completetransaction', array(
2628 'id' => $contribution['id'],
2629 'trxn_date' => date('2011-04-09'),
2630 'trxn_id' => 'kazam',
2631 'is_email_receipt' => 1,
2634 $this->mut
->checkMailLog(array(
2636 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2639 $this->revertTemplateToReservedTemplate();
2642 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2644 * If passed in it will override the default from contribution page.
2646 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2647 $mut = new CiviMailUtils($this, TRUE);
2648 $this->createLoggedInUser();
2649 // Create a Contribution Page with is_email_receipt = FALSE
2650 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2651 'receipt_from_name' => 'Mickey Mouse',
2652 'receipt_from_email' => 'mickey@mouse.com',
2653 'title' => "Test Contribution Page",
2654 'financial_type_id' => 1,
2655 'currency' => 'CAD',
2656 'is_monetary' => TRUE,
2657 'is_email_receipt' => 0,
2659 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2660 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2661 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2662 // Complete the transaction overriding is_email_receipt to = TRUE
2663 $this->callAPISuccess('contribution', 'completetransaction', array(
2664 'id' => $contribution['id'],
2665 'is_email_receipt' => 1,
2667 $mut->checkMailLog(array(
2668 'Please print this receipt for your records.',
2674 * Complete the transaction using the template with all the possible.
2676 public function testCompleteTransactionWithTestTemplate() {
2677 $this->swapMessageTemplateForTestTemplate();
2678 $contribution = $this->setUpForCompleteTransaction();
2679 $this->callAPISuccess('contribution', 'completetransaction', array(
2680 'id' => $contribution['id'],
2681 'trxn_date' => date('2011-04-09'),
2682 'trxn_id' => 'kazam',
2684 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2685 $this->mut
->checkMailLog(array(
2686 'email:::anthony_anderson@civicrm.org',
2690 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2691 'receipt_date:::' . date('Ymd'),
2692 'contributeMode:::notify',
2693 'title:::Contribution',
2694 'displayName:::Mr. Anthony Anderson II',
2696 'contactID:::' . $this->_params
['contact_id'],
2697 'contributionID:::' . $contribution['id'],
2698 'financialTypeId:::1',
2699 'financialTypeName:::Donation',
2702 $this->revertTemplateToReservedTemplate();
2706 * Complete the transaction using the template with all the possible.
2708 public function testCompleteTransactionContributionPageFromAddress() {
2709 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2710 'receipt_from_name' => 'Mickey Mouse',
2711 'receipt_from_email' => 'mickey@mouse.com',
2712 'title' => "Test Contribution Page",
2713 'financial_type_id' => 1,
2714 'currency' => 'NZD',
2715 'goal_amount' => 50,
2716 'is_pay_later' => 1,
2717 'is_monetary' => TRUE,
2718 'is_email_receipt' => TRUE,
2720 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2721 $contribution = $this->setUpForCompleteTransaction();
2722 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2723 $this->mut
->checkMailLog(array(
2731 * Test completing first transaction in a recurring series.
2733 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2735 * @dataProvider getScheduledDateData
2737 * @param array $dataSet
2739 * @throws \Exception
2741 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2742 $paymentProcessorID = $this->paymentProcessorCreate();
2743 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2744 'contact_id' => $this->_individualId
,
2745 'installments' => '2',
2746 'frequency_interval' => '1',
2748 'contribution_status_id' => 'Pending',
2749 'start_date' => '2012-01-01 00:00:00',
2750 'currency' => 'USD',
2751 'frequency_unit' => 'month',
2752 'payment_processor_id' => $paymentProcessorID,
2753 ), $dataSet['data']));
2754 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2757 'contribution_recur_id' => $contributionRecur['id'],
2758 'contribution_status_id' => 'Pending',
2759 'receive_date' => $dataSet['receive_date'],
2762 $this->callAPISuccess('Contribution', 'completetransaction', array(
2763 'id' => $contribution,
2764 'receive_date' => $dataSet['receive_date'],
2766 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2767 'id' => $contributionRecur['id'],
2768 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2770 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2771 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2772 $this->callAPISuccess('Contribution', 'create', array_merge(
2775 'contribution_recur_id' => $contributionRecur['id'],
2776 'contribution_status_id' => 'Completed',
2779 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2780 'id' => $contributionRecur['id'],
2781 'return' => array('contribution_status_id'),
2783 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2787 * Get dates for testing.
2791 public function getScheduledDateData() {
2793 $result[]['2016-08-31-1-month'] = array(
2795 'start_date' => '2016-08-31',
2796 'frequency_interval' => 1,
2797 'frequency_unit' => 'month',
2799 'receive_date' => '2016-08-31',
2800 'expected' => '2016-10-01 00:00:00',
2802 $result[]['2012-01-01-1-month'] = array(
2804 'start_date' => '2012-01-01',
2805 'frequency_interval' => 1,
2806 'frequency_unit' => 'month',
2808 'receive_date' => '2012-01-01',
2809 'expected' => '2012-02-01 00:00:00',
2811 $result[]['2012-01-01-1-month'] = array(
2813 'start_date' => '2012-01-01',
2814 'frequency_interval' => 1,
2815 'frequency_unit' => 'month',
2817 'receive_date' => '2012-02-29',
2818 'expected' => '2012-03-29 00:00:00',
2820 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2822 'start_date' => '2012-01-01',
2823 'frequency_interval' => 1,
2824 'frequency_unit' => 'month',
2825 'next_sched_contribution_date' => '2012-02-29',
2827 'receive_date' => '2012-02-29 16:00:00',
2828 'expected' => '2012-03-29 00:00:00',
2834 * Test completing a pledge with the completeTransaction api..
2836 * Note that we are creating a logged in user because email goes out from
2839 public function testCompleteTransactionUpdatePledgePayment() {
2840 $this->swapMessageTemplateForTestTemplate();
2841 $mut = new CiviMailUtils($this, TRUE);
2842 $mut->clearMessages();
2843 $this->createLoggedInUser();
2844 $contributionID = $this->createPendingPledgeContribution();
2845 $this->callAPISuccess('contribution', 'completetransaction', array(
2846 'id' => $contributionID,
2847 'trxn_date' => '1 Feb 2013',
2849 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2850 'id' => $this->_ids
['pledge'],
2852 $this->assertEquals('Completed', $pledge['pledge_status']);
2854 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2855 'pledge_id' => $this->_ids
['pledge'],
2856 'return' => 'status_id',
2858 $this->assertEquals(1, $status);
2859 $mut->checkMailLog(array(
2861 'receive_date:::20130201000000',
2862 "receipt_date:::\n",
2865 $this->revertTemplateToReservedTemplate();
2869 * Test completing a transaction with an event via the API.
2871 * Note that we are creating a logged in user because email goes out from
2874 public function testCompleteTransactionWithParticipantRecord() {
2875 $mut = new CiviMailUtils($this, TRUE);
2876 $mut->clearMessages();
2877 $this->_individualId
= $this->createLoggedInUser();
2878 $contributionID = $this->createPendingParticipantContribution();
2879 $this->callAPISuccess('contribution', 'completetransaction', array(
2880 'id' => $contributionID,
2883 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2884 'id' => $this->_ids
['participant'],
2885 'return' => 'participant_status_id',
2887 $this->assertEquals(1, $participantStatus);
2889 //Assert only three activities are created.
2890 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_individualId
);
2891 $this->assertEquals(3, count($activities));
2892 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
2893 // record two activities before and after completing payment for Event registration
2894 $this->assertEquals(2, $activityNames['Event Registration']);
2895 // update the original 'Contribution' activity created after completing payment
2896 $this->assertEquals(1, $activityNames['Contribution']);
2898 $mut->checkMailLog(array(
2899 'Annual CiviCRM meet',
2901 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2907 * Test membership is renewed when transaction completed.
2909 public function testCompleteTransactionMembershipPriceSet() {
2910 $this->createPriceSetWithPage('membership');
2911 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2915 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2916 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2917 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2918 'membership_id' => $this->_ids
['membership'],
2920 $this->assertEquals(1, $logs['count']);
2921 $this->assertEquals($stateOfGrace, $membership['status_id']);
2922 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2923 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2924 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2925 $this->callAPISuccessGetSingle('LineItem', array(
2926 'entity_id' => $this->_ids
['membership'],
2927 'entity_table' => 'civicrm_membership',
2929 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2930 'membership_id' => $this->_ids
['membership'],
2932 $this->assertEquals(2, $logs['count']);
2933 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2934 //Assert only three activities are created.
2935 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
2936 $this->assertEquals(3, count($activities));
2937 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
2938 $this->assertArrayHasKey('Contribution', $activityNames);
2939 $this->assertArrayHasKey('Membership Signup', $activityNames);
2940 $this->assertArrayHasKey('Change Membership Status', $activityNames);
2941 $this->cleanUpAfterPriceSets();
2945 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2947 public function testPendingToCompleteContribution() {
2948 $this->createPriceSetWithPage('membership');
2949 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2950 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2951 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2952 $activity = $this->callAPISuccess('Activity', 'get', array(
2953 'activity_type_id' => 'Membership Signup',
2954 'source_record_id' => $this->_ids
['membership'],
2955 'status_id' => 'Scheduled',
2957 $this->assertEquals(1, $activity['count']);
2959 // change pending contribution to completed
2960 $form = new CRM_Contribute_Form_Contribution();
2962 $form->_params
= array(
2963 'id' => $this->_ids
['contribution'],
2964 'total_amount' => 20,
2967 'financial_type_id' => 1,
2968 'contact_id' => $this->_individualId
,
2969 'contribution_status_id' => 1,
2970 'billing_middle_name' => '',
2971 'billing_last_name' => 'Adams',
2972 'billing_street_address-5' => '790L Lincoln St S',
2973 'billing_city-5' => 'Maryknoll',
2974 'billing_state_province_id-5' => 1031,
2975 'billing_postal_code-5' => 10545,
2976 'billing_country_id-5' => 1228,
2977 'frequency_interval' => 1,
2978 'frequency_unit' => 'month',
2979 'installments' => '',
2980 'hidden_AdditionalDetail' => 1,
2981 'hidden_Premium' => 1,
2982 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2983 'receipt_date' => '',
2984 'receipt_date_time' => '',
2985 'payment_processor_id' => $this->paymentProcessorID
,
2986 'currency' => 'USD',
2987 'contribution_page_id' => $this->_ids
['contribution_page'],
2988 'contribution_mode' => 'membership',
2989 'source' => 'Membership Signup and Renewal',
2992 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2994 // Case 2: After successful payment for Pending backoffice there are three activities created
2995 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
2996 $activity = $this->callAPISuccess('Activity', 'get', array(
2997 'activity_type_id' => 'Membership Signup',
2998 'source_record_id' => $this->_ids
['membership'],
2999 'status_id' => 'Completed',
3001 $this->assertEquals(1, $activity['count']);
3002 // 2.b Contribution activity created to record successful payment
3003 $activity = $this->callAPISuccess('Activity', 'get', array(
3004 'activity_type_id' => 'Contribution',
3005 'source_record_id' => $this->_ids
['contribution'],
3006 'status_id' => 'Completed',
3008 $this->assertEquals(1, $activity['count']);
3010 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
3011 $activity = $this->callAPISuccess('Activity', 'get', array(
3012 'activity_type_id' => 'Change Membership Status',
3013 'source_record_id' => $this->_ids
['membership'],
3014 'status_id' => 'Completed',
3016 $this->assertEquals(1, $activity['count']);
3017 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
3019 //Create another pending contribution for renewal
3020 $contribution = $this->callAPISuccess('contribution', 'create', array(
3022 'contact_id' => $this->_ids
['contact'],
3023 'receive_date' => date('Ymd'),
3024 'total_amount' => 20.00,
3025 'financial_type_id' => 1,
3026 'payment_instrument_id' => 'Credit Card',
3027 'non_deductible_amount' => 10.00,
3028 'trxn_id' => 'rdhfi88',
3029 'invoice_id' => 'dofhiewuyr',
3031 'contribution_status_id' => 2,
3032 'contribution_page_id' => $this->_ids
['contribution_page'],
3033 // We can't rely on contribution api to link line items correctly to membership
3034 'skipLineItem' => TRUE,
3035 'api.membership_payment.create' => array('membership_id' => $this->_ids
['membership']),
3038 $this->callAPISuccess('line_item', 'create', array(
3039 'entity_id' => $contribution['id'],
3040 'entity_table' => 'civicrm_contribution',
3041 'contribution_id' => $contribution['id'],
3042 'price_field_id' => $this->_ids
['price_field'][0],
3046 'financial_type_id' => 1,
3047 'price_field_value_id' => $this->_ids
['price_field_value']['cont'],
3049 $this->callAPISuccess('line_item', 'create', array(
3050 'entity_id' => $this->_ids
['membership'],
3051 'entity_table' => 'civicrm_membership',
3052 'contribution_id' => $contribution['id'],
3053 'price_field_id' => $this->_ids
['price_field'][0],
3057 'financial_type_id' => 1,
3058 'price_field_value_id' => $this->_ids
['price_field_value'][0],
3059 'membership_type_id' => $this->_ids
['membership_type'],
3062 //Update it to Failed.
3063 $form->_params
['id'] = $contribution['id'];
3064 $form->_params
['contribution_status_id'] = 4;
3066 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3067 //Existing membership should not get updated to expired.
3068 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3069 $this->assertNotEquals($membership['status_id'], 4);
3073 * Test membership is renewed when transaction completed.
3075 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3076 $this->createPriceSetWithPage('membership');
3077 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
3078 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
3079 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3080 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3081 $this->cleanUpAfterPriceSets();
3084 public function cleanUpAfterPriceSets() {
3085 $this->quickCleanUpFinancialEntities();
3086 $this->contactDelete($this->_ids
['contact']);
3090 * Set up a pending transaction with a specific price field id.
3092 * @param int $priceFieldValueID
3094 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
3095 $contactID = $this->individualCreate();
3096 $membership = $this->callAPISuccess('membership', 'create', array(
3097 'contact_id' => $contactID,
3098 'membership_type_id' => $this->_ids
['membership_type'],
3099 'start_date' => 'yesterday - 1 year',
3100 'end_date' => 'yesterday',
3101 'join_date' => 'yesterday - 1 year',
3103 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3105 'contact_id' => $contactID,
3106 'receive_date' => date('Ymd'),
3107 'total_amount' => 20.00,
3108 'financial_type_id' => 1,
3109 'payment_instrument_id' => 'Credit Card',
3110 'non_deductible_amount' => 10.00,
3111 'trxn_id' => 'jdhfi' . rand(1, 100),
3112 'invoice_id' => 'djfhiew' . rand(5, 100),
3114 'contribution_status_id' => 2,
3115 'contribution_page_id' => $this->_ids
['contribution_page'],
3116 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3119 $this->callAPISuccess('line_item', 'create', array(
3120 'entity_id' => $contribution['id'],
3121 'entity_table' => 'civicrm_contribution',
3122 'contribution_id' => $contribution['id'],
3123 'price_field_id' => $this->_ids
['price_field'][0],
3127 'financial_type_id' => 1,
3128 'price_field_value_id' => $priceFieldValueID,
3130 $this->_ids
['contact'] = $contactID;
3131 $this->_ids
['contribution'] = $contribution['id'];
3132 $this->_ids
['membership'] = $membership['id'];
3136 * Test sending a mail via the API.
3138 public function testSendMail() {
3139 $mut = new CiviMailUtils($this, TRUE);
3140 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3141 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3142 'id' => $contribution['id'],
3143 'receipt_from_email' => 'api@civicrm.org',
3146 $mut->checkMailLog(array(
3148 'Contribution Information',
3149 'Please print this confirmation for your records',
3155 $this->checkCreditCardDetails($mut, $contribution['id']);
3160 * Test sending a mail via the API.
3161 * This simulates webform_civicrm using pay later contribution page
3163 public function testSendconfirmationPayLater() {
3164 $mut = new CiviMailUtils($this, TRUE);
3166 // Create contribution page
3167 $pageParams = array(
3168 'title' => 'Webform Contributions',
3169 'financial_type_id' => 1,
3170 'contribution_type_id' => 1,
3171 'is_confirm_enabled' => 1,
3172 'is_pay_later' => 1,
3173 'pay_later_text' => 'I will send payment by cheque',
3174 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3176 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3178 // Create pay later contribution
3179 $contribParams = array(
3180 'contact_id' => $this->_individualId
,
3181 'financial_type_id' => 1,
3182 'is_pay_later' => 1,
3183 'contribution_status_id' => 2,
3184 'contribution_page_id' => $contributionPage['id'],
3185 'total_amount' => '10.00',
3187 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3190 $lineItemParams = array(
3191 'contribution_id' => $contribution['id'],
3192 'entity_id' => $contribution['id'],
3193 'entity_table' => 'civicrm_contribution',
3194 'label' => 'My lineitem label',
3196 'unit_price' => "10.00",
3197 'line_total' => "10.00",
3199 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3203 civicrm_api3('contribution', 'sendconfirmation', array(
3204 'id' => $contribution['id'],
3205 'receipt_from_email' => 'api@civicrm.org',
3209 catch (Exception
$e) {
3210 // Need to figure out how to stop this some other day
3211 // We don't care about the Payment Processor because this is Pay Later
3212 // The point of this test is to check we get the pay_later version of the mail
3213 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3218 // Retrieve mail & check it has the pay_later_receipt info
3219 $mut->getMostRecentEmail('raw');
3220 $mut->checkMailLog(array(
3221 (string) $contribParams['total_amount'],
3222 $pageParams['pay_later_receipt'],
3232 * Check credit card details in sent mail via API
3234 * @param $mut obj CiviMailUtils instance
3235 * @param int $contributionID Contribution ID
3238 public function checkCreditCardDetails($mut, $contributionID) {
3239 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3240 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3241 'id' => $contributionID,
3242 'receipt_from_email' => 'api@civicrm.org',
3243 'payment_processor_id' => $this->paymentProcessorID
,
3246 $mut->checkMailLog(array(
3247 'Credit Card Information', // credit card header
3248 'Billing Name and Address', // billing header
3249 'anthony_anderson@civicrm.org', // billing name
3257 * Test sending a mail via the API.
3259 public function testSendMailEvent() {
3260 $mut = new CiviMailUtils($this, TRUE);
3261 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3262 $event = $this->eventCreate(array(
3263 'is_email_confirm' => 1,
3264 'confirm_from_email' => 'test@civicrm.org',
3266 $this->_eventID
= $event['id'];
3267 $participantParams = array(
3268 'contact_id' => $this->_individualId
,
3269 'event_id' => $this->_eventID
,
3272 // to ensure it matches later on
3273 'register_date' => '2007-07-21 00:00:00',
3274 'source' => 'Online Event Registration: API Testing',
3277 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3278 $this->callAPISuccess('participant_payment', 'create', array(
3279 'participant_id' => $participant['id'],
3280 'contribution_id' => $contribution['id'],
3282 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3283 'id' => $contribution['id'],
3284 'receipt_from_email' => 'api@civicrm.org',
3288 $mut->checkMailLog(array(
3289 'Annual CiviCRM meet',
3291 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3298 * This function does a GET & compares the result against the $params.
3300 * Use as a double check on Creates.
3302 * @param array $params
3304 * @param bool $delete
3306 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3308 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3314 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3316 $this->assertAPISuccess($contribution, 0);
3317 $values = $contribution['values'][$contribution['id']];
3318 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3319 // this is not returned in id format
3320 unset($params['payment_instrument_id']);
3321 $params['contribution_source'] = $params['source'];
3322 unset($params['source']);
3323 foreach ($params as $key => $value) {
3324 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3329 * Create a pending contribution & linked pending pledge record.
3331 public function createPendingPledgeContribution() {
3333 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
3334 $this->_ids
['pledge'] = $pledgeID;
3335 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
3336 'contribution_status_id' => 'Pending',
3337 'total_amount' => 500,
3340 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3341 'options' => array('limit' => 1),
3344 $this->callAPISuccess('PledgePayment', 'create', array(
3346 'contribution_id' =>
3347 $contribution['id'],
3348 'status_id' => 'Pending',
3349 'scheduled_amount' => 500,
3352 return $contribution['id'];
3356 * Create a pending contribution & linked pending participant record (along with an event).
3358 public function createPendingParticipantContribution() {
3359 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3360 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId
));
3361 $this->_ids
['participant'] = $participantID;
3362 $params = array_merge($this->_params
, array('contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3363 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3364 $this->callAPISuccess('participant_payment', 'create', array(
3365 'contribution_id' => $contribution['id'],
3366 'participant_id' => $participantID,
3368 $this->callAPISuccess('line_item', 'get', array(
3369 'entity_id' => $contribution['id'],
3370 'entity_table' => 'civicrm_contribution',
3371 'api.line_item.create' => array(
3372 'entity_id' => $participantID,
3373 'entity_table' => 'civicrm_participant',
3376 return $contribution['id'];
3380 * Get financial transaction amount.
3382 * @param int $contId
3384 * @return null|string
3386 public function _getFinancialTrxnAmount($contId) {
3388 SUM( ft.total_amount ) AS total
3389 FROM civicrm_financial_trxn AS ft
3390 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3391 WHERE ceft.entity_table = 'civicrm_contribution'
3392 AND ceft.entity_id = {$contId}";
3394 $result = CRM_Core_DAO
::singleValueQuery($query);
3399 * @param int $contId
3401 * @return null|string
3403 public function _getFinancialItemAmount($contId) {
3404 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3407 FROM civicrm_financial_item
3408 WHERE entity_table = 'civicrm_line_item'
3409 AND entity_id = {$lineItem}";
3410 $result = CRM_Core_DAO
::singleValueQuery($query);
3415 * @param int $contId
3418 public function _checkFinancialItem($contId, $context) {
3419 if ($context != 'paylater') {
3421 'entity_id' => $contId,
3422 'entity_table' => 'civicrm_contribution',
3424 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3425 $entityParams = array(
3426 'financial_trxn_id' => $trxn['financial_trxn_id'],
3427 'entity_table' => 'civicrm_financial_item',
3429 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3431 'id' => $entityTrxn['entity_id'],
3434 if ($context == 'paylater') {
3435 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3436 foreach ($lineItems as $key => $item) {
3438 'entity_id' => $key,
3439 'entity_table' => 'civicrm_line_item',
3441 $compareParams = array('status_id' => 1);
3442 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3445 elseif ($context == 'refund') {
3446 $compareParams = array(
3448 'financial_account_id' => 1,
3452 elseif ($context == 'cancelPending') {
3453 $compareParams = array(
3455 'financial_account_id' => 1,
3459 elseif ($context == 'changeFinancial') {
3460 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3462 'entity_id' => $lineKey,
3465 $compareParams = array(
3466 'financial_account_id' => 1,
3468 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3470 'financial_account_id' => 3,
3471 'entity_id' => $lineKey,
3473 $compareParams = array(
3477 if ($context != 'paylater') {
3478 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3483 * Check correct financial transaction entries were created for the change in payment instrument.
3485 * @param int $contributionID
3486 * @param int $originalInstrumentID
3487 * @param int $newInstrumentID
3489 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3491 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3493 $originalTrxnParams = array(
3494 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3495 'payment_instrument_id' => $originalInstrumentID,
3496 'amount' => $amount,
3500 $reversalTrxnParams = array(
3501 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3502 'payment_instrument_id' => $originalInstrumentID,
3503 'amount' => -$amount,
3507 $newTrxnParams = array(
3508 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3509 'payment_instrument_id' => $newInstrumentID,
3510 'amount' => $amount,
3514 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3515 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3516 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3518 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3519 'id' => $entityFinancialTrxn['financial_trxn_id'],
3521 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3522 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3523 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3524 $this->assertEquals(0, $financialTrxn['fee_amount']);
3525 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3526 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3529 $this->assertEquals(1, $financialTrxn['is_payment']);
3530 $this->assertEquals('USD', $financialTrxn['currency']);
3531 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3536 * Check financial transaction.
3538 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3540 * @param array $contribution
3541 * @param string $context
3542 * @param int $instrumentId
3543 * @param array $extraParams
3545 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3546 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3547 $trxn = array_pop($financialTrxns);
3550 'id' => $trxn['financial_trxn_id'],
3552 if ($context == 'payLater') {
3553 $compareParams = array(
3555 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3558 elseif ($context == 'refund') {
3559 $compareParams = array(
3560 'to_financial_account_id' => 6,
3561 'total_amount' => -100,
3563 'trxn_date' => '2015-01-01 09:00:00',
3564 'trxn_id' => 'the refund',
3567 elseif ($context == 'cancelPending') {
3568 $compareParams = array(
3569 'to_financial_account_id' => 7,
3570 'total_amount' => -100,
3574 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3575 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3576 // It does the same thing with greater readability.
3577 // @todo remove handling for
3579 $entityParams = array(
3580 'entity_id' => $contribution['id'],
3581 'entity_table' => 'civicrm_contribution',
3584 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3585 $trxnParams1 = array(
3586 'id' => $trxn['financial_trxn_id'],
3588 if (empty($extraParams)) {
3589 $compareParams = array(
3590 'total_amount' => -100,
3595 $compareParams = array(
3596 'total_amount' => 100,
3600 if ($context == 'paymentInstrument') {
3601 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3602 $compareParams['payment_instrument_id'] = $instrumentId;
3605 $compareParams['to_financial_account_id'] = 12;
3607 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3608 $compareParams['total_amount'] = 100;
3611 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3617 public function _addPaymentInstrument() {
3618 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3619 $optionParams = array(
3620 'option_group_id' => $gId,
3621 'label' => 'Test Card',
3622 'name' => 'Test Card',
3627 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3628 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3629 $financialParams = array(
3630 'entity_table' => 'civicrm_option_value',
3631 'entity_id' => $optionValue['id'],
3632 'account_relationship' => $relationTypeId,
3633 'financial_account_id' => 7,
3635 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3636 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3637 return $optionValue['values'][$optionValue['id']]['value'];
3640 public function _deletedAddedPaymentInstrument() {
3641 $result = $this->callAPISuccess('OptionValue', 'get', array(
3642 'option_group_id' => 'payment_instrument',
3643 'name' => 'Test Card',
3647 if ($id = CRM_Utils_Array
::value('id', $result)) {
3648 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3653 * Set up the basic recurring contribution for tests.
3655 * @param array $generalParams
3656 * Parameters that can be merged into the recurring AND the contribution.
3658 * @param array $recurParams
3659 * Parameters to merge into the recur only.
3663 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3664 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3665 'contact_id' => $this->_individualId
,
3666 'installments' => '12',
3667 'frequency_interval' => '1',
3669 'contribution_status_id' => 1,
3670 'start_date' => '2012-01-01 00:00:00',
3671 'currency' => 'USD',
3672 'frequency_unit' => 'month',
3673 'payment_processor_id' => $this->paymentProcessorID
,
3674 ), $generalParams, $recurParams));
3675 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3678 'contribution_recur_id' => $contributionRecur['id'],
3681 return $originalContribution;
3685 * Set up a basic auto-renew membership for tests.
3687 * @param array $generalParams
3688 * Parameters that can be merged into the recurring AND the contribution.
3690 * @param array $recurParams
3691 * Parameters to merge into the recur only.
3695 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3696 $newContact = $this->callAPISuccess('Contact', 'create', array(
3697 'contact_type' => 'Individual',
3698 'sort_name' => 'McTesterson, Testy',
3699 'display_name' => 'Testy McTesterson',
3700 'preferred_language' => 'en_US',
3701 'preferred_mail_format' => 'Both',
3702 'first_name' => 'Testy',
3703 'last_name' => 'McTesterson',
3704 'contact_is_deleted' => '0',
3706 'email' => 'tmctesterson@example.com',
3709 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3710 'domain_id' => "Default Domain Name",
3711 'member_of_contact_id' => 1,
3712 'financial_type_id' => "Member Dues",
3713 'duration_unit' => "month",
3714 'duration_interval' => 1,
3715 'period_type' => 'rolling',
3716 'name' => "Standard Member",
3717 'minimum_fee' => 100,
3719 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3720 'contact_id' => $newContact['id'],
3721 'installments' => '12',
3722 'frequency_interval' => '1',
3724 'contribution_status_id' => 1,
3725 'start_date' => '2012-01-01 00:00:00',
3726 'currency' => 'USD',
3727 'frequency_unit' => 'month',
3728 'payment_processor_id' => $this->paymentProcessorID
,
3729 ), $generalParams, $recurParams));
3731 $membership = $this->callAPISuccess('membership', 'create', array(
3732 'contact_id' => $newContact['id'],
3733 'contribution_recur_id' => $contributionRecur['id'],
3734 'financial_type_id' => "Member Dues",
3735 'membership_type_id' => $membershipType['id'],
3737 'skipLineItem' => TRUE,
3740 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3741 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3744 'contact_id' => $newContact['id'],
3745 'contribution_recur_id' => $contributionRecur['id'],
3746 'financial_type_id' => "Member Dues",
3747 'contribution_status_id' => 1,
3748 'invoice_id' => uniqid(),
3751 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3752 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3753 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3754 $this->callAPISuccess('LineItem', 'getsingle', array());
3755 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3757 return array($originalContribution, $membership);
3760 * Set up a repeat transaction.
3762 * @param array $recurParams
3766 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3767 $paymentProcessorID = $this->paymentProcessorCreate();
3768 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3769 'contact_id' => $this->_individualId
,
3770 'installments' => '12',
3771 'frequency_interval' => '1',
3773 'contribution_status_id' => 1,
3774 'start_date' => '2012-01-01 00:00:00',
3775 'currency' => 'USD',
3776 'frequency_unit' => 'month',
3777 'payment_processor_id' => $paymentProcessorID,
3780 $originalContribution = '';
3781 if ($flag == 'multiple') {
3782 // CRM-19309 create a contribution + also add in line_items (plural):
3783 $params = array_merge($this->_params
, $contributionParams);
3784 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3787 'contribution_recur_id' => $contributionRecur['id'],
3788 'skipLineItem' => 1,
3789 'api.line_item.create' => array(
3791 'price_field_id' => 1,
3793 'line_total' => '20',
3794 'unit_price' => '10',
3795 'financial_type_id' => 1,
3798 'price_field_id' => 1,
3800 'line_total' => '80',
3801 'unit_price' => '80',
3802 'financial_type_id' => 2,
3809 elseif ($flag == 'single') {
3810 $params = array_merge($this->_params
, array('contribution_recur_id' => $contributionRecur['id']));
3811 $params = array_merge($params, $contributionParams);
3812 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3814 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3815 return $originalContribution;
3819 * Common set up routine.
3823 protected function setUpForCompleteTransaction() {
3824 $this->mut
= new CiviMailUtils($this, TRUE);
3825 $this->createLoggedInUser();
3826 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3827 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3828 return $contribution;
3832 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3834 public function testRepeatTransactionWithNonCreditCardDefault() {
3835 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3836 'contact_id' => $this->_individualId
,
3837 'installments' => '12',
3838 'frequency_interval' => '1',
3840 'contribution_status_id' => 1,
3841 'start_date' => '2012-01-01 00:00:00',
3842 'currency' => 'USD',
3843 'frequency_unit' => 'month',
3844 'payment_processor_id' => $this->paymentProcessorID
,
3846 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3848 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3850 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
3851 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3852 'contribution_status_id' => 'Completed',
3853 'trxn_id' => uniqid(),
3854 'original_contribution_id' => $contribution1,
3856 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3857 $this->quickCleanUpFinancialEntities();
3861 * CRM-20008 Tests repeattransaction creates pending membership.
3863 public function testRepeatTransactionPendingMembership() {
3864 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3865 $this->callAPISuccess('membership', 'create', array(
3866 'id' => $membership['id'],
3867 'end_date' => 'yesterday',
3868 'status_id' => 'Expired',
3870 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3871 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3872 'contribution_status_id' => 'Pending',
3873 'trxn_id' => uniqid(),
3875 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3876 'id' => $membership['id'],
3877 'return' => 'status_id',
3880 // Let's see if the membership payments got created while we're at it.
3881 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3882 'memberhip_id' => $membership['id'],
3884 $this->assertEquals(2, $membershipPayments['count']);
3886 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3887 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3888 $membership = $this->callAPISuccessGetSingle('membership', array(
3889 'id' => $membership['id'],
3890 'return' => 'status_id, end_date',
3892 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3893 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3895 $this->quickCleanUpFinancialEntities();
3896 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3900 * Test sending a mail via the API.
3902 public function testSendMailWithAPISetFromDetails() {
3903 $mut = new CiviMailUtils($this, TRUE);
3904 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3905 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3906 'id' => $contribution['id'],
3907 'receipt_from_email' => 'api@civicrm.org',
3908 'receipt_from_name' => 'CiviCRM LLC',
3910 $mut->checkMailLog(array(
3911 'From: CiviCRM LLC <api@civicrm.org>',
3912 'Contribution Information',
3913 'Please print this confirmation for your records',
3922 * Test sending a mail via the API.
3924 public function testSendMailWithNoFromSetFallToDomain() {
3925 $this->createLoggedInUser();
3926 $mut = new CiviMailUtils($this, TRUE);
3927 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3928 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3929 'id' => $contribution['id'],
3931 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3932 $mut->checkMailLog(array(
3933 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3934 'Contribution Information',
3935 'Please print this confirmation for your records',
3944 * Test sending a mail via the API.
3946 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3947 $this->createLoggedInUser();
3948 $mut = new CiviMailUtils($this, TRUE);
3949 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3950 $this->callAPISuccess('contribution', 'repeattransaction', array(
3951 'contribution_status_id' => 'Completed',
3952 'trxn_id' => uniqid(),
3953 'original_contribution_id' => $contribution,
3955 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3956 $mut->checkMailLog(array(
3957 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3958 'Contribution Information',
3959 'Please print this confirmation for your records',
3968 * Test sending a mail via the API.
3970 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3971 $mut = new CiviMailUtils($this, TRUE);
3972 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3973 $paymentProcessorID = $this->paymentProcessorCreate();
3974 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3975 'contact_id' => $this->_individualId
,
3976 'installments' => '12',
3977 'frequency_interval' => '1',
3979 'contribution_status_id' => 1,
3980 'start_date' => '2012-01-01 00:00:00',
3981 'currency' => 'USD',
3982 'frequency_unit' => 'month',
3983 'payment_processor_id' => $paymentProcessorID,
3985 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3988 'contribution_recur_id' => $contributionRecur['id'],
3989 'contribution_page_id' => $contributionPage['id']))
3991 $this->callAPISuccess('contribution', 'repeattransaction', array(
3992 'contribution_status_id' => 'Completed',
3993 'trxn_id' => uniqid(),
3994 'original_contribution_id' => $originalContribution,
3997 $mut->checkMailLog(array(
3998 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3999 'Contribution Information',
4000 'Please print this confirmation for your records',
4009 * Test sending a mail via the API.
4011 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4012 $mut = new CiviMailUtils($this, TRUE);
4013 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
4014 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
4015 foreach ($fromEmail['values'] as $from) {
4016 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
4018 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
4019 $this->callAPISuccess('contribution', 'repeattransaction', array(
4020 'contribution_status_id' => 'Completed',
4021 'trxn_id' => uniqid(),
4022 'original_contribution_id' => $originalContribution,
4025 $mut->checkMailLog(array(
4026 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4027 'Contribution Information',
4028 'Please print this confirmation for your records',
4037 * Create a Contribution Page with is_email_receipt = TRUE.
4039 * @param array $params
4040 * Params to overwrite with.
4044 protected function createReceiptableContributionPage($params = array()) {
4045 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
4046 'receipt_from_name' => 'Mickey Mouse',
4047 'receipt_from_email' => 'mickey@mouse.com',
4048 'title' => "Test Contribution Page",
4049 'financial_type_id' => 1,
4050 'currency' => 'CAD',
4051 'is_monetary' => TRUE,
4052 'is_email_receipt' => TRUE,
4054 return $contributionPage;
4058 * function to test card_type and pan truncation.
4060 public function testCardTypeAndPanTruncation() {
4061 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
4062 $contactId = $this->individualCreate();
4064 'contact_id' => $contactId,
4065 'receive_date' => '2016-01-20',
4066 'total_amount' => 100,
4067 'financial_type_id' => 1,
4068 'payment_instrument' => 'Credit Card',
4069 'card_type_id' => $creditCardTypeIDs['Visa'],
4070 'pan_truncation' => 4567,
4072 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4073 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
4074 $financialTrxn = $this->callAPISuccessGetSingle(
4077 'id' => $lastFinancialTrxnId['financialTrxnId'],
4078 'return' => array('card_type_id', 'pan_truncation'),
4081 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4082 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
4084 'id' => $contribution['id'],
4085 'pan_truncation' => 2345,
4086 'card_type_id' => $creditCardTypeIDs['Amex'],
4088 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4089 $financialTrxn = $this->callAPISuccessGetSingle(
4092 'id' => $lastFinancialTrxnId['financialTrxnId'],
4093 'return' => array('card_type_id', 'pan_truncation'),
4096 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4097 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
4101 * Test repeat contribution uses non default currency
4102 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4104 public function testRepeatTransactionWithDifferenceCurrency() {
4105 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4106 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4107 'original_contribution_id' => $originalContribution['id'],
4108 'contribution_status_id' => 'Completed',
4109 'trxn_id' => uniqid(),
4111 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4115 * Get the financial items for the contribution.
4117 * @param int $contributionID
4120 * Array of associated financial items.
4122 protected function getFinancialTransactionsForContribution($contributionID) {
4123 $trxnParams = array(
4124 'entity_id' => $contributionID,
4125 'entity_table' => 'civicrm_contribution',
4127 // @todo the following function has naming errors & has a weird signature & appears to
4128 // only be called from test classes. Move into test suite & maybe just use api
4129 // from this function.
4130 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));