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'));
130 public function testGetContribution() {
131 $contributionSettings = $this->enableTaxAndInvoicing();
132 $invoice_prefix = CRM_Contribute_BAO_Contribution
::checkContributeSettings('invoice_prefix', TRUE);
134 'contact_id' => $this->_individualId
,
135 'receive_date' => '2010-01-20',
136 'total_amount' => 100.00,
137 'financial_type_id' => $this->_financialTypeId
,
138 'non_deductible_amount' => 10.00,
139 'fee_amount' => 5.00,
140 'net_amount' => 95.00,
142 'invoice_id' => 78910,
144 'contribution_status_id' => 1,
146 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
149 'contribution_id' => $this->_contribution
['id'],
152 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
153 $financialParams['id'] = $this->_financialTypeId
;
155 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
157 $this->assertEquals(1, $contributions['count']);
158 $contribution = $contributions['values'][$contributions['id']];
159 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
160 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
161 // Passing back a string rather than an id seems like an error/cruft.
162 // If it is to be introduced we should discuss.
163 $this->assertEquals($contribution['financial_type_id'], 1);
164 $this->assertEquals($contribution['total_amount'], 100.00);
165 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
166 $this->assertEquals($contribution['fee_amount'], 5.00);
167 $this->assertEquals($contribution['net_amount'], 95.00);
168 $this->assertEquals($contribution['trxn_id'], 23456);
169 $this->assertEquals($contribution['invoice_id'], 78910);
170 $this->assertEquals($contribution['invoice_number'], $invoice_prefix . $contributions['id']);
171 $this->assertEquals($contribution['contribution_source'], 'SSF');
172 $this->assertEquals($contribution['contribution_status'], 'Completed');
173 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
174 $p['trxn_id'] = '3847';
175 $p['invoice_id'] = '3847';
177 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
179 // Now we have 2 - test getcount.
180 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
181 $this->assertEquals(2, $contribution);
182 // Test id only format.
183 $contribution = $this->callAPISuccess('contribution', 'get', array(
184 'id' => $this->_contribution
['id'],
185 'format.only_id' => 1,
187 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
188 // Test id only format.
189 $contribution = $this->callAPISuccess('contribution', 'get', array(
190 'id' => $contribution2['id'],
191 'format.only_id' => 1,
193 $this->assertEquals($contribution2['id'], $contribution);
195 $contribution = $this->callAPISuccess('contribution', 'get', array(
196 'id' => $this->_contribution
['id'],
198 $this->assertEquals(1, $contribution['count']);
200 // Test get by contact id works.
201 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
203 $this->assertEquals(2, $contribution['count']);
204 $this->callAPISuccess('Contribution', 'Delete', array(
205 'id' => $this->_contribution
['id'],
207 $this->callAPISuccess('Contribution', 'Delete', array(
208 'id' => $contribution2['id'],
213 * Test that test contributions can be retrieved.
215 public function testGetTestContribution() {
216 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('is_test' => 1)));
217 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
221 * Test the 'return' param works for all fields.
223 public function testGetContributionReturnFunctionality() {
224 $params = $this->_params
;
225 $params['check_number'] = 'bouncer';
226 $params['payment_instrument_id'] = 'Check';
227 $params['cancel_date'] = 'yesterday';
228 $params['receipt_date'] = 'yesterday';
229 $params['thankyou_date'] = 'yesterday';
230 $params['revenue_recognition_date'] = 'yesterday';
231 $params['amount_level'] = 'Unreasonable';
232 $params['cancel_reason'] = 'You lose sucker';
233 $params['creditnote_id'] = 'sudo rm -rf';
234 $params['tax_amount'] = '1';
235 $address = $this->callAPISuccess('Address', 'create', array(
236 'street_address' => 'Knockturn Alley',
237 'contact_id' => $this->_individualId
,
238 'location_type_id' => 'Home',
240 $params['address_id'] = $address['id'];
241 $contributionPage = $this->contributionPageCreate();
242 $params['contribution_page_id'] = $contributionPage['id'];
243 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array(
244 'contact_id' => $this->_individualId
,
245 'frequency_interval' => 1,
248 $params['contribution_recur_id'] = $contributionRecur['id'];
250 $params['campaign_id'] = $this->campaignCreate();
252 $contributionID = $this->contributionCreate($params);
254 // update contribution with invoice number
255 $params = array_merge($params, array(
256 'id' => $contributionID,
257 'invoice_number' => CRM_Utils_Array
::value('invoice_prefix', Civi
::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
259 $contributionID = $this->contributionCreate($params);
261 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
262 $this->assertEquals('bouncer', $contribution['check_number']);
263 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
265 $fields = CRM_Contribute_BAO_Contribution
::fields();
266 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
267 // need to specify them.
268 $fields['address_id'] = $fields['contribution_address_id'];
269 $fields['check_number'] = $fields['contribution_check_number'];
271 $fieldsLockedIn = array(
272 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
273 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
274 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
275 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
276 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
277 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
279 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
280 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
281 // covered as the test contract now guarantees them in the return array.
282 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
283 foreach ($fields as $fieldName => $fieldSpec) {
284 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
285 $returnField = $fieldName;
286 if ($returnField == 'contribution_contact_id') {
287 $returnField = 'contact_id';
289 $this->assertTrue((!empty($contribution[$returnField]) ||
$contribution[$returnField] === "0"), $returnField);
294 * We need to ensure previous tested behaviour still works as part of the api contract.
296 public function testGetContributionLegacyBehaviour() {
298 'contact_id' => $this->_individualId
,
299 'receive_date' => '2010-01-20',
300 'total_amount' => 100.00,
301 'contribution_type_id' => $this->_financialTypeId
,
302 'non_deductible_amount' => 10.00,
303 'fee_amount' => 5.00,
304 'net_amount' => 95.00,
306 'invoice_id' => 78910,
308 'contribution_status_id' => 1,
310 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
313 'contribution_id' => $this->_contribution
['id'],
315 $contribution = $this->callAPISuccess('contribution', 'get', $params);
316 $financialParams['id'] = $this->_financialTypeId
;
318 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
320 $this->assertEquals(1, $contribution['count']);
321 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
322 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
323 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
324 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
325 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
326 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
327 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
328 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
329 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
330 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
331 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
333 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
334 $p['trxn_id'] = '3847';
335 $p['invoice_id'] = '3847';
337 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
339 // now we have 2 - test getcount
340 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
341 $this->assertEquals(2, $contribution);
342 //test id only format
343 $contribution = $this->callAPISuccess('contribution', 'get', array(
344 'id' => $this->_contribution
['id'],
345 'format.only_id' => 1,
347 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
348 //test id only format
349 $contribution = $this->callAPISuccess('contribution', 'get', array(
350 'id' => $contribution2['id'],
351 'format.only_id' => 1,
353 $this->assertEquals($contribution2['id'], $contribution);
354 $contribution = $this->callAPISuccess('contribution', 'get', array(
355 'id' => $this->_contribution
['id'],
358 $this->assertEquals(1, $contribution['count']);
359 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
360 //test get by contact id works
361 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
363 $this->assertEquals(2, $contribution['count']);
364 $this->callAPISuccess('Contribution', 'Delete', array(
365 'id' => $this->_contribution
['id'],
367 $this->callAPISuccess('Contribution', 'Delete', array(
368 'id' => $contribution2['id'],
373 * Create an contribution_id=FALSE and financial_type_id=Donation.
375 public function testCreateEmptyContributionIDUseDonation() {
377 'contribution_id' => FALSE,
380 'check_permissions' => FALSE,
381 'financial_type_id' => 'Donation',
383 $this->callAPISuccess('contribution', 'create', $params);
387 * Check with complete array + custom field.
389 * Note that the test is written on purpose without any
390 * variables specific to participant so it can be replicated into other entities
391 * and / or moved to the automated test suite
393 public function testCreateWithCustom() {
394 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
396 $params = $this->_params
;
397 $params['custom_' . $ids['custom_field_id']] = "custom string";
399 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
400 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
401 $check = $this->callAPISuccess($this->_entity
, 'get', array(
402 'return.custom_' . $ids['custom_field_id'] => 1,
403 'id' => $result['id'],
405 $this->customFieldDelete($ids['custom_field_id']);
406 $this->customGroupDelete($ids['custom_group_id']);
407 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
411 * Check with complete array + custom field.
413 * Note that the test is written on purpose without any
414 * variables specific to participant so it can be replicated into other entities
415 * and / or moved to the automated test suite
417 public function testCreateGetFieldsWithCustom() {
418 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
419 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
420 $result = $this->callAPISuccess('Contribution', 'getfields', array());
421 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
422 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
423 $this->customFieldDelete($ids['custom_field_id']);
424 $this->customGroupDelete($ids['custom_group_id']);
425 $this->customFieldDelete($idsContact['custom_field_id']);
426 $this->customGroupDelete($idsContact['custom_group_id']);
429 public function testCreateContributionNoLineItems() {
432 'contact_id' => $this->_individualId
,
433 'receive_date' => '20120511',
434 'total_amount' => 100.00,
435 'financial_type_id' => $this->_financialTypeId
,
436 'payment_instrument_id' => 1,
437 'non_deductible_amount' => 10.00,
438 'fee_amount' => 50.00,
439 'net_amount' => 90.00,
441 'invoice_id' => 67890,
443 'contribution_status_id' => 1,
447 $contribution = $this->callAPISuccess('contribution', 'create', $params);
448 $lineItems = $this->callAPISuccess('line_item', 'get', array(
449 'entity_id' => $contribution['id'],
450 'entity_table' => 'civicrm_contribution',
453 $this->assertEquals(0, $lineItems['count']);
457 * Test checks that passing in line items suppresses the create mechanism.
459 public function testCreateContributionChainedLineItems() {
461 'contact_id' => $this->_individualId
,
462 'receive_date' => '20120511',
463 'total_amount' => 100.00,
464 'financial_type_id' => $this->_financialTypeId
,
465 'payment_instrument_id' => 1,
466 'non_deductible_amount' => 10.00,
467 'fee_amount' => 50.00,
468 'net_amount' => 90.00,
470 'invoice_id' => 67890,
472 'contribution_status_id' => 1,
474 'api.line_item.create' => array(
476 'price_field_id' => 1,
478 'line_total' => '20',
479 'unit_price' => '10',
482 'price_field_id' => 1,
484 'line_total' => '80',
485 'unit_price' => '80',
490 $description = "Create Contribution with Nested Line Items.";
491 $subfile = "CreateWithNestedLineItems";
492 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
494 $lineItems = $this->callAPISuccess('line_item', 'get', array(
495 'entity_id' => $contribution['id'],
496 'contribution_id' => $contribution['id'],
497 'entity_table' => 'civicrm_contribution',
500 $this->assertEquals(2, $lineItems['count']);
503 public function testCreateContributionOffline() {
505 'contact_id' => $this->_individualId
,
506 'receive_date' => '20120511',
507 'total_amount' => 100.00,
508 'financial_type_id' => 1,
510 'invoice_id' => 67890,
512 'contribution_status_id' => 1,
515 $contribution = $this->callAPISuccess('contribution', 'create', $params);
516 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
517 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
518 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
519 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
520 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
521 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
522 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
523 $lineItems = $this->callAPISuccess('line_item', 'get', array(
524 'entity_id' => $contribution['id'],
525 'contribution_id' => $contribution['id'],
526 'entity_table' => 'civicrm_contribution',
529 $this->assertEquals(1, $lineItems['count']);
530 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
531 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
532 $this->_checkFinancialRecords($contribution, 'offline');
533 $this->contributionGetnCheck($params, $contribution['id']);
537 * Test create with valid payment instrument.
539 public function testCreateContributionWithPaymentInstrument() {
540 $params = $this->_params +
array('payment_instrument' => 'EFT');
541 $contribution = $this->callAPISuccess('contribution', 'create', $params);
542 $contribution = $this->callAPISuccess('contribution', 'get', array(
544 'id' => $contribution['id'],
546 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
547 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
549 $this->callAPISuccess('contribution', 'create', array(
550 'id' => $contribution['id'],
551 'payment_instrument' => 'Credit Card',
553 $contribution = $this->callAPISuccess('contribution', 'get', array(
555 'id' => $contribution['id'],
557 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
558 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
561 public function testGetContributionByPaymentInstrument() {
562 $params = $this->_params +
array('payment_instrument' => 'EFT');
563 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
564 $this->callAPISuccess('contribution', 'create', $params);
565 $this->callAPISuccess('contribution', 'create', $params2);
566 $contribution = $this->callAPISuccess('contribution', 'get', array(
568 'contribution_payment_instrument' => 'Cash',
570 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
571 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
572 $this->assertEquals(1, $contribution['count']);
573 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
574 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
575 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
576 $this->assertEquals(1, $contribution['count']);
577 $contribution = $this->callAPISuccess('contribution', 'get', array(
579 'payment_instrument_id' => 5,
581 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
582 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
583 $this->assertEquals(1, $contribution['count']);
584 $contribution = $this->callAPISuccess('contribution', 'get', array(
586 'payment_instrument' => 'EFT',
588 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
589 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
590 $this->assertEquals(1, $contribution['count']);
591 $contribution = $this->callAPISuccess('contribution', 'create', array(
592 'id' => $contribution['id'],
593 'payment_instrument' => 'Credit Card',
595 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
596 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
597 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
598 $this->assertEquals(1, $contribution['count']);
602 * CRM-16227 introduces invoice_id as a parameter.
604 public function testGetContributionByInvoice() {
605 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('invoice_id' => 'curly')));
606 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), array('invoice_id' => 'churlish'));
607 $this->callAPISuccessGetCount('Contribution', array(), 2);
608 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
609 // The following don't work. They are the format we are trying to introduce but although the form uses this format
610 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
611 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
612 // come out of convertFormValues
613 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
614 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
615 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
616 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
621 * Check the credit note retrieval is case insensitive.
623 public function testGetCreditNoteCaseInsensitive() {
624 $this->contributionCreate(array('contact_id' => $this->_individualId
));
625 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId
, 'invoice_id' => rand(), 'trxn_id' => rand()));
626 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
627 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
631 * Test retrieval by total_amount works.
635 public function testGetContributionByTotalAmount() {
636 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '5')));
637 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '10')));
638 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
639 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
640 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
641 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
642 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
643 $this->callAPISuccessGetCount('Contribution', array(), 2);
647 * Create test with unique field name on source.
649 public function testCreateContributionSource() {
652 'contact_id' => $this->_individualId
,
653 'receive_date' => date('Ymd'),
654 'total_amount' => 100.00,
655 'financial_type_id' => $this->_financialTypeId
,
656 'payment_instrument_id' => 1,
657 'non_deductible_amount' => 10.00,
658 'fee_amount' => 50.00,
659 'net_amount' => 90.00,
661 'invoice_id' => 67890,
662 'contribution_source' => 'SSF',
663 'contribution_status_id' => 1,
666 $contribution = $this->callAPISuccess('contribution', 'create', $params);
667 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
668 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
672 * Create test with unique field name on source.
674 public function testCreateDefaultNow() {
676 $params = $this->_params
;
677 unset($params['receive_date']);
679 $contribution = $this->callAPISuccess('contribution', 'create', $params);
680 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
681 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
685 * Create test with unique field name on source.
687 public function testCreateContributionSourceInvalidContact() {
691 'receive_date' => date('Ymd'),
692 'total_amount' => 100.00,
693 'financial_type_id' => $this->_financialTypeId
,
694 'payment_instrument_id' => 1,
695 'non_deductible_amount' => 10.00,
696 'fee_amount' => 50.00,
697 'net_amount' => 90.00,
699 'invoice_id' => 67890,
700 'contribution_source' => 'SSF',
701 'contribution_status_id' => 1,
704 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
707 public function testCreateContributionSourceInvalidContContact() {
710 'contribution_contact_id' => 999,
711 'receive_date' => date('Ymd'),
712 'total_amount' => 100.00,
713 'financial_type_id' => $this->_financialTypeId
,
714 'payment_instrument_id' => 1,
715 'non_deductible_amount' => 10.00,
716 'fee_amount' => 50.00,
717 'net_amount' => 90.00,
719 'invoice_id' => 67890,
720 'contribution_source' => 'SSF',
721 'contribution_status_id' => 1,
724 $this->callAPIFailure('contribution', 'create', $params);
728 * Test note created correctly.
730 public function testCreateContributionWithNote() {
731 $description = "Demonstrates creating contribution with Note Entity.";
732 $subfile = "ContributionCreateWithNote";
734 'contact_id' => $this->_individualId
,
735 'receive_date' => '2012-01-01',
736 'total_amount' => 100.00,
737 'financial_type_id' => $this->_financialTypeId
,
738 'payment_instrument_id' => 1,
739 'non_deductible_amount' => 10.00,
740 'fee_amount' => 50.00,
741 'net_amount' => 90.00,
743 'invoice_id' => 67890,
745 'contribution_status_id' => 1,
746 'note' => 'my contribution note',
749 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
750 $result = $this->callAPISuccess('note', 'get', array(
751 'entity_table' => 'civicrm_contribution',
752 'entity_id' => $contribution['id'],
755 $this->assertEquals('my contribution note', $result['values'][0]['note']);
756 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
759 public function testCreateContributionWithNoteUniqueNameAliases() {
761 'contact_id' => $this->_individualId
,
762 'receive_date' => '2012-01-01',
763 'total_amount' => 100.00,
764 'financial_type_id' => $this->_financialTypeId
,
765 'payment_instrument_id' => 1,
766 'non_deductible_amount' => 10.00,
767 'fee_amount' => 50.00,
768 'net_amount' => 90.00,
770 'invoice_id' => 67890,
772 'contribution_status_id' => 1,
773 'contribution_note' => 'my contribution note',
776 $contribution = $this->callAPISuccess('contribution', 'create', $params);
777 $result = $this->callAPISuccess('note', 'get', array(
778 'entity_table' => 'civicrm_contribution',
779 'entity_id' => $contribution['id'],
782 $this->assertEquals('my contribution note', $result['values'][0]['note']);
783 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
787 * This is the test for creating soft credits.
789 public function testCreateContributionWithSoftCredit() {
790 $description = "Demonstrates creating contribution with SoftCredit.";
791 $subfile = "ContributionCreateWithSoftCredit";
792 $contact2 = $this->callAPISuccess('Contact', 'create', array(
793 'display_name' => 'superman',
794 'contact_type' => 'Individual',
797 'contact_id' => $contact2['id'],
799 'soft_credit_type_id' => 3,
802 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
803 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
804 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
806 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
807 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
808 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
810 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
811 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
814 public function testCreateContributionWithSoftCreditDefaults() {
815 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
816 $subfile = "ContributionCreateWithSoftCreditDefaults";
817 $contact2 = $this->callAPISuccess('Contact', 'create', array(
818 'display_name' => 'superman',
819 'contact_type' => 'Individual',
821 $params = $this->_params +
array(
822 'soft_credit_to' => $contact2['id'],
824 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
825 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
827 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
828 // Default soft credit amount = contribution.total_amount
829 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
830 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
832 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
833 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
837 * Test creating contribution with Soft Credit by passing in honor_contact_id.
839 public function testCreateContributionWithHonoreeContact() {
840 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
841 $subfile = "ContributionCreateWithHonoreeContact";
842 $contact2 = $this->callAPISuccess('Contact', 'create', array(
843 'display_name' => 'superman',
844 'contact_type' => 'Individual',
846 $params = $this->_params +
array(
847 'honor_contact_id' => $contact2['id'],
849 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
850 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
852 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
853 // Default soft credit amount = contribution.total_amount
854 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
855 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
856 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
857 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
859 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
860 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
864 * Test using example code.
866 public function testContributionCreateExample() {
867 //make sure at least on page exists since there is a truncate in tear down
868 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
869 require_once 'api/v3/examples/Contribution/Create.php';
870 $result = contribution_create_example();
872 $expectedResult = contribution_create_expectedresult();
873 $this->checkArrayEquals($expectedResult, $result);
874 $this->contributionDelete($id);
878 * Function tests that additional financial records are created when fee amount is recorded.
880 public function testCreateContributionWithFee() {
882 'contact_id' => $this->_individualId
,
883 'receive_date' => '20120511',
884 'total_amount' => 100.00,
886 'financial_type_id' => 1,
888 'invoice_id' => 67890,
890 'contribution_status_id' => 1,
893 $contribution = $this->callAPISuccess('contribution', 'create', $params);
894 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
895 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
896 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
897 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
898 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
899 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
900 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
901 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
902 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
904 $lineItems = $this->callAPISuccess('line_item', 'get', array(
906 'entity_id' => $contribution['id'],
907 'entity_table' => 'civicrm_contribution',
910 $this->assertEquals(1, $lineItems['count']);
911 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
912 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
913 $lineItems = $this->callAPISuccess('line_item', 'get', array(
915 'entity_id' => $contribution['id'],
916 'contribution_id' => $contribution['id'],
917 'entity_table' => 'civicrm_contribution',
920 $this->assertEquals(1, $lineItems['count']);
921 $this->_checkFinancialRecords($contribution, 'feeAmount');
926 * Function tests that additional financial records are created when online contribution is created.
928 public function testCreateContributionOnline() {
929 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
930 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
931 $this->assertAPISuccess($contributionPage);
933 'contact_id' => $this->_individualId
,
934 'receive_date' => '20120511',
935 'total_amount' => 100.00,
936 'financial_type_id' => 1,
937 'contribution_page_id' => $contributionPage['id'],
938 'payment_processor' => $this->paymentProcessorID
,
940 'invoice_id' => 67890,
942 'contribution_status_id' => 1,
946 $contribution = $this->callAPISuccess('contribution', 'create', $params);
947 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
948 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
949 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
950 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
951 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
952 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
953 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
954 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
955 'id' => $this->paymentProcessorID
,
956 'return' => 'payment_instrument_id',
958 $this->_checkFinancialRecords($contribution, 'online');
962 * Check handling of financial type.
964 * In the interests of removing financial type / contribution type checks from
965 * legacy format function lets test that the api is doing this for us
967 public function testCreateInvalidFinancialType() {
968 $params = $this->_params
;
969 $params['financial_type_id'] = 99999;
970 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
974 * Check handling of financial type.
976 * In the interests of removing financial type / contribution type checks from
977 * legacy format function lets test that the api is doing this for us
979 public function testValidNamedFinancialType() {
980 $params = $this->_params
;
981 $params['financial_type_id'] = 'Donation';
982 $this->callAPISuccess($this->_entity
, 'create', $params);
986 * Tests that additional financial records are created.
988 * Checks when online contribution with pay later option is created
990 public function testCreateContributionPayLaterOnline() {
991 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
992 $this->_pageParams
['is_pay_later'] = 1;
993 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
994 $this->assertAPISuccess($contributionPage);
996 'contact_id' => $this->_individualId
,
997 'receive_date' => '20120511',
998 'total_amount' => 100.00,
999 'financial_type_id' => 1,
1000 'contribution_page_id' => $contributionPage['id'],
1002 'is_pay_later' => 1,
1003 'invoice_id' => 67890,
1005 'contribution_status_id' => 2,
1009 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1010 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1011 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1012 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1013 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1014 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1015 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1016 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1017 $this->_checkFinancialRecords($contribution, 'payLater');
1021 * Function tests that additional financial records are created for online contribution with pending option.
1023 public function testCreateContributionPendingOnline() {
1024 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1025 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1026 $this->assertAPISuccess($contributionPage);
1028 'contact_id' => $this->_individualId
,
1029 'receive_date' => '20120511',
1030 'total_amount' => 100.00,
1031 'financial_type_id' => 1,
1032 'contribution_page_id' => $contributionPage['id'],
1034 'invoice_id' => 67890,
1036 'contribution_status_id' => 2,
1039 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1040 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1041 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1042 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1043 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1044 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1045 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1046 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1047 $this->_checkFinancialRecords($contribution, 'pending');
1051 * Test that BAO defaults work.
1053 public function testCreateBAODefaults() {
1054 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1055 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1056 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1057 'id' => $contribution['id'],
1058 'api.contribution.delete' => 1,
1060 $this->assertEquals(1, $contribution['contribution_status_id']);
1061 $this->assertEquals('Check', $contribution['payment_instrument']);
1065 * Function tests that line items, financial records are updated when contribution amount is changed.
1067 public function testCreateUpdateContributionChangeTotal() {
1068 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1069 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1071 'entity_id' => $contribution['id'],
1072 'entity_table' => 'civicrm_contribution',
1074 'return' => 'line_total',
1076 $this->assertEquals('100.00', $lineItems);
1077 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1078 // Financial trxn SUM = 100 + 5 (fee)
1079 $this->assertEquals('105.00', $trxnAmount);
1082 'id' => $contribution['id'],
1083 'total_amount' => '125',
1085 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1087 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1089 'entity_id' => $contribution['id'],
1090 'entity_table' => 'civicrm_contribution',
1092 'return' => 'line_total',
1095 $this->assertEquals('125.00', $lineItems);
1096 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1098 // Financial trxn SUM = 125 + 5 (fee).
1099 $this->assertEquals('130.00', $trxnAmount);
1100 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1104 * Function tests that line items, financial records are updated when pay later contribution is received.
1106 public function testCreateUpdateContributionPayLater() {
1107 $contribParams = array(
1108 'contact_id' => $this->_individualId
,
1109 'receive_date' => '2012-01-01',
1110 'total_amount' => 100.00,
1111 'financial_type_id' => $this->_financialTypeId
,
1112 'payment_instrument_id' => 1,
1113 'contribution_status_id' => 2,
1114 'is_pay_later' => 1,
1117 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1119 $newParams = array_merge($contribParams, array(
1120 'id' => $contribution['id'],
1121 'contribution_status_id' => 1,
1124 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1125 $contribution = $contribution['values'][$contribution['id']];
1126 $this->assertEquals($contribution['contribution_status_id'], '1');
1127 $this->_checkFinancialItem($contribution['id'], 'paylater');
1128 $this->_checkFinancialTrxn($contribution, 'payLater');
1132 * Function tests that financial records are updated when Payment Instrument is changed.
1134 public function testCreateUpdateContributionPaymentInstrument() {
1135 $instrumentId = $this->_addPaymentInstrument();
1136 $contribParams = array(
1137 'contact_id' => $this->_individualId
,
1138 'total_amount' => 100.00,
1139 'financial_type_id' => $this->_financialTypeId
,
1140 'payment_instrument_id' => 4,
1141 'contribution_status_id' => 1,
1144 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1146 $newParams = array_merge($contribParams, array(
1147 'id' => $contribution['id'],
1148 'payment_instrument_id' => $instrumentId,
1151 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1152 $this->assertAPISuccess($contribution);
1153 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1155 // cleanup - delete created payment instrument
1156 $this->_deletedAddedPaymentInstrument();
1160 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1162 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1163 $instrumentId = $this->_addPaymentInstrument();
1164 $contribParams = array(
1165 'contact_id' => $this->_individualId
,
1166 'total_amount' => -100.00,
1167 'financial_type_id' => $this->_financialTypeId
,
1168 'payment_instrument_id' => 4,
1169 'contribution_status_id' => 1,
1172 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1174 $newParams = array_merge($contribParams, array(
1175 'id' => $contribution['id'],
1176 'payment_instrument_id' => $instrumentId,
1179 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1180 $this->assertAPISuccess($contribution);
1181 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1183 // cleanup - delete created payment instrument
1184 $this->_deletedAddedPaymentInstrument();
1188 * Function tests that financial records are added when Contribution is Refunded.
1190 public function testCreateUpdateContributionRefund() {
1191 $contributionParams = array(
1192 'contact_id' => $this->_individualId
,
1193 'receive_date' => '2012-01-01',
1194 'total_amount' => 100.00,
1195 'financial_type_id' => $this->_financialTypeId
,
1196 'payment_instrument_id' => 4,
1197 'contribution_status_id' => 1,
1198 'trxn_id' => 'original_payment',
1200 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1201 $newParams = array_merge($contributionParams, array(
1202 'id' => $contribution['id'],
1203 'contribution_status_id' => 'Refunded',
1204 'cancel_date' => '2015-01-01 09:00',
1205 'refund_trxn_id' => 'the refund',
1209 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1210 $this->_checkFinancialTrxn($contribution, 'refund');
1211 $this->_checkFinancialItem($contribution['id'], 'refund');
1212 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1213 'id' => $contribution['id'],
1214 'return' => 'trxn_id',
1219 * Refund a contribution for a financial type with a contra account.
1221 * CRM-17951 the contra account is a financial account with a relationship to a
1222 * financial type. It is not always configured but should be reflected
1223 * in the financial_trxn & financial_item table if it is.
1225 public function testCreateUpdateChargebackContributionDefaultAccount() {
1226 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1227 $this->callAPISuccess('Contribution', 'create', array(
1228 'id' => $contribution['id'],
1229 'contribution_status_id' => 'Chargeback',
1231 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1233 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1234 'contribution_id' => $contribution['id'],
1235 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1237 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1238 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1239 'total_amount' => -100,
1240 'status_id' => 'Chargeback',
1241 'to_financial_account_id' => 6,
1246 * Refund a contribution for a financial type with a contra account.
1248 * CRM-17951 the contra account is a financial account with a relationship to a
1249 * financial type. It is not always configured but should be reflected
1250 * in the financial_trxn & financial_item table if it is.
1252 public function testCreateUpdateChargebackContributionCustomAccount() {
1253 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1254 'name' => 'Chargeback Account',
1255 'is_active' => TRUE,
1258 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1259 'entity_id' => $this->_financialTypeId
,
1260 'entity_table' => 'civicrm_financial_type',
1261 'account_relationship' => 'Chargeback Account is',
1262 'financial_account_id' => 'Chargeback Account',
1265 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1266 $this->callAPISuccess('Contribution', 'create', array(
1267 'id' => $contribution['id'],
1268 'contribution_status_id' => 'Chargeback',
1270 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1272 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1273 'contribution_id' => $contribution['id'],
1274 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1276 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1278 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1279 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1280 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1284 * Refund a contribution for a financial type with a contra account.
1286 * CRM-17951 the contra account is a financial account with a relationship to a
1287 * financial type. It is not always configured but should be reflected
1288 * in the financial_trxn & financial_item table if it is.
1290 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1291 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1292 'name' => 'Refund Account',
1293 'is_active' => TRUE,
1296 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1297 'entity_id' => $this->_financialTypeId
,
1298 'entity_table' => 'civicrm_financial_type',
1299 'account_relationship' => 'Credit/Contra Revenue Account is',
1300 'financial_account_id' => 'Refund Account',
1303 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1304 $this->callAPISuccess('Contribution', 'create', array(
1305 'id' => $contribution['id'],
1306 'contribution_status_id' => 'Refunded',
1309 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1310 'contribution_id' => $contribution['id'],
1311 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1313 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1315 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1316 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1317 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1321 * Function tests that trxn_id is set when passed in.
1323 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1324 * when trxn_id is passed in.
1326 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1327 $contributionParams = array(
1328 'contact_id' => $this->_individualId
,
1329 'receive_date' => '2012-01-01',
1330 'total_amount' => 100.00,
1331 'financial_type_id' => $this->_financialTypeId
,
1332 'payment_instrument_id' => 4,
1333 'contribution_status_id' => 1,
1334 'trxn_id' => 'original_payment',
1336 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1337 $newParams = array_merge($contributionParams, array(
1338 'id' => $contribution['id'],
1339 'contribution_status_id' => 'Refunded',
1340 'cancel_date' => '2015-01-01 09:00',
1341 'trxn_id' => 'the refund',
1345 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1346 $this->_checkFinancialTrxn($contribution, 'refund');
1347 $this->_checkFinancialItem($contribution['id'], 'refund');
1348 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1349 'id' => $contribution['id'],
1350 'return' => 'trxn_id',
1355 * Function tests that trxn_id is set when passed in.
1357 * Here we ensure that the civicrm_contribution.trxn_id is set
1358 * when trxn_id is passed in but if refund_trxn_id is different then that
1359 * is kept for the refund transaction.
1361 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1362 $contributionParams = array(
1363 'contact_id' => $this->_individualId
,
1364 'receive_date' => '2012-01-01',
1365 'total_amount' => 100.00,
1366 'financial_type_id' => $this->_financialTypeId
,
1367 'payment_instrument_id' => 4,
1368 'contribution_status_id' => 1,
1369 'trxn_id' => 'original_payment',
1371 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1372 $newParams = array_merge($contributionParams, array(
1373 'id' => $contribution['id'],
1374 'contribution_status_id' => 'Refunded',
1375 'cancel_date' => '2015-01-01 09:00',
1376 'trxn_id' => 'cont id',
1377 'refund_trxn_id' => 'the refund',
1381 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1382 $this->_checkFinancialTrxn($contribution, 'refund');
1383 $this->_checkFinancialItem($contribution['id'], 'refund');
1384 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1385 'id' => $contribution['id'],
1386 'return' => 'trxn_id',
1391 * Function tests that refund_trxn_id is set when passed in empty.
1393 * Here we ensure that the civicrm_contribution.trxn_id is set
1394 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1395 * is kept for the refund transaction.
1397 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1398 $contributionParams = array(
1399 'contact_id' => $this->_individualId
,
1400 'receive_date' => '2012-01-01',
1401 'total_amount' => 100.00,
1402 'financial_type_id' => $this->_financialTypeId
,
1403 'payment_instrument_id' => 4,
1404 'contribution_status_id' => 1,
1405 'trxn_id' => 'original_payment',
1407 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1408 $newParams = array_merge($contributionParams, array(
1409 'id' => $contribution['id'],
1410 'contribution_status_id' => 'Refunded',
1411 'cancel_date' => '2015-01-01 09:00',
1412 'trxn_id' => 'cont id',
1413 'refund_trxn_id' => '',
1417 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1418 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1419 $this->_checkFinancialItem($contribution['id'], 'refund');
1420 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1421 'id' => $contribution['id'],
1422 'return' => 'trxn_id',
1427 * Function tests invalid contribution status change.
1429 public function testCreateUpdateContributionInValidStatusChange() {
1430 $contribParams = array(
1432 'receive_date' => '2012-01-01',
1433 'total_amount' => 100.00,
1434 'financial_type_id' => 1,
1435 'payment_instrument_id' => 1,
1436 'contribution_status_id' => 1,
1438 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1439 $newParams = array_merge($contribParams, array(
1440 'id' => $contribution['id'],
1441 'contribution_status_id' => 2,
1444 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1449 * Function tests that financial records are added when Pending Contribution is Canceled.
1451 public function testCreateUpdateContributionCancelPending() {
1452 $contribParams = array(
1453 'contact_id' => $this->_individualId
,
1454 'receive_date' => '2012-01-01',
1455 'total_amount' => 100.00,
1456 'financial_type_id' => $this->_financialTypeId
,
1457 'payment_instrument_id' => 1,
1458 'contribution_status_id' => 2,
1459 'is_pay_later' => 1,
1462 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1463 $newParams = array_merge($contribParams, array(
1464 'id' => $contribution['id'],
1465 'contribution_status_id' => 3,
1466 'cancel_date' => '2012-02-02 09:00',
1469 //Check if trxn_date is same as cancel_date.
1470 $checkTrxnDate = array(
1471 'trxn_date' => '2012-02-02 09:00:00',
1473 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1474 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1475 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1479 * Function tests that financial records are added when Financial Type is Changed.
1481 public function testCreateUpdateContributionChangeFinancialType() {
1482 $contribParams = array(
1483 'contact_id' => $this->_individualId
,
1484 'receive_date' => '2012-01-01',
1485 'total_amount' => 100.00,
1486 'financial_type_id' => 1,
1487 'payment_instrument_id' => 1,
1488 'contribution_status_id' => 1,
1491 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1492 $newParams = array_merge($contribParams, array(
1493 'id' => $contribution['id'],
1494 'financial_type_id' => 3,
1497 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1498 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1499 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1503 * Test that update does not change status id CRM-15105.
1505 public function testCreateUpdateWithoutChangingPendingStatus() {
1506 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1507 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1508 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1509 'id' => $contribution['id'],
1510 'api.contribution.delete' => 1,
1512 $this->assertEquals(2, $contribution['contribution_status_id']);
1516 * Test Updating a Contribution.
1518 * CHANGE: we require the API to do an incremental update
1520 public function testCreateUpdateContribution() {
1521 $contributionID = $this->contributionCreate(array(
1522 'contact_id' => $this->_individualId
,
1523 'trxn_id' => 212355,
1524 'financial_type_id' => $this->_financialTypeId
,
1525 'invoice_id' => 'old_invoice',
1527 $old_params = array(
1528 'contribution_id' => $contributionID,
1530 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1531 $this->assertEquals($original['id'], $contributionID);
1532 //set up list of old params, verify
1534 //This should not be required on update:
1535 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1536 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1537 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1538 $old_source = $original['values'][$contributionID]['contribution_source'];
1540 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1541 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1543 //check against values in CiviUnitTestCase::createContribution()
1544 $this->assertEquals($old_contact_id, $this->_individualId
);
1545 $this->assertEquals($old_fee_amount, 5.00);
1546 $this->assertEquals($old_source, 'SSF');
1547 $this->assertEquals($old_trxn_id, 212355);
1548 $this->assertEquals($old_invoice_id, 'old_invoice');
1550 'id' => $contributionID,
1551 'contact_id' => $this->_individualId
,
1552 'total_amount' => 105.00,
1553 'fee_amount' => 7.00,
1554 'financial_type_id' => $this->_financialTypeId
,
1555 'non_deductible_amount' => 22.00,
1556 'contribution_status_id' => 1,
1557 'note' => 'Donating for Noble Cause',
1560 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1562 $new_params = array(
1563 'contribution_id' => $contribution['id'],
1566 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1568 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1569 $this->assertEquals($contribution['total_amount'], 105.00);
1570 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1571 $this->assertEquals($contribution['financial_type'], 'Donation');
1572 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1573 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1574 $this->assertEquals($contribution['fee_amount'], 7.00);
1575 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1576 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1577 $this->assertEquals($contribution['contribution_source'], $old_source);
1578 $this->assertEquals($contribution['contribution_status'], 'Completed');
1580 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1583 'contribution_id' => $contributionID,
1586 $result = $this->callAPISuccess('contribution', 'delete', $params);
1587 $this->assertAPISuccess($result);
1591 * Attempt (but fail) to delete a contribution without parameters.
1593 public function testDeleteEmptyParamsContribution() {
1595 $this->callAPIFailure('contribution', 'delete', $params);
1598 public function testDeleteParamsNotArrayContribution() {
1599 $params = 'contribution_id= 1';
1600 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1601 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1604 public function testDeleteWrongParamContribution() {
1606 'contribution_source' => 'SSF',
1609 $this->callAPIFailure('contribution', 'delete', $params);
1612 public function testDeleteContribution() {
1613 $contributionID = $this->contributionCreate(array(
1614 'contact_id' => $this->_individualId
,
1615 'financial_type_id' => $this->_financialTypeId
,
1618 'id' => $contributionID,
1620 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1624 * Test civicrm_contribution_search with empty params.
1626 * All available contributions expected.
1628 public function testSearchEmptyParams() {
1632 'contact_id' => $this->_individualId
,
1633 'receive_date' => date('Ymd'),
1634 'total_amount' => 100.00,
1635 'financial_type_id' => $this->_financialTypeId
,
1636 'non_deductible_amount' => 10.00,
1637 'fee_amount' => 5.00,
1638 'net_amount' => 95.00,
1640 'invoice_id' => 78910,
1642 'contribution_status_id' => 1,
1645 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1647 $result = $this->callAPISuccess('contribution', 'get', $params);
1648 // We're taking the first element.
1649 $res = $result['values'][$contribution['id']];
1651 $this->assertEquals($p['contact_id'], $res['contact_id']);
1652 $this->assertEquals($p['total_amount'], $res['total_amount']);
1653 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1654 $this->assertEquals($p['net_amount'], $res['net_amount']);
1655 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1656 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1657 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1658 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1659 $this->assertEquals($p['source'], $res['contribution_source']);
1660 // contribution_status_id = 1 => Completed
1661 $this->assertEquals('Completed', $res['contribution_status']);
1663 $this->contributionDelete($contribution['id']);
1667 * Test civicrm_contribution_search. Success expected.
1669 public function testSearch() {
1671 'contact_id' => $this->_individualId
,
1672 'receive_date' => date('Ymd'),
1673 'total_amount' => 100.00,
1674 'financial_type_id' => $this->_financialTypeId
,
1675 'non_deductible_amount' => 10.00,
1676 'contribution_status_id' => 1,
1679 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1682 'contact_id' => $this->_individualId
,
1683 'receive_date' => date('Ymd'),
1684 'total_amount' => 200.00,
1685 'financial_type_id' => $this->_financialTypeId
,
1686 'non_deductible_amount' => 20.00,
1687 'trxn_id' => 5454565,
1688 'invoice_id' => 1212124,
1689 'fee_amount' => 50.00,
1690 'net_amount' => 60.00,
1691 'contribution_status_id' => 2,
1694 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1697 'contribution_id' => $contribution2['id'],
1700 $result = $this->callAPISuccess('contribution', 'get', $params);
1701 $res = $result['values'][$contribution2['id']];
1703 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1704 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1705 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1706 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1707 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1708 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1709 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1710 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1711 // contribution_status_id = 2 => Pending
1712 $this->assertEquals('Pending', $res['contribution_status']);
1714 $this->contributionDelete($contribution1['id']);
1715 $this->contributionDelete($contribution2['id']);
1719 * Test completing a transaction via the API.
1721 * Note that we are creating a logged in user because email goes out from
1724 public function testCompleteTransaction() {
1725 $mut = new CiviMailUtils($this, TRUE);
1726 $this->swapMessageTemplateForTestTemplate();
1727 $this->createLoggedInUser();
1728 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1729 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1730 $this->callAPISuccess('contribution', 'completetransaction', array(
1731 'id' => $contribution['id'],
1733 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1734 $this->assertEquals('SSF', $contribution['contribution_source']);
1735 $this->assertEquals('Completed', $contribution['contribution_status']);
1736 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1737 $mut->checkMailLog(array(
1738 'email:::anthony_anderson@civicrm.org',
1742 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1743 "receipt_date:::\n",
1744 'contributeMode:::notify',
1745 'title:::Contribution',
1746 'displayName:::Mr. Anthony Anderson II',
1747 'contributionStatus:::Completed',
1750 $this->revertTemplateToReservedTemplate();
1754 * Test completing a transaction via the API with a non-USD transaction.
1756 public function testCompleteTransactionEuro() {
1757 $mut = new CiviMailUtils($this, TRUE);
1758 $this->swapMessageTemplateForTestTemplate();
1759 $this->createLoggedInUser();
1760 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1761 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1763 $this->callAPISuccess('contribution', 'completetransaction', array(
1764 'id' => $contribution['id'],
1767 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1768 $this->assertEquals('SSF', $contribution['contribution_source']);
1769 $this->assertEquals('Completed', $contribution['contribution_status']);
1770 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1772 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1773 $entityFinancialTransaction = reset($entityFinancialTransactions);
1774 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1775 $this->assertEquals('EUR', $financialTrxn['currency']);
1777 $mut->checkMailLog(array(
1778 'email:::anthony_anderson@civicrm.org',
1782 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1783 "receipt_date:::\n",
1784 'contributeMode:::notify',
1785 'title:::Contribution',
1786 'displayName:::Mr. Anthony Anderson II',
1787 'contributionStatus:::Completed',
1790 $this->revertTemplateToReservedTemplate();
1794 * Test to ensure mail is sent on chosing pay later
1796 public function testpayLater() {
1797 $mut = new CiviMailUtils($this, TRUE);
1798 $this->swapMessageTemplateForTestTemplate();
1799 $this->createLoggedInUser();
1801 // create contribution page first
1802 $contributionPageParams = array(
1803 'title' => 'Help Support CiviCRM!',
1804 'financial_type_id' => 1,
1805 'is_monetary' => TRUE,
1806 'is_pay_later' => 1,
1807 'is_quick_config' => TRUE,
1808 'pay_later_text' => 'I will send payment by check',
1809 'pay_later_receipt' => 'This is a pay later reciept',
1810 'is_allow_other_amount' => 1,
1811 'min_amount' => 10.00,
1812 'max_amount' => 10000.00,
1813 'goal_amount' => 100000.00,
1814 'is_email_receipt' => 1,
1816 'amount_block_is_active' => 1,
1817 'currency' => 'USD',
1818 'is_billing_required' => 0,
1820 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1822 // submit form values
1823 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1825 'id' => $contributionPageResult['id'],
1826 'contact_id' => $this->_individualId
,
1827 'email-5' => 'anthony_anderson@civicrm.org',
1828 'payment_processor_id' => 0,
1831 'currencyID' => 'USD',
1832 'is_pay_later' => 1,
1833 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1834 'is_quick_config' => 1,
1835 'description' => 'Online Contribution: Help Support CiviCRM!',
1836 'price_set_id' => $priceSet['id'],
1838 $this->callAPISuccess('contribution_page', 'submit', $params);
1840 $mut->checkMailLog(array(
1842 'email:::anthony_anderson@civicrm.org',
1843 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1844 'displayName:::Mr. Anthony Anderson II',
1845 'contributionPageId:::' . $contributionPageResult['id'],
1846 'title:::' . $contributionPageParams['title'],
1847 'amount:::' . $params['amount'],
1850 $this->revertTemplateToReservedTemplate();
1854 * Test to check whether contact billing address is used when no contribution address
1856 public function testBillingAddress() {
1857 $mut = new CiviMailUtils($this, TRUE);
1858 $this->swapMessageTemplateForTestTemplate();
1859 $this->createLoggedInUser();
1861 //Scenario 1: When Contact don't have any address
1862 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1863 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1864 $this->callAPISuccess('contribution', 'completetransaction', array(
1865 'id' => $contribution['id'],
1867 $mut->checkMailLog(array(
1871 // Scenario 2: Contribution using address
1872 $address = $this->callAPISuccess('address', 'create', array(
1873 'street_address' => 'contribution billing st',
1874 'location_type_id' => 2,
1875 'contact_id' => $this->_params
['contact_id'],
1877 $params = array_merge($this->_params
, array(
1878 'contribution_status_id' => 2,
1879 'address_id' => $address['id'],
1882 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1883 $this->callAPISuccess('contribution', 'completetransaction', array(
1884 'id' => $contribution['id'],
1886 $mut->checkMailLog(array(
1887 'address:::contribution billing st',
1890 // Scenario 3: Contribution wtth no address but contact has a billing address
1891 $this->callAPISuccess('address', 'create', array(
1892 'id' => $address['id'],
1893 'street_address' => 'is billing st',
1894 'contact_id' => $this->_params
['contact_id'],
1896 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1897 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1898 $this->callAPISuccess('contribution', 'completetransaction', array(
1899 'id' => $contribution['id'],
1901 $mut->checkMailLog(array(
1902 'address:::is billing st',
1906 $this->revertTemplateToReservedTemplate();
1910 * Test completing a transaction via the API.
1912 * Note that we are creating a logged in user because email goes out from
1915 public function testCompleteTransactionFeeAmount() {
1916 $this->createLoggedInUser();
1917 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1918 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1919 $this->callAPISuccess('contribution', 'completetransaction', array(
1920 'id' => $contribution['id'],
1921 'fee_amount' => '.56',
1922 'trxn_id' => '7778888',
1924 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1925 $this->assertEquals('Completed', $contribution['contribution_status']);
1926 $this->assertEquals('7778888', $contribution['trxn_id']);
1927 $this->assertEquals('.56', $contribution['fee_amount']);
1928 $this->assertEquals('99.44', $contribution['net_amount']);
1932 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed
1934 public function testCheckTaxAmount() {
1935 $contact = $this->createLoggedInUser();
1936 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1937 'name' => 'Test taxable financial Type',
1941 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1942 'name' => 'Test Tax financial account ',
1943 'contact_id' => $contact,
1944 'financial_account_type_id' => 2,
1951 $financialTypeId = $financialType['id'];
1952 $financialAccountId = $financialAccount['id'];
1953 $financialAccountParams = array(
1954 'entity_table' => 'civicrm_financial_type',
1955 'entity_id' => $financialTypeId,
1956 'account_relationship' => 10,
1957 'financial_account_id' => $financialAccountId,
1959 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
1961 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1962 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1963 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1964 $this->callAPISuccess('contribution', 'completetransaction', array(
1965 'id' => $contribution['id'],
1966 'trxn_id' => '777788888',
1967 'fee_amount' => '6.00',
1969 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
1970 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
1971 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
1972 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
1976 * Test repeat contribution successfully creates line item.
1978 public function testRepeatTransaction() {
1979 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
1980 $this->callAPISuccess('contribution', 'repeattransaction', array(
1981 'original_contribution_id' => $originalContribution['id'],
1982 'contribution_status_id' => 'Completed',
1983 'trxn_id' => uniqid(),
1985 $lineItemParams = array(
1986 'entity_id' => $originalContribution['id'],
1994 'financial_type_id',
1995 'deductible_amount',
1996 'price_field_value_id',
2000 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2001 'entity_id' => $originalContribution['id'],
2003 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2004 'entity_id' => $originalContribution['id'] +
1,
2006 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2007 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2008 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2009 $this->_checkFinancialRecords(array(
2010 'id' => $originalContribution['id'] +
1,
2011 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2012 'id' => $originalContribution['payment_processor_id'],
2013 'return' => 'payment_instrument_id',
2016 $this->quickCleanUpFinancialEntities();
2020 * Test repeat contribution successfully creates line items (plural).
2022 public function testRepeatTransactionLineItems() {
2024 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2025 $this->callAPISuccess('contribution', 'repeattransaction', array(
2026 'original_contribution_id' => $originalContribution['id'],
2027 'contribution_status_id' => 'Completed',
2028 'trxn_id' => uniqid(),
2031 $lineItemParams = array(
2032 'entity_id' => $originalContribution['id'],
2040 'financial_type_id',
2041 'deductible_amount',
2042 'price_field_value_id',
2046 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2047 'entity_id' => $originalContribution['id'],
2049 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2050 'entity_id' => $originalContribution['id'] +
1,
2053 // unset id and entity_id for all of them to be able to compare the lineItems:
2054 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2055 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2056 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2058 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2059 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2060 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2062 // CRM-19309 so in future we also want to:
2063 // check that financial_line_items have been created for entity_id 3 and 4;
2065 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2066 $this->quickCleanUpFinancialEntities();
2070 * Test repeat contribution successfully creates is_test transaction.
2072 public function testRepeatTransactionIsTest() {
2073 $this->_params
['is_test'] = 1;
2074 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2076 $this->callAPISuccess('contribution', 'repeattransaction', array(
2077 'original_contribution_id' => $originalContribution['id'],
2078 'contribution_status_id' => 'Completed',
2079 'trxn_id' => uniqid(),
2081 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2085 * Test repeat contribution passed in status.
2087 public function testRepeatTransactionPassedInStatus() {
2088 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2090 $this->callAPISuccess('contribution', 'repeattransaction', array(
2091 'original_contribution_id' => $originalContribution['id'],
2092 'contribution_status_id' => 'Pending',
2093 'trxn_id' => uniqid(),
2095 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2099 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2101 public function testRepeatTransactionAcceptRecurID() {
2102 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2103 'contact_id' => $this->_individualId
,
2104 'installments' => '12',
2105 'frequency_interval' => '1',
2107 'contribution_status_id' => 1,
2108 'start_date' => '2012-01-01 00:00:00',
2109 'currency' => 'USD',
2110 'frequency_unit' => 'month',
2111 'payment_processor_id' => $this->paymentProcessorID
,
2113 $this->callAPISuccess('contribution', 'create', array_merge(
2115 array('contribution_recur_id' => $contributionRecur['id']))
2118 $this->callAPISuccess('contribution', 'repeattransaction', array(
2119 'contribution_recur_id' => $contributionRecur['id'],
2120 'contribution_status_id' => 'Completed',
2121 'trxn_id' => uniqid(),
2124 $this->quickCleanUpFinancialEntities();
2128 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2130 public function testRepeatTransactionTestRecurId() {
2131 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2132 'contact_id' => $this->_individualId
,
2133 'frequency_interval' => '1',
2135 'contribution_status_id' => 1,
2136 'start_date' => '2017-01-01 00:00:00',
2137 'currency' => 'USD',
2138 'frequency_unit' => 'month',
2139 'payment_processor_id' => $this->paymentProcessorID
,
2142 $this->callAPISuccess('contribution', 'create', array_merge(
2145 'contribution_recur_id' => $contributionRecur['id'],
2150 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2151 'contribution_recur_id' => $contributionRecur['id'],
2152 'contribution_status_id' => 'Completed',
2153 'trxn_id' => uniqid(),
2156 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2157 $this->quickCleanUpFinancialEntities();
2160 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2162 * ( Tests membership is renewed after repeattransaction. )
2164 public function testRepeatTransactionUsesCompleted() {
2165 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2167 $this->callAPISuccess('contribution', 'create', array(
2168 'contact_id' => $originalContribution['values'][1]['contact_id'],
2169 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2170 'total_amount' => $originalContribution['values'][1]['total_amount'],
2171 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2172 'contribution_status_id' => "Failed",
2175 $this->callAPISuccess('membership', 'create', array(
2176 'id' => $membership['id'],
2177 'end_date' => 'yesterday',
2181 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2182 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2183 'contribution_status_id' => 'Completed',
2184 'trxn_id' => 'bobsled',
2187 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2188 'id' => $membership['id'],
2189 'return' => 'status_id',
2192 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2194 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2195 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2196 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2197 $this->quickCleanUpFinancialEntities();
2198 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2202 * CRM-16397 test appropriate action if total amount has changed for single line items.
2204 public function testRepeatTransactionAlteredAmount() {
2205 $paymentProcessorID = $this->paymentProcessorCreate();
2206 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2207 'contact_id' => $this->_individualId
,
2208 'installments' => '12',
2209 'frequency_interval' => '1',
2211 'contribution_status_id' => 1,
2212 'start_date' => '2012-01-01 00:00:00',
2213 'currency' => 'USD',
2214 'frequency_unit' => 'month',
2215 'payment_processor_id' => $paymentProcessorID,
2217 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2220 'contribution_recur_id' => $contributionRecur['id'],
2224 $this->callAPISuccess('contribution', 'repeattransaction', array(
2225 'original_contribution_id' => $originalContribution['id'],
2226 'contribution_status_id' => 'Completed',
2227 'trxn_id' => uniqid(),
2228 'total_amount' => '400',
2232 $lineItemParams = array(
2233 'entity_id' => $originalContribution['id'],
2241 'financial_type_id',
2242 'deductible_amount',
2243 'price_field_value_id',
2247 $this->callAPISuccessGetSingle('contribution', array(
2248 'total_amount' => 400,
2250 'net_amount' => 350,
2252 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2253 'entity_id' => $originalContribution['id'],
2255 $expectedLineItem = array_merge(
2256 $lineItem1['values'][0], array(
2257 'line_total' => '400.00',
2258 'unit_price' => '400.00',
2262 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2263 'entity_id' => $originalContribution['id'] +
1,
2266 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2267 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2268 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2272 * CRM-17718 test appropriate action if financial type has changed for single line items.
2274 public function testRepeatTransactionPassedInFinancialType() {
2275 $originalContribution = $this->setUpRecurringContribution();
2277 $this->callAPISuccess('contribution', 'repeattransaction', array(
2278 'original_contribution_id' => $originalContribution['id'],
2279 'contribution_status_id' => 'Completed',
2280 'trxn_id' => uniqid(),
2281 'financial_type_id' => 2,
2283 $lineItemParams = array(
2284 'entity_id' => $originalContribution['id'],
2292 'financial_type_id',
2293 'deductible_amount',
2294 'price_field_value_id',
2299 $this->callAPISuccessGetSingle('contribution', array(
2300 'total_amount' => 100,
2301 'financial_type_id' => 2,
2303 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2304 'entity_id' => $originalContribution['id'],
2306 $expectedLineItem = array_merge(
2307 $lineItem1['values'][0], array(
2308 'line_total' => '100.00',
2309 'unit_price' => '100.00',
2310 'financial_type_id' => 2,
2311 'contribution_type_id' => 2,
2314 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2315 'entity_id' => $originalContribution['id'] +
1,
2317 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2318 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2319 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2323 * CRM-17718 test appropriate action if financial type has changed for single line items.
2325 public function testRepeatTransactionUpdatedFinancialType() {
2326 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2328 $this->callAPISuccess('contribution', 'repeattransaction', array(
2329 'contribution_recur_id' => $originalContribution['id'],
2330 'contribution_status_id' => 'Completed',
2331 'trxn_id' => uniqid(),
2333 $lineItemParams = array(
2334 'entity_id' => $originalContribution['id'],
2342 'financial_type_id',
2343 'deductible_amount',
2344 'price_field_value_id',
2349 $this->callAPISuccessGetSingle('contribution', array(
2350 'total_amount' => 100,
2351 'financial_type_id' => 2,
2353 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2354 'entity_id' => $originalContribution['id'],
2356 $expectedLineItem = array_merge(
2357 $lineItem1['values'][0], array(
2358 'line_total' => '100.00',
2359 'unit_price' => '100.00',
2360 'financial_type_id' => 2,
2361 'contribution_type_id' => 2,
2365 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2366 'entity_id' => $originalContribution['id'] +
1,
2368 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2369 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2370 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2374 * CRM-16397 test appropriate action if campaign has been passed in.
2376 public function testRepeatTransactionPassedInCampaign() {
2377 $paymentProcessorID = $this->paymentProcessorCreate();
2378 $campaignID = $this->campaignCreate();
2379 $campaignID2 = $this->campaignCreate();
2380 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2381 'contact_id' => $this->_individualId
,
2382 'installments' => '12',
2383 'frequency_interval' => '1',
2385 'contribution_status_id' => 1,
2386 'start_date' => '2012-01-01 00:00:00',
2387 'currency' => 'USD',
2388 'frequency_unit' => 'month',
2389 'payment_processor_id' => $paymentProcessorID,
2391 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2394 'contribution_recur_id' => $contributionRecur['id'],
2395 'campaign_id' => $campaignID,
2399 $this->callAPISuccess('contribution', 'repeattransaction', array(
2400 'original_contribution_id' => $originalContribution['id'],
2401 'contribution_status_id' => 'Completed',
2402 'trxn_id' => uniqid(),
2403 'campaign_id' => $campaignID2,
2406 $this->callAPISuccessGetSingle('contribution', array(
2407 'total_amount' => 100,
2408 'campaign_id' => $campaignID2,
2413 * CRM-17718 campaign stored on contribution recur gets priority.
2415 * This reflects the fact we permit people to update them.
2417 public function testRepeatTransactionUpdatedCampaign() {
2418 $paymentProcessorID = $this->paymentProcessorCreate();
2419 $campaignID = $this->campaignCreate();
2420 $campaignID2 = $this->campaignCreate();
2421 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2422 'contact_id' => $this->_individualId
,
2423 'installments' => '12',
2424 'frequency_interval' => '1',
2426 'contribution_status_id' => 1,
2427 'start_date' => '2012-01-01 00:00:00',
2428 'currency' => 'USD',
2429 'frequency_unit' => 'month',
2430 'payment_processor_id' => $paymentProcessorID,
2431 'campaign_id' => $campaignID,
2433 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2436 'contribution_recur_id' => $contributionRecur['id'],
2437 'campaign_id' => $campaignID2,
2441 $this->callAPISuccess('contribution', 'repeattransaction', array(
2442 'original_contribution_id' => $originalContribution['id'],
2443 'contribution_status_id' => 'Completed',
2444 'trxn_id' => uniqid(),
2447 $this->callAPISuccessGetSingle('contribution', array(
2448 'total_amount' => 100,
2449 'campaign_id' => $campaignID,
2454 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2456 public function testCompleteTransactionNetAmountOK() {
2457 $this->createLoggedInUser();
2458 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2459 unset($params['net_amount']);
2460 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2461 $this->callAPISuccess('contribution', 'completetransaction', array(
2462 'id' => $contribution['id'],
2464 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2465 $this->assertEquals('Completed', $contribution['contribution_status']);
2466 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2470 * CRM-14151 - Test completing a transaction via the API.
2472 public function testCompleteTransactionWithReceiptDateSet() {
2473 $this->swapMessageTemplateForTestTemplate();
2474 $mut = new CiviMailUtils($this, TRUE);
2475 $this->createLoggedInUser();
2476 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2477 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2478 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2479 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2480 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2481 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2482 $mut->checkMailLog(array(
2483 'Receipt - Contribution',
2484 'receipt_date:::' . date('Ymd'),
2487 $this->revertTemplateToReservedTemplate();
2491 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2493 public function testCompleteTransactionWithEmailReceiptInput() {
2494 $contributionPage = $this->createReceiptableContributionPage();
2496 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2497 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2498 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2499 // Complete the transaction overriding is_email_receipt to = FALSE
2500 $this->callAPISuccess('contribution', 'completetransaction', array(
2501 'id' => $contribution['id'],
2502 'trxn_date' => date('2011-04-09'),
2503 'trxn_id' => 'kazam',
2504 'is_email_receipt' => 0,
2506 // Check if a receipt was issued
2507 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2508 $this->assertEquals('', $receipt_date);
2512 * Test that $is_recur is assigned to the receipt.
2514 public function testCompleteTransactionForRecurring() {
2516 $this->swapMessageTemplateForTestTemplate();
2517 $recurring = $this->setUpRecurringContribution();
2518 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2520 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2521 $this->_params
['contribution_recur_id'] = $recurring['id'];
2523 $contribution = $this->setUpForCompleteTransaction();
2525 $this->callAPISuccess('contribution', 'completetransaction', array(
2526 'id' => $contribution['id'],
2527 'trxn_date' => date('2011-04-09'),
2528 'trxn_id' => 'kazam',
2529 'is_email_receipt' => 1,
2532 $this->mut
->checkMailLog(array(
2534 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2537 $this->revertTemplateToReservedTemplate();
2540 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2542 * If passed in it will override the default from contribution page.
2544 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2545 $mut = new CiviMailUtils($this, TRUE);
2546 $this->createLoggedInUser();
2547 // Create a Contribution Page with is_email_receipt = FALSE
2548 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2549 'receipt_from_name' => 'Mickey Mouse',
2550 'receipt_from_email' => 'mickey@mouse.com',
2551 'title' => "Test Contribution Page",
2552 'financial_type_id' => 1,
2553 'currency' => 'CAD',
2554 'is_monetary' => TRUE,
2555 'is_email_receipt' => 0,
2557 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2558 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2559 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2560 // Complete the transaction overriding is_email_receipt to = TRUE
2561 $this->callAPISuccess('contribution', 'completetransaction', array(
2562 'id' => $contribution['id'],
2563 'is_email_receipt' => 1,
2565 $mut->checkMailLog(array(
2566 'Please print this receipt for your records.',
2572 * Complete the transaction using the template with all the possible.
2574 public function testCompleteTransactionWithTestTemplate() {
2575 $this->swapMessageTemplateForTestTemplate();
2576 $contribution = $this->setUpForCompleteTransaction();
2577 $this->callAPISuccess('contribution', 'completetransaction', array(
2578 'id' => $contribution['id'],
2579 'trxn_date' => date('2011-04-09'),
2580 'trxn_id' => 'kazam',
2582 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2583 $this->mut
->checkMailLog(array(
2584 'email:::anthony_anderson@civicrm.org',
2588 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2589 'receipt_date:::' . date('Ymd'),
2590 'contributeMode:::notify',
2591 'title:::Contribution',
2592 'displayName:::Mr. Anthony Anderson II',
2594 'contactID:::' . $this->_params
['contact_id'],
2595 'contributionID:::' . $contribution['id'],
2596 'financialTypeId:::1',
2597 'financialTypeName:::Donation',
2600 $this->revertTemplateToReservedTemplate();
2604 * Complete the transaction using the template with all the possible.
2606 public function testCompleteTransactionContributionPageFromAddress() {
2607 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2608 'receipt_from_name' => 'Mickey Mouse',
2609 'receipt_from_email' => 'mickey@mouse.com',
2610 'title' => "Test Contribution Page",
2611 'financial_type_id' => 1,
2612 'currency' => 'NZD',
2613 'goal_amount' => 50,
2614 'is_pay_later' => 1,
2615 'is_monetary' => TRUE,
2616 'is_email_receipt' => TRUE,
2618 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2619 $contribution = $this->setUpForCompleteTransaction();
2620 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2621 $this->mut
->checkMailLog(array(
2629 * Test completing first transaction in a recurring series.
2631 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2633 * @dataProvider getScheduledDateData
2635 * @param array $dataSet
2637 * @throws \Exception
2639 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2640 $paymentProcessorID = $this->paymentProcessorCreate();
2641 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2642 'contact_id' => $this->_individualId
,
2643 'installments' => '2',
2644 'frequency_interval' => '1',
2646 'contribution_status_id' => 'Pending',
2647 'start_date' => '2012-01-01 00:00:00',
2648 'currency' => 'USD',
2649 'frequency_unit' => 'month',
2650 'payment_processor_id' => $paymentProcessorID,
2651 ), $dataSet['data']));
2652 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2655 'contribution_recur_id' => $contributionRecur['id'],
2656 'contribution_status_id' => 'Pending',
2657 'receive_date' => $dataSet['receive_date'],
2660 $this->callAPISuccess('Contribution', 'completetransaction', array(
2661 'id' => $contribution,
2662 'receive_date' => $dataSet['receive_date'],
2664 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2665 'id' => $contributionRecur['id'],
2666 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2668 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2669 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2670 $this->callAPISuccess('Contribution', 'create', array_merge(
2673 'contribution_recur_id' => $contributionRecur['id'],
2674 'contribution_status_id' => 'Completed',
2677 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2678 'id' => $contributionRecur['id'],
2679 'return' => array('contribution_status_id'),
2681 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2685 * Get dates for testing.
2689 public function getScheduledDateData() {
2691 $result[]['2016-08-31-1-month'] = array(
2693 'start_date' => '2016-08-31',
2694 'frequency_interval' => 1,
2695 'frequency_unit' => 'month',
2697 'receive_date' => '2016-08-31',
2698 'expected' => '2016-10-01 00:00:00',
2700 $result[]['2012-01-01-1-month'] = array(
2702 'start_date' => '2012-01-01',
2703 'frequency_interval' => 1,
2704 'frequency_unit' => 'month',
2706 'receive_date' => '2012-01-01',
2707 'expected' => '2012-02-01 00:00:00',
2709 $result[]['2012-01-01-1-month'] = array(
2711 'start_date' => '2012-01-01',
2712 'frequency_interval' => 1,
2713 'frequency_unit' => 'month',
2715 'receive_date' => '2012-02-29',
2716 'expected' => '2012-03-29 00:00:00',
2718 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2720 'start_date' => '2012-01-01',
2721 'frequency_interval' => 1,
2722 'frequency_unit' => 'month',
2723 'next_sched_contribution_date' => '2012-02-29',
2725 'receive_date' => '2012-02-29 16:00:00',
2726 'expected' => '2012-03-29 00:00:00',
2732 * Test completing a pledge with the completeTransaction api..
2734 * Note that we are creating a logged in user because email goes out from
2737 public function testCompleteTransactionUpdatePledgePayment() {
2738 $this->swapMessageTemplateForTestTemplate();
2739 $mut = new CiviMailUtils($this, TRUE);
2740 $mut->clearMessages();
2741 $this->createLoggedInUser();
2742 $contributionID = $this->createPendingPledgeContribution();
2743 $this->callAPISuccess('contribution', 'completetransaction', array(
2744 'id' => $contributionID,
2745 'trxn_date' => '1 Feb 2013',
2747 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2748 'id' => $this->_ids
['pledge'],
2750 $this->assertEquals('Completed', $pledge['pledge_status']);
2752 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2753 'pledge_id' => $this->_ids
['pledge'],
2754 'return' => 'status_id',
2756 $this->assertEquals(1, $status);
2757 $mut->checkMailLog(array(
2759 'receive_date:::20130201000000',
2760 "receipt_date:::\n",
2763 $this->revertTemplateToReservedTemplate();
2767 * Test completing a transaction with an event via the API.
2769 * Note that we are creating a logged in user because email goes out from
2772 public function testCompleteTransactionWithParticipantRecord() {
2773 $mut = new CiviMailUtils($this, TRUE);
2774 $mut->clearMessages();
2775 $this->_individualId
= $this->createLoggedInUser();
2776 $contributionID = $this->createPendingParticipantContribution();
2777 $this->callAPISuccess('contribution', 'completetransaction', array(
2778 'id' => $contributionID,
2781 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2782 'id' => $this->_ids
['participant'],
2783 'return' => 'participant_status_id',
2785 $this->assertEquals(1, $participantStatus);
2787 //Assert only three activities are created.
2788 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_individualId
);
2789 $this->assertEquals(3, count($activities));
2790 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
2791 // record two activities before and after completing payment for Event registration
2792 $this->assertEquals(2, $activityNames['Event Registration']);
2793 // update the original 'Contribution' activity created after completing payment
2794 $this->assertEquals(1, $activityNames['Contribution']);
2796 $mut->checkMailLog(array(
2797 'Annual CiviCRM meet',
2799 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2805 * Test membership is renewed when transaction completed.
2807 public function testCompleteTransactionMembershipPriceSet() {
2808 $this->createPriceSetWithPage('membership');
2809 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2813 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2814 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2815 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2816 'membership_id' => $this->_ids
['membership'],
2818 $this->assertEquals(1, $logs['count']);
2819 $this->assertEquals($stateOfGrace, $membership['status_id']);
2820 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2821 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2822 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2823 $this->callAPISuccessGetSingle('LineItem', array(
2824 'entity_id' => $this->_ids
['membership'],
2825 'entity_table' => 'civicrm_membership',
2827 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2828 'membership_id' => $this->_ids
['membership'],
2830 $this->assertEquals(2, $logs['count']);
2831 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2832 //Assert only three activities are created.
2833 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
2834 $this->assertEquals(3, count($activities));
2835 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
2836 $this->assertArrayHasKey('Contribution', $activityNames);
2837 $this->assertArrayHasKey('Membership Signup', $activityNames);
2838 $this->assertArrayHasKey('Change Membership Status', $activityNames);
2839 $this->cleanUpAfterPriceSets();
2843 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2845 public function testPendingToCompleteContribution() {
2846 $contributionPage = $this->createPriceSetWithPage('membership');
2847 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2851 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2852 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2853 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2854 $activity = $this->callAPISuccess('Activity', 'get', array(
2855 'activity_type_id' => 'Membership Signup',
2856 'source_record_id' => $this->_ids
['membership'],
2857 'status_id' => 'Scheduled',
2859 $this->assertEquals(1, $activity['count']);
2861 // change pending contribution to completed
2862 $form = new CRM_Contribute_Form_Contribution();
2864 $form->_params
= array(
2865 'id' => $this->_ids
['contribution'],
2866 'total_amount' => 20,
2869 'financial_type_id' => 1,
2870 'receive_date' => '04/21/2015',
2871 'receive_date_time' => '11:27PM',
2872 'contact_id' => $this->_individualId
,
2873 'contribution_status_id' => 1,
2874 'billing_middle_name' => '',
2875 'billing_last_name' => 'Adams',
2876 'billing_street_address-5' => '790L Lincoln St S',
2877 'billing_city-5' => 'Maryknoll',
2878 'billing_state_province_id-5' => 1031,
2879 'billing_postal_code-5' => 10545,
2880 'billing_country_id-5' => 1228,
2881 'frequency_interval' => 1,
2882 'frequency_unit' => 'month',
2883 'installments' => '',
2884 'hidden_AdditionalDetail' => 1,
2885 'hidden_Premium' => 1,
2886 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2887 'receipt_date' => '',
2888 'receipt_date_time' => '',
2889 'payment_processor_id' => $this->paymentProcessorID
,
2890 'currency' => 'USD',
2891 'contribution_page_id' => $this->_ids
['contribution_page'],
2892 'contribution_mode' => 'membership',
2893 'source' => 'Membership Signup and Renewal',
2896 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2898 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
2901 // Case 2: After successful payment for Pending backoffice there are three activities created
2902 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
2903 $activity = $this->callAPISuccess('Activity', 'get', array(
2904 'activity_type_id' => 'Membership Signup',
2905 'source_record_id' => $this->_ids
['membership'],
2906 'status_id' => 'Completed',
2908 $this->assertEquals(1, $activity['count']);
2909 // 2.b Contribution activity created to record successful payment
2910 $activity = $this->callAPISuccess('Activity', 'get', array(
2911 'activity_type_id' => 'Contribution',
2912 'source_record_id' => $this->_ids
['contribution'],
2913 'status_id' => 'Completed',
2915 $this->assertEquals(1, $activity['count']);
2917 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
2918 $activity = $this->callAPISuccess('Activity', 'get', array(
2919 'activity_type_id' => 'Change Membership Status',
2920 'source_record_id' => $this->_ids
['membership'],
2921 'status_id' => 'Completed',
2923 $this->assertEquals(1, $activity['count']);
2924 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
2926 //Create another pending contribution for renewal
2927 $contribution = $this->callAPISuccess('contribution', 'create', array(
2929 'contact_id' => $this->_ids
['contact'],
2930 'receive_date' => date('Ymd'),
2931 'total_amount' => 20.00,
2932 'financial_type_id' => 1,
2933 'payment_instrument_id' => 'Credit Card',
2934 'non_deductible_amount' => 10.00,
2935 'trxn_id' => 'rdhfi88',
2936 'invoice_id' => 'dofhiewuyr',
2938 'contribution_status_id' => 2,
2939 'contribution_page_id' => $this->_ids
['contribution_page'],
2940 'api.membership_payment.create' => array('membership_id' => $this->_ids
['membership']),
2943 $this->callAPISuccess('line_item', 'create', array(
2944 'entity_id' => $contribution['id'],
2945 'entity_table' => 'civicrm_contribution',
2946 'contribution_id' => $contribution['id'],
2947 'price_field_id' => $this->_ids
['price_field'][0],
2951 'financial_type_id' => 1,
2952 'price_field_value_id' => $this->_ids
['price_field_value'][0],
2955 //Update it to Failed.
2956 $form->_params
['id'] = $contribution['id'];
2957 $form->_params
['contribution_status_id'] = 4;
2959 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2961 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
2964 //Existing membership should not get updated to expired.
2965 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2966 $this->assertNotEquals($membership['status_id'], 4);
2970 * Test membership is renewed when transaction completed.
2972 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2973 $this->createPriceSetWithPage('membership');
2974 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
2975 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2976 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2977 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2978 $this->cleanUpAfterPriceSets();
2981 public function cleanUpAfterPriceSets() {
2982 $this->quickCleanUpFinancialEntities();
2983 $this->contactDelete($this->_ids
['contact']);
2987 * Set up a pending transaction with a specific price field id.
2989 * @param int $priceFieldValueID
2991 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
2992 $contactID = $this->individualCreate();
2993 $membership = $this->callAPISuccess('membership', 'create', array(
2994 'contact_id' => $contactID,
2995 'membership_type_id' => $this->_ids
['membership_type'],
2996 'start_date' => 'yesterday - 1 year',
2997 'end_date' => 'yesterday',
2998 'join_date' => 'yesterday - 1 year',
3000 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3002 'contact_id' => $contactID,
3003 'receive_date' => date('Ymd'),
3004 'total_amount' => 20.00,
3005 'financial_type_id' => 1,
3006 'payment_instrument_id' => 'Credit Card',
3007 'non_deductible_amount' => 10.00,
3008 'trxn_id' => 'jdhfi' . rand(1, 100),
3009 'invoice_id' => 'djfhiew' . rand(5, 100),
3011 'contribution_status_id' => 2,
3012 'contribution_page_id' => $this->_ids
['contribution_page'],
3013 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3016 $this->callAPISuccess('line_item', 'create', array(
3017 'entity_id' => $contribution['id'],
3018 'entity_table' => 'civicrm_contribution',
3019 'contribution_id' => $contribution['id'],
3020 'price_field_id' => $this->_ids
['price_field'][0],
3024 'financial_type_id' => 1,
3025 'price_field_value_id' => $priceFieldValueID,
3027 $this->_ids
['contact'] = $contactID;
3028 $this->_ids
['contribution'] = $contribution['id'];
3029 $this->_ids
['membership'] = $membership['id'];
3033 * Test sending a mail via the API.
3035 public function testSendMail() {
3036 $mut = new CiviMailUtils($this, TRUE);
3037 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3038 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3039 'id' => $contribution['id'],
3040 'receipt_from_email' => 'api@civicrm.org',
3043 $mut->checkMailLog(array(
3045 'Contribution Information',
3046 'Please print this confirmation for your records',
3052 $this->checkCreditCardDetails($mut, $contribution['id']);
3057 * Test sending a mail via the API.
3058 * This simulates webform_civicrm using pay later contribution page
3060 public function testSendconfirmationPayLater() {
3061 $mut = new CiviMailUtils($this, TRUE);
3063 // Create contribution page
3064 $pageParams = array(
3065 'title' => 'Webform Contributions',
3066 'financial_type_id' => 1,
3067 'contribution_type_id' => 1,
3068 'is_confirm_enabled' => 1,
3069 'is_pay_later' => 1,
3070 'pay_later_text' => 'I will send payment by cheque',
3071 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3073 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3075 // Create pay later contribution
3076 $contribParams = array(
3077 'contact_id' => $this->_individualId
,
3078 'financial_type_id' => 1,
3079 'is_pay_later' => 1,
3080 'contribution_status_id' => 2,
3081 'contribution_page_id' => $contributionPage['id'],
3082 'total_amount' => '10.00',
3084 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3087 $lineItemParams = array(
3088 'contribution_id' => $contribution['id'],
3089 'entity_id' => $contribution['id'],
3090 'entity_table' => 'civicrm_contribution',
3091 'label' => 'My lineitem label',
3093 'unit_price' => "10.00",
3094 'line_total' => "10.00",
3096 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3100 civicrm_api3('contribution', 'sendconfirmation', array(
3101 'id' => $contribution['id'],
3102 'receipt_from_email' => 'api@civicrm.org',
3106 catch (Exception
$e) {
3107 // Need to figure out how to stop this some other day
3108 // We don't care about the Payment Processor because this is Pay Later
3109 // The point of this test is to check we get the pay_later version of the mail
3110 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3115 // Retrieve mail & check it has the pay_later_receipt info
3116 $mut->getMostRecentEmail('raw');
3117 $mut->checkMailLog(array(
3118 (string) $contribParams['total_amount'],
3119 $pageParams['pay_later_receipt'],
3129 * Check credit card details in sent mail via API
3131 * @param $mut obj CiviMailUtils instance
3132 * @param int $contributionID Contribution ID
3135 public function checkCreditCardDetails($mut, $contributionID) {
3136 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3137 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3138 'id' => $contributionID,
3139 'receipt_from_email' => 'api@civicrm.org',
3140 'payment_processor_id' => $this->paymentProcessorID
,
3143 $mut->checkMailLog(array(
3144 'Credit Card Information', // credit card header
3145 'Billing Name and Address', // billing header
3146 'anthony_anderson@civicrm.org', // billing name
3154 * Test sending a mail via the API.
3156 public function testSendMailEvent() {
3157 $mut = new CiviMailUtils($this, TRUE);
3158 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3159 $event = $this->eventCreate(array(
3160 'is_email_confirm' => 1,
3161 'confirm_from_email' => 'test@civicrm.org',
3163 $this->_eventID
= $event['id'];
3164 $participantParams = array(
3165 'contact_id' => $this->_individualId
,
3166 'event_id' => $this->_eventID
,
3169 // to ensure it matches later on
3170 'register_date' => '2007-07-21 00:00:00',
3171 'source' => 'Online Event Registration: API Testing',
3174 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3175 $this->callAPISuccess('participant_payment', 'create', array(
3176 'participant_id' => $participant['id'],
3177 'contribution_id' => $contribution['id'],
3179 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3180 'id' => $contribution['id'],
3181 'receipt_from_email' => 'api@civicrm.org',
3185 $mut->checkMailLog(array(
3186 'Annual CiviCRM meet',
3188 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3195 * This function does a GET & compares the result against the $params.
3197 * Use as a double check on Creates.
3199 * @param array $params
3201 * @param bool $delete
3203 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3205 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3211 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3213 $this->assertAPISuccess($contribution, 0);
3214 $values = $contribution['values'][$contribution['id']];
3215 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3216 // this is not returned in id format
3217 unset($params['payment_instrument_id']);
3218 $params['contribution_source'] = $params['source'];
3219 unset($params['source']);
3220 foreach ($params as $key => $value) {
3221 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3226 * Create a pending contribution & linked pending pledge record.
3228 public function createPendingPledgeContribution() {
3230 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
3231 $this->_ids
['pledge'] = $pledgeID;
3232 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
3233 'contribution_status_id' => 'Pending',
3234 'total_amount' => 500,
3237 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3238 'options' => array('limit' => 1),
3241 $this->callAPISuccess('PledgePayment', 'create', array(
3243 'contribution_id' =>
3244 $contribution['id'],
3245 'status_id' => 'Pending',
3246 'scheduled_amount' => 500,
3249 return $contribution['id'];
3253 * Create a pending contribution & linked pending participant record (along with an event).
3255 public function createPendingParticipantContribution() {
3256 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3257 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId
));
3258 $this->_ids
['participant'] = $participantID;
3259 $params = array_merge($this->_params
, array('contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3260 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3261 $this->callAPISuccess('participant_payment', 'create', array(
3262 'contribution_id' => $contribution['id'],
3263 'participant_id' => $participantID,
3265 $this->callAPISuccess('line_item', 'get', array(
3266 'entity_id' => $contribution['id'],
3267 'entity_table' => 'civicrm_contribution',
3268 'api.line_item.create' => array(
3269 'entity_id' => $participantID,
3270 'entity_table' => 'civicrm_participant',
3273 return $contribution['id'];
3277 * Get financial transaction amount.
3279 * @param int $contId
3281 * @return null|string
3283 public function _getFinancialTrxnAmount($contId) {
3285 SUM( ft.total_amount ) AS total
3286 FROM civicrm_financial_trxn AS ft
3287 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3288 WHERE ceft.entity_table = 'civicrm_contribution'
3289 AND ceft.entity_id = {$contId}";
3291 $result = CRM_Core_DAO
::singleValueQuery($query);
3296 * @param int $contId
3298 * @return null|string
3300 public function _getFinancialItemAmount($contId) {
3301 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3304 FROM civicrm_financial_item
3305 WHERE entity_table = 'civicrm_line_item'
3306 AND entity_id = {$lineItem}";
3307 $result = CRM_Core_DAO
::singleValueQuery($query);
3312 * @param int $contId
3315 public function _checkFinancialItem($contId, $context) {
3316 if ($context != 'paylater') {
3318 'entity_id' => $contId,
3319 'entity_table' => 'civicrm_contribution',
3321 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3322 $entityParams = array(
3323 'financial_trxn_id' => $trxn['financial_trxn_id'],
3324 'entity_table' => 'civicrm_financial_item',
3326 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3328 'id' => $entityTrxn['entity_id'],
3331 if ($context == 'paylater') {
3332 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3333 foreach ($lineItems as $key => $item) {
3335 'entity_id' => $key,
3336 'entity_table' => 'civicrm_line_item',
3338 $compareParams = array('status_id' => 1);
3339 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3342 elseif ($context == 'refund') {
3343 $compareParams = array(
3345 'financial_account_id' => 1,
3349 elseif ($context == 'cancelPending') {
3350 $compareParams = array(
3352 'financial_account_id' => 1,
3356 elseif ($context == 'changeFinancial') {
3357 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3359 'entity_id' => $lineKey,
3362 $compareParams = array(
3363 'financial_account_id' => 1,
3365 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3367 'financial_account_id' => 3,
3368 'entity_id' => $lineKey,
3370 $compareParams = array(
3374 if ($context != 'paylater') {
3375 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3380 * Check correct financial transaction entries were created for the change in payment instrument.
3382 * @param int $contributionID
3383 * @param int $originalInstrumentID
3384 * @param int $newInstrumentID
3386 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3388 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3390 $originalTrxnParams = array(
3391 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3392 'payment_instrument_id' => $originalInstrumentID,
3393 'amount' => $amount,
3397 $reversalTrxnParams = array(
3398 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3399 'payment_instrument_id' => $originalInstrumentID,
3400 'amount' => -$amount,
3404 $newTrxnParams = array(
3405 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3406 'payment_instrument_id' => $newInstrumentID,
3407 'amount' => $amount,
3411 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3412 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3413 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3415 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3416 'id' => $entityFinancialTrxn['financial_trxn_id'],
3418 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3419 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3420 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3421 $this->assertEquals(0, $financialTrxn['fee_amount']);
3422 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3423 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3426 $this->assertEquals(1, $financialTrxn['is_payment']);
3427 $this->assertEquals('USD', $financialTrxn['currency']);
3428 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3433 * Check financial transaction.
3435 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3437 * @param array $contribution
3438 * @param string $context
3439 * @param int $instrumentId
3440 * @param array $extraParams
3442 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3443 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3444 $trxn = array_pop($financialTrxns);
3447 'id' => $trxn['financial_trxn_id'],
3449 if ($context == 'payLater') {
3450 $compareParams = array(
3452 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3455 elseif ($context == 'refund') {
3456 $compareParams = array(
3457 'to_financial_account_id' => 6,
3458 'total_amount' => -100,
3460 'trxn_date' => '2015-01-01 09:00:00',
3461 'trxn_id' => 'the refund',
3464 elseif ($context == 'cancelPending') {
3465 $compareParams = array(
3466 'to_financial_account_id' => 7,
3467 'total_amount' => -100,
3471 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3472 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3473 // It does the same thing with greater readability.
3474 // @todo remove handling for
3476 $entityParams = array(
3477 'entity_id' => $contribution['id'],
3478 'entity_table' => 'civicrm_contribution',
3481 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3482 $trxnParams1 = array(
3483 'id' => $trxn['financial_trxn_id'],
3485 if (empty($extraParams)) {
3486 $compareParams = array(
3487 'total_amount' => -100,
3492 $compareParams = array(
3493 'total_amount' => 100,
3497 if ($context == 'paymentInstrument') {
3498 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3499 $compareParams['payment_instrument_id'] = $instrumentId;
3502 $compareParams['to_financial_account_id'] = 12;
3504 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3505 $compareParams['total_amount'] = 100;
3508 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3514 public function _addPaymentInstrument() {
3515 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3516 $optionParams = array(
3517 'option_group_id' => $gId,
3518 'label' => 'Test Card',
3519 'name' => 'Test Card',
3524 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3525 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3526 $financialParams = array(
3527 'entity_table' => 'civicrm_option_value',
3528 'entity_id' => $optionValue['id'],
3529 'account_relationship' => $relationTypeId,
3530 'financial_account_id' => 7,
3532 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3533 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3534 return $optionValue['values'][$optionValue['id']]['value'];
3537 public function _deletedAddedPaymentInstrument() {
3538 $result = $this->callAPISuccess('OptionValue', 'get', array(
3539 'option_group_id' => 'payment_instrument',
3540 'name' => 'Test Card',
3544 if ($id = CRM_Utils_Array
::value('id', $result)) {
3545 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3550 * Set up the basic recurring contribution for tests.
3552 * @param array $generalParams
3553 * Parameters that can be merged into the recurring AND the contribution.
3555 * @param array $recurParams
3556 * Parameters to merge into the recur only.
3560 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3561 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3562 'contact_id' => $this->_individualId
,
3563 'installments' => '12',
3564 'frequency_interval' => '1',
3566 'contribution_status_id' => 1,
3567 'start_date' => '2012-01-01 00:00:00',
3568 'currency' => 'USD',
3569 'frequency_unit' => 'month',
3570 'payment_processor_id' => $this->paymentProcessorID
,
3571 ), $generalParams, $recurParams));
3572 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3575 'contribution_recur_id' => $contributionRecur['id'],
3578 return $originalContribution;
3582 * Set up a basic auto-renew membership for tests.
3584 * @param array $generalParams
3585 * Parameters that can be merged into the recurring AND the contribution.
3587 * @param array $recurParams
3588 * Parameters to merge into the recur only.
3592 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3593 $newContact = $this->callAPISuccess('Contact', 'create', array(
3594 'contact_type' => 'Individual',
3595 'sort_name' => 'McTesterson, Testy',
3596 'display_name' => 'Testy McTesterson',
3597 'preferred_language' => 'en_US',
3598 'preferred_mail_format' => 'Both',
3599 'first_name' => 'Testy',
3600 'last_name' => 'McTesterson',
3601 'contact_is_deleted' => '0',
3603 'email' => 'tmctesterson@example.com',
3606 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3607 'domain_id' => "Default Domain Name",
3608 'member_of_contact_id' => 1,
3609 'financial_type_id' => "Member Dues",
3610 'duration_unit' => "month",
3611 'duration_interval' => 1,
3612 'period_type' => 'rolling',
3613 'name' => "Standard Member",
3614 'minimum_fee' => 100,
3616 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3617 'contact_id' => $newContact['id'],
3618 'installments' => '12',
3619 'frequency_interval' => '1',
3621 'contribution_status_id' => 1,
3622 'start_date' => '2012-01-01 00:00:00',
3623 'currency' => 'USD',
3624 'frequency_unit' => 'month',
3625 'payment_processor_id' => $this->paymentProcessorID
,
3626 ), $generalParams, $recurParams));
3628 $membership = $this->callAPISuccess('membership', 'create', array(
3629 'contact_id' => $newContact['id'],
3630 'contribution_recur_id' => $contributionRecur['id'],
3631 'financial_type_id' => "Member Dues",
3632 'membership_type_id' => $membershipType['id'],
3634 'skipLineItem' => TRUE,
3637 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3638 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3641 'contact_id' => $newContact['id'],
3642 'contribution_recur_id' => $contributionRecur['id'],
3643 'financial_type_id' => "Member Dues",
3644 'contribution_status_id' => 1,
3645 'invoice_id' => uniqid(),
3648 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3649 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3650 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3651 $this->callAPISuccess('LineItem', 'getsingle', array());
3652 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3654 return array($originalContribution, $membership);
3657 * Set up a repeat transaction.
3659 * @param array $recurParams
3663 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3664 $paymentProcessorID = $this->paymentProcessorCreate();
3665 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3666 'contact_id' => $this->_individualId
,
3667 'installments' => '12',
3668 'frequency_interval' => '1',
3670 'contribution_status_id' => 1,
3671 'start_date' => '2012-01-01 00:00:00',
3672 'currency' => 'USD',
3673 'frequency_unit' => 'month',
3674 'payment_processor_id' => $paymentProcessorID,
3677 $originalContribution = '';
3678 if ($flag == 'multiple') {
3679 // CRM-19309 create a contribution + also add in line_items (plural):
3680 $params = array_merge($this->_params
, $contributionParams);
3681 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3684 'contribution_recur_id' => $contributionRecur['id'],
3685 'skipLineItem' => 1,
3686 'api.line_item.create' => array(
3688 'price_field_id' => 1,
3690 'line_total' => '20',
3691 'unit_price' => '10',
3692 'financial_type_id' => 1,
3695 'price_field_id' => 1,
3697 'line_total' => '80',
3698 'unit_price' => '80',
3699 'financial_type_id' => 2,
3706 elseif ($flag == 'single') {
3707 $params = array_merge($this->_params
, array('contribution_recur_id' => $contributionRecur['id']));
3708 $params = array_merge($params, $contributionParams);
3709 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3711 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3712 return $originalContribution;
3716 * Common set up routine.
3720 protected function setUpForCompleteTransaction() {
3721 $this->mut
= new CiviMailUtils($this, TRUE);
3722 $this->createLoggedInUser();
3723 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3724 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3725 return $contribution;
3729 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3731 public function testRepeatTransactionWithNonCreditCardDefault() {
3732 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3733 'contact_id' => $this->_individualId
,
3734 'installments' => '12',
3735 'frequency_interval' => '1',
3737 'contribution_status_id' => 1,
3738 'start_date' => '2012-01-01 00:00:00',
3739 'currency' => 'USD',
3740 'frequency_unit' => 'month',
3741 'payment_processor_id' => $this->paymentProcessorID
,
3743 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3745 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3747 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
3748 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3749 'contribution_status_id' => 'Completed',
3750 'trxn_id' => uniqid(),
3751 'original_contribution_id' => $contribution1,
3753 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3754 $this->quickCleanUpFinancialEntities();
3758 * CRM-20008 Tests repeattransaction creates pending membership.
3760 public function testRepeatTransactionPendingMembership() {
3761 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3762 $this->callAPISuccess('membership', 'create', array(
3763 'id' => $membership['id'],
3764 'end_date' => 'yesterday',
3765 'status_id' => 'Expired',
3767 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3768 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3769 'contribution_status_id' => 'Pending',
3770 'trxn_id' => uniqid(),
3772 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3773 'id' => $membership['id'],
3774 'return' => 'status_id',
3777 // Let's see if the membership payments got created while we're at it.
3778 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3779 'memberhip_id' => $membership['id'],
3781 $this->assertEquals(2, $membershipPayments['count']);
3783 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3784 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3785 $membership = $this->callAPISuccessGetSingle('membership', array(
3786 'id' => $membership['id'],
3787 'return' => 'status_id, end_date',
3789 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3790 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3792 $this->quickCleanUpFinancialEntities();
3793 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3797 * Test sending a mail via the API.
3799 public function testSendMailWithAPISetFromDetails() {
3800 $mut = new CiviMailUtils($this, TRUE);
3801 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3802 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3803 'id' => $contribution['id'],
3804 'receipt_from_email' => 'api@civicrm.org',
3805 'receipt_from_name' => 'CiviCRM LLC',
3807 $mut->checkMailLog(array(
3808 'From: CiviCRM LLC <api@civicrm.org>',
3809 'Contribution Information',
3810 'Please print this confirmation for your records',
3819 * Test sending a mail via the API.
3821 public function testSendMailWithNoFromSetFallToDomain() {
3822 $this->createLoggedInUser();
3823 $mut = new CiviMailUtils($this, TRUE);
3824 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3825 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3826 'id' => $contribution['id'],
3828 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3829 $mut->checkMailLog(array(
3830 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3831 'Contribution Information',
3832 'Please print this confirmation for your records',
3841 * Test sending a mail via the API.
3843 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3844 $this->createLoggedInUser();
3845 $mut = new CiviMailUtils($this, TRUE);
3846 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3847 $this->callAPISuccess('contribution', 'repeattransaction', array(
3848 'contribution_status_id' => 'Completed',
3849 'trxn_id' => uniqid(),
3850 'original_contribution_id' => $contribution,
3852 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3853 $mut->checkMailLog(array(
3854 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3855 'Contribution Information',
3856 'Please print this confirmation for your records',
3865 * Test sending a mail via the API.
3867 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3868 $mut = new CiviMailUtils($this, TRUE);
3869 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3870 $paymentProcessorID = $this->paymentProcessorCreate();
3871 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3872 'contact_id' => $this->_individualId
,
3873 'installments' => '12',
3874 'frequency_interval' => '1',
3876 'contribution_status_id' => 1,
3877 'start_date' => '2012-01-01 00:00:00',
3878 'currency' => 'USD',
3879 'frequency_unit' => 'month',
3880 'payment_processor_id' => $paymentProcessorID,
3882 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3885 'contribution_recur_id' => $contributionRecur['id'],
3886 'contribution_page_id' => $contributionPage['id']))
3888 $this->callAPISuccess('contribution', 'repeattransaction', array(
3889 'contribution_status_id' => 'Completed',
3890 'trxn_id' => uniqid(),
3891 'original_contribution_id' => $originalContribution,
3894 $mut->checkMailLog(array(
3895 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3896 'Contribution Information',
3897 'Please print this confirmation for your records',
3906 * Test sending a mail via the API.
3908 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
3909 $mut = new CiviMailUtils($this, TRUE);
3910 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
3911 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
3912 foreach ($fromEmail['values'] as $from) {
3913 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
3915 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
3916 $this->callAPISuccess('contribution', 'repeattransaction', array(
3917 'contribution_status_id' => 'Completed',
3918 'trxn_id' => uniqid(),
3919 'original_contribution_id' => $originalContribution,
3922 $mut->checkMailLog(array(
3923 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
3924 'Contribution Information',
3925 'Please print this confirmation for your records',
3934 * Create a Contribution Page with is_email_receipt = TRUE.
3936 * @param array $params
3937 * Params to overwrite with.
3941 protected function createReceiptableContributionPage($params = array()) {
3942 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
3943 'receipt_from_name' => 'Mickey Mouse',
3944 'receipt_from_email' => 'mickey@mouse.com',
3945 'title' => "Test Contribution Page",
3946 'financial_type_id' => 1,
3947 'currency' => 'CAD',
3948 'is_monetary' => TRUE,
3949 'is_email_receipt' => TRUE,
3951 return $contributionPage;
3955 * function to test card_type and pan truncation.
3957 public function testCardTypeAndPanTruncation() {
3958 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
3959 $contactId = $this->individualCreate();
3961 'contact_id' => $contactId,
3962 'receive_date' => '2016-01-20',
3963 'total_amount' => 100,
3964 'financial_type_id' => 1,
3965 'payment_instrument' => 'Credit Card',
3966 'card_type_id' => $creditCardTypeIDs['Visa'],
3967 'pan_truncation' => 4567,
3969 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3970 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
3971 $financialTrxn = $this->callAPISuccessGetSingle(
3974 'id' => $lastFinancialTrxnId['financialTrxnId'],
3975 'return' => array('card_type_id', 'pan_truncation'),
3978 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
3979 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
3981 'id' => $contribution['id'],
3982 'pan_truncation' => 2345,
3983 'card_type_id' => $creditCardTypeIDs['Amex'],
3985 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3986 $financialTrxn = $this->callAPISuccessGetSingle(
3989 'id' => $lastFinancialTrxnId['financialTrxnId'],
3990 'return' => array('card_type_id', 'pan_truncation'),
3993 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
3994 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
3998 * Test repeat contribution uses non default currency
3999 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4001 public function testRepeatTransactionWithDifferenceCurrency() {
4002 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4003 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4004 'original_contribution_id' => $originalContribution['id'],
4005 'contribution_status_id' => 'Completed',
4006 'trxn_id' => uniqid(),
4008 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4012 * Get the financial items for the contribution.
4014 * @param int $contributionID
4017 * Array of associated financial items.
4019 protected function getFinancialTransactionsForContribution($contributionID) {
4020 $trxnParams = array(
4021 'entity_id' => $contributionID,
4022 'entity_table' => 'civicrm_contribution',
4024 // @todo the following function has naming errors & has a weird signature & appears to
4025 // only be called from test classes. Move into test suite & maybe just use api
4026 // from this function.
4027 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));