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() {
132 'contact_id' => $this->_individualId
,
133 'receive_date' => '2010-01-20',
134 'total_amount' => 100.00,
135 'financial_type_id' => $this->_financialTypeId
,
136 'non_deductible_amount' => 10.00,
137 'fee_amount' => 5.00,
138 'net_amount' => 95.00,
140 'invoice_id' => 78910,
142 'contribution_status_id' => 1,
144 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
147 'contribution_id' => $this->_contribution
['id'],
150 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
151 $financialParams['id'] = $this->_financialTypeId
;
153 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
155 $this->assertEquals(1, $contributions['count']);
156 $contribution = $contributions['values'][$contributions['id']];
157 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
158 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
159 // Passing back a string rather than an id seems like an error/cruft.
160 // If it is to be introduced we should discuss.
161 $this->assertEquals($contribution['financial_type_id'], 1);
162 $this->assertEquals($contribution['total_amount'], 100.00);
163 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
164 $this->assertEquals($contribution['fee_amount'], 5.00);
165 $this->assertEquals($contribution['net_amount'], 95.00);
166 $this->assertEquals($contribution['trxn_id'], 23456);
167 $this->assertEquals($contribution['invoice_id'], 78910);
168 $this->assertEquals($contribution['contribution_source'], 'SSF');
169 $this->assertEquals($contribution['contribution_status'], 'Completed');
170 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
171 $p['trxn_id'] = '3847';
172 $p['invoice_id'] = '3847';
174 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
176 // Now we have 2 - test getcount.
177 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
178 $this->assertEquals(2, $contribution);
179 // Test id only format.
180 $contribution = $this->callAPISuccess('contribution', 'get', array(
181 'id' => $this->_contribution
['id'],
182 'format.only_id' => 1,
184 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
185 // Test id only format.
186 $contribution = $this->callAPISuccess('contribution', 'get', array(
187 'id' => $contribution2['id'],
188 'format.only_id' => 1,
190 $this->assertEquals($contribution2['id'], $contribution);
192 $contribution = $this->callAPISuccess('contribution', 'get', array(
193 'id' => $this->_contribution
['id'],
195 $this->assertEquals(1, $contribution['count']);
197 // Test get by contact id works.
198 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
200 $this->assertEquals(2, $contribution['count']);
201 $this->callAPISuccess('Contribution', 'Delete', array(
202 'id' => $this->_contribution
['id'],
204 $this->callAPISuccess('Contribution', 'Delete', array(
205 'id' => $contribution2['id'],
210 * Test that test contributions can be retrieved.
212 public function testGetTestContribution() {
213 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('is_test' => 1)));
214 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
218 * Test the 'return' param works for all fields.
220 public function testGetContributionReturnFunctionality() {
221 $params = $this->_params
;
222 $params['check_number'] = 'bouncer';
223 $params['payment_instrument_id'] = 'Check';
224 $params['cancel_date'] = 'yesterday';
225 $params['receipt_date'] = 'yesterday';
226 $params['thankyou_date'] = 'yesterday';
227 $params['revenue_recognition_date'] = 'yesterday';
228 $params['amount_level'] = 'Unreasonable';
229 $params['cancel_reason'] = 'You lose sucker';
230 $params['creditnote_id'] = 'sudo rm -rf';
231 $params['tax_amount'] = '1';
232 $address = $this->callAPISuccess('Address', 'create', array(
233 'street_address' => 'Knockturn Alley',
234 'contact_id' => $this->_individualId
,
235 'location_type_id' => 'Home',
237 $params['address_id'] = $address['id'];
238 $contributionPage = $this->contributionPageCreate();
239 $params['contribution_page_id'] = $contributionPage['id'];
240 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array(
241 'contact_id' => $this->_individualId
,
242 'frequency_interval' => 1,
245 $params['contribution_recur_id'] = $contributionRecur['id'];
247 $params['campaign_id'] = $this->campaignCreate();
249 $contributionID = $this->contributionCreate($params);
251 // update contribution with invoice number
252 $params = array_merge($params, array(
253 'id' => $contributionID,
254 'invoice_number' => CRM_Utils_Array
::value('invoice_prefix', Civi
::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
256 $contributionID = $this->contributionCreate($params);
258 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
259 $this->assertEquals('bouncer', $contribution['check_number']);
260 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
262 $fields = CRM_Contribute_BAO_Contribution
::fields();
263 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
264 // need to specify them.
265 $fields['address_id'] = $fields['contribution_address_id'];
266 $fields['check_number'] = $fields['contribution_check_number'];
268 $fieldsLockedIn = array(
269 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
270 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
271 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
272 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
273 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
274 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
276 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
277 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
278 // covered as the test contract now guarantees them in the return array.
279 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
280 foreach ($fields as $fieldName => $fieldSpec) {
281 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
282 $returnField = $fieldName;
283 if ($returnField == 'contribution_contact_id') {
284 $returnField = 'contact_id';
286 $this->assertTrue((!empty($contribution[$returnField]) ||
$contribution[$returnField] === "0"), $returnField);
291 * We need to ensure previous tested behaviour still works as part of the api contract.
293 public function testGetContributionLegacyBehaviour() {
295 'contact_id' => $this->_individualId
,
296 'receive_date' => '2010-01-20',
297 'total_amount' => 100.00,
298 'contribution_type_id' => $this->_financialTypeId
,
299 'non_deductible_amount' => 10.00,
300 'fee_amount' => 5.00,
301 'net_amount' => 95.00,
303 'invoice_id' => 78910,
305 'contribution_status_id' => 1,
307 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
310 'contribution_id' => $this->_contribution
['id'],
312 $contribution = $this->callAPISuccess('contribution', 'get', $params);
313 $financialParams['id'] = $this->_financialTypeId
;
315 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
317 $this->assertEquals(1, $contribution['count']);
318 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
319 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
320 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
321 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
322 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
323 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
324 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
325 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
326 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
327 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
328 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
330 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
331 $p['trxn_id'] = '3847';
332 $p['invoice_id'] = '3847';
334 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
336 // now we have 2 - test getcount
337 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
338 $this->assertEquals(2, $contribution);
339 //test id only format
340 $contribution = $this->callAPISuccess('contribution', 'get', array(
341 'id' => $this->_contribution
['id'],
342 'format.only_id' => 1,
344 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
345 //test id only format
346 $contribution = $this->callAPISuccess('contribution', 'get', array(
347 'id' => $contribution2['id'],
348 'format.only_id' => 1,
350 $this->assertEquals($contribution2['id'], $contribution);
351 $contribution = $this->callAPISuccess('contribution', 'get', array(
352 'id' => $this->_contribution
['id'],
355 $this->assertEquals(1, $contribution['count']);
356 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
357 //test get by contact id works
358 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
360 $this->assertEquals(2, $contribution['count']);
361 $this->callAPISuccess('Contribution', 'Delete', array(
362 'id' => $this->_contribution
['id'],
364 $this->callAPISuccess('Contribution', 'Delete', array(
365 'id' => $contribution2['id'],
370 * Create an contribution_id=FALSE and financial_type_id=Donation.
372 public function testCreateEmptyContributionIDUseDonation() {
374 'contribution_id' => FALSE,
377 'check_permissions' => FALSE,
378 'financial_type_id' => 'Donation',
380 $this->callAPISuccess('contribution', 'create', $params);
384 * Check with complete array + custom field.
386 * Note that the test is written on purpose without any
387 * variables specific to participant so it can be replicated into other entities
388 * and / or moved to the automated test suite
390 public function testCreateWithCustom() {
391 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
393 $params = $this->_params
;
394 $params['custom_' . $ids['custom_field_id']] = "custom string";
396 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
397 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
398 $check = $this->callAPISuccess($this->_entity
, 'get', array(
399 'return.custom_' . $ids['custom_field_id'] => 1,
400 'id' => $result['id'],
402 $this->customFieldDelete($ids['custom_field_id']);
403 $this->customGroupDelete($ids['custom_group_id']);
404 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
408 * Check with complete array + custom field.
410 * Note that the test is written on purpose without any
411 * variables specific to participant so it can be replicated into other entities
412 * and / or moved to the automated test suite
414 public function testCreateGetFieldsWithCustom() {
415 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
416 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
417 $result = $this->callAPISuccess('Contribution', 'getfields', array());
418 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
419 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
420 $this->customFieldDelete($ids['custom_field_id']);
421 $this->customGroupDelete($ids['custom_group_id']);
422 $this->customFieldDelete($idsContact['custom_field_id']);
423 $this->customGroupDelete($idsContact['custom_group_id']);
426 public function testCreateContributionNoLineItems() {
429 'contact_id' => $this->_individualId
,
430 'receive_date' => '20120511',
431 'total_amount' => 100.00,
432 'financial_type_id' => $this->_financialTypeId
,
433 'payment_instrument_id' => 1,
434 'non_deductible_amount' => 10.00,
435 'fee_amount' => 50.00,
436 'net_amount' => 90.00,
438 'invoice_id' => 67890,
440 'contribution_status_id' => 1,
444 $contribution = $this->callAPISuccess('contribution', 'create', $params);
445 $lineItems = $this->callAPISuccess('line_item', 'get', array(
446 'entity_id' => $contribution['id'],
447 'entity_table' => 'civicrm_contribution',
450 $this->assertEquals(0, $lineItems['count']);
454 * Test checks that passing in line items suppresses the create mechanism.
456 public function testCreateContributionChainedLineItems() {
458 'contact_id' => $this->_individualId
,
459 'receive_date' => '20120511',
460 'total_amount' => 100.00,
461 'financial_type_id' => $this->_financialTypeId
,
462 'payment_instrument_id' => 1,
463 'non_deductible_amount' => 10.00,
464 'fee_amount' => 50.00,
465 'net_amount' => 90.00,
467 'invoice_id' => 67890,
469 'contribution_status_id' => 1,
471 'api.line_item.create' => array(
473 'price_field_id' => 1,
475 'line_total' => '20',
476 'unit_price' => '10',
479 'price_field_id' => 1,
481 'line_total' => '80',
482 'unit_price' => '80',
487 $description = "Create Contribution with Nested Line Items.";
488 $subfile = "CreateWithNestedLineItems";
489 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
491 $lineItems = $this->callAPISuccess('line_item', 'get', array(
492 'entity_id' => $contribution['id'],
493 'contribution_id' => $contribution['id'],
494 'entity_table' => 'civicrm_contribution',
497 $this->assertEquals(2, $lineItems['count']);
500 public function testCreateContributionOffline() {
502 'contact_id' => $this->_individualId
,
503 'receive_date' => '20120511',
504 'total_amount' => 100.00,
505 'financial_type_id' => 1,
507 'invoice_id' => 67890,
509 'contribution_status_id' => 1,
512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
513 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
514 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
515 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
516 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
517 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
518 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
519 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
520 $lineItems = $this->callAPISuccess('line_item', 'get', array(
521 'entity_id' => $contribution['id'],
522 'contribution_id' => $contribution['id'],
523 'entity_table' => 'civicrm_contribution',
526 $this->assertEquals(1, $lineItems['count']);
527 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
528 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
529 $this->_checkFinancialRecords($contribution, 'offline');
530 $this->contributionGetnCheck($params, $contribution['id']);
534 * Test create with valid payment instrument.
536 public function testCreateContributionWithPaymentInstrument() {
537 $params = $this->_params +
array('payment_instrument' => 'EFT');
538 $contribution = $this->callAPISuccess('contribution', 'create', $params);
539 $contribution = $this->callAPISuccess('contribution', 'get', array(
541 'id' => $contribution['id'],
543 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
544 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
546 $this->callAPISuccess('contribution', 'create', array(
547 'id' => $contribution['id'],
548 'payment_instrument' => 'Credit Card',
550 $contribution = $this->callAPISuccess('contribution', 'get', array(
552 'id' => $contribution['id'],
554 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
555 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
558 public function testGetContributionByPaymentInstrument() {
559 $params = $this->_params +
array('payment_instrument' => 'EFT');
560 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
561 $this->callAPISuccess('contribution', 'create', $params);
562 $this->callAPISuccess('contribution', 'create', $params2);
563 $contribution = $this->callAPISuccess('contribution', 'get', array(
565 'contribution_payment_instrument' => 'Cash',
567 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
568 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
569 $this->assertEquals(1, $contribution['count']);
570 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
571 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
572 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
573 $this->assertEquals(1, $contribution['count']);
574 $contribution = $this->callAPISuccess('contribution', 'get', array(
576 'payment_instrument_id' => 5,
578 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
579 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
580 $this->assertEquals(1, $contribution['count']);
581 $contribution = $this->callAPISuccess('contribution', 'get', array(
583 'payment_instrument' => 'EFT',
585 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
586 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
587 $this->assertEquals(1, $contribution['count']);
588 $contribution = $this->callAPISuccess('contribution', 'create', array(
589 'id' => $contribution['id'],
590 'payment_instrument' => 'Credit Card',
592 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
593 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
594 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
595 $this->assertEquals(1, $contribution['count']);
599 * CRM-16227 introduces invoice_id as a parameter.
601 public function testGetContributionByInvoice() {
602 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('invoice_id' => 'curly')));
603 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), array('invoice_id' => 'churlish'));
604 $this->callAPISuccessGetCount('Contribution', array(), 2);
605 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
606 // The following don't work. They are the format we are trying to introduce but although the form uses this format
607 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
608 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
609 // come out of convertFormValues
610 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
611 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
612 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
613 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
618 * Check the credit note retrieval is case insensitive.
620 public function testGetCreditNoteCaseInsensitive() {
621 $this->contributionCreate(array('contact_id' => $this->_individualId
));
622 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId
, 'invoice_id' => rand(), 'trxn_id' => rand()));
623 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
624 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
628 * Test retrieval by total_amount works.
632 public function testGetContributionByTotalAmount() {
633 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '5')));
634 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('total_amount' => '10')));
635 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
636 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
637 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
638 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
639 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
640 $this->callAPISuccessGetCount('Contribution', array(), 2);
644 * Create test with unique field name on source.
646 public function testCreateContributionSource() {
649 'contact_id' => $this->_individualId
,
650 'receive_date' => date('Ymd'),
651 'total_amount' => 100.00,
652 'financial_type_id' => $this->_financialTypeId
,
653 'payment_instrument_id' => 1,
654 'non_deductible_amount' => 10.00,
655 'fee_amount' => 50.00,
656 'net_amount' => 90.00,
658 'invoice_id' => 67890,
659 'contribution_source' => 'SSF',
660 'contribution_status_id' => 1,
663 $contribution = $this->callAPISuccess('contribution', 'create', $params);
664 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
665 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
669 * Create test with unique field name on source.
671 public function testCreateDefaultNow() {
673 $params = $this->_params
;
674 unset($params['receive_date']);
676 $contribution = $this->callAPISuccess('contribution', 'create', $params);
677 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
678 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
682 * Create test with unique field name on source.
684 public function testCreateContributionSourceInvalidContact() {
688 'receive_date' => date('Ymd'),
689 'total_amount' => 100.00,
690 'financial_type_id' => $this->_financialTypeId
,
691 'payment_instrument_id' => 1,
692 'non_deductible_amount' => 10.00,
693 'fee_amount' => 50.00,
694 'net_amount' => 90.00,
696 'invoice_id' => 67890,
697 'contribution_source' => 'SSF',
698 'contribution_status_id' => 1,
701 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
704 public function testCreateContributionSourceInvalidContContact() {
707 'contribution_contact_id' => 999,
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);
725 * Test note created correctly.
727 public function testCreateContributionWithNote() {
728 $description = "Demonstrates creating contribution with Note Entity.";
729 $subfile = "ContributionCreateWithNote";
731 'contact_id' => $this->_individualId
,
732 'receive_date' => '2012-01-01',
733 'total_amount' => 100.00,
734 'financial_type_id' => $this->_financialTypeId
,
735 'payment_instrument_id' => 1,
736 'non_deductible_amount' => 10.00,
737 'fee_amount' => 50.00,
738 'net_amount' => 90.00,
740 'invoice_id' => 67890,
742 'contribution_status_id' => 1,
743 'note' => 'my contribution note',
746 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
747 $result = $this->callAPISuccess('note', 'get', array(
748 'entity_table' => 'civicrm_contribution',
749 'entity_id' => $contribution['id'],
752 $this->assertEquals('my contribution note', $result['values'][0]['note']);
753 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
756 public function testCreateContributionWithNoteUniqueNameAliases() {
758 'contact_id' => $this->_individualId
,
759 'receive_date' => '2012-01-01',
760 'total_amount' => 100.00,
761 'financial_type_id' => $this->_financialTypeId
,
762 'payment_instrument_id' => 1,
763 'non_deductible_amount' => 10.00,
764 'fee_amount' => 50.00,
765 'net_amount' => 90.00,
767 'invoice_id' => 67890,
769 'contribution_status_id' => 1,
770 'contribution_note' => 'my contribution note',
773 $contribution = $this->callAPISuccess('contribution', 'create', $params);
774 $result = $this->callAPISuccess('note', 'get', array(
775 'entity_table' => 'civicrm_contribution',
776 'entity_id' => $contribution['id'],
779 $this->assertEquals('my contribution note', $result['values'][0]['note']);
780 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
784 * This is the test for creating soft credits.
786 public function testCreateContributionWithSoftCredit() {
787 $description = "Demonstrates creating contribution with SoftCredit.";
788 $subfile = "ContributionCreateWithSoftCredit";
789 $contact2 = $this->callAPISuccess('Contact', 'create', array(
790 'display_name' => 'superman',
791 'contact_type' => 'Individual',
794 'contact_id' => $contact2['id'],
796 'soft_credit_type_id' => 3,
799 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
800 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
801 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
803 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
804 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
805 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
807 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
808 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
811 public function testCreateContributionWithSoftCreditDefaults() {
812 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
813 $subfile = "ContributionCreateWithSoftCreditDefaults";
814 $contact2 = $this->callAPISuccess('Contact', 'create', array(
815 'display_name' => 'superman',
816 'contact_type' => 'Individual',
818 $params = $this->_params +
array(
819 'soft_credit_to' => $contact2['id'],
821 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
822 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
824 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
825 // Default soft credit amount = contribution.total_amount
826 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
827 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
829 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
830 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
834 * Test creating contribution with Soft Credit by passing in honor_contact_id.
836 public function testCreateContributionWithHonoreeContact() {
837 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
838 $subfile = "ContributionCreateWithHonoreeContact";
839 $contact2 = $this->callAPISuccess('Contact', 'create', array(
840 'display_name' => 'superman',
841 'contact_type' => 'Individual',
843 $params = $this->_params +
array(
844 'honor_contact_id' => $contact2['id'],
846 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
847 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
849 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
850 // Default soft credit amount = contribution.total_amount
851 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
852 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
853 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
854 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
856 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
857 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
861 * Test using example code.
863 public function testContributionCreateExample() {
864 //make sure at least on page exists since there is a truncate in tear down
865 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
866 require_once 'api/v3/examples/Contribution/Create.php';
867 $result = contribution_create_example();
869 $expectedResult = contribution_create_expectedresult();
870 $this->checkArrayEquals($expectedResult, $result);
871 $this->contributionDelete($id);
875 * Function tests that additional financial records are created when fee amount is recorded.
877 public function testCreateContributionWithFee() {
879 'contact_id' => $this->_individualId
,
880 'receive_date' => '20120511',
881 'total_amount' => 100.00,
883 'financial_type_id' => 1,
885 'invoice_id' => 67890,
887 'contribution_status_id' => 1,
890 $contribution = $this->callAPISuccess('contribution', 'create', $params);
891 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
892 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
893 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
894 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
895 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
896 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
897 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
898 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
899 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
901 $lineItems = $this->callAPISuccess('line_item', 'get', array(
903 'entity_id' => $contribution['id'],
904 'entity_table' => 'civicrm_contribution',
907 $this->assertEquals(1, $lineItems['count']);
908 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
909 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
910 $lineItems = $this->callAPISuccess('line_item', 'get', array(
912 'entity_id' => $contribution['id'],
913 'contribution_id' => $contribution['id'],
914 'entity_table' => 'civicrm_contribution',
917 $this->assertEquals(1, $lineItems['count']);
918 $this->_checkFinancialRecords($contribution, 'feeAmount');
923 * Function tests that additional financial records are created when online contribution is created.
925 public function testCreateContributionOnline() {
926 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
927 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
928 $this->assertAPISuccess($contributionPage);
930 'contact_id' => $this->_individualId
,
931 'receive_date' => '20120511',
932 'total_amount' => 100.00,
933 'financial_type_id' => 1,
934 'contribution_page_id' => $contributionPage['id'],
935 'payment_processor' => $this->paymentProcessorID
,
937 'invoice_id' => 67890,
939 'contribution_status_id' => 1,
943 $contribution = $this->callAPISuccess('contribution', 'create', $params);
944 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
945 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
946 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
947 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
948 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
949 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
950 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
951 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
952 'id' => $this->paymentProcessorID
,
953 'return' => 'payment_instrument_id',
955 $this->_checkFinancialRecords($contribution, 'online');
959 * Check handling of financial type.
961 * In the interests of removing financial type / contribution type checks from
962 * legacy format function lets test that the api is doing this for us
964 public function testCreateInvalidFinancialType() {
965 $params = $this->_params
;
966 $params['financial_type_id'] = 99999;
967 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
971 * Check handling of financial type.
973 * In the interests of removing financial type / contribution type checks from
974 * legacy format function lets test that the api is doing this for us
976 public function testValidNamedFinancialType() {
977 $params = $this->_params
;
978 $params['financial_type_id'] = 'Donation';
979 $this->callAPISuccess($this->_entity
, 'create', $params);
983 * Tests that additional financial records are created.
985 * Checks when online contribution with pay later option is created
987 public function testCreateContributionPayLaterOnline() {
988 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
989 $this->_pageParams
['is_pay_later'] = 1;
990 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
991 $this->assertAPISuccess($contributionPage);
993 'contact_id' => $this->_individualId
,
994 'receive_date' => '20120511',
995 'total_amount' => 100.00,
996 'financial_type_id' => 1,
997 'contribution_page_id' => $contributionPage['id'],
1000 'invoice_id' => 67890,
1002 'contribution_status_id' => 2,
1006 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1007 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1008 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1009 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1010 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1011 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1012 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1013 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1014 $this->_checkFinancialRecords($contribution, 'payLater');
1018 * Function tests that additional financial records are created for online contribution with pending option.
1020 public function testCreateContributionPendingOnline() {
1021 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1022 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1023 $this->assertAPISuccess($contributionPage);
1025 'contact_id' => $this->_individualId
,
1026 'receive_date' => '20120511',
1027 'total_amount' => 100.00,
1028 'financial_type_id' => 1,
1029 'contribution_page_id' => $contributionPage['id'],
1031 'invoice_id' => 67890,
1033 'contribution_status_id' => 2,
1036 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1037 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1038 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1039 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1040 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1041 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1042 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1043 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1044 $this->_checkFinancialRecords($contribution, 'pending');
1048 * Test that BAO defaults work.
1050 public function testCreateBAODefaults() {
1051 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1052 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1053 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1054 'id' => $contribution['id'],
1055 'api.contribution.delete' => 1,
1057 $this->assertEquals(1, $contribution['contribution_status_id']);
1058 $this->assertEquals('Check', $contribution['payment_instrument']);
1062 * Function tests that line items, financial records are updated when contribution amount is changed.
1064 public function testCreateUpdateContributionChangeTotal() {
1065 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1066 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1068 'entity_id' => $contribution['id'],
1069 'entity_table' => 'civicrm_contribution',
1071 'return' => 'line_total',
1073 $this->assertEquals('100.00', $lineItems);
1074 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1075 // Financial trxn SUM = 100 + 5 (fee)
1076 $this->assertEquals('105.00', $trxnAmount);
1079 'id' => $contribution['id'],
1080 'total_amount' => '125',
1082 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1084 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1086 'entity_id' => $contribution['id'],
1087 'entity_table' => 'civicrm_contribution',
1089 'return' => 'line_total',
1092 $this->assertEquals('125.00', $lineItems);
1093 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1095 // Financial trxn SUM = 125 + 5 (fee).
1096 $this->assertEquals('130.00', $trxnAmount);
1097 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1101 * Function tests that line items, financial records are updated when pay later contribution is received.
1103 public function testCreateUpdateContributionPayLater() {
1104 $contribParams = array(
1105 'contact_id' => $this->_individualId
,
1106 'receive_date' => '2012-01-01',
1107 'total_amount' => 100.00,
1108 'financial_type_id' => $this->_financialTypeId
,
1109 'payment_instrument_id' => 1,
1110 'contribution_status_id' => 2,
1111 'is_pay_later' => 1,
1114 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1116 $newParams = array_merge($contribParams, array(
1117 'id' => $contribution['id'],
1118 'contribution_status_id' => 1,
1121 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1122 $contribution = $contribution['values'][$contribution['id']];
1123 $this->assertEquals($contribution['contribution_status_id'], '1');
1124 $this->_checkFinancialItem($contribution['id'], 'paylater');
1125 $this->_checkFinancialTrxn($contribution, 'payLater');
1129 * Function tests that financial records are updated when Payment Instrument is changed.
1131 public function testCreateUpdateContributionPaymentInstrument() {
1132 $instrumentId = $this->_addPaymentInstrument();
1133 $contribParams = array(
1134 'contact_id' => $this->_individualId
,
1135 'total_amount' => 100.00,
1136 'financial_type_id' => $this->_financialTypeId
,
1137 'payment_instrument_id' => 4,
1138 'contribution_status_id' => 1,
1141 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1143 $newParams = array_merge($contribParams, array(
1144 'id' => $contribution['id'],
1145 'payment_instrument_id' => $instrumentId,
1148 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1149 $this->assertAPISuccess($contribution);
1150 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1152 // cleanup - delete created payment instrument
1153 $this->_deletedAddedPaymentInstrument();
1157 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1159 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1160 $instrumentId = $this->_addPaymentInstrument();
1161 $contribParams = array(
1162 'contact_id' => $this->_individualId
,
1163 'total_amount' => -100.00,
1164 'financial_type_id' => $this->_financialTypeId
,
1165 'payment_instrument_id' => 4,
1166 'contribution_status_id' => 1,
1169 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1171 $newParams = array_merge($contribParams, array(
1172 'id' => $contribution['id'],
1173 'payment_instrument_id' => $instrumentId,
1176 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1177 $this->assertAPISuccess($contribution);
1178 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1180 // cleanup - delete created payment instrument
1181 $this->_deletedAddedPaymentInstrument();
1185 * Function tests that financial records are added when Contribution is Refunded.
1187 public function testCreateUpdateContributionRefund() {
1188 $contributionParams = array(
1189 'contact_id' => $this->_individualId
,
1190 'receive_date' => '2012-01-01',
1191 'total_amount' => 100.00,
1192 'financial_type_id' => $this->_financialTypeId
,
1193 'payment_instrument_id' => 4,
1194 'contribution_status_id' => 1,
1195 'trxn_id' => 'original_payment',
1197 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1198 $newParams = array_merge($contributionParams, array(
1199 'id' => $contribution['id'],
1200 'contribution_status_id' => 'Refunded',
1201 'cancel_date' => '2015-01-01 09:00',
1202 'refund_trxn_id' => 'the refund',
1206 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1207 $this->_checkFinancialTrxn($contribution, 'refund');
1208 $this->_checkFinancialItem($contribution['id'], 'refund');
1209 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1210 'id' => $contribution['id'],
1211 'return' => 'trxn_id',
1216 * Refund a contribution for a financial type with a contra account.
1218 * CRM-17951 the contra account is a financial account with a relationship to a
1219 * financial type. It is not always configured but should be reflected
1220 * in the financial_trxn & financial_item table if it is.
1222 public function testCreateUpdateChargebackContributionDefaultAccount() {
1223 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1224 $this->callAPISuccess('Contribution', 'create', array(
1225 'id' => $contribution['id'],
1226 'contribution_status_id' => 'Chargeback',
1228 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1230 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1231 'contribution_id' => $contribution['id'],
1232 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1234 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1235 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1236 'total_amount' => -100,
1237 'status_id' => 'Chargeback',
1238 'to_financial_account_id' => 6,
1243 * Refund a contribution for a financial type with a contra account.
1245 * CRM-17951 the contra account is a financial account with a relationship to a
1246 * financial type. It is not always configured but should be reflected
1247 * in the financial_trxn & financial_item table if it is.
1249 public function testCreateUpdateChargebackContributionCustomAccount() {
1250 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1251 'name' => 'Chargeback Account',
1252 'is_active' => TRUE,
1255 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1256 'entity_id' => $this->_financialTypeId
,
1257 'entity_table' => 'civicrm_financial_type',
1258 'account_relationship' => 'Chargeback Account is',
1259 'financial_account_id' => 'Chargeback Account',
1262 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1263 $this->callAPISuccess('Contribution', 'create', array(
1264 'id' => $contribution['id'],
1265 'contribution_status_id' => 'Chargeback',
1267 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1269 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1270 'contribution_id' => $contribution['id'],
1271 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1273 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1275 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1276 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1277 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1281 * Refund a contribution for a financial type with a contra account.
1283 * CRM-17951 the contra account is a financial account with a relationship to a
1284 * financial type. It is not always configured but should be reflected
1285 * in the financial_trxn & financial_item table if it is.
1287 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1288 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1289 'name' => 'Refund Account',
1290 'is_active' => TRUE,
1293 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1294 'entity_id' => $this->_financialTypeId
,
1295 'entity_table' => 'civicrm_financial_type',
1296 'account_relationship' => 'Credit/Contra Revenue Account is',
1297 'financial_account_id' => 'Refund Account',
1300 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1301 $this->callAPISuccess('Contribution', 'create', array(
1302 'id' => $contribution['id'],
1303 'contribution_status_id' => 'Refunded',
1306 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1307 'contribution_id' => $contribution['id'],
1308 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1310 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1312 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1313 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1314 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1318 * Function tests that trxn_id is set when passed in.
1320 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1321 * when trxn_id is passed in.
1323 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1324 $contributionParams = array(
1325 'contact_id' => $this->_individualId
,
1326 'receive_date' => '2012-01-01',
1327 'total_amount' => 100.00,
1328 'financial_type_id' => $this->_financialTypeId
,
1329 'payment_instrument_id' => 4,
1330 'contribution_status_id' => 1,
1331 'trxn_id' => 'original_payment',
1333 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1334 $newParams = array_merge($contributionParams, array(
1335 'id' => $contribution['id'],
1336 'contribution_status_id' => 'Refunded',
1337 'cancel_date' => '2015-01-01 09:00',
1338 'trxn_id' => 'the refund',
1342 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1343 $this->_checkFinancialTrxn($contribution, 'refund');
1344 $this->_checkFinancialItem($contribution['id'], 'refund');
1345 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1346 'id' => $contribution['id'],
1347 'return' => 'trxn_id',
1352 * Function tests that trxn_id is set when passed in.
1354 * Here we ensure that the civicrm_contribution.trxn_id is set
1355 * when trxn_id is passed in but if refund_trxn_id is different then that
1356 * is kept for the refund transaction.
1358 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1359 $contributionParams = array(
1360 'contact_id' => $this->_individualId
,
1361 'receive_date' => '2012-01-01',
1362 'total_amount' => 100.00,
1363 'financial_type_id' => $this->_financialTypeId
,
1364 'payment_instrument_id' => 4,
1365 'contribution_status_id' => 1,
1366 'trxn_id' => 'original_payment',
1368 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1369 $newParams = array_merge($contributionParams, array(
1370 'id' => $contribution['id'],
1371 'contribution_status_id' => 'Refunded',
1372 'cancel_date' => '2015-01-01 09:00',
1373 'trxn_id' => 'cont id',
1374 'refund_trxn_id' => 'the refund',
1378 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1379 $this->_checkFinancialTrxn($contribution, 'refund');
1380 $this->_checkFinancialItem($contribution['id'], 'refund');
1381 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1382 'id' => $contribution['id'],
1383 'return' => 'trxn_id',
1388 * Function tests that refund_trxn_id is set when passed in empty.
1390 * Here we ensure that the civicrm_contribution.trxn_id is set
1391 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1392 * is kept for the refund transaction.
1394 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1395 $contributionParams = array(
1396 'contact_id' => $this->_individualId
,
1397 'receive_date' => '2012-01-01',
1398 'total_amount' => 100.00,
1399 'financial_type_id' => $this->_financialTypeId
,
1400 'payment_instrument_id' => 4,
1401 'contribution_status_id' => 1,
1402 'trxn_id' => 'original_payment',
1404 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1405 $newParams = array_merge($contributionParams, array(
1406 'id' => $contribution['id'],
1407 'contribution_status_id' => 'Refunded',
1408 'cancel_date' => '2015-01-01 09:00',
1409 'trxn_id' => 'cont id',
1410 'refund_trxn_id' => '',
1414 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1415 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1416 $this->_checkFinancialItem($contribution['id'], 'refund');
1417 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1418 'id' => $contribution['id'],
1419 'return' => 'trxn_id',
1424 * Function tests invalid contribution status change.
1426 public function testCreateUpdateContributionInValidStatusChange() {
1427 $contribParams = array(
1429 'receive_date' => '2012-01-01',
1430 'total_amount' => 100.00,
1431 'financial_type_id' => 1,
1432 'payment_instrument_id' => 1,
1433 'contribution_status_id' => 1,
1435 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1436 $newParams = array_merge($contribParams, array(
1437 'id' => $contribution['id'],
1438 'contribution_status_id' => 2,
1441 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1446 * Function tests that financial records are added when Pending Contribution is Canceled.
1448 public function testCreateUpdateContributionCancelPending() {
1449 $contribParams = array(
1450 'contact_id' => $this->_individualId
,
1451 'receive_date' => '2012-01-01',
1452 'total_amount' => 100.00,
1453 'financial_type_id' => $this->_financialTypeId
,
1454 'payment_instrument_id' => 1,
1455 'contribution_status_id' => 2,
1456 'is_pay_later' => 1,
1459 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1460 $newParams = array_merge($contribParams, array(
1461 'id' => $contribution['id'],
1462 'contribution_status_id' => 3,
1463 'cancel_date' => '2012-02-02 09:00',
1466 //Check if trxn_date is same as cancel_date.
1467 $checkTrxnDate = array(
1468 'trxn_date' => '2012-02-02 09:00:00',
1470 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1471 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1472 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1476 * Function tests that financial records are added when Financial Type is Changed.
1478 public function testCreateUpdateContributionChangeFinancialType() {
1479 $contribParams = array(
1480 'contact_id' => $this->_individualId
,
1481 'receive_date' => '2012-01-01',
1482 'total_amount' => 100.00,
1483 'financial_type_id' => 1,
1484 'payment_instrument_id' => 1,
1485 'contribution_status_id' => 1,
1488 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1489 $newParams = array_merge($contribParams, array(
1490 'id' => $contribution['id'],
1491 'financial_type_id' => 3,
1494 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1495 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1496 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1500 * Test that update does not change status id CRM-15105.
1502 public function testCreateUpdateWithoutChangingPendingStatus() {
1503 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1504 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1505 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1506 'id' => $contribution['id'],
1507 'api.contribution.delete' => 1,
1509 $this->assertEquals(2, $contribution['contribution_status_id']);
1513 * Test Updating a Contribution.
1515 * CHANGE: we require the API to do an incremental update
1517 public function testCreateUpdateContribution() {
1518 $contributionID = $this->contributionCreate(array(
1519 'contact_id' => $this->_individualId
,
1520 'trxn_id' => 212355,
1521 'financial_type_id' => $this->_financialTypeId
,
1522 'invoice_id' => 'old_invoice',
1524 $old_params = array(
1525 'contribution_id' => $contributionID,
1527 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1528 $this->assertEquals($original['id'], $contributionID);
1529 //set up list of old params, verify
1531 //This should not be required on update:
1532 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1533 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1534 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1535 $old_source = $original['values'][$contributionID]['contribution_source'];
1537 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1538 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1540 //check against values in CiviUnitTestCase::createContribution()
1541 $this->assertEquals($old_contact_id, $this->_individualId
);
1542 $this->assertEquals($old_fee_amount, 5.00);
1543 $this->assertEquals($old_source, 'SSF');
1544 $this->assertEquals($old_trxn_id, 212355);
1545 $this->assertEquals($old_invoice_id, 'old_invoice');
1547 'id' => $contributionID,
1548 'contact_id' => $this->_individualId
,
1549 'total_amount' => 105.00,
1550 'fee_amount' => 7.00,
1551 'financial_type_id' => $this->_financialTypeId
,
1552 'non_deductible_amount' => 22.00,
1553 'contribution_status_id' => 1,
1554 'note' => 'Donating for Noble Cause',
1557 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1559 $new_params = array(
1560 'contribution_id' => $contribution['id'],
1563 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1565 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1566 $this->assertEquals($contribution['total_amount'], 105.00);
1567 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1568 $this->assertEquals($contribution['financial_type'], 'Donation');
1569 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1570 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1571 $this->assertEquals($contribution['fee_amount'], 7.00);
1572 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1573 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1574 $this->assertEquals($contribution['contribution_source'], $old_source);
1575 $this->assertEquals($contribution['contribution_status'], 'Completed');
1577 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1580 'contribution_id' => $contributionID,
1583 $result = $this->callAPISuccess('contribution', 'delete', $params);
1584 $this->assertAPISuccess($result);
1588 * Attempt (but fail) to delete a contribution without parameters.
1590 public function testDeleteEmptyParamsContribution() {
1592 $this->callAPIFailure('contribution', 'delete', $params);
1595 public function testDeleteParamsNotArrayContribution() {
1596 $params = 'contribution_id= 1';
1597 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1598 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1601 public function testDeleteWrongParamContribution() {
1603 'contribution_source' => 'SSF',
1606 $this->callAPIFailure('contribution', 'delete', $params);
1609 public function testDeleteContribution() {
1610 $contributionID = $this->contributionCreate(array(
1611 'contact_id' => $this->_individualId
,
1612 'financial_type_id' => $this->_financialTypeId
,
1615 'id' => $contributionID,
1617 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1621 * Test civicrm_contribution_search with empty params.
1623 * All available contributions expected.
1625 public function testSearchEmptyParams() {
1629 'contact_id' => $this->_individualId
,
1630 'receive_date' => date('Ymd'),
1631 'total_amount' => 100.00,
1632 'financial_type_id' => $this->_financialTypeId
,
1633 'non_deductible_amount' => 10.00,
1634 'fee_amount' => 5.00,
1635 'net_amount' => 95.00,
1637 'invoice_id' => 78910,
1639 'contribution_status_id' => 1,
1642 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1644 $result = $this->callAPISuccess('contribution', 'get', $params);
1645 // We're taking the first element.
1646 $res = $result['values'][$contribution['id']];
1648 $this->assertEquals($p['contact_id'], $res['contact_id']);
1649 $this->assertEquals($p['total_amount'], $res['total_amount']);
1650 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1651 $this->assertEquals($p['net_amount'], $res['net_amount']);
1652 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1653 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1654 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1655 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1656 $this->assertEquals($p['source'], $res['contribution_source']);
1657 // contribution_status_id = 1 => Completed
1658 $this->assertEquals('Completed', $res['contribution_status']);
1660 $this->contributionDelete($contribution['id']);
1664 * Test civicrm_contribution_search. Success expected.
1666 public function testSearch() {
1668 'contact_id' => $this->_individualId
,
1669 'receive_date' => date('Ymd'),
1670 'total_amount' => 100.00,
1671 'financial_type_id' => $this->_financialTypeId
,
1672 'non_deductible_amount' => 10.00,
1673 'contribution_status_id' => 1,
1676 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1679 'contact_id' => $this->_individualId
,
1680 'receive_date' => date('Ymd'),
1681 'total_amount' => 200.00,
1682 'financial_type_id' => $this->_financialTypeId
,
1683 'non_deductible_amount' => 20.00,
1684 'trxn_id' => 5454565,
1685 'invoice_id' => 1212124,
1686 'fee_amount' => 50.00,
1687 'net_amount' => 60.00,
1688 'contribution_status_id' => 2,
1691 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1694 'contribution_id' => $contribution2['id'],
1697 $result = $this->callAPISuccess('contribution', 'get', $params);
1698 $res = $result['values'][$contribution2['id']];
1700 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1701 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1702 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1703 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1704 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1705 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1706 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1707 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1708 // contribution_status_id = 2 => Pending
1709 $this->assertEquals('Pending', $res['contribution_status']);
1711 $this->contributionDelete($contribution1['id']);
1712 $this->contributionDelete($contribution2['id']);
1716 * Test completing a transaction via the API.
1718 * Note that we are creating a logged in user because email goes out from
1721 public function testCompleteTransaction() {
1722 $mut = new CiviMailUtils($this, TRUE);
1723 $this->swapMessageTemplateForTestTemplate();
1724 $this->createLoggedInUser();
1725 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1726 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1727 $this->callAPISuccess('contribution', 'completetransaction', array(
1728 'id' => $contribution['id'],
1730 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1731 $this->assertEquals('SSF', $contribution['contribution_source']);
1732 $this->assertEquals('Completed', $contribution['contribution_status']);
1733 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1734 $mut->checkMailLog(array(
1735 'email:::anthony_anderson@civicrm.org',
1739 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1740 "receipt_date:::\n",
1741 'contributeMode:::notify',
1742 'title:::Contribution',
1743 'displayName:::Mr. Anthony Anderson II',
1744 'contributionStatus:::Completed',
1747 $this->revertTemplateToReservedTemplate();
1751 * Test completing a transaction via the API with a non-USD transaction.
1753 public function testCompleteTransactionEuro() {
1754 $mut = new CiviMailUtils($this, TRUE);
1755 $this->swapMessageTemplateForTestTemplate();
1756 $this->createLoggedInUser();
1757 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1758 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1760 $this->callAPISuccess('contribution', 'completetransaction', array(
1761 'id' => $contribution['id'],
1764 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1765 $this->assertEquals('SSF', $contribution['contribution_source']);
1766 $this->assertEquals('Completed', $contribution['contribution_status']);
1767 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1769 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1770 $entityFinancialTransaction = reset($entityFinancialTransactions);
1771 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1772 $this->assertEquals('EUR', $financialTrxn['currency']);
1774 $mut->checkMailLog(array(
1775 'email:::anthony_anderson@civicrm.org',
1779 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1780 "receipt_date:::\n",
1781 'contributeMode:::notify',
1782 'title:::Contribution',
1783 'displayName:::Mr. Anthony Anderson II',
1784 'contributionStatus:::Completed',
1787 $this->revertTemplateToReservedTemplate();
1791 * Test to ensure mail is sent on chosing pay later
1793 public function testpayLater() {
1794 $mut = new CiviMailUtils($this, TRUE);
1795 $this->swapMessageTemplateForTestTemplate();
1796 $this->createLoggedInUser();
1798 // create contribution page first
1799 $contributionPageParams = array(
1800 'title' => 'Help Support CiviCRM!',
1801 'financial_type_id' => 1,
1802 'is_monetary' => TRUE,
1803 'is_pay_later' => 1,
1804 'is_quick_config' => TRUE,
1805 'pay_later_text' => 'I will send payment by check',
1806 'pay_later_receipt' => 'This is a pay later reciept',
1807 'is_allow_other_amount' => 1,
1808 'min_amount' => 10.00,
1809 'max_amount' => 10000.00,
1810 'goal_amount' => 100000.00,
1811 'is_email_receipt' => 1,
1813 'amount_block_is_active' => 1,
1814 'currency' => 'USD',
1815 'is_billing_required' => 0,
1817 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1819 // submit form values
1820 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1822 'id' => $contributionPageResult['id'],
1823 'contact_id' => $this->_individualId
,
1824 'email-5' => 'anthony_anderson@civicrm.org',
1825 'payment_processor_id' => 0,
1828 'currencyID' => 'USD',
1829 'is_pay_later' => 1,
1830 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1831 'is_quick_config' => 1,
1832 'description' => 'Online Contribution: Help Support CiviCRM!',
1833 'price_set_id' => $priceSet['id'],
1835 $this->callAPISuccess('contribution_page', 'submit', $params);
1837 $mut->checkMailLog(array(
1839 'email:::anthony_anderson@civicrm.org',
1840 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1841 'displayName:::Mr. Anthony Anderson II',
1842 'contributionPageId:::' . $contributionPageResult['id'],
1843 'title:::' . $contributionPageParams['title'],
1844 'amount:::' . $params['amount'],
1847 $this->revertTemplateToReservedTemplate();
1851 * Test to check whether contact billing address is used when no contribution address
1853 public function testBillingAddress() {
1854 $mut = new CiviMailUtils($this, TRUE);
1855 $this->swapMessageTemplateForTestTemplate();
1856 $this->createLoggedInUser();
1858 //Scenario 1: When Contact don't have any address
1859 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1860 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1861 $this->callAPISuccess('contribution', 'completetransaction', array(
1862 'id' => $contribution['id'],
1864 $mut->checkMailLog(array(
1868 // Scenario 2: Contribution using address
1869 $address = $this->callAPISuccess('address', 'create', array(
1870 'street_address' => 'contribution billing st',
1871 'location_type_id' => 2,
1872 'contact_id' => $this->_params
['contact_id'],
1874 $params = array_merge($this->_params
, array(
1875 'contribution_status_id' => 2,
1876 'address_id' => $address['id'],
1879 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1880 $this->callAPISuccess('contribution', 'completetransaction', array(
1881 'id' => $contribution['id'],
1883 $mut->checkMailLog(array(
1884 'address:::contribution billing st',
1887 // Scenario 3: Contribution wtth no address but contact has a billing address
1888 $this->callAPISuccess('address', 'create', array(
1889 'id' => $address['id'],
1890 'street_address' => 'is billing st',
1891 'contact_id' => $this->_params
['contact_id'],
1893 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1894 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1895 $this->callAPISuccess('contribution', 'completetransaction', array(
1896 'id' => $contribution['id'],
1898 $mut->checkMailLog(array(
1899 'address:::is billing st',
1903 $this->revertTemplateToReservedTemplate();
1907 * Test completing a transaction via the API.
1909 * Note that we are creating a logged in user because email goes out from
1912 public function testCompleteTransactionFeeAmount() {
1913 $this->createLoggedInUser();
1914 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1915 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1916 $this->callAPISuccess('contribution', 'completetransaction', array(
1917 'id' => $contribution['id'],
1918 'fee_amount' => '.56',
1919 'trxn_id' => '7778888',
1921 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1922 $this->assertEquals('Completed', $contribution['contribution_status']);
1923 $this->assertEquals('7778888', $contribution['trxn_id']);
1924 $this->assertEquals('.56', $contribution['fee_amount']);
1925 $this->assertEquals('99.44', $contribution['net_amount']);
1929 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed
1931 public function testCheckTaxAmount() {
1932 $contact = $this->createLoggedInUser();
1933 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1934 'name' => 'Test taxable financial Type',
1938 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1939 'name' => 'Test Tax financial account ',
1940 'contact_id' => $contact,
1941 'financial_account_type_id' => 2,
1948 $financialTypeId = $financialType['id'];
1949 $financialAccountId = $financialAccount['id'];
1950 $financialAccountParams = array(
1951 'entity_table' => 'civicrm_financial_type',
1952 'entity_id' => $financialTypeId,
1953 'account_relationship' => 10,
1954 'financial_account_id' => $financialAccountId,
1956 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
1958 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1959 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1960 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1961 $this->callAPISuccess('contribution', 'completetransaction', array(
1962 'id' => $contribution['id'],
1963 'trxn_id' => '777788888',
1964 'fee_amount' => '6.00',
1966 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
1967 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
1968 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
1969 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
1973 * Test repeat contribution successfully creates line item.
1975 public function testRepeatTransaction() {
1976 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
1977 $this->callAPISuccess('contribution', 'repeattransaction', array(
1978 'original_contribution_id' => $originalContribution['id'],
1979 'contribution_status_id' => 'Completed',
1980 'trxn_id' => uniqid(),
1982 $lineItemParams = array(
1983 'entity_id' => $originalContribution['id'],
1991 'financial_type_id',
1992 'deductible_amount',
1993 'price_field_value_id',
1997 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1998 'entity_id' => $originalContribution['id'],
2000 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2001 'entity_id' => $originalContribution['id'] +
1,
2003 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2004 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2005 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2006 $this->_checkFinancialRecords(array(
2007 'id' => $originalContribution['id'] +
1,
2008 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2009 'id' => $originalContribution['payment_processor_id'],
2010 'return' => 'payment_instrument_id',
2013 $this->quickCleanUpFinancialEntities();
2017 * Test repeat contribution successfully creates line items (plural).
2019 public function testRepeatTransactionLineItems() {
2021 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2022 $this->callAPISuccess('contribution', 'repeattransaction', array(
2023 'original_contribution_id' => $originalContribution['id'],
2024 'contribution_status_id' => 'Completed',
2025 'trxn_id' => uniqid(),
2028 $lineItemParams = array(
2029 'entity_id' => $originalContribution['id'],
2037 'financial_type_id',
2038 'deductible_amount',
2039 'price_field_value_id',
2043 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2044 'entity_id' => $originalContribution['id'],
2046 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2047 'entity_id' => $originalContribution['id'] +
1,
2050 // unset id and entity_id for all of them to be able to compare the lineItems:
2051 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2052 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2053 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2055 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2056 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2057 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2059 // CRM-19309 so in future we also want to:
2060 // check that financial_line_items have been created for entity_id 3 and 4;
2062 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2063 $this->quickCleanUpFinancialEntities();
2067 * Test repeat contribution successfully creates is_test transaction.
2069 public function testRepeatTransactionIsTest() {
2070 $this->_params
['is_test'] = 1;
2071 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2073 $this->callAPISuccess('contribution', 'repeattransaction', array(
2074 'original_contribution_id' => $originalContribution['id'],
2075 'contribution_status_id' => 'Completed',
2076 'trxn_id' => uniqid(),
2078 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2082 * Test repeat contribution passed in status.
2084 public function testRepeatTransactionPassedInStatus() {
2085 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2087 $this->callAPISuccess('contribution', 'repeattransaction', array(
2088 'original_contribution_id' => $originalContribution['id'],
2089 'contribution_status_id' => 'Pending',
2090 'trxn_id' => uniqid(),
2092 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2096 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2098 public function testRepeatTransactionAcceptRecurID() {
2099 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2100 'contact_id' => $this->_individualId
,
2101 'installments' => '12',
2102 'frequency_interval' => '1',
2104 'contribution_status_id' => 1,
2105 'start_date' => '2012-01-01 00:00:00',
2106 'currency' => 'USD',
2107 'frequency_unit' => 'month',
2108 'payment_processor_id' => $this->paymentProcessorID
,
2110 $this->callAPISuccess('contribution', 'create', array_merge(
2112 array('contribution_recur_id' => $contributionRecur['id']))
2115 $this->callAPISuccess('contribution', 'repeattransaction', array(
2116 'contribution_recur_id' => $contributionRecur['id'],
2117 'contribution_status_id' => 'Completed',
2118 'trxn_id' => uniqid(),
2121 $this->quickCleanUpFinancialEntities();
2125 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2127 public function testRepeatTransactionTestRecurId() {
2128 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2129 'contact_id' => $this->_individualId
,
2130 'frequency_interval' => '1',
2132 'contribution_status_id' => 1,
2133 'start_date' => '2017-01-01 00:00:00',
2134 'currency' => 'USD',
2135 'frequency_unit' => 'month',
2136 'payment_processor_id' => $this->paymentProcessorID
,
2139 $this->callAPISuccess('contribution', 'create', array_merge(
2142 'contribution_recur_id' => $contributionRecur['id'],
2147 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2148 'contribution_recur_id' => $contributionRecur['id'],
2149 'contribution_status_id' => 'Completed',
2150 'trxn_id' => uniqid(),
2153 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2154 $this->quickCleanUpFinancialEntities();
2157 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2159 * ( Tests membership is renewed after repeattransaction. )
2161 public function testRepeatTransactionUsesCompleted() {
2162 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2164 $this->callAPISuccess('contribution', 'create', array(
2165 'contact_id' => $originalContribution['values'][1]['contact_id'],
2166 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2167 'total_amount' => $originalContribution['values'][1]['total_amount'],
2168 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2169 'contribution_status_id' => "Failed",
2172 $this->callAPISuccess('membership', 'create', array(
2173 'id' => $membership['id'],
2174 'end_date' => 'yesterday',
2178 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2179 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2180 'contribution_status_id' => 'Completed',
2181 'trxn_id' => 'bobsled',
2184 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2185 'id' => $membership['id'],
2186 'return' => 'status_id',
2189 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2191 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2192 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2193 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2194 $this->quickCleanUpFinancialEntities();
2195 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2199 * CRM-16397 test appropriate action if total amount has changed for single line items.
2201 public function testRepeatTransactionAlteredAmount() {
2202 $paymentProcessorID = $this->paymentProcessorCreate();
2203 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2204 'contact_id' => $this->_individualId
,
2205 'installments' => '12',
2206 'frequency_interval' => '1',
2208 'contribution_status_id' => 1,
2209 'start_date' => '2012-01-01 00:00:00',
2210 'currency' => 'USD',
2211 'frequency_unit' => 'month',
2212 'payment_processor_id' => $paymentProcessorID,
2214 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2217 'contribution_recur_id' => $contributionRecur['id'],
2221 $this->callAPISuccess('contribution', 'repeattransaction', array(
2222 'original_contribution_id' => $originalContribution['id'],
2223 'contribution_status_id' => 'Completed',
2224 'trxn_id' => uniqid(),
2225 'total_amount' => '400',
2229 $lineItemParams = array(
2230 'entity_id' => $originalContribution['id'],
2238 'financial_type_id',
2239 'deductible_amount',
2240 'price_field_value_id',
2244 $this->callAPISuccessGetSingle('contribution', array(
2245 'total_amount' => 400,
2247 'net_amount' => 350,
2249 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2250 'entity_id' => $originalContribution['id'],
2252 $expectedLineItem = array_merge(
2253 $lineItem1['values'][0], array(
2254 'line_total' => '400.00',
2255 'unit_price' => '400.00',
2259 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2260 'entity_id' => $originalContribution['id'] +
1,
2263 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2264 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2265 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2269 * CRM-17718 test appropriate action if financial type has changed for single line items.
2271 public function testRepeatTransactionPassedInFinancialType() {
2272 $originalContribution = $this->setUpRecurringContribution();
2274 $this->callAPISuccess('contribution', 'repeattransaction', array(
2275 'original_contribution_id' => $originalContribution['id'],
2276 'contribution_status_id' => 'Completed',
2277 'trxn_id' => uniqid(),
2278 'financial_type_id' => 2,
2280 $lineItemParams = array(
2281 'entity_id' => $originalContribution['id'],
2289 'financial_type_id',
2290 'deductible_amount',
2291 'price_field_value_id',
2296 $this->callAPISuccessGetSingle('contribution', array(
2297 'total_amount' => 100,
2298 'financial_type_id' => 2,
2300 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2301 'entity_id' => $originalContribution['id'],
2303 $expectedLineItem = array_merge(
2304 $lineItem1['values'][0], array(
2305 'line_total' => '100.00',
2306 'unit_price' => '100.00',
2307 'financial_type_id' => 2,
2308 'contribution_type_id' => 2,
2311 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2312 'entity_id' => $originalContribution['id'] +
1,
2314 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2315 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2316 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2320 * CRM-17718 test appropriate action if financial type has changed for single line items.
2322 public function testRepeatTransactionUpdatedFinancialType() {
2323 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2325 $this->callAPISuccess('contribution', 'repeattransaction', array(
2326 'contribution_recur_id' => $originalContribution['id'],
2327 'contribution_status_id' => 'Completed',
2328 'trxn_id' => uniqid(),
2330 $lineItemParams = array(
2331 'entity_id' => $originalContribution['id'],
2339 'financial_type_id',
2340 'deductible_amount',
2341 'price_field_value_id',
2346 $this->callAPISuccessGetSingle('contribution', array(
2347 'total_amount' => 100,
2348 'financial_type_id' => 2,
2350 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2351 'entity_id' => $originalContribution['id'],
2353 $expectedLineItem = array_merge(
2354 $lineItem1['values'][0], array(
2355 'line_total' => '100.00',
2356 'unit_price' => '100.00',
2357 'financial_type_id' => 2,
2358 'contribution_type_id' => 2,
2362 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2363 'entity_id' => $originalContribution['id'] +
1,
2365 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2366 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2367 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2371 * CRM-16397 test appropriate action if campaign has been passed in.
2373 public function testRepeatTransactionPassedInCampaign() {
2374 $paymentProcessorID = $this->paymentProcessorCreate();
2375 $campaignID = $this->campaignCreate();
2376 $campaignID2 = $this->campaignCreate();
2377 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2378 'contact_id' => $this->_individualId
,
2379 'installments' => '12',
2380 'frequency_interval' => '1',
2382 'contribution_status_id' => 1,
2383 'start_date' => '2012-01-01 00:00:00',
2384 'currency' => 'USD',
2385 'frequency_unit' => 'month',
2386 'payment_processor_id' => $paymentProcessorID,
2388 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2391 'contribution_recur_id' => $contributionRecur['id'],
2392 'campaign_id' => $campaignID,
2396 $this->callAPISuccess('contribution', 'repeattransaction', array(
2397 'original_contribution_id' => $originalContribution['id'],
2398 'contribution_status_id' => 'Completed',
2399 'trxn_id' => uniqid(),
2400 'campaign_id' => $campaignID2,
2403 $this->callAPISuccessGetSingle('contribution', array(
2404 'total_amount' => 100,
2405 'campaign_id' => $campaignID2,
2410 * CRM-17718 campaign stored on contribution recur gets priority.
2412 * This reflects the fact we permit people to update them.
2414 public function testRepeatTransactionUpdatedCampaign() {
2415 $paymentProcessorID = $this->paymentProcessorCreate();
2416 $campaignID = $this->campaignCreate();
2417 $campaignID2 = $this->campaignCreate();
2418 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2419 'contact_id' => $this->_individualId
,
2420 'installments' => '12',
2421 'frequency_interval' => '1',
2423 'contribution_status_id' => 1,
2424 'start_date' => '2012-01-01 00:00:00',
2425 'currency' => 'USD',
2426 'frequency_unit' => 'month',
2427 'payment_processor_id' => $paymentProcessorID,
2428 'campaign_id' => $campaignID,
2430 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2433 'contribution_recur_id' => $contributionRecur['id'],
2434 'campaign_id' => $campaignID2,
2438 $this->callAPISuccess('contribution', 'repeattransaction', array(
2439 'original_contribution_id' => $originalContribution['id'],
2440 'contribution_status_id' => 'Completed',
2441 'trxn_id' => uniqid(),
2444 $this->callAPISuccessGetSingle('contribution', array(
2445 'total_amount' => 100,
2446 'campaign_id' => $campaignID,
2451 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2453 public function testCompleteTransactionNetAmountOK() {
2454 $this->createLoggedInUser();
2455 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2456 unset($params['net_amount']);
2457 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2458 $this->callAPISuccess('contribution', 'completetransaction', array(
2459 'id' => $contribution['id'],
2461 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2462 $this->assertEquals('Completed', $contribution['contribution_status']);
2463 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2467 * CRM-14151 - Test completing a transaction via the API.
2469 public function testCompleteTransactionWithReceiptDateSet() {
2470 $this->swapMessageTemplateForTestTemplate();
2471 $mut = new CiviMailUtils($this, TRUE);
2472 $this->createLoggedInUser();
2473 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2474 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2475 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2476 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2477 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2478 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2479 $mut->checkMailLog(array(
2480 'Receipt - Contribution',
2481 'receipt_date:::' . date('Ymd'),
2484 $this->revertTemplateToReservedTemplate();
2488 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2490 public function testCompleteTransactionWithEmailReceiptInput() {
2491 $contributionPage = $this->createReceiptableContributionPage();
2493 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2494 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
2495 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2496 // Complete the transaction overriding is_email_receipt to = FALSE
2497 $this->callAPISuccess('contribution', 'completetransaction', array(
2498 'id' => $contribution['id'],
2499 'trxn_date' => date('2011-04-09'),
2500 'trxn_id' => 'kazam',
2501 'is_email_receipt' => 0,
2503 // Check if a receipt was issued
2504 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2505 $this->assertEquals('', $receipt_date);
2509 * Test that $is_recur is assigned to the receipt.
2511 public function testCompleteTransactionForRecurring() {
2513 $this->swapMessageTemplateForTestTemplate();
2514 $recurring = $this->setUpRecurringContribution();
2515 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2517 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2518 $this->_params
['contribution_recur_id'] = $recurring['id'];
2520 $contribution = $this->setUpForCompleteTransaction();
2522 $this->callAPISuccess('contribution', 'completetransaction', array(
2523 'id' => $contribution['id'],
2524 'trxn_date' => date('2011-04-09'),
2525 'trxn_id' => 'kazam',
2526 'is_email_receipt' => 1,
2529 $this->mut
->checkMailLog(array(
2531 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2534 $this->revertTemplateToReservedTemplate();
2537 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2539 * If passed in it will override the default from contribution page.
2541 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2542 $mut = new CiviMailUtils($this, TRUE);
2543 $this->createLoggedInUser();
2544 // Create a Contribution Page with is_email_receipt = FALSE
2545 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2546 'receipt_from_name' => 'Mickey Mouse',
2547 'receipt_from_email' => 'mickey@mouse.com',
2548 'title' => "Test Contribution Page",
2549 'financial_type_id' => 1,
2550 'currency' => 'CAD',
2551 'is_monetary' => TRUE,
2552 'is_email_receipt' => 0,
2554 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2555 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2556 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2557 // Complete the transaction overriding is_email_receipt to = TRUE
2558 $this->callAPISuccess('contribution', 'completetransaction', array(
2559 'id' => $contribution['id'],
2560 'is_email_receipt' => 1,
2562 $mut->checkMailLog(array(
2563 'Please print this receipt for your records.',
2569 * Complete the transaction using the template with all the possible.
2571 public function testCompleteTransactionWithTestTemplate() {
2572 $this->swapMessageTemplateForTestTemplate();
2573 $contribution = $this->setUpForCompleteTransaction();
2574 $this->callAPISuccess('contribution', 'completetransaction', array(
2575 'id' => $contribution['id'],
2576 'trxn_date' => date('2011-04-09'),
2577 'trxn_id' => 'kazam',
2579 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2580 $this->mut
->checkMailLog(array(
2581 'email:::anthony_anderson@civicrm.org',
2585 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2586 'receipt_date:::' . date('Ymd'),
2587 'contributeMode:::notify',
2588 'title:::Contribution',
2589 'displayName:::Mr. Anthony Anderson II',
2591 'contactID:::' . $this->_params
['contact_id'],
2592 'contributionID:::' . $contribution['id'],
2593 'financialTypeId:::1',
2594 'financialTypeName:::Donation',
2597 $this->revertTemplateToReservedTemplate();
2601 * Complete the transaction using the template with all the possible.
2603 public function testCompleteTransactionContributionPageFromAddress() {
2604 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2605 'receipt_from_name' => 'Mickey Mouse',
2606 'receipt_from_email' => 'mickey@mouse.com',
2607 'title' => "Test Contribution Page",
2608 'financial_type_id' => 1,
2609 'currency' => 'NZD',
2610 'goal_amount' => 50,
2611 'is_pay_later' => 1,
2612 'is_monetary' => TRUE,
2613 'is_email_receipt' => TRUE,
2615 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2616 $contribution = $this->setUpForCompleteTransaction();
2617 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2618 $this->mut
->checkMailLog(array(
2626 * Test completing first transaction in a recurring series.
2628 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2630 * @dataProvider getScheduledDateData
2632 * @param array $dataSet
2634 * @throws \Exception
2636 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2637 $paymentProcessorID = $this->paymentProcessorCreate();
2638 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2639 'contact_id' => $this->_individualId
,
2640 'installments' => '2',
2641 'frequency_interval' => '1',
2643 'contribution_status_id' => 'Pending',
2644 'start_date' => '2012-01-01 00:00:00',
2645 'currency' => 'USD',
2646 'frequency_unit' => 'month',
2647 'payment_processor_id' => $paymentProcessorID,
2648 ), $dataSet['data']));
2649 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2652 'contribution_recur_id' => $contributionRecur['id'],
2653 'contribution_status_id' => 'Pending',
2654 'receive_date' => $dataSet['receive_date'],
2657 $this->callAPISuccess('Contribution', 'completetransaction', array(
2658 'id' => $contribution,
2659 'receive_date' => $dataSet['receive_date'],
2661 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2662 'id' => $contributionRecur['id'],
2663 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2665 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2666 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2667 $this->callAPISuccess('Contribution', 'create', array_merge(
2670 'contribution_recur_id' => $contributionRecur['id'],
2671 'contribution_status_id' => 'Completed',
2674 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2675 'id' => $contributionRecur['id'],
2676 'return' => array('contribution_status_id'),
2678 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2682 * Get dates for testing.
2686 public function getScheduledDateData() {
2688 $result[]['2016-08-31-1-month'] = array(
2690 'start_date' => '2016-08-31',
2691 'frequency_interval' => 1,
2692 'frequency_unit' => 'month',
2694 'receive_date' => '2016-08-31',
2695 'expected' => '2016-10-01 00:00:00',
2697 $result[]['2012-01-01-1-month'] = array(
2699 'start_date' => '2012-01-01',
2700 'frequency_interval' => 1,
2701 'frequency_unit' => 'month',
2703 'receive_date' => '2012-01-01',
2704 'expected' => '2012-02-01 00:00:00',
2706 $result[]['2012-01-01-1-month'] = array(
2708 'start_date' => '2012-01-01',
2709 'frequency_interval' => 1,
2710 'frequency_unit' => 'month',
2712 'receive_date' => '2012-02-29',
2713 'expected' => '2012-03-29 00:00:00',
2715 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2717 'start_date' => '2012-01-01',
2718 'frequency_interval' => 1,
2719 'frequency_unit' => 'month',
2720 'next_sched_contribution_date' => '2012-02-29',
2722 'receive_date' => '2012-02-29 16:00:00',
2723 'expected' => '2012-03-29 00:00:00',
2729 * Test completing a pledge with the completeTransaction api..
2731 * Note that we are creating a logged in user because email goes out from
2734 public function testCompleteTransactionUpdatePledgePayment() {
2735 $this->swapMessageTemplateForTestTemplate();
2736 $mut = new CiviMailUtils($this, TRUE);
2737 $mut->clearMessages();
2738 $this->createLoggedInUser();
2739 $contributionID = $this->createPendingPledgeContribution();
2740 $this->callAPISuccess('contribution', 'completetransaction', array(
2741 'id' => $contributionID,
2742 'trxn_date' => '1 Feb 2013',
2744 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2745 'id' => $this->_ids
['pledge'],
2747 $this->assertEquals('Completed', $pledge['pledge_status']);
2749 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2750 'pledge_id' => $this->_ids
['pledge'],
2751 'return' => 'status_id',
2753 $this->assertEquals(1, $status);
2754 $mut->checkMailLog(array(
2756 'receive_date:::20130201000000',
2757 "receipt_date:::\n",
2760 $this->revertTemplateToReservedTemplate();
2764 * Test completing a transaction with an event via the API.
2766 * Note that we are creating a logged in user because email goes out from
2769 public function testCompleteTransactionWithParticipantRecord() {
2770 $mut = new CiviMailUtils($this, TRUE);
2771 $mut->clearMessages();
2772 $this->_individualId
= $this->createLoggedInUser();
2773 $contributionID = $this->createPendingParticipantContribution();
2774 $this->callAPISuccess('contribution', 'completetransaction', array(
2775 'id' => $contributionID,
2778 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2779 'id' => $this->_ids
['participant'],
2780 'return' => 'participant_status_id',
2782 $this->assertEquals(1, $participantStatus);
2784 //Assert only three activities are created.
2785 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_individualId
);
2786 $this->assertEquals(3, count($activities));
2787 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
2788 // record two activities before and after completing payment for Event registration
2789 $this->assertEquals(2, $activityNames['Event Registration']);
2790 // update the original 'Contribution' activity created after completing payment
2791 $this->assertEquals(1, $activityNames['Contribution']);
2793 $mut->checkMailLog(array(
2794 'Annual CiviCRM meet',
2796 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2802 * Test membership is renewed when transaction completed.
2804 public function testCompleteTransactionMembershipPriceSet() {
2805 $this->createPriceSetWithPage('membership');
2806 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2810 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2811 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2812 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2813 'membership_id' => $this->_ids
['membership'],
2815 $this->assertEquals(1, $logs['count']);
2816 $this->assertEquals($stateOfGrace, $membership['status_id']);
2817 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2818 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2819 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2820 $this->callAPISuccessGetSingle('LineItem', array(
2821 'entity_id' => $this->_ids
['membership'],
2822 'entity_table' => 'civicrm_membership',
2824 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2825 'membership_id' => $this->_ids
['membership'],
2827 $this->assertEquals(2, $logs['count']);
2828 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2829 //Assert only three activities are created.
2830 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
2831 $this->assertEquals(3, count($activities));
2832 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
2833 $this->assertArrayHasKey('Contribution', $activityNames);
2834 $this->assertArrayHasKey('Membership Signup', $activityNames);
2835 $this->assertArrayHasKey('Change Membership Status', $activityNames);
2836 $this->cleanUpAfterPriceSets();
2840 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2842 public function testPendingToCompleteContribution() {
2843 $contributionPage = $this->createPriceSetWithPage('membership');
2844 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2848 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
2849 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2850 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2851 $activity = $this->callAPISuccess('Activity', 'get', array(
2852 'activity_type_id' => 'Membership Signup',
2853 'source_record_id' => $this->_ids
['membership'],
2854 'status_id' => 'Scheduled',
2856 $this->assertEquals(1, $activity['count']);
2858 // change pending contribution to completed
2859 $form = new CRM_Contribute_Form_Contribution();
2861 $form->_params
= array(
2862 'id' => $this->_ids
['contribution'],
2863 'total_amount' => 20,
2866 'financial_type_id' => 1,
2867 'receive_date' => '04/21/2015',
2868 'receive_date_time' => '11:27PM',
2869 'contact_id' => $this->_individualId
,
2870 'contribution_status_id' => 1,
2871 'billing_middle_name' => '',
2872 'billing_last_name' => 'Adams',
2873 'billing_street_address-5' => '790L Lincoln St S',
2874 'billing_city-5' => 'Maryknoll',
2875 'billing_state_province_id-5' => 1031,
2876 'billing_postal_code-5' => 10545,
2877 'billing_country_id-5' => 1228,
2878 'frequency_interval' => 1,
2879 'frequency_unit' => 'month',
2880 'installments' => '',
2881 'hidden_AdditionalDetail' => 1,
2882 'hidden_Premium' => 1,
2883 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2884 'receipt_date' => '',
2885 'receipt_date_time' => '',
2886 'payment_processor_id' => $this->paymentProcessorID
,
2887 'currency' => 'USD',
2888 'contribution_page_id' => $this->_ids
['contribution_page'],
2889 'contribution_mode' => 'membership',
2890 'source' => 'Membership Signup and Renewal',
2893 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2895 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
2898 // Case 2: After successful payment for Pending backoffice there are three activities created
2899 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
2900 $activity = $this->callAPISuccess('Activity', 'get', array(
2901 'activity_type_id' => 'Membership Signup',
2902 'source_record_id' => $this->_ids
['membership'],
2903 'status_id' => 'Completed',
2905 $this->assertEquals(1, $activity['count']);
2906 // 2.b Contribution activity created to record successful payment
2907 $activity = $this->callAPISuccess('Activity', 'get', array(
2908 'activity_type_id' => 'Contribution',
2909 'source_record_id' => $this->_ids
['contribution'],
2910 'status_id' => 'Completed',
2912 $this->assertEquals(1, $activity['count']);
2914 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
2915 $activity = $this->callAPISuccess('Activity', 'get', array(
2916 'activity_type_id' => 'Change Membership Status',
2917 'source_record_id' => $this->_ids
['membership'],
2918 'status_id' => 'Completed',
2920 $this->assertEquals(1, $activity['count']);
2921 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
2923 //Create another pending contribution for renewal
2924 $contribution = $this->callAPISuccess('contribution', 'create', array(
2926 'contact_id' => $this->_ids
['contact'],
2927 'receive_date' => date('Ymd'),
2928 'total_amount' => 20.00,
2929 'financial_type_id' => 1,
2930 'payment_instrument_id' => 'Credit Card',
2931 'non_deductible_amount' => 10.00,
2932 'trxn_id' => 'rdhfi88',
2933 'invoice_id' => 'dofhiewuyr',
2935 'contribution_status_id' => 2,
2936 'contribution_page_id' => $this->_ids
['contribution_page'],
2937 'api.membership_payment.create' => array('membership_id' => $this->_ids
['membership']),
2940 $this->callAPISuccess('line_item', 'create', array(
2941 'entity_id' => $contribution['id'],
2942 'entity_table' => 'civicrm_contribution',
2943 'contribution_id' => $contribution['id'],
2944 'price_field_id' => $this->_ids
['price_field'][0],
2948 'financial_type_id' => 1,
2949 'price_field_value_id' => $this->_ids
['price_field_value'][0],
2952 //Update it to Failed.
2953 $form->_params
['id'] = $contribution['id'];
2954 $form->_params
['contribution_status_id'] = 4;
2956 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
2958 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
2961 //Existing membership should not get updated to expired.
2962 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2963 $this->assertNotEquals($membership['status_id'], 4);
2967 * Test membership is renewed when transaction completed.
2969 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2970 $this->createPriceSetWithPage('membership');
2971 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
2972 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
2973 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
2974 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2975 $this->cleanUpAfterPriceSets();
2978 public function cleanUpAfterPriceSets() {
2979 $this->quickCleanUpFinancialEntities();
2980 $this->contactDelete($this->_ids
['contact']);
2984 * Set up a pending transaction with a specific price field id.
2986 * @param int $priceFieldValueID
2988 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
2989 $contactID = $this->individualCreate();
2990 $membership = $this->callAPISuccess('membership', 'create', array(
2991 'contact_id' => $contactID,
2992 'membership_type_id' => $this->_ids
['membership_type'],
2993 'start_date' => 'yesterday - 1 year',
2994 'end_date' => 'yesterday',
2995 'join_date' => 'yesterday - 1 year',
2997 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
2999 'contact_id' => $contactID,
3000 'receive_date' => date('Ymd'),
3001 'total_amount' => 20.00,
3002 'financial_type_id' => 1,
3003 'payment_instrument_id' => 'Credit Card',
3004 'non_deductible_amount' => 10.00,
3005 'trxn_id' => 'jdhfi' . rand(1, 100),
3006 'invoice_id' => 'djfhiew' . rand(5, 100),
3008 'contribution_status_id' => 2,
3009 'contribution_page_id' => $this->_ids
['contribution_page'],
3010 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3013 $this->callAPISuccess('line_item', 'create', array(
3014 'entity_id' => $contribution['id'],
3015 'entity_table' => 'civicrm_contribution',
3016 'contribution_id' => $contribution['id'],
3017 'price_field_id' => $this->_ids
['price_field'][0],
3021 'financial_type_id' => 1,
3022 'price_field_value_id' => $priceFieldValueID,
3024 $this->_ids
['contact'] = $contactID;
3025 $this->_ids
['contribution'] = $contribution['id'];
3026 $this->_ids
['membership'] = $membership['id'];
3030 * Test sending a mail via the API.
3032 public function testSendMail() {
3033 $mut = new CiviMailUtils($this, TRUE);
3034 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3035 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3036 'id' => $contribution['id'],
3037 'receipt_from_email' => 'api@civicrm.org',
3040 $mut->checkMailLog(array(
3042 'Contribution Information',
3043 'Please print this confirmation for your records',
3049 $this->checkCreditCardDetails($mut, $contribution['id']);
3054 * Test sending a mail via the API.
3055 * This simulates webform_civicrm using pay later contribution page
3057 public function testSendconfirmationPayLater() {
3058 $mut = new CiviMailUtils($this, TRUE);
3060 // Create contribution page
3061 $pageParams = array(
3062 'title' => 'Webform Contributions',
3063 'financial_type_id' => 1,
3064 'contribution_type_id' => 1,
3065 'is_confirm_enabled' => 1,
3066 'is_pay_later' => 1,
3067 'pay_later_text' => 'I will send payment by cheque',
3068 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3070 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3072 // Create pay later contribution
3073 $contribParams = array(
3074 'contact_id' => $this->_individualId
,
3075 'financial_type_id' => 1,
3076 'is_pay_later' => 1,
3077 'contribution_status_id' => 2,
3078 'contribution_page_id' => $contributionPage['id'],
3079 'total_amount' => '10.00',
3081 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3084 $lineItemParams = array(
3085 'contribution_id' => $contribution['id'],
3086 'entity_id' => $contribution['id'],
3087 'entity_table' => 'civicrm_contribution',
3088 'label' => 'My lineitem label',
3090 'unit_price' => "10.00",
3091 'line_total' => "10.00",
3093 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3097 civicrm_api3('contribution', 'sendconfirmation', array(
3098 'id' => $contribution['id'],
3099 'receipt_from_email' => 'api@civicrm.org',
3103 catch (Exception
$e) {
3104 // Need to figure out how to stop this some other day
3105 // We don't care about the Payment Processor because this is Pay Later
3106 // The point of this test is to check we get the pay_later version of the mail
3107 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3112 // Retrieve mail & check it has the pay_later_receipt info
3113 $mut->getMostRecentEmail('raw');
3114 $mut->checkMailLog(array(
3115 (string) $contribParams['total_amount'],
3116 $pageParams['pay_later_receipt'],
3126 * Check credit card details in sent mail via API
3128 * @param $mut obj CiviMailUtils instance
3129 * @param int $contributionID Contribution ID
3132 public function checkCreditCardDetails($mut, $contributionID) {
3133 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3134 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3135 'id' => $contributionID,
3136 'receipt_from_email' => 'api@civicrm.org',
3137 'payment_processor_id' => $this->paymentProcessorID
,
3140 $mut->checkMailLog(array(
3141 'Credit Card Information', // credit card header
3142 'Billing Name and Address', // billing header
3143 'anthony_anderson@civicrm.org', // billing name
3151 * Test sending a mail via the API.
3153 public function testSendMailEvent() {
3154 $mut = new CiviMailUtils($this, TRUE);
3155 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3156 $event = $this->eventCreate(array(
3157 'is_email_confirm' => 1,
3158 'confirm_from_email' => 'test@civicrm.org',
3160 $this->_eventID
= $event['id'];
3161 $participantParams = array(
3162 'contact_id' => $this->_individualId
,
3163 'event_id' => $this->_eventID
,
3166 // to ensure it matches later on
3167 'register_date' => '2007-07-21 00:00:00',
3168 'source' => 'Online Event Registration: API Testing',
3171 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3172 $this->callAPISuccess('participant_payment', 'create', array(
3173 'participant_id' => $participant['id'],
3174 'contribution_id' => $contribution['id'],
3176 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3177 'id' => $contribution['id'],
3178 'receipt_from_email' => 'api@civicrm.org',
3182 $mut->checkMailLog(array(
3183 'Annual CiviCRM meet',
3185 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3192 * This function does a GET & compares the result against the $params.
3194 * Use as a double check on Creates.
3196 * @param array $params
3198 * @param bool $delete
3200 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3202 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3208 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3210 $this->assertAPISuccess($contribution, 0);
3211 $values = $contribution['values'][$contribution['id']];
3212 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3213 // this is not returned in id format
3214 unset($params['payment_instrument_id']);
3215 $params['contribution_source'] = $params['source'];
3216 unset($params['source']);
3217 foreach ($params as $key => $value) {
3218 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3223 * Create a pending contribution & linked pending pledge record.
3225 public function createPendingPledgeContribution() {
3227 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500));
3228 $this->_ids
['pledge'] = $pledgeID;
3229 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array(
3230 'contribution_status_id' => 'Pending',
3231 'total_amount' => 500,
3234 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3235 'options' => array('limit' => 1),
3238 $this->callAPISuccess('PledgePayment', 'create', array(
3240 'contribution_id' =>
3241 $contribution['id'],
3242 'status_id' => 'Pending',
3243 'scheduled_amount' => 500,
3246 return $contribution['id'];
3250 * Create a pending contribution & linked pending participant record (along with an event).
3252 public function createPendingParticipantContribution() {
3253 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3254 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId
));
3255 $this->_ids
['participant'] = $participantID;
3256 $params = array_merge($this->_params
, array('contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3257 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3258 $this->callAPISuccess('participant_payment', 'create', array(
3259 'contribution_id' => $contribution['id'],
3260 'participant_id' => $participantID,
3262 $this->callAPISuccess('line_item', 'get', array(
3263 'entity_id' => $contribution['id'],
3264 'entity_table' => 'civicrm_contribution',
3265 'api.line_item.create' => array(
3266 'entity_id' => $participantID,
3267 'entity_table' => 'civicrm_participant',
3270 return $contribution['id'];
3274 * Get financial transaction amount.
3276 * @param int $contId
3278 * @return null|string
3280 public function _getFinancialTrxnAmount($contId) {
3282 SUM( ft.total_amount ) AS total
3283 FROM civicrm_financial_trxn AS ft
3284 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3285 WHERE ceft.entity_table = 'civicrm_contribution'
3286 AND ceft.entity_id = {$contId}";
3288 $result = CRM_Core_DAO
::singleValueQuery($query);
3293 * @param int $contId
3295 * @return null|string
3297 public function _getFinancialItemAmount($contId) {
3298 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3301 FROM civicrm_financial_item
3302 WHERE entity_table = 'civicrm_line_item'
3303 AND entity_id = {$lineItem}";
3304 $result = CRM_Core_DAO
::singleValueQuery($query);
3309 * @param int $contId
3312 public function _checkFinancialItem($contId, $context) {
3313 if ($context != 'paylater') {
3315 'entity_id' => $contId,
3316 'entity_table' => 'civicrm_contribution',
3318 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3319 $entityParams = array(
3320 'financial_trxn_id' => $trxn['financial_trxn_id'],
3321 'entity_table' => 'civicrm_financial_item',
3323 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3325 'id' => $entityTrxn['entity_id'],
3328 if ($context == 'paylater') {
3329 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3330 foreach ($lineItems as $key => $item) {
3332 'entity_id' => $key,
3333 'entity_table' => 'civicrm_line_item',
3335 $compareParams = array('status_id' => 1);
3336 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3339 elseif ($context == 'refund') {
3340 $compareParams = array(
3342 'financial_account_id' => 1,
3346 elseif ($context == 'cancelPending') {
3347 $compareParams = array(
3349 'financial_account_id' => 1,
3353 elseif ($context == 'changeFinancial') {
3354 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3356 'entity_id' => $lineKey,
3359 $compareParams = array(
3360 'financial_account_id' => 1,
3362 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3364 'financial_account_id' => 3,
3365 'entity_id' => $lineKey,
3367 $compareParams = array(
3371 if ($context != 'paylater') {
3372 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3377 * Check correct financial transaction entries were created for the change in payment instrument.
3379 * @param int $contributionID
3380 * @param int $originalInstrumentID
3381 * @param int $newInstrumentID
3383 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3385 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3387 $originalTrxnParams = array(
3388 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3389 'payment_instrument_id' => $originalInstrumentID,
3390 'amount' => $amount,
3394 $reversalTrxnParams = array(
3395 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3396 'payment_instrument_id' => $originalInstrumentID,
3397 'amount' => -$amount,
3401 $newTrxnParams = array(
3402 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3403 'payment_instrument_id' => $newInstrumentID,
3404 'amount' => $amount,
3408 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3409 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3410 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3412 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3413 'id' => $entityFinancialTrxn['financial_trxn_id'],
3415 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3416 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3417 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3418 $this->assertEquals(0, $financialTrxn['fee_amount']);
3419 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3420 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3423 $this->assertEquals(1, $financialTrxn['is_payment']);
3424 $this->assertEquals('USD', $financialTrxn['currency']);
3425 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3430 * Check financial transaction.
3432 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3434 * @param array $contribution
3435 * @param string $context
3436 * @param int $instrumentId
3437 * @param array $extraParams
3439 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3440 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3441 $trxn = array_pop($financialTrxns);
3444 'id' => $trxn['financial_trxn_id'],
3446 if ($context == 'payLater') {
3447 $compareParams = array(
3449 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3452 elseif ($context == 'refund') {
3453 $compareParams = array(
3454 'to_financial_account_id' => 6,
3455 'total_amount' => -100,
3457 'trxn_date' => '2015-01-01 09:00:00',
3458 'trxn_id' => 'the refund',
3461 elseif ($context == 'cancelPending') {
3462 $compareParams = array(
3463 'to_financial_account_id' => 7,
3464 'total_amount' => -100,
3468 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3469 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3470 // It does the same thing with greater readability.
3471 // @todo remove handling for
3473 $entityParams = array(
3474 'entity_id' => $contribution['id'],
3475 'entity_table' => 'civicrm_contribution',
3478 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3479 $trxnParams1 = array(
3480 'id' => $trxn['financial_trxn_id'],
3482 if (empty($extraParams)) {
3483 $compareParams = array(
3484 'total_amount' => -100,
3489 $compareParams = array(
3490 'total_amount' => 100,
3494 if ($context == 'paymentInstrument') {
3495 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3496 $compareParams['payment_instrument_id'] = $instrumentId;
3499 $compareParams['to_financial_account_id'] = 12;
3501 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3502 $compareParams['total_amount'] = 100;
3505 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3511 public function _addPaymentInstrument() {
3512 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3513 $optionParams = array(
3514 'option_group_id' => $gId,
3515 'label' => 'Test Card',
3516 'name' => 'Test Card',
3521 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3522 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3523 $financialParams = array(
3524 'entity_table' => 'civicrm_option_value',
3525 'entity_id' => $optionValue['id'],
3526 'account_relationship' => $relationTypeId,
3527 'financial_account_id' => 7,
3529 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
3530 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3531 return $optionValue['values'][$optionValue['id']]['value'];
3534 public function _deletedAddedPaymentInstrument() {
3535 $result = $this->callAPISuccess('OptionValue', 'get', array(
3536 'option_group_id' => 'payment_instrument',
3537 'name' => 'Test Card',
3541 if ($id = CRM_Utils_Array
::value('id', $result)) {
3542 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3547 * Set up the basic recurring contribution for tests.
3549 * @param array $generalParams
3550 * Parameters that can be merged into the recurring AND the contribution.
3552 * @param array $recurParams
3553 * Parameters to merge into the recur only.
3557 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3558 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3559 'contact_id' => $this->_individualId
,
3560 'installments' => '12',
3561 'frequency_interval' => '1',
3563 'contribution_status_id' => 1,
3564 'start_date' => '2012-01-01 00:00:00',
3565 'currency' => 'USD',
3566 'frequency_unit' => 'month',
3567 'payment_processor_id' => $this->paymentProcessorID
,
3568 ), $generalParams, $recurParams));
3569 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3572 'contribution_recur_id' => $contributionRecur['id'],
3575 return $originalContribution;
3579 * Set up a basic auto-renew membership for tests.
3581 * @param array $generalParams
3582 * Parameters that can be merged into the recurring AND the contribution.
3584 * @param array $recurParams
3585 * Parameters to merge into the recur only.
3589 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3590 $newContact = $this->callAPISuccess('Contact', 'create', array(
3591 'contact_type' => 'Individual',
3592 'sort_name' => 'McTesterson, Testy',
3593 'display_name' => 'Testy McTesterson',
3594 'preferred_language' => 'en_US',
3595 'preferred_mail_format' => 'Both',
3596 'first_name' => 'Testy',
3597 'last_name' => 'McTesterson',
3598 'contact_is_deleted' => '0',
3600 'email' => 'tmctesterson@example.com',
3603 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3604 'domain_id' => "Default Domain Name",
3605 'member_of_contact_id' => 1,
3606 'financial_type_id' => "Member Dues",
3607 'duration_unit' => "month",
3608 'duration_interval' => 1,
3609 'period_type' => 'rolling',
3610 'name' => "Standard Member",
3611 'minimum_fee' => 100,
3613 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3614 'contact_id' => $newContact['id'],
3615 'installments' => '12',
3616 'frequency_interval' => '1',
3618 'contribution_status_id' => 1,
3619 'start_date' => '2012-01-01 00:00:00',
3620 'currency' => 'USD',
3621 'frequency_unit' => 'month',
3622 'payment_processor_id' => $this->paymentProcessorID
,
3623 ), $generalParams, $recurParams));
3625 $membership = $this->callAPISuccess('membership', 'create', array(
3626 'contact_id' => $newContact['id'],
3627 'contribution_recur_id' => $contributionRecur['id'],
3628 'financial_type_id' => "Member Dues",
3629 'membership_type_id' => $membershipType['id'],
3631 'skipLineItem' => TRUE,
3634 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
3635 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3638 'contact_id' => $newContact['id'],
3639 'contribution_recur_id' => $contributionRecur['id'],
3640 'financial_type_id' => "Member Dues",
3641 'contribution_status_id' => 1,
3642 'invoice_id' => uniqid(),
3645 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3646 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3647 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3648 $this->callAPISuccess('LineItem', 'getsingle', array());
3649 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3651 return array($originalContribution, $membership);
3654 * Set up a repeat transaction.
3656 * @param array $recurParams
3660 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3661 $paymentProcessorID = $this->paymentProcessorCreate();
3662 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3663 'contact_id' => $this->_individualId
,
3664 'installments' => '12',
3665 'frequency_interval' => '1',
3667 'contribution_status_id' => 1,
3668 'start_date' => '2012-01-01 00:00:00',
3669 'currency' => 'USD',
3670 'frequency_unit' => 'month',
3671 'payment_processor_id' => $paymentProcessorID,
3674 $originalContribution = '';
3675 if ($flag == 'multiple') {
3676 // CRM-19309 create a contribution + also add in line_items (plural):
3677 $params = array_merge($this->_params
, $contributionParams);
3678 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3681 'contribution_recur_id' => $contributionRecur['id'],
3682 'skipLineItem' => 1,
3683 'api.line_item.create' => array(
3685 'price_field_id' => 1,
3687 'line_total' => '20',
3688 'unit_price' => '10',
3689 'financial_type_id' => 1,
3692 'price_field_id' => 1,
3694 'line_total' => '80',
3695 'unit_price' => '80',
3696 'financial_type_id' => 2,
3703 elseif ($flag == 'single') {
3704 $params = array_merge($this->_params
, array('contribution_recur_id' => $contributionRecur['id']));
3705 $params = array_merge($params, $contributionParams);
3706 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3708 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3709 return $originalContribution;
3713 * Common set up routine.
3717 protected function setUpForCompleteTransaction() {
3718 $this->mut
= new CiviMailUtils($this, TRUE);
3719 $this->createLoggedInUser();
3720 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3721 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3722 return $contribution;
3726 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3728 public function testRepeatTransactionWithNonCreditCardDefault() {
3729 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3730 'contact_id' => $this->_individualId
,
3731 'installments' => '12',
3732 'frequency_interval' => '1',
3734 'contribution_status_id' => 1,
3735 'start_date' => '2012-01-01 00:00:00',
3736 'currency' => 'USD',
3737 'frequency_unit' => 'month',
3738 'payment_processor_id' => $this->paymentProcessorID
,
3740 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3742 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3744 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
3745 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3746 'contribution_status_id' => 'Completed',
3747 'trxn_id' => uniqid(),
3748 'original_contribution_id' => $contribution1,
3750 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3751 $this->quickCleanUpFinancialEntities();
3755 * CRM-20008 Tests repeattransaction creates pending membership.
3757 public function testRepeatTransactionPendingMembership() {
3758 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3759 $this->callAPISuccess('membership', 'create', array(
3760 'id' => $membership['id'],
3761 'end_date' => 'yesterday',
3762 'status_id' => 'Expired',
3764 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3765 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3766 'contribution_status_id' => 'Pending',
3767 'trxn_id' => uniqid(),
3769 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3770 'id' => $membership['id'],
3771 'return' => 'status_id',
3774 // Let's see if the membership payments got created while we're at it.
3775 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3776 'memberhip_id' => $membership['id'],
3778 $this->assertEquals(2, $membershipPayments['count']);
3780 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3781 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3782 $membership = $this->callAPISuccessGetSingle('membership', array(
3783 'id' => $membership['id'],
3784 'return' => 'status_id, end_date',
3786 $this->assertEquals('New', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3787 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3789 $this->quickCleanUpFinancialEntities();
3790 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3794 * Test sending a mail via the API.
3796 public function testSendMailWithAPISetFromDetails() {
3797 $mut = new CiviMailUtils($this, TRUE);
3798 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3799 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3800 'id' => $contribution['id'],
3801 'receipt_from_email' => 'api@civicrm.org',
3802 'receipt_from_name' => 'CiviCRM LLC',
3804 $mut->checkMailLog(array(
3805 'From: CiviCRM LLC <api@civicrm.org>',
3806 'Contribution Information',
3807 'Please print this confirmation for your records',
3816 * Test sending a mail via the API.
3818 public function testSendMailWithNoFromSetFallToDomain() {
3819 $this->createLoggedInUser();
3820 $mut = new CiviMailUtils($this, TRUE);
3821 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3822 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3823 'id' => $contribution['id'],
3825 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3826 $mut->checkMailLog(array(
3827 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3828 'Contribution Information',
3829 'Please print this confirmation for your records',
3838 * Test sending a mail via the API.
3840 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3841 $this->createLoggedInUser();
3842 $mut = new CiviMailUtils($this, TRUE);
3843 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3844 $this->callAPISuccess('contribution', 'repeattransaction', array(
3845 'contribution_status_id' => 'Completed',
3846 'trxn_id' => uniqid(),
3847 'original_contribution_id' => $contribution,
3849 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3850 $mut->checkMailLog(array(
3851 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3852 'Contribution Information',
3853 'Please print this confirmation for your records',
3862 * Test sending a mail via the API.
3864 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3865 $mut = new CiviMailUtils($this, TRUE);
3866 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3867 $paymentProcessorID = $this->paymentProcessorCreate();
3868 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3869 'contact_id' => $this->_individualId
,
3870 'installments' => '12',
3871 'frequency_interval' => '1',
3873 'contribution_status_id' => 1,
3874 'start_date' => '2012-01-01 00:00:00',
3875 'currency' => 'USD',
3876 'frequency_unit' => 'month',
3877 'payment_processor_id' => $paymentProcessorID,
3879 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3882 'contribution_recur_id' => $contributionRecur['id'],
3883 'contribution_page_id' => $contributionPage['id']))
3885 $this->callAPISuccess('contribution', 'repeattransaction', array(
3886 'contribution_status_id' => 'Completed',
3887 'trxn_id' => uniqid(),
3888 'original_contribution_id' => $originalContribution,
3891 $mut->checkMailLog(array(
3892 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3893 'Contribution Information',
3894 'Please print this confirmation for your records',
3903 * Test sending a mail via the API.
3905 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
3906 $mut = new CiviMailUtils($this, TRUE);
3907 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
3908 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
3909 foreach ($fromEmail['values'] as $from) {
3910 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
3912 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
3913 $this->callAPISuccess('contribution', 'repeattransaction', array(
3914 'contribution_status_id' => 'Completed',
3915 'trxn_id' => uniqid(),
3916 'original_contribution_id' => $originalContribution,
3919 $mut->checkMailLog(array(
3920 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
3921 'Contribution Information',
3922 'Please print this confirmation for your records',
3931 * Create a Contribution Page with is_email_receipt = TRUE.
3933 * @param array $params
3934 * Params to overwrite with.
3938 protected function createReceiptableContributionPage($params = array()) {
3939 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
3940 'receipt_from_name' => 'Mickey Mouse',
3941 'receipt_from_email' => 'mickey@mouse.com',
3942 'title' => "Test Contribution Page",
3943 'financial_type_id' => 1,
3944 'currency' => 'CAD',
3945 'is_monetary' => TRUE,
3946 'is_email_receipt' => TRUE,
3948 return $contributionPage;
3952 * function to test card_type and pan truncation.
3954 public function testCardTypeAndPanTruncation() {
3955 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
3956 $contactId = $this->individualCreate();
3958 'contact_id' => $contactId,
3959 'receive_date' => '2016-01-20',
3960 'total_amount' => 100,
3961 'financial_type_id' => 1,
3962 'payment_instrument' => 'Credit Card',
3963 'card_type_id' => $creditCardTypeIDs['Visa'],
3964 'pan_truncation' => 4567,
3966 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3967 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
3968 $financialTrxn = $this->callAPISuccessGetSingle(
3971 'id' => $lastFinancialTrxnId['financialTrxnId'],
3972 'return' => array('card_type_id', 'pan_truncation'),
3975 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
3976 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
3978 'id' => $contribution['id'],
3979 'pan_truncation' => 2345,
3980 'card_type_id' => $creditCardTypeIDs['Amex'],
3982 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3983 $financialTrxn = $this->callAPISuccessGetSingle(
3986 'id' => $lastFinancialTrxnId['financialTrxnId'],
3987 'return' => array('card_type_id', 'pan_truncation'),
3990 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
3991 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
3995 * Test repeat contribution uses non default currency
3996 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
3998 public function testRepeatTransactionWithDifferenceCurrency() {
3999 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4000 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4001 'original_contribution_id' => $originalContribution['id'],
4002 'contribution_status_id' => 'Completed',
4003 'trxn_id' => uniqid(),
4005 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4009 * Get the financial items for the contribution.
4011 * @param int $contributionID
4014 * Array of associated financial items.
4016 protected function getFinancialTransactionsForContribution($contributionID) {
4017 $trxnParams = array(
4018 'entity_id' => $contributionID,
4019 'entity_table' => 'civicrm_contribution',
4021 // @todo the following function has naming errors & has a weird signature & appears to
4022 // only be called from test classes. Move into test suite & maybe just use api
4023 // from this function.
4024 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));