3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 public function testCreateDefaultNow() {
688 $params = $this->_params
;
689 unset($params['receive_date']);
691 $contribution = $this->callAPISuccess('contribution', 'create', $params);
692 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
693 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
697 * Create test with unique field name on source.
699 public function testCreateContributionNullOutThankyouDate() {
701 $params = $this->_params
;
702 $params['thankyou_date'] = 'yesterday';
704 $contribution = $this->callAPISuccess('contribution', 'create', $params);
705 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
706 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
708 $params['thankyou_date'] = 'null';
709 $contribution = $this->callAPISuccess('contribution', 'create', $params);
710 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
714 * Create test with unique field name on source.
716 public function testCreateContributionSourceInvalidContact() {
720 'receive_date' => date('Ymd'),
721 'total_amount' => 100.00,
722 'financial_type_id' => $this->_financialTypeId
,
723 'payment_instrument_id' => 1,
724 'non_deductible_amount' => 10.00,
725 'fee_amount' => 50.00,
726 'net_amount' => 90.00,
728 'invoice_id' => 67890,
729 'contribution_source' => 'SSF',
730 'contribution_status_id' => 1,
733 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
736 public function testCreateContributionSourceInvalidContContact() {
739 'contribution_contact_id' => 999,
740 'receive_date' => date('Ymd'),
741 'total_amount' => 100.00,
742 'financial_type_id' => $this->_financialTypeId
,
743 'payment_instrument_id' => 1,
744 'non_deductible_amount' => 10.00,
745 'fee_amount' => 50.00,
746 'net_amount' => 90.00,
748 'invoice_id' => 67890,
749 'contribution_source' => 'SSF',
750 'contribution_status_id' => 1,
753 $this->callAPIFailure('contribution', 'create', $params);
757 * Test note created correctly.
759 public function testCreateContributionWithNote() {
760 $description = "Demonstrates creating contribution with Note Entity.";
761 $subfile = "ContributionCreateWithNote";
763 'contact_id' => $this->_individualId
,
764 'receive_date' => '2012-01-01',
765 'total_amount' => 100.00,
766 'financial_type_id' => $this->_financialTypeId
,
767 'payment_instrument_id' => 1,
768 'non_deductible_amount' => 10.00,
769 'fee_amount' => 50.00,
770 'net_amount' => 90.00,
772 'invoice_id' => 67890,
774 'contribution_status_id' => 1,
775 'note' => 'my contribution note',
778 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
779 $result = $this->callAPISuccess('note', 'get', array(
780 'entity_table' => 'civicrm_contribution',
781 'entity_id' => $contribution['id'],
784 $this->assertEquals('my contribution note', $result['values'][0]['note']);
785 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
788 public function testCreateContributionWithNoteUniqueNameAliases() {
790 'contact_id' => $this->_individualId
,
791 'receive_date' => '2012-01-01',
792 'total_amount' => 100.00,
793 'financial_type_id' => $this->_financialTypeId
,
794 'payment_instrument_id' => 1,
795 'non_deductible_amount' => 10.00,
796 'fee_amount' => 50.00,
797 'net_amount' => 90.00,
799 'invoice_id' => 67890,
801 'contribution_status_id' => 1,
802 'contribution_note' => 'my contribution note',
805 $contribution = $this->callAPISuccess('contribution', 'create', $params);
806 $result = $this->callAPISuccess('note', 'get', array(
807 'entity_table' => 'civicrm_contribution',
808 'entity_id' => $contribution['id'],
811 $this->assertEquals('my contribution note', $result['values'][0]['note']);
812 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
816 * This is the test for creating soft credits.
818 public function testCreateContributionWithSoftCredit() {
819 $description = "Demonstrates creating contribution with SoftCredit.";
820 $subfile = "ContributionCreateWithSoftCredit";
821 $contact2 = $this->callAPISuccess('Contact', 'create', array(
822 'display_name' => 'superman',
823 'contact_type' => 'Individual',
826 'contact_id' => $contact2['id'],
828 'soft_credit_type_id' => 3,
831 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
832 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
833 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
835 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
836 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
837 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
839 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
840 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
843 public function testCreateContributionWithSoftCreditDefaults() {
844 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
845 $subfile = "ContributionCreateWithSoftCreditDefaults";
846 $contact2 = $this->callAPISuccess('Contact', 'create', array(
847 'display_name' => 'superman',
848 'contact_type' => 'Individual',
850 $params = $this->_params +
array(
851 'soft_credit_to' => $contact2['id'],
853 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
854 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
856 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
857 // Default soft credit amount = contribution.total_amount
858 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
859 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
861 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
862 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
866 * Test creating contribution with Soft Credit by passing in honor_contact_id.
868 public function testCreateContributionWithHonoreeContact() {
869 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
870 $subfile = "ContributionCreateWithHonoreeContact";
871 $contact2 = $this->callAPISuccess('Contact', 'create', array(
872 'display_name' => 'superman',
873 'contact_type' => 'Individual',
875 $params = $this->_params +
array(
876 'honor_contact_id' => $contact2['id'],
878 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
879 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
881 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
882 // Default soft credit amount = contribution.total_amount
883 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
884 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
885 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
886 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
888 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
889 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
893 * Test using example code.
895 public function testContributionCreateExample() {
896 //make sure at least on page exists since there is a truncate in tear down
897 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
898 require_once 'api/v3/examples/Contribution/Create.php';
899 $result = contribution_create_example();
901 $expectedResult = contribution_create_expectedresult();
902 $this->checkArrayEquals($expectedResult, $result);
903 $this->contributionDelete($id);
907 * Function tests that additional financial records are created when fee amount is recorded.
909 public function testCreateContributionWithFee() {
911 'contact_id' => $this->_individualId
,
912 'receive_date' => '20120511',
913 'total_amount' => 100.00,
915 'financial_type_id' => 1,
917 'invoice_id' => 67890,
919 'contribution_status_id' => 1,
922 $contribution = $this->callAPISuccess('contribution', 'create', $params);
923 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
924 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
925 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
926 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
927 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
928 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
929 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
930 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
931 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
933 $lineItems = $this->callAPISuccess('line_item', 'get', array(
935 'entity_id' => $contribution['id'],
936 'entity_table' => 'civicrm_contribution',
939 $this->assertEquals(1, $lineItems['count']);
940 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
941 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
942 $lineItems = $this->callAPISuccess('line_item', 'get', array(
944 'entity_id' => $contribution['id'],
945 'contribution_id' => $contribution['id'],
946 'entity_table' => 'civicrm_contribution',
949 $this->assertEquals(1, $lineItems['count']);
950 $this->_checkFinancialRecords($contribution, 'feeAmount');
955 * Function tests that additional financial records are created when online contribution is created.
957 public function testCreateContributionOnline() {
958 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
959 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
960 $this->assertAPISuccess($contributionPage);
962 'contact_id' => $this->_individualId
,
963 'receive_date' => '20120511',
964 'total_amount' => 100.00,
965 'financial_type_id' => 1,
966 'contribution_page_id' => $contributionPage['id'],
967 'payment_processor' => $this->paymentProcessorID
,
969 'invoice_id' => 67890,
971 'contribution_status_id' => 1,
975 $contribution = $this->callAPISuccess('contribution', 'create', $params);
976 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
977 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
978 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
979 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
980 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
981 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
982 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
983 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
984 'id' => $this->paymentProcessorID
,
985 'return' => 'payment_instrument_id',
987 $this->_checkFinancialRecords($contribution, 'online');
991 * Check handling of financial type.
993 * In the interests of removing financial type / contribution type checks from
994 * legacy format function lets test that the api is doing this for us
996 public function testCreateInvalidFinancialType() {
997 $params = $this->_params
;
998 $params['financial_type_id'] = 99999;
999 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
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 testValidNamedFinancialType() {
1009 $params = $this->_params
;
1010 $params['financial_type_id'] = 'Donation';
1011 $this->callAPISuccess($this->_entity
, 'create', $params);
1015 * Tests that additional financial records are created.
1017 * Checks when online contribution with pay later option is created
1019 public function testCreateContributionPayLaterOnline() {
1020 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1021 $this->_pageParams
['is_pay_later'] = 1;
1022 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1023 $this->assertAPISuccess($contributionPage);
1025 'contact_id' => $this->_individualId
,
1026 'receive_date' => '20120511',
1027 'total_amount' => 100.00,
1028 'financial_type_id' => 1,
1029 'contribution_page_id' => $contributionPage['id'],
1031 'is_pay_later' => 1,
1032 'invoice_id' => 67890,
1034 'contribution_status_id' => 2,
1038 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1039 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1040 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1041 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1042 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1043 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1044 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1045 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1046 $this->_checkFinancialRecords($contribution, 'payLater');
1050 * Function tests that additional financial records are created for online contribution with pending option.
1052 public function testCreateContributionPendingOnline() {
1053 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1054 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1055 $this->assertAPISuccess($contributionPage);
1057 'contact_id' => $this->_individualId
,
1058 'receive_date' => '20120511',
1059 'total_amount' => 100.00,
1060 'financial_type_id' => 1,
1061 'contribution_page_id' => $contributionPage['id'],
1063 'invoice_id' => 67890,
1065 'contribution_status_id' => 2,
1068 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1069 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1070 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1071 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1072 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1073 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1074 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1075 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1076 $this->_checkFinancialRecords($contribution, 'pending');
1080 * Test that BAO defaults work.
1082 public function testCreateBAODefaults() {
1083 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1084 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1085 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1086 'id' => $contribution['id'],
1087 'api.contribution.delete' => 1,
1089 $this->assertEquals(1, $contribution['contribution_status_id']);
1090 $this->assertEquals('Check', $contribution['payment_instrument']);
1094 * Function tests that line items, financial records are updated when contribution amount is changed.
1096 public function testCreateUpdateContributionChangeTotal() {
1097 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1098 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1100 'entity_id' => $contribution['id'],
1101 'entity_table' => 'civicrm_contribution',
1103 'return' => 'line_total',
1105 $this->assertEquals('100.00', $lineItems);
1106 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1107 // Financial trxn SUM = 100 + 5 (fee)
1108 $this->assertEquals('105.00', $trxnAmount);
1111 'id' => $contribution['id'],
1112 'total_amount' => '125',
1114 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1116 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1118 'entity_id' => $contribution['id'],
1119 'entity_table' => 'civicrm_contribution',
1121 'return' => 'line_total',
1124 $this->assertEquals('125.00', $lineItems);
1125 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1127 // Financial trxn SUM = 125 + 5 (fee).
1128 $this->assertEquals('130.00', $trxnAmount);
1129 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1133 * Function tests that line items, financial records are updated when pay later contribution is received.
1135 public function testCreateUpdateContributionPayLater() {
1136 $contribParams = array(
1137 'contact_id' => $this->_individualId
,
1138 'receive_date' => '2012-01-01',
1139 'total_amount' => 100.00,
1140 'financial_type_id' => $this->_financialTypeId
,
1141 'payment_instrument_id' => 1,
1142 'contribution_status_id' => 2,
1143 'is_pay_later' => 1,
1146 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1148 $newParams = array_merge($contribParams, array(
1149 'id' => $contribution['id'],
1150 'contribution_status_id' => 1,
1153 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1154 $contribution = $contribution['values'][$contribution['id']];
1155 $this->assertEquals($contribution['contribution_status_id'], '1');
1156 $this->_checkFinancialItem($contribution['id'], 'paylater');
1157 $this->_checkFinancialTrxn($contribution, 'payLater');
1161 * Function tests that financial records are updated when Payment Instrument is changed.
1163 public function testCreateUpdateContributionPaymentInstrument() {
1164 $instrumentId = $this->_addPaymentInstrument();
1165 $contribParams = array(
1166 'contact_id' => $this->_individualId
,
1167 'total_amount' => 100.00,
1168 'financial_type_id' => $this->_financialTypeId
,
1169 'payment_instrument_id' => 4,
1170 'contribution_status_id' => 1,
1173 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1175 $newParams = array_merge($contribParams, array(
1176 'id' => $contribution['id'],
1177 'payment_instrument_id' => $instrumentId,
1180 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1181 $this->assertAPISuccess($contribution);
1182 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1184 // cleanup - delete created payment instrument
1185 $this->_deletedAddedPaymentInstrument();
1189 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1191 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1192 $instrumentId = $this->_addPaymentInstrument();
1193 $contribParams = array(
1194 'contact_id' => $this->_individualId
,
1195 'total_amount' => -100.00,
1196 'financial_type_id' => $this->_financialTypeId
,
1197 'payment_instrument_id' => 4,
1198 'contribution_status_id' => 1,
1201 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1203 $newParams = array_merge($contribParams, array(
1204 'id' => $contribution['id'],
1205 'payment_instrument_id' => $instrumentId,
1208 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1209 $this->assertAPISuccess($contribution);
1210 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1212 // cleanup - delete created payment instrument
1213 $this->_deletedAddedPaymentInstrument();
1217 * Function tests that financial records are added when Contribution is Refunded.
1219 public function testCreateUpdateContributionRefund() {
1220 $contributionParams = array(
1221 'contact_id' => $this->_individualId
,
1222 'receive_date' => '2012-01-01',
1223 'total_amount' => 100.00,
1224 'financial_type_id' => $this->_financialTypeId
,
1225 'payment_instrument_id' => 4,
1226 'contribution_status_id' => 1,
1227 'trxn_id' => 'original_payment',
1229 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1230 $newParams = array_merge($contributionParams, array(
1231 'id' => $contribution['id'],
1232 'contribution_status_id' => 'Refunded',
1233 'cancel_date' => '2015-01-01 09:00',
1234 'refund_trxn_id' => 'the refund',
1238 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1239 $this->_checkFinancialTrxn($contribution, 'refund');
1240 $this->_checkFinancialItem($contribution['id'], 'refund');
1241 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1242 'id' => $contribution['id'],
1243 'return' => 'trxn_id',
1248 * Refund a contribution for a financial type with a contra account.
1250 * CRM-17951 the contra account is a financial account with a relationship to a
1251 * financial type. It is not always configured but should be reflected
1252 * in the financial_trxn & financial_item table if it is.
1254 public function testCreateUpdateChargebackContributionDefaultAccount() {
1255 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1256 $this->callAPISuccess('Contribution', 'create', array(
1257 'id' => $contribution['id'],
1258 'contribution_status_id' => 'Chargeback',
1260 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1262 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1263 'contribution_id' => $contribution['id'],
1264 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1266 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1267 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1268 'total_amount' => -100,
1269 'status_id' => 'Chargeback',
1270 'to_financial_account_id' => 6,
1275 * Refund a contribution for a financial type with a contra account.
1277 * CRM-17951 the contra account is a financial account with a relationship to a
1278 * financial type. It is not always configured but should be reflected
1279 * in the financial_trxn & financial_item table if it is.
1281 public function testCreateUpdateChargebackContributionCustomAccount() {
1282 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1283 'name' => 'Chargeback Account',
1284 'is_active' => TRUE,
1287 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1288 'entity_id' => $this->_financialTypeId
,
1289 'entity_table' => 'civicrm_financial_type',
1290 'account_relationship' => 'Chargeback Account is',
1291 'financial_account_id' => 'Chargeback Account',
1294 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1295 $this->callAPISuccess('Contribution', 'create', array(
1296 'id' => $contribution['id'],
1297 'contribution_status_id' => 'Chargeback',
1299 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1301 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1302 'contribution_id' => $contribution['id'],
1303 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1305 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1307 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1308 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1309 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1313 * Refund a contribution for a financial type with a contra account.
1315 * CRM-17951 the contra account is a financial account with a relationship to a
1316 * financial type. It is not always configured but should be reflected
1317 * in the financial_trxn & financial_item table if it is.
1319 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1320 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1321 'name' => 'Refund Account',
1322 'is_active' => TRUE,
1325 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1326 'entity_id' => $this->_financialTypeId
,
1327 'entity_table' => 'civicrm_financial_type',
1328 'account_relationship' => 'Credit/Contra Revenue Account is',
1329 'financial_account_id' => 'Refund Account',
1332 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1333 $this->callAPISuccess('Contribution', 'create', array(
1334 'id' => $contribution['id'],
1335 'contribution_status_id' => 'Refunded',
1338 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1339 'contribution_id' => $contribution['id'],
1340 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1342 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1344 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1345 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1346 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1350 * Function tests that trxn_id is set when passed in.
1352 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1353 * when trxn_id is passed in.
1355 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1356 $contributionParams = array(
1357 'contact_id' => $this->_individualId
,
1358 'receive_date' => '2012-01-01',
1359 'total_amount' => 100.00,
1360 'financial_type_id' => $this->_financialTypeId
,
1361 'payment_instrument_id' => 4,
1362 'contribution_status_id' => 1,
1363 'trxn_id' => 'original_payment',
1365 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1366 $newParams = array_merge($contributionParams, array(
1367 'id' => $contribution['id'],
1368 'contribution_status_id' => 'Refunded',
1369 'cancel_date' => '2015-01-01 09:00',
1370 'trxn_id' => 'the refund',
1374 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1375 $this->_checkFinancialTrxn($contribution, 'refund');
1376 $this->_checkFinancialItem($contribution['id'], 'refund');
1377 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1378 'id' => $contribution['id'],
1379 'return' => 'trxn_id',
1384 * Function tests that trxn_id is set when passed in.
1386 * Here we ensure that the civicrm_contribution.trxn_id is set
1387 * when trxn_id is passed in but if refund_trxn_id is different then that
1388 * is kept for the refund transaction.
1390 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1391 $contributionParams = array(
1392 'contact_id' => $this->_individualId
,
1393 'receive_date' => '2012-01-01',
1394 'total_amount' => 100.00,
1395 'financial_type_id' => $this->_financialTypeId
,
1396 'payment_instrument_id' => 4,
1397 'contribution_status_id' => 1,
1398 'trxn_id' => 'original_payment',
1400 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1401 $newParams = array_merge($contributionParams, array(
1402 'id' => $contribution['id'],
1403 'contribution_status_id' => 'Refunded',
1404 'cancel_date' => '2015-01-01 09:00',
1405 'trxn_id' => 'cont id',
1406 'refund_trxn_id' => 'the refund',
1410 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1411 $this->_checkFinancialTrxn($contribution, 'refund');
1412 $this->_checkFinancialItem($contribution['id'], 'refund');
1413 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1414 'id' => $contribution['id'],
1415 'return' => 'trxn_id',
1420 * Function tests that refund_trxn_id is set when passed in empty.
1422 * Here we ensure that the civicrm_contribution.trxn_id is set
1423 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1424 * is kept for the refund transaction.
1426 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1427 $contributionParams = array(
1428 'contact_id' => $this->_individualId
,
1429 'receive_date' => '2012-01-01',
1430 'total_amount' => 100.00,
1431 'financial_type_id' => $this->_financialTypeId
,
1432 'payment_instrument_id' => 4,
1433 'contribution_status_id' => 1,
1434 'trxn_id' => 'original_payment',
1436 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1437 $newParams = array_merge($contributionParams, array(
1438 'id' => $contribution['id'],
1439 'contribution_status_id' => 'Refunded',
1440 'cancel_date' => '2015-01-01 09:00',
1441 'trxn_id' => 'cont id',
1442 'refund_trxn_id' => '',
1446 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1447 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1448 $this->_checkFinancialItem($contribution['id'], 'refund');
1449 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1450 'id' => $contribution['id'],
1451 'return' => 'trxn_id',
1456 * Function tests invalid contribution status change.
1458 public function testCreateUpdateContributionInValidStatusChange() {
1459 $contribParams = array(
1461 'receive_date' => '2012-01-01',
1462 'total_amount' => 100.00,
1463 'financial_type_id' => 1,
1464 'payment_instrument_id' => 1,
1465 'contribution_status_id' => 1,
1467 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1468 $newParams = array_merge($contribParams, array(
1469 'id' => $contribution['id'],
1470 'contribution_status_id' => 2,
1473 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1478 * Function tests that financial records are added when Pending Contribution is Canceled.
1480 public function testCreateUpdateContributionCancelPending() {
1481 $contribParams = array(
1482 'contact_id' => $this->_individualId
,
1483 'receive_date' => '2012-01-01',
1484 'total_amount' => 100.00,
1485 'financial_type_id' => $this->_financialTypeId
,
1486 'payment_instrument_id' => 1,
1487 'contribution_status_id' => 2,
1488 'is_pay_later' => 1,
1491 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1492 $newParams = array_merge($contribParams, array(
1493 'id' => $contribution['id'],
1494 'contribution_status_id' => 3,
1495 'cancel_date' => '2012-02-02 09:00',
1498 //Check if trxn_date is same as cancel_date.
1499 $checkTrxnDate = array(
1500 'trxn_date' => '2012-02-02 09:00:00',
1502 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1503 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1504 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1508 * Function tests that financial records are added when Financial Type is Changed.
1510 public function testCreateUpdateContributionChangeFinancialType() {
1511 $contribParams = array(
1512 'contact_id' => $this->_individualId
,
1513 'receive_date' => '2012-01-01',
1514 'total_amount' => 100.00,
1515 'financial_type_id' => 1,
1516 'payment_instrument_id' => 1,
1517 'contribution_status_id' => 1,
1520 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1521 $newParams = array_merge($contribParams, array(
1522 'id' => $contribution['id'],
1523 'financial_type_id' => 3,
1526 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1527 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1528 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1532 * Test that update does not change status id CRM-15105.
1534 public function testCreateUpdateWithoutChangingPendingStatus() {
1535 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1536 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1537 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1538 'id' => $contribution['id'],
1539 'api.contribution.delete' => 1,
1541 $this->assertEquals(2, $contribution['contribution_status_id']);
1545 * Test Updating a Contribution.
1547 * CHANGE: we require the API to do an incremental update
1549 public function testCreateUpdateContribution() {
1550 $contributionID = $this->contributionCreate(array(
1551 'contact_id' => $this->_individualId
,
1552 'trxn_id' => 212355,
1553 'financial_type_id' => $this->_financialTypeId
,
1554 'invoice_id' => 'old_invoice',
1556 $old_params = array(
1557 'contribution_id' => $contributionID,
1559 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1560 $this->assertEquals($original['id'], $contributionID);
1561 //set up list of old params, verify
1563 //This should not be required on update:
1564 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1565 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1566 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1567 $old_source = $original['values'][$contributionID]['contribution_source'];
1569 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1570 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1572 //check against values in CiviUnitTestCase::createContribution()
1573 $this->assertEquals($old_contact_id, $this->_individualId
);
1574 $this->assertEquals($old_fee_amount, 5.00);
1575 $this->assertEquals($old_source, 'SSF');
1576 $this->assertEquals($old_trxn_id, 212355);
1577 $this->assertEquals($old_invoice_id, 'old_invoice');
1579 'id' => $contributionID,
1580 'contact_id' => $this->_individualId
,
1581 'total_amount' => 105.00,
1582 'fee_amount' => 7.00,
1583 'financial_type_id' => $this->_financialTypeId
,
1584 'non_deductible_amount' => 22.00,
1585 'contribution_status_id' => 1,
1586 'note' => 'Donating for Noble Cause',
1589 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1591 $new_params = array(
1592 'contribution_id' => $contribution['id'],
1595 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1597 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1598 $this->assertEquals($contribution['total_amount'], 105.00);
1599 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1600 $this->assertEquals($contribution['financial_type'], 'Donation');
1601 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1602 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1603 $this->assertEquals($contribution['fee_amount'], 7.00);
1604 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1605 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1606 $this->assertEquals($contribution['contribution_source'], $old_source);
1607 $this->assertEquals($contribution['contribution_status'], 'Completed');
1609 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1612 'contribution_id' => $contributionID,
1615 $result = $this->callAPISuccess('contribution', 'delete', $params);
1616 $this->assertAPISuccess($result);
1620 * Attempt (but fail) to delete a contribution without parameters.
1622 public function testDeleteEmptyParamsContribution() {
1624 $this->callAPIFailure('contribution', 'delete', $params);
1627 public function testDeleteParamsNotArrayContribution() {
1628 $params = 'contribution_id= 1';
1629 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1630 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1633 public function testDeleteWrongParamContribution() {
1635 'contribution_source' => 'SSF',
1638 $this->callAPIFailure('contribution', 'delete', $params);
1641 public function testDeleteContribution() {
1642 $contributionID = $this->contributionCreate(array(
1643 'contact_id' => $this->_individualId
,
1644 'financial_type_id' => $this->_financialTypeId
,
1647 'id' => $contributionID,
1649 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1653 * Test civicrm_contribution_search with empty params.
1655 * All available contributions expected.
1657 public function testSearchEmptyParams() {
1661 'contact_id' => $this->_individualId
,
1662 'receive_date' => date('Ymd'),
1663 'total_amount' => 100.00,
1664 'financial_type_id' => $this->_financialTypeId
,
1665 'non_deductible_amount' => 10.00,
1666 'fee_amount' => 5.00,
1667 'net_amount' => 95.00,
1669 'invoice_id' => 78910,
1671 'contribution_status_id' => 1,
1674 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1676 $result = $this->callAPISuccess('contribution', 'get', $params);
1677 // We're taking the first element.
1678 $res = $result['values'][$contribution['id']];
1680 $this->assertEquals($p['contact_id'], $res['contact_id']);
1681 $this->assertEquals($p['total_amount'], $res['total_amount']);
1682 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1683 $this->assertEquals($p['net_amount'], $res['net_amount']);
1684 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1685 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1686 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1687 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1688 $this->assertEquals($p['source'], $res['contribution_source']);
1689 // contribution_status_id = 1 => Completed
1690 $this->assertEquals('Completed', $res['contribution_status']);
1692 $this->contributionDelete($contribution['id']);
1696 * Test civicrm_contribution_search. Success expected.
1698 public function testSearch() {
1700 'contact_id' => $this->_individualId
,
1701 'receive_date' => date('Ymd'),
1702 'total_amount' => 100.00,
1703 'financial_type_id' => $this->_financialTypeId
,
1704 'non_deductible_amount' => 10.00,
1705 'contribution_status_id' => 1,
1708 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1711 'contact_id' => $this->_individualId
,
1712 'receive_date' => date('Ymd'),
1713 'total_amount' => 200.00,
1714 'financial_type_id' => $this->_financialTypeId
,
1715 'non_deductible_amount' => 20.00,
1716 'trxn_id' => 5454565,
1717 'invoice_id' => 1212124,
1718 'fee_amount' => 50.00,
1719 'net_amount' => 60.00,
1720 'contribution_status_id' => 2,
1723 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1726 'contribution_id' => $contribution2['id'],
1729 $result = $this->callAPISuccess('contribution', 'get', $params);
1730 $res = $result['values'][$contribution2['id']];
1732 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1733 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1734 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1735 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1736 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1737 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1738 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1739 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1740 // contribution_status_id = 2 => Pending
1741 $this->assertEquals('Pending', $res['contribution_status']);
1743 $this->contributionDelete($contribution1['id']);
1744 $this->contributionDelete($contribution2['id']);
1748 * Test completing a transaction via the API.
1750 * Note that we are creating a logged in user because email goes out from
1753 public function testCompleteTransaction() {
1754 $mut = new CiviMailUtils($this, TRUE);
1755 $this->swapMessageTemplateForTestTemplate();
1756 $this->createLoggedInUser();
1757 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1758 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1759 $this->callAPISuccess('contribution', 'completetransaction', array(
1760 'id' => $contribution['id'],
1762 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1763 $this->assertEquals('SSF', $contribution['contribution_source']);
1764 $this->assertEquals('Completed', $contribution['contribution_status']);
1765 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1766 $mut->checkMailLog(array(
1767 'email:::anthony_anderson@civicrm.org',
1771 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1772 "receipt_date:::\n",
1773 'contributeMode:::notify',
1774 'title:::Contribution',
1775 'displayName:::Mr. Anthony Anderson II',
1776 'contributionStatus:::Completed',
1779 $this->revertTemplateToReservedTemplate();
1783 * Test completing a transaction via the API with a non-USD transaction.
1785 public function testCompleteTransactionEuro() {
1786 $mut = new CiviMailUtils($this, TRUE);
1787 $this->swapMessageTemplateForTestTemplate();
1788 $this->createLoggedInUser();
1789 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1790 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1792 $this->callAPISuccess('contribution', 'completetransaction', array(
1793 'id' => $contribution['id'],
1796 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1797 $this->assertEquals('SSF', $contribution['contribution_source']);
1798 $this->assertEquals('Completed', $contribution['contribution_status']);
1799 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1801 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1802 $entityFinancialTransaction = reset($entityFinancialTransactions);
1803 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1804 $this->assertEquals('EUR', $financialTrxn['currency']);
1806 $mut->checkMailLog(array(
1807 'email:::anthony_anderson@civicrm.org',
1811 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1812 "receipt_date:::\n",
1813 'contributeMode:::notify',
1814 'title:::Contribution',
1815 'displayName:::Mr. Anthony Anderson II',
1816 'contributionStatus:::Completed',
1819 $this->revertTemplateToReservedTemplate();
1823 * Test to ensure mail is sent on chosing pay later
1825 public function testpayLater() {
1826 $mut = new CiviMailUtils($this, TRUE);
1827 $this->swapMessageTemplateForTestTemplate();
1828 $this->createLoggedInUser();
1830 // create contribution page first
1831 $contributionPageParams = array(
1832 'title' => 'Help Support CiviCRM!',
1833 'financial_type_id' => 1,
1834 'is_monetary' => TRUE,
1835 'is_pay_later' => 1,
1836 'is_quick_config' => TRUE,
1837 'pay_later_text' => 'I will send payment by check',
1838 'pay_later_receipt' => 'This is a pay later reciept',
1839 'is_allow_other_amount' => 1,
1840 'min_amount' => 10.00,
1841 'max_amount' => 10000.00,
1842 'goal_amount' => 100000.00,
1843 'is_email_receipt' => 1,
1845 'amount_block_is_active' => 1,
1846 'currency' => 'USD',
1847 'is_billing_required' => 0,
1849 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1851 // submit form values
1852 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1854 'id' => $contributionPageResult['id'],
1855 'contact_id' => $this->_individualId
,
1856 'email-5' => 'anthony_anderson@civicrm.org',
1857 'payment_processor_id' => 0,
1860 'currencyID' => 'USD',
1861 'is_pay_later' => 1,
1862 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1863 'is_quick_config' => 1,
1864 'description' => 'Online Contribution: Help Support CiviCRM!',
1865 'price_set_id' => $priceSet['id'],
1867 $this->callAPISuccess('contribution_page', 'submit', $params);
1869 $mut->checkMailLog(array(
1871 'email:::anthony_anderson@civicrm.org',
1872 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1873 'displayName:::Mr. Anthony Anderson II',
1874 'contributionPageId:::' . $contributionPageResult['id'],
1875 'title:::' . $contributionPageParams['title'],
1876 'amount:::' . $params['amount'],
1879 $this->revertTemplateToReservedTemplate();
1883 * Test to check whether contact billing address is used when no contribution address
1885 public function testBillingAddress() {
1886 $mut = new CiviMailUtils($this, TRUE);
1887 $this->swapMessageTemplateForTestTemplate();
1888 $this->createLoggedInUser();
1890 //Scenario 1: When Contact don't have any address
1891 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1892 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1893 $this->callAPISuccess('contribution', 'completetransaction', array(
1894 'id' => $contribution['id'],
1896 $mut->checkMailLog(array(
1900 // Scenario 2: Contribution using address
1901 $address = $this->callAPISuccess('address', 'create', array(
1902 'street_address' => 'contribution billing st',
1903 'location_type_id' => 2,
1904 'contact_id' => $this->_params
['contact_id'],
1906 $params = array_merge($this->_params
, array(
1907 'contribution_status_id' => 2,
1908 'address_id' => $address['id'],
1911 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1912 $this->callAPISuccess('contribution', 'completetransaction', array(
1913 'id' => $contribution['id'],
1915 $mut->checkMailLog(array(
1916 'address:::contribution billing st',
1919 // Scenario 3: Contribution wtth no address but contact has a billing address
1920 $this->callAPISuccess('address', 'create', array(
1921 'id' => $address['id'],
1922 'street_address' => 'is billing st',
1923 'contact_id' => $this->_params
['contact_id'],
1925 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1926 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1927 $this->callAPISuccess('contribution', 'completetransaction', array(
1928 'id' => $contribution['id'],
1930 $mut->checkMailLog(array(
1931 'address:::is billing st',
1935 $this->revertTemplateToReservedTemplate();
1939 * Test completing a transaction via the API.
1941 * Note that we are creating a logged in user because email goes out from
1944 public function testCompleteTransactionFeeAmount() {
1945 $this->createLoggedInUser();
1946 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1947 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1948 $this->callAPISuccess('contribution', 'completetransaction', array(
1949 'id' => $contribution['id'],
1950 'fee_amount' => '.56',
1951 'trxn_id' => '7778888',
1953 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1954 $this->assertEquals('Completed', $contribution['contribution_status']);
1955 $this->assertEquals('7778888', $contribution['trxn_id']);
1956 $this->assertEquals('.56', $contribution['fee_amount']);
1957 $this->assertEquals('99.44', $contribution['net_amount']);
1961 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
1963 * @param string $thousandSeparator
1964 * punctuation used to refer to thousands.
1966 * @dataProvider getThousandSeparators
1968 public function testCheckTaxAmount($thousandSeparator) {
1969 $this->setCurrencySeparators($thousandSeparator);
1970 $contact = $this->createLoggedInUser();
1971 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1972 'name' => 'Test taxable financial Type',
1976 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1977 'name' => 'Test Tax financial account ',
1978 'contact_id' => $contact,
1979 'financial_account_type_id' => 2,
1986 $financialTypeId = $financialType['id'];
1987 $financialAccountId = $financialAccount['id'];
1988 $financialAccountParams = array(
1989 'entity_table' => 'civicrm_financial_type',
1990 'entity_id' => $financialTypeId,
1991 'account_relationship' => 10,
1992 'financial_account_id' => $financialAccountId,
1994 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
1996 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1997 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1998 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1999 $this->callAPISuccess('contribution', 'completetransaction', array(
2000 'id' => $contribution['id'],
2001 'trxn_id' => '777788888',
2002 'fee_amount' => '6.00',
2004 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
2005 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
2006 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
2007 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
2011 * Test repeat contribution successfully creates line item.
2013 public function testRepeatTransaction() {
2014 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2015 $this->callAPISuccess('contribution', 'repeattransaction', array(
2016 'original_contribution_id' => $originalContribution['id'],
2017 'contribution_status_id' => 'Completed',
2018 'trxn_id' => uniqid(),
2020 $lineItemParams = array(
2021 'entity_id' => $originalContribution['id'],
2029 'financial_type_id',
2030 'deductible_amount',
2031 'price_field_value_id',
2035 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2036 'entity_id' => $originalContribution['id'],
2038 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2039 'entity_id' => $originalContribution['id'] +
1,
2041 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2042 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2043 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2044 $this->_checkFinancialRecords(array(
2045 'id' => $originalContribution['id'] +
1,
2046 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2047 'id' => $originalContribution['payment_processor_id'],
2048 'return' => 'payment_instrument_id',
2051 $this->quickCleanUpFinancialEntities();
2055 * Test repeat contribution successfully creates line items (plural).
2057 public function testRepeatTransactionLineItems() {
2059 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2060 $this->callAPISuccess('contribution', 'repeattransaction', array(
2061 'original_contribution_id' => $originalContribution['id'],
2062 'contribution_status_id' => 'Completed',
2063 'trxn_id' => uniqid(),
2066 $lineItemParams = array(
2067 'entity_id' => $originalContribution['id'],
2075 'financial_type_id',
2076 'deductible_amount',
2077 'price_field_value_id',
2081 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2082 'entity_id' => $originalContribution['id'],
2084 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2085 'entity_id' => $originalContribution['id'] +
1,
2088 // unset id and entity_id for all of them to be able to compare the lineItems:
2089 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2090 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2091 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2093 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2094 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2095 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2097 // CRM-19309 so in future we also want to:
2098 // check that financial_line_items have been created for entity_id 3 and 4;
2100 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2101 $this->quickCleanUpFinancialEntities();
2105 * Test repeat contribution successfully creates is_test transaction.
2107 public function testRepeatTransactionIsTest() {
2108 $this->_params
['is_test'] = 1;
2109 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2111 $this->callAPISuccess('contribution', 'repeattransaction', array(
2112 'original_contribution_id' => $originalContribution['id'],
2113 'contribution_status_id' => 'Completed',
2114 'trxn_id' => uniqid(),
2116 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2120 * Test repeat contribution passed in status.
2122 public function testRepeatTransactionPassedInStatus() {
2123 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2125 $this->callAPISuccess('contribution', 'repeattransaction', array(
2126 'original_contribution_id' => $originalContribution['id'],
2127 'contribution_status_id' => 'Pending',
2128 'trxn_id' => uniqid(),
2130 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2134 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2136 public function testRepeatTransactionAcceptRecurID() {
2137 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2138 'contact_id' => $this->_individualId
,
2139 'installments' => '12',
2140 'frequency_interval' => '1',
2142 'contribution_status_id' => 1,
2143 'start_date' => '2012-01-01 00:00:00',
2144 'currency' => 'USD',
2145 'frequency_unit' => 'month',
2146 'payment_processor_id' => $this->paymentProcessorID
,
2148 $this->callAPISuccess('contribution', 'create', array_merge(
2150 array('contribution_recur_id' => $contributionRecur['id']))
2153 $this->callAPISuccess('contribution', 'repeattransaction', array(
2154 'contribution_recur_id' => $contributionRecur['id'],
2155 'contribution_status_id' => 'Completed',
2156 'trxn_id' => uniqid(),
2159 $this->quickCleanUpFinancialEntities();
2163 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2165 public function testRepeatTransactionTestRecurId() {
2166 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2167 'contact_id' => $this->_individualId
,
2168 'frequency_interval' => '1',
2170 'contribution_status_id' => 1,
2171 'start_date' => '2017-01-01 00:00:00',
2172 'currency' => 'USD',
2173 'frequency_unit' => 'month',
2174 'payment_processor_id' => $this->paymentProcessorID
,
2177 $this->callAPISuccess('contribution', 'create', array_merge(
2180 'contribution_recur_id' => $contributionRecur['id'],
2185 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2186 'contribution_recur_id' => $contributionRecur['id'],
2187 'contribution_status_id' => 'Completed',
2188 'trxn_id' => uniqid(),
2191 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2192 $this->quickCleanUpFinancialEntities();
2195 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2197 * ( Tests membership is renewed after repeattransaction. )
2199 public function testRepeatTransactionUsesCompleted() {
2200 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2202 $this->callAPISuccess('contribution', 'create', array(
2203 'contact_id' => $originalContribution['values'][1]['contact_id'],
2204 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2205 'total_amount' => $originalContribution['values'][1]['total_amount'],
2206 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2207 'contribution_status_id' => "Failed",
2210 $this->callAPISuccess('membership', 'create', array(
2211 'id' => $membership['id'],
2212 'end_date' => 'yesterday',
2216 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2217 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2218 'contribution_status_id' => 'Completed',
2219 'trxn_id' => 'bobsled',
2222 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2223 'id' => $membership['id'],
2224 'return' => 'status_id',
2227 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2229 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2230 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2231 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2232 $this->quickCleanUpFinancialEntities();
2233 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2237 * CRM-16397 test appropriate action if total amount has changed for single line items.
2239 public function testRepeatTransactionAlteredAmount() {
2240 $paymentProcessorID = $this->paymentProcessorCreate();
2241 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2242 'contact_id' => $this->_individualId
,
2243 'installments' => '12',
2244 'frequency_interval' => '1',
2246 'contribution_status_id' => 1,
2247 'start_date' => '2012-01-01 00:00:00',
2248 'currency' => 'USD',
2249 'frequency_unit' => 'month',
2250 'payment_processor_id' => $paymentProcessorID,
2252 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2255 'contribution_recur_id' => $contributionRecur['id'],
2259 $this->callAPISuccess('contribution', 'repeattransaction', array(
2260 'original_contribution_id' => $originalContribution['id'],
2261 'contribution_status_id' => 'Completed',
2262 'trxn_id' => uniqid(),
2263 'total_amount' => '400',
2267 $lineItemParams = array(
2268 'entity_id' => $originalContribution['id'],
2276 'financial_type_id',
2277 'deductible_amount',
2278 'price_field_value_id',
2282 $this->callAPISuccessGetSingle('contribution', array(
2283 'total_amount' => 400,
2285 'net_amount' => 350,
2287 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2288 'entity_id' => $originalContribution['id'],
2290 $expectedLineItem = array_merge(
2291 $lineItem1['values'][0], array(
2292 'line_total' => '400.00',
2293 'unit_price' => '400.00',
2297 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2298 'entity_id' => $originalContribution['id'] +
1,
2301 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2302 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2303 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2307 * CRM-17718 test appropriate action if financial type has changed for single line items.
2309 public function testRepeatTransactionPassedInFinancialType() {
2310 $originalContribution = $this->setUpRecurringContribution();
2312 $this->callAPISuccess('contribution', 'repeattransaction', array(
2313 'original_contribution_id' => $originalContribution['id'],
2314 'contribution_status_id' => 'Completed',
2315 'trxn_id' => uniqid(),
2316 'financial_type_id' => 2,
2318 $lineItemParams = array(
2319 'entity_id' => $originalContribution['id'],
2327 'financial_type_id',
2328 'deductible_amount',
2329 'price_field_value_id',
2334 $this->callAPISuccessGetSingle('contribution', array(
2335 'total_amount' => 100,
2336 'financial_type_id' => 2,
2338 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2339 'entity_id' => $originalContribution['id'],
2341 $expectedLineItem = array_merge(
2342 $lineItem1['values'][0], array(
2343 'line_total' => '100.00',
2344 'unit_price' => '100.00',
2345 'financial_type_id' => 2,
2346 'contribution_type_id' => 2,
2349 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2350 'entity_id' => $originalContribution['id'] +
1,
2352 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2353 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2354 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2358 * CRM-17718 test appropriate action if financial type has changed for single line items.
2360 public function testRepeatTransactionUpdatedFinancialType() {
2361 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2363 $this->callAPISuccess('contribution', 'repeattransaction', array(
2364 'contribution_recur_id' => $originalContribution['id'],
2365 'contribution_status_id' => 'Completed',
2366 'trxn_id' => uniqid(),
2368 $lineItemParams = array(
2369 'entity_id' => $originalContribution['id'],
2377 'financial_type_id',
2378 'deductible_amount',
2379 'price_field_value_id',
2384 $this->callAPISuccessGetSingle('contribution', array(
2385 'total_amount' => 100,
2386 'financial_type_id' => 2,
2388 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2389 'entity_id' => $originalContribution['id'],
2391 $expectedLineItem = array_merge(
2392 $lineItem1['values'][0], array(
2393 'line_total' => '100.00',
2394 'unit_price' => '100.00',
2395 'financial_type_id' => 2,
2396 'contribution_type_id' => 2,
2400 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2401 'entity_id' => $originalContribution['id'] +
1,
2403 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2404 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2405 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2409 * CRM-16397 test appropriate action if campaign has been passed in.
2411 public function testRepeatTransactionPassedInCampaign() {
2412 $paymentProcessorID = $this->paymentProcessorCreate();
2413 $campaignID = $this->campaignCreate();
2414 $campaignID2 = $this->campaignCreate();
2415 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2416 'contact_id' => $this->_individualId
,
2417 'installments' => '12',
2418 'frequency_interval' => '1',
2420 'contribution_status_id' => 1,
2421 'start_date' => '2012-01-01 00:00:00',
2422 'currency' => 'USD',
2423 'frequency_unit' => 'month',
2424 'payment_processor_id' => $paymentProcessorID,
2426 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2429 'contribution_recur_id' => $contributionRecur['id'],
2430 'campaign_id' => $campaignID,
2434 $this->callAPISuccess('contribution', 'repeattransaction', array(
2435 'original_contribution_id' => $originalContribution['id'],
2436 'contribution_status_id' => 'Completed',
2437 'trxn_id' => uniqid(),
2438 'campaign_id' => $campaignID2,
2441 $this->callAPISuccessGetSingle('contribution', array(
2442 'total_amount' => 100,
2443 'campaign_id' => $campaignID2,
2448 * CRM-17718 campaign stored on contribution recur gets priority.
2450 * This reflects the fact we permit people to update them.
2452 public function testRepeatTransactionUpdatedCampaign() {
2453 $paymentProcessorID = $this->paymentProcessorCreate();
2454 $campaignID = $this->campaignCreate();
2455 $campaignID2 = $this->campaignCreate();
2456 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2457 'contact_id' => $this->_individualId
,
2458 'installments' => '12',
2459 'frequency_interval' => '1',
2461 'contribution_status_id' => 1,
2462 'start_date' => '2012-01-01 00:00:00',
2463 'currency' => 'USD',
2464 'frequency_unit' => 'month',
2465 'payment_processor_id' => $paymentProcessorID,
2466 'campaign_id' => $campaignID,
2468 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2471 'contribution_recur_id' => $contributionRecur['id'],
2472 'campaign_id' => $campaignID2,
2476 $this->callAPISuccess('contribution', 'repeattransaction', array(
2477 'original_contribution_id' => $originalContribution['id'],
2478 'contribution_status_id' => 'Completed',
2479 'trxn_id' => uniqid(),
2482 $this->callAPISuccessGetSingle('contribution', array(
2483 'total_amount' => 100,
2484 'campaign_id' => $campaignID,
2489 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2491 public function testCompleteTransactionNetAmountOK() {
2492 $this->createLoggedInUser();
2493 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2494 unset($params['net_amount']);
2495 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2496 $this->callAPISuccess('contribution', 'completetransaction', array(
2497 'id' => $contribution['id'],
2499 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2500 $this->assertEquals('Completed', $contribution['contribution_status']);
2501 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2505 * CRM-14151 - Test completing a transaction via the API.
2507 public function testCompleteTransactionWithReceiptDateSet() {
2508 $this->swapMessageTemplateForTestTemplate();
2509 $mut = new CiviMailUtils($this, TRUE);
2510 $this->createLoggedInUser();
2511 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2513 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2514 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2515 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2516 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2517 $mut->checkMailLog(array(
2518 'Receipt - Contribution',
2519 'receipt_date:::' . date('Ymd'),
2522 $this->revertTemplateToReservedTemplate();
2526 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2528 public function testCompleteTransactionWithEmailReceiptInput() {
2529 $contributionPage = $this->createReceiptableContributionPage();
2531 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2532 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2533 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2534 // Complete the transaction overriding is_email_receipt to = FALSE
2535 $this->callAPISuccess('contribution', 'completetransaction', array(
2536 'id' => $contribution['id'],
2537 'trxn_date' => date('2011-04-09'),
2538 'trxn_id' => 'kazam',
2539 'is_email_receipt' => 0,
2541 // Check if a receipt was issued
2542 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2543 $this->assertEquals('', $receipt_date);
2547 * Test that $is_recur is assigned to the receipt.
2549 public function testCompleteTransactionForRecurring() {
2551 $this->swapMessageTemplateForTestTemplate();
2552 $recurring = $this->setUpRecurringContribution();
2553 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2555 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2556 $this->_params
['contribution_recur_id'] = $recurring['id'];
2558 $contribution = $this->setUpForCompleteTransaction();
2560 $this->callAPISuccess('contribution', 'completetransaction', array(
2561 'id' => $contribution['id'],
2562 'trxn_date' => date('2011-04-09'),
2563 'trxn_id' => 'kazam',
2564 'is_email_receipt' => 1,
2567 $this->mut
->checkMailLog(array(
2569 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2572 $this->revertTemplateToReservedTemplate();
2575 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2577 * If passed in it will override the default from contribution page.
2579 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2580 $mut = new CiviMailUtils($this, TRUE);
2581 $this->createLoggedInUser();
2582 // Create a Contribution Page with is_email_receipt = FALSE
2583 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2584 'receipt_from_name' => 'Mickey Mouse',
2585 'receipt_from_email' => 'mickey@mouse.com',
2586 'title' => "Test Contribution Page",
2587 'financial_type_id' => 1,
2588 'currency' => 'CAD',
2589 'is_monetary' => TRUE,
2590 'is_email_receipt' => 0,
2592 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2593 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2594 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2595 // Complete the transaction overriding is_email_receipt to = TRUE
2596 $this->callAPISuccess('contribution', 'completetransaction', array(
2597 'id' => $contribution['id'],
2598 'is_email_receipt' => 1,
2600 $mut->checkMailLog(array(
2601 'Please print this receipt for your records.',
2607 * Complete the transaction using the template with all the possible.
2609 public function testCompleteTransactionWithTestTemplate() {
2610 $this->swapMessageTemplateForTestTemplate();
2611 $contribution = $this->setUpForCompleteTransaction();
2612 $this->callAPISuccess('contribution', 'completetransaction', array(
2613 'id' => $contribution['id'],
2614 'trxn_date' => date('2011-04-09'),
2615 'trxn_id' => 'kazam',
2617 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2618 $this->mut
->checkMailLog(array(
2619 'email:::anthony_anderson@civicrm.org',
2623 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2624 'receipt_date:::' . date('Ymd'),
2625 'contributeMode:::notify',
2626 'title:::Contribution',
2627 'displayName:::Mr. Anthony Anderson II',
2629 'contactID:::' . $this->_params
['contact_id'],
2630 'contributionID:::' . $contribution['id'],
2631 'financialTypeId:::1',
2632 'financialTypeName:::Donation',
2635 $this->revertTemplateToReservedTemplate();
2639 * Complete the transaction using the template with all the possible.
2641 public function testCompleteTransactionContributionPageFromAddress() {
2642 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2643 'receipt_from_name' => 'Mickey Mouse',
2644 'receipt_from_email' => 'mickey@mouse.com',
2645 'title' => "Test Contribution Page",
2646 'financial_type_id' => 1,
2647 'currency' => 'NZD',
2648 'goal_amount' => 50,
2649 'is_pay_later' => 1,
2650 'is_monetary' => TRUE,
2651 'is_email_receipt' => TRUE,
2653 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2654 $contribution = $this->setUpForCompleteTransaction();
2655 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2656 $this->mut
->checkMailLog(array(
2664 * Test completing first transaction in a recurring series.
2666 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2668 * @dataProvider getScheduledDateData
2670 * @param array $dataSet
2672 * @throws \Exception
2674 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2675 $paymentProcessorID = $this->paymentProcessorCreate();
2676 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2677 'contact_id' => $this->_individualId
,
2678 'installments' => '2',
2679 'frequency_interval' => '1',
2681 'contribution_status_id' => 'Pending',
2682 'start_date' => '2012-01-01 00:00:00',
2683 'currency' => 'USD',
2684 'frequency_unit' => 'month',
2685 'payment_processor_id' => $paymentProcessorID,
2686 ), $dataSet['data']));
2687 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2690 'contribution_recur_id' => $contributionRecur['id'],
2691 'contribution_status_id' => 'Pending',
2692 'receive_date' => $dataSet['receive_date'],
2695 $this->callAPISuccess('Contribution', 'completetransaction', array(
2696 'id' => $contribution,
2697 'receive_date' => $dataSet['receive_date'],
2699 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2700 'id' => $contributionRecur['id'],
2701 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2703 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2704 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2705 $this->callAPISuccess('Contribution', 'create', array_merge(
2708 'contribution_recur_id' => $contributionRecur['id'],
2709 'contribution_status_id' => 'Completed',
2712 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2713 'id' => $contributionRecur['id'],
2714 'return' => array('contribution_status_id'),
2716 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2720 * Get dates for testing.
2724 public function getScheduledDateData() {
2726 $result[]['2016-08-31-1-month'] = array(
2728 'start_date' => '2016-08-31',
2729 'frequency_interval' => 1,
2730 'frequency_unit' => 'month',
2732 'receive_date' => '2016-08-31',
2733 'expected' => '2016-10-01 00:00:00',
2735 $result[]['2012-01-01-1-month'] = array(
2737 'start_date' => '2012-01-01',
2738 'frequency_interval' => 1,
2739 'frequency_unit' => 'month',
2741 'receive_date' => '2012-01-01',
2742 'expected' => '2012-02-01 00:00:00',
2744 $result[]['2012-01-01-1-month'] = array(
2746 'start_date' => '2012-01-01',
2747 'frequency_interval' => 1,
2748 'frequency_unit' => 'month',
2750 'receive_date' => '2012-02-29',
2751 'expected' => '2012-03-29 00:00:00',
2753 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2755 'start_date' => '2012-01-01',
2756 'frequency_interval' => 1,
2757 'frequency_unit' => 'month',
2758 'next_sched_contribution_date' => '2012-02-29',
2760 'receive_date' => '2012-02-29 16:00:00',
2761 'expected' => '2012-03-29 00:00:00',
2767 * Test completing a pledge with the completeTransaction api..
2769 * Note that we are creating a logged in user because email goes out from
2772 public function testCompleteTransactionUpdatePledgePayment() {
2773 $this->swapMessageTemplateForTestTemplate();
2774 $mut = new CiviMailUtils($this, TRUE);
2775 $mut->clearMessages();
2776 $this->createLoggedInUser();
2777 $contributionID = $this->createPendingPledgeContribution();
2778 $this->callAPISuccess('contribution', 'completetransaction', array(
2779 'id' => $contributionID,
2780 'trxn_date' => '1 Feb 2013',
2782 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2783 'id' => $this->_ids
['pledge'],
2785 $this->assertEquals('Completed', $pledge['pledge_status']);
2787 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2788 'pledge_id' => $this->_ids
['pledge'],
2789 'return' => 'status_id',
2791 $this->assertEquals(1, $status);
2792 $mut->checkMailLog(array(
2794 'receive_date:::20130201000000',
2795 "receipt_date:::\n",
2798 $this->revertTemplateToReservedTemplate();
2802 * Test completing a transaction with an event via the API.
2804 * Note that we are creating a logged in user because email goes out from
2807 public function testCompleteTransactionWithParticipantRecord() {
2808 $mut = new CiviMailUtils($this, TRUE);
2809 $mut->clearMessages();
2810 $this->_individualId
= $this->createLoggedInUser();
2811 $contributionID = $this->createPendingParticipantContribution();
2812 $this->callAPISuccess('contribution', 'completetransaction', array(
2813 'id' => $contributionID,
2816 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2817 'id' => $this->_ids
['participant'],
2818 'return' => 'participant_status_id',
2820 $this->assertEquals(1, $participantStatus);
2822 //Assert only three activities are created.
2823 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_individualId
);
2824 $this->assertEquals(3, count($activities));
2825 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
2826 // record two activities before and after completing payment for Event registration
2827 $this->assertEquals(2, $activityNames['Event Registration']);
2828 // update the original 'Contribution' activity created after completing payment
2829 $this->assertEquals(1, $activityNames['Contribution']);
2831 $mut->checkMailLog(array(
2832 'Annual CiviCRM meet',
2834 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2840 * Test membership is renewed when transaction completed.
2842 public function testCompleteTransactionMembershipPriceSet() {
2843 $this->createPriceSetWithPage('membership');
2844 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2848 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2849 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2850 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2851 'membership_id' => $this->_ids
['membership'],
2853 $this->assertEquals(1, $logs['count']);
2854 $this->assertEquals($stateOfGrace, $membership['status_id']);
2855 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2856 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2857 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2858 $this->callAPISuccessGetSingle('LineItem', array(
2859 'entity_id' => $this->_ids
['membership'],
2860 'entity_table' => 'civicrm_membership',
2862 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2863 'membership_id' => $this->_ids
['membership'],
2865 $this->assertEquals(2, $logs['count']);
2866 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2867 //Assert only three activities are created.
2868 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
2869 $this->assertEquals(3, count($activities));
2870 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
2871 $this->assertArrayHasKey('Contribution', $activityNames);
2872 $this->assertArrayHasKey('Membership Signup', $activityNames);
2873 $this->assertArrayHasKey('Change Membership Status', $activityNames);
2874 $this->cleanUpAfterPriceSets();
2878 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2880 public function testPendingToCompleteContribution() {
2881 $contributionPage = $this->createPriceSetWithPage('membership');
2882 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2886 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2887 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2888 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2889 $activity = $this->callAPISuccess('Activity', 'get', array(
2890 'activity_type_id' => 'Membership Signup',
2891 'source_record_id' => $this->_ids
['membership'],
2892 'status_id' => 'Scheduled',
2894 $this->assertEquals(1, $activity['count']);
2896 // change pending contribution to completed
2897 $form = new CRM_Contribute_Form_Contribution();
2899 $form->_params
= array(
2900 'id' => $this->_ids
['contribution'],
2901 'total_amount' => 20,
2904 'financial_type_id' => 1,
2905 'contact_id' => $this->_individualId
,
2906 'contribution_status_id' => 1,
2907 'billing_middle_name' => '',
2908 'billing_last_name' => 'Adams',
2909 'billing_street_address-5' => '790L Lincoln St S',
2910 'billing_city-5' => 'Maryknoll',
2911 'billing_state_province_id-5' => 1031,
2912 'billing_postal_code-5' => 10545,
2913 'billing_country_id-5' => 1228,
2914 'frequency_interval' => 1,
2915 'frequency_unit' => 'month',
2916 'installments' => '',
2917 'hidden_AdditionalDetail' => 1,
2918 'hidden_Premium' => 1,
2919 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2920 'receipt_date' => '',
2921 'receipt_date_time' => '',
2922 'payment_processor_id' => $this->paymentProcessorID
,
2923 'currency' => 'USD',
2924 'contribution_page_id' => $this->_ids
['contribution_page'],
2925 'contribution_mode' => 'membership',
2926 'source' => 'Membership Signup and Renewal',
2929 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2931 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
2934 // Case 2: After successful payment for Pending backoffice there are three activities created
2935 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
2936 $activity = $this->callAPISuccess('Activity', 'get', array(
2937 'activity_type_id' => 'Membership Signup',
2938 'source_record_id' => $this->_ids
['membership'],
2939 'status_id' => 'Completed',
2941 $this->assertEquals(1, $activity['count']);
2942 // 2.b Contribution activity created to record successful payment
2943 $activity = $this->callAPISuccess('Activity', 'get', array(
2944 'activity_type_id' => 'Contribution',
2945 'source_record_id' => $this->_ids
['contribution'],
2946 'status_id' => 'Completed',
2948 $this->assertEquals(1, $activity['count']);
2950 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
2951 $activity = $this->callAPISuccess('Activity', 'get', array(
2952 'activity_type_id' => 'Change Membership Status',
2953 'source_record_id' => $this->_ids
['membership'],
2954 'status_id' => 'Completed',
2956 $this->assertEquals(1, $activity['count']);
2957 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
2959 //Create another pending contribution for renewal
2960 $contribution = $this->callAPISuccess('contribution', 'create', array(
2962 'contact_id' => $this->_ids
['contact'],
2963 'receive_date' => date('Ymd'),
2964 'total_amount' => 20.00,
2965 'financial_type_id' => 1,
2966 'payment_instrument_id' => 'Credit Card',
2967 'non_deductible_amount' => 10.00,
2968 'trxn_id' => 'rdhfi88',
2969 'invoice_id' => 'dofhiewuyr',
2971 'contribution_status_id' => 2,
2972 'contribution_page_id' => $this->_ids
['contribution_page'],
2973 'api.membership_payment.create' => array('membership_id' => $this->_ids
['membership']),
2976 $this->callAPISuccess('line_item', 'create', array(
2977 'entity_id' => $contribution['id'],
2978 'entity_table' => 'civicrm_contribution',
2979 'contribution_id' => $contribution['id'],
2980 'price_field_id' => $this->_ids
['price_field'][0],
2984 'financial_type_id' => 1,
2985 'price_field_value_id' => $this->_ids
['price_field_value'][0],
2988 //Update it to Failed.
2989 $form->_params
['id'] = $contribution['id'];
2990 $form->_params
['contribution_status_id'] = 4;
2992 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2994 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
2997 //Existing membership should not get updated to expired.
2998 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2999 $this->assertNotEquals($membership['status_id'], 4);
3003 * Test membership is renewed when transaction completed.
3005 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3006 $this->createPriceSetWithPage('membership');
3007 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
3008 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
3009 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
3010 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3011 $this->cleanUpAfterPriceSets();
3014 public function cleanUpAfterPriceSets() {
3015 $this->quickCleanUpFinancialEntities();
3016 $this->contactDelete($this->_ids
['contact']);
3020 * Set up a pending transaction with a specific price field id.
3022 * @param int $priceFieldValueID
3024 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
3025 $contactID = $this->individualCreate();
3026 $membership = $this->callAPISuccess('membership', 'create', array(
3027 'contact_id' => $contactID,
3028 'membership_type_id' => $this->_ids
['membership_type'],
3029 'start_date' => 'yesterday - 1 year',
3030 'end_date' => 'yesterday',
3031 'join_date' => 'yesterday - 1 year',
3033 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3035 'contact_id' => $contactID,
3036 'receive_date' => date('Ymd'),
3037 'total_amount' => 20.00,
3038 'financial_type_id' => 1,
3039 'payment_instrument_id' => 'Credit Card',
3040 'non_deductible_amount' => 10.00,
3041 'trxn_id' => 'jdhfi' . rand(1, 100),
3042 'invoice_id' => 'djfhiew' . rand(5, 100),
3044 'contribution_status_id' => 2,
3045 'contribution_page_id' => $this->_ids
['contribution_page'],
3046 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3049 $this->callAPISuccess('line_item', 'create', array(
3050 'entity_id' => $contribution['id'],
3051 'entity_table' => 'civicrm_contribution',
3052 'contribution_id' => $contribution['id'],
3053 'price_field_id' => $this->_ids
['price_field'][0],
3057 'financial_type_id' => 1,
3058 'price_field_value_id' => $priceFieldValueID,
3060 $this->_ids
['contact'] = $contactID;
3061 $this->_ids
['contribution'] = $contribution['id'];
3062 $this->_ids
['membership'] = $membership['id'];
3066 * Test sending a mail via the API.
3068 public function testSendMail() {
3069 $mut = new CiviMailUtils($this, TRUE);
3070 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3071 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3072 'id' => $contribution['id'],
3073 'receipt_from_email' => 'api@civicrm.org',
3076 $mut->checkMailLog(array(
3078 'Contribution Information',
3079 'Please print this confirmation for your records',
3085 $this->checkCreditCardDetails($mut, $contribution['id']);
3090 * Test sending a mail via the API.
3091 * This simulates webform_civicrm using pay later contribution page
3093 public function testSendconfirmationPayLater() {
3094 $mut = new CiviMailUtils($this, TRUE);
3096 // Create contribution page
3097 $pageParams = array(
3098 'title' => 'Webform Contributions',
3099 'financial_type_id' => 1,
3100 'contribution_type_id' => 1,
3101 'is_confirm_enabled' => 1,
3102 'is_pay_later' => 1,
3103 'pay_later_text' => 'I will send payment by cheque',
3104 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3106 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3108 // Create pay later contribution
3109 $contribParams = array(
3110 'contact_id' => $this->_individualId
,
3111 'financial_type_id' => 1,
3112 'is_pay_later' => 1,
3113 'contribution_status_id' => 2,
3114 'contribution_page_id' => $contributionPage['id'],
3115 'total_amount' => '10.00',
3117 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3120 $lineItemParams = array(
3121 'contribution_id' => $contribution['id'],
3122 'entity_id' => $contribution['id'],
3123 'entity_table' => 'civicrm_contribution',
3124 'label' => 'My lineitem label',
3126 'unit_price' => "10.00",
3127 'line_total' => "10.00",
3129 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3133 civicrm_api3('contribution', 'sendconfirmation', array(
3134 'id' => $contribution['id'],
3135 'receipt_from_email' => 'api@civicrm.org',
3139 catch (Exception
$e) {
3140 // Need to figure out how to stop this some other day
3141 // We don't care about the Payment Processor because this is Pay Later
3142 // The point of this test is to check we get the pay_later version of the mail
3143 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3148 // Retrieve mail & check it has the pay_later_receipt info
3149 $mut->getMostRecentEmail('raw');
3150 $mut->checkMailLog(array(
3151 (string) $contribParams['total_amount'],
3152 $pageParams['pay_later_receipt'],
3162 * Check credit card details in sent mail via API
3164 * @param $mut obj CiviMailUtils instance
3165 * @param int $contributionID Contribution ID
3168 public function checkCreditCardDetails($mut, $contributionID) {
3169 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3170 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3171 'id' => $contributionID,
3172 'receipt_from_email' => 'api@civicrm.org',
3173 'payment_processor_id' => $this->paymentProcessorID
,
3176 $mut->checkMailLog(array(
3177 'Credit Card Information', // credit card header
3178 'Billing Name and Address', // billing header
3179 'anthony_anderson@civicrm.org', // billing name
3187 * Test sending a mail via the API.
3189 public function testSendMailEvent() {
3190 $mut = new CiviMailUtils($this, TRUE);
3191 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3192 $event = $this->eventCreate(array(
3193 'is_email_confirm' => 1,
3194 'confirm_from_email' => 'test@civicrm.org',
3196 $this->_eventID
= $event['id'];
3197 $participantParams = array(
3198 'contact_id' => $this->_individualId
,
3199 'event_id' => $this->_eventID
,
3202 // to ensure it matches later on
3203 'register_date' => '2007-07-21 00:00:00',
3204 'source' => 'Online Event Registration: API Testing',
3207 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3208 $this->callAPISuccess('participant_payment', 'create', array(
3209 'participant_id' => $participant['id'],
3210 'contribution_id' => $contribution['id'],
3212 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3213 'id' => $contribution['id'],
3214 'receipt_from_email' => 'api@civicrm.org',
3218 $mut->checkMailLog(array(
3219 'Annual CiviCRM meet',
3221 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3228 * This function does a GET & compares the result against the $params.
3230 * Use as a double check on Creates.
3232 * @param array $params
3234 * @param bool $delete
3236 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3238 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3244 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3246 $this->assertAPISuccess($contribution, 0);
3247 $values = $contribution['values'][$contribution['id']];
3248 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3249 // this is not returned in id format
3250 unset($params['payment_instrument_id']);
3251 $params['contribution_source'] = $params['source'];
3252 unset($params['source']);
3253 foreach ($params as $key => $value) {
3254 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3259 * Create a pending contribution & linked pending pledge record.
3261 public function createPendingPledgeContribution() {
3263 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
3264 $this->_ids
['pledge'] = $pledgeID;
3265 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
3266 'contribution_status_id' => 'Pending',
3267 'total_amount' => 500,
3270 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3271 'options' => array('limit' => 1),
3274 $this->callAPISuccess('PledgePayment', 'create', array(
3276 'contribution_id' =>
3277 $contribution['id'],
3278 'status_id' => 'Pending',
3279 'scheduled_amount' => 500,
3282 return $contribution['id'];
3286 * Create a pending contribution & linked pending participant record (along with an event).
3288 public function createPendingParticipantContribution() {
3289 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3290 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId
));
3291 $this->_ids
['participant'] = $participantID;
3292 $params = array_merge($this->_params
, array('contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3293 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3294 $this->callAPISuccess('participant_payment', 'create', array(
3295 'contribution_id' => $contribution['id'],
3296 'participant_id' => $participantID,
3298 $this->callAPISuccess('line_item', 'get', array(
3299 'entity_id' => $contribution['id'],
3300 'entity_table' => 'civicrm_contribution',
3301 'api.line_item.create' => array(
3302 'entity_id' => $participantID,
3303 'entity_table' => 'civicrm_participant',
3306 return $contribution['id'];
3310 * Get financial transaction amount.
3312 * @param int $contId
3314 * @return null|string
3316 public function _getFinancialTrxnAmount($contId) {
3318 SUM( ft.total_amount ) AS total
3319 FROM civicrm_financial_trxn AS ft
3320 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3321 WHERE ceft.entity_table = 'civicrm_contribution'
3322 AND ceft.entity_id = {$contId}";
3324 $result = CRM_Core_DAO
::singleValueQuery($query);
3329 * @param int $contId
3331 * @return null|string
3333 public function _getFinancialItemAmount($contId) {
3334 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3337 FROM civicrm_financial_item
3338 WHERE entity_table = 'civicrm_line_item'
3339 AND entity_id = {$lineItem}";
3340 $result = CRM_Core_DAO
::singleValueQuery($query);
3345 * @param int $contId
3348 public function _checkFinancialItem($contId, $context) {
3349 if ($context != 'paylater') {
3351 'entity_id' => $contId,
3352 'entity_table' => 'civicrm_contribution',
3354 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3355 $entityParams = array(
3356 'financial_trxn_id' => $trxn['financial_trxn_id'],
3357 'entity_table' => 'civicrm_financial_item',
3359 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3361 'id' => $entityTrxn['entity_id'],
3364 if ($context == 'paylater') {
3365 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3366 foreach ($lineItems as $key => $item) {
3368 'entity_id' => $key,
3369 'entity_table' => 'civicrm_line_item',
3371 $compareParams = array('status_id' => 1);
3372 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3375 elseif ($context == 'refund') {
3376 $compareParams = array(
3378 'financial_account_id' => 1,
3382 elseif ($context == 'cancelPending') {
3383 $compareParams = array(
3385 'financial_account_id' => 1,
3389 elseif ($context == 'changeFinancial') {
3390 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3392 'entity_id' => $lineKey,
3395 $compareParams = array(
3396 'financial_account_id' => 1,
3398 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3400 'financial_account_id' => 3,
3401 'entity_id' => $lineKey,
3403 $compareParams = array(
3407 if ($context != 'paylater') {
3408 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3413 * Check correct financial transaction entries were created for the change in payment instrument.
3415 * @param int $contributionID
3416 * @param int $originalInstrumentID
3417 * @param int $newInstrumentID
3419 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3421 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3423 $originalTrxnParams = array(
3424 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3425 'payment_instrument_id' => $originalInstrumentID,
3426 'amount' => $amount,
3430 $reversalTrxnParams = array(
3431 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3432 'payment_instrument_id' => $originalInstrumentID,
3433 'amount' => -$amount,
3437 $newTrxnParams = array(
3438 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3439 'payment_instrument_id' => $newInstrumentID,
3440 'amount' => $amount,
3444 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3445 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3446 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3448 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3449 'id' => $entityFinancialTrxn['financial_trxn_id'],
3451 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3452 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3453 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3454 $this->assertEquals(0, $financialTrxn['fee_amount']);
3455 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3456 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3459 $this->assertEquals(1, $financialTrxn['is_payment']);
3460 $this->assertEquals('USD', $financialTrxn['currency']);
3461 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3466 * Check financial transaction.
3468 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3470 * @param array $contribution
3471 * @param string $context
3472 * @param int $instrumentId
3473 * @param array $extraParams
3475 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3476 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3477 $trxn = array_pop($financialTrxns);
3480 'id' => $trxn['financial_trxn_id'],
3482 if ($context == 'payLater') {
3483 $compareParams = array(
3485 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3488 elseif ($context == 'refund') {
3489 $compareParams = array(
3490 'to_financial_account_id' => 6,
3491 'total_amount' => -100,
3493 'trxn_date' => '2015-01-01 09:00:00',
3494 'trxn_id' => 'the refund',
3497 elseif ($context == 'cancelPending') {
3498 $compareParams = array(
3499 'to_financial_account_id' => 7,
3500 'total_amount' => -100,
3504 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3505 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3506 // It does the same thing with greater readability.
3507 // @todo remove handling for
3509 $entityParams = array(
3510 'entity_id' => $contribution['id'],
3511 'entity_table' => 'civicrm_contribution',
3514 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3515 $trxnParams1 = array(
3516 'id' => $trxn['financial_trxn_id'],
3518 if (empty($extraParams)) {
3519 $compareParams = array(
3520 'total_amount' => -100,
3525 $compareParams = array(
3526 'total_amount' => 100,
3530 if ($context == 'paymentInstrument') {
3531 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3532 $compareParams['payment_instrument_id'] = $instrumentId;
3535 $compareParams['to_financial_account_id'] = 12;
3537 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3538 $compareParams['total_amount'] = 100;
3541 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3547 public function _addPaymentInstrument() {
3548 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3549 $optionParams = array(
3550 'option_group_id' => $gId,
3551 'label' => 'Test Card',
3552 'name' => 'Test Card',
3557 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3558 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3559 $financialParams = array(
3560 'entity_table' => 'civicrm_option_value',
3561 'entity_id' => $optionValue['id'],
3562 'account_relationship' => $relationTypeId,
3563 'financial_account_id' => 7,
3565 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3566 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3567 return $optionValue['values'][$optionValue['id']]['value'];
3570 public function _deletedAddedPaymentInstrument() {
3571 $result = $this->callAPISuccess('OptionValue', 'get', array(
3572 'option_group_id' => 'payment_instrument',
3573 'name' => 'Test Card',
3577 if ($id = CRM_Utils_Array
::value('id', $result)) {
3578 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3583 * Set up the basic recurring contribution for tests.
3585 * @param array $generalParams
3586 * Parameters that can be merged into the recurring AND the contribution.
3588 * @param array $recurParams
3589 * Parameters to merge into the recur only.
3593 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3594 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3595 'contact_id' => $this->_individualId
,
3596 'installments' => '12',
3597 'frequency_interval' => '1',
3599 'contribution_status_id' => 1,
3600 'start_date' => '2012-01-01 00:00:00',
3601 'currency' => 'USD',
3602 'frequency_unit' => 'month',
3603 'payment_processor_id' => $this->paymentProcessorID
,
3604 ), $generalParams, $recurParams));
3605 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3608 'contribution_recur_id' => $contributionRecur['id'],
3611 return $originalContribution;
3615 * Set up a basic auto-renew membership for tests.
3617 * @param array $generalParams
3618 * Parameters that can be merged into the recurring AND the contribution.
3620 * @param array $recurParams
3621 * Parameters to merge into the recur only.
3625 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3626 $newContact = $this->callAPISuccess('Contact', 'create', array(
3627 'contact_type' => 'Individual',
3628 'sort_name' => 'McTesterson, Testy',
3629 'display_name' => 'Testy McTesterson',
3630 'preferred_language' => 'en_US',
3631 'preferred_mail_format' => 'Both',
3632 'first_name' => 'Testy',
3633 'last_name' => 'McTesterson',
3634 'contact_is_deleted' => '0',
3636 'email' => 'tmctesterson@example.com',
3639 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3640 'domain_id' => "Default Domain Name",
3641 'member_of_contact_id' => 1,
3642 'financial_type_id' => "Member Dues",
3643 'duration_unit' => "month",
3644 'duration_interval' => 1,
3645 'period_type' => 'rolling',
3646 'name' => "Standard Member",
3647 'minimum_fee' => 100,
3649 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3650 'contact_id' => $newContact['id'],
3651 'installments' => '12',
3652 'frequency_interval' => '1',
3654 'contribution_status_id' => 1,
3655 'start_date' => '2012-01-01 00:00:00',
3656 'currency' => 'USD',
3657 'frequency_unit' => 'month',
3658 'payment_processor_id' => $this->paymentProcessorID
,
3659 ), $generalParams, $recurParams));
3661 $membership = $this->callAPISuccess('membership', 'create', array(
3662 'contact_id' => $newContact['id'],
3663 'contribution_recur_id' => $contributionRecur['id'],
3664 'financial_type_id' => "Member Dues",
3665 'membership_type_id' => $membershipType['id'],
3667 'skipLineItem' => TRUE,
3670 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3671 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3674 'contact_id' => $newContact['id'],
3675 'contribution_recur_id' => $contributionRecur['id'],
3676 'financial_type_id' => "Member Dues",
3677 'contribution_status_id' => 1,
3678 'invoice_id' => uniqid(),
3681 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3682 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3683 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3684 $this->callAPISuccess('LineItem', 'getsingle', array());
3685 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3687 return array($originalContribution, $membership);
3690 * Set up a repeat transaction.
3692 * @param array $recurParams
3696 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3697 $paymentProcessorID = $this->paymentProcessorCreate();
3698 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3699 'contact_id' => $this->_individualId
,
3700 'installments' => '12',
3701 'frequency_interval' => '1',
3703 'contribution_status_id' => 1,
3704 'start_date' => '2012-01-01 00:00:00',
3705 'currency' => 'USD',
3706 'frequency_unit' => 'month',
3707 'payment_processor_id' => $paymentProcessorID,
3710 $originalContribution = '';
3711 if ($flag == 'multiple') {
3712 // CRM-19309 create a contribution + also add in line_items (plural):
3713 $params = array_merge($this->_params
, $contributionParams);
3714 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3717 'contribution_recur_id' => $contributionRecur['id'],
3718 'skipLineItem' => 1,
3719 'api.line_item.create' => array(
3721 'price_field_id' => 1,
3723 'line_total' => '20',
3724 'unit_price' => '10',
3725 'financial_type_id' => 1,
3728 'price_field_id' => 1,
3730 'line_total' => '80',
3731 'unit_price' => '80',
3732 'financial_type_id' => 2,
3739 elseif ($flag == 'single') {
3740 $params = array_merge($this->_params
, array('contribution_recur_id' => $contributionRecur['id']));
3741 $params = array_merge($params, $contributionParams);
3742 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3744 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3745 return $originalContribution;
3749 * Common set up routine.
3753 protected function setUpForCompleteTransaction() {
3754 $this->mut
= new CiviMailUtils($this, TRUE);
3755 $this->createLoggedInUser();
3756 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3757 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3758 return $contribution;
3762 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3764 public function testRepeatTransactionWithNonCreditCardDefault() {
3765 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3766 'contact_id' => $this->_individualId
,
3767 'installments' => '12',
3768 'frequency_interval' => '1',
3770 'contribution_status_id' => 1,
3771 'start_date' => '2012-01-01 00:00:00',
3772 'currency' => 'USD',
3773 'frequency_unit' => 'month',
3774 'payment_processor_id' => $this->paymentProcessorID
,
3776 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3778 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3780 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
3781 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3782 'contribution_status_id' => 'Completed',
3783 'trxn_id' => uniqid(),
3784 'original_contribution_id' => $contribution1,
3786 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3787 $this->quickCleanUpFinancialEntities();
3791 * CRM-20008 Tests repeattransaction creates pending membership.
3793 public function testRepeatTransactionPendingMembership() {
3794 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3795 $this->callAPISuccess('membership', 'create', array(
3796 'id' => $membership['id'],
3797 'end_date' => 'yesterday',
3798 'status_id' => 'Expired',
3800 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3801 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3802 'contribution_status_id' => 'Pending',
3803 'trxn_id' => uniqid(),
3805 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3806 'id' => $membership['id'],
3807 'return' => 'status_id',
3810 // Let's see if the membership payments got created while we're at it.
3811 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3812 'memberhip_id' => $membership['id'],
3814 $this->assertEquals(2, $membershipPayments['count']);
3816 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3817 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3818 $membership = $this->callAPISuccessGetSingle('membership', array(
3819 'id' => $membership['id'],
3820 'return' => 'status_id, end_date',
3822 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3823 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3825 $this->quickCleanUpFinancialEntities();
3826 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3830 * Test sending a mail via the API.
3832 public function testSendMailWithAPISetFromDetails() {
3833 $mut = new CiviMailUtils($this, TRUE);
3834 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3835 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3836 'id' => $contribution['id'],
3837 'receipt_from_email' => 'api@civicrm.org',
3838 'receipt_from_name' => 'CiviCRM LLC',
3840 $mut->checkMailLog(array(
3841 'From: CiviCRM LLC <api@civicrm.org>',
3842 'Contribution Information',
3843 'Please print this confirmation for your records',
3852 * Test sending a mail via the API.
3854 public function testSendMailWithNoFromSetFallToDomain() {
3855 $this->createLoggedInUser();
3856 $mut = new CiviMailUtils($this, TRUE);
3857 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3858 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3859 'id' => $contribution['id'],
3861 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3862 $mut->checkMailLog(array(
3863 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3864 'Contribution Information',
3865 'Please print this confirmation for your records',
3874 * Test sending a mail via the API.
3876 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3877 $this->createLoggedInUser();
3878 $mut = new CiviMailUtils($this, TRUE);
3879 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3880 $this->callAPISuccess('contribution', 'repeattransaction', array(
3881 'contribution_status_id' => 'Completed',
3882 'trxn_id' => uniqid(),
3883 'original_contribution_id' => $contribution,
3885 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3886 $mut->checkMailLog(array(
3887 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3888 'Contribution Information',
3889 'Please print this confirmation for your records',
3898 * Test sending a mail via the API.
3900 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3901 $mut = new CiviMailUtils($this, TRUE);
3902 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3903 $paymentProcessorID = $this->paymentProcessorCreate();
3904 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3905 'contact_id' => $this->_individualId
,
3906 'installments' => '12',
3907 'frequency_interval' => '1',
3909 'contribution_status_id' => 1,
3910 'start_date' => '2012-01-01 00:00:00',
3911 'currency' => 'USD',
3912 'frequency_unit' => 'month',
3913 'payment_processor_id' => $paymentProcessorID,
3915 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3918 'contribution_recur_id' => $contributionRecur['id'],
3919 'contribution_page_id' => $contributionPage['id']))
3921 $this->callAPISuccess('contribution', 'repeattransaction', array(
3922 'contribution_status_id' => 'Completed',
3923 'trxn_id' => uniqid(),
3924 'original_contribution_id' => $originalContribution,
3927 $mut->checkMailLog(array(
3928 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3929 'Contribution Information',
3930 'Please print this confirmation for your records',
3939 * Test sending a mail via the API.
3941 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
3942 $mut = new CiviMailUtils($this, TRUE);
3943 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
3944 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
3945 foreach ($fromEmail['values'] as $from) {
3946 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
3948 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
3949 $this->callAPISuccess('contribution', 'repeattransaction', array(
3950 'contribution_status_id' => 'Completed',
3951 'trxn_id' => uniqid(),
3952 'original_contribution_id' => $originalContribution,
3955 $mut->checkMailLog(array(
3956 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
3957 'Contribution Information',
3958 'Please print this confirmation for your records',
3967 * Create a Contribution Page with is_email_receipt = TRUE.
3969 * @param array $params
3970 * Params to overwrite with.
3974 protected function createReceiptableContributionPage($params = array()) {
3975 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
3976 'receipt_from_name' => 'Mickey Mouse',
3977 'receipt_from_email' => 'mickey@mouse.com',
3978 'title' => "Test Contribution Page",
3979 'financial_type_id' => 1,
3980 'currency' => 'CAD',
3981 'is_monetary' => TRUE,
3982 'is_email_receipt' => TRUE,
3984 return $contributionPage;
3988 * function to test card_type and pan truncation.
3990 public function testCardTypeAndPanTruncation() {
3991 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
3992 $contactId = $this->individualCreate();
3994 'contact_id' => $contactId,
3995 'receive_date' => '2016-01-20',
3996 'total_amount' => 100,
3997 'financial_type_id' => 1,
3998 'payment_instrument' => 'Credit Card',
3999 'card_type_id' => $creditCardTypeIDs['Visa'],
4000 'pan_truncation' => 4567,
4002 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4003 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
4004 $financialTrxn = $this->callAPISuccessGetSingle(
4007 'id' => $lastFinancialTrxnId['financialTrxnId'],
4008 'return' => array('card_type_id', 'pan_truncation'),
4011 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4012 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
4014 'id' => $contribution['id'],
4015 'pan_truncation' => 2345,
4016 'card_type_id' => $creditCardTypeIDs['Amex'],
4018 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4019 $financialTrxn = $this->callAPISuccessGetSingle(
4022 'id' => $lastFinancialTrxnId['financialTrxnId'],
4023 'return' => array('card_type_id', 'pan_truncation'),
4026 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4027 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
4031 * Test repeat contribution uses non default currency
4032 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4034 public function testRepeatTransactionWithDifferenceCurrency() {
4035 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4036 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4037 'original_contribution_id' => $originalContribution['id'],
4038 'contribution_status_id' => 'Completed',
4039 'trxn_id' => uniqid(),
4041 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4045 * Get the financial items for the contribution.
4047 * @param int $contributionID
4050 * Array of associated financial items.
4052 protected function getFinancialTransactionsForContribution($contributionID) {
4053 $trxnParams = array(
4054 'entity_id' => $contributionID,
4055 'entity_table' => 'civicrm_contribution',
4057 // @todo the following function has naming errors & has a weird signature & appears to
4058 // only be called from test classes. Move into test suite & maybe just use api
4059 // from this function.
4060 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));