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 testCreateContributionNullOutThankyouDate() {
689 $params = $this->_params
;
690 $params['thankyou_date'] = 'yesterday';
692 $contribution = $this->callAPISuccess('contribution', 'create', $params);
693 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
694 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
696 $params['thankyou_date'] = 'null';
697 $contribution = $this->callAPISuccess('contribution', 'create', $params);
698 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
702 * Create test with unique field name on source.
704 public function testCreateContributionSourceInvalidContact() {
708 'receive_date' => date('Ymd'),
709 'total_amount' => 100.00,
710 'financial_type_id' => $this->_financialTypeId
,
711 'payment_instrument_id' => 1,
712 'non_deductible_amount' => 10.00,
713 'fee_amount' => 50.00,
714 'net_amount' => 90.00,
716 'invoice_id' => 67890,
717 'contribution_source' => 'SSF',
718 'contribution_status_id' => 1,
721 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
724 public function testCreateContributionSourceInvalidContContact() {
727 'contribution_contact_id' => 999,
728 'receive_date' => date('Ymd'),
729 'total_amount' => 100.00,
730 'financial_type_id' => $this->_financialTypeId
,
731 'payment_instrument_id' => 1,
732 'non_deductible_amount' => 10.00,
733 'fee_amount' => 50.00,
734 'net_amount' => 90.00,
736 'invoice_id' => 67890,
737 'contribution_source' => 'SSF',
738 'contribution_status_id' => 1,
741 $this->callAPIFailure('contribution', 'create', $params);
745 * Test note created correctly.
747 public function testCreateContributionWithNote() {
748 $description = "Demonstrates creating contribution with Note Entity.";
749 $subfile = "ContributionCreateWithNote";
751 'contact_id' => $this->_individualId
,
752 'receive_date' => '2012-01-01',
753 'total_amount' => 100.00,
754 'financial_type_id' => $this->_financialTypeId
,
755 'payment_instrument_id' => 1,
756 'non_deductible_amount' => 10.00,
757 'fee_amount' => 50.00,
758 'net_amount' => 90.00,
760 'invoice_id' => 67890,
762 'contribution_status_id' => 1,
763 'note' => 'my contribution note',
766 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
767 $result = $this->callAPISuccess('note', 'get', array(
768 'entity_table' => 'civicrm_contribution',
769 'entity_id' => $contribution['id'],
772 $this->assertEquals('my contribution note', $result['values'][0]['note']);
773 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
776 public function testCreateContributionWithNoteUniqueNameAliases() {
778 'contact_id' => $this->_individualId
,
779 'receive_date' => '2012-01-01',
780 'total_amount' => 100.00,
781 'financial_type_id' => $this->_financialTypeId
,
782 'payment_instrument_id' => 1,
783 'non_deductible_amount' => 10.00,
784 'fee_amount' => 50.00,
785 'net_amount' => 90.00,
787 'invoice_id' => 67890,
789 'contribution_status_id' => 1,
790 'contribution_note' => 'my contribution note',
793 $contribution = $this->callAPISuccess('contribution', 'create', $params);
794 $result = $this->callAPISuccess('note', 'get', array(
795 'entity_table' => 'civicrm_contribution',
796 'entity_id' => $contribution['id'],
799 $this->assertEquals('my contribution note', $result['values'][0]['note']);
800 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
804 * This is the test for creating soft credits.
806 public function testCreateContributionWithSoftCredit() {
807 $description = "Demonstrates creating contribution with SoftCredit.";
808 $subfile = "ContributionCreateWithSoftCredit";
809 $contact2 = $this->callAPISuccess('Contact', 'create', array(
810 'display_name' => 'superman',
811 'contact_type' => 'Individual',
814 'contact_id' => $contact2['id'],
816 'soft_credit_type_id' => 3,
819 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
820 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
821 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
823 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
824 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
825 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
827 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
828 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
831 public function testCreateContributionWithSoftCreditDefaults() {
832 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
833 $subfile = "ContributionCreateWithSoftCreditDefaults";
834 $contact2 = $this->callAPISuccess('Contact', 'create', array(
835 'display_name' => 'superman',
836 'contact_type' => 'Individual',
838 $params = $this->_params +
array(
839 'soft_credit_to' => $contact2['id'],
841 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
842 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
844 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
845 // Default soft credit amount = contribution.total_amount
846 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
847 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
849 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
850 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
854 * Test creating contribution with Soft Credit by passing in honor_contact_id.
856 public function testCreateContributionWithHonoreeContact() {
857 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
858 $subfile = "ContributionCreateWithHonoreeContact";
859 $contact2 = $this->callAPISuccess('Contact', 'create', array(
860 'display_name' => 'superman',
861 'contact_type' => 'Individual',
863 $params = $this->_params +
array(
864 'honor_contact_id' => $contact2['id'],
866 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
867 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
869 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
870 // Default soft credit amount = contribution.total_amount
871 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
872 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
873 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
874 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
876 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
877 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
881 * Test using example code.
883 public function testContributionCreateExample() {
884 //make sure at least on page exists since there is a truncate in tear down
885 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
886 require_once 'api/v3/examples/Contribution/Create.php';
887 $result = contribution_create_example();
889 $expectedResult = contribution_create_expectedresult();
890 $this->checkArrayEquals($expectedResult, $result);
891 $this->contributionDelete($id);
895 * Function tests that additional financial records are created when fee amount is recorded.
897 public function testCreateContributionWithFee() {
899 'contact_id' => $this->_individualId
,
900 'receive_date' => '20120511',
901 'total_amount' => 100.00,
903 'financial_type_id' => 1,
905 'invoice_id' => 67890,
907 'contribution_status_id' => 1,
910 $contribution = $this->callAPISuccess('contribution', 'create', $params);
911 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
912 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
913 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
914 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
915 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
916 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
917 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
918 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
919 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
921 $lineItems = $this->callAPISuccess('line_item', 'get', array(
923 'entity_id' => $contribution['id'],
924 'entity_table' => 'civicrm_contribution',
927 $this->assertEquals(1, $lineItems['count']);
928 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
929 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
930 $lineItems = $this->callAPISuccess('line_item', 'get', array(
932 'entity_id' => $contribution['id'],
933 'contribution_id' => $contribution['id'],
934 'entity_table' => 'civicrm_contribution',
937 $this->assertEquals(1, $lineItems['count']);
938 $this->_checkFinancialRecords($contribution, 'feeAmount');
943 * Function tests that additional financial records are created when online contribution is created.
945 public function testCreateContributionOnline() {
946 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
947 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
948 $this->assertAPISuccess($contributionPage);
950 'contact_id' => $this->_individualId
,
951 'receive_date' => '20120511',
952 'total_amount' => 100.00,
953 'financial_type_id' => 1,
954 'contribution_page_id' => $contributionPage['id'],
955 'payment_processor' => $this->paymentProcessorID
,
957 'invoice_id' => 67890,
959 'contribution_status_id' => 1,
963 $contribution = $this->callAPISuccess('contribution', 'create', $params);
964 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
965 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
966 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
967 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
968 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
969 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
970 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
971 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
972 'id' => $this->paymentProcessorID
,
973 'return' => 'payment_instrument_id',
975 $this->_checkFinancialRecords($contribution, 'online');
979 * Check handling of financial type.
981 * In the interests of removing financial type / contribution type checks from
982 * legacy format function lets test that the api is doing this for us
984 public function testCreateInvalidFinancialType() {
985 $params = $this->_params
;
986 $params['financial_type_id'] = 99999;
987 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
991 * Check handling of financial type.
993 * In the interests of removing financial type / contribution type checks from
994 * legacy format function lets test that the api is doing this for us
996 public function testValidNamedFinancialType() {
997 $params = $this->_params
;
998 $params['financial_type_id'] = 'Donation';
999 $this->callAPISuccess($this->_entity
, 'create', $params);
1003 * Tests that additional financial records are created.
1005 * Checks when online contribution with pay later option is created
1007 public function testCreateContributionPayLaterOnline() {
1008 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1009 $this->_pageParams
['is_pay_later'] = 1;
1010 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1011 $this->assertAPISuccess($contributionPage);
1013 'contact_id' => $this->_individualId
,
1014 'receive_date' => '20120511',
1015 'total_amount' => 100.00,
1016 'financial_type_id' => 1,
1017 'contribution_page_id' => $contributionPage['id'],
1019 'is_pay_later' => 1,
1020 'invoice_id' => 67890,
1022 'contribution_status_id' => 2,
1026 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1027 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1028 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1029 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1030 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1031 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1032 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1033 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1034 $this->_checkFinancialRecords($contribution, 'payLater');
1038 * Function tests that additional financial records are created for online contribution with pending option.
1040 public function testCreateContributionPendingOnline() {
1041 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1042 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1043 $this->assertAPISuccess($contributionPage);
1045 'contact_id' => $this->_individualId
,
1046 'receive_date' => '20120511',
1047 'total_amount' => 100.00,
1048 'financial_type_id' => 1,
1049 'contribution_page_id' => $contributionPage['id'],
1051 'invoice_id' => 67890,
1053 'contribution_status_id' => 2,
1056 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1057 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1058 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1059 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1060 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1061 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1062 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1063 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1064 $this->_checkFinancialRecords($contribution, 'pending');
1068 * Test that BAO defaults work.
1070 public function testCreateBAODefaults() {
1071 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1072 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1073 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1074 'id' => $contribution['id'],
1075 'api.contribution.delete' => 1,
1077 $this->assertEquals(1, $contribution['contribution_status_id']);
1078 $this->assertEquals('Check', $contribution['payment_instrument']);
1082 * Function tests that line items, financial records are updated when contribution amount is changed.
1084 public function testCreateUpdateContributionChangeTotal() {
1085 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1086 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1088 'entity_id' => $contribution['id'],
1089 'entity_table' => 'civicrm_contribution',
1091 'return' => 'line_total',
1093 $this->assertEquals('100.00', $lineItems);
1094 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1095 // Financial trxn SUM = 100 + 5 (fee)
1096 $this->assertEquals('105.00', $trxnAmount);
1099 'id' => $contribution['id'],
1100 'total_amount' => '125',
1102 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1104 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1106 'entity_id' => $contribution['id'],
1107 'entity_table' => 'civicrm_contribution',
1109 'return' => 'line_total',
1112 $this->assertEquals('125.00', $lineItems);
1113 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1115 // Financial trxn SUM = 125 + 5 (fee).
1116 $this->assertEquals('130.00', $trxnAmount);
1117 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1121 * Function tests that line items, financial records are updated when pay later contribution is received.
1123 public function testCreateUpdateContributionPayLater() {
1124 $contribParams = array(
1125 'contact_id' => $this->_individualId
,
1126 'receive_date' => '2012-01-01',
1127 'total_amount' => 100.00,
1128 'financial_type_id' => $this->_financialTypeId
,
1129 'payment_instrument_id' => 1,
1130 'contribution_status_id' => 2,
1131 'is_pay_later' => 1,
1134 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1136 $newParams = array_merge($contribParams, array(
1137 'id' => $contribution['id'],
1138 'contribution_status_id' => 1,
1141 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1142 $contribution = $contribution['values'][$contribution['id']];
1143 $this->assertEquals($contribution['contribution_status_id'], '1');
1144 $this->_checkFinancialItem($contribution['id'], 'paylater');
1145 $this->_checkFinancialTrxn($contribution, 'payLater');
1149 * Function tests that financial records are updated when Payment Instrument is changed.
1151 public function testCreateUpdateContributionPaymentInstrument() {
1152 $instrumentId = $this->_addPaymentInstrument();
1153 $contribParams = array(
1154 'contact_id' => $this->_individualId
,
1155 'total_amount' => 100.00,
1156 'financial_type_id' => $this->_financialTypeId
,
1157 'payment_instrument_id' => 4,
1158 'contribution_status_id' => 1,
1161 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1163 $newParams = array_merge($contribParams, array(
1164 'id' => $contribution['id'],
1165 'payment_instrument_id' => $instrumentId,
1168 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1169 $this->assertAPISuccess($contribution);
1170 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1172 // cleanup - delete created payment instrument
1173 $this->_deletedAddedPaymentInstrument();
1177 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1179 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1180 $instrumentId = $this->_addPaymentInstrument();
1181 $contribParams = array(
1182 'contact_id' => $this->_individualId
,
1183 'total_amount' => -100.00,
1184 'financial_type_id' => $this->_financialTypeId
,
1185 'payment_instrument_id' => 4,
1186 'contribution_status_id' => 1,
1189 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1191 $newParams = array_merge($contribParams, array(
1192 'id' => $contribution['id'],
1193 'payment_instrument_id' => $instrumentId,
1196 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1197 $this->assertAPISuccess($contribution);
1198 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1200 // cleanup - delete created payment instrument
1201 $this->_deletedAddedPaymentInstrument();
1205 * Function tests that financial records are added when Contribution is Refunded.
1207 public function testCreateUpdateContributionRefund() {
1208 $contributionParams = array(
1209 'contact_id' => $this->_individualId
,
1210 'receive_date' => '2012-01-01',
1211 'total_amount' => 100.00,
1212 'financial_type_id' => $this->_financialTypeId
,
1213 'payment_instrument_id' => 4,
1214 'contribution_status_id' => 1,
1215 'trxn_id' => 'original_payment',
1217 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1218 $newParams = array_merge($contributionParams, array(
1219 'id' => $contribution['id'],
1220 'contribution_status_id' => 'Refunded',
1221 'cancel_date' => '2015-01-01 09:00',
1222 'refund_trxn_id' => 'the refund',
1226 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1227 $this->_checkFinancialTrxn($contribution, 'refund');
1228 $this->_checkFinancialItem($contribution['id'], 'refund');
1229 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1230 'id' => $contribution['id'],
1231 'return' => 'trxn_id',
1236 * Refund a contribution for a financial type with a contra account.
1238 * CRM-17951 the contra account is a financial account with a relationship to a
1239 * financial type. It is not always configured but should be reflected
1240 * in the financial_trxn & financial_item table if it is.
1242 public function testCreateUpdateChargebackContributionDefaultAccount() {
1243 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1244 $this->callAPISuccess('Contribution', 'create', array(
1245 'id' => $contribution['id'],
1246 'contribution_status_id' => 'Chargeback',
1248 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1250 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1251 'contribution_id' => $contribution['id'],
1252 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1254 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1255 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1256 'total_amount' => -100,
1257 'status_id' => 'Chargeback',
1258 'to_financial_account_id' => 6,
1263 * Refund a contribution for a financial type with a contra account.
1265 * CRM-17951 the contra account is a financial account with a relationship to a
1266 * financial type. It is not always configured but should be reflected
1267 * in the financial_trxn & financial_item table if it is.
1269 public function testCreateUpdateChargebackContributionCustomAccount() {
1270 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1271 'name' => 'Chargeback Account',
1272 'is_active' => TRUE,
1275 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1276 'entity_id' => $this->_financialTypeId
,
1277 'entity_table' => 'civicrm_financial_type',
1278 'account_relationship' => 'Chargeback Account is',
1279 'financial_account_id' => 'Chargeback Account',
1282 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1283 $this->callAPISuccess('Contribution', 'create', array(
1284 'id' => $contribution['id'],
1285 'contribution_status_id' => 'Chargeback',
1287 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1289 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1290 'contribution_id' => $contribution['id'],
1291 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1293 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1295 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1296 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1297 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1301 * Refund a contribution for a financial type with a contra account.
1303 * CRM-17951 the contra account is a financial account with a relationship to a
1304 * financial type. It is not always configured but should be reflected
1305 * in the financial_trxn & financial_item table if it is.
1307 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1308 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1309 'name' => 'Refund Account',
1310 'is_active' => TRUE,
1313 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1314 'entity_id' => $this->_financialTypeId
,
1315 'entity_table' => 'civicrm_financial_type',
1316 'account_relationship' => 'Credit/Contra Revenue Account is',
1317 'financial_account_id' => 'Refund Account',
1320 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1321 $this->callAPISuccess('Contribution', 'create', array(
1322 'id' => $contribution['id'],
1323 'contribution_status_id' => 'Refunded',
1326 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1327 'contribution_id' => $contribution['id'],
1328 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1330 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1332 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1333 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1334 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1338 * Function tests that trxn_id is set when passed in.
1340 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1341 * when trxn_id is passed in.
1343 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1344 $contributionParams = array(
1345 'contact_id' => $this->_individualId
,
1346 'receive_date' => '2012-01-01',
1347 'total_amount' => 100.00,
1348 'financial_type_id' => $this->_financialTypeId
,
1349 'payment_instrument_id' => 4,
1350 'contribution_status_id' => 1,
1351 'trxn_id' => 'original_payment',
1353 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1354 $newParams = array_merge($contributionParams, array(
1355 'id' => $contribution['id'],
1356 'contribution_status_id' => 'Refunded',
1357 'cancel_date' => '2015-01-01 09:00',
1358 'trxn_id' => 'the refund',
1362 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1363 $this->_checkFinancialTrxn($contribution, 'refund');
1364 $this->_checkFinancialItem($contribution['id'], 'refund');
1365 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1366 'id' => $contribution['id'],
1367 'return' => 'trxn_id',
1372 * Function tests that trxn_id is set when passed in.
1374 * Here we ensure that the civicrm_contribution.trxn_id is set
1375 * when trxn_id is passed in but if refund_trxn_id is different then that
1376 * is kept for the refund transaction.
1378 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1379 $contributionParams = array(
1380 'contact_id' => $this->_individualId
,
1381 'receive_date' => '2012-01-01',
1382 'total_amount' => 100.00,
1383 'financial_type_id' => $this->_financialTypeId
,
1384 'payment_instrument_id' => 4,
1385 'contribution_status_id' => 1,
1386 'trxn_id' => 'original_payment',
1388 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1389 $newParams = array_merge($contributionParams, array(
1390 'id' => $contribution['id'],
1391 'contribution_status_id' => 'Refunded',
1392 'cancel_date' => '2015-01-01 09:00',
1393 'trxn_id' => 'cont id',
1394 'refund_trxn_id' => 'the refund',
1398 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1399 $this->_checkFinancialTrxn($contribution, 'refund');
1400 $this->_checkFinancialItem($contribution['id'], 'refund');
1401 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1402 'id' => $contribution['id'],
1403 'return' => 'trxn_id',
1408 * Function tests that refund_trxn_id is set when passed in empty.
1410 * Here we ensure that the civicrm_contribution.trxn_id is set
1411 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1412 * is kept for the refund transaction.
1414 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1415 $contributionParams = array(
1416 'contact_id' => $this->_individualId
,
1417 'receive_date' => '2012-01-01',
1418 'total_amount' => 100.00,
1419 'financial_type_id' => $this->_financialTypeId
,
1420 'payment_instrument_id' => 4,
1421 'contribution_status_id' => 1,
1422 'trxn_id' => 'original_payment',
1424 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1425 $newParams = array_merge($contributionParams, array(
1426 'id' => $contribution['id'],
1427 'contribution_status_id' => 'Refunded',
1428 'cancel_date' => '2015-01-01 09:00',
1429 'trxn_id' => 'cont id',
1430 'refund_trxn_id' => '',
1434 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1435 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1436 $this->_checkFinancialItem($contribution['id'], 'refund');
1437 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1438 'id' => $contribution['id'],
1439 'return' => 'trxn_id',
1444 * Function tests invalid contribution status change.
1446 public function testCreateUpdateContributionInValidStatusChange() {
1447 $contribParams = array(
1449 'receive_date' => '2012-01-01',
1450 'total_amount' => 100.00,
1451 'financial_type_id' => 1,
1452 'payment_instrument_id' => 1,
1453 'contribution_status_id' => 1,
1455 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1456 $newParams = array_merge($contribParams, array(
1457 'id' => $contribution['id'],
1458 'contribution_status_id' => 2,
1461 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1466 * Function tests that financial records are added when Pending Contribution is Canceled.
1468 public function testCreateUpdateContributionCancelPending() {
1469 $contribParams = array(
1470 'contact_id' => $this->_individualId
,
1471 'receive_date' => '2012-01-01',
1472 'total_amount' => 100.00,
1473 'financial_type_id' => $this->_financialTypeId
,
1474 'payment_instrument_id' => 1,
1475 'contribution_status_id' => 2,
1476 'is_pay_later' => 1,
1479 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1480 $newParams = array_merge($contribParams, array(
1481 'id' => $contribution['id'],
1482 'contribution_status_id' => 3,
1483 'cancel_date' => '2012-02-02 09:00',
1486 //Check if trxn_date is same as cancel_date.
1487 $checkTrxnDate = array(
1488 'trxn_date' => '2012-02-02 09:00:00',
1490 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1491 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1492 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1496 * Function tests that financial records are added when Financial Type is Changed.
1498 public function testCreateUpdateContributionChangeFinancialType() {
1499 $contribParams = array(
1500 'contact_id' => $this->_individualId
,
1501 'receive_date' => '2012-01-01',
1502 'total_amount' => 100.00,
1503 'financial_type_id' => 1,
1504 'payment_instrument_id' => 1,
1505 'contribution_status_id' => 1,
1508 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1509 $newParams = array_merge($contribParams, array(
1510 'id' => $contribution['id'],
1511 'financial_type_id' => 3,
1514 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1515 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1516 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1520 * Test that update does not change status id CRM-15105.
1522 public function testCreateUpdateWithoutChangingPendingStatus() {
1523 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1524 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1525 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1526 'id' => $contribution['id'],
1527 'api.contribution.delete' => 1,
1529 $this->assertEquals(2, $contribution['contribution_status_id']);
1533 * Test Updating a Contribution.
1535 * CHANGE: we require the API to do an incremental update
1537 public function testCreateUpdateContribution() {
1538 $contributionID = $this->contributionCreate(array(
1539 'contact_id' => $this->_individualId
,
1540 'trxn_id' => 212355,
1541 'financial_type_id' => $this->_financialTypeId
,
1542 'invoice_id' => 'old_invoice',
1544 $old_params = array(
1545 'contribution_id' => $contributionID,
1547 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1548 $this->assertEquals($original['id'], $contributionID);
1549 //set up list of old params, verify
1551 //This should not be required on update:
1552 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1553 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1554 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1555 $old_source = $original['values'][$contributionID]['contribution_source'];
1557 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1558 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1560 //check against values in CiviUnitTestCase::createContribution()
1561 $this->assertEquals($old_contact_id, $this->_individualId
);
1562 $this->assertEquals($old_fee_amount, 5.00);
1563 $this->assertEquals($old_source, 'SSF');
1564 $this->assertEquals($old_trxn_id, 212355);
1565 $this->assertEquals($old_invoice_id, 'old_invoice');
1567 'id' => $contributionID,
1568 'contact_id' => $this->_individualId
,
1569 'total_amount' => 105.00,
1570 'fee_amount' => 7.00,
1571 'financial_type_id' => $this->_financialTypeId
,
1572 'non_deductible_amount' => 22.00,
1573 'contribution_status_id' => 1,
1574 'note' => 'Donating for Noble Cause',
1577 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1579 $new_params = array(
1580 'contribution_id' => $contribution['id'],
1583 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1585 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1586 $this->assertEquals($contribution['total_amount'], 105.00);
1587 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1588 $this->assertEquals($contribution['financial_type'], 'Donation');
1589 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1590 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1591 $this->assertEquals($contribution['fee_amount'], 7.00);
1592 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1593 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1594 $this->assertEquals($contribution['contribution_source'], $old_source);
1595 $this->assertEquals($contribution['contribution_status'], 'Completed');
1597 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1600 'contribution_id' => $contributionID,
1603 $result = $this->callAPISuccess('contribution', 'delete', $params);
1604 $this->assertAPISuccess($result);
1608 * Attempt (but fail) to delete a contribution without parameters.
1610 public function testDeleteEmptyParamsContribution() {
1612 $this->callAPIFailure('contribution', 'delete', $params);
1615 public function testDeleteParamsNotArrayContribution() {
1616 $params = 'contribution_id= 1';
1617 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1618 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1621 public function testDeleteWrongParamContribution() {
1623 'contribution_source' => 'SSF',
1626 $this->callAPIFailure('contribution', 'delete', $params);
1629 public function testDeleteContribution() {
1630 $contributionID = $this->contributionCreate(array(
1631 'contact_id' => $this->_individualId
,
1632 'financial_type_id' => $this->_financialTypeId
,
1635 'id' => $contributionID,
1637 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1641 * Test civicrm_contribution_search with empty params.
1643 * All available contributions expected.
1645 public function testSearchEmptyParams() {
1649 'contact_id' => $this->_individualId
,
1650 'receive_date' => date('Ymd'),
1651 'total_amount' => 100.00,
1652 'financial_type_id' => $this->_financialTypeId
,
1653 'non_deductible_amount' => 10.00,
1654 'fee_amount' => 5.00,
1655 'net_amount' => 95.00,
1657 'invoice_id' => 78910,
1659 'contribution_status_id' => 1,
1662 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1664 $result = $this->callAPISuccess('contribution', 'get', $params);
1665 // We're taking the first element.
1666 $res = $result['values'][$contribution['id']];
1668 $this->assertEquals($p['contact_id'], $res['contact_id']);
1669 $this->assertEquals($p['total_amount'], $res['total_amount']);
1670 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1671 $this->assertEquals($p['net_amount'], $res['net_amount']);
1672 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1673 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1674 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1675 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1676 $this->assertEquals($p['source'], $res['contribution_source']);
1677 // contribution_status_id = 1 => Completed
1678 $this->assertEquals('Completed', $res['contribution_status']);
1680 $this->contributionDelete($contribution['id']);
1684 * Test civicrm_contribution_search. Success expected.
1686 public function testSearch() {
1688 'contact_id' => $this->_individualId
,
1689 'receive_date' => date('Ymd'),
1690 'total_amount' => 100.00,
1691 'financial_type_id' => $this->_financialTypeId
,
1692 'non_deductible_amount' => 10.00,
1693 'contribution_status_id' => 1,
1696 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1699 'contact_id' => $this->_individualId
,
1700 'receive_date' => date('Ymd'),
1701 'total_amount' => 200.00,
1702 'financial_type_id' => $this->_financialTypeId
,
1703 'non_deductible_amount' => 20.00,
1704 'trxn_id' => 5454565,
1705 'invoice_id' => 1212124,
1706 'fee_amount' => 50.00,
1707 'net_amount' => 60.00,
1708 'contribution_status_id' => 2,
1711 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1714 'contribution_id' => $contribution2['id'],
1717 $result = $this->callAPISuccess('contribution', 'get', $params);
1718 $res = $result['values'][$contribution2['id']];
1720 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1721 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1722 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1723 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1724 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1725 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1726 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1727 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1728 // contribution_status_id = 2 => Pending
1729 $this->assertEquals('Pending', $res['contribution_status']);
1731 $this->contributionDelete($contribution1['id']);
1732 $this->contributionDelete($contribution2['id']);
1736 * Test completing a transaction via the API.
1738 * Note that we are creating a logged in user because email goes out from
1741 public function testCompleteTransaction() {
1742 $mut = new CiviMailUtils($this, TRUE);
1743 $this->swapMessageTemplateForTestTemplate();
1744 $this->createLoggedInUser();
1745 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1746 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1747 $this->callAPISuccess('contribution', 'completetransaction', array(
1748 'id' => $contribution['id'],
1750 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1751 $this->assertEquals('SSF', $contribution['contribution_source']);
1752 $this->assertEquals('Completed', $contribution['contribution_status']);
1753 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1754 $mut->checkMailLog(array(
1755 'email:::anthony_anderson@civicrm.org',
1759 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1760 "receipt_date:::\n",
1761 'contributeMode:::notify',
1762 'title:::Contribution',
1763 'displayName:::Mr. Anthony Anderson II',
1764 'contributionStatus:::Completed',
1767 $this->revertTemplateToReservedTemplate();
1771 * Test completing a transaction via the API with a non-USD transaction.
1773 public function testCompleteTransactionEuro() {
1774 $mut = new CiviMailUtils($this, TRUE);
1775 $this->swapMessageTemplateForTestTemplate();
1776 $this->createLoggedInUser();
1777 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1778 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1780 $this->callAPISuccess('contribution', 'completetransaction', array(
1781 'id' => $contribution['id'],
1784 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1785 $this->assertEquals('SSF', $contribution['contribution_source']);
1786 $this->assertEquals('Completed', $contribution['contribution_status']);
1787 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1789 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1790 $entityFinancialTransaction = reset($entityFinancialTransactions);
1791 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1792 $this->assertEquals('EUR', $financialTrxn['currency']);
1794 $mut->checkMailLog(array(
1795 'email:::anthony_anderson@civicrm.org',
1799 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1800 "receipt_date:::\n",
1801 'contributeMode:::notify',
1802 'title:::Contribution',
1803 'displayName:::Mr. Anthony Anderson II',
1804 'contributionStatus:::Completed',
1807 $this->revertTemplateToReservedTemplate();
1811 * Test to ensure mail is sent on chosing pay later
1813 public function testpayLater() {
1814 $mut = new CiviMailUtils($this, TRUE);
1815 $this->swapMessageTemplateForTestTemplate();
1816 $this->createLoggedInUser();
1818 // create contribution page first
1819 $contributionPageParams = array(
1820 'title' => 'Help Support CiviCRM!',
1821 'financial_type_id' => 1,
1822 'is_monetary' => TRUE,
1823 'is_pay_later' => 1,
1824 'is_quick_config' => TRUE,
1825 'pay_later_text' => 'I will send payment by check',
1826 'pay_later_receipt' => 'This is a pay later reciept',
1827 'is_allow_other_amount' => 1,
1828 'min_amount' => 10.00,
1829 'max_amount' => 10000.00,
1830 'goal_amount' => 100000.00,
1831 'is_email_receipt' => 1,
1833 'amount_block_is_active' => 1,
1834 'currency' => 'USD',
1835 'is_billing_required' => 0,
1837 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1839 // submit form values
1840 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1842 'id' => $contributionPageResult['id'],
1843 'contact_id' => $this->_individualId
,
1844 'email-5' => 'anthony_anderson@civicrm.org',
1845 'payment_processor_id' => 0,
1848 'currencyID' => 'USD',
1849 'is_pay_later' => 1,
1850 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1851 'is_quick_config' => 1,
1852 'description' => 'Online Contribution: Help Support CiviCRM!',
1853 'price_set_id' => $priceSet['id'],
1855 $this->callAPISuccess('contribution_page', 'submit', $params);
1857 $mut->checkMailLog(array(
1859 'email:::anthony_anderson@civicrm.org',
1860 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1861 'displayName:::Mr. Anthony Anderson II',
1862 'contributionPageId:::' . $contributionPageResult['id'],
1863 'title:::' . $contributionPageParams['title'],
1864 'amount:::' . $params['amount'],
1867 $this->revertTemplateToReservedTemplate();
1871 * Test to check whether contact billing address is used when no contribution address
1873 public function testBillingAddress() {
1874 $mut = new CiviMailUtils($this, TRUE);
1875 $this->swapMessageTemplateForTestTemplate();
1876 $this->createLoggedInUser();
1878 //Scenario 1: When Contact don't have any address
1879 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1880 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1881 $this->callAPISuccess('contribution', 'completetransaction', array(
1882 'id' => $contribution['id'],
1884 $mut->checkMailLog(array(
1888 // Scenario 2: Contribution using address
1889 $address = $this->callAPISuccess('address', 'create', array(
1890 'street_address' => 'contribution billing st',
1891 'location_type_id' => 2,
1892 'contact_id' => $this->_params
['contact_id'],
1894 $params = array_merge($this->_params
, array(
1895 'contribution_status_id' => 2,
1896 'address_id' => $address['id'],
1899 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1900 $this->callAPISuccess('contribution', 'completetransaction', array(
1901 'id' => $contribution['id'],
1903 $mut->checkMailLog(array(
1904 'address:::contribution billing st',
1907 // Scenario 3: Contribution wtth no address but contact has a billing address
1908 $this->callAPISuccess('address', 'create', array(
1909 'id' => $address['id'],
1910 'street_address' => 'is billing st',
1911 'contact_id' => $this->_params
['contact_id'],
1913 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1914 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1915 $this->callAPISuccess('contribution', 'completetransaction', array(
1916 'id' => $contribution['id'],
1918 $mut->checkMailLog(array(
1919 'address:::is billing st',
1923 $this->revertTemplateToReservedTemplate();
1927 * Test completing a transaction via the API.
1929 * Note that we are creating a logged in user because email goes out from
1932 public function testCompleteTransactionFeeAmount() {
1933 $this->createLoggedInUser();
1934 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1935 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1936 $this->callAPISuccess('contribution', 'completetransaction', array(
1937 'id' => $contribution['id'],
1938 'fee_amount' => '.56',
1939 'trxn_id' => '7778888',
1941 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1942 $this->assertEquals('Completed', $contribution['contribution_status']);
1943 $this->assertEquals('7778888', $contribution['trxn_id']);
1944 $this->assertEquals('.56', $contribution['fee_amount']);
1945 $this->assertEquals('99.44', $contribution['net_amount']);
1949 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed
1951 public function testCheckTaxAmount() {
1952 $contact = $this->createLoggedInUser();
1953 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1954 'name' => 'Test taxable financial Type',
1958 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1959 'name' => 'Test Tax financial account ',
1960 'contact_id' => $contact,
1961 'financial_account_type_id' => 2,
1968 $financialTypeId = $financialType['id'];
1969 $financialAccountId = $financialAccount['id'];
1970 $financialAccountParams = array(
1971 'entity_table' => 'civicrm_financial_type',
1972 'entity_id' => $financialTypeId,
1973 'account_relationship' => 10,
1974 'financial_account_id' => $financialAccountId,
1976 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
1978 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1979 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1980 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1981 $this->callAPISuccess('contribution', 'completetransaction', array(
1982 'id' => $contribution['id'],
1983 'trxn_id' => '777788888',
1984 'fee_amount' => '6.00',
1986 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
1987 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
1988 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
1989 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
1993 * Test repeat contribution successfully creates line item.
1995 public function testRepeatTransaction() {
1996 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
1997 $this->callAPISuccess('contribution', 'repeattransaction', array(
1998 'original_contribution_id' => $originalContribution['id'],
1999 'contribution_status_id' => 'Completed',
2000 'trxn_id' => uniqid(),
2002 $lineItemParams = array(
2003 'entity_id' => $originalContribution['id'],
2011 'financial_type_id',
2012 'deductible_amount',
2013 'price_field_value_id',
2017 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2018 'entity_id' => $originalContribution['id'],
2020 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2021 'entity_id' => $originalContribution['id'] +
1,
2023 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2024 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2025 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2026 $this->_checkFinancialRecords(array(
2027 'id' => $originalContribution['id'] +
1,
2028 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2029 'id' => $originalContribution['payment_processor_id'],
2030 'return' => 'payment_instrument_id',
2033 $this->quickCleanUpFinancialEntities();
2037 * Test repeat contribution successfully creates line items (plural).
2039 public function testRepeatTransactionLineItems() {
2041 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2042 $this->callAPISuccess('contribution', 'repeattransaction', array(
2043 'original_contribution_id' => $originalContribution['id'],
2044 'contribution_status_id' => 'Completed',
2045 'trxn_id' => uniqid(),
2048 $lineItemParams = array(
2049 'entity_id' => $originalContribution['id'],
2057 'financial_type_id',
2058 'deductible_amount',
2059 'price_field_value_id',
2063 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2064 'entity_id' => $originalContribution['id'],
2066 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2067 'entity_id' => $originalContribution['id'] +
1,
2070 // unset id and entity_id for all of them to be able to compare the lineItems:
2071 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2072 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2073 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2075 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2076 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2077 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2079 // CRM-19309 so in future we also want to:
2080 // check that financial_line_items have been created for entity_id 3 and 4;
2082 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2083 $this->quickCleanUpFinancialEntities();
2087 * Test repeat contribution successfully creates is_test transaction.
2089 public function testRepeatTransactionIsTest() {
2090 $this->_params
['is_test'] = 1;
2091 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2093 $this->callAPISuccess('contribution', 'repeattransaction', array(
2094 'original_contribution_id' => $originalContribution['id'],
2095 'contribution_status_id' => 'Completed',
2096 'trxn_id' => uniqid(),
2098 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2102 * Test repeat contribution passed in status.
2104 public function testRepeatTransactionPassedInStatus() {
2105 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2107 $this->callAPISuccess('contribution', 'repeattransaction', array(
2108 'original_contribution_id' => $originalContribution['id'],
2109 'contribution_status_id' => 'Pending',
2110 'trxn_id' => uniqid(),
2112 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2116 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2118 public function testRepeatTransactionAcceptRecurID() {
2119 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2120 'contact_id' => $this->_individualId
,
2121 'installments' => '12',
2122 'frequency_interval' => '1',
2124 'contribution_status_id' => 1,
2125 'start_date' => '2012-01-01 00:00:00',
2126 'currency' => 'USD',
2127 'frequency_unit' => 'month',
2128 'payment_processor_id' => $this->paymentProcessorID
,
2130 $this->callAPISuccess('contribution', 'create', array_merge(
2132 array('contribution_recur_id' => $contributionRecur['id']))
2135 $this->callAPISuccess('contribution', 'repeattransaction', array(
2136 'contribution_recur_id' => $contributionRecur['id'],
2137 'contribution_status_id' => 'Completed',
2138 'trxn_id' => uniqid(),
2141 $this->quickCleanUpFinancialEntities();
2145 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2147 public function testRepeatTransactionTestRecurId() {
2148 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2149 'contact_id' => $this->_individualId
,
2150 'frequency_interval' => '1',
2152 'contribution_status_id' => 1,
2153 'start_date' => '2017-01-01 00:00:00',
2154 'currency' => 'USD',
2155 'frequency_unit' => 'month',
2156 'payment_processor_id' => $this->paymentProcessorID
,
2159 $this->callAPISuccess('contribution', 'create', array_merge(
2162 'contribution_recur_id' => $contributionRecur['id'],
2167 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2168 'contribution_recur_id' => $contributionRecur['id'],
2169 'contribution_status_id' => 'Completed',
2170 'trxn_id' => uniqid(),
2173 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2174 $this->quickCleanUpFinancialEntities();
2177 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2179 * ( Tests membership is renewed after repeattransaction. )
2181 public function testRepeatTransactionUsesCompleted() {
2182 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2184 $this->callAPISuccess('contribution', 'create', array(
2185 'contact_id' => $originalContribution['values'][1]['contact_id'],
2186 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2187 'total_amount' => $originalContribution['values'][1]['total_amount'],
2188 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2189 'contribution_status_id' => "Failed",
2192 $this->callAPISuccess('membership', 'create', array(
2193 'id' => $membership['id'],
2194 'end_date' => 'yesterday',
2198 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2199 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2200 'contribution_status_id' => 'Completed',
2201 'trxn_id' => 'bobsled',
2204 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2205 'id' => $membership['id'],
2206 'return' => 'status_id',
2209 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2211 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2212 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2213 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2214 $this->quickCleanUpFinancialEntities();
2215 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2219 * CRM-16397 test appropriate action if total amount has changed for single line items.
2221 public function testRepeatTransactionAlteredAmount() {
2222 $paymentProcessorID = $this->paymentProcessorCreate();
2223 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2224 'contact_id' => $this->_individualId
,
2225 'installments' => '12',
2226 'frequency_interval' => '1',
2228 'contribution_status_id' => 1,
2229 'start_date' => '2012-01-01 00:00:00',
2230 'currency' => 'USD',
2231 'frequency_unit' => 'month',
2232 'payment_processor_id' => $paymentProcessorID,
2234 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2237 'contribution_recur_id' => $contributionRecur['id'],
2241 $this->callAPISuccess('contribution', 'repeattransaction', array(
2242 'original_contribution_id' => $originalContribution['id'],
2243 'contribution_status_id' => 'Completed',
2244 'trxn_id' => uniqid(),
2245 'total_amount' => '400',
2249 $lineItemParams = array(
2250 'entity_id' => $originalContribution['id'],
2258 'financial_type_id',
2259 'deductible_amount',
2260 'price_field_value_id',
2264 $this->callAPISuccessGetSingle('contribution', array(
2265 'total_amount' => 400,
2267 'net_amount' => 350,
2269 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2270 'entity_id' => $originalContribution['id'],
2272 $expectedLineItem = array_merge(
2273 $lineItem1['values'][0], array(
2274 'line_total' => '400.00',
2275 'unit_price' => '400.00',
2279 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2280 'entity_id' => $originalContribution['id'] +
1,
2283 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2284 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2285 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2289 * CRM-17718 test appropriate action if financial type has changed for single line items.
2291 public function testRepeatTransactionPassedInFinancialType() {
2292 $originalContribution = $this->setUpRecurringContribution();
2294 $this->callAPISuccess('contribution', 'repeattransaction', array(
2295 'original_contribution_id' => $originalContribution['id'],
2296 'contribution_status_id' => 'Completed',
2297 'trxn_id' => uniqid(),
2298 'financial_type_id' => 2,
2300 $lineItemParams = array(
2301 'entity_id' => $originalContribution['id'],
2309 'financial_type_id',
2310 'deductible_amount',
2311 'price_field_value_id',
2316 $this->callAPISuccessGetSingle('contribution', array(
2317 'total_amount' => 100,
2318 'financial_type_id' => 2,
2320 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2321 'entity_id' => $originalContribution['id'],
2323 $expectedLineItem = array_merge(
2324 $lineItem1['values'][0], array(
2325 'line_total' => '100.00',
2326 'unit_price' => '100.00',
2327 'financial_type_id' => 2,
2328 'contribution_type_id' => 2,
2331 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2332 'entity_id' => $originalContribution['id'] +
1,
2334 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2335 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2336 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2340 * CRM-17718 test appropriate action if financial type has changed for single line items.
2342 public function testRepeatTransactionUpdatedFinancialType() {
2343 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2345 $this->callAPISuccess('contribution', 'repeattransaction', array(
2346 'contribution_recur_id' => $originalContribution['id'],
2347 'contribution_status_id' => 'Completed',
2348 'trxn_id' => uniqid(),
2350 $lineItemParams = array(
2351 'entity_id' => $originalContribution['id'],
2359 'financial_type_id',
2360 'deductible_amount',
2361 'price_field_value_id',
2366 $this->callAPISuccessGetSingle('contribution', array(
2367 'total_amount' => 100,
2368 'financial_type_id' => 2,
2370 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2371 'entity_id' => $originalContribution['id'],
2373 $expectedLineItem = array_merge(
2374 $lineItem1['values'][0], array(
2375 'line_total' => '100.00',
2376 'unit_price' => '100.00',
2377 'financial_type_id' => 2,
2378 'contribution_type_id' => 2,
2382 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2383 'entity_id' => $originalContribution['id'] +
1,
2385 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2386 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2387 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2391 * CRM-16397 test appropriate action if campaign has been passed in.
2393 public function testRepeatTransactionPassedInCampaign() {
2394 $paymentProcessorID = $this->paymentProcessorCreate();
2395 $campaignID = $this->campaignCreate();
2396 $campaignID2 = $this->campaignCreate();
2397 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2398 'contact_id' => $this->_individualId
,
2399 'installments' => '12',
2400 'frequency_interval' => '1',
2402 'contribution_status_id' => 1,
2403 'start_date' => '2012-01-01 00:00:00',
2404 'currency' => 'USD',
2405 'frequency_unit' => 'month',
2406 'payment_processor_id' => $paymentProcessorID,
2408 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2411 'contribution_recur_id' => $contributionRecur['id'],
2412 'campaign_id' => $campaignID,
2416 $this->callAPISuccess('contribution', 'repeattransaction', array(
2417 'original_contribution_id' => $originalContribution['id'],
2418 'contribution_status_id' => 'Completed',
2419 'trxn_id' => uniqid(),
2420 'campaign_id' => $campaignID2,
2423 $this->callAPISuccessGetSingle('contribution', array(
2424 'total_amount' => 100,
2425 'campaign_id' => $campaignID2,
2430 * CRM-17718 campaign stored on contribution recur gets priority.
2432 * This reflects the fact we permit people to update them.
2434 public function testRepeatTransactionUpdatedCampaign() {
2435 $paymentProcessorID = $this->paymentProcessorCreate();
2436 $campaignID = $this->campaignCreate();
2437 $campaignID2 = $this->campaignCreate();
2438 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2439 'contact_id' => $this->_individualId
,
2440 'installments' => '12',
2441 'frequency_interval' => '1',
2443 'contribution_status_id' => 1,
2444 'start_date' => '2012-01-01 00:00:00',
2445 'currency' => 'USD',
2446 'frequency_unit' => 'month',
2447 'payment_processor_id' => $paymentProcessorID,
2448 'campaign_id' => $campaignID,
2450 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2453 'contribution_recur_id' => $contributionRecur['id'],
2454 'campaign_id' => $campaignID2,
2458 $this->callAPISuccess('contribution', 'repeattransaction', array(
2459 'original_contribution_id' => $originalContribution['id'],
2460 'contribution_status_id' => 'Completed',
2461 'trxn_id' => uniqid(),
2464 $this->callAPISuccessGetSingle('contribution', array(
2465 'total_amount' => 100,
2466 'campaign_id' => $campaignID,
2471 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2473 public function testCompleteTransactionNetAmountOK() {
2474 $this->createLoggedInUser();
2475 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2476 unset($params['net_amount']);
2477 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2478 $this->callAPISuccess('contribution', 'completetransaction', array(
2479 'id' => $contribution['id'],
2481 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2482 $this->assertEquals('Completed', $contribution['contribution_status']);
2483 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2487 * CRM-14151 - Test completing a transaction via the API.
2489 public function testCompleteTransactionWithReceiptDateSet() {
2490 $this->swapMessageTemplateForTestTemplate();
2491 $mut = new CiviMailUtils($this, TRUE);
2492 $this->createLoggedInUser();
2493 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2494 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2495 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2496 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2497 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2498 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2499 $mut->checkMailLog(array(
2500 'Receipt - Contribution',
2501 'receipt_date:::' . date('Ymd'),
2504 $this->revertTemplateToReservedTemplate();
2508 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2510 public function testCompleteTransactionWithEmailReceiptInput() {
2511 $contributionPage = $this->createReceiptableContributionPage();
2513 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2514 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2515 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2516 // Complete the transaction overriding is_email_receipt to = FALSE
2517 $this->callAPISuccess('contribution', 'completetransaction', array(
2518 'id' => $contribution['id'],
2519 'trxn_date' => date('2011-04-09'),
2520 'trxn_id' => 'kazam',
2521 'is_email_receipt' => 0,
2523 // Check if a receipt was issued
2524 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2525 $this->assertEquals('', $receipt_date);
2529 * Test that $is_recur is assigned to the receipt.
2531 public function testCompleteTransactionForRecurring() {
2533 $this->swapMessageTemplateForTestTemplate();
2534 $recurring = $this->setUpRecurringContribution();
2535 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2537 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2538 $this->_params
['contribution_recur_id'] = $recurring['id'];
2540 $contribution = $this->setUpForCompleteTransaction();
2542 $this->callAPISuccess('contribution', 'completetransaction', array(
2543 'id' => $contribution['id'],
2544 'trxn_date' => date('2011-04-09'),
2545 'trxn_id' => 'kazam',
2546 'is_email_receipt' => 1,
2549 $this->mut
->checkMailLog(array(
2551 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2554 $this->revertTemplateToReservedTemplate();
2557 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2559 * If passed in it will override the default from contribution page.
2561 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2562 $mut = new CiviMailUtils($this, TRUE);
2563 $this->createLoggedInUser();
2564 // Create a Contribution Page with is_email_receipt = FALSE
2565 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2566 'receipt_from_name' => 'Mickey Mouse',
2567 'receipt_from_email' => 'mickey@mouse.com',
2568 'title' => "Test Contribution Page",
2569 'financial_type_id' => 1,
2570 'currency' => 'CAD',
2571 'is_monetary' => TRUE,
2572 'is_email_receipt' => 0,
2574 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2575 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2576 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2577 // Complete the transaction overriding is_email_receipt to = TRUE
2578 $this->callAPISuccess('contribution', 'completetransaction', array(
2579 'id' => $contribution['id'],
2580 'is_email_receipt' => 1,
2582 $mut->checkMailLog(array(
2583 'Please print this receipt for your records.',
2589 * Complete the transaction using the template with all the possible.
2591 public function testCompleteTransactionWithTestTemplate() {
2592 $this->swapMessageTemplateForTestTemplate();
2593 $contribution = $this->setUpForCompleteTransaction();
2594 $this->callAPISuccess('contribution', 'completetransaction', array(
2595 'id' => $contribution['id'],
2596 'trxn_date' => date('2011-04-09'),
2597 'trxn_id' => 'kazam',
2599 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2600 $this->mut
->checkMailLog(array(
2601 'email:::anthony_anderson@civicrm.org',
2605 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2606 'receipt_date:::' . date('Ymd'),
2607 'contributeMode:::notify',
2608 'title:::Contribution',
2609 'displayName:::Mr. Anthony Anderson II',
2611 'contactID:::' . $this->_params
['contact_id'],
2612 'contributionID:::' . $contribution['id'],
2613 'financialTypeId:::1',
2614 'financialTypeName:::Donation',
2617 $this->revertTemplateToReservedTemplate();
2621 * Complete the transaction using the template with all the possible.
2623 public function testCompleteTransactionContributionPageFromAddress() {
2624 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2625 'receipt_from_name' => 'Mickey Mouse',
2626 'receipt_from_email' => 'mickey@mouse.com',
2627 'title' => "Test Contribution Page",
2628 'financial_type_id' => 1,
2629 'currency' => 'NZD',
2630 'goal_amount' => 50,
2631 'is_pay_later' => 1,
2632 'is_monetary' => TRUE,
2633 'is_email_receipt' => TRUE,
2635 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2636 $contribution = $this->setUpForCompleteTransaction();
2637 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2638 $this->mut
->checkMailLog(array(
2646 * Test completing first transaction in a recurring series.
2648 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2650 * @dataProvider getScheduledDateData
2652 * @param array $dataSet
2654 * @throws \Exception
2656 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2657 $paymentProcessorID = $this->paymentProcessorCreate();
2658 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2659 'contact_id' => $this->_individualId
,
2660 'installments' => '2',
2661 'frequency_interval' => '1',
2663 'contribution_status_id' => 'Pending',
2664 'start_date' => '2012-01-01 00:00:00',
2665 'currency' => 'USD',
2666 'frequency_unit' => 'month',
2667 'payment_processor_id' => $paymentProcessorID,
2668 ), $dataSet['data']));
2669 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2672 'contribution_recur_id' => $contributionRecur['id'],
2673 'contribution_status_id' => 'Pending',
2674 'receive_date' => $dataSet['receive_date'],
2677 $this->callAPISuccess('Contribution', 'completetransaction', array(
2678 'id' => $contribution,
2679 'receive_date' => $dataSet['receive_date'],
2681 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2682 'id' => $contributionRecur['id'],
2683 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2685 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2686 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2687 $this->callAPISuccess('Contribution', 'create', array_merge(
2690 'contribution_recur_id' => $contributionRecur['id'],
2691 'contribution_status_id' => 'Completed',
2694 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2695 'id' => $contributionRecur['id'],
2696 'return' => array('contribution_status_id'),
2698 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2702 * Get dates for testing.
2706 public function getScheduledDateData() {
2708 $result[]['2016-08-31-1-month'] = array(
2710 'start_date' => '2016-08-31',
2711 'frequency_interval' => 1,
2712 'frequency_unit' => 'month',
2714 'receive_date' => '2016-08-31',
2715 'expected' => '2016-10-01 00:00:00',
2717 $result[]['2012-01-01-1-month'] = array(
2719 'start_date' => '2012-01-01',
2720 'frequency_interval' => 1,
2721 'frequency_unit' => 'month',
2723 'receive_date' => '2012-01-01',
2724 'expected' => '2012-02-01 00:00:00',
2726 $result[]['2012-01-01-1-month'] = array(
2728 'start_date' => '2012-01-01',
2729 'frequency_interval' => 1,
2730 'frequency_unit' => 'month',
2732 'receive_date' => '2012-02-29',
2733 'expected' => '2012-03-29 00:00:00',
2735 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2737 'start_date' => '2012-01-01',
2738 'frequency_interval' => 1,
2739 'frequency_unit' => 'month',
2740 'next_sched_contribution_date' => '2012-02-29',
2742 'receive_date' => '2012-02-29 16:00:00',
2743 'expected' => '2012-03-29 00:00:00',
2749 * Test completing a pledge with the completeTransaction api..
2751 * Note that we are creating a logged in user because email goes out from
2754 public function testCompleteTransactionUpdatePledgePayment() {
2755 $this->swapMessageTemplateForTestTemplate();
2756 $mut = new CiviMailUtils($this, TRUE);
2757 $mut->clearMessages();
2758 $this->createLoggedInUser();
2759 $contributionID = $this->createPendingPledgeContribution();
2760 $this->callAPISuccess('contribution', 'completetransaction', array(
2761 'id' => $contributionID,
2762 'trxn_date' => '1 Feb 2013',
2764 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2765 'id' => $this->_ids
['pledge'],
2767 $this->assertEquals('Completed', $pledge['pledge_status']);
2769 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2770 'pledge_id' => $this->_ids
['pledge'],
2771 'return' => 'status_id',
2773 $this->assertEquals(1, $status);
2774 $mut->checkMailLog(array(
2776 'receive_date:::20130201000000',
2777 "receipt_date:::\n",
2780 $this->revertTemplateToReservedTemplate();
2784 * Test completing a transaction with an event via the API.
2786 * Note that we are creating a logged in user because email goes out from
2789 public function testCompleteTransactionWithParticipantRecord() {
2790 $mut = new CiviMailUtils($this, TRUE);
2791 $mut->clearMessages();
2792 $this->_individualId
= $this->createLoggedInUser();
2793 $contributionID = $this->createPendingParticipantContribution();
2794 $this->callAPISuccess('contribution', 'completetransaction', array(
2795 'id' => $contributionID,
2798 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2799 'id' => $this->_ids
['participant'],
2800 'return' => 'participant_status_id',
2802 $this->assertEquals(1, $participantStatus);
2804 //Assert only three activities are created.
2805 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_individualId
);
2806 $this->assertEquals(3, count($activities));
2807 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
2808 // record two activities before and after completing payment for Event registration
2809 $this->assertEquals(2, $activityNames['Event Registration']);
2810 // update the original 'Contribution' activity created after completing payment
2811 $this->assertEquals(1, $activityNames['Contribution']);
2813 $mut->checkMailLog(array(
2814 'Annual CiviCRM meet',
2816 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2822 * Test membership is renewed when transaction completed.
2824 public function testCompleteTransactionMembershipPriceSet() {
2825 $this->createPriceSetWithPage('membership');
2826 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2830 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2831 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2832 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2833 'membership_id' => $this->_ids
['membership'],
2835 $this->assertEquals(1, $logs['count']);
2836 $this->assertEquals($stateOfGrace, $membership['status_id']);
2837 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2838 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2839 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2840 $this->callAPISuccessGetSingle('LineItem', array(
2841 'entity_id' => $this->_ids
['membership'],
2842 'entity_table' => 'civicrm_membership',
2844 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2845 'membership_id' => $this->_ids
['membership'],
2847 $this->assertEquals(2, $logs['count']);
2848 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2849 //Assert only three activities are created.
2850 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
2851 $this->assertEquals(3, count($activities));
2852 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
2853 $this->assertArrayHasKey('Contribution', $activityNames);
2854 $this->assertArrayHasKey('Membership Signup', $activityNames);
2855 $this->assertArrayHasKey('Change Membership Status', $activityNames);
2856 $this->cleanUpAfterPriceSets();
2860 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2862 public function testPendingToCompleteContribution() {
2863 $contributionPage = $this->createPriceSetWithPage('membership');
2864 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2868 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2869 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2870 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2871 $activity = $this->callAPISuccess('Activity', 'get', array(
2872 'activity_type_id' => 'Membership Signup',
2873 'source_record_id' => $this->_ids
['membership'],
2874 'status_id' => 'Scheduled',
2876 $this->assertEquals(1, $activity['count']);
2878 // change pending contribution to completed
2879 $form = new CRM_Contribute_Form_Contribution();
2881 $form->_params
= array(
2882 'id' => $this->_ids
['contribution'],
2883 'total_amount' => 20,
2886 'financial_type_id' => 1,
2887 'receive_date' => '04/21/2015',
2888 'receive_date_time' => '11:27PM',
2889 'contact_id' => $this->_individualId
,
2890 'contribution_status_id' => 1,
2891 'billing_middle_name' => '',
2892 'billing_last_name' => 'Adams',
2893 'billing_street_address-5' => '790L Lincoln St S',
2894 'billing_city-5' => 'Maryknoll',
2895 'billing_state_province_id-5' => 1031,
2896 'billing_postal_code-5' => 10545,
2897 'billing_country_id-5' => 1228,
2898 'frequency_interval' => 1,
2899 'frequency_unit' => 'month',
2900 'installments' => '',
2901 'hidden_AdditionalDetail' => 1,
2902 'hidden_Premium' => 1,
2903 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2904 'receipt_date' => '',
2905 'receipt_date_time' => '',
2906 'payment_processor_id' => $this->paymentProcessorID
,
2907 'currency' => 'USD',
2908 'contribution_page_id' => $this->_ids
['contribution_page'],
2909 'contribution_mode' => 'membership',
2910 'source' => 'Membership Signup and Renewal',
2913 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2915 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
2918 // Case 2: After successful payment for Pending backoffice there are three activities created
2919 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
2920 $activity = $this->callAPISuccess('Activity', 'get', array(
2921 'activity_type_id' => 'Membership Signup',
2922 'source_record_id' => $this->_ids
['membership'],
2923 'status_id' => 'Completed',
2925 $this->assertEquals(1, $activity['count']);
2926 // 2.b Contribution activity created to record successful payment
2927 $activity = $this->callAPISuccess('Activity', 'get', array(
2928 'activity_type_id' => 'Contribution',
2929 'source_record_id' => $this->_ids
['contribution'],
2930 'status_id' => 'Completed',
2932 $this->assertEquals(1, $activity['count']);
2934 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
2935 $activity = $this->callAPISuccess('Activity', 'get', array(
2936 'activity_type_id' => 'Change Membership Status',
2937 'source_record_id' => $this->_ids
['membership'],
2938 'status_id' => 'Completed',
2940 $this->assertEquals(1, $activity['count']);
2941 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
2943 //Create another pending contribution for renewal
2944 $contribution = $this->callAPISuccess('contribution', 'create', array(
2946 'contact_id' => $this->_ids
['contact'],
2947 'receive_date' => date('Ymd'),
2948 'total_amount' => 20.00,
2949 'financial_type_id' => 1,
2950 'payment_instrument_id' => 'Credit Card',
2951 'non_deductible_amount' => 10.00,
2952 'trxn_id' => 'rdhfi88',
2953 'invoice_id' => 'dofhiewuyr',
2955 'contribution_status_id' => 2,
2956 'contribution_page_id' => $this->_ids
['contribution_page'],
2957 'api.membership_payment.create' => array('membership_id' => $this->_ids
['membership']),
2960 $this->callAPISuccess('line_item', 'create', array(
2961 'entity_id' => $contribution['id'],
2962 'entity_table' => 'civicrm_contribution',
2963 'contribution_id' => $contribution['id'],
2964 'price_field_id' => $this->_ids
['price_field'][0],
2968 'financial_type_id' => 1,
2969 'price_field_value_id' => $this->_ids
['price_field_value'][0],
2972 //Update it to Failed.
2973 $form->_params
['id'] = $contribution['id'];
2974 $form->_params
['contribution_status_id'] = 4;
2976 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2978 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
2981 //Existing membership should not get updated to expired.
2982 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2983 $this->assertNotEquals($membership['status_id'], 4);
2987 * Test membership is renewed when transaction completed.
2989 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2990 $this->createPriceSetWithPage('membership');
2991 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
2992 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2993 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2994 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2995 $this->cleanUpAfterPriceSets();
2998 public function cleanUpAfterPriceSets() {
2999 $this->quickCleanUpFinancialEntities();
3000 $this->contactDelete($this->_ids
['contact']);
3004 * Set up a pending transaction with a specific price field id.
3006 * @param int $priceFieldValueID
3008 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
3009 $contactID = $this->individualCreate();
3010 $membership = $this->callAPISuccess('membership', 'create', array(
3011 'contact_id' => $contactID,
3012 'membership_type_id' => $this->_ids
['membership_type'],
3013 'start_date' => 'yesterday - 1 year',
3014 'end_date' => 'yesterday',
3015 'join_date' => 'yesterday - 1 year',
3017 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3019 'contact_id' => $contactID,
3020 'receive_date' => date('Ymd'),
3021 'total_amount' => 20.00,
3022 'financial_type_id' => 1,
3023 'payment_instrument_id' => 'Credit Card',
3024 'non_deductible_amount' => 10.00,
3025 'trxn_id' => 'jdhfi' . rand(1, 100),
3026 'invoice_id' => 'djfhiew' . rand(5, 100),
3028 'contribution_status_id' => 2,
3029 'contribution_page_id' => $this->_ids
['contribution_page'],
3030 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3033 $this->callAPISuccess('line_item', 'create', array(
3034 'entity_id' => $contribution['id'],
3035 'entity_table' => 'civicrm_contribution',
3036 'contribution_id' => $contribution['id'],
3037 'price_field_id' => $this->_ids
['price_field'][0],
3041 'financial_type_id' => 1,
3042 'price_field_value_id' => $priceFieldValueID,
3044 $this->_ids
['contact'] = $contactID;
3045 $this->_ids
['contribution'] = $contribution['id'];
3046 $this->_ids
['membership'] = $membership['id'];
3050 * Test sending a mail via the API.
3052 public function testSendMail() {
3053 $mut = new CiviMailUtils($this, TRUE);
3054 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3055 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3056 'id' => $contribution['id'],
3057 'receipt_from_email' => 'api@civicrm.org',
3060 $mut->checkMailLog(array(
3062 'Contribution Information',
3063 'Please print this confirmation for your records',
3069 $this->checkCreditCardDetails($mut, $contribution['id']);
3074 * Test sending a mail via the API.
3075 * This simulates webform_civicrm using pay later contribution page
3077 public function testSendconfirmationPayLater() {
3078 $mut = new CiviMailUtils($this, TRUE);
3080 // Create contribution page
3081 $pageParams = array(
3082 'title' => 'Webform Contributions',
3083 'financial_type_id' => 1,
3084 'contribution_type_id' => 1,
3085 'is_confirm_enabled' => 1,
3086 'is_pay_later' => 1,
3087 'pay_later_text' => 'I will send payment by cheque',
3088 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3090 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3092 // Create pay later contribution
3093 $contribParams = array(
3094 'contact_id' => $this->_individualId
,
3095 'financial_type_id' => 1,
3096 'is_pay_later' => 1,
3097 'contribution_status_id' => 2,
3098 'contribution_page_id' => $contributionPage['id'],
3099 'total_amount' => '10.00',
3101 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3104 $lineItemParams = array(
3105 'contribution_id' => $contribution['id'],
3106 'entity_id' => $contribution['id'],
3107 'entity_table' => 'civicrm_contribution',
3108 'label' => 'My lineitem label',
3110 'unit_price' => "10.00",
3111 'line_total' => "10.00",
3113 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3117 civicrm_api3('contribution', 'sendconfirmation', array(
3118 'id' => $contribution['id'],
3119 'receipt_from_email' => 'api@civicrm.org',
3123 catch (Exception
$e) {
3124 // Need to figure out how to stop this some other day
3125 // We don't care about the Payment Processor because this is Pay Later
3126 // The point of this test is to check we get the pay_later version of the mail
3127 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3132 // Retrieve mail & check it has the pay_later_receipt info
3133 $mut->getMostRecentEmail('raw');
3134 $mut->checkMailLog(array(
3135 (string) $contribParams['total_amount'],
3136 $pageParams['pay_later_receipt'],
3146 * Check credit card details in sent mail via API
3148 * @param $mut obj CiviMailUtils instance
3149 * @param int $contributionID Contribution ID
3152 public function checkCreditCardDetails($mut, $contributionID) {
3153 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3154 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3155 'id' => $contributionID,
3156 'receipt_from_email' => 'api@civicrm.org',
3157 'payment_processor_id' => $this->paymentProcessorID
,
3160 $mut->checkMailLog(array(
3161 'Credit Card Information', // credit card header
3162 'Billing Name and Address', // billing header
3163 'anthony_anderson@civicrm.org', // billing name
3171 * Test sending a mail via the API.
3173 public function testSendMailEvent() {
3174 $mut = new CiviMailUtils($this, TRUE);
3175 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3176 $event = $this->eventCreate(array(
3177 'is_email_confirm' => 1,
3178 'confirm_from_email' => 'test@civicrm.org',
3180 $this->_eventID
= $event['id'];
3181 $participantParams = array(
3182 'contact_id' => $this->_individualId
,
3183 'event_id' => $this->_eventID
,
3186 // to ensure it matches later on
3187 'register_date' => '2007-07-21 00:00:00',
3188 'source' => 'Online Event Registration: API Testing',
3191 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3192 $this->callAPISuccess('participant_payment', 'create', array(
3193 'participant_id' => $participant['id'],
3194 'contribution_id' => $contribution['id'],
3196 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3197 'id' => $contribution['id'],
3198 'receipt_from_email' => 'api@civicrm.org',
3202 $mut->checkMailLog(array(
3203 'Annual CiviCRM meet',
3205 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3212 * This function does a GET & compares the result against the $params.
3214 * Use as a double check on Creates.
3216 * @param array $params
3218 * @param bool $delete
3220 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3222 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3228 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3230 $this->assertAPISuccess($contribution, 0);
3231 $values = $contribution['values'][$contribution['id']];
3232 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3233 // this is not returned in id format
3234 unset($params['payment_instrument_id']);
3235 $params['contribution_source'] = $params['source'];
3236 unset($params['source']);
3237 foreach ($params as $key => $value) {
3238 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3243 * Create a pending contribution & linked pending pledge record.
3245 public function createPendingPledgeContribution() {
3247 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
3248 $this->_ids
['pledge'] = $pledgeID;
3249 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
3250 'contribution_status_id' => 'Pending',
3251 'total_amount' => 500,
3254 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3255 'options' => array('limit' => 1),
3258 $this->callAPISuccess('PledgePayment', 'create', array(
3260 'contribution_id' =>
3261 $contribution['id'],
3262 'status_id' => 'Pending',
3263 'scheduled_amount' => 500,
3266 return $contribution['id'];
3270 * Create a pending contribution & linked pending participant record (along with an event).
3272 public function createPendingParticipantContribution() {
3273 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3274 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId
));
3275 $this->_ids
['participant'] = $participantID;
3276 $params = array_merge($this->_params
, array('contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3277 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3278 $this->callAPISuccess('participant_payment', 'create', array(
3279 'contribution_id' => $contribution['id'],
3280 'participant_id' => $participantID,
3282 $this->callAPISuccess('line_item', 'get', array(
3283 'entity_id' => $contribution['id'],
3284 'entity_table' => 'civicrm_contribution',
3285 'api.line_item.create' => array(
3286 'entity_id' => $participantID,
3287 'entity_table' => 'civicrm_participant',
3290 return $contribution['id'];
3294 * Get financial transaction amount.
3296 * @param int $contId
3298 * @return null|string
3300 public function _getFinancialTrxnAmount($contId) {
3302 SUM( ft.total_amount ) AS total
3303 FROM civicrm_financial_trxn AS ft
3304 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3305 WHERE ceft.entity_table = 'civicrm_contribution'
3306 AND ceft.entity_id = {$contId}";
3308 $result = CRM_Core_DAO
::singleValueQuery($query);
3313 * @param int $contId
3315 * @return null|string
3317 public function _getFinancialItemAmount($contId) {
3318 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3321 FROM civicrm_financial_item
3322 WHERE entity_table = 'civicrm_line_item'
3323 AND entity_id = {$lineItem}";
3324 $result = CRM_Core_DAO
::singleValueQuery($query);
3329 * @param int $contId
3332 public function _checkFinancialItem($contId, $context) {
3333 if ($context != 'paylater') {
3335 'entity_id' => $contId,
3336 'entity_table' => 'civicrm_contribution',
3338 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3339 $entityParams = array(
3340 'financial_trxn_id' => $trxn['financial_trxn_id'],
3341 'entity_table' => 'civicrm_financial_item',
3343 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3345 'id' => $entityTrxn['entity_id'],
3348 if ($context == 'paylater') {
3349 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3350 foreach ($lineItems as $key => $item) {
3352 'entity_id' => $key,
3353 'entity_table' => 'civicrm_line_item',
3355 $compareParams = array('status_id' => 1);
3356 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3359 elseif ($context == 'refund') {
3360 $compareParams = array(
3362 'financial_account_id' => 1,
3366 elseif ($context == 'cancelPending') {
3367 $compareParams = array(
3369 'financial_account_id' => 1,
3373 elseif ($context == 'changeFinancial') {
3374 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3376 'entity_id' => $lineKey,
3379 $compareParams = array(
3380 'financial_account_id' => 1,
3382 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3384 'financial_account_id' => 3,
3385 'entity_id' => $lineKey,
3387 $compareParams = array(
3391 if ($context != 'paylater') {
3392 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3397 * Check correct financial transaction entries were created for the change in payment instrument.
3399 * @param int $contributionID
3400 * @param int $originalInstrumentID
3401 * @param int $newInstrumentID
3403 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3405 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3407 $originalTrxnParams = array(
3408 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3409 'payment_instrument_id' => $originalInstrumentID,
3410 'amount' => $amount,
3414 $reversalTrxnParams = array(
3415 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3416 'payment_instrument_id' => $originalInstrumentID,
3417 'amount' => -$amount,
3421 $newTrxnParams = array(
3422 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3423 'payment_instrument_id' => $newInstrumentID,
3424 'amount' => $amount,
3428 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3429 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3430 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3432 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3433 'id' => $entityFinancialTrxn['financial_trxn_id'],
3435 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3436 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3437 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3438 $this->assertEquals(0, $financialTrxn['fee_amount']);
3439 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3440 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3443 $this->assertEquals(1, $financialTrxn['is_payment']);
3444 $this->assertEquals('USD', $financialTrxn['currency']);
3445 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3450 * Check financial transaction.
3452 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3454 * @param array $contribution
3455 * @param string $context
3456 * @param int $instrumentId
3457 * @param array $extraParams
3459 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3460 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3461 $trxn = array_pop($financialTrxns);
3464 'id' => $trxn['financial_trxn_id'],
3466 if ($context == 'payLater') {
3467 $compareParams = array(
3469 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3472 elseif ($context == 'refund') {
3473 $compareParams = array(
3474 'to_financial_account_id' => 6,
3475 'total_amount' => -100,
3477 'trxn_date' => '2015-01-01 09:00:00',
3478 'trxn_id' => 'the refund',
3481 elseif ($context == 'cancelPending') {
3482 $compareParams = array(
3483 'to_financial_account_id' => 7,
3484 'total_amount' => -100,
3488 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3489 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3490 // It does the same thing with greater readability.
3491 // @todo remove handling for
3493 $entityParams = array(
3494 'entity_id' => $contribution['id'],
3495 'entity_table' => 'civicrm_contribution',
3498 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3499 $trxnParams1 = array(
3500 'id' => $trxn['financial_trxn_id'],
3502 if (empty($extraParams)) {
3503 $compareParams = array(
3504 'total_amount' => -100,
3509 $compareParams = array(
3510 'total_amount' => 100,
3514 if ($context == 'paymentInstrument') {
3515 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3516 $compareParams['payment_instrument_id'] = $instrumentId;
3519 $compareParams['to_financial_account_id'] = 12;
3521 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3522 $compareParams['total_amount'] = 100;
3525 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3531 public function _addPaymentInstrument() {
3532 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3533 $optionParams = array(
3534 'option_group_id' => $gId,
3535 'label' => 'Test Card',
3536 'name' => 'Test Card',
3541 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3542 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3543 $financialParams = array(
3544 'entity_table' => 'civicrm_option_value',
3545 'entity_id' => $optionValue['id'],
3546 'account_relationship' => $relationTypeId,
3547 'financial_account_id' => 7,
3549 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3550 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3551 return $optionValue['values'][$optionValue['id']]['value'];
3554 public function _deletedAddedPaymentInstrument() {
3555 $result = $this->callAPISuccess('OptionValue', 'get', array(
3556 'option_group_id' => 'payment_instrument',
3557 'name' => 'Test Card',
3561 if ($id = CRM_Utils_Array
::value('id', $result)) {
3562 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3567 * Set up the basic recurring contribution for tests.
3569 * @param array $generalParams
3570 * Parameters that can be merged into the recurring AND the contribution.
3572 * @param array $recurParams
3573 * Parameters to merge into the recur only.
3577 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3578 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3579 'contact_id' => $this->_individualId
,
3580 'installments' => '12',
3581 'frequency_interval' => '1',
3583 'contribution_status_id' => 1,
3584 'start_date' => '2012-01-01 00:00:00',
3585 'currency' => 'USD',
3586 'frequency_unit' => 'month',
3587 'payment_processor_id' => $this->paymentProcessorID
,
3588 ), $generalParams, $recurParams));
3589 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3592 'contribution_recur_id' => $contributionRecur['id'],
3595 return $originalContribution;
3599 * Set up a basic auto-renew membership for tests.
3601 * @param array $generalParams
3602 * Parameters that can be merged into the recurring AND the contribution.
3604 * @param array $recurParams
3605 * Parameters to merge into the recur only.
3609 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3610 $newContact = $this->callAPISuccess('Contact', 'create', array(
3611 'contact_type' => 'Individual',
3612 'sort_name' => 'McTesterson, Testy',
3613 'display_name' => 'Testy McTesterson',
3614 'preferred_language' => 'en_US',
3615 'preferred_mail_format' => 'Both',
3616 'first_name' => 'Testy',
3617 'last_name' => 'McTesterson',
3618 'contact_is_deleted' => '0',
3620 'email' => 'tmctesterson@example.com',
3623 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3624 'domain_id' => "Default Domain Name",
3625 'member_of_contact_id' => 1,
3626 'financial_type_id' => "Member Dues",
3627 'duration_unit' => "month",
3628 'duration_interval' => 1,
3629 'period_type' => 'rolling',
3630 'name' => "Standard Member",
3631 'minimum_fee' => 100,
3633 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3634 'contact_id' => $newContact['id'],
3635 'installments' => '12',
3636 'frequency_interval' => '1',
3638 'contribution_status_id' => 1,
3639 'start_date' => '2012-01-01 00:00:00',
3640 'currency' => 'USD',
3641 'frequency_unit' => 'month',
3642 'payment_processor_id' => $this->paymentProcessorID
,
3643 ), $generalParams, $recurParams));
3645 $membership = $this->callAPISuccess('membership', 'create', array(
3646 'contact_id' => $newContact['id'],
3647 'contribution_recur_id' => $contributionRecur['id'],
3648 'financial_type_id' => "Member Dues",
3649 'membership_type_id' => $membershipType['id'],
3651 'skipLineItem' => TRUE,
3654 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3655 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3658 'contact_id' => $newContact['id'],
3659 'contribution_recur_id' => $contributionRecur['id'],
3660 'financial_type_id' => "Member Dues",
3661 'contribution_status_id' => 1,
3662 'invoice_id' => uniqid(),
3665 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3666 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3667 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3668 $this->callAPISuccess('LineItem', 'getsingle', array());
3669 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3671 return array($originalContribution, $membership);
3674 * Set up a repeat transaction.
3676 * @param array $recurParams
3680 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3681 $paymentProcessorID = $this->paymentProcessorCreate();
3682 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3683 'contact_id' => $this->_individualId
,
3684 'installments' => '12',
3685 'frequency_interval' => '1',
3687 'contribution_status_id' => 1,
3688 'start_date' => '2012-01-01 00:00:00',
3689 'currency' => 'USD',
3690 'frequency_unit' => 'month',
3691 'payment_processor_id' => $paymentProcessorID,
3694 $originalContribution = '';
3695 if ($flag == 'multiple') {
3696 // CRM-19309 create a contribution + also add in line_items (plural):
3697 $params = array_merge($this->_params
, $contributionParams);
3698 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3701 'contribution_recur_id' => $contributionRecur['id'],
3702 'skipLineItem' => 1,
3703 'api.line_item.create' => array(
3705 'price_field_id' => 1,
3707 'line_total' => '20',
3708 'unit_price' => '10',
3709 'financial_type_id' => 1,
3712 'price_field_id' => 1,
3714 'line_total' => '80',
3715 'unit_price' => '80',
3716 'financial_type_id' => 2,
3723 elseif ($flag == 'single') {
3724 $params = array_merge($this->_params
, array('contribution_recur_id' => $contributionRecur['id']));
3725 $params = array_merge($params, $contributionParams);
3726 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3728 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3729 return $originalContribution;
3733 * Common set up routine.
3737 protected function setUpForCompleteTransaction() {
3738 $this->mut
= new CiviMailUtils($this, TRUE);
3739 $this->createLoggedInUser();
3740 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3741 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3742 return $contribution;
3746 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3748 public function testRepeatTransactionWithNonCreditCardDefault() {
3749 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3750 'contact_id' => $this->_individualId
,
3751 'installments' => '12',
3752 'frequency_interval' => '1',
3754 'contribution_status_id' => 1,
3755 'start_date' => '2012-01-01 00:00:00',
3756 'currency' => 'USD',
3757 'frequency_unit' => 'month',
3758 'payment_processor_id' => $this->paymentProcessorID
,
3760 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3762 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3764 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
3765 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3766 'contribution_status_id' => 'Completed',
3767 'trxn_id' => uniqid(),
3768 'original_contribution_id' => $contribution1,
3770 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3771 $this->quickCleanUpFinancialEntities();
3775 * CRM-20008 Tests repeattransaction creates pending membership.
3777 public function testRepeatTransactionPendingMembership() {
3778 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3779 $this->callAPISuccess('membership', 'create', array(
3780 'id' => $membership['id'],
3781 'end_date' => 'yesterday',
3782 'status_id' => 'Expired',
3784 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3785 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3786 'contribution_status_id' => 'Pending',
3787 'trxn_id' => uniqid(),
3789 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3790 'id' => $membership['id'],
3791 'return' => 'status_id',
3794 // Let's see if the membership payments got created while we're at it.
3795 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3796 'memberhip_id' => $membership['id'],
3798 $this->assertEquals(2, $membershipPayments['count']);
3800 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3801 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3802 $membership = $this->callAPISuccessGetSingle('membership', array(
3803 'id' => $membership['id'],
3804 'return' => 'status_id, end_date',
3806 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3807 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3809 $this->quickCleanUpFinancialEntities();
3810 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3814 * Test sending a mail via the API.
3816 public function testSendMailWithAPISetFromDetails() {
3817 $mut = new CiviMailUtils($this, TRUE);
3818 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3819 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3820 'id' => $contribution['id'],
3821 'receipt_from_email' => 'api@civicrm.org',
3822 'receipt_from_name' => 'CiviCRM LLC',
3824 $mut->checkMailLog(array(
3825 'From: CiviCRM LLC <api@civicrm.org>',
3826 'Contribution Information',
3827 'Please print this confirmation for your records',
3836 * Test sending a mail via the API.
3838 public function testSendMailWithNoFromSetFallToDomain() {
3839 $this->createLoggedInUser();
3840 $mut = new CiviMailUtils($this, TRUE);
3841 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3842 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3843 'id' => $contribution['id'],
3845 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3846 $mut->checkMailLog(array(
3847 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3848 'Contribution Information',
3849 'Please print this confirmation for your records',
3858 * Test sending a mail via the API.
3860 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3861 $this->createLoggedInUser();
3862 $mut = new CiviMailUtils($this, TRUE);
3863 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3864 $this->callAPISuccess('contribution', 'repeattransaction', array(
3865 'contribution_status_id' => 'Completed',
3866 'trxn_id' => uniqid(),
3867 'original_contribution_id' => $contribution,
3869 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3870 $mut->checkMailLog(array(
3871 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3872 'Contribution Information',
3873 'Please print this confirmation for your records',
3882 * Test sending a mail via the API.
3884 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3885 $mut = new CiviMailUtils($this, TRUE);
3886 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3887 $paymentProcessorID = $this->paymentProcessorCreate();
3888 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3889 'contact_id' => $this->_individualId
,
3890 'installments' => '12',
3891 'frequency_interval' => '1',
3893 'contribution_status_id' => 1,
3894 'start_date' => '2012-01-01 00:00:00',
3895 'currency' => 'USD',
3896 'frequency_unit' => 'month',
3897 'payment_processor_id' => $paymentProcessorID,
3899 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3902 'contribution_recur_id' => $contributionRecur['id'],
3903 'contribution_page_id' => $contributionPage['id']))
3905 $this->callAPISuccess('contribution', 'repeattransaction', array(
3906 'contribution_status_id' => 'Completed',
3907 'trxn_id' => uniqid(),
3908 'original_contribution_id' => $originalContribution,
3911 $mut->checkMailLog(array(
3912 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3913 'Contribution Information',
3914 'Please print this confirmation for your records',
3923 * Test sending a mail via the API.
3925 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
3926 $mut = new CiviMailUtils($this, TRUE);
3927 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
3928 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
3929 foreach ($fromEmail['values'] as $from) {
3930 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
3932 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
3933 $this->callAPISuccess('contribution', 'repeattransaction', array(
3934 'contribution_status_id' => 'Completed',
3935 'trxn_id' => uniqid(),
3936 'original_contribution_id' => $originalContribution,
3939 $mut->checkMailLog(array(
3940 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
3941 'Contribution Information',
3942 'Please print this confirmation for your records',
3951 * Create a Contribution Page with is_email_receipt = TRUE.
3953 * @param array $params
3954 * Params to overwrite with.
3958 protected function createReceiptableContributionPage($params = array()) {
3959 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
3960 'receipt_from_name' => 'Mickey Mouse',
3961 'receipt_from_email' => 'mickey@mouse.com',
3962 'title' => "Test Contribution Page",
3963 'financial_type_id' => 1,
3964 'currency' => 'CAD',
3965 'is_monetary' => TRUE,
3966 'is_email_receipt' => TRUE,
3968 return $contributionPage;
3972 * function to test card_type and pan truncation.
3974 public function testCardTypeAndPanTruncation() {
3975 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
3976 $contactId = $this->individualCreate();
3978 'contact_id' => $contactId,
3979 'receive_date' => '2016-01-20',
3980 'total_amount' => 100,
3981 'financial_type_id' => 1,
3982 'payment_instrument' => 'Credit Card',
3983 'card_type_id' => $creditCardTypeIDs['Visa'],
3984 'pan_truncation' => 4567,
3986 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3987 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
3988 $financialTrxn = $this->callAPISuccessGetSingle(
3991 'id' => $lastFinancialTrxnId['financialTrxnId'],
3992 'return' => array('card_type_id', 'pan_truncation'),
3995 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
3996 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
3998 'id' => $contribution['id'],
3999 'pan_truncation' => 2345,
4000 'card_type_id' => $creditCardTypeIDs['Amex'],
4002 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4003 $financialTrxn = $this->callAPISuccessGetSingle(
4006 'id' => $lastFinancialTrxnId['financialTrxnId'],
4007 'return' => array('card_type_id', 'pan_truncation'),
4010 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4011 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
4015 * Test repeat contribution uses non default currency
4016 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4018 public function testRepeatTransactionWithDifferenceCurrency() {
4019 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4020 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4021 'original_contribution_id' => $originalContribution['id'],
4022 'contribution_status_id' => 'Completed',
4023 'trxn_id' => uniqid(),
4025 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4029 * Get the financial items for the contribution.
4031 * @param int $contributionID
4034 * Array of associated financial items.
4036 protected function getFinancialTransactionsForContribution($contributionID) {
4037 $trxnParams = array(
4038 'entity_id' => $contributionID,
4039 'entity_table' => 'civicrm_contribution',
4041 // @todo the following function has naming errors & has a weird signature & appears to
4042 // only be called from test classes. Move into test suite & maybe just use api
4043 // from this function.
4044 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));