3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_contribute_* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
19 class api_v3_ContributionTest
extends CiviUnitTestCase
{
21 use CRMTraits_Profile_ProfileTrait
;
22 use CRMTraits_Custom_CustomDataTrait
;
24 protected $_individualId;
25 protected $_contribution;
26 protected $_financialTypeId = 1;
27 protected $_apiversion;
28 protected $_entity = 'Contribution';
32 protected $_pageParams = [];
34 * Payment processor ID (dummy processor).
38 protected $paymentProcessorID;
41 * Parameters to create payment processor.
45 protected $_processorParams = [];
48 * ID of created event.
62 * @throws \CRM_Core_Exception
64 public function setUp() {
67 $this->_apiversion
= 3;
68 $this->_individualId
= $this->individualCreate();
70 'contact_id' => $this->_individualId
,
71 'receive_date' => '20120511',
72 'total_amount' => 100.00,
73 'financial_type_id' => $this->_financialTypeId
,
74 'non_deductible_amount' => 10.00,
76 'net_amount' => 95.00,
78 'contribution_status_id' => 1,
80 $this->_processorParams
= [
83 'payment_processor_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
84 'financial_account_id' => 12,
87 'url_site' => 'http://dummy.com',
88 'url_recur' => 'http://dummy.com',
91 $this->paymentProcessorID
= $this->processorCreate();
92 $this->_pageParams
= [
93 'title' => 'Test Contribution Page',
94 'financial_type_id' => 1,
96 'financial_account_id' => 1,
97 'payment_processor' => $this->paymentProcessorID
,
99 'is_allow_other_amount' => 1,
101 'max_amount' => 1000,
103 $this->entity
= $this->_entity
;
107 * Clean up after each test.
109 * @throws \CRM_Core_Exception
111 public function tearDown() {
112 $this->quickCleanUpFinancialEntities();
113 $this->quickCleanup(['civicrm_uf_match']);
114 $financialAccounts = $this->callAPISuccess('FinancialAccount', 'get', []);
115 foreach ($financialAccounts['values'] as $financialAccount) {
116 if ($financialAccount['name'] === 'Test Tax financial account ' ||
$financialAccount['name'] === 'Test taxable financial Type') {
117 $entityFinancialTypes = $this->callAPISuccess('EntityFinancialAccount', 'get', [
118 'financial_account_id' => $financialAccount['id'],
120 foreach ($entityFinancialTypes['values'] as $entityFinancialType) {
121 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialType['id']]);
123 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
126 $this->restoreUFGroupOne();
132 * @throws \CRM_Core_Exception
134 public function testGetContribution() {
135 $contributionSettings = $this->enableTaxAndInvoicing();
136 $invoice_prefix = CRM_Contribute_BAO_Contribution
::checkContributeSettings('invoice_prefix', TRUE);
138 'contact_id' => $this->_individualId
,
139 'receive_date' => '2010-01-20',
140 'total_amount' => 100.00,
141 'financial_type_id' => $this->_financialTypeId
,
142 'non_deductible_amount' => 10.00,
143 'fee_amount' => 5.00,
144 'net_amount' => 95.00,
146 'invoice_id' => 78910,
148 'contribution_status_id' => 1,
150 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
153 'contribution_id' => $this->_contribution
['id'],
156 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
157 $financialParams['id'] = $this->_financialTypeId
;
159 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
161 $this->assertEquals(1, $contributions['count']);
162 $contribution = $contributions['values'][$contributions['id']];
163 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
164 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
165 // Passing back a string rather than an id seems like an error/cruft.
166 // If it is to be introduced we should discuss.
167 $this->assertEquals($contribution['financial_type_id'], 1);
168 $this->assertEquals($contribution['total_amount'], 100.00);
169 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
170 $this->assertEquals($contribution['fee_amount'], 5.00);
171 $this->assertEquals($contribution['net_amount'], 95.00);
172 $this->assertEquals($contribution['trxn_id'], 23456);
173 $this->assertEquals($contribution['invoice_id'], 78910);
174 $this->assertEquals($contribution['invoice_number'], $invoice_prefix . $contributions['id']);
175 $this->assertEquals($contribution['contribution_source'], 'SSF');
176 $this->assertEquals($contribution['contribution_status'], 'Completed');
177 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
178 $p['trxn_id'] = '3847';
179 $p['invoice_id'] = '3847';
181 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
183 // Now we have 2 - test getcount.
184 $contribution = $this->callAPISuccess('contribution', 'getcount', []);
185 $this->assertEquals(2, $contribution);
186 // Test id only format.
187 $contribution = $this->callAPISuccess('contribution', 'get', [
188 'id' => $this->_contribution
['id'],
189 'format.only_id' => 1,
191 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
192 // Test id only format.
193 $contribution = $this->callAPISuccess('contribution', 'get', [
194 'id' => $contribution2['id'],
195 'format.only_id' => 1,
197 $this->assertEquals($contribution2['id'], $contribution);
199 $contribution = $this->callAPISuccess('contribution', 'get', [
200 'id' => $this->_contribution
['id'],
202 $this->assertEquals(1, $contribution['count']);
204 // Test get by contact id works.
205 $contribution = $this->callAPISuccess('contribution', 'get', ['contact_id' => $this->_individualId
]);
207 $this->assertEquals(2, $contribution['count']);
208 $this->callAPISuccess('Contribution', 'Delete', [
209 'id' => $this->_contribution
['id'],
211 $this->callAPISuccess('Contribution', 'Delete', [
212 'id' => $contribution2['id'],
217 * Test that test contributions can be retrieved.
219 * @throws \CRM_Core_Exception
221 public function testGetTestContribution() {
222 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, ['is_test' => 1]));
223 $this->callAPISuccessGetSingle('Contribution', ['is_test' => 1]);
227 * Test Creating a check contribution with original check_number field
229 public function testCreateCheckContribution() {
230 $params = $this->_params
;
231 $params['contribution_check_number'] = 'bouncer';
232 $params['payment_instrument_id'] = 'Check';
233 $params['cancel_date'] = 'yesterday';
234 $params['receipt_date'] = 'yesterday';
235 $params['thankyou_date'] = 'yesterday';
236 $params['revenue_recognition_date'] = 'yesterday';
237 $params['amount_level'] = 'Unreasonable';
238 $params['cancel_reason'] = 'You lose sucker';
239 $params['creditnote_id'] = 'sudo rm -rf';
240 $params['tax_amount'] = '1';
241 $address = $this->callAPISuccess('Address', 'create', [
242 'street_address' => 'Knockturn Alley',
243 'contact_id' => $this->_individualId
,
244 'location_type_id' => 'Home',
246 $params['address_id'] = $address['id'];
247 $contributionPage = $this->contributionPageCreate();
248 $params['contribution_page_id'] = $contributionPage['id'];
249 $params['campaign_id'] = $this->campaignCreate();
250 $contributionID = $this->contributionCreate($params);
251 $getResult = $this->callAPISuccess('Contribution', 'get', ['id' => $contributionID]);
252 $this->assertEquals('bouncer', $getResult['values'][$contributionID]['check_number']);
253 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['entity_id' => $contributionID, 'entity_table' => 'civicrm_contribution']);
254 foreach ($entityFinancialTrxn['values'] as $eft) {
255 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'get', ['id' => $eft['financial_trxn_id']]);
256 $this->assertEquals('bouncer', $financialTrxn['values'][$financialTrxn['id']]['check_number']);
261 * Test the 'return' param works for all fields.
263 public function testGetContributionReturnFunctionality() {
264 $params = $this->_params
;
265 $params['contribution_check_number'] = 'bouncer';
266 $params['payment_instrument_id'] = 'Check';
267 $params['cancel_date'] = 'yesterday';
268 $params['receipt_date'] = 'yesterday';
269 $params['thankyou_date'] = 'yesterday';
270 $params['revenue_recognition_date'] = 'yesterday';
271 $params['amount_level'] = 'Unreasonable';
272 $params['cancel_reason'] = 'You lose sucker';
273 $params['creditnote_id'] = 'sudo rm -rf';
274 $params['tax_amount'] = '1';
275 $address = $this->callAPISuccess('Address', 'create', [
276 'street_address' => 'Knockturn Alley',
277 'contact_id' => $this->_individualId
,
278 'location_type_id' => 'Home',
280 $params['address_id'] = $address['id'];
281 $contributionPage = $this->contributionPageCreate();
282 $params['contribution_page_id'] = $contributionPage['id'];
283 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', [
284 'contact_id' => $this->_individualId
,
285 'frequency_interval' => 1,
288 $params['contribution_recur_id'] = $contributionRecur['id'];
290 $params['campaign_id'] = $this->campaignCreate();
292 $contributionID = $this->contributionCreate($params);
294 // update contribution with invoice number
295 $params = array_merge($params, [
296 'id' => $contributionID,
297 'invoice_number' => CRM_Utils_Array
::value('invoice_prefix', Civi
::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
299 'invoice_id' => 6789,
301 $contributionID = $this->contributionCreate($params);
303 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]);
304 $this->assertEquals('bouncer', $contribution['check_number']);
305 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
307 $fields = CRM_Contribute_BAO_Contribution
::fields();
308 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
309 // need to specify them.
310 $fields['address_id'] = $fields['contribution_address_id'];
311 $fields['check_number'] = $fields['contribution_check_number'];
314 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
315 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
316 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'contribution_cancel_date', 'cancel_reason',
317 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
318 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
319 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
321 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
322 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
323 // covered as the test contract now guarantees them in the return array.
324 $this->assertEquals($missingFields, [29 => 'decoy'], 'A field which was covered by the test contract has changed.');
325 foreach ($fields as $fieldName => $fieldSpec) {
326 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID, 'return' => $fieldName]);
327 $returnField = $fieldName;
328 if ($returnField == 'contribution_contact_id') {
329 $returnField = 'contact_id';
331 $this->assertTrue((!empty($contribution[$returnField]) ||
$contribution[$returnField] === "0"), $returnField);
333 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['entity_id' => $contributionID, 'entity_table' => 'civicrm_contribution']);
334 foreach ($entityFinancialTrxn['values'] as $eft) {
335 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'get', ['id' => $eft['financial_trxn_id']]);
336 $this->assertEquals('bouncer', $financialTrxn['values'][$financialTrxn['id']]['check_number']);
341 * Test cancel reason works as a filter.
343 public function testFilterCancelReason() {
344 $params = $this->_params
;
345 $params['cancel_date'] = 'yesterday';
346 $params['cancel_reason'] = 'You lose sucker';
347 $this->callAPISuccess('Contribution', 'create', $params);
348 $params = $this->_params
;
349 $params['cancel_date'] = 'yesterday';
350 $params['cancel_reason'] = 'You are a winner';
351 $this->callAPISuccess('Contribution', 'create', $params);
352 $this->callAPISuccessGetCount('Contribution', ['cancel_reason' => 'You are a winner'], 1);
356 * We need to ensure previous tested behaviour still works as part of the api contract.
358 public function testGetContributionLegacyBehaviour() {
360 'contact_id' => $this->_individualId
,
361 'receive_date' => '2010-01-20',
362 'total_amount' => 100.00,
363 'contribution_type_id' => $this->_financialTypeId
,
364 'non_deductible_amount' => 10.00,
365 'fee_amount' => 5.00,
366 'net_amount' => 95.00,
368 'invoice_id' => 78910,
370 'contribution_status_id' => 1,
372 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
375 'contribution_id' => $this->_contribution
['id'],
377 $contribution = $this->callAPISuccess('contribution', 'get', $params);
378 $financialParams['id'] = $this->_financialTypeId
;
380 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
382 $this->assertEquals(1, $contribution['count']);
383 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
384 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
385 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
386 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
387 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
388 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
389 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
390 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
391 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
392 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
393 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
395 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
396 $p['trxn_id'] = '3847';
397 $p['invoice_id'] = '3847';
399 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
401 // now we have 2 - test getcount
402 $contribution = $this->callAPISuccess('contribution', 'getcount', []);
403 $this->assertEquals(2, $contribution);
404 //test id only format
405 $contribution = $this->callAPISuccess('contribution', 'get', [
406 'id' => $this->_contribution
['id'],
407 'format.only_id' => 1,
409 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
410 //test id only format
411 $contribution = $this->callAPISuccess('contribution', 'get', [
412 'id' => $contribution2['id'],
413 'format.only_id' => 1,
415 $this->assertEquals($contribution2['id'], $contribution);
416 $contribution = $this->callAPISuccess('contribution', 'get', [
417 'id' => $this->_contribution
['id'],
420 $this->assertEquals(1, $contribution['count']);
421 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
422 //test get by contact id works
423 $contribution = $this->callAPISuccess('contribution', 'get', ['contact_id' => $this->_individualId
]);
425 $this->assertEquals(2, $contribution['count']);
426 $this->callAPISuccess('Contribution', 'Delete', [
427 'id' => $this->_contribution
['id'],
429 $this->callAPISuccess('Contribution', 'Delete', [
430 'id' => $contribution2['id'],
435 * Create an contribution_id=FALSE and financial_type_id=Donation.
437 public function testCreateEmptyContributionIDUseDonation() {
439 'contribution_id' => FALSE,
442 'check_permissions' => FALSE,
443 'financial_type_id' => 'Donation',
445 $this->callAPISuccess('contribution', 'create', $params);
449 * Check with complete array + custom field.
451 * Note that the test is written on purpose without any
452 * variables specific to participant so it can be replicated into other entities
453 * and / or moved to the automated test suite
455 public function testCreateWithCustom() {
456 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
458 $params = $this->_params
;
459 $params['custom_' . $ids['custom_field_id']] = "custom string";
461 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
462 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
463 $check = $this->callAPISuccess($this->_entity
, 'get', [
464 'return.custom_' . $ids['custom_field_id'] => 1,
465 'id' => $result['id'],
467 $this->customFieldDelete($ids['custom_field_id']);
468 $this->customGroupDelete($ids['custom_group_id']);
469 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
473 * Check with complete array + custom field.
475 * Note that the test is written on purpose without any
476 * variables specific to participant so it can be replicated into other entities
477 * and / or moved to the automated test suite
479 public function testCreateGetFieldsWithCustom() {
480 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
481 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
482 $result = $this->callAPISuccess('Contribution', 'getfields', []);
483 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
484 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
485 $this->customFieldDelete($ids['custom_field_id']);
486 $this->customGroupDelete($ids['custom_group_id']);
487 $this->customFieldDelete($idsContact['custom_field_id']);
488 $this->customGroupDelete($idsContact['custom_group_id']);
491 public function testCreateContributionNoLineItems() {
494 'contact_id' => $this->_individualId
,
495 'receive_date' => '20120511',
496 'total_amount' => 100.00,
497 'financial_type_id' => $this->_financialTypeId
,
498 'payment_instrument_id' => 1,
499 'non_deductible_amount' => 10.00,
500 'fee_amount' => 50.00,
501 'net_amount' => 90.00,
503 'invoice_id' => 67890,
505 'contribution_status_id' => 1,
509 $contribution = $this->callAPISuccess('contribution', 'create', $params);
510 $lineItems = $this->callAPISuccess('line_item', 'get', [
511 'entity_id' => $contribution['id'],
512 'entity_table' => 'civicrm_contribution',
515 $this->assertEquals(0, $lineItems['count']);
519 * Test checks that passing in line items suppresses the create mechanism.
521 public function testCreateContributionChainedLineItems() {
523 'contact_id' => $this->_individualId
,
524 'receive_date' => '20120511',
525 'total_amount' => 100.00,
526 'financial_type_id' => $this->_financialTypeId
,
527 'payment_instrument_id' => 1,
528 'non_deductible_amount' => 10.00,
529 'fee_amount' => 50.00,
530 'net_amount' => 90.00,
532 'invoice_id' => 67890,
534 'contribution_status_id' => 1,
536 'api.line_item.create' => [
538 'price_field_id' => 1,
540 'line_total' => '20',
541 'unit_price' => '10',
544 'price_field_id' => 1,
546 'line_total' => '80',
547 'unit_price' => '80',
552 $description = "Create Contribution with Nested Line Items.";
553 $subfile = "CreateWithNestedLineItems";
554 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
556 $lineItems = $this->callAPISuccess('line_item', 'get', [
557 'entity_id' => $contribution['id'],
558 'contribution_id' => $contribution['id'],
559 'entity_table' => 'civicrm_contribution',
562 $this->assertEquals(2, $lineItems['count']);
565 public function testCreateContributionOffline() {
567 'contact_id' => $this->_individualId
,
568 'receive_date' => '20120511',
569 'total_amount' => 100.00,
570 'financial_type_id' => 1,
572 'invoice_id' => 67890,
574 'contribution_status_id' => 1,
577 $contribution = $this->callAPISuccess('contribution', 'create', $params);
578 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
579 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
580 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
581 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
582 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
583 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
584 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
585 $lineItems = $this->callAPISuccess('line_item', 'get', [
586 'entity_id' => $contribution['id'],
587 'contribution_id' => $contribution['id'],
588 'entity_table' => 'civicrm_contribution',
591 $this->assertEquals(1, $lineItems['count']);
592 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
593 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
594 $this->_checkFinancialRecords($contribution, 'offline');
595 $this->contributionGetnCheck($params, $contribution['id']);
599 * Test create with valid payment instrument.
601 public function testCreateContributionWithPaymentInstrument() {
602 $params = $this->_params +
['payment_instrument' => 'EFT'];
603 $contribution = $this->callAPISuccess('contribution', 'create', $params);
604 $contribution = $this->callAPISuccess('contribution', 'get', [
606 'id' => $contribution['id'],
608 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
609 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
611 $this->callAPISuccess('contribution', 'create', [
612 'id' => $contribution['id'],
613 'payment_instrument' => 'Credit Card',
615 $contribution = $this->callAPISuccess('contribution', 'get', [
617 'id' => $contribution['id'],
619 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
620 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
623 public function testGetContributionByPaymentInstrument() {
624 $params = $this->_params +
['payment_instrument' => 'EFT'];
625 $params2 = $this->_params +
['payment_instrument' => 'Cash'];
626 $this->callAPISuccess('contribution', 'create', $params);
627 $this->callAPISuccess('contribution', 'create', $params2);
628 $contribution = $this->callAPISuccess('contribution', 'get', [
630 'contribution_payment_instrument' => 'Cash',
632 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
633 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
634 $this->assertEquals(1, $contribution['count']);
635 $contribution = $this->callAPISuccess('contribution', 'get', ['sequential' => 1, 'payment_instrument' => 'Cash']);
636 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
637 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
638 $this->assertEquals(1, $contribution['count']);
639 $contribution = $this->callAPISuccess('contribution', 'get', [
641 'payment_instrument_id' => 5,
643 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
644 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
645 $this->assertEquals(1, $contribution['count']);
646 $contribution = $this->callAPISuccess('contribution', 'get', [
648 'payment_instrument' => 'EFT',
650 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
651 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
652 $this->assertEquals(1, $contribution['count']);
653 $contribution = $this->callAPISuccess('contribution', 'create', [
654 'id' => $contribution['id'],
655 'payment_instrument' => 'Credit Card',
657 $contribution = $this->callAPISuccess('contribution', 'get', ['sequential' => 1, 'id' => $contribution['id']]);
658 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
659 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
660 $this->assertEquals(1, $contribution['count']);
664 * CRM-16227 introduces invoice_id as a parameter.
666 public function testGetContributionByInvoice() {
667 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, ['invoice_id' => 'curly']));
668 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), ['invoice_id' => 'churlish']);
669 $this->callAPISuccessGetCount('Contribution', [], 2);
670 $this->callAPISuccessGetSingle('Contribution', ['invoice_id' => 'curly']);
671 // The following don't work. They are the format we are trying to introduce but although the form uses this format
672 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
673 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
674 // come out of convertFormValues
675 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
676 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
677 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
678 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
683 * Check the credit note retrieval is case insensitive.
685 public function testGetCreditNoteCaseInsensitive() {
686 $this->contributionCreate(['contact_id' => $this->_individualId
]);
687 $this->contributionCreate(['creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId
, 'invoice_id' => rand(), 'trxn_id' => rand()]);
688 $contribution = $this->callAPISuccess('Contribution', 'getsingle', ['creditnote_id' => 'CN1234']);
689 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
693 * Test retrieval by total_amount works.
697 public function testGetContributionByTotalAmount() {
698 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, ['total_amount' => '5']));
699 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, ['total_amount' => '10']));
700 $this->callAPISuccessGetCount('Contribution', ['total_amount' => 10], 1);
701 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => 6]], 1);
702 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => 0]], 2);
703 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['>' => -5]], 2);
704 $this->callAPISuccessGetCount('Contribution', ['total_amount' => ['<' => 0]], 0);
705 $this->callAPISuccessGetCount('Contribution', [], 2);
709 * @dataProvider createLocalizedContributionDataProvider
710 * @param $totalAmount
711 * @param $decimalPoint
712 * @param $thousandSeparator
714 * @param $expectedResult
717 public function testCreateLocalizedContribution($totalAmount, $decimalPoint, $thousandSeparator, $currency, $expectedResult) {
718 $this->setDefaultCurrency($currency);
719 $this->setMonetaryDecimalPoint($decimalPoint);
720 $this->setMonetaryThousandSeparator($thousandSeparator);
723 'contact_id' => $this->_individualId
,
724 'receive_date' => '20120511',
725 'total_amount' => $totalAmount,
726 'financial_type_id' => $this->_financialTypeId
,
727 'contribution_status_id' => 1,
730 if ($expectedResult) {
731 $this->callAPISuccess('Contribution', 'create', $_params);
734 $this->callAPIFailure('Contribution', 'create', $_params);
741 public function createLocalizedContributionDataProvider() {
743 [10, '.', ',', 'USD', TRUE],
744 ['145.0E+3', '.', ',', 'USD', FALSE],
745 ['10', '.', ',', 'USD', TRUE],
746 [-10, '.', ',', 'USD', TRUE],
747 ['-10', '.', ',', 'USD', TRUE],
748 ['-10foo', '.', ',', 'USD', FALSE],
749 ['-10.0345619', '.', ',', 'USD', TRUE],
750 ['-10.010,4345619', '.', ',', 'USD', TRUE],
751 ['10.0104345619', '.', ',', 'USD', TRUE],
752 ['-0', '.', ',', 'USD', TRUE],
753 ['-.1', '.', ',', 'USD', TRUE],
754 ['.1', '.', ',', 'USD', TRUE],
755 // Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale
756 ['$1,234,567.89', '.', ',', 'USD', TRUE],
757 ['-$1,234,567.89', '.', ',', 'USD', TRUE],
758 ['$-1,234,567.89', '.', ',', 'USD', TRUE],
759 // This is the float format. Encapsulated in strings
760 ['1234567.89', '.', ',', 'USD', TRUE],
761 // This is the float format.
762 [1234567.89, '.', ',', 'USD', TRUE],
763 // Test EURO currency
764 ['€1,234,567.89', '.', ',', 'EUR', TRUE],
765 ['-€1,234,567.89', '.', ',', 'EUR', TRUE],
766 ['€-1,234,567.89', '.', ',', 'EUR', TRUE],
767 // This is the float format. Encapsulated in strings
768 ['1234567.89', '.', ',', 'EUR', TRUE],
769 // This is the float format.
770 [1234567.89, '.', ',', 'EUR', TRUE],
771 // Test Norwegian KR currency
772 ['kr1,234,567.89', '.', ',', 'NOK', TRUE],
773 ['kr 1,234,567.89', '.', ',', 'NOK', TRUE],
774 ['-kr1,234,567.89', '.', ',', 'NOK', TRUE],
775 ['-kr 1,234,567.89', '.', ',', 'NOK', TRUE],
776 ['kr-1,234,567.89', '.', ',', 'NOK', TRUE],
777 ['kr -1,234,567.89', '.', ',', 'NOK', TRUE],
778 // This is the float format. Encapsulated in strings
779 ['1234567.89', '.', ',', 'NOK', TRUE],
780 // This is the float format.
781 [1234567.89, '.', ',', 'NOK', TRUE],
782 // Test different localization options: , as decimal separator and dot as thousand separator
783 ['$1.234.567,89', ',', '.', 'USD', TRUE],
784 ['-$1.234.567,89', ',', '.', 'USD', TRUE],
785 ['$-1.234.567,89', ',', '.', 'USD', TRUE],
786 ['1.234.567,89', ',', '.', 'USD', TRUE],
787 // This is the float format. Encapsulated in strings
788 ['1234567.89', ',', '.', 'USD', TRUE],
789 // This is the float format.
790 [1234567.89, ',', '.', 'USD', TRUE],
791 ['$1,234,567.89', ',', '.', 'USD', FALSE],
792 ['-$1,234,567.89', ',', '.', 'USD', FALSE],
793 ['$-1,234,567.89', ',', '.', 'USD', FALSE],
794 // Now with a space as thousand separator
795 ['$1 234 567,89', ',', ' ', 'USD', TRUE],
796 ['-$1 234 567,89', ',', ' ', 'USD', TRUE],
797 ['$-1 234 567,89', ',', ' ', 'USD', TRUE],
798 ['1 234 567,89', ',', ' ', 'USD', TRUE],
799 // This is the float format. Encapsulated in strings
800 ['1234567.89', ',', ' ', 'USD', TRUE],
801 // This is the float format.
802 [1234567.89, ',', ' ', 'USD', TRUE],
807 * Create test with unique field name on source.
809 public function testCreateContributionSource() {
812 'contact_id' => $this->_individualId
,
813 'receive_date' => date('Ymd'),
814 'total_amount' => 100.00,
815 'financial_type_id' => $this->_financialTypeId
,
816 'payment_instrument_id' => 1,
817 'non_deductible_amount' => 10.00,
818 'fee_amount' => 50.00,
819 'net_amount' => 90.00,
821 'invoice_id' => 67890,
822 'contribution_source' => 'SSF',
823 'contribution_status_id' => 1,
826 $contribution = $this->callAPISuccess('contribution', 'create', $params);
827 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
828 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
832 * Create test with unique field name on source.
834 * @param string $thousandSeparator
835 * punctuation used to refer to thousands.
837 * @dataProvider getThousandSeparators
839 public function testCreateDefaultNow($thousandSeparator) {
840 $this->setCurrencySeparators($thousandSeparator);
841 $params = $this->_params
;
842 unset($params['receive_date'], $params['net_amount']);
844 $params['total_amount'] = $this->formatMoneyInput(5000.77);
845 $params['fee_amount'] = $this->formatMoneyInput(.77);
846 $params['skipCleanMoney'] = FALSE;
848 $contribution = $this->callAPISuccess('contribution', 'create', $params);
849 $contribution = $this->callAPISuccessGetSingle('contribution', ['id' => $contribution['id']]);
850 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
851 $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77));
852 $this->assertEquals(.77, $contribution['fee_amount']);
853 $this->assertEquals(5000, $contribution['net_amount']);
857 * Create test with unique field name on source.
859 public function testCreateContributionNullOutThankyouDate() {
861 $params = $this->_params
;
862 $params['thankyou_date'] = 'yesterday';
864 $contribution = $this->callAPISuccess('contribution', 'create', $params);
865 $contribution = $this->callAPISuccessGetSingle('contribution', ['id' => $contribution['id']]);
866 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
868 $params['thankyou_date'] = 'null';
869 $contribution = $this->callAPISuccess('contribution', 'create', $params);
870 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
874 * Create test with unique field name on source.
876 public function testCreateContributionSourceInvalidContact() {
880 'receive_date' => date('Ymd'),
881 'total_amount' => 100.00,
882 'financial_type_id' => $this->_financialTypeId
,
883 'payment_instrument_id' => 1,
884 'non_deductible_amount' => 10.00,
885 'fee_amount' => 50.00,
886 'net_amount' => 90.00,
888 'invoice_id' => 67890,
889 'contribution_source' => 'SSF',
890 'contribution_status_id' => 1,
893 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
896 public function testCreateContributionSourceInvalidContContact() {
899 'contribution_contact_id' => 999,
900 'receive_date' => date('Ymd'),
901 'total_amount' => 100.00,
902 'financial_type_id' => $this->_financialTypeId
,
903 'payment_instrument_id' => 1,
904 'non_deductible_amount' => 10.00,
905 'fee_amount' => 50.00,
906 'net_amount' => 90.00,
908 'invoice_id' => 67890,
909 'contribution_source' => 'SSF',
910 'contribution_status_id' => 1,
913 $this->callAPIFailure('contribution', 'create', $params);
917 * Test note created correctly.
919 public function testCreateContributionWithNote() {
920 $description = "Demonstrates creating contribution with Note Entity.";
921 $subfile = "ContributionCreateWithNote";
923 'contact_id' => $this->_individualId
,
924 'receive_date' => '2012-01-01',
925 'total_amount' => 100.00,
926 'financial_type_id' => $this->_financialTypeId
,
927 'payment_instrument_id' => 1,
928 'non_deductible_amount' => 10.00,
929 'fee_amount' => 50.00,
930 'net_amount' => 90.00,
932 'invoice_id' => 67890,
934 'contribution_status_id' => 1,
935 'note' => 'my contribution note',
938 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
939 $result = $this->callAPISuccess('note', 'get', [
940 'entity_table' => 'civicrm_contribution',
941 'entity_id' => $contribution['id'],
944 $this->assertEquals('my contribution note', $result['values'][0]['note']);
945 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
948 public function testCreateContributionWithNoteUniqueNameAliases() {
950 'contact_id' => $this->_individualId
,
951 'receive_date' => '2012-01-01',
952 'total_amount' => 100.00,
953 'financial_type_id' => $this->_financialTypeId
,
954 'payment_instrument_id' => 1,
955 'non_deductible_amount' => 10.00,
956 'fee_amount' => 50.00,
957 'net_amount' => 90.00,
959 'invoice_id' => 67890,
961 'contribution_status_id' => 1,
962 'contribution_note' => 'my contribution note',
965 $contribution = $this->callAPISuccess('contribution', 'create', $params);
966 $result = $this->callAPISuccess('note', 'get', [
967 'entity_table' => 'civicrm_contribution',
968 'entity_id' => $contribution['id'],
971 $this->assertEquals('my contribution note', $result['values'][0]['note']);
972 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
976 * This is the test for creating soft credits.
978 public function testCreateContributionWithSoftCredit() {
979 $description = "Demonstrates creating contribution with SoftCredit.";
980 $subfile = "ContributionCreateWithSoftCredit";
981 $contact2 = $this->callAPISuccess('Contact', 'create', [
982 'display_name' => 'superman',
983 'contact_type' => 'Individual',
986 'contact_id' => $contact2['id'],
988 'soft_credit_type_id' => 3,
991 $params = $this->_params +
['soft_credit' => [1 => $softParams]];
992 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
993 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
995 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
996 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
997 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
999 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
1000 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
1003 public function testCreateContributionWithSoftCreditDefaults() {
1004 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
1005 $subfile = "ContributionCreateWithSoftCreditDefaults";
1006 $contact2 = $this->callAPISuccess('Contact', 'create', [
1007 'display_name' => 'superman',
1008 'contact_type' => 'Individual',
1010 $params = $this->_params +
[
1011 'soft_credit_to' => $contact2['id'],
1013 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
1014 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
1016 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
1017 // Default soft credit amount = contribution.total_amount
1018 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
1019 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
1021 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
1022 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
1026 * Test creating contribution with Soft Credit by passing in honor_contact_id.
1028 public function testCreateContributionWithHonoreeContact() {
1029 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
1030 $subfile = "ContributionCreateWithHonoreeContact";
1031 $contact2 = $this->callAPISuccess('Contact', 'create', [
1032 'display_name' => 'superman',
1033 'contact_type' => 'Individual',
1035 $params = $this->_params +
[
1036 'honor_contact_id' => $contact2['id'],
1038 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
1039 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'soft_credit', 'sequential' => 1]);
1041 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
1042 // Default soft credit amount = contribution.total_amount
1043 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
1044 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
1045 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
1046 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
1048 $this->callAPISuccess('contribution', 'delete', ['id' => $contribution['id']]);
1049 $this->callAPISuccess('contact', 'delete', ['id' => $contact2['id']]);
1053 * Test using example code.
1055 public function testContributionCreateExample() {
1056 //make sure at least on page exists since there is a truncate in tear down
1057 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1058 require_once 'api/v3/examples/Contribution/Create.ex.php';
1059 $result = contribution_create_example();
1060 $id = $result['id'];
1061 $expectedResult = contribution_create_expectedresult();
1062 $this->checkArrayEquals($expectedResult, $result);
1063 $this->contributionDelete($id);
1067 * Function tests that additional financial records are created when fee amount is recorded.
1069 public function testCreateContributionWithFee() {
1071 'contact_id' => $this->_individualId
,
1072 'receive_date' => '20120511',
1073 'total_amount' => 100.00,
1075 'financial_type_id' => 1,
1077 'invoice_id' => 67890,
1079 'contribution_status_id' => 1,
1082 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1083 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1084 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1085 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
1086 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
1087 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1088 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1089 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1090 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1091 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
1093 $lineItems = $this->callAPISuccess('line_item', 'get', [
1095 'entity_id' => $contribution['id'],
1096 'entity_table' => 'civicrm_contribution',
1099 $this->assertEquals(1, $lineItems['count']);
1100 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
1101 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
1102 $lineItems = $this->callAPISuccess('line_item', 'get', [
1104 'entity_id' => $contribution['id'],
1105 'contribution_id' => $contribution['id'],
1106 'entity_table' => 'civicrm_contribution',
1109 $this->assertEquals(1, $lineItems['count']);
1110 $this->_checkFinancialRecords($contribution, 'feeAmount');
1114 * Function tests that additional financial records are created when online contribution is created.
1116 public function testCreateContributionOnline() {
1117 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1118 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1119 $this->assertAPISuccess($contributionPage);
1121 'contact_id' => $this->_individualId
,
1122 'receive_date' => '20120511',
1123 'total_amount' => 100.00,
1124 'financial_type_id' => 1,
1125 'contribution_page_id' => $contributionPage['id'],
1126 'payment_processor' => $this->paymentProcessorID
,
1128 'invoice_id' => 67890,
1130 'contribution_status_id' => 1,
1134 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1135 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1136 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1137 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1138 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1139 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1140 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1141 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
1142 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', [
1143 'id' => $this->paymentProcessorID
,
1144 'return' => 'payment_instrument_id',
1146 $this->_checkFinancialRecords($contribution, 'online');
1150 * Check handling of financial type.
1152 * In the interests of removing financial type / contribution type checks from
1153 * legacy format function lets test that the api is doing this for us
1155 public function testCreateInvalidFinancialType() {
1156 $params = $this->_params
;
1157 $params['financial_type_id'] = 99999;
1158 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
1162 * Check handling of financial type.
1164 * In the interests of removing financial type / contribution type checks from
1165 * legacy format function lets test that the api is doing this for us
1167 public function testValidNamedFinancialType() {
1168 $params = $this->_params
;
1169 $params['financial_type_id'] = 'Donation';
1170 $this->callAPISuccess($this->_entity
, 'create', $params);
1174 * Tests that additional financial records are created.
1176 * Checks when online contribution with pay later option is created
1178 public function testCreateContributionPayLaterOnline() {
1179 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1180 $this->_pageParams
['is_pay_later'] = 1;
1181 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1182 $this->assertAPISuccess($contributionPage);
1184 'contact_id' => $this->_individualId
,
1185 'receive_date' => '20120511',
1186 'total_amount' => 100.00,
1187 'financial_type_id' => 1,
1188 'contribution_page_id' => $contributionPage['id'],
1190 'is_pay_later' => 1,
1191 'invoice_id' => 67890,
1193 'contribution_status_id' => 2,
1197 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
1198 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1199 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1200 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1201 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1202 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1203 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1204 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1205 $this->_checkFinancialRecords($contribution, 'payLater');
1209 * Function tests that additional financial records are created for online contribution with pending option.
1211 public function testCreateContributionPendingOnline() {
1212 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
1213 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
1214 $this->assertAPISuccess($contributionPage);
1216 'contact_id' => $this->_individualId
,
1217 'receive_date' => '20120511',
1218 'total_amount' => 100.00,
1219 'financial_type_id' => 1,
1220 'contribution_page_id' => $contributionPage['id'],
1222 'invoice_id' => 67890,
1224 'contribution_status_id' => 2,
1227 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1228 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
1229 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1230 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1231 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1232 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1233 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1234 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1235 $this->_checkFinancialRecords($contribution, 'pending');
1239 * Test that BAO defaults work.
1241 public function testCreateBAODefaults() {
1242 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
1243 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1244 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1245 'id' => $contribution['id'],
1246 'api.contribution.delete' => 1,
1248 $this->assertEquals(1, $contribution['contribution_status_id']);
1249 $this->assertEquals('Check', $contribution['payment_instrument']);
1250 $this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
1254 * Test that getsingle can be chained with delete.
1256 public function testDeleteChainedGetSingle() {
1257 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1258 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1259 'id' => $contribution['id'],
1260 'api.contribution.delete' => 1,
1262 $this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
1266 * Function tests that line items, financial records are updated when contribution amount is changed.
1268 public function testCreateUpdateContributionChangeTotal() {
1269 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1270 $lineItems = $this->callAPISuccess('line_item', 'getvalue', [
1272 'entity_id' => $contribution['id'],
1273 'entity_table' => 'civicrm_contribution',
1275 'return' => 'line_total',
1277 $this->assertEquals('100.00', $lineItems);
1278 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1279 // Financial trxn SUM = 100 + 5 (fee)
1280 $this->assertEquals('105.00', $trxnAmount);
1283 'id' => $contribution['id'],
1284 'total_amount' => '125',
1286 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1288 $lineItems = $this->callAPISuccess('line_item', 'getvalue', [
1290 'entity_id' => $contribution['id'],
1291 'entity_table' => 'civicrm_contribution',
1293 'return' => 'line_total',
1296 $this->assertEquals('125.00', $lineItems);
1297 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1299 // Financial trxn SUM = 125 + 5 (fee).
1300 $this->assertEquals('130.00', $trxnAmount);
1301 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1305 * Function tests that line items, financial records are updated when pay later contribution is received.
1307 public function testCreateUpdateContributionPayLater() {
1309 'contact_id' => $this->_individualId
,
1310 'receive_date' => '2012-01-01',
1311 'total_amount' => 100.00,
1312 'financial_type_id' => $this->_financialTypeId
,
1313 'payment_instrument_id' => 1,
1314 'contribution_status_id' => 2,
1315 'is_pay_later' => 1,
1318 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1320 $newParams = array_merge($contribParams, [
1321 'id' => $contribution['id'],
1322 'contribution_status_id' => 1,
1324 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1325 $contribution = $contribution['values'][$contribution['id']];
1326 $this->assertEquals($contribution['contribution_status_id'], '1');
1327 $this->_checkFinancialItem($contribution['id'], 'paylater');
1328 $this->_checkFinancialTrxn($contribution, 'payLater');
1332 * Function tests that financial records are updated when Payment Instrument is changed.
1334 public function testCreateUpdateContributionPaymentInstrument() {
1335 $instrumentId = $this->_addPaymentInstrument();
1337 'contact_id' => $this->_individualId
,
1338 'total_amount' => 100.00,
1339 'financial_type_id' => $this->_financialTypeId
,
1340 'payment_instrument_id' => 4,
1341 'contribution_status_id' => 1,
1344 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1346 $newParams = array_merge($contribParams, [
1347 'id' => $contribution['id'],
1348 'payment_instrument_id' => $instrumentId,
1350 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1351 $this->assertAPISuccess($contribution);
1352 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1354 // cleanup - delete created payment instrument
1355 $this->_deletedAddedPaymentInstrument();
1359 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1361 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1362 $instrumentId = $this->_addPaymentInstrument();
1364 'contact_id' => $this->_individualId
,
1365 'total_amount' => -100.00,
1366 'financial_type_id' => $this->_financialTypeId
,
1367 'payment_instrument_id' => 4,
1368 'contribution_status_id' => 1,
1371 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1373 $newParams = array_merge($contribParams, [
1374 'id' => $contribution['id'],
1375 'payment_instrument_id' => $instrumentId,
1377 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1378 $this->assertAPISuccess($contribution);
1379 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1381 // cleanup - delete created payment instrument
1382 $this->_deletedAddedPaymentInstrument();
1386 * Function tests that financial records are added when Contribution is Refunded.
1388 * @throws \CRM_Core_Exception
1390 public function testCreateUpdateContributionRefund() {
1391 $contributionParams = [
1392 'contact_id' => $this->_individualId
,
1393 'receive_date' => '2012-01-01',
1394 'total_amount' => 100.00,
1395 'financial_type_id' => $this->_financialTypeId
,
1396 'payment_instrument_id' => 4,
1397 'contribution_status_id' => 1,
1398 'trxn_id' => 'original_payment',
1400 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1401 $newParams = array_merge($contributionParams, [
1402 'id' => $contribution['id'],
1403 'contribution_status_id' => 'Refunded',
1404 'cancel_date' => '2015-01-01 09:00',
1405 'refund_trxn_id' => 'the refund',
1408 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1409 $this->_checkFinancialTrxn($contribution, 'refund');
1410 $this->_checkFinancialItem($contribution['id'], 'refund');
1411 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', [
1412 'id' => $contribution['id'],
1413 'return' => 'trxn_id',
1418 * Refund a contribution for a financial type with a contra account.
1420 * CRM-17951 the contra account is a financial account with a relationship to a
1421 * financial type. It is not always configured but should be reflected
1422 * in the financial_trxn & financial_item table if it is.
1424 * @throws \CRM_Core_Exception
1426 public function testCreateUpdateChargebackContributionDefaultAccount() {
1427 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1428 $this->callAPISuccess('Contribution', 'create', [
1429 'id' => $contribution['id'],
1430 'contribution_status_id' => 'Chargeback',
1432 $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Chargeback']);
1434 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
1435 'contribution_id' => $contribution['id'],
1436 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1438 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1439 $this->callAPISuccessGetSingle('FinancialTrxn', [
1440 'total_amount' => -100,
1441 'status_id' => 'Chargeback',
1442 'to_financial_account_id' => 6,
1447 * Refund a contribution for a financial type with a contra account.
1449 * CRM-17951 the contra account is a financial account with a relationship to a
1450 * financial type. It is not always configured but should be reflected
1451 * in the financial_trxn & financial_item table if it is.
1453 * @throws \CRM_Core_Exception
1455 public function testCreateUpdateChargebackContributionCustomAccount() {
1456 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', [
1457 'name' => 'Chargeback Account',
1458 'is_active' => TRUE,
1461 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [
1462 'entity_id' => $this->_financialTypeId
,
1463 'entity_table' => 'civicrm_financial_type',
1464 'account_relationship' => 'Chargeback Account is',
1465 'financial_account_id' => 'Chargeback Account',
1468 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1469 $this->callAPISuccess('Contribution', 'create', [
1470 'id' => $contribution['id'],
1471 'contribution_status_id' => 'Chargeback',
1473 $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Chargeback']);
1475 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
1476 'contribution_id' => $contribution['id'],
1477 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1479 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1481 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
1482 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialAccount['id']]);
1483 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
1487 * Refund a contribution for a financial type with a contra account.
1489 * CRM-17951 the contra account is a financial account with a relationship to a
1490 * financial type. It is not always configured but should be reflected
1491 * in the financial_trxn & financial_item table if it is.
1493 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1494 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', [
1495 'name' => 'Refund Account',
1496 'is_active' => TRUE,
1499 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [
1500 'entity_id' => $this->_financialTypeId
,
1501 'entity_table' => 'civicrm_financial_type',
1502 'account_relationship' => 'Credit/Contra Revenue Account is',
1503 'financial_account_id' => 'Refund Account',
1506 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
1507 $this->callAPISuccess('Contribution', 'create', [
1508 'id' => $contribution['id'],
1509 'contribution_status_id' => 'Refunded',
1512 $lineItems = $this->callAPISuccessGetSingle('LineItem', [
1513 'contribution_id' => $contribution['id'],
1514 'api.FinancialItem.getsingle' => ['amount' => ['<' => 0]],
1516 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1518 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
1519 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $entityFinancialAccount['id']]);
1520 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $financialAccount['id']]);
1524 * Function tests that trxn_id is set when passed in.
1526 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1527 * when trxn_id is passed in.
1529 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1530 $contributionParams = [
1531 'contact_id' => $this->_individualId
,
1532 'receive_date' => '2012-01-01',
1533 'total_amount' => 100.00,
1534 'financial_type_id' => $this->_financialTypeId
,
1535 'payment_instrument_id' => 4,
1536 'contribution_status_id' => 1,
1537 'trxn_id' => 'original_payment',
1539 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1540 $newParams = array_merge($contributionParams, [
1541 'id' => $contribution['id'],
1542 'contribution_status_id' => 'Refunded',
1543 'cancel_date' => '2015-01-01 09:00',
1544 'trxn_id' => 'the refund',
1547 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1548 $this->_checkFinancialTrxn($contribution, 'refund');
1549 $this->_checkFinancialItem($contribution['id'], 'refund');
1550 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', [
1551 'id' => $contribution['id'],
1552 'return' => 'trxn_id',
1557 * Function tests that trxn_id is set when passed in.
1559 * Here we ensure that the civicrm_contribution.trxn_id is set
1560 * when trxn_id is passed in but if refund_trxn_id is different then that
1561 * is kept for the refund transaction.
1563 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1564 $contributionParams = [
1565 'contact_id' => $this->_individualId
,
1566 'receive_date' => '2012-01-01',
1567 'total_amount' => 100.00,
1568 'financial_type_id' => $this->_financialTypeId
,
1569 'payment_instrument_id' => 4,
1570 'contribution_status_id' => 1,
1571 'trxn_id' => 'original_payment',
1573 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1574 $newParams = array_merge($contributionParams, [
1575 'id' => $contribution['id'],
1576 'contribution_status_id' => 'Refunded',
1577 'cancel_date' => '2015-01-01 09:00',
1578 'trxn_id' => 'cont id',
1579 'refund_trxn_id' => 'the refund',
1582 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1583 $this->_checkFinancialTrxn($contribution, 'refund');
1584 $this->_checkFinancialItem($contribution['id'], 'refund');
1585 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', [
1586 'id' => $contribution['id'],
1587 'return' => 'trxn_id',
1592 * Function tests that refund_trxn_id is set when passed in empty.
1594 * Here we ensure that the civicrm_contribution.trxn_id is set
1595 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1596 * is kept for the refund transaction.
1598 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1599 $contributionParams = [
1600 'contact_id' => $this->_individualId
,
1601 'receive_date' => '2012-01-01',
1602 'total_amount' => 100.00,
1603 'financial_type_id' => $this->_financialTypeId
,
1604 'payment_instrument_id' => 4,
1605 'contribution_status_id' => 1,
1606 'trxn_id' => 'original_payment',
1608 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1609 $newParams = array_merge($contributionParams, [
1610 'id' => $contribution['id'],
1611 'contribution_status_id' => 'Refunded',
1612 'cancel_date' => '2015-01-01 09:00',
1613 'trxn_id' => 'cont id',
1614 'refund_trxn_id' => '',
1617 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1618 $this->_checkFinancialTrxn($contribution, 'refund', NULL, ['trxn_id' => NULL]);
1619 $this->_checkFinancialItem($contribution['id'], 'refund');
1620 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', [
1621 'id' => $contribution['id'],
1622 'return' => 'trxn_id',
1627 * Function tests invalid contribution status change.
1629 public function testCreateUpdateContributionInValidStatusChange() {
1632 'receive_date' => '2012-01-01',
1633 'total_amount' => 100.00,
1634 'financial_type_id' => 1,
1635 'payment_instrument_id' => 1,
1636 'contribution_status_id' => 1,
1638 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1639 $newParams = array_merge($contribParams, [
1640 'id' => $contribution['id'],
1641 'contribution_status_id' => 2,
1643 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1648 * Function tests that financial records are added when Pending Contribution is Canceled.
1650 public function testCreateUpdateContributionCancelPending() {
1651 // Enable & disable invoicing just to standardise the credit note id setting.
1652 // Longer term we want to separate that setting from 'taxAndInvoicing'.
1653 // and / or remove from core.
1654 $this->enableTaxAndInvoicing();
1656 'contact_id' => $this->_individualId
,
1657 'receive_date' => '2012-01-01',
1658 'total_amount' => 100.00,
1659 'financial_type_id' => $this->_financialTypeId
,
1660 'payment_instrument_id' => 1,
1661 'contribution_status_id' => 2,
1662 'is_pay_later' => 1,
1665 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1666 $newParams = array_merge($contribParams, [
1667 'id' => $contribution['id'],
1668 'contribution_status_id' => 3,
1669 'cancel_date' => '2012-02-02 09:00',
1671 //Check if trxn_date is same as cancel_date.
1673 'trxn_date' => '2012-02-02 09:00:00',
1675 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1676 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1677 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1678 $this->assertEquals('CN_1', $contribution['values'][$contribution['id']]['creditnote_id']);
1679 $this->disableTaxAndInvoicing();
1683 * Function tests that financial records are added when Financial Type is Changed.
1685 * @throws \CRM_Core_Exception
1687 public function testCreateUpdateContributionChangeFinancialType() {
1689 'contact_id' => $this->_individualId
,
1690 'receive_date' => '2012-01-01',
1691 'total_amount' => 100.00,
1692 'financial_type_id' => 1,
1693 'payment_instrument_id' => 1,
1694 'contribution_status_id' => 1,
1697 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1698 $newParams = array_merge($contribParams, [
1699 'id' => $contribution['id'],
1700 'financial_type_id' => 3,
1702 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1703 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1704 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1708 * Test that update does not change status id CRM-15105.
1710 public function testCreateUpdateWithoutChangingPendingStatus() {
1711 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, ['contribution_status_id' => 2]));
1712 $this->callAPISuccess('contribution', 'create', ['id' => $contribution['id'], 'source' => 'new source']);
1713 $contribution = $this->callAPISuccess('contribution', 'getsingle', [
1714 'id' => $contribution['id'],
1715 'api.contribution.delete' => 1,
1717 $this->assertEquals(2, $contribution['contribution_status_id']);
1721 * Test Updating a Contribution.
1723 * CHANGE: we require the API to do an incremental update
1725 public function testCreateUpdateContribution() {
1726 $contributionID = $this->contributionCreate([
1727 'contact_id' => $this->_individualId
,
1728 'trxn_id' => 212355,
1729 'financial_type_id' => $this->_financialTypeId
,
1730 'invoice_id' => 'old_invoice',
1733 'contribution_id' => $contributionID,
1735 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1736 $this->assertEquals($original['id'], $contributionID);
1737 //set up list of old params, verify
1739 //This should not be required on update:
1740 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1741 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1742 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1743 $old_source = $original['values'][$contributionID]['contribution_source'];
1745 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1746 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1748 //check against values in CiviUnitTestCase::createContribution()
1749 $this->assertEquals($old_contact_id, $this->_individualId
);
1750 $this->assertEquals($old_fee_amount, 5.00);
1751 $this->assertEquals($old_source, 'SSF');
1752 $this->assertEquals($old_trxn_id, 212355);
1753 $this->assertEquals($old_invoice_id, 'old_invoice');
1755 'id' => $contributionID,
1756 'contact_id' => $this->_individualId
,
1757 'total_amount' => 105.00,
1758 'fee_amount' => 7.00,
1759 'financial_type_id' => $this->_financialTypeId
,
1760 'non_deductible_amount' => 22.00,
1761 'contribution_status_id' => 1,
1762 'note' => 'Donating for Noble Cause',
1765 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1768 'contribution_id' => $contribution['id'],
1770 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1772 $this->assertEquals($contribution['contact_id'], $this->_individualId
);
1773 $this->assertEquals($contribution['total_amount'], 105.00);
1774 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId
);
1775 $this->assertEquals($contribution['financial_type'], 'Donation');
1776 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1777 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1778 $this->assertEquals($contribution['fee_amount'], 7.00);
1779 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1780 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1781 $this->assertEquals($contribution['contribution_source'], $old_source);
1782 $this->assertEquals($contribution['contribution_status'], 'Completed');
1784 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1787 'contribution_id' => $contributionID,
1789 $result = $this->callAPISuccess('contribution', 'delete', $params);
1790 $this->assertAPISuccess($result);
1794 * Check that net_amount is updated when a contribution is updated.
1796 * Update fee amount AND total amount, just fee amount, just total amount
1797 * and neither to check that net_amount is keep updated.
1799 public function testUpdateContributionNetAmountVariants() {
1800 $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
1802 $this->callAPISuccess('Contribution', 'create', [
1803 'id' => $contributionID,
1804 'total_amount' => 90,
1807 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1808 'id' => $contributionID,
1809 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1811 $this->assertEquals(6, $contribution['fee_amount']);
1812 $this->assertEquals(90, $contribution['total_amount']);
1813 $this->assertEquals(84, $contribution['net_amount']);
1815 $this->callAPISuccess('Contribution', 'create', [
1816 'id' => $contributionID,
1819 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1820 'id' => $contributionID,
1821 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1823 $this->assertEquals(3, $contribution['fee_amount']);
1824 $this->assertEquals(90, $contribution['total_amount']);
1825 $this->assertEquals(87, $contribution['net_amount']);
1827 $this->callAPISuccess('Contribution', 'create', [
1828 'id' => $contributionID,
1829 'total_amount' => 200,
1831 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1832 'id' => $contributionID,
1833 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1835 $this->assertEquals(3, $contribution['fee_amount']);
1836 $this->assertEquals(200, $contribution['total_amount']);
1837 $this->assertEquals(197, $contribution['net_amount']);
1839 $this->callAPISuccess('Contribution', 'create', [
1840 'id' => $contributionID,
1841 'payment_instrument' => 'Cash',
1843 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1844 'id' => $contributionID,
1845 'return' => ['net_amount', 'fee_amount', 'total_amount'],
1847 $this->assertEquals(3, $contribution['fee_amount']);
1848 $this->assertEquals(200, $contribution['total_amount']);
1849 $this->assertEquals(197, $contribution['net_amount']);
1853 * Attempt (but fail) to delete a contribution without parameters.
1855 public function testDeleteEmptyParamsContribution() {
1857 $this->callAPIFailure('contribution', 'delete', $params);
1860 public function testDeleteParamsNotArrayContribution() {
1861 $this->expectException(TypeError
::class);
1862 $params = 'contribution_id= 1';
1863 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1866 public function testDeleteWrongParamContribution() {
1868 'contribution_source' => 'SSF',
1870 $this->callAPIFailure('contribution', 'delete', $params);
1873 public function testDeleteContribution() {
1874 $contributionID = $this->contributionCreate([
1875 'contact_id' => $this->_individualId
,
1876 'financial_type_id' => $this->_financialTypeId
,
1879 'id' => $contributionID,
1881 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1885 * Test civicrm_contribution_search with empty params.
1887 * All available contributions expected.
1889 public function testSearchEmptyParams() {
1893 'contact_id' => $this->_individualId
,
1894 'receive_date' => date('Ymd'),
1895 'total_amount' => 100.00,
1896 'financial_type_id' => $this->_financialTypeId
,
1897 'non_deductible_amount' => 10.00,
1898 'fee_amount' => 5.00,
1899 'net_amount' => 95.00,
1901 'invoice_id' => 78910,
1903 'contribution_status_id' => 1,
1905 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1907 $result = $this->callAPISuccess('contribution', 'get', $params);
1908 // We're taking the first element.
1909 $res = $result['values'][$contribution['id']];
1911 $this->assertEquals($p['contact_id'], $res['contact_id']);
1912 $this->assertEquals($p['total_amount'], $res['total_amount']);
1913 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1914 $this->assertEquals($p['net_amount'], $res['net_amount']);
1915 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1916 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1917 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1918 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1919 $this->assertEquals($p['source'], $res['contribution_source']);
1920 // contribution_status_id = 1 => Completed
1921 $this->assertEquals('Completed', $res['contribution_status']);
1923 $this->contributionDelete($contribution['id']);
1927 * Test civicrm_contribution_search. Success expected.
1929 public function testSearch() {
1931 'contact_id' => $this->_individualId
,
1932 'receive_date' => date('Ymd'),
1933 'total_amount' => 100.00,
1934 'financial_type_id' => $this->_financialTypeId
,
1935 'non_deductible_amount' => 10.00,
1936 'contribution_status_id' => 1,
1938 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1941 'contact_id' => $this->_individualId
,
1942 'receive_date' => date('Ymd'),
1943 'total_amount' => 200.00,
1944 'financial_type_id' => $this->_financialTypeId
,
1945 'non_deductible_amount' => 20.00,
1946 'trxn_id' => 5454565,
1947 'invoice_id' => 1212124,
1948 'fee_amount' => 50.00,
1949 'net_amount' => 60.00,
1950 'contribution_status_id' => 2,
1952 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1955 'contribution_id' => $contribution2['id'],
1957 $result = $this->callAPISuccess('contribution', 'get', $params);
1958 $res = $result['values'][$contribution2['id']];
1960 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1961 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1962 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1963 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1964 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1965 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1966 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1967 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1968 $this->assertEquals(CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'), $res['contribution_status_id']);
1970 $this->contributionDelete($contribution1['id']);
1971 $this->contributionDelete($contribution2['id']);
1975 * Test completing a transaction via the API.
1977 * Note that we are creating a logged in user because email goes out from
1980 public function testCompleteTransaction() {
1981 $mut = new CiviMailUtils($this, TRUE);
1982 $this->swapMessageTemplateForTestTemplate();
1983 $this->createLoggedInUser();
1984 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
1985 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1986 $this->callAPISuccess('contribution', 'completetransaction', [
1987 'id' => $contribution['id'],
1989 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
1990 $this->assertEquals('SSF', $contribution['contribution_source']);
1991 $this->assertEquals('Completed', $contribution['contribution_status']);
1992 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1993 $mut->checkMailLog([
1994 'email:::anthony_anderson@civicrm.org',
1998 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1999 "receipt_date:::\n",
2000 'contributeMode:::notify',
2001 'title:::Contribution',
2002 'displayName:::Mr. Anthony Anderson II',
2003 'contributionStatus:::Completed',
2006 $this->revertTemplateToReservedTemplate();
2010 * Test completing a transaction via the API with a non-USD transaction.
2012 public function testCompleteTransactionEuro() {
2013 $mut = new CiviMailUtils($this, TRUE);
2014 $this->swapMessageTemplateForTestTemplate();
2015 $this->createLoggedInUser();
2016 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'currency' => 'EUR']);
2017 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2019 $this->callAPISuccess('contribution', 'completetransaction', [
2020 'id' => $contribution['id'],
2023 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
2024 $this->assertEquals('SSF', $contribution['contribution_source']);
2025 $this->assertEquals('Completed', $contribution['contribution_status']);
2026 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
2028 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
2029 $entityFinancialTransaction = reset($entityFinancialTransactions);
2030 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', ['id' => $entityFinancialTransaction['financial_trxn_id']]);
2031 $this->assertEquals('EUR', $financialTrxn['currency']);
2033 $mut->checkMailLog([
2034 'email:::anthony_anderson@civicrm.org',
2038 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
2039 "receipt_date:::\n",
2040 'contributeMode:::notify',
2041 'title:::Contribution',
2042 'displayName:::Mr. Anthony Anderson II',
2043 'contributionStatus:::Completed',
2046 $this->revertTemplateToReservedTemplate();
2050 * Test to ensure mail is sent on chosing pay later
2052 public function testpayLater() {
2053 $mut = new CiviMailUtils($this, TRUE);
2054 $this->swapMessageTemplateForTestTemplate();
2055 $this->createLoggedInUser();
2057 // create contribution page first
2058 $contributionPageParams = [
2059 'title' => 'Help Support CiviCRM!',
2060 'financial_type_id' => 1,
2061 'is_monetary' => TRUE,
2062 'is_pay_later' => 1,
2063 'is_quick_config' => TRUE,
2064 'pay_later_text' => 'I will send payment by check',
2065 'pay_later_receipt' => 'This is a pay later reciept',
2066 'is_allow_other_amount' => 1,
2067 'min_amount' => 10.00,
2068 'max_amount' => 10000.00,
2069 'goal_amount' => 100000.00,
2070 'is_email_receipt' => 1,
2072 'amount_block_is_active' => 1,
2073 'currency' => 'USD',
2074 'is_billing_required' => 0,
2076 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
2078 // submit form values
2079 $priceSet = $this->callAPISuccess('price_set', 'getsingle', ['name' => 'default_contribution_amount']);
2081 'id' => $contributionPageResult['id'],
2082 'contact_id' => $this->_individualId
,
2083 'email-5' => 'anthony_anderson@civicrm.org',
2084 'payment_processor_id' => 0,
2087 'currencyID' => 'USD',
2088 'is_pay_later' => 1,
2089 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
2090 'is_quick_config' => 1,
2091 'description' => 'Online Contribution: Help Support CiviCRM!',
2092 'price_set_id' => $priceSet['id'],
2094 $this->callAPISuccess('contribution_page', 'submit', $params);
2096 $mut->checkMailLog([
2098 'email:::anthony_anderson@civicrm.org',
2099 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
2100 'displayName:::Mr. Anthony Anderson II',
2101 'contributionPageId:::' . $contributionPageResult['id'],
2102 'title:::' . $contributionPageParams['title'],
2103 'amount:::' . $params['amount'],
2106 $this->revertTemplateToReservedTemplate();
2110 * Test to check whether contact billing address is used when no contribution address
2112 public function testBillingAddress() {
2113 $mut = new CiviMailUtils($this, TRUE);
2114 $this->swapMessageTemplateForTestTemplate();
2115 $this->createLoggedInUser();
2117 //Scenario 1: When Contact don't have any address
2118 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2119 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2120 $this->callAPISuccess('contribution', 'completetransaction', [
2121 'id' => $contribution['id'],
2123 $mut->checkMailLog([
2127 // Scenario 2: Contribution using address
2128 $address = $this->callAPISuccess('address', 'create', [
2129 'street_address' => 'contribution billing st',
2130 'location_type_id' => 2,
2131 'contact_id' => $this->_params
['contact_id'],
2133 $params = array_merge($this->_params
, [
2134 'contribution_status_id' => 2,
2135 'address_id' => $address['id'],
2138 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2139 $this->callAPISuccess('contribution', 'completetransaction', [
2140 'id' => $contribution['id'],
2142 $mut->checkMailLog([
2143 'address:::contribution billing st',
2146 // Scenario 3: Contribution wtth no address but contact has a billing address
2147 $this->callAPISuccess('address', 'create', [
2148 'id' => $address['id'],
2149 'street_address' => 'is billing st',
2150 'contact_id' => $this->_params
['contact_id'],
2152 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2153 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2154 $this->callAPISuccess('contribution', 'completetransaction', [
2155 'id' => $contribution['id'],
2157 $mut->checkMailLog([
2158 'address:::is billing st',
2162 $this->revertTemplateToReservedTemplate();
2166 * Test completing a transaction via the API.
2168 * Note that we are creating a logged in user because email goes out from
2171 public function testCompleteTransactionFeeAmount() {
2172 $this->createLoggedInUser();
2173 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2174 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2175 $this->callAPISuccess('contribution', 'completetransaction', [
2176 'id' => $contribution['id'],
2177 'fee_amount' => '.56',
2178 'trxn_id' => '7778888',
2180 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id'], 'sequential' => 1]);
2181 $this->assertEquals('Completed', $contribution['contribution_status']);
2182 $this->assertEquals('7778888', $contribution['trxn_id']);
2183 $this->assertEquals('.56', $contribution['fee_amount']);
2184 $this->assertEquals('99.44', $contribution['net_amount']);
2188 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
2190 * @param string $thousandSeparator
2191 * punctuation used to refer to thousands.
2193 * @dataProvider getThousandSeparators
2195 public function testCheckTaxAmount($thousandSeparator) {
2196 $this->setCurrencySeparators($thousandSeparator);
2197 $contact = $this->createLoggedInUser();
2198 $financialType = $this->callAPISuccess('financial_type', 'create', [
2199 'name' => 'Test taxable financial Type',
2203 $financialAccount = $this->callAPISuccess('financial_account', 'create', [
2204 'name' => 'Test Tax financial account ',
2205 'contact_id' => $contact,
2206 'financial_account_type_id' => 2,
2213 $financialTypeId = $financialType['id'];
2214 $financialAccountId = $financialAccount['id'];
2215 $financialAccountParams = [
2216 'entity_table' => 'civicrm_financial_type',
2217 'entity_id' => $financialTypeId,
2218 'account_relationship' => 10,
2219 'financial_account_id' => $financialAccountId,
2221 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
2223 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'financial_type_id' => $financialTypeId]);
2224 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2225 $contribution1 = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1]);
2226 $this->callAPISuccess('contribution', 'completetransaction', [
2227 'id' => $contribution['id'],
2228 'trxn_id' => '777788888',
2229 'fee_amount' => '6.00',
2231 $contribution2 = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'return' => ['tax_amount', 'fee_amount', 'net_amount'], 'sequential' => 1]);
2232 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
2233 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
2234 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
2238 * Test repeat contribution successfully creates line item.
2240 * @throws \CRM_Core_Exception
2242 public function testRepeatTransaction() {
2243 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');
2244 $this->callAPISuccess('contribution', 'repeattransaction', [
2245 'original_contribution_id' => $originalContribution['id'],
2246 'contribution_status_id' => 'Completed',
2247 'trxn_id' => uniqid(),
2250 'entity_id' => $originalContribution['id'],
2258 'financial_type_id',
2259 'deductible_amount',
2260 'price_field_value_id',
2264 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2265 'entity_id' => $originalContribution['id'],
2267 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2268 'entity_id' => $originalContribution['id'] +
1,
2270 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2271 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2272 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2273 $this->_checkFinancialRecords([
2274 'id' => $originalContribution['id'] +
1,
2275 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', [
2276 'id' => $originalContribution['payment_processor_id'],
2277 'return' => 'payment_instrument_id',
2280 $this->quickCleanUpFinancialEntities();
2284 * Test repeat contribution successfully creates line items (plural).
2286 public function testRepeatTransactionLineItems() {
2288 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'multiple');
2289 $this->callAPISuccess('contribution', 'repeattransaction', [
2290 'original_contribution_id' => $originalContribution['id'],
2291 'contribution_status_id' => 'Completed',
2292 'trxn_id' => uniqid(),
2296 'entity_id' => $originalContribution['id'],
2304 'financial_type_id',
2305 'deductible_amount',
2306 'price_field_value_id',
2310 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2311 'entity_id' => $originalContribution['id'],
2313 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2314 'entity_id' => $originalContribution['id'] +
1,
2317 // unset id and entity_id for all of them to be able to compare the lineItems:
2318 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2319 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2320 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2322 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2323 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2324 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2326 // CRM-19309 so in future we also want to:
2327 // check that financial_line_items have been created for entity_id 3 and 4;
2329 $this->callAPISuccessGetCount('FinancialItem', ['description' => 'Sales Tax', 'amount' => 0], 0);
2330 $this->quickCleanUpFinancialEntities();
2334 * Test repeat contribution successfully creates is_test transaction.
2336 public function testRepeatTransactionIsTest() {
2337 $this->_params
['is_test'] = 1;
2338 $originalContribution = $this->setUpRepeatTransaction(['is_test' => 1], 'single');
2340 $this->callAPISuccess('contribution', 'repeattransaction', [
2341 'original_contribution_id' => $originalContribution['id'],
2342 'contribution_status_id' => 'Completed',
2343 'trxn_id' => uniqid(),
2345 $this->callAPISuccessGetCount('Contribution', ['contribution_test' => 1], 2);
2349 * Test repeat contribution passed in status.
2351 public function testRepeatTransactionPassedInStatus() {
2352 $originalContribution = $this->setUpRepeatTransaction($recurParams = [], 'single');
2354 $this->callAPISuccess('contribution', 'repeattransaction', [
2355 'original_contribution_id' => $originalContribution['id'],
2356 'contribution_status_id' => 'Pending',
2357 'trxn_id' => uniqid(),
2359 $this->callAPISuccessGetCount('Contribution', ['contribution_status_id' => 2], 1);
2363 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2365 public function testRepeatTransactionAcceptRecurID() {
2366 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2367 'contact_id' => $this->_individualId
,
2368 'installments' => '12',
2369 'frequency_interval' => '1',
2371 'contribution_status_id' => 1,
2372 'start_date' => '2012-01-01 00:00:00',
2373 'currency' => 'USD',
2374 'frequency_unit' => 'month',
2375 'payment_processor_id' => $this->paymentProcessorID
,
2377 $this->callAPISuccess('contribution', 'create', array_merge(
2379 ['contribution_recur_id' => $contributionRecur['id']])
2382 $this->callAPISuccess('contribution', 'repeattransaction', [
2383 'contribution_recur_id' => $contributionRecur['id'],
2384 'contribution_status_id' => 'Completed',
2385 'trxn_id' => uniqid(),
2388 $this->quickCleanUpFinancialEntities();
2392 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2394 public function testRepeatTransactionTestRecurId() {
2395 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2396 'contact_id' => $this->_individualId
,
2397 'frequency_interval' => '1',
2399 'contribution_status_id' => 1,
2400 'start_date' => '2017-01-01 00:00:00',
2401 'currency' => 'USD',
2402 'frequency_unit' => 'month',
2403 'payment_processor_id' => $this->paymentProcessorID
,
2406 $this->callAPISuccess('contribution', 'create', array_merge(
2409 'contribution_recur_id' => $contributionRecur['id'],
2414 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', [
2415 'contribution_recur_id' => $contributionRecur['id'],
2416 'contribution_status_id' => 'Completed',
2417 'trxn_id' => uniqid(),
2420 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2421 $this->quickCleanUpFinancialEntities();
2425 * CRM-19945 Tests that Contribute.repeattransaction renews a membership when contribution status=Completed
2428 public function testRepeatTransactionMembershipRenewCompletedContribution() {
2429 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2431 $this->callAPISuccess('contribution', 'create', [
2432 'contact_id' => $originalContribution['values'][1]['contact_id'],
2433 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2434 'total_amount' => $originalContribution['values'][1]['total_amount'],
2435 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2436 'contribution_status_id' => "Failed",
2439 $this->callAPISuccess('membership', 'create', [
2440 'id' => $membership['id'],
2441 'end_date' => 'yesterday',
2442 'status_id' => 'Expired',
2445 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
2446 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2447 'contribution_status_id' => 'Completed',
2448 'trxn_id' => 'bobsled',
2451 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', [
2452 'id' => $membership['id'],
2453 'return' => 'status_id',
2456 $membership = $this->callAPISuccess('membership', 'get', [
2457 'id' => $membership['id'],
2460 $this->assertEquals('New', CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2462 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id']]);
2463 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2464 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']]);
2465 $this->quickCleanUpFinancialEntities();
2466 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2470 * CRM-19945 Tests that Contribute.repeattransaction DOES NOT renew a membership when contribution status=Failed
2472 * @dataProvider contributionStatusProvider
2474 * @throws \CRM_Core_Exception
2476 public function testRepeatTransactionMembershipRenewContributionNotCompleted($contributionStatus) {
2477 // Completed status should renew so we don't test that here
2478 // In Progress status was never actually intended to be available for contributions.
2479 // Partially paid is not valid.
2480 if (in_array($contributionStatus['name'], ['Completed', 'In Progress', 'Partially paid'])) {
2483 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2485 $this->callAPISuccess('contribution', 'create', [
2486 'contact_id' => $originalContribution['values'][1]['contact_id'],
2487 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2488 'total_amount' => $originalContribution['values'][1]['total_amount'],
2489 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2490 'contribution_status_id' => "Completed",
2493 $this->callAPISuccess('membership', 'create', [
2494 'id' => $membership['id'],
2495 'end_date' => 'yesterday',
2496 'status_id' => 'Expired',
2499 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
2500 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2501 'contribution_status_id' => $contributionStatus['name'],
2502 'trxn_id' => 'bobsled',
2505 $updatedMembership = $this->callAPISuccess('membership', 'getsingle', [
2506 'id' => $membership['id'],
2509 $dateTime = new DateTime('yesterday');
2510 $this->assertEquals($dateTime->format('Y-m-d'), $updatedMembership['end_date']);
2511 $this->assertEquals(CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Expired'), $updatedMembership['status_id']);
2513 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id']]);
2514 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2515 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']]);
2516 $this->quickCleanUpFinancialEntities();
2517 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2521 * Dataprovider provides contribution status as [optionvalue=>contribution_status_name]
2522 * FIXME: buildOptions seems to die in CRM_Core_Config::_construct when in test mode.
2525 * @throws \CiviCRM_API3_Exception
2527 public function contributionStatusProvider() {
2528 $contributionStatuses = civicrm_api3('OptionValue', 'get', [
2529 'return' => ["id", "name"],
2530 'option_group_id' => "contribution_status",
2532 foreach ($contributionStatuses['values'] as $statusName) {
2533 $statuses[] = [$statusName];
2539 * CRM-16397 test appropriate action if total amount has changed for single line items.
2541 public function testRepeatTransactionAlteredAmount() {
2542 $paymentProcessorID = $this->paymentProcessorCreate();
2543 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2544 'contact_id' => $this->_individualId
,
2545 'installments' => '12',
2546 'frequency_interval' => '1',
2548 'contribution_status_id' => 1,
2549 'start_date' => '2012-01-01 00:00:00',
2550 'currency' => 'USD',
2551 'frequency_unit' => 'month',
2552 'payment_processor_id' => $paymentProcessorID,
2554 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2557 'contribution_recur_id' => $contributionRecur['id'],
2561 $this->callAPISuccess('contribution', 'repeattransaction', [
2562 'original_contribution_id' => $originalContribution['id'],
2563 'contribution_status_id' => 'Completed',
2564 'trxn_id' => uniqid(),
2565 'total_amount' => '400',
2570 'entity_id' => $originalContribution['id'],
2578 'financial_type_id',
2579 'deductible_amount',
2580 'price_field_value_id',
2584 $this->callAPISuccessGetSingle('contribution', [
2585 'total_amount' => 400,
2587 'net_amount' => 350,
2589 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2590 'entity_id' => $originalContribution['id'],
2592 $expectedLineItem = array_merge(
2593 $lineItem1['values'][0], [
2594 'line_total' => '400.00',
2595 'unit_price' => '400.00',
2599 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2600 'entity_id' => $originalContribution['id'] +
1,
2603 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2604 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2605 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2609 * CRM-17718 test appropriate action if financial type has changed for single line items.
2611 public function testRepeatTransactionPassedInFinancialType() {
2612 $originalContribution = $this->setUpRecurringContribution();
2614 $this->callAPISuccess('contribution', 'repeattransaction', [
2615 'original_contribution_id' => $originalContribution['id'],
2616 'contribution_status_id' => 'Completed',
2617 'trxn_id' => uniqid(),
2618 'financial_type_id' => 2,
2621 'entity_id' => $originalContribution['id'],
2629 'financial_type_id',
2630 'deductible_amount',
2631 'price_field_value_id',
2636 $this->callAPISuccessGetSingle('contribution', [
2637 'total_amount' => 100,
2638 'financial_type_id' => 2,
2640 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2641 'entity_id' => $originalContribution['id'],
2643 $expectedLineItem = array_merge(
2644 $lineItem1['values'][0], [
2645 'line_total' => '100.00',
2646 'unit_price' => '100.00',
2647 'financial_type_id' => 2,
2648 'contribution_type_id' => 2,
2651 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2652 'entity_id' => $originalContribution['id'] +
1,
2654 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2655 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2656 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2660 * CRM-17718 test appropriate action if financial type has changed for single line items.
2662 public function testRepeatTransactionUpdatedFinancialType() {
2663 $originalContribution = $this->setUpRecurringContribution([], ['financial_type_id' => 2]);
2665 $this->callAPISuccess('contribution', 'repeattransaction', [
2666 'contribution_recur_id' => $originalContribution['id'],
2667 'contribution_status_id' => 'Completed',
2668 'trxn_id' => uniqid(),
2671 'entity_id' => $originalContribution['id'],
2679 'financial_type_id',
2680 'deductible_amount',
2681 'price_field_value_id',
2686 $this->callAPISuccessGetSingle('contribution', [
2687 'total_amount' => 100,
2688 'financial_type_id' => 2,
2690 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2691 'entity_id' => $originalContribution['id'],
2693 $expectedLineItem = array_merge(
2694 $lineItem1['values'][0], [
2695 'line_total' => '100.00',
2696 'unit_price' => '100.00',
2697 'financial_type_id' => 2,
2698 'contribution_type_id' => 2,
2702 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2703 'entity_id' => $originalContribution['id'] +
1,
2705 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2706 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2707 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2711 * CRM-16397 test appropriate action if campaign has been passed in.
2713 public function testRepeatTransactionPassedInCampaign() {
2714 $paymentProcessorID = $this->paymentProcessorCreate();
2715 $campaignID = $this->campaignCreate();
2716 $campaignID2 = $this->campaignCreate();
2717 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2718 'contact_id' => $this->_individualId
,
2719 'installments' => '12',
2720 'frequency_interval' => '1',
2722 'contribution_status_id' => 1,
2723 'start_date' => '2012-01-01 00:00:00',
2724 'currency' => 'USD',
2725 'frequency_unit' => 'month',
2726 'payment_processor_id' => $paymentProcessorID,
2728 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2731 'contribution_recur_id' => $contributionRecur['id'],
2732 'campaign_id' => $campaignID,
2736 $this->callAPISuccess('contribution', 'repeattransaction', [
2737 'original_contribution_id' => $originalContribution['id'],
2738 'contribution_status_id' => 'Completed',
2739 'trxn_id' => uniqid(),
2740 'campaign_id' => $campaignID2,
2743 $this->callAPISuccessGetSingle('contribution', [
2744 'total_amount' => 100,
2745 'campaign_id' => $campaignID2,
2750 * CRM-17718 campaign stored on contribution recur gets priority.
2752 * This reflects the fact we permit people to update them.
2754 public function testRepeatTransactionUpdatedCampaign() {
2755 $paymentProcessorID = $this->paymentProcessorCreate();
2756 $campaignID = $this->campaignCreate();
2757 $campaignID2 = $this->campaignCreate();
2758 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
2759 'contact_id' => $this->_individualId
,
2760 'installments' => '12',
2761 'frequency_interval' => '1',
2763 'contribution_status_id' => 1,
2764 'start_date' => '2012-01-01 00:00:00',
2765 'currency' => 'USD',
2766 'frequency_unit' => 'month',
2767 'payment_processor_id' => $paymentProcessorID,
2768 'campaign_id' => $campaignID,
2770 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2773 'contribution_recur_id' => $contributionRecur['id'],
2774 'campaign_id' => $campaignID2,
2778 $this->callAPISuccess('contribution', 'repeattransaction', [
2779 'original_contribution_id' => $originalContribution['id'],
2780 'contribution_status_id' => 'Completed',
2781 'trxn_id' => uniqid(),
2784 $this->callAPISuccessGetSingle('contribution', [
2785 'total_amount' => 100,
2786 'campaign_id' => $campaignID,
2791 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2793 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2795 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
2796 $originalContribution = $this->setUpRecurringContribution([], ['financial_type_id' => 2]);
2797 // This will made the trick to get the not equals behaviour.
2798 $this->callAPISuccess('line_item', 'create', ['id' => 1, 'financial_type_id' => 4]);
2799 $this->callAPISuccess('contribution', 'repeattransaction', [
2800 'contribution_recur_id' => $originalContribution['id'],
2801 'contribution_status_id' => 'Completed',
2802 'trxn_id' => uniqid(),
2805 'entity_id' => $originalContribution['id'],
2813 'financial_type_id',
2814 'deductible_amount',
2815 'price_field_value_id',
2819 $this->callAPISuccessGetSingle('contribution', [
2820 'total_amount' => 100,
2821 'financial_type_id' => 2,
2823 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2824 'entity_id' => $originalContribution['id'],
2826 $expectedLineItem = array_merge(
2827 $lineItem1['values'][0], [
2828 'line_total' => '100.00',
2829 'unit_price' => '100.00',
2830 'financial_type_id' => 4,
2831 'contribution_type_id' => 4,
2835 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, [
2836 'entity_id' => $originalContribution['id'] +
1,
2838 $this->callAPISuccess('line_item', 'create', ['id' => 1, 'financial_type_id' => 1]);
2839 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2840 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2841 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2845 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2847 public function testCompleteTransactionNetAmountOK() {
2848 $this->createLoggedInUser();
2849 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2850 unset($params['net_amount']);
2851 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2852 $this->callAPISuccess('contribution', 'completetransaction', [
2853 'id' => $contribution['id'],
2855 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $contribution['id']]);
2856 $this->assertEquals('Completed', $contribution['contribution_status']);
2857 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2861 * CRM-14151 - Test completing a transaction via the API.
2863 public function testCompleteTransactionWithReceiptDateSet() {
2864 $this->swapMessageTemplateForTestTemplate();
2865 $mut = new CiviMailUtils($this, TRUE);
2866 $this->createLoggedInUser();
2867 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
2868 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2869 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $contribution['id'], 'trxn_date' => date('Y-m-d')]);
2870 $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id'], 'sequential' => 1]);
2871 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2872 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2873 $mut->checkMailLog([
2874 'Receipt - Contribution',
2875 'receipt_date:::' . date('Ymd'),
2878 $this->revertTemplateToReservedTemplate();
2882 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2884 public function testCompleteTransactionWithEmailReceiptInput() {
2885 $contributionPage = $this->createReceiptableContributionPage();
2887 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2888 $params = array_merge($this->_params
, ['contribution_status_id' => 2]);
2889 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2890 // Complete the transaction overriding is_email_receipt to = FALSE
2891 $this->callAPISuccess('contribution', 'completetransaction', [
2892 'id' => $contribution['id'],
2893 'trxn_date' => date('2011-04-09'),
2894 'trxn_id' => 'kazam',
2895 'is_email_receipt' => 0,
2897 // Check if a receipt was issued
2898 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', ['id' => $contribution['id'], 'return' => 'receipt_date']);
2899 $this->assertEquals('', $receipt_date);
2903 * Test that $is_recur is assigned to the receipt.
2905 public function testCompleteTransactionForRecurring() {
2907 $this->swapMessageTemplateForTestTemplate();
2908 $recurring = $this->setUpRecurringContribution();
2909 $contributionPage = $this->createReceiptableContributionPage(['is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1]);
2911 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2912 $this->_params
['contribution_recur_id'] = $recurring['id'];
2914 $contribution = $this->setUpForCompleteTransaction();
2916 $this->callAPISuccess('contribution', 'completetransaction', [
2917 'id' => $contribution['id'],
2918 'trxn_date' => date('2011-04-09'),
2919 'trxn_id' => 'kazam',
2920 'is_email_receipt' => 1,
2923 $this->mut
->checkMailLog([
2925 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL
,
2928 $this->revertTemplateToReservedTemplate();
2932 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2934 * If passed in it will override the default from contribution page.
2936 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2937 $mut = new CiviMailUtils($this, TRUE);
2938 $this->createLoggedInUser();
2939 // Create a Contribution Page with is_email_receipt = FALSE
2940 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [
2941 'receipt_from_name' => 'Mickey Mouse',
2942 'receipt_from_email' => 'mickey@mouse.com',
2943 'title' => "Test Contribution Page",
2944 'financial_type_id' => 1,
2945 'currency' => 'CAD',
2946 'is_monetary' => TRUE,
2947 'is_email_receipt' => 0,
2949 $this->_params
['contribution_page_id'] = $contributionPage['id'];
2950 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
2951 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2952 // Complete the transaction overriding is_email_receipt to = TRUE
2953 $this->callAPISuccess('contribution', 'completetransaction', [
2954 'id' => $contribution['id'],
2955 'is_email_receipt' => 1,
2957 $mut->checkMailLog([
2958 'Contribution Information',
2964 * Complete the transaction using the template with all the possible.
2966 public function testCompleteTransactionWithTestTemplate() {
2967 $this->swapMessageTemplateForTestTemplate();
2968 $contribution = $this->setUpForCompleteTransaction();
2969 $this->callAPISuccess('contribution', 'completetransaction', [
2970 'id' => $contribution['id'],
2971 'trxn_date' => date('2011-04-09'),
2972 'trxn_id' => 'kazam',
2974 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', ['id' => $contribution['id'], 'return' => 'receive_date']);
2975 $this->mut
->checkMailLog([
2976 'email:::anthony_anderson@civicrm.org',
2980 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2981 'receipt_date:::' . date('Ymd'),
2982 'contributeMode:::notify',
2983 'title:::Contribution',
2984 'displayName:::Mr. Anthony Anderson II',
2986 'contactID:::' . $this->_params
['contact_id'],
2987 'contributionID:::' . $contribution['id'],
2988 'financialTypeId:::1',
2989 'financialTypeName:::Donation',
2992 $this->revertTemplateToReservedTemplate();
2996 * Complete the transaction using the template with all the possible.
2998 public function testCompleteTransactionContributionPageFromAddress() {
2999 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [
3000 'receipt_from_name' => 'Mickey Mouse',
3001 'receipt_from_email' => 'mickey@mouse.com',
3002 'title' => "Test Contribution Page",
3003 'financial_type_id' => 1,
3004 'currency' => 'NZD',
3005 'goal_amount' => 50,
3006 'is_pay_later' => 1,
3007 'is_monetary' => TRUE,
3008 'is_email_receipt' => TRUE,
3010 $this->_params
['contribution_page_id'] = $contributionPage['id'];
3011 $contribution = $this->setUpForCompleteTransaction();
3012 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $contribution['id']]);
3013 $this->mut
->checkMailLog([
3021 * Test completing first transaction in a recurring series.
3023 * The status should be set to 'in progress' and the next scheduled payment date calculated.
3025 * @dataProvider getScheduledDateData
3027 * @param array $dataSet
3029 * @throws \Exception
3031 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
3032 $paymentProcessorID = $this->paymentProcessorCreate();
3033 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
3034 'contact_id' => $this->_individualId
,
3035 'installments' => '2',
3036 'frequency_interval' => '1',
3038 'contribution_status_id' => 'Pending',
3039 'start_date' => '2012-01-01 00:00:00',
3040 'currency' => 'USD',
3041 'frequency_unit' => 'month',
3042 'payment_processor_id' => $paymentProcessorID,
3043 ], $dataSet['data']));
3044 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
3047 'contribution_recur_id' => $contributionRecur['id'],
3048 'contribution_status_id' => 'Pending',
3049 'receive_date' => $dataSet['receive_date'],
3052 $this->callAPISuccess('Contribution', 'completetransaction', [
3053 'id' => $contribution,
3054 'receive_date' => $dataSet['receive_date'],
3056 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', [
3057 'id' => $contributionRecur['id'],
3058 'return' => ['next_sched_contribution_date', 'contribution_status_id'],
3060 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
3061 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
3062 $this->callAPISuccess('Contribution', 'create', array_merge(
3065 'contribution_recur_id' => $contributionRecur['id'],
3066 'contribution_status_id' => 'Completed',
3069 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', [
3070 'id' => $contributionRecur['id'],
3071 'return' => ['contribution_status_id'],
3073 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
3077 * Get dates for testing.
3081 public function getScheduledDateData() {
3083 $result[]['2016-08-31-1-month'] = [
3085 'start_date' => '2016-08-31',
3086 'frequency_interval' => 1,
3087 'frequency_unit' => 'month',
3089 'receive_date' => '2016-08-31',
3090 'expected' => '2016-10-01 00:00:00',
3092 $result[]['2012-01-01-1-month'] = [
3094 'start_date' => '2012-01-01',
3095 'frequency_interval' => 1,
3096 'frequency_unit' => 'month',
3098 'receive_date' => '2012-01-01',
3099 'expected' => '2012-02-01 00:00:00',
3101 $result[]['2012-01-01-1-month'] = [
3103 'start_date' => '2012-01-01',
3104 'frequency_interval' => 1,
3105 'frequency_unit' => 'month',
3107 'receive_date' => '2012-02-29',
3108 'expected' => '2012-03-29 00:00:00',
3110 $result['receive_date_includes_time']['2012-01-01-1-month'] = [
3112 'start_date' => '2012-01-01',
3113 'frequency_interval' => 1,
3114 'frequency_unit' => 'month',
3115 'next_sched_contribution_date' => '2012-02-29',
3117 'receive_date' => '2012-02-29 16:00:00',
3118 'expected' => '2012-03-29 00:00:00',
3124 * Test completing a pledge with the completeTransaction api..
3126 * Note that we are creating a logged in user because email goes out from
3129 public function testCompleteTransactionUpdatePledgePayment() {
3130 $this->swapMessageTemplateForTestTemplate();
3131 $mut = new CiviMailUtils($this, TRUE);
3132 $mut->clearMessages();
3133 $this->createLoggedInUser();
3134 $contributionID = $this->createPendingPledgeContribution();
3135 $this->callAPISuccess('contribution', 'completetransaction', [
3136 'id' => $contributionID,
3137 'trxn_date' => '1 Feb 2013',
3139 $pledge = $this->callAPISuccessGetSingle('Pledge', [
3140 'id' => $this->_ids
['pledge'],
3142 $this->assertEquals('Completed', $pledge['pledge_status']);
3144 $status = $this->callAPISuccessGetValue('PledgePayment', [
3145 'pledge_id' => $this->_ids
['pledge'],
3146 'return' => 'status_id',
3148 $this->assertEquals(1, $status);
3149 $mut->checkMailLog([
3151 'receive_date:::20130201000000',
3152 "receipt_date:::\n",
3155 $this->revertTemplateToReservedTemplate();
3159 * Test completing a transaction with an event via the API.
3161 * Note that we are creating a logged in user because email goes out from
3164 * @throws \CRM_Core_Exception
3166 public function testCompleteTransactionWithParticipantRecord() {
3167 $mut = new CiviMailUtils($this, TRUE);
3168 $mut->clearMessages();
3169 $this->_individualId
= $this->createLoggedInUser();
3170 $contributionID = $this->createPendingParticipantContribution();
3171 $this->createJoinedProfile(['entity_id' => $this->_ids
['event']['test'], 'entity_table' => 'civicrm_event']);
3172 $this->createJoinedProfile(['entity_id' => $this->_ids
['event']['test'], 'entity_table' => 'civicrm_event', 'weight' => 2], ['name' => 'post_1', 'title' => 'title_post_2', 'frontend_title' => 'public 2']);
3173 $this->createJoinedProfile(['entity_id' => $this->_ids
['event']['test'], 'entity_table' => 'civicrm_event', 'weight' => 3], ['name' => 'post_2', 'title' => 'title_post_3', 'frontend_title' => 'public 3']);
3174 $this->eliminateUFGroupOne();
3176 $this->callAPISuccess('contribution', 'completetransaction', [
3177 'id' => $contributionID,
3180 $participantStatus = $this->callAPISuccessGetValue('participant', [
3181 'id' => $this->_ids
['participant'],
3182 'return' => 'participant_status_id',
3184 $this->assertEquals(1, $participantStatus);
3186 //Assert only three activities are created.
3187 $activities = $this->callAPISuccess('Activity', 'get', [
3188 'contact_id' => $this->_individualId
,
3191 $this->assertCount(3, $activities);
3192 $activityNames = array_count_values(CRM_Utils_Array
::collect('activity_name', $activities));
3193 // record two activities before and after completing payment for Event registration
3194 $this->assertEquals(2, $activityNames['Event Registration']);
3195 // update the original 'Contribution' activity created after completing payment
3196 $this->assertEquals(1, $activityNames['Contribution']);
3198 $mut->checkMailLog([
3199 'Annual CiviCRM meet',
3201 'This is a confirmation that your registration has been received and your status has been updated to Registered.',
3202 'First Name: Logged In',
3206 ], ['Back end title', 'title_post_2', 'title_post_3']);
3211 * Test membership is renewed when transaction completed.
3213 public function testCompleteTransactionMembershipPriceSet() {
3214 $this->createPriceSetWithPage('membership');
3215 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', [
3219 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
3220 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3221 $logs = $this->callAPISuccess('MembershipLog', 'get', [
3222 'membership_id' => $this->_ids
['membership'],
3224 $this->assertEquals(1, $logs['count']);
3225 $this->assertEquals($stateOfGrace, $membership['status_id']);
3226 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $this->_ids
['contribution']]);
3227 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3228 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
3229 $this->callAPISuccessGetSingle('LineItem', [
3230 'entity_id' => $this->_ids
['membership'],
3231 'entity_table' => 'civicrm_membership',
3233 $logs = $this->callAPISuccess('MembershipLog', 'get', [
3234 'membership_id' => $this->_ids
['membership'],
3236 $this->assertEquals(2, $logs['count']);
3237 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
3238 //Assert only three activities are created.
3239 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_ids
['contact']);
3240 $this->assertEquals(3, count($activities));
3241 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
3242 $this->assertArrayHasKey('Contribution', $activityNames);
3243 $this->assertArrayHasKey('Membership Signup', $activityNames);
3244 $this->assertArrayHasKey('Change Membership Status', $activityNames);
3245 $this->cleanUpAfterPriceSets();
3249 * Test if renewal activity is create after changing Pending contribution to Completed via offline
3251 public function testPendingToCompleteContribution() {
3252 $this->createPriceSetWithPage('membership');
3253 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
3254 $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3255 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
3256 $activity = $this->callAPISuccess('Activity', 'get', [
3257 'activity_type_id' => 'Membership Signup',
3258 'source_record_id' => $this->_ids
['membership'],
3259 'status_id' => 'Scheduled',
3261 $this->assertEquals(1, $activity['count']);
3263 // change pending contribution to completed
3264 $form = new CRM_Contribute_Form_Contribution();
3267 'id' => $this->_ids
['contribution'],
3268 'total_amount' => 20,
3271 'financial_type_id' => 1,
3272 'contact_id' => $this->_individualId
,
3273 'contribution_status_id' => 1,
3274 'billing_middle_name' => '',
3275 'billing_last_name' => 'Adams',
3276 'billing_street_address-5' => '790L Lincoln St S',
3277 'billing_city-5' => 'Maryknoll',
3278 'billing_state_province_id-5' => 1031,
3279 'billing_postal_code-5' => 10545,
3280 'billing_country_id-5' => 1228,
3281 'frequency_interval' => 1,
3282 'frequency_unit' => 'month',
3283 'installments' => '',
3284 'hidden_AdditionalDetail' => 1,
3285 'hidden_Premium' => 1,
3286 'from_email_address' => '"civi45" <civi45@civicrm.com>',
3287 'receipt_date' => '',
3288 'receipt_date_time' => '',
3289 'payment_processor_id' => $this->paymentProcessorID
,
3290 'currency' => 'USD',
3291 'contribution_page_id' => $this->_ids
['contribution_page'],
3292 'contribution_mode' => 'membership',
3293 'source' => 'Membership Signup and Renewal',
3296 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3298 // Case 2: After successful payment for Pending backoffice there are three activities created
3299 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
3300 $activity = $this->callAPISuccess('Activity', 'get', [
3301 'activity_type_id' => 'Membership Signup',
3302 'source_record_id' => $this->_ids
['membership'],
3303 'status_id' => 'Completed',
3305 $this->assertEquals(1, $activity['count']);
3306 // 2.b Contribution activity created to record successful payment
3307 $activity = $this->callAPISuccess('Activity', 'get', [
3308 'activity_type_id' => 'Contribution',
3309 'source_record_id' => $this->_ids
['contribution'],
3310 'status_id' => 'Completed',
3312 $this->assertEquals(1, $activity['count']);
3314 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
3315 $activity = $this->callAPISuccess('Activity', 'get', [
3316 'activity_type_id' => 'Change Membership Status',
3317 'source_record_id' => $this->_ids
['membership'],
3318 'status_id' => 'Completed',
3320 $this->assertEquals(1, $activity['count']);
3321 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
3323 //Create another pending contribution for renewal
3324 $contribution = $this->callAPISuccess('contribution', 'create', [
3326 'contact_id' => $this->_ids
['contact'],
3327 'receive_date' => date('Ymd'),
3328 'total_amount' => 20.00,
3329 'financial_type_id' => 1,
3330 'payment_instrument_id' => 'Credit Card',
3331 'non_deductible_amount' => 10.00,
3332 'trxn_id' => 'rdhfi88',
3333 'invoice_id' => 'dofhiewuyr',
3335 'contribution_status_id' => 2,
3336 'contribution_page_id' => $this->_ids
['contribution_page'],
3337 // We can't rely on contribution api to link line items correctly to membership
3338 'skipLineItem' => TRUE,
3339 'api.membership_payment.create' => ['membership_id' => $this->_ids
['membership']],
3342 $this->callAPISuccess('line_item', 'create', [
3343 'entity_id' => $contribution['id'],
3344 'entity_table' => 'civicrm_contribution',
3345 'contribution_id' => $contribution['id'],
3346 'price_field_id' => $this->_ids
['price_field'][0],
3350 'financial_type_id' => 1,
3351 'price_field_value_id' => $this->_ids
['price_field_value']['cont'],
3353 $this->callAPISuccess('line_item', 'create', [
3354 'entity_id' => $this->_ids
['membership'],
3355 'entity_table' => 'civicrm_membership',
3356 'contribution_id' => $contribution['id'],
3357 'price_field_id' => $this->_ids
['price_field'][0],
3361 'financial_type_id' => 1,
3362 'price_field_value_id' => $this->_ids
['price_field_value'][0],
3363 'membership_type_id' => $this->_ids
['membership_type'],
3366 //Update it to Failed.
3367 $form->_params
['id'] = $contribution['id'];
3368 $form->_params
['contribution_status_id'] = 4;
3370 $form->testSubmit($form->_params
, CRM_Core_Action
::UPDATE
);
3371 //Existing membership should not get updated to expired.
3372 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3373 $this->assertNotEquals($membership['status_id'], 4);
3377 * Test membership is renewed when transaction completed.
3379 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3380 $this->createPriceSetWithPage('membership');
3381 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
3382 $this->callAPISuccess('contribution', 'completetransaction', ['id' => $this->_ids
['contribution']]);
3383 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids
['membership']]);
3384 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3385 $this->cleanUpAfterPriceSets();
3388 public function cleanUpAfterPriceSets() {
3389 $this->quickCleanUpFinancialEntities();
3390 $this->contactDelete($this->_ids
['contact']);
3394 * Set up a pending transaction with a specific price field id.
3396 * @param int $priceFieldValueID
3397 * @param array $contriParams
3399 public function setUpPendingContribution($priceFieldValueID, $contriParams = []) {
3400 $contactID = $this->individualCreate();
3401 $membership = $this->callAPISuccess('membership', 'create', [
3402 'contact_id' => $contactID,
3403 'membership_type_id' => $this->_ids
['membership_type'],
3404 'start_date' => 'yesterday - 1 year',
3405 'end_date' => 'yesterday',
3406 'join_date' => 'yesterday - 1 year',
3408 $contribution = $this->callAPISuccess('contribution', 'create', array_merge([
3410 'contact_id' => $contactID,
3411 'receive_date' => date('Ymd'),
3412 'total_amount' => 20.00,
3413 'financial_type_id' => 1,
3414 'payment_instrument_id' => 'Credit Card',
3415 'non_deductible_amount' => 10.00,
3416 'trxn_id' => 'jdhfi' . rand(1, 100),
3417 'invoice_id' => 'djfhiew' . rand(5, 100),
3419 'contribution_status_id' => 2,
3420 'contribution_page_id' => $this->_ids
['contribution_page'],
3421 'api.membership_payment.create' => ['membership_id' => $membership['id']],
3424 $this->callAPISuccess('line_item', 'create', [
3425 'entity_id' => $contribution['id'],
3426 'entity_table' => 'civicrm_contribution',
3427 'contribution_id' => $contribution['id'],
3428 'price_field_id' => $this->_ids
['price_field'][0],
3432 'financial_type_id' => 1,
3433 'price_field_value_id' => $priceFieldValueID,
3435 $this->_ids
['contact'] = $contactID;
3436 $this->_ids
['contribution'] = $contribution['id'];
3437 $this->_ids
['membership'] = $membership['id'];
3441 * Test sending a mail via the API.
3443 * @throws \CRM_Core_Exception
3444 * @throws \CiviCRM_API3_Exception
3446 public function testSendMail() {
3447 $mut = new CiviMailUtils($this, TRUE);
3448 $orderParams = $this->_params
;
3449 $orderParams['contribution_status_id'] = 'Pending';
3450 $orderParams['api.PaymentProcessor.pay'] = [
3451 'payment_processor_id' => $this->paymentProcessorID
,
3452 'credit_card_type' => 'Visa',
3453 'credit_card_number' => 41111111111111,
3457 $order = $this->callAPISuccess('Order', 'create', $orderParams);
3458 $this->callAPISuccess('Payment', 'create', ['total_amount' => 5, 'is_send_notification' => 0, 'order_id' => $order['id']]);
3459 $address = $this->callAPISuccess('Address', 'create', ['contribution_id' => $order['id'], 'name' => 'bob', 'contact_id' => 1, 'street_address' => 'blah']);
3460 $this->callAPISuccess('Contribution', 'create', ['id' => $order['id'], 'address_id' => $address['id']]);
3461 $this->callAPISuccess('contribution', 'sendconfirmation', [
3462 'id' => $order['id'],
3463 'receipt_from_email' => 'api@civicrm.org',
3465 $mut->checkMailLog([
3467 'Contribution Information',
3472 $this->checkCreditCardDetails($mut, $order['id']);
3474 $tplVars = CRM_Core_Smarty
::singleton()->get_template_vars();
3475 $this->assertEquals('bob', $tplVars['billingName']);
3476 $this->assertEquals("bob\nblah\n", $tplVars['address']);
3480 * Test sending a mail via the API.
3481 * This simulates webform_civicrm using pay later contribution page
3483 public function testSendconfirmationPayLater() {
3484 $mut = new CiviMailUtils($this, TRUE);
3486 // Create contribution page
3488 'title' => 'Webform Contributions',
3489 'financial_type_id' => 1,
3490 'contribution_type_id' => 1,
3491 'is_confirm_enabled' => 1,
3492 'is_pay_later' => 1,
3493 'pay_later_text' => 'I will send payment by cheque',
3494 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3496 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3498 // Create pay later contribution
3500 'contact_id' => $this->_individualId
,
3501 'financial_type_id' => 1,
3502 'is_pay_later' => 1,
3503 'contribution_status_id' => 2,
3504 'contribution_page_id' => $contributionPage['id'],
3505 'total_amount' => '10.00',
3507 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3511 'contribution_id' => $contribution['id'],
3512 'entity_id' => $contribution['id'],
3513 'entity_table' => 'civicrm_contribution',
3514 'label' => 'My lineitem label',
3516 'unit_price' => "10.00",
3517 'line_total' => "10.00",
3519 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3523 civicrm_api3('contribution', 'sendconfirmation', [
3524 'id' => $contribution['id'],
3525 'receipt_from_email' => 'api@civicrm.org',
3528 catch (Exception
$e) {
3529 // Need to figure out how to stop this some other day
3530 // We don't care about the Payment Processor because this is Pay Later
3531 // The point of this test is to check we get the pay_later version of the mail
3532 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3537 // Retrieve mail & check it has the pay_later_receipt info
3538 $mut->getMostRecentEmail('raw');
3539 $mut->checkMailLog([
3540 (string) $contribParams['total_amount'],
3541 $pageParams['pay_later_receipt'],
3549 * Check credit card details in sent mail via API
3551 * @param CiviMailUtils $mut
3552 * @param int $contributionID Contribution ID
3554 * @throws \CRM_Core_Exception
3556 public function checkCreditCardDetails($mut, $contributionID) {
3557 $this->callAPISuccess('contribution', 'create', $this->_params
);
3558 $this->callAPISuccess('contribution', 'sendconfirmation', [
3559 'id' => $contributionID,
3560 'receipt_from_email' => 'api@civicrm.org',
3561 'payment_processor_id' => $this->paymentProcessorID
,
3563 $mut->checkMailLog([
3565 'Billing Name and Address',
3567 'anthony_anderson@civicrm.org',
3574 * Test sending a mail via the API.
3576 public function testSendMailEvent() {
3577 $mut = new CiviMailUtils($this, TRUE);
3578 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
3579 $event = $this->eventCreate([
3580 'is_email_confirm' => 1,
3581 'confirm_from_email' => 'test@civicrm.org',
3583 $this->_eventID
= $event['id'];
3584 $participantParams = [
3585 'contact_id' => $this->_individualId
,
3586 'event_id' => $this->_eventID
,
3589 // to ensure it matches later on
3590 'register_date' => '2007-07-21 00:00:00',
3591 'source' => 'Online Event Registration: API Testing',
3594 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3595 $this->callAPISuccess('participant_payment', 'create', [
3596 'participant_id' => $participant['id'],
3597 'contribution_id' => $contribution['id'],
3599 $this->callAPISuccess('contribution', 'sendconfirmation', [
3600 'id' => $contribution['id'],
3601 'receipt_from_email' => 'api@civicrm.org',
3604 $mut->checkMailLog([
3605 'Annual CiviCRM meet',
3607 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3613 * This function does a GET & compares the result against the $params.
3615 * Use as a double check on Creates.
3617 * @param array $params
3619 * @param bool $delete
3621 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3623 $contribution = $this->callAPISuccess('Contribution', 'Get', [
3628 $this->callAPISuccess('contribution', 'delete', ['id' => $id]);
3630 $this->assertAPISuccess($contribution, 0);
3631 $values = $contribution['values'][$contribution['id']];
3632 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3633 // this is not returned in id format
3634 unset($params['payment_instrument_id']);
3635 $params['contribution_source'] = $params['source'];
3636 unset($params['source']);
3637 foreach ($params as $key => $value) {
3638 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3643 * Create a pending contribution & linked pending pledge record.
3645 public function createPendingPledgeContribution() {
3647 $pledgeID = $this->pledgeCreate(['contact_id' => $this->_individualId
, 'installments' => 1, 'amount' => 500]);
3648 $this->_ids
['pledge'] = $pledgeID;
3649 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, [
3650 'contribution_status_id' => 'Pending',
3651 'total_amount' => 500,
3653 $paymentID = $this->callAPISuccessGetValue('PledgePayment', [
3654 'options' => ['limit' => 1],
3657 $this->callAPISuccess('PledgePayment', 'create', [
3659 'contribution_id' =>
3660 $contribution['id'],
3661 'status_id' => 'Pending',
3662 'scheduled_amount' => 500,
3665 return $contribution['id'];
3669 * Create a pending contribution & linked pending participant record (along with an event).
3671 public function createPendingParticipantContribution() {
3672 $this->_ids
['event']['test'] = $this->eventCreate(['is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'])['id'];
3673 $participantID = $this->participantCreate(['event_id' => $this->_ids
['event']['test'], 'status_id' => 6, 'contact_id' => $this->_individualId
]);
3674 $this->_ids
['participant'] = $participantID;
3675 $params = array_merge($this->_params
, ['contact_id' => $this->_individualId
, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee']);
3676 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3677 $this->callAPISuccess('participant_payment', 'create', [
3678 'contribution_id' => $contribution['id'],
3679 'participant_id' => $participantID,
3681 $this->callAPISuccess('line_item', 'get', [
3682 'entity_id' => $contribution['id'],
3683 'entity_table' => 'civicrm_contribution',
3684 'api.line_item.create' => [
3685 'entity_id' => $participantID,
3686 'entity_table' => 'civicrm_participant',
3689 return $contribution['id'];
3693 * Get financial transaction amount.
3695 * @param int $contId
3697 * @return null|string
3699 public function _getFinancialTrxnAmount($contId) {
3701 SUM( ft.total_amount ) AS total
3702 FROM civicrm_financial_trxn AS ft
3703 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3704 WHERE ceft.entity_table = 'civicrm_contribution'
3705 AND ceft.entity_id = {$contId}";
3707 $result = CRM_Core_DAO
::singleValueQuery($query);
3712 * @param int $contId
3714 * @return null|string
3716 public function _getFinancialItemAmount($contId) {
3717 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3720 FROM civicrm_financial_item
3721 WHERE entity_table = 'civicrm_line_item'
3722 AND entity_id = {$lineItem}";
3723 $result = CRM_Core_DAO
::singleValueQuery($query);
3728 * @param int $contId
3731 public function _checkFinancialItem($contId, $context) {
3732 if ($context != 'paylater') {
3734 'entity_id' => $contId,
3735 'entity_table' => 'civicrm_contribution',
3737 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
3739 'financial_trxn_id' => $trxn['financial_trxn_id'],
3740 'entity_table' => 'civicrm_financial_item',
3742 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3744 'id' => $entityTrxn['entity_id'],
3747 if ($context == 'paylater') {
3748 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
3749 foreach ($lineItems as $key => $item) {
3751 'entity_id' => $key,
3752 'entity_table' => 'civicrm_line_item',
3754 $compareParams = ['status_id' => 1];
3755 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3758 elseif ($context == 'refund') {
3761 'financial_account_id' => 1,
3765 elseif ($context == 'cancelPending') {
3768 'financial_account_id' => 1,
3772 elseif ($context == 'changeFinancial') {
3773 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
3775 'entity_id' => $lineKey,
3779 'financial_account_id' => 1,
3781 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3783 'financial_account_id' => 3,
3784 'entity_id' => $lineKey,
3790 if ($context != 'paylater') {
3791 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3796 * Check correct financial transaction entries were created for the change in payment instrument.
3798 * @param int $contributionID
3799 * @param int $originalInstrumentID
3800 * @param int $newInstrumentID
3801 * @param int $amount
3803 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3805 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3807 $originalTrxnParams = [
3808 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3809 'payment_instrument_id' => $originalInstrumentID,
3810 'amount' => $amount,
3814 $reversalTrxnParams = [
3815 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($originalInstrumentID),
3816 'payment_instrument_id' => $originalInstrumentID,
3817 'amount' => -$amount,
3822 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($newInstrumentID),
3823 'payment_instrument_id' => $newInstrumentID,
3824 'amount' => $amount,
3828 foreach ([$originalTrxnParams, $reversalTrxnParams, $newTrxnParams] as $index => $transaction) {
3829 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3830 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3832 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', [
3833 'id' => $entityFinancialTrxn['financial_trxn_id'],
3835 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3836 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3837 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3838 $this->assertEquals(0, $financialTrxn['fee_amount']);
3839 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3840 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3843 $this->assertEquals(1, $financialTrxn['is_payment']);
3844 $this->assertEquals('USD', $financialTrxn['currency']);
3845 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3850 * Check financial transaction.
3852 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3854 * @param array $contribution
3855 * @param string $context
3856 * @param int $instrumentId
3857 * @param array $extraParams
3859 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = []) {
3860 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3861 $trxn = array_pop($financialTrxns);
3864 'id' => $trxn['financial_trxn_id'],
3866 if ($context == 'payLater') {
3869 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3872 elseif ($context == 'refund') {
3874 'to_financial_account_id' => 6,
3875 'total_amount' => -100,
3877 'trxn_date' => '2015-01-01 09:00:00',
3878 'trxn_id' => 'the refund',
3881 elseif ($context == 'cancelPending') {
3883 'to_financial_account_id' => 7,
3884 'total_amount' => -100,
3888 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
3889 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3890 // It does the same thing with greater readability.
3891 // @todo remove handling for
3894 'entity_id' => $contribution['id'],
3895 'entity_table' => 'civicrm_contribution',
3898 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
3900 'id' => $trxn['financial_trxn_id'],
3902 if (empty($extraParams)) {
3904 'total_amount' => -100,
3910 'total_amount' => 100,
3914 if ($context == 'paymentInstrument') {
3915 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
3916 $compareParams['payment_instrument_id'] = $instrumentId;
3919 $compareParams['to_financial_account_id'] = 12;
3921 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3922 $compareParams['total_amount'] = 100;
3925 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3931 public function _addPaymentInstrument() {
3932 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3934 'option_group_id' => $gId,
3935 'label' => 'Test Card',
3936 'name' => 'Test Card',
3941 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3942 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3943 $financialParams = [
3944 'entity_table' => 'civicrm_option_value',
3945 'entity_id' => $optionValue['id'],
3946 'account_relationship' => $relationTypeId,
3947 'financial_account_id' => 7,
3949 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams);
3950 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3951 return $optionValue['values'][$optionValue['id']]['value'];
3954 public function _deletedAddedPaymentInstrument() {
3955 $result = $this->callAPISuccess('OptionValue', 'get', [
3956 'option_group_id' => 'payment_instrument',
3957 'name' => 'Test Card',
3961 if ($id = CRM_Utils_Array
::value('id', $result)) {
3962 $this->callAPISuccess('OptionValue', 'delete', ['id' => $id]);
3967 * Set up the basic recurring contribution for tests.
3969 * @param array $generalParams
3970 * Parameters that can be merged into the recurring AND the contribution.
3972 * @param array $recurParams
3973 * Parameters to merge into the recur only.
3977 protected function setUpRecurringContribution($generalParams = [], $recurParams = []) {
3978 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
3979 'contact_id' => $this->_individualId
,
3980 'installments' => '12',
3981 'frequency_interval' => '1',
3983 'contribution_status_id' => 1,
3984 'start_date' => '2012-01-01 00:00:00',
3985 'currency' => 'USD',
3986 'frequency_unit' => 'month',
3987 'payment_processor_id' => $this->paymentProcessorID
,
3988 ], $generalParams, $recurParams));
3989 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3992 'contribution_recur_id' => $contributionRecur['id'],
3995 return $originalContribution;
3999 * Set up a basic auto-renew membership for tests.
4001 * @param array $generalParams
4002 * Parameters that can be merged into the recurring AND the contribution.
4004 * @param array $recurParams
4005 * Parameters to merge into the recur only.
4009 protected function setUpAutoRenewMembership($generalParams = [], $recurParams = []) {
4010 $newContact = $this->callAPISuccess('Contact', 'create', [
4011 'contact_type' => 'Individual',
4012 'sort_name' => 'McTesterson, Testy',
4013 'display_name' => 'Testy McTesterson',
4014 'preferred_language' => 'en_US',
4015 'preferred_mail_format' => 'Both',
4016 'first_name' => 'Testy',
4017 'last_name' => 'McTesterson',
4018 'contact_is_deleted' => '0',
4020 'email' => 'tmctesterson@example.com',
4023 $membershipType = $this->callAPISuccess('MembershipType', 'create', [
4024 'domain_id' => "Default Domain Name",
4025 'member_of_contact_id' => 1,
4026 'financial_type_id' => "Member Dues",
4027 'duration_unit' => "month",
4028 'duration_interval' => 1,
4029 'period_type' => 'rolling',
4030 'name' => "Standard Member",
4031 'minimum_fee' => 100,
4033 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
4034 'contact_id' => $newContact['id'],
4035 'installments' => '12',
4036 'frequency_interval' => '1',
4038 'contribution_status_id' => 1,
4039 'start_date' => '2012-01-01 00:00:00',
4040 'currency' => 'USD',
4041 'frequency_unit' => 'month',
4042 'payment_processor_id' => $this->paymentProcessorID
,
4043 ], $generalParams, $recurParams));
4045 $membership = $this->callAPISuccess('membership', 'create', [
4046 'contact_id' => $newContact['id'],
4047 'contribution_recur_id' => $contributionRecur['id'],
4048 'financial_type_id' => "Member Dues",
4049 'membership_type_id' => $membershipType['id'],
4051 'skipLineItem' => TRUE,
4054 CRM_Price_BAO_LineItem
::getLineItemArray($this->_params
, NULL, 'membership', $membershipType['id']);
4055 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
4058 'contact_id' => $newContact['id'],
4059 'contribution_recur_id' => $contributionRecur['id'],
4060 'financial_type_id' => "Member Dues",
4061 'contribution_status_id' => 1,
4062 'invoice_id' => uniqid(),
4065 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', []);
4066 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
4067 $membership = $this->callAPISuccess('Membership', 'getsingle', ['id' => $lineItem['entity_id']]);
4068 $this->callAPISuccess('LineItem', 'getsingle', []);
4069 $this->callAPISuccessGetCount('MembershipPayment', ['membership_id' => $membership['id']], 1);
4071 return [$originalContribution, $membership];
4075 * Set up a repeat transaction.
4077 * @param array $recurParams
4078 * @param mixed $flag
4079 * @param array $contributionParams
4082 protected function setUpRepeatTransaction($recurParams = [], $flag, $contributionParams = []) {
4083 $paymentProcessorID = $this->paymentProcessorCreate();
4084 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
4085 'contact_id' => $this->_individualId
,
4086 'installments' => '12',
4087 'frequency_interval' => '1',
4089 'contribution_status_id' => 1,
4090 'start_date' => '2012-01-01 00:00:00',
4091 'currency' => 'USD',
4092 'frequency_unit' => 'month',
4093 'payment_processor_id' => $paymentProcessorID,
4096 $originalContribution = '';
4097 if ($flag == 'multiple') {
4098 // CRM-19309 create a contribution + also add in line_items (plural):
4099 $params = array_merge($this->_params
, $contributionParams);
4100 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
4103 'contribution_recur_id' => $contributionRecur['id'],
4104 'skipLineItem' => 1,
4105 'api.line_item.create' => [
4107 'price_field_id' => 1,
4109 'line_total' => '20',
4110 'unit_price' => '10',
4111 'financial_type_id' => 1,
4114 'price_field_id' => 1,
4116 'line_total' => '80',
4117 'unit_price' => '80',
4118 'financial_type_id' => 2,
4125 elseif ($flag == 'single') {
4126 $params = array_merge($this->_params
, ['contribution_recur_id' => $contributionRecur['id']]);
4127 $params = array_merge($params, $contributionParams);
4128 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
4130 $originalContribution['payment_processor_id'] = $paymentProcessorID;
4131 return $originalContribution;
4135 * Common set up routine.
4139 protected function setUpForCompleteTransaction() {
4140 $this->mut
= new CiviMailUtils($this, TRUE);
4141 $this->createLoggedInUser();
4142 $params = array_merge($this->_params
, ['contribution_status_id' => 2, 'receipt_date' => 'now']);
4143 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4144 return $contribution;
4148 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
4150 public function testRepeatTransactionWithNonCreditCardDefault() {
4151 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
4152 'contact_id' => $this->_individualId
,
4153 'installments' => '12',
4154 'frequency_interval' => '1',
4156 'contribution_status_id' => 1,
4157 'start_date' => '2012-01-01 00:00:00',
4158 'currency' => 'USD',
4159 'frequency_unit' => 'month',
4160 'payment_processor_id' => $this->paymentProcessorID
,
4162 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
4164 ['contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2])
4166 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument('name');
4167 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', [
4168 'contribution_status_id' => 'Completed',
4169 'trxn_id' => uniqid(),
4170 'original_contribution_id' => $contribution1,
4172 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
4173 $this->quickCleanUpFinancialEntities();
4177 * CRM-20008 Tests repeattransaction creates pending membership.
4179 public function testRepeatTransactionMembershipCreatePendingContribution() {
4180 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
4181 $this->callAPISuccess('membership', 'create', [
4182 'id' => $membership['id'],
4183 'end_date' => 'yesterday',
4184 'status_id' => 'Expired',
4186 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', [
4187 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
4188 'contribution_status_id' => 'Pending',
4189 'trxn_id' => uniqid(),
4191 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', [
4192 'id' => $membership['id'],
4193 'return' => 'status_id',
4196 // Let's see if the membership payments got created while we're at it.
4197 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', [
4198 'membership_id' => $membership['id'],
4200 $this->assertEquals(2, $membershipPayments['count']);
4202 $this->assertEquals('Expired', CRM_Core_PseudoConstant
::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
4203 $this->callAPISuccess('Contribution', 'completetransaction', ['id' => $repeatedContribution['id']]);
4204 $membership = $this->callAPISuccessGetSingle('membership', [
4205 'id' => $membership['id'],
4206 'return' => 'status_id, end_date',
4208 $this->assertEquals('New', CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
4209 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
4211 $this->quickCleanUpFinancialEntities();
4212 $this->contactDelete($originalContribution['values'][1]['contact_id']);
4216 * Test sending a mail via the API.
4218 public function testSendMailWithAPISetFromDetails() {
4219 $mut = new CiviMailUtils($this, TRUE);
4220 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
4221 $this->callAPISuccess('contribution', 'sendconfirmation', [
4222 'id' => $contribution['id'],
4223 'receipt_from_email' => 'api@civicrm.org',
4224 'receipt_from_name' => 'CiviCRM LLC',
4226 $mut->checkMailLog([
4227 'From: CiviCRM LLC <api@civicrm.org>',
4228 'Contribution Information',
4236 * Test sending a mail via the API.
4238 public function testSendMailWithNoFromSetFallToDomain() {
4239 $this->createLoggedInUser();
4240 $mut = new CiviMailUtils($this, TRUE);
4241 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
4242 $this->callAPISuccess('contribution', 'sendconfirmation', [
4243 'id' => $contribution['id'],
4245 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => 1]);
4246 $mut->checkMailLog([
4247 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4248 'Contribution Information',
4256 * Test sending a mail via the API.
4258 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
4259 $this->createLoggedInUser();
4260 $mut = new CiviMailUtils($this, TRUE);
4261 $contribution = $this->setUpRepeatTransaction([], 'single');
4262 $this->callAPISuccess('contribution', 'repeattransaction', [
4263 'contribution_status_id' => 'Completed',
4264 'trxn_id' => uniqid(),
4265 'original_contribution_id' => $contribution,
4267 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => 1]);
4268 $mut->checkMailLog([
4269 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4270 'Contribution Information',
4279 * Test sending a mail via the API.
4281 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
4282 $mut = new CiviMailUtils($this, TRUE);
4283 $contributionPage = $this->contributionPageCreate(['receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1]);
4284 $paymentProcessorID = $this->paymentProcessorCreate();
4285 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
4286 'contact_id' => $this->_individualId
,
4287 'installments' => '12',
4288 'frequency_interval' => '1',
4290 'contribution_status_id' => 1,
4291 'start_date' => '2012-01-01 00:00:00',
4292 'currency' => 'USD',
4293 'frequency_unit' => 'month',
4294 'payment_processor_id' => $paymentProcessorID,
4296 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
4299 'contribution_recur_id' => $contributionRecur['id'],
4300 'contribution_page_id' => $contributionPage['id'],
4303 $this->callAPISuccess('contribution', 'repeattransaction', [
4304 'contribution_status_id' => 'Completed',
4305 'trxn_id' => uniqid(),
4306 'original_contribution_id' => $originalContribution,
4309 $mut->checkMailLog([
4310 'From: CiviCRM LLC <contributionpage@civicrm.org>',
4311 'Contribution Information',
4319 * Test sending a mail via the API.
4321 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4322 $mut = new CiviMailUtils($this, TRUE);
4323 $originalContribution = $contribution = $this->setUpRepeatTransaction([], 'single');
4324 $fromEmail = $this->CallAPISuccess('optionValue', 'get', ['is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1]);
4325 foreach ($fromEmail['values'] as $from) {
4326 $this->callAPISuccess('optionValue', 'create', ['is_default' => 0, 'id' => $from['id']]);
4328 $domain = $this->callAPISuccess('domain', 'getsingle', ['id' => CRM_Core_Config
::domainID()]);
4329 $this->callAPISuccess('contribution', 'repeattransaction', [
4330 'contribution_status_id' => 'Completed',
4331 'trxn_id' => uniqid(),
4332 'original_contribution_id' => $originalContribution,
4334 $mut->checkMailLog([
4335 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4336 'Contribution Information',
4344 * Create a Contribution Page with is_email_receipt = TRUE.
4346 * @param array $params
4347 * Params to overwrite with.
4351 protected function createReceiptableContributionPage($params = []) {
4352 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge([
4353 'receipt_from_name' => 'Mickey Mouse',
4354 'receipt_from_email' => 'mickey@mouse.com',
4355 'title' => "Test Contribution Page",
4356 'financial_type_id' => 1,
4357 'currency' => 'CAD',
4358 'is_monetary' => TRUE,
4359 'is_email_receipt' => TRUE,
4361 return $contributionPage;
4365 * function to test card_type and pan truncation.
4367 public function testCardTypeAndPanTruncation() {
4368 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'));
4369 $contactId = $this->individualCreate();
4371 'contact_id' => $contactId,
4372 'receive_date' => '2016-01-20',
4373 'total_amount' => 100,
4374 'financial_type_id' => 1,
4375 'payment_instrument' => 'Credit Card',
4376 'card_type_id' => $creditCardTypeIDs['Visa'],
4377 'pan_truncation' => 4567,
4379 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4380 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
4381 $financialTrxn = $this->callAPISuccessGetSingle(
4384 'id' => $lastFinancialTrxnId['financialTrxnId'],
4385 'return' => ['card_type_id', 'pan_truncation'],
4388 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4389 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 4567);
4391 'id' => $contribution['id'],
4392 'pan_truncation' => 2345,
4393 'card_type_id' => $creditCardTypeIDs['Amex'],
4395 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4396 $financialTrxn = $this->callAPISuccessGetSingle(
4399 'id' => $lastFinancialTrxnId['financialTrxnId'],
4400 'return' => ['card_type_id', 'pan_truncation'],
4403 $this->assertEquals(CRM_Utils_Array
::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4404 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 2345);
4408 * Test repeat contribution uses non default currency
4409 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4411 public function testRepeatTransactionWithDifferenceCurrency() {
4412 $originalContribution = $this->setUpRepeatTransaction(['currency' => 'AUD'], 'single', ['currency' => 'AUD']);
4413 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', [
4414 'original_contribution_id' => $originalContribution['id'],
4415 'contribution_status_id' => 'Completed',
4416 'trxn_id' => uniqid(),
4418 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4422 * Get the financial items for the contribution.
4424 * @param int $contributionID
4427 * Array of associated financial items.
4429 protected function getFinancialTransactionsForContribution($contributionID) {
4431 'entity_id' => $contributionID,
4432 'entity_table' => 'civicrm_contribution',
4434 // @todo the following function has naming errors & has a weird signature & appears to
4435 // only be called from test classes. Move into test suite & maybe just use api
4436 // from this function.
4437 return array_merge(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, FALSE, []));
4441 * Test getunique api call for Contribution entity
4443 public function testContributionGetUnique() {
4444 $result = $this->callAPIAndDocument($this->_entity
, 'getunique', [], __FUNCTION__
, __FILE__
);
4445 $this->assertEquals(2, $result['count']);
4446 $this->assertEquals(['trxn_id'], $result['values']['UI_contrib_trxn_id']);
4447 $this->assertEquals(['invoice_id'], $result['values']['UI_contrib_invoice_id']);
4451 * Test Repeat Transaction Contribution with Tax amount.
4452 * https://lab.civicrm.org/dev/core/issues/806
4454 public function testRepeatContributionWithTaxAmount() {
4455 $this->enableTaxAndInvoicing();
4456 $financialType = $this->callAPISuccess('financial_type', 'create', [
4457 'name' => 'Test taxable financial Type',
4461 $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
4462 $contribution = $this->setUpRepeatTransaction(
4466 'financial_type_id' => $financialType['id'],
4469 $this->callAPISuccess('contribution', 'repeattransaction', [
4470 'original_contribution_id' => $contribution['id'],
4471 'contribution_status_id' => 'Completed',
4472 'trxn_id' => uniqid(),
4474 $payments = $this->callAPISuccess('Contribution', 'get', ['sequential' => 1])['values'];
4475 //Assert if first payment and repeated payment has the same contribution amount.
4476 $this->assertEquals($payments[0]['total_amount'], $payments[1]['total_amount']);
4477 $this->callAPISuccessGetCount('Contribution', [], 2);
4479 //Assert line item records.
4480 $lineItems = $this->callAPISuccess('LineItem', 'get', ['sequential' => 1])['values'];
4481 foreach ($lineItems as $lineItem) {
4482 $this->assertEquals($lineItem['unit_price'], $this->_params
['total_amount']);
4483 $this->assertEquals($lineItem['line_total'], $this->_params
['total_amount']);
4485 $this->callAPISuccessGetCount('Contribution', [], 2);
4488 public function testGetCurrencyOptions() {
4489 $result = $this->callAPISuccess('Contribution', 'getoptions', [
4490 'field' => 'currency',
4492 $this->assertEquals('US Dollar', $result['values']['USD']);
4493 $this->assertNotContains('$', $result['values']);
4494 $result = $this->callAPISuccess('Contribution', 'getoptions', [
4495 'field' => 'currency',
4496 'context' => "abbreviate",
4498 $this->assertEquals('$', $result['values']['USD']);
4499 $this->assertNotContains('US Dollar', $result['values']);
4502 public function testSetCustomDataInCreateAndHook() {
4503 $this->createCustomGroupWithFieldsOfAllTypes();
4504 $this->hookClass
->setHook('civicrm_post', [
4506 'civicrmPostContributionCustom',
4508 $params = $this->_params
;
4509 $params['custom_' . $this->ids
['CustomField']['text']] = 'Some Text';
4510 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
4511 $getContribution = $this->callAPISuccess('Contribution', 'get', [
4512 'id' => $contribution['id'],
4513 'return' => ['id', 'custom_' . $this->ids
['CustomField']['text'], 'custom_' . $this->ids
['CustomField']['int']],
4515 $this->assertEquals(5, $getContribution['values'][$contribution['id']]['custom_' . $this->ids
['CustomField']['int']]);
4516 $this->assertEquals('Some Text', $getContribution['values'][$contribution['id']]['custom_' . $this->ids
['CustomField']['text']]);
4519 public function civicrmPostContributionCustom($op, $objectName, $objectId, &$objectRef) {
4520 if ($objectName === 'Contribution' && $op === 'create') {
4521 $this->callAPISuccess('Contribution', 'create', [
4523 'custom_' . $this->ids
['CustomField']['int'] => 5,